(TWILL-156) use Files.move instead of File.renameTo so we can have options to replace existing files and perform atomic move, this allows us to support windows rename
This closes #72 on GitHub. Signed-off-by: Terence Yim <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/incubator-twill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-twill/commit/359b12b9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-twill/tree/359b12b9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-twill/diff/359b12b9 Branch: refs/heads/site Commit: 359b12b904e0c52c54453c6afc5ef578d484c0a5 Parents: 87b063c Author: shankar <[email protected]> Authored: Fri Dec 4 15:46:59 2015 -0800 Committer: Terence Yim <[email protected]> Committed: Fri Dec 4 23:56:58 2015 -0800 ---------------------------------------------------------------------- .../java/org/apache/twill/filesystem/LocalLocation.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/359b12b9/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java ---------------------------------------------------------------------- diff --git a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java index a560694..a873545 100644 --- a/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java +++ b/twill-common/src/main/java/org/apache/twill/filesystem/LocalLocation.java @@ -25,6 +25,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collections; import java.util.Deque; @@ -176,9 +179,11 @@ final class LocalLocation implements Location { @Override public Location renameTo(Location destination) throws IOException { // destination will always be of the same type as this location - boolean success = file.renameTo(((LocalLocation) destination).file); - if (success) { - return new LocalLocation(locationFactory, ((LocalLocation) destination).file); + Path target = Files.move(file.toPath(), ((LocalLocation) destination).file.toPath(), + StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + + if (target != null) { + return new LocalLocation(locationFactory, target.toFile()); } else { return null; }
