summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2010-12-30 23:10:55 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2011-01-14 17:28:14 +0100
commitc45f2aec56a323a55119de0d17951fdb302f1c72 (patch)
treee730a1c0e65f113e3d37ea890d8937d2fb1b8e6d
parent05ca0c49f90842da9ad81a0d5a5b91e834648073 (diff)
downloadjgit-c45f2aec56a323a55119de0d17951fdb302f1c72.tar.gz
jgit-c45f2aec56a323a55119de0d17951fdb302f1c72.zip
File utility for creating a new empty file
The java.io.File.createNewFile() method for creating new empty files reports failure by returning false. To ease proper checking of return values provide a utility method wrapping createNewFile() throwing IOException on failure. Change-Id: I42a3dc9d8ff70af62e84de396e6a740050afa896 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java15
-rw-r--r--org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties1
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java1
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java23
4 files changed, 40 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
index 4ad4234868..d81e686c14 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
@@ -175,4 +175,19 @@ public class FileUtilTest {
assertTrue(f.delete());
}
+ public void testCreateNewFile() throws IOException {
+ File f = new File(trash, "x");
+ FileUtils.createNewFile(f);
+ assertTrue(f.exists());
+
+ try {
+ FileUtils.createNewFile(f);
+ fail("creation of already existing file must fail");
+ } catch (IOException e) {
+ // expected
+ }
+
+ FileUtils.delete(f);
+ }
+
}
diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
index c8f5920977..3049ae7d09 100644
--- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
+++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
@@ -130,6 +130,7 @@ couldNotWriteFile=Could not write file {0}
countingObjects=Counting objects
createBranchFailedUnknownReason=Create branch failed for unknown reason
createBranchUnexpectedResult=Create branch returned unexpected result {0}
+createNewFileFailed=Could not create new file {0}
credentialPassword=Password
credentialUsername=Username
daemonAlreadyRunning=Daemon already running
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
index 083abe5f02..9718e62945 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
@@ -190,6 +190,7 @@ public class JGitText extends TranslationBundle {
/***/ public String countingObjects;
/***/ public String createBranchFailedUnknownReason;
/***/ public String createBranchUnexpectedResult;
+ /***/ public String createNewFileFailed;
/***/ public String credentialPassword;
/***/ public String credentialUsername;
/***/ public String daemonAlreadyRunning;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
index 568f2882ba..56d20d4ff0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
@@ -47,6 +47,7 @@ package org.eclipse.jgit.util;
import java.io.File;
import java.io.IOException;
+import java.nio.channels.FileLock;
import java.text.MessageFormat;
import org.eclipse.jgit.JGitText;
@@ -219,4 +220,26 @@ public class FileUtils {
JGitText.get().mkDirsFailed, d.getAbsolutePath()));
}
}
+
+ /**
+ * Atomically creates a new, empty file named by this abstract pathname if
+ * and only if a file with this name does not yet exist. The check for the
+ * existence of the file and the creation of the file if it does not exist
+ * are a single operation that is atomic with respect to all other
+ * filesystem activities that might affect the file.
+ * <p>
+ * Note: this method should not be used for file-locking, as the resulting
+ * protocol cannot be made to work reliably. The {@link FileLock} facility
+ * should be used instead.
+ *
+ * @param f
+ * the file to be created
+ * @throws IOException
+ * if the named file already exists or if an I/O error occurred
+ */
+ public static void createNewFile(File f) throws IOException {
+ if (!f.createNewFile())
+ throw new IOException(MessageFormat.format(
+ JGitText.get().createNewFileFailed, f));
+ }
}