summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jacob Abrams <satur9nine@gmail.com> 2021-03-08 14:35:44 -0800
committer Jacob Abrams <satur9nine@gmail.com> 2021-03-08 14:35:44 -0800
commit1d5dbcaaf447d1142958428ae57a19815259f066 (patch)
tree520474f8e51e586197ac5ac51145e613505b216b
parent055977a3c956458b1b44d27d6e14e7466bf744c0 (diff)
Make FileUtils#createDir threadsafe
Prior to this change the createDir method would return false if invoked around the same time on two different threads for the same directory. Test: manual Change-Id: I34ba4f645a034f47917054af4b299f916a9cfa67
-rw-r--r--core/java/android/os/FileUtils.java20
1 files changed, 15 insertions, 5 deletions
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index a4d6c3845fbf..0264d2335c4b 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -1235,9 +1235,9 @@ public final class FileUtils {
}
/**
- * Creates a directory with name {@code name} under an existing directory {@code baseDir}.
- * Returns a {@code File} object representing the directory on success, {@code null} on
- * failure.
+ * Creates a directory with name {@code name} under an existing directory {@code baseDir} if it
+ * doesn't exist already. Returns a {@code File} object representing the directory if it exists
+ * and {@code null} if not.
*
* @hide
*/
@@ -1247,13 +1247,23 @@ public final class FileUtils {
return createDir(dir) ? dir : null;
}
- /** @hide */
+ /**
+ * Ensure the given directory exists, creating it if needed. This method is threadsafe.
+ *
+ * @return false if the directory doesn't exist and couldn't be created
+ *
+ * @hide
+ */
public static boolean createDir(File dir) {
+ if (dir.mkdir()) {
+ return true;
+ }
+
if (dir.exists()) {
return dir.isDirectory();
}
- return dir.mkdir();
+ return false;
}
/**