aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2017-12-21 15:07:51 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-12-21 15:10:13 -0800
commit6a54339bb943702bd7dffc43f85267dac98dc355 (patch)
treea8c66b38c64d3f67833b83cd7a814231ae920069 /src/main/java/com/google/devtools/build/lib/vfs
parent9ccafc68eff8485f7abedf35e6130d564f5c118e (diff)
Call through to Path#createDirectoryAndParents from FileUtils.
This CL removes a method that due to its implementation causes threading difficulties for Path#createDirectory. The tests for the method are brought across to FileSystemTests since the methods are now implemented natively by the FileSystem classes. The tests were also cleaned up. The test revealed an edge case bug in JavaIoFileSystem, so fix this. In two cases some code was using the return value from the old method. Returning "false" essentially means that the directory already existed, and the code doesn't look racy, so we can replace it with an existence check. PiperOrigin-RevId: 179864042
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java43
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java10
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/UnionFileSystem.java2
3 files changed, 16 insertions, 39 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
index e42abdfd8a..adeb9c847c 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
@@ -616,46 +616,15 @@ public class FileSystemUtils {
}
/**
- * Attempts to create a directory with the name of the given path, creating
- * ancestors as necessary.
+ * Attempts to create a directory with the name of the given path, creating ancestors as
+ * necessary.
*
- * <p>Postcondition: completes normally iff {@code dir} denotes an existing
- * directory (not necessarily canonical); completes abruptly otherwise.
- *
- * @return true if the directory was successfully created anew, false if it
- * already existed (including the case where {@code dir} denotes a symlink
- * to an existing directory)
- * @throws IOException if the directory could not be created
+ * <p>Deprecated. Prefer to call {@link Path#createDirectoryAndParents()} directly.
*/
+ @Deprecated
@ThreadSafe
- public static boolean createDirectoryAndParents(Path dir) throws IOException {
- // Optimised for minimal number of I/O calls.
-
- // Don't attempt to create the root directory.
- if (dir.getParentDirectory() == null) {
- return false;
- }
-
- FileSystem filesystem = dir.getFileSystem();
- if (filesystem instanceof UnionFileSystem) {
- // If using UnionFS, make sure that we do not traverse filesystem boundaries when creating
- // parent directories by rehoming the path on the most specific filesystem.
- FileSystem delegate = ((UnionFileSystem) filesystem).getDelegate(dir);
- dir = delegate.getPath(dir.asFragment());
- }
-
- try {
- return dir.createDirectory();
- } catch (IOException e) {
- if (e.getMessage().endsWith(" (No such file or directory)")) { // ENOENT
- createDirectoryAndParents(dir.getParentDirectory());
- return dir.createDirectory();
- } else if (e.getMessage().endsWith(" (File exists)") && dir.isDirectory()) { // EEXIST
- return false;
- } else {
- throw e; // some other error (e.g. ENOTDIR, EACCES, etc.)
- }
- }
+ public static void createDirectoryAndParents(Path dir) throws IOException {
+ dir.createDirectoryAndParents();
}
/**
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
index 4477275f1f..0dac1c4725 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
@@ -261,7 +261,15 @@ public class JavaIoFileSystem extends AbstractFileSystemWithCustomStat {
@Override
public void createDirectoryAndParents(Path path) throws IOException {
java.nio.file.Path nioPath = getNioPath(path);
- Files.createDirectories(nioPath);
+ try {
+ Files.createDirectories(nioPath);
+ } catch (java.nio.file.FileAlreadyExistsException e) {
+ // Files.createDirectories will handle this case normally, but if the existing
+ // file is a symlink to a directory then it still throws. Swallow this.
+ if (!path.isDirectory()) {
+ throw e;
+ }
+ }
}
private boolean linkExists(File file) {
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/UnionFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/UnionFileSystem.java
index 569357bcb2..d924e19d45 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/UnionFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/UnionFileSystem.java
@@ -209,7 +209,7 @@ public class UnionFileSystem extends FileSystem {
public void createDirectoryAndParents(Path path) throws IOException {
checkModifiable(path);
FileSystem delegate = getDelegate(path);
- delegate.createDirectoryAndParents(path);
+ delegate.createDirectoryAndParents(adjustPath(path, delegate));
}
@Override