From aebfc70cc84b1492cd3f5df04dd9b057d0bec1f7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 9 Jan 2012 08:46:13 -0800 Subject: Provide helper for unlocking a file This will allow recovery from a LockFailedException where the file associated with an exception is passed to FileUtils.unlock to attempt an unlock on the file so the operation can be retried Change-Id: I580166d386126bfb54a318a65253070a6e325936 --- .../org/eclipse/jgit/storage/file/LockFile.java | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit') 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. + *

+ * 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; } -- cgit v1.2.3