This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit fdb99b4c1d2bf35fcd2d2c034e98c607e10d931f Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Apr 15 08:33:40 2015 +0200 refs #6935 Deplacement a la main d'une arborescence si pas possible de manière atomique --- tutti-ui-swing-updater/pom.xml | 7 ++ .../ifremer/tutti/ui/swing/updater/MoveHelper.java | 97 ++++++++++++++++++++++ .../fr/ifremer/tutti/ui/swing/updater/Updater.java | 8 +- .../ui/swing/updater/UpdaterFileSystemPathes.java | 50 +++++------ .../tutti/ui/swing/updater/MoveHelperTest.java | 28 +++++++ 5 files changed, 156 insertions(+), 34 deletions(-) diff --git a/tutti-ui-swing-updater/pom.xml b/tutti-ui-swing-updater/pom.xml index 2287152..5bfc629 100644 --- a/tutti-ui-swing-updater/pom.xml +++ b/tutti-ui-swing-updater/pom.xml @@ -45,6 +45,13 @@ <dependencies> <!-- please, no dependency --> + + <!-- Tests --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> <build> diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/MoveHelper.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/MoveHelper.java new file mode 100644 index 0000000..ef8e9b4 --- /dev/null +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/MoveHelper.java @@ -0,0 +1,97 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * Created on 4/15/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.13.8 + */ +public class MoveHelper { + + + public static void move(Path sourceDirectory, Path targetDirectory, boolean dryRun) throws IOException { + + MoveTreeVisitor copyVisitor = new MoveTreeVisitor(sourceDirectory, targetDirectory, dryRun); + Files.walkFileTree(sourceDirectory, copyVisitor); + + } + + static class MoveTreeVisitor extends SimpleFileVisitor<Path> { + + final Path sourceDirectory; + + final int sourcePathCount; + + final Path targetDirectory; + + final boolean dryRun; + + MoveTreeVisitor(Path sourceDirectory, Path targetDirectory, boolean dryRun) { + this.sourceDirectory = sourceDirectory; + this.targetDirectory = targetDirectory; + this.dryRun = dryRun; + this.sourcePathCount = sourceDirectory.getNameCount(); + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + + Path target; + + if (sourceDirectory.equals(dir)) { + + target = targetDirectory; + + } else { + + Path sourceRelativize = dir.subpath(sourcePathCount, dir.getNameCount()); + target = targetDirectory.resolve(sourceRelativize).toAbsolutePath(); + + } + + System.out.println(String.format("Create directory: %s", target)); + if (!dryRun) { + Files.createDirectories(target); + } + + return FileVisitResult.CONTINUE; + + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + + Path subPath = file.subpath(sourcePathCount, file.getNameCount()); + Path target = targetDirectory.resolve(subPath).normalize().toAbsolutePath(); + + System.out.println(String.format("Copy file from %s to %s", file, target)); + if (!dryRun) { + Files.move(file, target); + } + + return FileVisitResult.CONTINUE; + + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + + System.out.println(String.format("Delete directory: %s", dir)); + if (!dryRun) { + Files.delete(dir); + } + + return FileVisitResult.CONTINUE; + + } + + } + +} diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java index fbb7cc0..163497d 100644 --- a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/Updater.java @@ -43,8 +43,6 @@ import java.util.Date; */ public class Updater { - public static final String APPLICATION_UPDATER_TITLE = "Allegro Campaign UI Updater"; - public static final int NORMAL_EXIT_CODE = 0; public static final int ERROR_EXIT_CODE = 1; @@ -254,14 +252,14 @@ public class Updater { } - // Installing new& module + // Installing new module System.out.println(String.format("%s Install new version %s", moduleNameStr, newVersion)); Path modulePath = pathHelper.getModulePath(updateModule); Path moduleNewPath = pathHelper.getUpdateModulePath(updateModule); try { - pathHelper.move(moduleNewPath, modulePath, true); + pathHelper.move(moduleNewPath, modulePath); } catch (IOException e) { @@ -271,10 +269,8 @@ public class Updater { e.printStackTrace(); - } - } else { System.out.println(String.format("%s No update found", moduleNameStr)); diff --git a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java index 029d543..2a5e634 100644 --- a/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java +++ b/tutti-ui-swing-updater/src/main/java/fr/ifremer/tutti/ui/swing/updater/UpdaterFileSystemPathes.java @@ -27,7 +27,6 @@ package fr.ifremer.tutti.ui.swing.updater; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -203,7 +202,28 @@ public class UpdaterFileSystemPathes { Path backupModulePath = backupDirectory.resolve(String.format("%s-%s-%s", moduleName, version, backupDate)); System.out.println(String.format("%s Backup old version %s from %s to %s", updateModule.getModuleLoggerName(), version, modulePath, backupModulePath)); - move(modulePath, backupModulePath, false); + move(modulePath, backupModulePath); + + } + + public void move(Path source, Path target) throws IOException { + + Path absoluteSourcePath = source.toAbsolutePath(); + Path absoluteTargetPath = target.toAbsolutePath(); + + try { + + System.out.println(String.format("Try to move from %s to %s", absoluteSourcePath, absoluteTargetPath)); + Files.move(absoluteSourcePath, absoluteTargetPath, StandardCopyOption.ATOMIC_MOVE); + + } catch (IOException e) { + + // copy atomic impossible + System.out.println(String.format("Try fallback install (copy then delete, atomic move is not possible from %s to %s)", source, target)); + + MoveHelper.move(absoluteSourcePath, absoluteTargetPath, false); + + } } @@ -229,30 +249,4 @@ public class UpdaterFileSystemPathes { return lines.get(0); } - protected void move(Path source, Path target, boolean deleteOnExitAsFallback) throws IOException { - - try { - - Files.move(source.toAbsolutePath(), target.toAbsolutePath(), StandardCopyOption.ATOMIC_MOVE); - - } catch (AtomicMoveNotSupportedException e) { - - // copy atomic impossible - System.out.println(String.format("%s Try fallback install (copy then delete, atomic move is not possible to %s)", source, target)); - Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); - - if (deleteOnExitAsFallback) { - - DeleteHelper.deleteDirectoryOnExit(source); - - } else { - - DeleteHelper.deleteDirectory(source); - - } - - } - - - } } diff --git a/tutti-ui-swing-updater/src/test/java/fr/ifremer/tutti/ui/swing/updater/MoveHelperTest.java b/tutti-ui-swing-updater/src/test/java/fr/ifremer/tutti/ui/swing/updater/MoveHelperTest.java new file mode 100644 index 0000000..7110747 --- /dev/null +++ b/tutti-ui-swing-updater/src/test/java/fr/ifremer/tutti/ui/swing/updater/MoveHelperTest.java @@ -0,0 +1,28 @@ +package fr.ifremer.tutti.ui.swing.updater; + +import org.junit.Ignore; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Created on 4/15/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.13.8 + */ +public class MoveHelperTest { + + @Ignore + @Test + public void testMove() throws IOException { + + Path source = Paths.get("/opt/"); + Path target= Paths.get("/home/"); + + MoveHelper.move(source, target, true); + + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.