@Override public void delete() throws IOException, UnsupportedFileOperationException { file.delete(); }
/** * Deletes the given file. If the file is a directory, enclosing files are deleted recursively. * Symbolic links to directories are simply deleted, without deleting the contents of the linked directory. * * @param file the file to delete * @throws IOException if an error occurred while deleting a file or listing a directory's contents * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ protected final void deleteRecursively(AbstractFile file) throws IOException, UnsupportedFileOperationException { if(file.isDirectory() && !file.isSymlink()) { AbstractFile children[] = file.ls(); for (AbstractFile child : children) deleteRecursively(child); } file.delete(); }
/** * Deletes the given file, either by moving it to the trash (if {@link #moveToTrash} is true) or by deleting the * file directly. * * @param file the file to delete * @throws IOException if an error occurred while deleting the file */ private void deleteFile(AbstractFile file) throws IOException { if(moveToTrash) trash.moveToTrash(file); else file.delete(); }
/** * Overwrites the target file with the backup one. * @exception IOException thrown if any IO related error occurs. */ private void backup() throws IOException { // Deletes the destination file (AbstractFile.copyTo now fails when the destination exists). if(target.exists()) target.delete(); // We're not using backup.moveTo(target) because we want to make absolutely sure // that if an error occurs in the middle of the operation, at least one of the two files // is complete. backup.copyTo(target); backup.delete(); }
public static void deleteCustomTheme(String name) throws IOException { AbstractFile file; // Makes sure the specified theme is not the current one. if(isCurrentTheme(Theme.CUSTOM_THEME, name)) throw new IllegalArgumentException("Cannot delete current theme."); // Deletes the theme. file = getCustomThemesFolder().getChild(name + ".xml"); if(file.exists()) file.delete(); }
/** * Writes the current tool bar to the user's tool bar file. * @throws IOException */ public static void saveToolBar() throws IOException { if (ToolBarAttributes.areDefaultAttributes()) { AbstractFile toolBarFile = getDescriptionFile(); if (toolBarFile != null && toolBarFile.exists()) { LOGGER.info("Toolbar use default settings, removing descriptor file"); toolBarFile.delete(); } else LOGGER.debug("Toolbar not modified, not saving"); } else if (toolBarWriter != null) { if (wasToolBarModified) toolBarWriter.write(); else LOGGER.debug("Toolbar not modified, not saving"); } else LOGGER.warn("Could not save toolbar. writer is null"); }
/** * Writes the current command bar to the user's command bar file. * @throws IOException * @throws IOException */ public static void saveCommandBar() throws IOException { if (CommandBarAttributes.areDefaultAttributes()) { AbstractFile commandBarFile = getDescriptionFile(); if(commandBarFile != null && commandBarFile.exists()) { LOGGER.info("Command bar use default settings, removing descriptor file"); commandBarFile.delete(); } else LOGGER.debug("Command bar not modified, not saving"); } else if (commandBarWriter != null) { if (wasCommandBarModified) commandBarWriter.write(); else LOGGER.debug("Command bar not modified, not saving"); } else LOGGER.warn("Could not save command bar. writer is null"); }
/** * Tests {@link AbstractFile#delete()} when the operation is not supported. * * @throws IOException should not happen */ protected void testDeleteUnsupported() throws IOException { // Assert that #delete throws a proper UnsupportedFileOperationException when called UnsupportedFileOperationException e = null; try { tempFile.delete(); } catch(UnsupportedFileOperationException ex) { e = ex; } assertUnsupportedFileOperationException(e, FileOperation.DELETE); }
@Override public void renameTo(AbstractFile destFile) throws IOException { // Throw an exception if the file cannot be renamed to the specified destination checkRenamePrerequisites(destFile, false, false); // Delete the destination if it already exists as FileSystem#rename would otherwise fail. // Note: HadoopFile#delete() does not delete directories recursively (good). if(destFile.exists()) destFile.delete(); if(!fs.rename(path, ((HadoopFile)destFile).path)) throw new IOException(); // Update destination file attributes by fetching them from the server ((HadoopFileAttributes)destFile.getUnderlyingFileObject()).fetchAttributes(); // Update this file's attributes locally fileAttributes.setExists(false); fileAttributes.setDirectory(false); fileAttributes.setSize(0); }
/** * Validates that FileMonitor properly reports {@link FileMonitor#IS_DIRECTORY_ATTRIBUTE} changes when a file * becomes a directory and vice-versa. * * @throws IOException should not normally happen */ @Test public void testIsDirectoryAttribute() throws IOException { setUp(IS_DIRECTORY_ATTRIBUTE); file.delete(); file.mkdir(); assert hasAttributeChanged(IS_DIRECTORY_ATTRIBUTE); file.delete(); file.mkfile(); assert hasAttributeChanged(IS_DIRECTORY_ATTRIBUTE); }
/** * Tests {@link com.mucommander.commons.file.AbstractFile#exists()} in various situations. * * @throws IOException should not happen */ @Test public void testExists() throws IOException { assert !tempFile.exists(); tempFile.mkfile(); assert tempFile.exists(); tempFile.delete(); assert !tempFile.exists(); tempFile.mkdir(); assert tempFile.exists(); tempFile.delete(); assert !tempFile.exists(); }
/** * Validates that FileMonitor properly reports {@link FileMonitor#EXISTS_ATTRIBUTE} changes when an existing file or * directory is deleted, or when a non-existing file or directory is created. * * @throws IOException should not normally happen */ @Test public void testExistsAttribute() throws IOException { setUp(EXISTS_ATTRIBUTE); file.delete(); assert hasAttributeChanged(EXISTS_ATTRIBUTE); file.mkdir(); assert hasAttributeChanged(EXISTS_ATTRIBUTE); file.delete(); assert hasAttributeChanged(EXISTS_ATTRIBUTE); file.mkfile(); assert hasAttributeChanged(EXISTS_ATTRIBUTE); }
/** * Imports the specified file in muCommander's libraries. * @param file path to the library to import. * @param force wether to overwrite eventual existing libraries of the same name. * @return <code>true</code> if the operation was a success, * <code>false</code> if a library of the same name already exists and * <code>force</code> is set to <code>false</code>. * @throws IOException if an I/O error occurs. */ public static boolean importLibrary(AbstractFile file, boolean force) throws IOException { AbstractFile dest; // If the file is already in the extensions or classpath, // there's nothing to do. if(isAvailable(file)) return true; // If the destination file already exists, either delete it // if force is set to true or just return false. dest = getExtensionsFile(file.getName()); if(dest.exists()) { if(!force) return false; dest.delete(); } // Copies the library and adds it to the extensions classpath. file.copyTo(dest); addToClassPath(dest); return true; }
/** * Tests {@link AbstractFile#getAbsolutePath()} by asserting that it returns a non-null value, that the file can * be resolved again using this path, and that the resolved file is the same as the orginal file. * The tests are performed on a regular file and a directory file. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testAbsolutePath() throws IOException, NoSuchAlgorithmException { // Regular file createFile(tempFile, 1); testPathResolution(tempFile, tempFile.getAbsolutePath()); // Directory file tempFile.delete(); tempFile.mkdir(); testPathResolution(tempFile, tempFile.getAbsolutePath()); // Test getAbsolutePath(boolean) on the directory file assert tempFile.getAbsolutePath(true).endsWith(tempFile.getSeparator()); assert !tempFile.getAbsolutePath(false).endsWith(tempFile.getSeparator()); }
/** * Asserts that a file can be renamed to a filename variation of the same file. * * @throws IOException should not normally happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testRenameToCaseVariation() throws IOException, NoSuchAlgorithmException { // First test with a regular file createFile(tempFile, 1); AbstractFile destFile = tempFile.getParent().getDirectChild(tempFile.getName().toUpperCase()); deleteWhenFinished(destFile); tempFile.renameTo(destFile); assert !destFile.isSymlink(); // Leave me // Repeat the test with a directory destFile.delete(); tempFile.mkdir(); tempFile.renameTo(destFile); assert !destFile.isSymlink(); // Leave me }
/** * Tests {@link AbstractFile#getCanonicalPath()} by asserting that it returns a non-null value, that the file can * be resolved again using this path, and that the resolved file is the same as the orginal file. * The tests are performed on a regular file and a directory file. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testCanonicalPath() throws IOException, NoSuchAlgorithmException { // Regular file createFile(tempFile, 1); testPathResolution(tempFile.getCanonicalFile(), tempFile.getCanonicalPath()); // Directory file tempFile.delete(); tempFile.mkdir(); testPathResolution(tempFile.getCanonicalFile(), tempFile.getCanonicalPath()); // Test getCanonicalPath(boolean) on the directory file assert tempFile.getCanonicalPath(true).endsWith(tempFile.getSeparator()); assert !tempFile.getCanonicalPath(false).endsWith(tempFile.getSeparator()); }
tempFile.delete(); tempFile.mkfile();
/** * Tests {@link AbstractFile#getURL()} by asserting that it returns a non-null value, that the file can * be resolved again using its string representation (with credentials), and that the resolved file is the same as * the orginal file. The tests are performed on a regular file and a directory file. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testFileURL() throws IOException, NoSuchAlgorithmException { FileURL fileURL; // Regular file createFile(tempFile, 1); fileURL = tempFile.getURL(); assert fileURL != null; testPathResolution(tempFile, fileURL.toString(true)); // Directory file tempFile.delete(); tempFile.mkdir(); fileURL = tempFile.getURL(); assert fileURL != null; testPathResolution(tempFile, fileURL.toString(true)); }
/** * Creates a SimpleFileAttributes instance from an AbstractFile and ensures that the values returned by * SimpleFileAttributes' getters match those of AbstractFile. * * @throws IOException should not happen */ @Test public void testAccessors() throws IOException { LocalFileTest lft = new LocalFileTest(); // File doesn't exist AbstractFile tempFile = lft.getTemporaryFile(); assertAttributesMatch(tempFile, new SimpleFileAttributes(tempFile)); // File exists as a regular file tempFile.mkfile(); assertAttributesMatch(tempFile, new SimpleFileAttributes(tempFile)); // File exists as a directory tempFile.delete(); tempFile.mkdir(); assertAttributesMatch(tempFile, new SimpleFileAttributes(tempFile)); }
children[0].delete(); assert !children[0].exists();