+ copy files with attributes

* xattr will only be set on destination files, not source files
This commit is contained in:
Reinhard Pointner 2014-01-08 17:23:04 +00:00
parent 4b15736faf
commit 4354dc2fd6
3 changed files with 44 additions and 45 deletions

View File

@ -219,19 +219,6 @@ public class CmdlineOperations implements CmdlineInterface {
// add matches from other files that are linked via filenames
matches.addAll(derivateMatches);
// first write all the metadata if xattr is enabled
if (useExtendedFileAttributes()) {
try {
for (Match<File, ?> match : matches) {
if (match.getCandidate() instanceof Episode) {
MediaDetection.storeMetaInfo(match.getValue(), match.getCandidate());
}
}
} catch (Throwable e) {
CLILogger.warning("Failed to write xattr: " + e.getMessage());
}
}
// map old files to new paths by applying formatting and validating filenames
Map<File, File> renameMap = new LinkedHashMap<File, File>();
@ -245,7 +232,7 @@ public class CmdlineOperations implements CmdlineInterface {
// rename episodes
Analytics.trackEvent("CLI", "Rename", "Episode", renameMap.size());
return renameAll(renameMap, renameAction, conflictAction);
return renameAll(renameMap, renameAction, conflictAction, matches);
}
private List<Match<File, Object>> matchEpisodes(Collection<File> files, Collection<Episode> episodes, boolean strict) throws Exception {
@ -475,19 +462,6 @@ public class CmdlineOperations implements CmdlineInterface {
}
}
// first write all the metadata if xattr is enabled
if (useExtendedFileAttributes()) {
try {
for (Match<File, ?> match : matches) {
if (match.getCandidate() instanceof Movie) {
MediaDetection.storeMetaInfo(match.getValue(), match.getCandidate());
}
}
} catch (Throwable e) {
CLILogger.warning("Failed to write xattr: " + e.getMessage());
}
}
// map old files to new paths by applying formatting and validating filenames
Map<File, File> renameMap = new LinkedHashMap<File, File>();
@ -501,7 +475,7 @@ public class CmdlineOperations implements CmdlineInterface {
// rename movies
Analytics.trackEvent("CLI", "Rename", "Movie", renameMap.size());
return renameAll(renameMap, renameAction, conflictAction);
return renameAll(renameMap, renameAction, conflictAction, matches);
}
public List<File> renameMusic(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, MusicIdentificationService service) throws Exception {
@ -538,7 +512,7 @@ public class CmdlineOperations implements CmdlineInterface {
// rename movies
Analytics.trackEvent("CLI", "Rename", "AudioTrack", renameMap.size());
return renameAll(renameMap, renameAction, conflictAction);
return renameAll(renameMap, renameAction, conflictAction, null);
}
private Map<File, Object> getContext(final Collection<Match<File, ?>> matches) {
@ -574,7 +548,7 @@ public class CmdlineOperations implements CmdlineInterface {
return newFile;
}
public List<File> renameAll(Map<File, File> renameMap, RenameAction renameAction, ConflictAction conflictAction) throws Exception {
public List<File> renameAll(Map<File, File> renameMap, RenameAction renameAction, ConflictAction conflictAction, List<Match<File, ?>> matches) throws Exception {
if (renameMap.isEmpty()) {
throw new Exception(format("[%s] Unable to process any files", renameAction));
}
@ -631,6 +605,24 @@ public class CmdlineOperations implements CmdlineInterface {
}
}
// write metadata into xattr if xattr is enabled
if (matches != null && useExtendedFileAttributes()) {
try {
for (Match<File, ?> match : matches) {
File file = match.getValue();
Object meta = match.getCandidate();
if (renameMap.containsKey(file) && meta != null) {
File destination = resolveDestination(file, renameMap.get(file), false);
if (destination.isFile()) {
MediaDetection.storeMetaInfo(destination, meta);
}
}
}
} catch (Throwable e) {
CLILogger.warning("Failed to write xattr: " + e.getMessage());
}
}
// new file names
List<File> destinationList = new ArrayList<File>();
for (Entry<File, File> it : renameLog) {

View File

@ -77,26 +77,15 @@ class RenameAction extends AbstractAction {
}
Map<File, File> renameMap = checkRenamePlan(validate(model.getRenameMap(), window), window);
StandardRenameAction action = (StandardRenameAction) getValue(RENAME_ACTION);
if (renameMap.isEmpty()) {
return;
}
// start processing
window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
List<Match<Object, File>> matches = new ArrayList<Match<Object, File>>(model.matches());
StandardRenameAction action = (StandardRenameAction) getValue(RENAME_ACTION);
// first write all the metadata if xattr is enabled
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());
}
}
window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if (useNativeShell() && isNativeActionSupported(action)) {
RenameJob renameJob = new NativeRenameJob(renameMap, NativeRenameAction.valueOf(action.name()));
@ -124,6 +113,24 @@ class RenameAction extends AbstractAction {
dialog.setVisible(true);
}
}
// write metadata into xattr if xattr is enabled
if (useExtendedFileAttributes()) {
try {
for (Match<Object, File> match : matches) {
File file = match.getCandidate();
Object meta = match.getValue();
if (renameMap.containsKey(file) && meta != null) {
File destination = resolveDestination(file, renameMap.get(file), false);
if (destination.isFile()) {
MediaDetection.storeMetaInfo(destination, meta);
}
}
}
} catch (Throwable e) {
Logger.getLogger(RenameAction.class.getName()).warning("Failed to write xattr: " + e.getMessage());
}
}
} catch (ExecutionException e) {
// ignore, handled in rename worker
} catch (Throwable e) {

View File

@ -71,7 +71,7 @@ public final class FileUtilities {
} else {
// copy file
try {
java.nio.file.Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
java.nio.file.Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
} catch (LinkageError e) {
org.apache.commons.io.FileUtils.copyFile(source, destination);
}