예제 #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);
          }
        }
      }
    }
  }
예제 #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;
 }
예제 #3
0
  /**
   * If there's an operator and filename in the list of parameters, then if operator is '>',
   * overwrites the contents of outFile with the string text; if the operator is '>>', appends text
   * to the contents of outFile; If outFile doesn't exist, creates a new file outFile and appends
   * text to its contents.
   *
   * @param params A list of parameters which must include a string and may optionally include an
   *     operator and a filename.
   */
  public String echo(List<String> params) throws Exception {
    // The content to be output in retrieved
    String content = getContent(params);
    // The last index of the string, if it exists, in the List params.
    int last_index = getLastIndex(params);
    // If content is a valid string and the correct syntax is used to write
    // to a file, then this code is executed.
    if (params.size() > last_index + 2
        && content.startsWith("\"")
        && content.endsWith("\"")
        && content.startsWith("\"")
        && params.get(last_index + 1).startsWith(">")) {

      // The file name and parameter for writing is retrieved.
      String file_name = params.get(last_index + 2);
      String param = params.get(last_index + 1);
      // The quotations are striped from the string to be used.
      content = content.substring(1, content.length() - 1);

      // The file is retrieved if it exists.
      JShellItem targetFile = getItemAtPath(file_name, 0);

      // The file is deleted if it exists and is to be overwritten.
      // Otherwise the file is created.
      if (!(param.equals(">>") & targetFile != null)) {
        if (targetFile != null) {
          targetFile.getParentDirectory().removeItem(targetFile);
        }
        mkfile(file_name);
        targetFile = getItemAtPath(file_name, 0);
      }

      // The file's contents are appended to either a blank (new) file
      // or an old file, depending on the user's input.
      ((File) targetFile).setContent(((File) targetFile).getContent() + content);

      return "";
      // If the string is valid but the syntax is incorrect, then this
      // code is
      // executed.
    } else if (content.endsWith("\"") & content.startsWith("\"")) {
      if (content.substring(1, content.length() - 1).isEmpty()) {
        return content.substring(1, content.length() - 1);
      }
      return content.substring(1, content.length() - 1) + "\n";
      // If the string is not valid, this code is executed.
    } else {
      return "Echo requires a string with \" & \" surrounding the words" + "\n";
    }
  }
예제 #4
0
  /**
   * 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);
  }