* @throws InvalidPathException
*/
private Pair<Inode, Integer> traverseToInode(String[] pathNames) throws InvalidPathException {
synchronized (mRootLock) {
if (pathNames == null || pathNames.length == 0) {
throw new InvalidPathException("passed-in pathNames is null or empty");
}
if (pathNames.length == 1) {
if (pathNames[0].equals("")) {
return new Pair<Inode, Integer>(mRoot, -1);
} else {
final String msg = "File name starts with " + pathNames[0];
LOG.info("InvalidPathException: " + msg);
throw new InvalidPathException(msg);
}
}
Pair<Inode, Integer> ret = new Pair<Inode, Integer>(mRoot, -1);
for (int k = 1; k < pathNames.length; k ++) {
Inode next = ((InodeFolder) ret.getFirst()).getChild(pathNames[k]);
if (next == null) {
// The user might want to create the nonexistent directories, so we leave ret.getFirst()
// as the last Inode taken. We set nonexistentInd to k, to indicate that the kth path
// component was the first one that couldn't be found.
ret.setSecond(k);
break;
}
ret.setFirst(next);
if (!ret.getFirst().isDirectory()) {
// The inode can't have any children. If this is the last path component, we're good.
// Otherwise, we can't traverse further, so we clean up and throw an exception.
if (k == pathNames.length - 1) {
break;
} else {
final String msg =
"Traversal failed. Component " + k + "(" + ret.getFirst().getName() + ") is a file";
LOG.info("InvalidPathException: " + msg);
throw new InvalidPathException(msg);
}
}
}
return ret;
}