This is an automated email from the git hooks/post-receive script. New commit to branch support/3.13.x in repository tutti. See http://git.codelutin.com/tutti.git commit 1f5e000ddffbed86df421b6ebf5cc4f38f2f0a1f Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Apr 15 08:33:40 2015 +0200 refs #6908 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 | 17 +++- .../ui/swing/updater/UpdaterFileSystemPathes.java | 27 +++++- .../tutti/ui/swing/updater/MoveHelperTest.java | 28 +++++++ 5 files changed, 171 insertions(+), 5 deletions(-) diff --git a/tutti-ui-swing-updater/pom.xml b/tutti-ui-swing-updater/pom.xml index bfef0a0..f8476cf 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 c4b487f..5f19bd8 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; @@ -252,7 +250,20 @@ public class Updater { // Installing new module System.out.println(String.format("%s Install new version %s", moduleNameStr, newVersion)); Path moduleNewPath = pathHelper.getUpdateModulePath(updateModule); - Files.move(moduleNewPath, modulePath, StandardCopyOption.REPLACE_EXISTING); + + try { + + pathHelper.move(moduleNewPath, modulePath); + + } catch (IOException e) { + + String message = String.format("La mise à jour du module '%s' a échouée.\nVeuillez contacter un administrateur.", updateModule); + System.out.println(message); + JOptionPane.showMessageDialog(null, message); + + e.printStackTrace(); + + } } else { 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 ef85150..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 @@ -29,6 +29,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.PosixFilePermission; import java.text.SimpleDateFormat; import java.util.Date; @@ -200,8 +201,29 @@ 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 to %s", updateModule.getModuleLoggerName(), version, backupModulePath)); - Files.move(modulePath, backupModulePath); + System.out.println(String.format("%s Backup old version %s from %s to %s", updateModule.getModuleLoggerName(), version, modulePath, backupModulePath)); + 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); + + } } @@ -226,4 +248,5 @@ public class UpdaterFileSystemPathes { } return lines.get(0); } + } 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>.