HADOOP-7322. Adding a util method in FileUtil for directory listing, avoid NPEs on File.listFiles(). Contributed by Bharath Mundlapudi.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1127697 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Foley 2011-05-25 21:51:35 +00:00
parent e79d8afcd0
commit 42a185b57d
4 changed files with 55 additions and 4 deletions

View File

@ -12,6 +12,9 @@ Trunk (unreleased changes)
NEW FEATURES
HADOOP-7322. Adding a util method in FileUtil for directory listing,
avoid NPEs on File.listFiles() (Bharath Mundlapudi via mattf)
HADOOP-7023. Add listCorruptFileBlocks to Filesysem. (Patrick Kling
via hairong)

View File

@ -324,7 +324,7 @@ public static boolean copy(File src,
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 static long getDU(File dir) {
} 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;
}
@ -707,4 +709,23 @@ public static void replaceFile(File src, File target) throws IOException {
}
}
}
/**
* 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;
}
}

View File

@ -276,7 +276,7 @@ public boolean delete(Path p, boolean recursive) throws IOException {
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);

View File

@ -117,6 +117,33 @@ private void createFile(File directory, String name, String contents)
}
}
@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
}
}
@After
public void tearDown() throws IOException {
FileUtil.fullyDelete(del);