public Directory getLastDirectory(String path, Directory currentDir, User currentUser) throws FileUnknownException, PathIsTooBigException, AccessDeniedException { if (path.equals("/")) { getSlash().checkAccessEx(currentUser); return getSlash(); } if (path.equals(".")) return currentDir; else if (path.equals("..")) { currentDir.getFather().checkAccessEx(currentUser); return currentDir.getFather(); } Directory beforeLast = absolutePath(path, currentUser, currentDir); String[] tokens = path.split(PATH_DELIM); String name = tokens.length == 0 ? path : tokens[tokens.length - 1]; beforeLast.checkAccessEx(currentUser); beforeLast.changeDirectory(name, currentUser); return (Directory) beforeLast.getFileByName(name); }
Directory absolutePath(String path, User currentUser, Directory currentDirectory) throws FileUnknownException, PathIsTooBigException { String resultantPath; if (path.equals(PATH_DELIM)) { return getFsRoot(); } else if (path.startsWith(PATH_DELIM)) { resultantPath = path; } else { resultantPath = currentDirectory.getPath().equals(PATH_DELIM) ? currentDirectory.getPath() + path : currentDirectory.getPath() + PATH_DELIM + path; } if ((resultantPath.length() > MAX_PATH_SIZE)) { throw new PathIsTooBigException(path); } Directory directory = getSlash(); String[] fileLocation = resultantPath.split(PATH_DELIM); for (int i = 1; i < (fileLocation.length - 1); i++) directory = changeDirectory(fileLocation[i], directory, getRoot()); directory.checkAccessEx(currentUser); return directory; }
private Directory changeDirectory( String directoryName, Directory currentDirectory, User currentUser) throws FileUnknownException, AccessDeniedException { currentDirectory.checkAccessEx(currentUser); return currentDirectory.changeDirectory(directoryName, currentUser); }