diff --git a/src/main/java/caosdb/server/utils/FileUtils.java b/src/main/java/caosdb/server/utils/FileUtils.java index b360c1a86afa3576b40de7091496b358defea2a8..842af491d9aae3db347b579ddcccfada170aeb1c 100644 --- a/src/main/java/caosdb/server/utils/FileUtils.java +++ b/src/main/java/caosdb/server/utils/FileUtils.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.file.LinkOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -290,6 +291,21 @@ public class FileUtils { } } } + + private static boolean exists(File file) { + return java.nio.file.Files.exists(file.toPath(), LinkOption.NOFOLLOW_LINKS); + } + + private static void moveReplace(File file, File target) throws IOException { + if(exists(target)) { + org.apache.commons.io.FileUtils.forceDelete(target); + } + if (java.nio.file.Files.isDirectory(file.toPath(), LinkOption.NOFOLLOW_LINKS)) { + org.apache.commons.io.FileUtils.moveDirectory(file, target); + } else { + org.apache.commons.io.FileUtils.moveFile(file, target); + } + } public static Undoable rename(final File file, final File target) throws Message, IOException { final File backup; @@ -302,17 +318,17 @@ public class FileUtils { public void cleanUp() {} }; } - if (target.exists()) { + if (exists(target)) { // in case this is a update transaction, the old version of the file // must be stored somewhere until the transaction is done. final File tmp = new File(new File(FileSystem.getTmp()), target.getName() + Utils.getUID()); - java.nio.file.Files.move(target.toPath(), tmp.toPath()); + moveReplace(target, tmp); backup = tmp; } else { backup = null; } try { - java.nio.file.Files.move(file.toPath(), target.toPath()); + moveReplace(file, target); return new UndoHandler() { private File _backup = backup;