Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn2012-01-10 17:24:49 +0000
committerCode Review2012-01-10 17:24:49 +0000
commit6a582970bfa1120dfe99e7568ea2d471dfc59387 (patch)
tree24376b2ade117a90ddc5620af739dacef73fb641 /org.eclipse.jgit
parentf78e52b645e0397f723f16e1199ffa3ee69e4350 (diff)
parentaebfc70cc84b1492cd3f5df04dd9b057d0bec1f7 (diff)
downloadjgit-6a582970bfa1120dfe99e7568ea2d471dfc59387.tar.gz
jgit-6a582970bfa1120dfe99e7568ea2d471dfc59387.tar.xz
jgit-6a582970bfa1120dfe99e7568ea2d471dfc59387.zip
Merge "Provide helper for unlocking a file"
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java36
1 files changed, 34 insertions, 2 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java
index 08dfd7e19b..dd58fecf59 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java
@@ -57,6 +57,7 @@ import java.nio.channels.FileChannel;
import java.text.MessageFormat;
import org.eclipse.jgit.JGitText;
+import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.util.FS;
@@ -75,6 +76,37 @@ import org.eclipse.jgit.util.FileUtils;
public class LockFile {
static final String SUFFIX = ".lock"; //$NON-NLS-1$
+ /**
+ * Unlock the given file.
+ * <p>
+ * This method can be used for recovering from a thrown
+ * {@link LockFailedException} . This method does not validate that the lock
+ * is or is not currently held before attempting to unlock it.
+ *
+ * @param file
+ * @return true if unlocked, false if unlocking failed
+ */
+ public static boolean unlock(final File file) {
+ final File lockFile = getLockFile(file);
+ final int flags = FileUtils.RETRY | FileUtils.SKIP_MISSING;
+ try {
+ FileUtils.delete(lockFile, flags);
+ } catch (IOException ignored) {
+ // Ignore and return whether lock file still exists
+ }
+ return !lockFile.exists();
+ }
+
+ /**
+ * Get the lock file corresponding to the given file.
+ *
+ * @param file
+ * @return lock file
+ */
+ static File getLockFile(File file) {
+ return new File(file.getParentFile(), file.getName() + SUFFIX);
+ }
+
/** Filter to skip over active lock files when listing a directory. */
static final FilenameFilter FILTER = new FilenameFilter() {
public boolean accept(File dir, String name) {
@@ -107,9 +139,9 @@ public class LockFile {
* the file system abstraction which will be necessary to perform
* certain file system operations.
*/
- public LockFile(final File f, FS fs) {
+ public LockFile(final File f, final FS fs) {
ref = f;
- lck = new File(ref.getParentFile(), ref.getName() + SUFFIX);
+ lck = getLockFile(ref);
this.fs = fs;
}

Back to the top