X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FFsUtils.java;h=cd61b56198fc2428fcf96ce02be5bd6656497f34;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=2d1363b616394a88725831b877f2937cfdcd9411;hpb=51efb630db7314b67654a03d1bd983b45aa2f1ed;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/FsUtils.java b/org.argeo.util/src/org/argeo/util/FsUtils.java index 2d1363b61..cd61b5619 100644 --- a/org.argeo.util/src/org/argeo/util/FsUtils.java +++ b/org.argeo.util/src/org/argeo/util/FsUtils.java @@ -9,23 +9,38 @@ import java.nio.file.attribute.BasicFileAttributes; /** Utilities around the standard Java file abstractions. */ public class FsUtils { - /** Sync a source path with a target path. */ - public static void sync(Path sourceBasePath, Path targetBasePath) { - sync(sourceBasePath, targetBasePath, false); - } - - /** Sync a source path with a target path. */ - public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) { - sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete, true)); - } - public static void sync(BasicSyncFileVisitor syncFileVisitor) { + /** Deletes this path, recursively if needed. */ + public static void copyDirectory(Path source, Path target) { + if (!Files.exists(source) || !Files.isDirectory(source)) + throw new IllegalArgumentException(source + " is not a directory"); + if (Files.exists(target) && !Files.isDirectory(target)) + throw new IllegalArgumentException(target + " is not a directory"); try { - Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor); - } catch (Exception e) { - throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with " - + syncFileVisitor.getTargetBasePath(), e); + Files.createDirectories(target); + Files.walkFileTree(source, new SimpleFileVisitor() { + + @Override + public FileVisitResult preVisitDirectory(Path directory, BasicFileAttributes attrs) throws IOException { + Path relativePath = source.relativize(directory); + Path targetDirectory = target.resolve(relativePath); + if (!Files.exists(targetDirectory)) + Files.createDirectory(targetDirectory); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Path relativePath = source.relativize(file); + Path targetFile = target.resolve(relativePath); + Files.copy(file, targetFile); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new RuntimeException("Cannot copy " + source + " to " + target, e); } + } /**