Browse Source

HADOOP-909. Fix the 'du' command to correctly compute the size of FileSystem directory trees. Contributed by Hairong.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@500354 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 years ago
parent
commit
7c852d0656

+ 3 - 0
CHANGES.txt

@@ -60,6 +60,9 @@ Trunk (unreleased changes)
 18. HADOOP-912.  Fix a bug in TaskTracker.isIdle() that was
     sporadically causing unit test failures.  (Arun C Murthy via cutting)
 
+19. HADOOP-909.  Fix the 'du' command to correctly compute the size of
+    FileSystem directory trees.  (Hairong Kuang via cutting)
+
 
 Release 0.10.1 - 2007-01-10
 

+ 9 - 0
src/java/org/apache/hadoop/dfs/DistributedFileSystem.java

@@ -176,6 +176,15 @@ public class DistributedFileSystem extends FileSystem {
         return (info == null) ? 0 : info[0].getLen();
     }
 
+    public long getContentLength(Path f) throws IOException {
+        if (f instanceof DfsPath) {
+            return ((DfsPath)f).getContentsLength();
+          }
+
+          DFSFileInfo info[] = dfs.listPaths(getPath(f));
+          return (info == null) ? 0 : info[0].getContentsLen();
+    }
+
     public short getReplication(Path f) throws IOException {
       if (f instanceof DfsPath) {
         return ((DfsPath)f).getReplication();

+ 19 - 0
src/java/org/apache/hadoop/fs/FileSystem.java

@@ -534,6 +534,25 @@ public abstract class FileSystem extends Configured {
     
     /** The number of bytes in a file. */
     public abstract long getLength(Path f) throws IOException;
+    
+    /** Return the number of bytes of the given path 
+     * If <i>f</i> is a file, return the size of the file;
+     * If <i>f</i> is a directory, return the size of the directory tree
+     */
+    public long getContentLength(Path f) throws IOException {
+        if (!isDirectory(f)) {
+            // f is a file
+            return getLength(f);
+        }
+            
+        // f is a diretory
+        Path[] contents = listPathsRaw(f);
+        long size = 0;
+        for(int i=0; i<contents.length; i++) {
+            size += getContentLength(contents[i]);
+        }
+        return size;
+    }
 
     final private static PathFilter DEFAULT_FILTER = new PathFilter() {
       public boolean accept(Path file) {

+ 2 - 2
src/java/org/apache/hadoop/fs/FsShell.java

@@ -352,7 +352,7 @@ public class FsShell extends ToolBase {
             System.out.println("Found " + items.length + " items");
             for (int i = 0; i < items.length; i++) {
               Path cur = items[i];
-              System.out.println(cur + "\t" + fs.getLength(cur));
+              System.out.println(cur + "\t" + fs.getContentLength(cur));
             }
         }
     }
@@ -374,7 +374,7 @@ public class FsShell extends ToolBase {
         if (items != null) {
           int totalSize=0;
           for(int j=0; j<items.length; j++) {
-            totalSize += fs.getLength(items[j]);
+            totalSize += fs.getContentLength(items[j]);
           }
           String pathStr = paths[i].toString();
           System.out.println(