/** * Prompt to confirm that the user wants to delete the JShellItem from the file system. If so, * remove it. If -f is supplied, do not prompt and confirm. If the root directory is specified, * its contents but not itself will be subject to deletion. * * @param paths the JShellItem to be removed. */ public void rm(List<String> paths) throws Exception { // Loop through the user inputs. for (String path : paths) { // Initialize the current JShellItem. JShellItem item = getItemAtPath(path, 0); if (item == null) { System.out.printf("%s: does not exist.\n", path); } else { // Keep track of the size of that JShellItem. int size = item.getSize(); if (size > 0) { List<String> recursiveList = recurseOnPath(path, true); rm(recursiveList.subList(0, size + 1)); if (item.getSize() == 0) { rm(recursiveList.subList(size + 1, size + 2)); } } else { if (item.getPath().equals("/")) { return; } // Check the -f option. if (!currentOptions_.equals("f")) { System.out.printf( "Really remove %s from %s? (y/n) ", item.getName(), item.getParentDirectory().getPath()); // Read the user input. ArrayList<String> in = readInput(); // If 'y' if (in.toString().equals("[y]")) { item.getParentDirectory().removeItem(item); // if not 'n', ask again. } else if (!in.toString().equals("[n]")) { List<String> tempStore = new ArrayList<String>(); tempStore.add(path); rm(tempStore); } // If -f is specified. } else { item.getParentDirectory().removeItem(item); } } } } }
// TODO: Make ln fit the 30 line public void ln(String firstPath, String secondPath) throws Exception { // Initialize the variables. JShellItem targetItem; JShellItem aliasItem; String newName; Directory destinationDirectory; // Find and reference the target item. Throw Exception if the path // doesn't exist. targetItem = getItemAtPath(firstPath, 0); if (targetItem == null) { throw new Exception(firstPath + ": doesn't exist."); } // Find and reference the destination Directory. if (secondPath.endsWith("/")) { destinationDirectory = (Directory) getItemAtPath(secondPath, 0); newName = targetItem.getName(); } else { destinationDirectory = (Directory) getItemAtPath(secondPath, 1); newName = secondPath.substring(secondPath.lastIndexOf("/") + 1); } // If the Directory destination is not found. if (destinationDirectory == null) { throw new Exception(secondPath + " is an invalid destination path."); } // Throw Exception when there is a JShellItem that already exist within // the destination Directory. if (destinationDirectory.contains(newName)) { throw new Exception(secondPath + ": already exists."); } // If the target item is a Directory object. if (targetItem instanceof Directory) // Create a new Directory object that will references it's contents // from the target item. aliasItem = new DirectoryAlias( newName, destinationDirectory.getPath() + newName + "/", destinationDirectory, (Directory) targetItem); // If the target item is a File object. else // Create a new FileAlias object that will reference it's contents // from the target item. aliasItem = new FileAlias( newName, destinationDirectory.getPath() + newName, destinationDirectory, (File) targetItem); // Add the alias object to the destination Directory. destinationDirectory.addItem(aliasItem); }
/** * Copy file or directory oldFile to newFile; * * @param oldFile a full path to a JShellItem or the name of a JShellItem in the current directory * which needs to be copied. * @param newFile a full path to a directory or the name of a directory in the current directory * to which oldFile needs to be copied. */ public void cp(String oldPath, String newPath) throws Exception { Directory destinationParent; String newName; if (oldPath.equals(newPath)) { return; } JShellItem oldItem = getItemAtPath(oldPath, 0); if (oldItem == null) { throw new Exception(oldItem + " does not exist."); } if (newPath.endsWith("/")) { destinationParent = (Directory) getItemAtPath(newPath, 0); newName = oldItem.getName(); } else { destinationParent = (Directory) getItemAtPath(newPath, 1); newName = newPath.substring(newPath.lastIndexOf("/") + 1); } if (destinationParent == null) { throw new Exception(newPath + " is an invalid destination path."); } if (destinationParent.contains(newName)) { throw new Exception(destinationParent.getPath() + newName + " already exists."); } if (oldItem instanceof File) { File newItem = new File( newName, destinationParent.getPath() + "/" + newName, destinationParent, ((File) oldItem).getContent()); destinationParent.addItem(newItem); } else { if (destinationParent.isChildOf((Directory) oldItem)) { throw new Exception( String.format("cp: cannot copy '%s' into itself, '%s'", oldPath, newPath)); } Directory newItem = new Directory(newName, destinationParent.getPath() + newName + "/", destinationParent); destinationParent.addItem(newItem); for (JShellItem item : ((Directory) oldItem).getContents().values()) { cp(item.getPath(), newItem.getPath()); } } }
/** * Move file or directory oldFile to newFile; * * @param oldFile a full path to a JShellItem or the name of a JShellItem in the current directory * which needs to be moved. * @param newFile a full path to a directory or the name of a directory in the current directory * to which oldFile needs to be moved. */ public void mv(String oldFile, String newFile) throws Exception { JShellItem source = getItemAtPath(oldFile, 0); if (source == null) { throw new Exception(oldFile + ": doesn't exist."); } Directory sourceParent = source.getParentDirectory(); Directory destinationParent; String newName; if (newFile.endsWith("/")) { destinationParent = (Directory) getItemAtPath(newFile, 0); newName = source.getName(); } else { destinationParent = (Directory) getItemAtPath(newFile, 1); newName = newFile.substring(newFile.lastIndexOf("/") + 1); } if (destinationParent == null) { throw new Exception(newFile + ": invalid destination path."); } if (destinationParent.contains(newName)) { throw new Exception(newFile + ": already exists."); } sourceParent.removeItem(source); source.setName(newName); if (source instanceof Directory) { if (destinationParent.isChildOf((Directory) source)) { throw new Exception( String.format( "mv: cannot move '%s' to a subdirectory of itself, '%s'", oldFile, newFile)); } newName.concat("/"); } source.setPath(destinationParent.getPath() + newName); source.setParentDirectory(destinationParent); destinationParent.addItem(source); }