aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-01-05 10:37:58 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-05 10:39:28 -0800
commitcb7689404ef9a69488d64bfc2e0bfb9326e664d6 (patch)
tree1e7454d6d3d5fdccb992f08ea2152b0d5fd4a7e4 /src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
parent80c8ff1819a889814cbe2dae477d7fedce6aa181 (diff)
Automated rollback of commit 6a54339bb943702bd7dffc43f85267dac98dc355.
*** Reason for rollback *** b/71442447 *** Original change description *** 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 "f... *** ROLLBACK_OF=179864042 PiperOrigin-RevId: 180946251
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java43
1 files changed, 37 insertions, 6 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 adeb9c847c..e42abdfd8a 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,15 +616,46 @@ 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>Deprecated. Prefer to call {@link Path#createDirectoryAndParents()} directly.
+ * <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
*/
- @Deprecated
@ThreadSafe
- public static void createDirectoryAndParents(Path dir) throws IOException {
- dir.createDirectoryAndParents();
+ 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.)
+ }
+ }
}
/**