瀏覽代碼

HADOOP-16685: FileSystem#listStatusIterator does not check if given path exists (#1695)

Sahil Takiar 5 年之前
父節點
當前提交
3161813482

+ 3 - 8
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

@@ -2160,24 +2160,19 @@ public abstract class FileSystem extends Configured
     private DirectoryEntries entries;
     private int i = 0;
 
-    DirListingIterator(Path path) {
+    DirListingIterator(Path path) throws IOException {
       this.path = path;
+      this.entries = listStatusBatch(path, null);
     }
 
     @Override
     public boolean hasNext() throws IOException {
-      if (entries == null) {
-        fetchMore();
-      }
       return i < entries.getEntries().length ||
           entries.hasMore();
     }
 
     private void fetchMore() throws IOException {
-      byte[] token = null;
-      if (entries != null) {
-        token = entries.getToken();
-      }
+      byte[] token = entries.getToken();
       entries = listStatusBatch(path, token);
       i = 0;
     }

+ 8 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractContractGetFileStatusTest.java

@@ -279,6 +279,14 @@ public abstract class AbstractContractGetFileStatusTest extends
     }
   }
 
+  @Test
+  public void testListStatusIteratorNoDir() throws Throwable {
+    describe("test the listStatusIterator call on a path which is not " +
+        "present");
+    intercept(FileNotFoundException.class,
+        () -> getFileSystem().listStatusIterator(path("missing")));
+  }
+
   @Test
   public void testLocatedStatusNoDir() throws Throwable {
     describe("test the LocatedStatus call on a path which is not present");