瀏覽代碼

Merging change r1127697 for HADOOP-7322 from trunk

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/yahoo-merge@1134079 13f79535-47bb-0310-9956-ffa450edef68
Suresh Srinivas 14 年之前
父節點
當前提交
32af4f1cc9

+ 3 - 0
CHANGES.txt

@@ -9,6 +9,9 @@ Trunk (unreleased changes)
     HADOOP-7342. Add an utility API in FileUtil for JDK File.list
     avoid NPEs on File.list() (Bharath Mundlapudi via mattf)
 
+    HADOOP-7322. Adding a util method in FileUtil for directory listing,
+    avoid NPEs on File.listFiles() (Bharath Mundlapudi via mattf)
+
     HADOOP-6994. Api to get delegation token in AbstractFileSystem. (jitendra)
 
     HADOOP-7171. Support UGI in FileContext API. (jitendra)

+ 24 - 3
src/java/org/apache/hadoop/fs/FileUtil.java

@@ -324,7 +324,7 @@ public class FileUtil {
       if (!dstFS.mkdirs(dst)) {
         return false;
       }
-      File contents[] = src.listFiles();
+      File contents[] = listFiles(src);
       for (int i = 0; i < contents.length; i++) {
         copy(contents[i], dstFS, new Path(dst, contents[i].getName()),
              deleteSource, conf);
@@ -486,8 +486,10 @@ public class FileUtil {
     } else {
       size = dir.length();
       File[] allFiles = dir.listFiles();
-      for (int i = 0; i < allFiles.length; i++) {
-        size = size + getDU(allFiles[i]);
+      if(allFiles != null) {
+         for (int i = 0; i < allFiles.length; i++) {
+            size = size + getDU(allFiles[i]);
+         }
       }
       return size;
     }
@@ -708,6 +710,25 @@ public class FileUtil {
     }
   }
   
+  /**
+   * A wrapper for {@link File#listFiles()}. This java.io API returns null 
+   * when a dir is not a directory or for any I/O error. Instead of having
+   * null check everywhere File#listFiles() is used, we will add utility API
+   * to get around this problem. For the majority of cases where we prefer 
+   * an IOException to be thrown.
+   * @param dir directory for which listing should be performed
+   * @return list of files or empty list
+   * @exception IOException for invalid directory or for a bad disk.
+   */
+  public static File[] listFiles(File dir) throws IOException {
+    File[] files = dir.listFiles();
+    if(files == null) {
+      throw new IOException("Invalid directory or I/O error occurred for dir: "
+                + dir.toString());
+    }
+    return files;
+  }  
+
   /**
    * A wrapper for {@link File#list()}. This java.io API returns null 
    * when a dir is not a directory or for any I/O error. Instead of having

+ 1 - 1
src/java/org/apache/hadoop/fs/RawLocalFileSystem.java

@@ -274,7 +274,7 @@ public class RawLocalFileSystem extends FileSystem {
     if (f.isFile()) {
       return f.delete();
     } else if ((!recursive) && f.isDirectory() && 
-        (f.listFiles().length != 0)) {
+        (FileUtil.listFiles(f).length != 0)) {
       throw new IOException("Directory " + f.toString() + " is not empty");
     }
     return FileUtil.fullyDelete(f);

+ 26 - 0
src/test/core/org/apache/hadoop/fs/TestFileUtil.java

@@ -117,6 +117,32 @@ public class TestFileUtil {
     }
   }
 
+  @Test
+  public void testListFiles() throws IOException {
+    setupDirs();
+    //Test existing files case 
+    File[] files = FileUtil.listFiles(partitioned);
+    Assert.assertEquals(2, files.length);
+
+    //Test existing directory with no files case 
+    File newDir = new File(tmp.getPath(),"test");
+    newDir.mkdir();
+    Assert.assertTrue("Failed to create test dir", newDir.exists());
+    files = FileUtil.listFiles(newDir);
+    Assert.assertEquals(0, files.length);
+    newDir.delete();
+    Assert.assertFalse("Failed to delete test dir", newDir.exists());
+    
+    //Test non-existing directory case, this throws 
+    //IOException
+    try {
+      files = FileUtil.listFiles(newDir);
+      Assert.fail("IOException expected on listFiles() for non-existent dir "
+      		+ newDir.toString());
+    } catch(IOException ioe) {
+    	//Expected an IOException
+    }
+  }
 
   @Test
   public void testListAPI() throws IOException {