/** * Remove leading "/"s in the given path string. * @param path * @return */ public static String removeLeadingSlash(String path) { if (path.length() > 0 && path.charAt(0) == '/') { String newPath = path.substring(1); return removeLeadingSlash(newPath); } else { return path; } } }
/** * Resolve given table path relative to source resolve it to a valid path in filesystem. * If the resolved path refers to an entity not under the base of the source then a permission error is thrown. * @param tablePath * @return */ public Path resolveTablePathToValidPath(String tablePath) { String relativePathClean = PathUtils.removeLeadingSlash(tablePath); Path combined = new Path(basePath, relativePathClean); PathUtils.verifyNoAccessOutsideBase(basePath, combined); return combined; }
/** * Get container name from the path. * * @param path path * @return container name */ private static String getContainerName(Path path) { final List<String> pathComponents = Arrays.asList( removeLeadingSlash(Path.getPathWithoutSchemeAndAuthority(path).toString()) .split(Path.SEPARATOR) ); return pathComponents.get(0); }
/** * Transform remote path to local. * @param path * @param containerName * @return */ private static Path transform(Path path, String containerName) { String relativePath = removeLeadingSlash(Path.getPathWithoutSchemeAndAuthority(path).toString()); Path containerPath = new Path(Path.SEPARATOR + containerName); return Strings.isNullOrEmpty(relativePath) ? containerPath : new Path(containerPath, relativePath); }
private Path pathWithoutContainer(Path path) { List<String> pathComponents = Arrays.asList(removeLeadingSlash(Path.getPathWithoutSchemeAndAuthority(path).toString()).split(Path.SEPARATOR)); return new Path("/" + Joiner.on(Path.SEPARATOR).join(pathComponents.subList(1, pathComponents.size()))); }
public static Path getPathBasedOnFullPath(List<String> fullPath) { String parent = Joiner.on(PATH_SEPARATOR).join(fullPath.subList(0, fullPath.size() - 1)); if (Strings.isNullOrEmpty(parent)) { parent = "/"; } parent = Path.getPathWithoutSchemeAndAuthority(new Path(Path.SEPARATOR, parent)).toString(); String path = PathUtils.removeQuotes(fullPath.get(fullPath.size() - 1)); return new Path(parent, removeLeadingSlash(path)); }
@Override public WorkAndComponent apply(FileWork work) { final ComponentizedPath path = new ComponentizedPath(); if (selectionRoot != null) { String prefixString = Path.getPathWithoutSchemeAndAuthority(new Path(selectionRoot)).toString(); String fullString = Path.getPathWithoutSchemeAndAuthority(work.getStatus().getPath()).toString(); if (prefixString.length() < fullString.length()) { path.path = removeLeadingSlash(fullString.substring(prefixString.length(), fullString.length())); } else { path.path = removeLeadingSlash(fullString); } final String[] prefix = prefixString.split("/"); final String[] full = fullString.split("/"); if (full.length > prefix.length) { final String[] q = ArrayUtils.subarray(full, prefix.length, full.length - 1); path.directories = q; max.value = Math.max(max.value, q.length); } else { path.directories = new String[0]; } path.completePath = fullString; } else { // no selection root so no columns path.path = Path.getPathWithoutSchemeAndAuthority(work.getStatus().getPath()).toString(); path.completePath = path.path; path.directories = new String[0]; } return new WorkAndComponent(path, work); }}).toList();
@Test public void testRemoveLeadingSlash() { assertEquals("", PathUtils.removeLeadingSlash("")); assertEquals("", PathUtils.removeLeadingSlash("/")); assertEquals("aaaa", PathUtils.removeLeadingSlash("/aaaa")); assertEquals("aaaa/bbb", PathUtils.removeLeadingSlash("/aaaa/bbb")); assertEquals("aaaa/bbb", PathUtils.removeLeadingSlash("///aaaa/bbb")); }
private SourceTableDefinition getDataset(NamespaceKey datasetPath, DatasetConfig oldConfig, FormatPlugin formatPlugin, FileSystemWrapper fs, FileConfig fileConfig) throws IOException { final List<FileSystemCachedEntity> cachedEntities = Lists.newArrayList(); final FileStatus rootStatus = fs.getFileStatus(new Path(fileConfig.getLocation())); final Path combined = new Path("/", PathUtils.removeLeadingSlash(fileConfig.getLocation())); final FileSelection fileSelection = FileSelection.create(fs, combined); if (fileSelection == null) { return null; } // first entity is always a root if (rootStatus.isDirectory()) { cachedEntities.add(fromFileStatus(rootStatus)); } for (FileStatus dirStatus: fileSelection.getAllDirectories()) { cachedEntities.add(fromFileStatus(dirStatus)); } if(cachedEntities.isEmpty()){ // this is a single file. cachedEntities.add(fromFileStatus(rootStatus)); } final FileUpdateKey updateKey = new FileUpdateKey().setCachedEntitiesList(cachedEntities); final boolean hasDirectories = fileSelection.containsDirectories(); // Expand selection by copying it first used to check extensions of files in directory. final FileSelection fileSelectionWithoutDir = hasDirectories? fileSelection.minusDirectories(): fileSelection; if(fileSelectionWithoutDir == null){ // no files in the found directory, not a table. return null; } return formatPlugin.getDatasetAccessor(oldConfig, fs, fileSelectionWithoutDir, this, datasetPath, datasetPath.getName(), updateKey); }