Example #1
1
  /**
   * 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);
          }
        }
      }
    }
  }
Example #2
0
 /**
  * Gets the JShellItem located numFoldersUp numbers of folders up from the item located at path;
  * path can be a full path or a path relative to the current directory; if there is no such item,
  * returns null.
  *
  * <p>Call JShellItem(path, 0) to get the item located at path.
  *
  * @param path is a string that represents the full path to the JShellItem.
  * @param numFoldersUp
  * @return the JShellItem found at path or null if the item doesn't exist.
  */
 public JShellItem getItemAtPath(String path, int numFoldersUp) throws Exception {
   JShellItem item = currentDirectory_;
   int startIndex = 0;
   if (path.startsWith("/")) {
     startIndex = 1;
     item = rootDirectory_;
   }
   String[] directories = path.split("/");
   while (startIndex < directories.length - numFoldersUp) {
     if (directories[startIndex].equals("..")) {
       item = item.getParentDirectory();
       startIndex++;
     } else if (((Directory) item).contains(directories[startIndex])) {
       if (item instanceof File) {
         throw new ClassCastException(item.getPath() + "is not a Directory");
       }
       item = ((Directory) item).getItem(directories[startIndex]);
       startIndex++;
     } else if (!directories[startIndex].equals(".")) {
       return null;
     } else {
       startIndex++;
     }
   }
   return item;
 }
Example #3
0
  /**
   * @param path
   * @param includeFiles
   * @return
   * @throws Exception
   */
  public List<String> recurseOnPath(String path, boolean includeFiles) throws Exception {

    List<String> paths = new ArrayList<String>();
    JShellItem item = getItemAtPath(path, 0);
    if (!(item instanceof DirectoryAlias) && (includeFiles || item instanceof Directory)) {
      paths.add(item.getPath());
    }
    if (item instanceof File) {
      return paths;
    } else {
      for (JShellItem cont : ((Directory) item).getContents().values()) {
        paths.addAll(0, recurseOnPath(cont.getPath(), includeFiles));
      }
    }
    return paths;
  }
Example #4
0
  /**
   * 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());
      }
    }
  }
Example #5
0
  /**
   * Prints the path of the files specified by the paths argument that contain a string that matches
   * regex, followed by the particular line.
   *
   * @param regex is a String regular expression.
   * @param paths is a List of paths.
   */
  public String grep(String regex, List<String> paths) throws Exception {
    if (regex.startsWith("\"")) {
      int i = 0;
      for (i = 0; i < paths.size() && !regex.endsWith("\""); i++) regex += " " + paths.get(i);
      regex = regex.substring(1, regex.length() - 1);
      paths = paths.subList(i, paths.size());
    }
    if (paths.isEmpty()) throw new Exception("Usage: grep [OPTION]... PATTERN [FILE]...");
    String results = "";

    for (String path : paths) {

      JShellItem item = getItemAtPath(path, 0);
      if (item.getSize() > 0 && currentOptions_.equals("R")) {

        results += grep(regex, recurseOnPath(path, true).subList(0, item.getSize()));
      } else if (item instanceof File) {
        String content = ((File) item).getContent();
        if (content.contains(regex)) {
          if (!results.isEmpty()) results += "\n";
          results += item.getPath() + ":\n";
          for (String line : content.split("\\n")) {
            if (line.contains(regex)) {
              results += line + "\n";
            }
          }
          results = results.substring(0, results.length() - 1);
        }
      } else if (item instanceof Directory && !currentOptions_.equals("R")) {
        results += "Cannot call grep on a directory without -R.";
      } else {
        results += "";
      }
    }
    return results;
  }