zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [zookeeper] Diff for: [GitHub] asfgit closed pull request #630: ZOOKEEPER-2284:LogFormatter and SnapshotFormatter does not handle FileNotFoundException gracefully
Date Mon, 14 Jan 2019 18:43:26 GMT
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java
index a6abf2f42e..f9cfe4bfc2 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java
@@ -17,6 +17,7 @@
  */
 package org.apache.zookeeper;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Deque;
@@ -88,6 +89,24 @@ public static void deleteRecursive(ZooKeeper zk, final String pathRoot,
VoidCall
             zk.delete(tree.get(i), -1, cb, ctx); //Delete all versions of the node with -1.
         }
     }
+    
+    /**
+     * @param filePath the file path to be validated
+     * @return Returns null if valid otherwise error message
+     */
+    public static String validateFileInput(String filePath) {
+        File file = new File(filePath);
+        if (!file.exists()) {
+            return "File '" + file.getAbsolutePath() + "' does not exist.";
+        }
+        if (!file.canRead()) {
+            return "Read permission is denied on the file '" + file.getAbsolutePath() + "'";
+        }
+        if (file.isDirectory()) {
+            return "'" + file.getAbsolutePath() + "' is a direcory. it must be a file.";
+        }
+        return null;
+    }
 
     /**
      * BFS Traversal of the system under pathRoot, with the entries in the list, in the
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java
index 0e72a6739b..92ac503755 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java
@@ -31,6 +31,7 @@
 import org.apache.yetus.audience.InterfaceAudience;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.zookeeper.ZKUtil;
 import org.apache.zookeeper.server.persistence.FileHeader;
 import org.apache.zookeeper.server.persistence.FileTxnLog;
 import org.apache.zookeeper.server.util.SerializeUtils;
@@ -48,6 +49,13 @@ public static void main(String[] args) throws Exception {
             System.err.println("USAGE: LogFormatter log_file");
             System.exit(ExitCode.INVALID_INVOCATION.getValue());
         }
+        
+        String error = ZKUtil.validateFileInput(args[0]);
+        if (null != error) {
+            System.err.println(error);
+            System.exit(ExitCode.INVALID_INVOCATION.getValue());
+        }
+        
         FileInputStream fis = new FileInputStream(args[0]);
         BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
         FileHeader fhdr = new FileHeader();
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
index 2a80d897bd..50230eda9d 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java
@@ -34,6 +34,7 @@
 import org.apache.jute.BinaryInputArchive;
 import org.apache.jute.InputArchive;
 import org.apache.yetus.audience.InterfaceAudience;
+import org.apache.zookeeper.ZKUtil;
 import org.apache.zookeeper.data.StatPersisted;
 import org.apache.zookeeper.server.persistence.FileSnap;
 import org.apache.zookeeper.server.persistence.Util;
@@ -78,7 +79,13 @@ public static void main(String[] args) throws Exception {
             System.err.println("       -json dump znode info in json format");
             System.exit(ExitCode.INVALID_INVOCATION.getValue());
         }
-
+        
+        String error = ZKUtil.validateFileInput(snapshotFile);
+        if (null != error) {
+            System.err.println(error);
+            System.exit(ExitCode.INVALID_INVOCATION.getValue());
+        }
+        
         if (dumpData && dumpJson) {
             System.err.println("Cannot specify both data dump (-d) and json mode (-json)
in same call");
             System.exit(ExitCode.INVALID_INVOCATION.getValue());
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java
new file mode 100644
index 0000000000..cd8e59b00c
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zookeeper;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.UUID;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class ZKUtilTest {
+    private static final File testData = new File(System.getProperty("test.data.dir", "build/test/data"));
+
+    @BeforeClass
+    public static void init() {
+        testData.mkdirs();
+    }
+
+    @Test
+    public void testValidateFileInput() throws IOException {
+        File file = File.createTempFile("test", ".junit", testData);
+        file.deleteOnExit();
+        String absolutePath = file.getAbsolutePath();
+        String error = ZKUtil.validateFileInput(absolutePath);
+        assertNull(error);
+    }
+
+    @Test
+    public void testValidateFileInputNotExist() {
+        String fileName = UUID.randomUUID().toString();
+        File file = new File(testData, fileName);
+        String absolutePath = file.getAbsolutePath();
+        String error = ZKUtil.validateFileInput(absolutePath);
+        assertNotNull(error);
+        String expectedMessage = "File '" + absolutePath + "' does not exist.";
+        assertEquals(expectedMessage, error);
+    }
+
+    @Test
+    public void testValidateFileInputDirectory() throws Exception {
+        File file = File.createTempFile("test", ".junit", testData);
+        file.deleteOnExit();
+        // delete file, as we need directory not file
+        file.delete();
+        file.mkdir();
+        String absolutePath = file.getAbsolutePath();
+        String error = ZKUtil.validateFileInput(absolutePath);
+        assertNotNull(error);
+        String expectedMessage = "'" + absolutePath + "' is a direcory. it must be a file.";
+        assertEquals(expectedMessage, error);
+    }
+
+    @Test
+    public void testUnreadableFileInput() throws Exception {
+        //skip this test on Windows, coverage on Linux
+        assumeTrue(!org.apache.zookeeper.Shell.WINDOWS);
+        File file = File.createTempFile("test", ".junit", testData);
+        file.setReadable(false, false);
+        file.deleteOnExit();
+        String absolutePath = file.getAbsolutePath();
+        String error = ZKUtil.validateFileInput(absolutePath);
+        assertNotNull(error);
+        String expectedMessage = "Read permission is denied on the file '" + absolutePath
+ "'";
+        assertEquals(expectedMessage, error);
+    }
+
+}
\ No newline at end of file


With regards,
Apache Git Services

Mime
View raw message