Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1e9d6f
commit 9e8e297
Showing
5 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.utils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.FileAttribute; | ||
import java.nio.file.attribute.PosixFilePermission; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.util.EnumSet; | ||
|
||
/** | ||
* Some utility functions for managing files. | ||
*/ | ||
public final class FileUtils { | ||
|
||
private static final FileAttribute<?>[] POSIX_TMP_DIR_PERMISSIONS = new FileAttribute<?>[] { | ||
PosixFilePermissions.asFileAttribute( | ||
EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE) | ||
) }; | ||
|
||
/** | ||
* Recreates the Elasticsearch temporary directory if it doesn't exist. | ||
* The operating system may have cleaned it up due to inactivity, which | ||
* causes some (machine learning) processes to fail. | ||
* @param tmpDir the path to the temporary directory | ||
*/ | ||
public static void recreateTempDirectoryIfNeeded(Path tmpDir) throws IOException { | ||
if (tmpDir.getFileSystem().supportedFileAttributeViews().contains("posix")) { | ||
Files.createDirectories(tmpDir, POSIX_TMP_DIR_PERMISSIONS); | ||
} else { | ||
Files.createDirectories(tmpDir); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/FileUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.utils; | ||
|
||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
|
||
import org.elasticsearch.core.PathUtilsForTesting; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.nio.file.attribute.PosixFileAttributes; | ||
import java.nio.file.attribute.PosixFilePermission; | ||
import java.util.Set; | ||
|
||
public class FileUtilsTests extends ESTestCase { | ||
|
||
public void test_recreateTempDirectoryIfNeeded_forWindows() throws IOException { | ||
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.windows()); | ||
PathUtilsForTesting.installMock(fileSystem); | ||
|
||
Path tmpDir = fileSystem.getPath("c:\\tmp\\elasticsearch"); | ||
|
||
assertFalse(Files.exists(tmpDir)); | ||
FileUtils.recreateTempDirectoryIfNeeded(tmpDir); | ||
assertTrue(Files.exists(tmpDir)); | ||
|
||
BasicFileAttributes attributes = Files.readAttributes(tmpDir, BasicFileAttributes.class); | ||
assertTrue(attributes.isDirectory()); | ||
} | ||
|
||
public void test_recreateTempDirectoryIfNeeded_forPosix() throws IOException { | ||
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix().toBuilder().setAttributeViews("posix").build()); | ||
PathUtilsForTesting.installMock(fileSystem); | ||
|
||
Path tmpDir = fileSystem.getPath("/tmp/elasticsearch-1234567890"); | ||
|
||
assertFalse(Files.exists(tmpDir)); | ||
FileUtils.recreateTempDirectoryIfNeeded(tmpDir); | ||
assertTrue(Files.exists(tmpDir)); | ||
|
||
PosixFileAttributes attributes = Files.readAttributes(tmpDir, PosixFileAttributes.class); | ||
assertTrue(attributes.isDirectory()); | ||
assertEquals( | ||
attributes.permissions(), | ||
Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE) | ||
); | ||
} | ||
} |