This is an automated email from the ASF dual-hosted git repository.
andor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git
The following commit(s) were added to refs/heads/master by this push:
new d149b13 ZOOKEEPER-2284: LogFormatter and SnapshotFormatter does not
handle FileNotFoundException gracefully
d149b13 is described below
commit d149b134049dc8527ba81a9546836942fcabc5b6
Author: maoling <[email protected]>
AuthorDate: Mon Jan 14 11:43:00 2019 -0700
ZOOKEEPER-2284: LogFormatter and SnapshotFormatter does not handle
FileNotFoundException gracefully
- More details in
[ZOOKEEPER-2284](https://issues.apache.org/jira/browse/ZOOKEEPER-2284) .Thanks
[arshad.mohammad](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=arshad.mohammad)
for original work!
Author: maoling <[email protected]>
Reviewers: [email protected], [email protected]
Closes #630 from maoling/ZOOKEEPER-2284 and squashes the following commits:
393cccdc5 [maoling] git rebase the code again
92c855f30 [maoling] rebase the code
5042a65bc [maoling] ZOOKEEPER-2284:LogFormatter and SnapshotFormatter does
not handle FileNotFoundException gracefully
---
.../src/main/java/org/apache/zookeeper/ZKUtil.java | 19 +++++
.../org/apache/zookeeper/server/LogFormatter.java | 8 ++
.../apache/zookeeper/server/SnapshotFormatter.java | 9 ++-
.../test/java/org/apache/zookeeper/ZKUtilTest.java | 88 ++++++++++++++++++++++
4 files changed, 123 insertions(+), 1 deletion(-)
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 a6abf2f..f9cfe4b 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 class ZKUtil {
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 0e72a67..92ac503 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.jute.Record;
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 class LogFormatter {
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 2a80d89..50230ed 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 java.util.zip.CheckedInputStream;
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 class SnapshotFormatter {
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 0000000..cd8e59b
--- /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