diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index f1d48bc620..b1a7a7ded3 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -13,6 +13,9 @@ Trunk (Unreleased) HADOOP-10950. rework heap management vars (John Smith via aw) + HADOOP-11657. Align the output of `hadoop fs -du` to be more Unix-like. + (aajisaka) + NEW FEATURES HADOOP-6590. Add a username check for hadoop sub-commands (John Smith via aw) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/FsUsage.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/FsUsage.java index 5c1dbf032a..765b181761 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/FsUsage.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/FsUsage.java @@ -132,15 +132,23 @@ protected void processOptions(LinkedList args) throws IOException { } @Override - protected void processPathArgument(PathData item) throws IOException { + protected void processArguments(LinkedList args) + throws IOException { usagesTable = new TableBuilder(3); + super.processArguments(args); + if (!usagesTable.isEmpty()) { + usagesTable.printToStream(out); + } + } + + @Override + protected void processPathArgument(PathData item) throws IOException { // go one level deep on dirs from cmdline unless in summary mode if (!summary && item.stat.isDirectory()) { recursePath(item); } else { super.processPathArgument(item); } - usagesTable.printToStream(out); } @Override diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java index ee04076028..0a88208214 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java @@ -95,6 +95,14 @@ static Path writeFile(FileSystem fs, Path f) throws IOException { return f; } + static Path writeByte(FileSystem fs, Path f) throws IOException { + DataOutputStream out = fs.create(f); + out.writeByte(1); + out.close(); + assertTrue(fs.exists(f)); + return f; + } + static Path mkdir(FileSystem fs, Path p) throws IOException { assertTrue(fs.mkdirs(p)); assertTrue(fs.exists(p)); @@ -272,6 +280,27 @@ public void testDu() throws IOException { Long combinedDiskUsed = myFileDiskUsed + myFile2DiskUsed; assertThat(returnString, containsString(combinedLength.toString())); assertThat(returnString, containsString(combinedDiskUsed.toString())); + + // Check if output is rendered properly with multiple input paths + Path myFile3 = new Path("/test/dir/file3"); + writeByte(fs, myFile3); + assertTrue(fs.exists(myFile3)); + args = new String[3]; + args[0] = "-du"; + args[1] = "/test/dir/file3"; + args[2] = "/test/dir/file2"; + val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals("Return code should be 0.", 0, val); + returnString = out.toString(); + out.reset(); + assertTrue(returnString.contains("1 2 /test/dir/file3")); + assertTrue(returnString.contains("23 46 /test/dir/file2")); } finally { System.setOut(psBackup); cluster.shutdown();