diff options
author | Simon Rettberg | 2020-01-17 12:48:41 +0100 |
---|---|---|
committer | Simon Rettberg | 2020-01-17 12:48:41 +0100 |
commit | 805c69cbf378205406659a3680f80eb48cb90c6f (patch) | |
tree | db2320730b5bcf2454d821dd77d120c7470c0014 /src/main/java/org/openslx/taskmanager | |
parent | [WakeOnLan] Native Java implementation (diff) | |
download | tmlite-bwlp-805c69cbf378205406659a3680f80eb48cb90c6f.tar.gz tmlite-bwlp-805c69cbf378205406659a3680f80eb48cb90c6f.tar.xz tmlite-bwlp-805c69cbf378205406659a3680f80eb48cb90c6f.zip |
[Symlink] Don't follow symlinks when deleting old link source
Diffstat (limited to 'src/main/java/org/openslx/taskmanager')
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/Symlink.java | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/Symlink.java b/src/main/java/org/openslx/taskmanager/tasks/Symlink.java index 7a6a3bd..44919fc 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/Symlink.java +++ b/src/main/java/org/openslx/taskmanager/tasks/Symlink.java @@ -59,7 +59,7 @@ public class Symlink extends AbstractTask @Override protected boolean execute() { - FileUtils.deleteQuietly( new File( linkname ) ); + deleteRecursiveIfExists( new File( linkname ) ); if ( this.target != null ) { try { Files.createSymbolicLink( Paths.get( this.linkname ), Paths.get( this.target ) ); @@ -72,6 +72,28 @@ public class Symlink extends AbstractTask } /** + * Recursively deletes `item`, which may be a directory. + * Symbolic links will be deleted instead of their referents. + * Returns a boolean indicating whether `item` still exists. + * http://stackoverflow.com/questions/8666420 + */ + public static boolean deleteRecursiveIfExists( File item ) + { + if ( !item.exists() ) + return true; + boolean ret = true; + if ( !Files.isSymbolicLink( item.toPath() ) && item.isDirectory() ) { + File[] subitems = item.listFiles(); + for ( File subitem : subitems ) { + if ( !deleteRecursiveIfExists( subitem ) ) { + ret = false; + } + } + } + return item.delete() && ret; + } + + /** * Output - contains additional status data of this task */ @SuppressWarnings( "unused" ) |