From 9cb0a0cd46cf91b324b7c6c66d7479d21073d6f6 Mon Sep 17 00:00:00 2001 From: shroman Date: Tue, 3 Jan 2017 17:35:47 +0800 Subject: [PATCH] [ROCKETMQ-5] Avoid creating directories in UtilAll#getDiskPartitionSpaceUsedPercent(), closes apache/incubator-rocketmq#23 --- .../java/org/apache/rocketmq/common/UtilAll.java | 15 +++++++-------- .../org/apache/rocketmq/common/UtilAllTest.java | 13 +++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index 56015b36..016da0b4 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -183,17 +183,16 @@ public class UtilAll { try { File file = new File(path); - if (!file.exists()) { - boolean result = file.mkdirs(); - if (!result) { - //TO DO - } - } + + if (!file.exists()) + return -1; long totalSpace = file.getTotalSpace(); - long freeSpace = file.getFreeSpace(); - long usedSpace = totalSpace - freeSpace; + if (totalSpace > 0) { + long freeSpace = file.getFreeSpace(); + long usedSpace = totalSpace - freeSpace; + return usedSpace / (double) totalSpace; } } catch (Exception e) { diff --git a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java index 8d8cf796..0db84fe4 100644 --- a/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java @@ -21,6 +21,8 @@ import java.net.URL; import java.util.Properties; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; public class UtilAllTest { @@ -78,6 +80,17 @@ public class UtilAllTest { assertTrue(pid > 0); } + @Test + public void test_getDiskPartitionSpaceUsedPercent() { + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(null), 0); + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(""), 0); + + assertEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent("nonExistingPath"), 0); + + String tmpDir = System.getProperty("java.io.tmpdir"); + assertNotEquals(-1, UtilAll.getDiskPartitionSpaceUsedPercent(tmpDir), 0); + } + @Test public void test_isBlank() { { -- GitLab