diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFs.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFs.java index 75622ad3742..c56f6e09e2d 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFs.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/ChecksumFs.java @@ -27,10 +27,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; +import java.util.NoSuchElementException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.fs.Options.ChecksumOpt; import org.apache.hadoop.fs.permission.FsPermission; +import org.apache.hadoop.security.AccessControlException; import org.apache.hadoop.util.DataChecksum; import org.apache.hadoop.util.Progressable; import org.slf4j.Logger; @@ -527,4 +529,39 @@ public abstract class ChecksumFs extends FilterFs { } return results.toArray(new FileStatus[results.size()]); } + + @Override + public RemoteIterator listLocatedStatus(final Path f) + throws AccessControlException, FileNotFoundException, + UnresolvedLinkException, IOException { + final RemoteIterator iter = + getMyFs().listLocatedStatus(f); + return new RemoteIterator() { + + private LocatedFileStatus next = null; + + @Override + public boolean hasNext() throws IOException { + while (next == null && iter.hasNext()) { + LocatedFileStatus unfilteredNext = iter.next(); + if (!isChecksumFile(unfilteredNext.getPath())) { + next = unfilteredNext; + } + } + return next != null; + } + + @Override + public LocatedFileStatus next() throws IOException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + LocatedFileStatus tmp = next; + next = null; + return tmp; + } + + }; + } + } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/FileContextMainOperationsBaseTest.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/FileContextMainOperationsBaseTest.java index 35ec4ff6b68..7be3b541e7d 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/FileContextMainOperationsBaseTest.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/FileContextMainOperationsBaseTest.java @@ -369,6 +369,44 @@ public abstract class FileContextMainOperationsBaseTest { pathsIterator = fc.listStatus(getTestRootPath(fc, "test/hadoop/a")); Assert.assertFalse(pathsIterator.hasNext()); } + + @Test + public void testListFiles() throws Exception { + Path[] testDirs = { + getTestRootPath(fc, "test/dir1"), + getTestRootPath(fc, "test/dir1/dir1"), + getTestRootPath(fc, "test/dir2") + }; + Path[] testFiles = { + new Path(testDirs[0], "file1"), + new Path(testDirs[0], "file2"), + new Path(testDirs[1], "file2"), + new Path(testDirs[2], "file1") + }; + + for (Path path : testDirs) { + fc.mkdir(path, FsPermission.getDefault(), true); + } + for (Path p : testFiles) { + FSDataOutputStream out = fc.create(p, EnumSet.of(CreateFlag.CREATE)); + out.writeByte(0); + out.close(); + } + + RemoteIterator filesIterator = + fc.util().listFiles(getTestRootPath(fc, "test"), true); + LocatedFileStatus[] fileStats = + new LocatedFileStatus[testFiles.length]; + for (int i = 0; i < fileStats.length; i++) { + assertTrue(filesIterator.hasNext()); + fileStats[i] = filesIterator.next(); + } + assertFalse(filesIterator.hasNext()); + + for (Path p : testFiles) { + assertTrue(containsPath(p, fileStats)); + } + } @Test public void testListStatusFilterWithNoMatches() throws Exception {