public boolean deleteFSObject(String rawPath) { FSObject obj = findFSObject(rawPath); if (obj == null || !obj.isWritePermission()) { return false; } return obj.delete(); }
/** * Return the VFS object represented by the given {@code rowPath}. * * @param rawPath * @return {@code null} if the object does not exist in the VFS */ public FSObject findFSObject(String rawPath) { String path = new File(rawPath).getAbsolutePath(); String[] tokens = tokenize(path); markAccessedFile(path); VFolder parent = root; for (int i = 0; i < tokens.length; i++) { String name = tokens[i]; FSObject child = parent.getChild(name); // child might not exist if (child == null || child.isDeleted()) { return null; } // we might end up with a file on the path that is not a folder if (i < (tokens.length - 1) && !child.isFolder()) { return null; } else { if (i == (tokens.length - 1)) { return child; } } parent = (VFolder) child; } return parent; }
public boolean createFolder(String rawPath) { String[] tokens = tokenize(new File(rawPath).getAbsolutePath()); VFolder parent = root; for (String name : tokens) { if (!parent.isReadPermission() || !parent.isWritePermission() || parent.isDeleted()) { return false; } VFolder folder = null; if (!parent.hasChild(name)) { String path = null; if (isUnixStyle() || !parent.isRoot()) { path = parent.getPath() + File.separator + name; } else { // this means we are in Windows, and that we are considering a direct child of the root path = name; } folder = new VFolder(path, parent); } else { FSObject child = parent.getChild(name); if (!child.isFolder()) { return false; } folder = (VFolder) child; } parent.addChild(folder); parent = folder; } markAccessedFile(parent.getPath()); return true; }
public boolean rename(String source, String destination) { String parentSource = new File(source).getParent(); String parentDest = new File(destination).getParent(); if ((parentSource == null && parentDest != null) || (!parentSource.equals(parentDest))) { // both need to be in the same folder return false; } FSObject src = findFSObject(source); if (src == null) { // source should exist return false; } FSObject dest = findFSObject(destination); if (dest != null) { // destination should not exist return false; } return src.rename(destination); }