HDFS-1727. fsck command should display command usage if user passes any illegal argument. Contributed by Sravan Kumar.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hdfs/trunk@1129831 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 850605b..3cf6bf7 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -459,6 +459,9 @@
     HDFS-1884. Improve TestDFSStorageStateRecovery to properly throw in the
     case of errors. (Aaron T. Myers via todd)
 
+    HDFS-1727. fsck command should display command usage if user passes any
+    illegal argument. (Sravan Kumar via todd)
+
   OPTIMIZATIONS
 
     HDFS-1458. Improve checkpoint performance by avoiding unnecessary image
diff --git a/src/java/org/apache/hadoop/hdfs/tools/DFSck.java b/src/java/org/apache/hadoop/hdfs/tools/DFSck.java
index c2250e7..799b0cf 100644
--- a/src/java/org/apache/hadoop/hdfs/tools/DFSck.java
+++ b/src/java/org/apache/hadoop/hdfs/tools/DFSck.java
@@ -250,13 +250,8 @@
     url.append(namenodeAddress);
     System.err.println("Connecting to namenode via " + url.toString());
     
-    url.append("/fsck?ugi=").append(ugi.getShortUserName()).append("&path=");
-    String dir = "/";
-    // find top-level dir first
-    for (int idx = 0; idx < args.length; idx++) {
-      if (!args[idx].startsWith("-")) { dir = args[idx]; break; }
-    }
-    url.append(URLEncoder.encode(dir, "UTF-8"));
+    url.append("/fsck?ugi=").append(ugi.getShortUserName());
+    String dir = null;
     boolean doListCorruptFileBlocks = false;
     for (int idx = 0; idx < args.length; idx++) {
       if (args[idx].equals("-move")) { url.append("&move=1"); }
@@ -269,8 +264,25 @@
       else if (args[idx].equals("-list-corruptfileblocks")) {
         url.append("&listcorruptfileblocks=1");
         doListCorruptFileBlocks = true;
+      } else if (!args[idx].startsWith("-")) {
+        if (null == dir) {
+          dir = args[idx];
+        } else {
+          System.err.println("fsck: can only operate on one path at a time '"
+              + args[idx] + "'");
+          printUsage();
+          return -1;
+        }
+      } else {
+        System.err.println("fsck: Illegal option '" + args[idx] + "'");
+        printUsage();
+        return -1;
       }
     }
+    if (null == dir) {
+      dir = "/";
+    }
+    url.append("&path=").append(URLEncoder.encode(dir, "UTF-8"));
     if (doListCorruptFileBlocks) {
       return listCorruptFileBlocks(dir, url.toString());
     }
diff --git a/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFsck.java b/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFsck.java
index ea957d7..647e47d 100644
--- a/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFsck.java
+++ b/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFsck.java
@@ -506,5 +506,40 @@
       if (cluster != null) {cluster.shutdown();}
     }
   }
+  
+  /**
+   * Test for checking fsck command on illegal arguments should print the proper
+   * usage.
+   */
+  public void testToCheckTheFsckCommandOnIllegalArguments() throws Exception {
+    MiniDFSCluster cluster = null;
+    try {
+      // bring up a one-node cluster
+      Configuration conf = new HdfsConfiguration();
+      cluster = new MiniDFSCluster.Builder(conf).build();
+      String fileName = "/test.txt";
+      Path filePath = new Path(fileName);
+      FileSystem fs = cluster.getFileSystem();
 
+      // create a one-block file
+      DFSTestUtil.createFile(fs, filePath, 1L, (short) 1, 1L);
+      DFSTestUtil.waitReplication(fs, filePath, (short) 1);
+
+      // passing illegal option
+      String outStr = runFsck(conf, -1, true, fileName, "-thisIsNotAValidFlag");
+      System.out.println(outStr);
+      assertTrue(!outStr.contains(NamenodeFsck.HEALTHY_STATUS));
+
+      // passing multiple paths are arguments
+      outStr = runFsck(conf, -1, true, "/", fileName);
+      System.out.println(outStr);
+      assertTrue(!outStr.contains(NamenodeFsck.HEALTHY_STATUS));
+      // clean up file system
+      fs.delete(filePath, true);
+    } finally {
+      if (cluster != null) {
+        cluster.shutdown();
+      }
+    }
+  }
 }