diff options
author | Kevin Sawicki <kevin@github.com> | 2012-01-09 08:46:13 -0800 |
---|---|---|
committer | Kevin Sawicki <kevin@github.com> | 2012-01-09 08:46:13 -0800 |
commit | aebfc70cc84b1492cd3f5df04dd9b057d0bec1f7 (patch) | |
tree | 05a4e86b892548663f28eaf2953a574f58a89a70 /org.eclipse.jgit | |
parent | 0f15d656f26d48516eee661bacfd7e9750b35ab3 (diff) | |
download | jgit-aebfc70cc84b1492cd3f5df04dd9b057d0bec1f7.tar.gz jgit-aebfc70cc84b1492cd3f5df04dd9b057d0bec1f7.zip |
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
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/storage/file/LockFile.java | 36 |
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; } |