PathData implements Comparable<PathData> { public static PathData[] expandAsGlob(String pattern, Configuration conf) throws IOException { Path globPath = new Path(pattern); FileSystem fs = globPath.getFileSystem(conf); FileStatus[] stats = fs.globStatus(globPath); PathData[] items = null; if (stats == null) { pattern = pattern.replaceAll("\\\\(.)", "$1"); items = new PathData[]{ new PathData(fs, pattern, null) }; } else { PathType globType; URI globUri = globPath.toUri(); if (globUri.getScheme() != null) { globType = PathType.HAS_SCHEME; } else if (!globUri.getPath().isEmpty() && new Path(globUri.getPath()).isAbsolute()) { globType = PathType.SCHEMELESS_ABSOLUTE; } else { globType = PathType.RELATIVE; } items = new PathData[stats.length]; int i=0; for (FileStatus stat : stats) { URI matchUri = stat.getPath().toUri(); String globMatch = null; switch (globType) { case HAS_SCHEME: if (globUri.getAuthority() == null) { matchUri = removeAuthority(matchUri); } globMatch = uriToString(matchUri, false); break; case SCHEMELESS_ABSOLUTE: globMatch = matchUri.getPath(); break; case RELATIVE: URI cwdUri = fs.getWorkingDirectory().toUri(); globMatch = relativize(cwdUri, matchUri, stat.isDirectory()); break; } items[i++] = new PathData(fs, globMatch, stat); } } Arrays.sort(items); return items; } PathData(String pathString, Configuration conf);  PathData(URI localPath, Configuration conf); private  PathData(FileSystem fs, String pathString); private  PathData(FileSystem fs, String pathString, FileStatus stat); FileStatus refreshStatus(); PathData suffix(String extension); boolean parentExists(); boolean representsDirectory(); PathData[] getDirectoryContents(); RemoteIterator<PathData> getDirectoryContentsIterator(); PathData getPathDataForChild(PathData child); static PathData[] expandAsGlob(String pattern, Configuration conf); @Override String toString(); File toFile(); @Override int compareTo(PathData o); @Override boolean equals(Object o); @Override int hashCode(); public FileSystem fs; final Path path; public FileStatus stat; public boolean exists; }
@Test (timeout = 30000) public void testAbsoluteGlob() throws Exception { PathData[] items = PathData.expandAsGlob(testDir+"/d1/f1*", conf); assertEquals( sortedString(testDir+"/d1/f1", testDir+"/d1/f1.1"), sortedString(items) ); String absolutePathNoDriveLetter = testDir+"/d1/f1"; if (Shell.WINDOWS) { absolutePathNoDriveLetter = absolutePathNoDriveLetter.substring(2); } items = PathData.expandAsGlob(absolutePathNoDriveLetter, conf); assertEquals( sortedString(absolutePathNoDriveLetter), sortedString(items) ); items = PathData.expandAsGlob(".", conf); assertEquals( sortedString("."), sortedString(items) ); }
@Test (timeout = 30000) public void testRelativeGlob() throws Exception { PathData[] items = PathData.expandAsGlob("d1/f1*", conf); assertEquals( sortedString("d1/f1", "d1/f1.1"), sortedString(items) ); }
@Test (timeout = 30000) public void testRelativeGlobBack() throws Exception { fs.setWorkingDirectory(new Path("d1")); PathData[] items = PathData.expandAsGlob("../d2/*", conf); assertEquals( sortedString("../d2/f3"), sortedString(items) ); }
@Test public void testGlobThrowsExceptionForUnreadableDir() throws Exception { Path obscuredDir = new Path("foo"); Path subDir = new Path(obscuredDir, "bar"); fs.mkdirs(subDir); fs.setPermission(obscuredDir, new FsPermission((short)0)); try { PathData.expandAsGlob("foo/*", conf); Assert.fail("Should throw IOException"); } catch (IOException ioe) { } finally { fs.setPermission(obscuredDir, new FsPermission((short)0755)); } }