/**
   * 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);
    }
  }
  /**
   * Copies the source file to the target file.
   *
   * @param aSourceFile The source file to use. May not be <code>null</code>. Needs to be an
   *     existing file.
   * @param aTargetFile The destination files. May not be <code>null</code> and may not be an
   *     existing file.
   * @return A non-<code>null</code> error code.
   */
  @Nonnull
  public static FileIOError copyFile(
      @Nonnull final File aSourceFile, @Nonnull final File aTargetFile) {
    ValueEnforcer.notNull(aSourceFile, "SourceFile");
    ValueEnforcer.notNull(aTargetFile, "TargetFile");

    // Does the source file exist?
    if (!FileUtils.existsFile(aSourceFile))
      return EFileIOErrorCode.SOURCE_DOES_NOT_EXIST.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Are source and target different?
    if (EqualsUtils.equals(aSourceFile, aTargetFile))
      return EFileIOErrorCode.SOURCE_EQUALS_TARGET.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Does the target file already exist?
    if (aTargetFile.exists())
      return EFileIOErrorCode.TARGET_ALREADY_EXISTS.getAsIOError(
          EFileIOOperation.COPY_FILE, aTargetFile);

    // Is the source file readable?
    if (!FileUtils.canRead(aSourceFile))
      return EFileIOErrorCode.SOURCE_NOT_READABLE.getAsIOError(
          EFileIOOperation.COPY_FILE, aSourceFile);

    // Is the target parent directory writable?
    final File aTargetParentDir = aTargetFile.getParentFile();
    if (aTargetParentDir != null
        && aTargetParentDir.exists()
        && !FileUtils.canWrite(aTargetParentDir))
      return EFileIOErrorCode.TARGET_PARENT_NOT_WRITABLE.getAsIOError(
          EFileIOOperation.COPY_FILE, aTargetFile);

    // Ensure the targets parent directory is present
    FileUtils.ensureParentDirectoryIsPresent(aTargetFile);

    // Used FileChannel for better performance
    final EFileIOErrorCode eError =
        _copyFile(aSourceFile, aTargetFile).isSuccess()
            ? EFileIOErrorCode.NO_ERROR
            : EFileIOErrorCode.OPERATION_FAILED;
    return eError.getAsIOError(EFileIOOperation.COPY_FILE, aSourceFile, aTargetFile);
  }
  /**
   * 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);
  }