/** * 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"; } }
/** * Removes all the files in this folder. * * @return True if any files were successfully removed */ public boolean removeFiles() { final File[] files = getFiles(); boolean success = true; for (File element : files) { success = element.remove() && success; } return success; }
void removeFile(String path, User currentUser, Directory currentDir) throws FileUnknownException, AccessDeniedException { File fileToRemove = absolutePath(path, currentUser, currentDir).getFileByName(getLastPathToken(path)); fileToRemove.checkAccessDelete(currentUser); try { fileToRemove.isEmpty(); for (File f : getRecursiveRemovalContent((Directory) fileToRemove)) { f.checkAccessDelete(currentUser); f.remove(); } } catch (IsNotDirectoryException e) { // } finally { fileToRemove.remove(); } }
/** * 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"; } }