[tor-commits] [collector/master] task-19830: Check root of given path avoids non-existant files exception.
karsten at torproject.org
karsten at torproject.org
Sun Aug 7 09:32:36 UTC 2016
commit ffdb49851faff6bec3d7a67b43f327f134055742
Author: iwakeh <iwakeh at torproject.org>
Date: Fri Aug 5 13:33:44 2016 +0200
task-19830: Check root of given path avoids non-existant files exception.
---
.../torproject/collector/cron/CollecTorMain.java | 9 ++---
.../collector/cron/CollecTorMainTest.java | 42 ++++++++++++++++++++++
src/test/resources/junittest.policy | 2 ++
3 files changed, 49 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/torproject/collector/cron/CollecTorMain.java b/src/main/java/org/torproject/collector/cron/CollecTorMain.java
index e869dae..e9274ba 100644
--- a/src/main/java/org/torproject/collector/cron/CollecTorMain.java
+++ b/src/main/java/org/torproject/collector/cron/CollecTorMain.java
@@ -63,8 +63,9 @@ public abstract class CollecTorMain implements Runnable {
*/
public static void checkAvailableSpace(Path location) {
try {
- long megaBytes = (long) (Files.getFileStore(location).getUsableSpace()
- / 1024 / 1024);
+ long megaBytes = (long) (Files.getFileStore(location.toFile()
+ .getAbsoluteFile().toPath().getRoot()).getUsableSpace()
+ / 1024 / 1024);
if (megaBytes < LIMIT_MB) {
log.warn("Available storage critical for {}; only {} MiB left.",
location, megaBytes);
@@ -72,8 +73,8 @@ public abstract class CollecTorMain implements Runnable {
log.trace("Available storage for {}: {} MiB", location, megaBytes);
}
} catch (IOException ioe) {
- log.warn("Cannot access {}; reason: {}", location, ioe.getMessage(),
- ioe);
+ throw new RuntimeException("Cannot access " + location + " reason: "
+ + ioe.getMessage(), ioe);
}
}
}
diff --git a/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java b/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java
new file mode 100644
index 0000000..7eb1f38
--- /dev/null
+++ b/src/test/java/org/torproject/collector/cron/CollecTorMainTest.java
@@ -0,0 +1,42 @@
+/* Copyright 2016 The Tor Project
+ * See LICENSE for licensing information */
+package org.torproject.collector.cron;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.torproject.collector.conf.Key;
+import org.torproject.collector.conf.Configuration;
+import org.torproject.collector.cron.Scheduler;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Paths;
+
+public class CollecTorMainTest {
+
+ @Rule
+ public TemporaryFolder tmpf = new TemporaryFolder();
+
+ @Test()
+ public void testCheckAvailableSpace() {
+ File someFile = null;
+ try {
+ someFile = tmpf.newFile("existing.file");
+ assertTrue(someFile.exists());
+ } catch (IOException ioe) {
+ fail("Cannot perform test. File creation failed.");
+ }
+ CollecTorMain.checkAvailableSpace(someFile.toPath());
+ CollecTorMain.checkAvailableSpace(Paths.get("/fantasy", "path", "non",
+ "existant", "but", "no", "exception"));
+ }
+
+}
+
diff --git a/src/test/resources/junittest.policy b/src/test/resources/junittest.policy
index 35c30c0..0c7ccac 100644
--- a/src/test/resources/junittest.policy
+++ b/src/test/resources/junittest.policy
@@ -4,7 +4,9 @@ grant {
permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete, execute";
permission java.util.PropertyPermission "*", "read, write";
permission java.lang.RuntimePermission "setIO";
+ permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.fs";
permission java.lang.RuntimePermission "accessDeclaredMembers";
+ permission java.lang.RuntimePermission "getFileStoreAttributes";
permission java.lang.RuntimePermission "modifyThread";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission java.lang.RuntimePermission "shutdownHooks";
More information about the tor-commits
mailing list