2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2012-03-07 09:26:47 -05:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-07-15 08:57:07 -04:00
|
|
|
import java.nio.file.Files;
|
2016-03-10 11:23:45 -05:00
|
|
|
import java.nio.file.LinkOption;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2012-03-07 09:26:47 -05:00
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
import net.filebot.util.FileUtilities;
|
2012-03-07 09:26:47 -05:00
|
|
|
|
|
|
|
public enum StandardRenameAction implements RenameAction {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
MOVE {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
|
|
|
return FileUtilities.moveRename(from, to);
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
COPY {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
|
|
|
return FileUtilities.copyAs(from, to);
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-08 00:11:06 -05:00
|
|
|
KEEPLINK {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-08 00:11:06 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
2016-04-13 07:22:03 -04:00
|
|
|
File destionation = FileUtilities.resolveDestination(from, to);
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-08 00:11:06 -05:00
|
|
|
// move file and the create a symlink to the new location via NIO.2
|
|
|
|
try {
|
2014-07-15 08:57:07 -04:00
|
|
|
Files.move(from.toPath(), destionation.toPath());
|
2013-08-15 06:51:39 -04:00
|
|
|
FileUtilities.createRelativeSymlink(from, destionation, true);
|
2012-03-08 00:11:06 -05:00
|
|
|
} catch (LinkageError e) {
|
|
|
|
throw new Exception("Unsupported Operation: move, createSymbolicLink");
|
|
|
|
}
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-08 00:11:06 -05:00
|
|
|
return destionation;
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
SYMLINK {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
2016-04-13 07:22:03 -04:00
|
|
|
File destionation = FileUtilities.resolveDestination(from, to);
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
// create symlink via NIO.2
|
|
|
|
try {
|
2013-08-15 06:51:39 -04:00
|
|
|
return FileUtilities.createRelativeSymlink(destionation, from, true);
|
2012-03-07 09:26:47 -05:00
|
|
|
} catch (LinkageError e) {
|
|
|
|
throw new Exception("Unsupported Operation: createSymbolicLink");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
HARDLINK {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
2016-04-13 07:22:03 -04:00
|
|
|
File destionation = FileUtilities.resolveDestination(from, to);
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
// create hardlink via NIO.2
|
|
|
|
try {
|
2014-07-15 08:57:07 -04:00
|
|
|
return FileUtilities.createHardLinkStructure(destionation, from);
|
2012-03-07 09:26:47 -05:00
|
|
|
} catch (LinkageError e) {
|
|
|
|
throw new Exception("Unsupported Operation: createLink");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2014-08-30 15:51:25 -04:00
|
|
|
DUPLICATE {
|
2014-08-10 03:11:09 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws Exception {
|
|
|
|
try {
|
|
|
|
return HARDLINK.rename(from, to);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return COPY.rename(from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-11-16 23:14:09 -05:00
|
|
|
RENAME {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-11-16 23:14:09 -05:00
|
|
|
@Override
|
2014-08-10 03:11:09 -04:00
|
|
|
public File rename(File from, File to) throws Exception {
|
2012-11-16 23:14:09 -05:00
|
|
|
// rename only the filename
|
|
|
|
File dest = new File(from.getParentFile(), to.getName());
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-11-16 23:14:09 -05:00
|
|
|
if (!from.renameTo(dest))
|
|
|
|
throw new IOException("Rename failed: " + dest);
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-11-16 23:14:09 -05:00
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
},
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
TEST {
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
@Override
|
|
|
|
public File rename(File from, File to) throws IOException {
|
2016-04-13 07:22:03 -04:00
|
|
|
return FileUtilities.resolve(from, to);
|
2012-03-07 09:26:47 -05:00
|
|
|
}
|
|
|
|
};
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2013-02-08 10:35:23 -05:00
|
|
|
public String getDisplayName() {
|
|
|
|
switch (this) {
|
2013-08-15 06:51:39 -04:00
|
|
|
case MOVE:
|
|
|
|
return "Rename";
|
|
|
|
case COPY:
|
|
|
|
return "Copy";
|
|
|
|
case KEEPLINK:
|
|
|
|
return "Keeplink";
|
|
|
|
case SYMLINK:
|
|
|
|
return "Symlink";
|
|
|
|
case HARDLINK:
|
|
|
|
return "Hardlink";
|
|
|
|
default:
|
|
|
|
return null;
|
2013-02-08 10:35:23 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
public static StandardRenameAction forName(String action) {
|
|
|
|
for (StandardRenameAction it : values()) {
|
|
|
|
if (it.name().equalsIgnoreCase(action))
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Illegal rename action: " + action);
|
|
|
|
}
|
2013-08-15 06:51:39 -04:00
|
|
|
|
2016-03-10 11:23:45 -05:00
|
|
|
public static File revert(File current, File original) throws IOException {
|
2016-05-15 13:49:54 -04:00
|
|
|
// do nothing if current and original path is exactly the same
|
|
|
|
if (current.equals(original)) {
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
2016-03-10 11:23:45 -05:00
|
|
|
// reverse move
|
|
|
|
if (current.exists() && !original.exists()) {
|
|
|
|
return FileUtilities.moveRename(current, original);
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicFileAttributes currentAttr = Files.readAttributes(current.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
|
|
|
BasicFileAttributes originalAttr = Files.readAttributes(original.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
|
|
|
|
|
|
|
|
// reverse symlink
|
|
|
|
if (currentAttr.isSymbolicLink() && !originalAttr.isSymbolicLink()) {
|
|
|
|
Files.delete(current.toPath());
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse keeplink
|
|
|
|
if (!currentAttr.isSymbolicLink() && originalAttr.isSymbolicLink()) {
|
|
|
|
Files.delete(original.toPath());
|
|
|
|
return FileUtilities.moveRename(current, original);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse copy / hardlink
|
|
|
|
if (currentAttr.isRegularFile() && originalAttr.isRegularFile()) {
|
|
|
|
Files.delete(current.toPath());
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse folder copy
|
|
|
|
if (currentAttr.isDirectory() && originalAttr.isDirectory()) {
|
|
|
|
FileUtilities.delete(original);
|
|
|
|
return FileUtilities.moveRename(current, original);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new IllegalArgumentException(String.format("Cannot revert files: %s => %s", current, original));
|
|
|
|
}
|
|
|
|
|
2012-03-07 09:26:47 -05:00
|
|
|
}
|