下面列出了org.apache.hadoop.fs.Path#isRoot ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
@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));
}
}
@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));
}
}
@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();
}
/**
* 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));
}
}
}
private static boolean isChildDirectory(Path parentDirectory, Path childDirectory)
{
if (parentDirectory.equals(childDirectory)) {
return true;
}
if (childDirectory.isRoot()) {
return false;
}
return isChildDirectory(parentDirectory, childDirectory.getParent());
}
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();
}
}
/**
* 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);
}
}
@Override
public FileStatus getFileStatus(Path f) throws IOException {
if (f.isRoot()) {
return new FileStatus(0, true, 0, 0, 0, f);
}
return null;
}
public static Path getRoot(Path path) {
Path current = path;
while (!current.isRoot()) {
current = current.getParent();
}
return current;
}
/**
* 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));
}
}
/**
* 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));
}
}
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();
}
}
/**
* 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);
}
}
@Override
public FileStatus getFileStatus(Path f) throws IOException {
if (f.isRoot()) {
return new FileStatus(0, true, 0, 0, 0, f);
}
return null;
}
/**
* 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());
}
/**
* 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());
}
/**
* 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);
}
/**
* 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);
}
}
/**
* 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);
}
}