Java Code Examples for org.apache.hadoop.fs.Path#isRoot()

The following examples show how to use org.apache.hadoop.fs.Path#isRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicOzoneFileSystem.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the children of the input dir path by iterating though the
 * DeleteIterator.
 *
 * @param f directory path to be deleted
 * @return true if successfully deletes all required keys, false otherwise
 * @throws IOException
 */
private boolean innerDelete(Path f, boolean recursive) throws IOException {
  LOG.trace("delete() path:{} recursive:{}", f, recursive);
  try {
    DeleteIterator iterator = new DeleteIterator(f, recursive);

    if (f.isRoot()) {
      LOG.warn("Cannot delete root directory.");
      return false;
    }

    return iterator.iterate();
  } catch (FileNotFoundException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Couldn't delete {} - does not exist", f);
    }
    return false;
  }
}
 
Example 2
Source File: ExportSnapshot.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create the output folder and optionally set ownership.
 */
private void createOutputPath(final Path path) throws IOException {
  if (filesUser == null && filesGroup == null) {
    outputFs.mkdirs(path);
  } else {
    Path parent = path.getParent();
    if (!outputFs.exists(parent) && !parent.isRoot()) {
      createOutputPath(parent);
    }
    outputFs.mkdirs(path);
    if (filesUser != null || filesGroup != null) {
      // override the owner when non-null user/group is specified
      outputFs.setOwner(path, filesUser, filesGroup);
    }
    if (filesMode > 0) {
      outputFs.setPermission(path, new FsPermission(filesMode));
    }
  }
}
 
Example 3
Source File: PseudoDistributedFileSystem.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public boolean mkdirs(Path f, FsPermission permission) throws IOException {
  Path absolutePath = toAbsolutePath(f);
  checkPath(absolutePath);

  // Handle root
  if (absolutePath.isRoot()) {
    // Path always exists
    return true;
  }

  if (isRemoteFile(absolutePath)) {
    // Attempting to create a subdirectory for a file
    throw new IOException("Cannot create a directory under file " + f);
  }

  return new MkdirsTask(absolutePath, permission).get();
}
 
Example 4
Source File: PseudoDistributedFileSystem.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public FSDataInputStream open(Path f, int bufferSize) throws IOException {
  Path absolutePath = toAbsolutePath(f);
  checkPath(absolutePath);

  // Handle root
  if (absolutePath.isRoot()) {
    throw new AccessControlException("Cannot open " + f);
  }

  try {
    RemotePath remotePath = getRemotePath(absolutePath);

    FileSystem delegate = getDelegateFileSystem(remotePath.address);
    return delegate.open(remotePath.path, bufferSize);
  } catch (IllegalArgumentException e) {
    throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e));
  }
}
 
Example 5
Source File: PseudoDistributedFileSystem.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(Path f, boolean recursive) throws IOException {
  Path absolutePath = toAbsolutePath(f);
  checkPath(absolutePath);

  // Handle root
  if (absolutePath.isRoot()) {
    throw new AccessControlException("Cannot delete " + f);
  }

  if (!isRemoteFile(f)) {
    // In our remote view, there might be a directory, so delete task should handle this case
    return new DeleteTask(absolutePath, recursive).get();
  }

  try {
    RemotePath remotePath = getRemotePath(absolutePath);

    FileSystem delegate = getDelegateFileSystem(remotePath.address);
    return delegate.delete(remotePath.path, recursive);
  } catch (IllegalArgumentException e) {
    throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e));
  }
}
 
Example 6
Source File: PathUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the root path for the specified path.
 *
 * @see Path
 */
public static Path getRootPath(Path path) {
  if (path.isRoot()) {
    return path;
  }
  return getRootPath(path.getParent());
}
 
Example 7
Source File: GoogleDriveFileSystem.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * org.apache.hadoop.fs.Path assumes that there separator in file system naming and "/" is the separator.
 * When org.apache.hadoop.fs.Path sees "/" in path String, it splits into parent and name. As fileID is a random
 * String determined by Google and it can contain "/" itself, this method check if parent and name is separated and
 * restore "/" back to file ID.
 *
 * @param p
 * @return
 */
public static String toFileId(Path p) {
  if (p.isRoot()) {
    return "";
  }
  final String format = "%s" + Path.SEPARATOR + "%s";
  if (p.getParent() != null && StringUtils.isEmpty(p.getParent().getName())) {
    return p.getName();
  }
  return String.format(format, toFileId(p.getParent()), p.getName());
}
 
Example 8
Source File: TestAclCommands.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public FileStatus getFileStatus(Path f) throws IOException {
  if (f.isRoot()) {
    return new FileStatus(0, true, 0, 0, 0, f);
  }
  return null;
}
 
Example 9
Source File: ContractTestUtils.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Block any operation on the root path. This is a safety check
 * @param path path in the filesystem
 * @param allowRootOperation can the root directory be manipulated?
 * @throws IOException if the operation was rejected
 */
public static void rejectRootOperation(Path path,
    boolean allowRootOperation) throws IOException {
  if (path.isRoot() && !allowRootOperation) {
    throw new IOException("Root directory operation rejected: " + path);
  }
}
 
Example 10
Source File: S3AFileSystem.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void deleteUnnecessaryFakeDirectories(Path f) throws IOException {
  while (true) {
    try {
      String key = pathToKey(f);
      if (key.isEmpty()) {
        break;
      }

      S3AFileStatus status = getFileStatus(f);

      if (status.isDirectory() && status.isEmptyDirectory()) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Deleting fake directory " + key + "/");
        }
        s3.deleteObject(bucket, key + "/");
        statistics.incrementWriteOps(1);
      }
    } catch (FileNotFoundException | AmazonServiceException e) {
    }

    if (f.isRoot()) {
      break;
    }

    f = f.getParent();
  }
}
 
Example 11
Source File: PseudoDistributedFileSystem.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new file. Three possibilities:
 *  - This is a data node and you're trying to append a unqualified file => write locally.
 *  - The path you provide is qualified => write to that node.
 *
 *  If this is a client node and you try to write to a unqualified file, we'll throw
 */
@Override
public FSDataOutputStream append(Path f, int bufferSize, Progressable progress) throws IOException {
  Path absolutePath = toAbsolutePath(f);
  checkPath(absolutePath);

  // Handle root
  if (absolutePath.isRoot()) {
    throw new AccessControlException("Cannot open " + f);
  }

  if(!isRemoteFile(f)){
    if (isDirectory(absolutePath)) {
      throw new FileAlreadyExistsException("Directory already exists: " + f);
    }

    // Only fully canonicalized/remote files are allowed
    throw new IOException("Cannot create non-canonical path " + f);
  }

  try {
    RemotePath remotePath = getRemotePath(absolutePath);

    FileSystem delegate = getDelegateFileSystem(remotePath.address);
    return delegate.append(remotePath.path, bufferSize, progress);
  } catch (IllegalArgumentException e) {
    throw (FileNotFoundException) (new FileNotFoundException("No file " + absolutePath).initCause(e));
  }
}
 
Example 12
Source File: PseudoDistributedFileSystem.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new file. Three possibilities:
 *  - This is a data node and you're trying to create a unqualified file => write locally.
 *  - This is a client node and you're trying to create unqualified file => pick a random data node and write there.
 *  - The path you provide is qualified => write to that node.
 */
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize,
    short replication, long blockSize, Progressable progress) throws IOException {
  final Path absolutePath = toAbsolutePath(f);
  checkPath(absolutePath);

  // Handle root
  if (absolutePath.isRoot()) {
    throw new AccessControlException("Cannot create " + f);
  }

  if(!isRemoteFile(f)){
    if (isDirectory(absolutePath)) {
      throw new FileAlreadyExistsException("Directory already exists: " + f);
    }

    // Only canonicalized path/remote files are allowed
    throw new IOException("Cannot create non-canonical path " + f);
  }

  try {
    RemotePath remotePath = getRemotePath(absolutePath);
    return getDelegateFileSystem(remotePath.address).create(remotePath.path, permission, overwrite, bufferSize, replication, blockSize, progress);
  } catch (IllegalArgumentException e) {
    throw (IOException) (new IOException("Cannot create file " + absolutePath).initCause(e));
  }
}
 
Example 13
Source File: Paths.java    From s3committer with Apache License 2.0 5 votes vote down vote up
public static Path getRoot(Path path) {
  Path current = path;
  while (!current.isRoot()) {
    current = current.getParent();
  }
  return current;
}
 
Example 14
Source File: TestAclCommands.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public FileStatus getFileStatus(Path f) throws IOException {
  if (f.isRoot()) {
    return new FileStatus(0, true, 0, 0, 0, f);
  }
  return null;
}
 
Example 15
Source File: ContractTestUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Block any operation on the root path. This is a safety check
 * @param path path in the filesystem
 * @param allowRootOperation can the root directory be manipulated?
 * @throws IOException if the operation was rejected
 */
public static void rejectRootOperation(Path path,
    boolean allowRootOperation) throws IOException {
  if (path.isRoot() && !allowRootOperation) {
    throw new IOException("Root directory operation rejected: " + path);
  }
}
 
Example 16
Source File: S3AFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void deleteUnnecessaryFakeDirectories(Path f) throws IOException {
  while (true) {
    try {
      String key = pathToKey(f);
      if (key.isEmpty()) {
        break;
      }

      S3AFileStatus status = getFileStatus(f);

      if (status.isDirectory() && status.isEmptyDirectory()) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Deleting fake directory " + key + "/");
        }
        s3.deleteObject(bucket, key + "/");
        statistics.incrementWriteOps(1);
      }
    } catch (FileNotFoundException | AmazonServiceException e) {
    }

    if (f.isRoot()) {
      break;
    }

    f = f.getParent();
  }
}
 
Example 17
Source File: FileHiveMetastore.java    From presto with Apache License 2.0 5 votes vote down vote up
private static boolean isChildDirectory(Path parentDirectory, Path childDirectory)
{
    if (parentDirectory.equals(childDirectory)) {
        return true;
    }
    if (childDirectory.isRoot()) {
        return false;
    }
    return isChildDirectory(parentDirectory, childDirectory.getParent());
}
 
Example 18
Source File: CoprocessorWhitelistMasterObserver.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Validates a single whitelist path against the coprocessor path
 * @param  coprocPath the path to the coprocessor including scheme
 * @param  wlPath     can be:
 *                      1) a "*" to wildcard all coprocessor paths
 *                      2) a specific filesystem (e.g. hdfs://my-cluster/)
 *                      3) a wildcard path to be evaluated by
 *                         {@link FilenameUtils#wildcardMatch(String, String)}
 *                         path can specify scheme or not (e.g.
 *                         "file:///usr/hbase/coprocessors" or for all
 *                         filesystems "/usr/hbase/coprocessors")
 * @return             if the path was found under the wlPath
 */
private static boolean validatePath(Path coprocPath, Path wlPath) {
  // verify if all are allowed
  if (wlPath.toString().equals("*")) {
    return(true);
  }

  // verify we are on the same filesystem if wlPath has a scheme
  if (!wlPath.isAbsoluteAndSchemeAuthorityNull()) {
    String wlPathScheme = wlPath.toUri().getScheme();
    String coprocPathScheme = coprocPath.toUri().getScheme();
    String wlPathHost = wlPath.toUri().getHost();
    String coprocPathHost = coprocPath.toUri().getHost();
    if (wlPathScheme != null) {
      wlPathScheme = wlPathScheme.toString().toLowerCase();
    } else {
      wlPathScheme = "";
    }
    if (wlPathHost != null) {
      wlPathHost = wlPathHost.toString().toLowerCase();
    } else {
      wlPathHost = "";
    }
    if (coprocPathScheme != null) {
      coprocPathScheme = coprocPathScheme.toString().toLowerCase();
    } else {
      coprocPathScheme = "";
    }
    if (coprocPathHost != null) {
      coprocPathHost = coprocPathHost.toString().toLowerCase();
    } else {
      coprocPathHost = "";
    }
    if (!wlPathScheme.equals(coprocPathScheme) || !wlPathHost.equals(coprocPathHost)) {
      return(false);
    }
  }

  // allow any on this file-system (file systems were verified to be the same above)
  if (wlPath.isRoot()) {
    return(true);
  }

  // allow "loose" matches stripping scheme
  if (FilenameUtils.wildcardMatch(
      Path.getPathWithoutSchemeAndAuthority(coprocPath).toString(),
      Path.getPathWithoutSchemeAndAuthority(wlPath).toString())) {
    return(true);
  }
  return(false);
}
 
Example 19
Source File: BasicOzoneFileSystem.java    From hadoop-ozone with Apache License 2.0 3 votes vote down vote up
/**
 * Create a fake parent directory key if it does not already exist and no
 * other child of this parent directory exists.
 *
 * @param f path to the fake parent directory
 * @throws IOException
 */
private void createFakeParentDirectory(Path f) throws IOException {
  Path parent = f.getParent();
  if (parent != null && !parent.isRoot()) {
    createFakeDirectoryIfNecessary(parent);
  }
}
 
Example 20
Source File: BasicRootedOzoneFileSystem.java    From hadoop-ozone with Apache License 2.0 3 votes vote down vote up
/**
 * Create a fake parent directory key if it does not already exist and no
 * other child of this parent directory exists.
 *
 * @param f path to the fake parent directory
 * @throws IOException
 */
private void createFakeParentDirectory(Path f) throws IOException {
  Path parent = f.getParent();
  if (parent != null && !parent.isRoot()) {
    createFakeDirectoryIfNecessary(parent);
  }
}