From edf5f38f959a7e902ff4e425edd1ceb30b48d0f6 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <timm.fitschen@ds.mpg.de> Date: Wed, 27 Mar 2019 23:28:09 +0100 Subject: [PATCH] BUG: delete directories --- .../java/caosdb/server/utils/FileUtils.java | 110 +++++++++++------- 1 file changed, 71 insertions(+), 39 deletions(-) diff --git a/src/main/java/caosdb/server/utils/FileUtils.java b/src/main/java/caosdb/server/utils/FileUtils.java index 842af491..560696fd 100644 --- a/src/main/java/caosdb/server/utils/FileUtils.java +++ b/src/main/java/caosdb/server/utils/FileUtils.java @@ -32,7 +32,6 @@ import caosdb.server.FileSystem; import caosdb.server.ServerProperties; import caosdb.server.database.exceptions.TransactionException; import caosdb.server.entity.Message; -import com.google.common.io.Files; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -291,20 +290,28 @@ public class FileUtils { } } } - + + private static File getTmpFile(String prefix) { + return new File(new File(FileSystem.getTmp()), prefix + Utils.getUID()); + } + private static boolean exists(File file) { - return java.nio.file.Files.exists(file.toPath(), LinkOption.NOFOLLOW_LINKS); + return java.nio.file.Files.exists(file.toPath(), LinkOption.NOFOLLOW_LINKS); + } + + private static boolean isDir(File file) { + return java.nio.file.Files.isDirectory(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); - } + if (exists(target)) { + org.apache.commons.io.FileUtils.forceDelete(target); + } + if (isDir(file)) { + 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 { @@ -321,8 +328,8 @@ public class FileUtils { 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()); - moveReplace(target, tmp); + final File tmp = getTmpFile(target.getName()); + moveReplace(target, tmp); backup = tmp; } else { backup = null; @@ -336,14 +343,16 @@ public class FileUtils { @Override public void undo() { try { - java.nio.file.Files.move(target.toPath(), file.toPath()); + moveReplace(target, file); if (_backup != null) { - java.nio.file.Files.move(_backup.toPath(), target.toPath()); + moveReplace(_backup, target); } } catch (Exception e) { - System.err.println("UNDO ERROR. Backup File in " + _backup.getAbsolutePath()); - // prevent _backup from being deleted during cleanup - _backup = null; + if (_backup != null) { + System.err.println("UNDO ERROR. Backup File in " + _backup.getAbsolutePath()); + // prevent _backup from being deleted during cleanup + _backup = null; + } e.printStackTrace(); } } @@ -351,7 +360,11 @@ public class FileUtils { @Override public void cleanUp() { if (_backup != null) { - _backup.delete(); + try { + deleteFinally(_backup); + } catch (IOException e) { + e.printStackTrace(); + } } } @@ -366,6 +379,10 @@ public class FileUtils { } } + private static void deleteFinally(File file) throws IOException { + org.apache.commons.io.FileUtils.forceDelete(file); + } + public static Undoable delete(final File file) throws IOException { return delete(file, false); } @@ -384,60 +401,75 @@ public class FileUtils { public static Undoable delete(final File file, final boolean recursive) throws IOException { if (file.exists() && file.isFile()) { // delete file - final File backup = - File.createTempFile(file.getName(), Utils.getUID(), new File(FileSystem.getTmp())); + final File backup = getTmpFile(file.getName()); // move to tmp file - file.renameTo(backup); + moveReplace(file, backup); return new Undoable() { + + private File _backup = backup; + @Override public void undo() { - // move back - backup.renameTo(file); + try { + moveReplace(_backup, file); + } catch (Exception e) { + System.err.println("UNDO ERROR. Backup File in " + _backup.getAbsolutePath()); + // prevent _backup from being deleted during cleanup + _backup = null; + e.printStackTrace(); + } } @Override public void cleanUp() { - backup.delete(); + if (_backup != null) { + try { + deleteFinally(_backup); + } catch (IOException e) { + e.printStackTrace(); + } + } } }; } else if (file.exists() && file.isDirectory()) { // directory if (recursive && file.listFiles().length > 0) { // delete directory recursively with all of its sub folders - final File backup = new File(FileSystem.getTmp() + file.getName() + Utils.getUID()); + final File backup = getTmpFile(file.getName()); // move to tmp folder - Files.move(file, backup); + moveReplace(file, backup); return new Undoable() { + + private File _backup = backup; + @Override public void undo() { try { - // move back - Files.move(backup, file); - } catch (final IOException e) { + moveReplace(_backup, file); + } catch (Exception e) { + System.err.println("UNDO ERROR. Backup File in " + _backup.getAbsolutePath()); + // prevent _backup from being deleted during cleanup + _backup = null; e.printStackTrace(); } } @Override public void cleanUp() { - Process cmd; - try { - cmd = Runtime.getRuntime().exec("rm -r " + backup.getAbsolutePath()); + if (_backup != null) { try { - cmd.waitFor(); - } catch (final InterruptedException e) { + deleteFinally(_backup); + } catch (IOException e) { e.printStackTrace(); } - } catch (final IOException e1) { - e1.printStackTrace(); } } }; } else if (file.listFiles().length == 0) { // non-recursive mode: delete empty directory - file.delete(); + deleteFinally(file); return new Undoable() { @Override public void undo() { -- GitLab