Skip to content

Commit

Permalink
Remove peekInode(boolean) method.
Browse files Browse the repository at this point in the history
Replace it with two methods:
1. peekInode() that always returns the last existing inode in the
InodePath, or null if there is none.
2. getParent() that returns the parent of the target inode, or null if
the parent does not exist.
  • Loading branch information
riversand9 committed Aug 23, 2017
1 parent 511d1fc commit 63e7a0e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
Expand Up @@ -1107,8 +1107,12 @@ public long createFile(AlluxioURI path, CreateFileOptions options)
try (JournalContext journalContext = createJournalContext(); try (JournalContext journalContext = createJournalContext();
LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE); LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE);
MasterAuditContext auditContext = MasterAuditContext auditContext =
createAuditContext("createFile", path, null, createAuditContext("createFile", path, null, null)) {
inodePath.peekInode(options.isRecursive()))) { if (options.isRecursive()) {
auditContext.setSrcInode(inodePath.peekInode());
} else {
auditContext.setSrcInode(inodePath.getParentInode());
}
try { try {
mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath); mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath);
} catch (AccessControlException e) { } catch (AccessControlException e) {
Expand Down Expand Up @@ -1823,8 +1827,12 @@ public long createDirectory(AlluxioURI path, CreateDirectoryOptions options)
try (JournalContext journalContext = createJournalContext(); try (JournalContext journalContext = createJournalContext();
LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE); LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE);
MasterAuditContext auditContext = MasterAuditContext auditContext =
createAuditContext("mkdir", path, null, createAuditContext("mkdir", path, null, null)) {
inodePath.peekInode(options.isRecursive()))) { if (options.isRecursive()) {
auditContext.setSrcInode(inodePath.peekInode());
} else {
auditContext.setSrcInode(inodePath.getParentInode());
}
try { try {
mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath); mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath);
} catch (AccessControlException e) { } catch (AccessControlException e) {
Expand Down Expand Up @@ -1914,7 +1922,7 @@ public void rename(AlluxioURI srcPath, AlluxioURI dstPath, RenameOptions options
MasterAuditContext auditContext = createAuditContext("rename", srcPath, dstPath, null)) { MasterAuditContext auditContext = createAuditContext("rename", srcPath, dstPath, null)) {
LockedInodePath srcInodePath = inodePathPair.getFirst(); LockedInodePath srcInodePath = inodePathPair.getFirst();
LockedInodePath dstInodePath = inodePathPair.getSecond(); LockedInodePath dstInodePath = inodePathPair.getSecond();
auditContext.setSrcInode(srcInodePath.peekInode(false)); auditContext.setSrcInode(srcInodePath.getParentInode());
try { try {
mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, srcInodePath); mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, srcInodePath);
mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, dstInodePath); mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, dstInodePath);
Expand Down Expand Up @@ -2382,8 +2390,12 @@ public long loadMetadata(AlluxioURI path, LoadMetadataOptions options)
InvalidFileSizeException, FileAlreadyCompletedException, IOException, AccessControlException { InvalidFileSizeException, FileAlreadyCompletedException, IOException, AccessControlException {
try (JournalContext journalContext = createJournalContext(); try (JournalContext journalContext = createJournalContext();
LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE); LockedInodePath inodePath = mInodeTree.lockInodePath(path, InodeTree.LockMode.WRITE);
MasterAuditContext auditContext = createAuditContext("loadMetadata", path, null, MasterAuditContext auditContext = createAuditContext("loadMetadata", path, null, null)) {
inodePath.peekInode(options.isCreateAncestors()))) { if (options.isCreateAncestors()) {
auditContext.setSrcInode(inodePath.peekInode());
} else {
auditContext.setSrcInode(inodePath.getParentInode());
}
try { try {
mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath); mPermissionChecker.checkParentPermission(Mode.Bits.WRITE, inodePath);
} catch (AccessControlException e) { } catch (AccessControlException e) {
Expand Down
Expand Up @@ -91,43 +91,42 @@ public synchronized InodeFile getInodeFile() throws FileDoesNotExistException {
* @throws InvalidPathException if the parent inode is not a directory * @throws InvalidPathException if the parent inode is not a directory
* @throws FileDoesNotExistException if the parent of the target does not exist * @throws FileDoesNotExistException if the parent of the target does not exist
*/ */
public synchronized InodeDirectory getParentInodeDirectory() public synchronized InodeDirectory getParentInodeDirectory()
throws InvalidPathException, FileDoesNotExistException { throws InvalidPathException, FileDoesNotExistException {
InodeDirectory inodeDirectory = (InodeDirectory) peekInode(false); Inode inode = getParentInode();
if (inodeDirectory == null) { if (inode == null) {
throw new FileDoesNotExistException( throw new FileDoesNotExistException(
ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(mUri.getParent())); ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(mUri.getParent()));
} }
return inodeDirectory; if (!inode.isDirectory()) {
throw new InvalidPathException(
ExceptionMessage.PATH_MUST_HAVE_VALID_PARENT.getMessage(mUri));
}
return (InodeDirectory) inode;
}

/**
* Checks and returns the parent inode of the target inode.
*
* @return the parent of the target inode, or null if the parent does not exist
*/
public synchronized Inode getParentInode() {
if (mPathComponents.length < 2 || mInodes.size() < (mPathComponents.length - 1)) {
// The path is only the root, or the list of inodes is not long enough to contain the parent
return null;
}
return mInodes.get(mPathComponents.length - 2);
} }


/** /**
* Peeks an inode on the inode path. The choice of which inode depends on whether the request * Peeks an inode on the inode path. The choice of which inode depends on whether the request
* comes from a command with recursive flag or not. * comes from a command with recursive flag or not.
* *
* @param isRecursive true if this peekInode is invoked by a command with recursive flag * @return the last existing inode on the inode path
* @return the last existing inode on the inode path if isRecursive is true, the second
* last inode on the inode path if isRecursive is false
* @throws InvalidPathException if the parent exists but is not a directory * @throws InvalidPathException if the parent exists but is not a directory
*/ */
public synchronized Inode peekInode(boolean isRecursive) public synchronized Inode peekInode() {
throws InvalidPathException { return mInodes.get(mInodes.size() - 1);
Inode<?> inode;
if (!isRecursive) {
if (mPathComponents.length < 2 || mInodes.size() < (mPathComponents.length - 1)) {
// The path is only the root, or the list of inodes is not long enough to contain the parent
return null;
}
inode = mInodes.get(mPathComponents.length - 2);
if (!inode.isDirectory()) {
throw new InvalidPathException(
ExceptionMessage.PATH_MUST_HAVE_VALID_PARENT.getMessage(mUri));
}
return inode;
} else {
inode = mInodes.get(mInodes.size() - 1);
return inode;
}
} }


/** /**
Expand Down

0 comments on commit 63e7a0e

Please sign in to comment.