commit 715f98d356b742a466bb57ef3040bb437fb262a8 Author: Karsten Loesing karsten.loesing@gmx.net Date: Mon Jun 1 10:40:28 2015 +0200
Properly close BufferedWriter in LockFile.
Patch by firebrand. Fixes #15654. --- .../java/org/torproject/onionoo/util/LockFile.java | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/util/LockFile.java b/src/main/java/org/torproject/onionoo/util/LockFile.java index 0a1f17d..fc8a792 100644 --- a/src/main/java/org/torproject/onionoo/util/LockFile.java +++ b/src/main/java/org/torproject/onionoo/util/LockFile.java @@ -17,6 +17,13 @@ public class LockFile {
private final File lockFile = new File("lock");
+ /** + * Acquire the lock by writing a lock file with the current time in + * milliseconds and return whether this operation was successful. + * + * @return <code>true</code> if the lock file did not exist and writing + * that file now succeeded, <code>false</code> otherwise. + */ public boolean acquireLock() { Time time = TimeFactory.getTime(); try { @@ -26,10 +33,15 @@ public class LockFile { if (this.lockFile.getParentFile() != null) { this.lockFile.getParentFile().mkdirs(); } - BufferedWriter bw = new BufferedWriter(new FileWriter( - this.lockFile)); - bw.append("" + time.currentTimeMillis() + "\n"); - bw.close(); + } catch (SecurityException e) { + log.error("Unable to access lock file location", e); + return false; + } + + try (BufferedWriter bw = new BufferedWriter(new FileWriter( + this.lockFile))) { + bw.append(String.valueOf(time.currentTimeMillis())); + bw.newLine(); return true; } catch (IOException e) { log.error("Caught exception while trying to acquire lock!", e); @@ -37,6 +49,13 @@ public class LockFile { } }
+ /** + * Release the lock by deleting the lock file if it exists and return + * whether the file was successfully deleted. + * + * @return <code>true</code> if the lock file does not exist anymore + * when returning, <code>false</code> otherwise. + */ public boolean releaseLock() { if (this.lockFile.exists()) { this.lockFile.delete();