mirror of
https://github.com/mitb-archive/filebot
synced 2025-03-09 22:09:47 -04:00
DUPLICATE:
1. try to clone 2. try to hardlink 3. copy if necessary
This commit is contained in:
parent
a064f6b954
commit
2dea2091d4
@ -1,8 +1,11 @@
|
|||||||
package net.filebot;
|
package net.filebot;
|
||||||
|
|
||||||
|
import static java.nio.file.Files.*;
|
||||||
import static java.util.Arrays.*;
|
import static java.util.Arrays.*;
|
||||||
import static java.util.stream.Collectors.*;
|
import static java.util.stream.Collectors.*;
|
||||||
|
import static net.filebot.Logging.*;
|
||||||
import static net.filebot.UserFiles.*;
|
import static net.filebot.UserFiles.*;
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,15 +16,13 @@ import java.util.List;
|
|||||||
|
|
||||||
import com.sun.jna.Platform;
|
import com.sun.jna.Platform;
|
||||||
|
|
||||||
import net.filebot.util.FileUtilities;
|
|
||||||
|
|
||||||
public enum StandardRenameAction implements RenameAction {
|
public enum StandardRenameAction implements RenameAction {
|
||||||
|
|
||||||
MOVE {
|
MOVE {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
return FileUtilities.moveRename(from, to);
|
return moveRename(from, to);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
return FileUtilities.copyAs(from, to);
|
return copyAs(from, to);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -37,12 +38,12 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
File dest = FileUtilities.resolveDestination(from, to);
|
File dest = resolveDestination(from, to);
|
||||||
|
|
||||||
// move file and the create a symlink to the new location via NIO.2
|
// move file and the create a symlink to the new location via NIO.2
|
||||||
try {
|
try {
|
||||||
Files.move(from.toPath(), dest.toPath());
|
move(from.toPath(), dest.toPath());
|
||||||
FileUtilities.createRelativeSymlink(from, dest, true);
|
createRelativeSymlink(from, dest, true);
|
||||||
} catch (LinkageError e) {
|
} catch (LinkageError e) {
|
||||||
throw new Exception("Unsupported Operation: move, createSymbolicLink");
|
throw new Exception("Unsupported Operation: move, createSymbolicLink");
|
||||||
}
|
}
|
||||||
@ -55,11 +56,11 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
File dest = FileUtilities.resolveDestination(from, to);
|
File dest = resolveDestination(from, to);
|
||||||
|
|
||||||
// create symlink via NIO.2
|
// create symlink via NIO.2
|
||||||
try {
|
try {
|
||||||
return FileUtilities.createRelativeSymlink(dest, from, true);
|
return createRelativeSymlink(dest, from, true);
|
||||||
} catch (LinkageError e) {
|
} catch (LinkageError e) {
|
||||||
throw new Exception("Unsupported Operation: createSymbolicLink");
|
throw new Exception("Unsupported Operation: createSymbolicLink");
|
||||||
}
|
}
|
||||||
@ -70,11 +71,11 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
File dest = FileUtilities.resolveDestination(from, to);
|
File dest = resolveDestination(from, to);
|
||||||
|
|
||||||
// create hardlink via NIO.2
|
// create hardlink via NIO.2
|
||||||
try {
|
try {
|
||||||
return FileUtilities.createHardLinkStructure(dest, from);
|
return createHardLinkStructure(dest, from);
|
||||||
} catch (LinkageError e) {
|
} catch (LinkageError e) {
|
||||||
throw new Exception("Unsupported Operation: createLink");
|
throw new Exception("Unsupported Operation: createLink");
|
||||||
}
|
}
|
||||||
@ -85,7 +86,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
File dest = FileUtilities.resolveDestination(from, to);
|
File dest = resolveDestination(from, to);
|
||||||
|
|
||||||
// clonefile or reflink requires filesystem that supports copy-on-write (e.g. apfs or btrfs)
|
// clonefile or reflink requires filesystem that supports copy-on-write (e.g. apfs or btrfs)
|
||||||
ProcessBuilder process = new ProcessBuilder();
|
ProcessBuilder process = new ProcessBuilder();
|
||||||
@ -114,11 +115,24 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws Exception {
|
public File rename(File from, File to) throws Exception {
|
||||||
|
// try to clone
|
||||||
|
if (Platform.isMac() || Platform.isLinux()) {
|
||||||
|
try {
|
||||||
|
CLONE.rename(from, to);
|
||||||
|
} catch (Exception e) {
|
||||||
|
debug.finest(format("[%s] %s", CLONE, e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to hardlink
|
||||||
try {
|
try {
|
||||||
return HARDLINK.rename(from, to);
|
return HARDLINK.rename(from, to);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return COPY.rename(from, to);
|
debug.finest(format("[%s] %s", HARDLINK, e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy if necessary
|
||||||
|
return COPY.rename(from, to);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -126,7 +140,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File rename(File from, File to) throws IOException {
|
public File rename(File from, File to) throws IOException {
|
||||||
return FileUtilities.resolve(from, to);
|
return resolve(from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -199,7 +213,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
|
|
||||||
// reverse move
|
// reverse move
|
||||||
if (current.exists() && !original.exists()) {
|
if (current.exists() && !original.exists()) {
|
||||||
return FileUtilities.moveRename(current, original);
|
return moveRename(current, original);
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicFileAttributes currentAttr = Files.readAttributes(current.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
BasicFileAttributes currentAttr = Files.readAttributes(current.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
||||||
@ -214,7 +228,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
// reverse keeplink
|
// reverse keeplink
|
||||||
if (!currentAttr.isSymbolicLink() && originalAttr.isSymbolicLink()) {
|
if (!currentAttr.isSymbolicLink() && originalAttr.isSymbolicLink()) {
|
||||||
trash(original);
|
trash(original);
|
||||||
return FileUtilities.moveRename(current, original);
|
return moveRename(current, original);
|
||||||
}
|
}
|
||||||
|
|
||||||
// reverse copy / hardlink
|
// reverse copy / hardlink
|
||||||
@ -226,7 +240,7 @@ public enum StandardRenameAction implements RenameAction {
|
|||||||
// reverse folder copy
|
// reverse folder copy
|
||||||
if (currentAttr.isDirectory() && originalAttr.isDirectory()) {
|
if (currentAttr.isDirectory() && originalAttr.isDirectory()) {
|
||||||
trash(original);
|
trash(original);
|
||||||
return FileUtilities.moveRename(current, original);
|
return moveRename(current, original);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalArgumentException(String.format("Cannot revert file: %s => %s", current, original));
|
throw new IllegalArgumentException(String.format("Cannot revert file: %s => %s", current, original));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user