DfsUtil { public static Set<Path> listDirs(FileSystem hdfs, Path path, boolean recurse, boolean filterMetaData) throws IOException { Map<Path, Boolean> dirs = new HashMap<>(); try { RemoteIterator<LocatedFileStatus> filesIterator = hdfs.listFiles(path, recurse); while (filesIterator.hasNext()) { Path file = filesIterator.next().getPath(); Path dir = file.getParent(); dirs.put(dir, (dirs.getOrDefault(dir, false)) || file.getName().startsWith(FILE_METADATA_PREFIX)); } } catch (FileNotFoundException exception) { } Set<Path> dirsFiltered = dirs.keySet(); if (filterMetaData) { dirsFiltered = new HashSet<>(); for (Path dir : dirs.keySet()) { if (!dirs.get(dir) && !dir.getName().startsWith(FILE_METADATA_PREFIX)) { dirsFiltered.add(dir); } } } return dirsFiltered; }  static boolean canDoAction(FileSystem hdfs, String user, String[] groups, Path path, FsAction action); static Set<Path> listFiles(FileSystem hdfs, Path path, boolean recurse, boolean filterMetaData); static Set<Path> listDirs(FileSystem hdfs, Path path, boolean recurse, boolean filterMetaData);  }
@Test public void testListDirs() throws IllegalArgumentException, IOException { assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmq/t"), false, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmq/t"), false, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmq/t"), true, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmq/t"), true, true).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t"), false, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t"), false, false).size()); assertEquals(7, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t"), true, false).size()); assertEquals(3, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t"), true, true).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t1"), false, false).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t1"), true, false).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t1"), true, true).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t3"), false, false).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t3"), true, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t3"), true, true).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t6"), false, false).size()); assertEquals(1, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t6"), true, false).size()); assertEquals(0, DfsUtil.listDirs(dfsServer.getFileSystem(), dfsServer.getPath("/tmp/t/t6"), true, true).size()); }