Advertisement
filebot

revert

May 6th, 2020
1,621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1.     public static File revert(File current, File original) throws IOException {
  2.         // do nothing if current and original path is exactly the same
  3.         if (current.equals(original)) {
  4.             return original;
  5.         }
  6.  
  7.         // reverse move
  8.         if (current.exists() && !original.exists()) {
  9.             return moveRename(current, original);
  10.         }
  11.  
  12.         BasicFileAttributes currentAttr = Files.readAttributes(current.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
  13.         BasicFileAttributes originalAttr = Files.readAttributes(original.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
  14.  
  15.         // reverse symlink
  16.         if (currentAttr.isSymbolicLink() && !originalAttr.isSymbolicLink()) {
  17.             trash(current);
  18.             return original;
  19.         }
  20.  
  21.         // reverse keeplink
  22.         if (!currentAttr.isSymbolicLink() && originalAttr.isSymbolicLink()) {
  23.             trash(original);
  24.             return moveRename(current, original);
  25.         }
  26.  
  27.         // reverse copy / hardlink
  28.         if (currentAttr.isRegularFile() && originalAttr.isRegularFile()) {
  29.             trash(current);
  30.             return original;
  31.         }
  32.  
  33.         // reverse folder copy
  34.         if (currentAttr.isDirectory() && originalAttr.isDirectory()) {
  35.             trash(original);
  36.             return moveRename(current, original);
  37.         }
  38.  
  39.         throw new IllegalStateException(String.format("Cannot revert file: %s => %s", current, original));
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement