コード例 #1
0
  /**
   * Moves recursively the given file or folder.
   *
   * @param file the file or folder to move
   * @param recurseParams destination folder where the given file will be moved (null for top level
   *     files)
   * @return <code>true</code> if the file has been moved completly (copied + deleted).
   */
  @Override
  protected boolean processFile(AbstractFile file, Object recurseParams) {
    // Stop if interrupted
    if (getState() == State.INTERRUPTED) return false;

    // Destination folder
    AbstractFile destFolder = recurseParams == null ? baseDestFolder : (AbstractFile) recurseParams;

    // Is current file at the base folder level ?
    boolean isFileInBaseFolder = files.contains(file);

    // Determine filename in destination
    String originalName = file.getName();
    String destFileName = isFileInBaseFolder && newName != null ? newName : originalName;

    // create destination AbstractFile instance
    AbstractFile destFile = createDestinationFile(destFolder, destFileName);
    if (destFile == null) return false;

    // Do not follow symlink, simply delete it and return
    if (file.isSymlink()) {
      do { // Loop for retry
        try {
          file.delete();
          return true;
        } catch (IOException e) {
          LOGGER.debug("IOException caught", e);

          int ret =
              showErrorDialog(
                  errorDialogTitle, Translator.get("cannot_delete_file", file.getAbsolutePath()));
          // Retry loops
          if (ret == RETRY_ACTION) {
            continue;
          }
          // Cancel, skip or close dialog returns false
          return false;
        }
      } while (true);
    }

    destFile = checkForCollision(file, destFolder, destFile, renameMode);
    if (destFile == null) return false;

    // Let's try to rename the file using AbstractFile#renameTo() whenever possible, as it is more
    // efficient
    // than moving the file manually.
    //
    // Do not attempt to rename the file in the following cases:
    // - destination has to be appended
    // - file schemes do not match (at the time of writing, no filesystem supports mixed scheme
    // renaming)
    // - if the 'rename' operation is not supported
    // Note: we want to avoid calling AbstractFile#renameTo when we know it will fail, as it
    // performs some costly
    // I/O bound checks and ends up throwing an exception which also comes at a cost.
    if (!append
        && file.getURL().schemeEquals(destFile.getURL())
        && file.isFileOperationSupported(FileOperation.RENAME)) {
      try {
        file.renameTo(destFile);
        return true;
      } catch (IOException e) {
        // Fail silently: renameTo might fail under normal conditions, for instance for local files
        // which are
        // not located on the same volume.
        LOGGER.debug(
            "Failed to rename " + file + " into " + destFile + " (not necessarily an error)", e);
      }
    }
    // Rename couldn't be used or didn't succeed: move the file manually

    // Move the directory and all its children recursively, by copying files to the destination and
    // then deleting them.
    if (file.isDirectory()) {
      // create the destination folder if it doesn't exist
      if (!(destFile.exists() && destFile.isDirectory())) {
        do { // Loop for retry
          try {
            destFile.mkdir();
          } catch (IOException e) {
            int ret =
                showErrorDialog(
                    errorDialogTitle,
                    Translator.get("cannot_create_folder", destFile.getAbsolutePath()));
            // Retry loops
            if (ret == RETRY_ACTION) {
              continue;
            }
            // Cancel, skip or close dialog returns false
            return false;
          }
          break;
        } while (true);
      }

      // move each file in this folder recursively
      do { // Loop for retry
        try {
          AbstractFile subFiles[] = file.ls();
          boolean isFolderEmpty = true;
          for (AbstractFile subFile : subFiles) {
            // Return now if the job was interrupted, so that we do not attempt to delete this
            // folder
            if (getState() == State.INTERRUPTED) return false;

            // Notify job that we're starting to process this file (needed for recursive calls to
            // processFile)
            nextFile(subFile);
            if (!processFile(subFile, destFile)) isFolderEmpty = false;
          }

          // Only when finished with folder, set destination folder's date to match the original
          // folder one
          if (destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) {
            try {
              destFile.changeDate(file.getDate());
            } catch (IOException e) {
              LOGGER.debug("failed to change the date of " + destFile, e);
              // Fail silently
            }
          }

          // If one file failed to be moved, return false (failure) since this folder could not be
          // moved totally
          if (!isFolderEmpty) return false;
        } catch (IOException e) {
          // file.ls() failed
          int ret =
              showErrorDialog(
                  errorDialogTitle, Translator.get("cannot_read_folder", file.getName()));
          // Retry loops
          if (ret == RETRY_ACTION) continue;
          // Cancel, skip or close dialog returns false
          return false;
        }
        break;
      } while (true);

      // Return now if the job was interrupted, so that we do not attempt to delete this folder
      if (getState() == State.INTERRUPTED) return false;

      // finally, delete the empty folder
      do { // Loop for retry
        try {
          file.delete();
          return true;
        } catch (IOException e) {
          int ret =
              showErrorDialog(
                  errorDialogTitle, Translator.get("cannot_delete_folder", file.getAbsolutePath()));
          // Retry loops
          if (ret == RETRY_ACTION) continue;
          // Cancel, skip or close dialog returns false
          return false;
        }
      } while (true);
    }
    // File is a regular file, move it by copying it to the destination and then deleting it
    else {
      // if renameTo() was not supported or failed, or if it wasn't possible because of 'append',
      // try the hard way by copying the file first, and then deleting the source file.
      if (tryCopyFile(file, destFile, append, errorDialogTitle)
          && getState() != State.INTERRUPTED) {
        // Delete the source file
        do { // Loop for retry
          try {
            file.delete();
            // All OK
            return true;
          } catch (IOException e) {
            LOGGER.debug("IOException caught", e);

            int ret =
                showErrorDialog(
                    errorDialogTitle, Translator.get("cannot_delete_file", file.getAbsolutePath()));
            // Retry loops
            if (ret == RETRY_ACTION) continue;
            // Cancel, skip or close dialog returns false
            return false;
          }
        } while (true);
      }

      return false;
    }
  }
コード例 #2
0
 @Override
 public void delete() throws IOException {
   file.delete();
 }
コード例 #3
0
  /**
   * Implementation of {@link com.mucommander.desktop.QueuedTrash} moveToTrash method.
   *
   * <p>Try to copy a collection of files to the Xfce's Trash.
   *
   * @param queuedFiles Collection of files to the trash
   * @return <code>true</code> if movement has been successful or <code>false</code> otherwise
   */
  @Override
  protected boolean moveToTrash(List<AbstractFile> queuedFiles) {
    boolean retVal =
        true; // overall return value (if everything went OK or at least one file wasn't moved
              // properly

    for (AbstractFile fileToDelete : queuedFiles) {
      String fileInfoContent;
      String trashFileName;

      // generate content of info file and new filename
      try {
        fileInfoContent = getFileInfoContent(fileToDelete);
        trashFileName = getUniqueFilename(fileToDelete);
      } catch (IOException ex) {
        LOGGER.debug("Failed to create filename for new trash item: " + fileToDelete.getName(), ex);

        // continue with other file (do not move file, because info file cannot be properly created
        continue;
      }

      AbstractFile infoFile = null;
      OutputStreamWriter infoWriter = null;
      try {
        // create info file
        infoFile = TRASH_INFO_SUBFOLDER.getChild(trashFileName + ".trashinfo");
        infoWriter = new OutputStreamWriter(infoFile.getOutputStream());
        infoWriter.write(fileInfoContent);
      } catch (IOException ex) {
        retVal = false;
        LOGGER.debug("Failed to create trash info file: " + trashFileName, ex);

        // continue with other file (do not move file, because info file wasn't properly created)
        continue;
      } finally {
        if (infoWriter != null) {
          try {
            infoWriter.close();
          } catch (IOException e) {
            // Not much else to do
          }
        }
      }

      try {
        // rename original file
        fileToDelete.renameTo(TRASH_FILES_SUBFOLDER.getChild(trashFileName));
      } catch (IOException ex) {
        try {
          // remove info file
          infoFile.delete();

        } catch (IOException ex1) {
          // simply ignore
        }

        retVal = false;
        LOGGER.debug("Failed to move file to trash: " + trashFileName, ex);
      }
    }

    return retVal;
  }