* better handling of rename sets where the destination file already exists

This commit is contained in:
Reinhard Pointner 2013-03-23 12:34:15 +00:00
parent 331ac63201
commit 9065429152
2 changed files with 19 additions and 8 deletions

View File

@ -94,8 +94,10 @@ class RenameAction extends AbstractAction {
if (useExtendedFileAttributes()) {
try {
for (Match<Object, File> match : model.matches()) {
if (renameMap.containsKey(match.getCandidate()) && match.getValue() != null) {
MediaDetection.storeMetaInfo(match.getCandidate(), match.getValue());
}
}
} catch (Throwable e) {
Logger.getLogger(RenameAction.class.getName()).warning("Failed to write xattr: " + e.getMessage());
}
@ -169,9 +171,6 @@ class RenameAction extends AbstractAction {
if (destinationSet.contains(destination))
throw new IllegalArgumentException("Conflict detected: " + mapping.getValue().getPath());
if (destination.exists())
throw new IllegalArgumentException("File already exists: " + mapping.getValue().getPath());
// use original mapping values
renameMap.put(mapping.getKey(), mapping.getValue());
destinationSet.add(destination);
@ -304,8 +303,10 @@ class RenameAction extends AbstractAction {
firePropertyChange("currentFile", mapping.getKey(), mapping.getValue());
// rename file, throw exception on failure
if (!mapping.getKey().equals(mapping.getValue())) {
action.rename(mapping.getKey(), mapping.getValue());
File source = mapping.getKey();
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false);
if (!source.equals(destination)) {
action.rename(source, destination);
}
// remember successfully renamed matches for history entry and possible revert
@ -375,9 +376,19 @@ class RenameAction extends AbstractAction {
protected Map<File, File> doInBackground() throws Exception {
NativeRenameAction shell = (NativeRenameAction) action;
// prepare delta, ignore files already named as desired
Map<File, File> todo = new LinkedHashMap<File, File>();
for (Entry<File, File> mapping : renameMap.entrySet()) {
File source = mapping.getKey();
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false);
if (!source.equals(destination)) {
todo.put(source, destination);
}
}
// call native shell move/copy
try {
shell.rename(renameMap);
shell.rename(todo);
} catch (CancellationException e) {
// set as cancelled and propagate the exception
super.cancel(false);

View File

@ -87,7 +87,7 @@ public class RenameModel extends MatchModel<Object, File> {
for (int i = 0; i < names.size(); i++) {
if (hasComplement(i)) {
File originalFile = new File(files().get(i).getAbsolutePath());
File originalFile = files().get(i);
FormattedFuture formattedFuture = names.get(i);
StringBuilder nameBuilder = new StringBuilder();