/** * Delete an existing file. * * @param aFile The file to be deleted. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError deleteFile(@Nonnull final File aFile) { ValueEnforcer.notNull(aFile, "File"); if (!FileUtils.existsFile(aFile)) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError( EFileIOOperation.DELETE_FILE, aFile); // Is the parent directory writable? final File aParentDir = aFile.getParentFile(); if (aParentDir != null && !FileUtils.canWrite(aParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.DELETE_FILE, aFile); try { // delete may return true even so it internally failed! final EFileIOErrorCode eError = aFile.delete() && !aFile.exists() ? EFileIOErrorCode.NO_ERROR : EFileIOErrorCode.OPERATION_FAILED; return eError.getAsIOError(EFileIOOperation.DELETE_FILE, aFile); } catch (final SecurityException ex) { return EFileIOErrorCode.getAsIOError(EFileIOOperation.DELETE_FILE, ex); } }
/** * Delete an existing directory. The directory needs to be empty before it can be deleted. * * @param aDir The directory to be deleted. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError deleteDir(@Nonnull final File aDir) { ValueEnforcer.notNull(aDir, "Directory"); // Does the directory not exist? if (!FileUtils.existsDir(aDir)) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError(EFileIOOperation.DELETE_DIR, aDir); if (isWarnOnDeleteRoot()) { // Check that we're not deleting the complete hard drive... if (aDir.getAbsoluteFile().getParent() == null) throw new IllegalArgumentException( "Aren't we deleting the full drive: '" + aDir.getAbsolutePath() + "'"); } // Is the parent directory writable? final File aParentDir = aDir.getParentFile(); if (aParentDir != null && !FileUtils.canWrite(aParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.DELETE_DIR, aDir); try { // delete may return true even so it internally failed! final EFileIOErrorCode eError = aDir.delete() && !aDir.exists() ? EFileIOErrorCode.NO_ERROR : EFileIOErrorCode.OPERATION_FAILED; return eError.getAsIOError(EFileIOOperation.DELETE_DIR, aDir); } catch (final SecurityException ex) { return EFileIOErrorCode.getAsIOError(EFileIOOperation.DELETE_DIR, ex); } }
/** * Rename a directory. * * @param aSourceDir The original directory name. May not be <code>null</code>. * @param aTargetDir The destination directory name. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError renameDir( @Nonnull final File aSourceDir, @Nonnull final File aTargetDir) { ValueEnforcer.notNull(aSourceDir, "SourceDirectory"); ValueEnforcer.notNull(aTargetDir, "TargetDirectory"); // Does the source directory exist? if (!FileUtils.existsDir(aSourceDir)) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError( EFileIOOperation.RENAME_DIR, aSourceDir); // Are source and target different? if (EqualsUtils.equals(aSourceDir, aTargetDir)) return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError( EFileIOOperation.RENAME_DIR, aSourceDir); // Does the target directory already exist? if (aTargetDir.exists()) return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError( EFileIOOperation.RENAME_DIR, aTargetDir); // Is the source a parent of target? if (FileUtils.isParentDirectory(aSourceDir, aTargetDir)) return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError( EFileIOOperation.RENAME_DIR, aSourceDir, aTargetDir); // Is the source parent directory writable? final File aSourceParentDir = aSourceDir.getParentFile(); if (aSourceParentDir != null && !FileUtils.canWrite(aSourceParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.RENAME_DIR, aSourceDir); // Is the target parent directory writable? final File aTargetParentDir = aTargetDir.getParentFile(); if (aTargetParentDir != null && aTargetParentDir.exists() && !FileUtils.canWrite(aTargetParentDir)) return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.RENAME_DIR, aTargetDir); // Ensure parent of target directory is present FileUtils.ensureParentDirectoryIsPresent(aTargetDir); try { final EFileIOErrorCode eError = aSourceDir.renameTo(aTargetDir) ? EFileIOErrorCode.NO_ERROR : EFileIOErrorCode.OPERATION_FAILED; return eError.getAsIOError(EFileIOOperation.RENAME_DIR, aSourceDir, aTargetDir); } catch (final SecurityException ex) { return EFileIOErrorCode.getAsIOError(EFileIOOperation.RENAME_DIR, ex); } }
/** * Delete an existing directory including all child objects. * * @param aDir The directory to be deleted. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError deleteDirRecursive(@Nonnull final File aDir) { ValueEnforcer.notNull(aDir, "Directory"); // Non-existing directory? if (!FileUtils.existsDir(aDir)) return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError( EFileIOOperation.DELETE_DIR_RECURSIVE, aDir); if (isWarnOnDeleteRoot()) { // Check that we're not deleting the complete hard drive... if (aDir.getAbsoluteFile().getParent() == null) throw new IllegalArgumentException( "Aren't we deleting the full drive: '" + aDir.getAbsolutePath() + "'"); } // Is the parent directory writable? final File aParentDir = aDir.getParentFile(); if (aParentDir != null && !FileUtils.canWrite(aParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.DELETE_DIR_RECURSIVE, aDir); // iterate directory for (final File aChild : FileUtils.getDirectoryContent(aDir)) { // is it a file or a directory or ... if (aChild.isDirectory()) { // Ignore "." and ".." directory if (FilenameHelper.isSystemInternalDirectory(aChild.getName())) continue; // recursive call final FileIOError eCode = deleteDirRecursive(aChild); if (eCode.isFailure()) return eCode; } else if (aChild.isFile()) { // delete file final FileIOError eCode = deleteFile(aChild); if (eCode.isFailure()) return eCode; } else { // Neither directory no file - don't know how to handle return EFileIOErrorCode.OBJECT_CANNOT_BE_HANDLED.getAsIOError( EFileIOOperation.DELETE_DIR_RECURSIVE, aChild); } } // Now this directory should be empty -> delete as if empty return deleteDir(aDir); }
/** * Create a new directory. The direct parent directory already needs to exist. * * @param aDir The directory to be created. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError createDir(@Nonnull final File aDir) { ValueEnforcer.notNull(aDir, "Directory"); // Does the directory already exist? if (aDir.exists()) return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError(EFileIOOperation.CREATE_DIR, aDir); // Is the parent directory writable? final File aParentDir = aDir.getParentFile(); if (aParentDir != null && aParentDir.exists() && !FileUtils.canWrite(aParentDir)) return EFileIOErrorCode.SOURCE_PARENT_NOT_WRITABLE.getAsIOError( EFileIOOperation.CREATE_DIR, aDir); try { final EFileIOErrorCode eError = aDir.mkdir() ? EFileIOErrorCode.NO_ERROR : EFileIOErrorCode.OPERATION_FAILED; return eError.getAsIOError(EFileIOOperation.CREATE_DIR, aDir); } catch (final SecurityException ex) { return EFileIOErrorCode.getAsIOError(EFileIOOperation.CREATE_DIR, ex); } }