Exemple #1
0
  /**
   * Extends a <code>File</code> by the given size in bytes. Pre-condition: size must be greater
   * than 0 Post-condition: <code>File</code> with the given filename will be extended by passed in
   * amount of bytes, and additional <code>Blocks</code> will be allocated to it as needed
   *
   * @param filename the name of the <code>File</code>
   * @param size number of bytes to increase the <code>File</code> by
   * @return successful extension message
   * @throws InSufficientDiskSpaceException if there are not enough blocks to extend <code>File
   *     </code>
   * @throws NoSuchFileException if the filename does not match any existing <code>File</code>
   */
  public String extend(String filename, int size)
      throws InsufficientDiskSpaceException, NoSuchFileException {

    Iterator<File> findFile = FAT32.iterator();
    boolean fileFound = false;

    // Check if file exists
    while (findFile.hasNext()) {
      if (findFile.next().getName().equalsIgnoreCase(filename)) {
        fileFound = true;
      }
    }

    // Throw exception if it doesn't
    if (fileFound == false) {
      throw new NoSuchFileException(filename + " does not exist!");
    }

    // Check there is enough space to extend file
    if (size > (freeBlocks.size() * blockSize)) {
      throw new InsufficientDiskSpaceException(
          "Not enough disk space to extend " + filename + " \n");
    } else {

      Iterator<File> itr = FAT32.iterator();
      // Iterate through list of files to find filename
      while (itr.hasNext()) {
        File file = itr.next();
        if (file.getName().equals(filename)) {
          // Calculate the extended size and how many blocks need to be added
          int newSize = file.getBytes() + size;
          int blocksToAdd = bytesBlocks(newSize) - bytesBlocks(file.getBytes());
          // Take blocks from available blocks and add them to the file
          for (int i = 0; i < blocksToAdd; i++) {
            file.add(freeBlocks.remove(0));
          }
          file.addBytes(size);
        }
      }

      // Throw exception or return appropriate message
      if (fileFound == false) {
        throw new NoSuchFileException(filename + " does not exist!");
      } else {
        return filename + " extended successfully!\n";
      }
    }
  }
Exemple #2
0
  /**
   * Deletes a <code>File</code> with the given name from the internal <code>File</code> list
   * Post-condition: <code>File</code> with the passed in filename will be deleted and its <code>
   * Blocks</code> added back to available <code>Blocks</code>
   *
   * @param filename the name of the <code>File</code>
   * @return successful deletion message
   * @throws NoSuchFileException if the filename does not match any existing <code>File</code>
   */
  public String delete(String filename) throws NoSuchFileException {

    Iterator<File> itr = FAT32.iterator();
    boolean fileFound = false;

    // Iterate through list of files to look for filename
    while (itr.hasNext()) {
      File file = itr.next();
      if (file.getName().equals(filename)) {
        fileFound = true;
        // Iterate through file's blocks, adding them back to available blocks
        for (int i = 0; i < bytesBlocks(file.getBytes()); i++) {
          freeBlocks.add(file.remove());
        }
        // Remove file
        itr.remove();
      }
    }

    // Throw an exception or return appropriate message
    if (fileFound == false) {
      throw new NoSuchFileException(filename + " does not exist!");
    } else {
      return filename + " deleted successfully!\n";
    }
  }
Exemple #3
0
  /**
   * Truncates a <code>File</code> by the given size in bytes. Pre-condition: size must be greater
   * than 0 Post-condition: <code>File</code> with the given filename will be truncated by passed in
   * amount of bytes, and <code>Blocks</code> will be de-allocated to it as needed
   *
   * @param filename the name of the <code>File</code>
   * @param size number of bytes to truncate the <code>File</code> by
   * @return successful truncation message
   * @throws NoSuchFileException if the filename does not match any existing <code>File</code>
   * @throws FileUnderflowException if number of truncating bytes is greater than <code>File</code>
   *     size
   */
  public String truncate(String filename, int size)
      throws NoSuchFileException, FileUnderflowException {

    Iterator<File> itr = FAT32.iterator();
    boolean fileFound = false;
    // Iterate through list of files to find filename
    while (itr.hasNext()) {
      File file = itr.next();
      if (file.getName().equals(filename)) {
        fileFound = true;
        // If truncating by total file size, delete the file instead
        if (file.getBytes() == size) {
          delete(filename);
        } else if (file.getBytes() < size) {
          throw new FileUnderflowException(
              "Tried to truncate "
                  + filename
                  + " by "
                  + size
                  + " bytes, but it is only "
                  + file.getBytes()
                  + " bytes!\n");
        } else {
          // Calculate the truncated size and how many blocks need to be removed
          int newSize = file.getBytes() - size;
          int blocksToRemove = bytesBlocks(file.getBytes()) - bytesBlocks(newSize);
          // Take blocks from file and add them to available blocks
          for (int i = 0; i < blocksToRemove; i++) {
            freeBlocks.add(file.remove());
          }
          file.delBytes(size);
        }
      }
    }

    // Throw exception or return appropriate message
    if (fileFound == false) {
      throw new NoSuchFileException(filename + " does not exist!");
    } else {
      return filename + " truncated successfully!\n";
    }
  }