/** * 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); }
/** * Copy a directory including all child objects. * * @param aSourceDir The source directory to be copied. May not be <code>null</code>. * @param aTargetDir The destination directory where to be copied. This directory may not be * existing. May not be <code>null</code>. * @return A non-<code>null</code> error code. */ @Nonnull public static FileIOError copyDirRecursive( @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.COPY_DIR_RECURSIVE, aSourceDir); // Are source and target different? if (EqualsUtils.equals(aSourceDir, aTargetDir)) return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir); // Is the source a parent of target? if (FileUtils.isParentDirectory(aSourceDir, aTargetDir)) return EFileIOErrorCode.TARGET_IS_CHILD_OF_SOURCE.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir); // Does the target directory already exist? if (aTargetDir.exists()) return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aTargetDir); // Is the source directory readable? if (!FileUtils.canRead(aSourceDir)) return EFileIOErrorCode.SOURCE_NOT_READABLE.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, 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.COPY_DIR_RECURSIVE, aTargetDir); FileIOError eCode; // Ensure the targets parent directory is present eCode = createDirRecursive(aTargetDir); if (eCode.isFailure()) return eCode; for (final File aChild : FileUtils.getDirectoryContent(aSourceDir)) { if (aChild.isDirectory()) { // Skip "." and ".." if (FilenameHelper.isSystemInternalDirectory(aChild.getName())) continue; // Copy directory eCode = copyDirRecursive(aChild, new File(aTargetDir, aChild.getName())); if (eCode.isFailure()) return eCode; } else if (aChild.isFile()) { // Copy a file eCode = copyFile(aChild, new File(aTargetDir, aChild.getName())); if (eCode.isFailure()) return eCode; } else { // Neither directory not file - don't know how to handle return EFileIOErrorCode.OBJECT_CANNOT_BE_HANDLED.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aChild); } } // Done return EFileIOErrorCode.NO_ERROR.getAsIOError( EFileIOOperation.COPY_DIR_RECURSIVE, aSourceDir, aTargetDir); }