Replace usages of File.mkdir() with Java 7's Files.createDirectories().
Summary: We favour using the Java 7 APIs where possible, so switch to using them.
Test Plan: buck test --all
diff --git a/src/com/facebook/buck/event/listener/JavaUtilsLoggingBuildListener.java b/src/com/facebook/buck/event/listener/JavaUtilsLoggingBuildListener.java
index 0c80db1..9140f65 100644
--- a/src/com/facebook/buck/event/listener/JavaUtilsLoggingBuildListener.java
+++ b/src/com/facebook/buck/event/listener/JavaUtilsLoggingBuildListener.java
@@ -24,8 +24,10 @@
import com.google.common.base.Throwables;
import com.google.common.eventbus.Subscribe;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
@@ -46,9 +48,12 @@
public static void ensureLogFileIsWritten() {
try {
- File dir = new File(BuckConstant.BIN_DIR);
- if (!dir.exists() && !dir.mkdirs()) {
- throw new HumanReadableException("Unable to create output directory: " + dir);
+ Path dir = Paths.get(BuckConstant.BIN_DIR);
+
+ try {
+ Files.createDirectories(dir);
+ } catch (IOException e) {
+ throw new HumanReadableException(e, "Unable to create output directory: " + dir);
}
FileHandler handler = new FileHandler(
diff --git a/src/com/facebook/buck/rules/DirArtifactCache.java b/src/com/facebook/buck/rules/DirArtifactCache.java
index f41a60a..e244e25 100644
--- a/src/com/facebook/buck/rules/DirArtifactCache.java
+++ b/src/com/facebook/buck/rules/DirArtifactCache.java
@@ -16,11 +16,15 @@
package com.facebook.buck.rules;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+
import com.google.common.base.Preconditions;
-import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.logging.Level;
import java.util.logging.Logger;
public class DirArtifactCache implements ArtifactCache {
@@ -30,11 +34,7 @@
public DirArtifactCache(File cacheDir) throws IOException {
this.cacheDir = Preconditions.checkNotNull(cacheDir);
- Files.createParentDirs(cacheDir);
- if (!cacheDir.mkdir() && !cacheDir.exists()) {
- throw new IOException(String.format("Failed to create cache directory: \"%s\"",
- cacheDir.getPath()));
- }
+ Files.createDirectories(cacheDir.toPath());
}
@Override
@@ -43,8 +43,8 @@
File cacheEntry = new File(cacheDir, ruleKey.toString());
if (cacheEntry.exists()) {
try {
- Files.createParentDirs(output);
- Files.copy(cacheEntry, output);
+ Files.createDirectories(output.toPath().getParent());
+ Files.copy(cacheEntry.toPath(), output.toPath(), REPLACE_EXISTING);
success = CacheResult.DIR_HIT;
} catch (IOException e) {
logger.warning(String.format("Artifact fetch(%s, %s) error: %s",
@@ -63,21 +63,26 @@
@Override
public void store(RuleKey ruleKey, File output) {
File cacheEntry = new File(cacheDir, ruleKey.toString());
- File tmpCacheEntry = null;
+ Path tmpCacheEntry = null;
try {
// Write to a temporary file and move the file to its final location atomically to protect
// against partial artifacts (whether due to buck interruption or filesystem failure) posing
// as valid artifacts during subsequent buck runs.
- tmpCacheEntry = File.createTempFile(ruleKey.toString(), ".tmp", cacheDir);
- Files.copy(output, tmpCacheEntry);
- Files.move(tmpCacheEntry, cacheEntry);
+ tmpCacheEntry = File.createTempFile(ruleKey.toString(), ".tmp", cacheDir).toPath();
+ Files.copy(output.toPath(), tmpCacheEntry, REPLACE_EXISTING);
+ Files.move(tmpCacheEntry, cacheEntry.toPath());
} catch (IOException e) {
logger.warning(String.format("Artifact store(%s, %s) error: %s",
ruleKey,
output.getPath(),
e.getMessage()));
if (tmpCacheEntry != null) {
- tmpCacheEntry.delete();
+ try {
+ Files.deleteIfExists(tmpCacheEntry);
+ } catch (IOException ignored) {
+ // Unable to delete a temporary file. Nothing sane to do.
+ logger.log(Level.INFO, "Unable to delete temp cache file", ignored);
+ }
}
}
}
diff --git a/src/com/facebook/buck/util/DefaultFilteredDirectoryCopier.java b/src/com/facebook/buck/util/DefaultFilteredDirectoryCopier.java
index 0da9e28..4a103c0 100644
--- a/src/com/facebook/buck/util/DefaultFilteredDirectoryCopier.java
+++ b/src/com/facebook/buck/util/DefaultFilteredDirectoryCopier.java
@@ -17,10 +17,11 @@
package com.facebook.buck.util;
import com.google.common.base.Predicate;
-import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Map;
/**
@@ -58,22 +59,22 @@
String srcDir,
String destDir,
final Predicate<File> pred) throws IOException {
- final File dest = new File(destDir);
+ final Path dest = MorePaths.absolutify(MorePaths.newPathInstance(destDir));
// Remove existing contents if any.
- if (dest.exists()) {
- MoreFiles.rmdir(java.nio.file.Paths.get(dest.getAbsolutePath()));
+ if (dest.toFile().exists()) {
+ MoreFiles.rmdir(dest);
}
- dest.mkdirs();
+ Files.createDirectories(dest);
// Copy filtered contents.
new DirectoryTraversal(new File(srcDir)) {
@Override
public void visit(File srcFile, String relativePath) throws IOException {
if (pred.apply(srcFile)) {
- File destFile = new File(dest, relativePath);
- Files.createParentDirs(destFile);
- Files.copy(srcFile, destFile);
+ Path destPath = dest.resolve(relativePath);
+ Files.createDirectories(destPath.getParent());
+ Files.copy(srcFile.toPath(), destPath);
}
}
}.traverse();
diff --git a/src/com/facebook/buck/util/ProjectFilesystem.java b/src/com/facebook/buck/util/ProjectFilesystem.java
index bc98a7a..f80ab68 100644
--- a/src/com/facebook/buck/util/ProjectFilesystem.java
+++ b/src/com/facebook/buck/util/ProjectFilesystem.java
@@ -184,7 +184,8 @@
}
/**
- * Resolves the relative path against the project root and then calls {@link File#mkdirs()}.
+ * Resolves the relative path against the project root and then calls
+ * {@link java.nio.file.Files#createDirectories(java.nio.file.Path, java.nio.file.attribute.FileAttribute[])}
*/
public void mkdirs(Path pathRelativeToProjectRoot) throws IOException {
java.nio.file.Files.createDirectories(resolve(pathRelativeToProjectRoot));
diff --git a/src/com/facebook/buck/zip/UnzipStep.java b/src/com/facebook/buck/zip/UnzipStep.java
index 5a521da..2f1c610 100644
--- a/src/com/facebook/buck/zip/UnzipStep.java
+++ b/src/com/facebook/buck/zip/UnzipStep.java
@@ -30,6 +30,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.file.Files;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -137,12 +138,10 @@
String destination,
ImmutableSet<String> filesToExtract,
boolean overwriteExistingFiles) throws IOException {
- // Create output directory is not exists
+ // Create output directory if it does not exist
File folder = new File(destination);
// TODO UnzipStep could be a CompositeStep with a MakeCleanDirectoryStep for the output dir.
- if (!folder.exists() && !folder.mkdirs()) {
- throw new IOException(String.format("Folder %s could not be created.", folder.toString()));
- }
+ Files.createDirectories(folder.toPath());
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
@@ -157,17 +156,10 @@
}
if (entry.isDirectory()) {
// Create the directory and all its parent directories
- if (!target.mkdirs()) {
- throw new IOException(String.format("Folder %s could not be created.",
- target.toString()));
- }
+ Files.createDirectories(target.toPath());
} else {
// Create parent folder
- File parentFolder = target.getParentFile();
- if (!parentFolder.exists() && !parentFolder.mkdirs()) {
- throw new IOException(String.format("Folder %s could not be created.",
- parentFolder.toString()));
- }
+ Files.createDirectories(target.toPath().getParent());
// Write file
try (FileOutputStream out = new FileOutputStream(target)) {
ByteStreams.copy(zip, out);