X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Ffs%2FBasicSyncFileVisitor.java;h=03bac592c72d100a0bf56a9e210c245e16b94572;hb=a8731626eaf812794bec138649de575e6e036245;hp=c60492d0847285ce3eae690f37bf74e2250dd1f9;hpb=4835aba6de0e6e2f7ef2da9e3bd19adca661c8bc;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java b/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java index c60492d08..03bac592c 100644 --- a/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java +++ b/org.argeo.jcr/src/org/argeo/fs/BasicSyncFileVisitor.java @@ -9,23 +9,31 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; +import org.argeo.sync.SyncResult; + /** Synchronises two directory structures. */ public class BasicSyncFileVisitor extends SimpleFileVisitor { // TODO make it configurable - private boolean debug = false; + private boolean trace = false; private final Path sourceBasePath; private final Path targetBasePath; private final boolean delete; + private final boolean recursive; + + private SyncResult syncResult = new SyncResult<>(); - public BasicSyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete) { + public BasicSyncFileVisitor(Path sourceBasePath, Path targetBasePath, boolean delete, boolean recursive) { this.sourceBasePath = sourceBasePath; this.targetBasePath = targetBasePath; this.delete = delete; + this.recursive = recursive; } @Override public FileVisitResult preVisitDirectory(Path sourceDir, BasicFileAttributes attrs) throws IOException { + if (!recursive && !sourceDir.equals(sourceBasePath)) + return FileVisitResult.SKIP_SUBTREE; Path targetDir = toTargetPath(sourceDir); Files.createDirectories(targetDir); return FileVisitResult.CONTINUE; @@ -56,7 +64,7 @@ public class BasicSyncFileVisitor extends SimpleFileVisitor { try { if (!Files.exists(targetFile)) { Files.copy(sourceFile, targetFile); - copied(sourceFile, targetFile); + added(sourceFile, targetFile); } else { if (shouldOverwrite(sourceFile, targetFile)) { Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING); @@ -105,22 +113,34 @@ public class BasicSyncFileVisitor extends SimpleFileVisitor { return targetBasePath; } - protected void copied(Path sourcePath, Path targetPath) { - if (isDebugEnabled()) - debug("Copied " + sourcePath + " to " + targetPath); + protected void added(Path sourcePath, Path targetPath) { + syncResult.getAdded().add(targetPath); + if (isTraceEnabled()) + trace("Added " + sourcePath + " as " + targetPath); + } + + protected void modified(Path sourcePath, Path targetPath) { + syncResult.getModified().add(targetPath); + if (isTraceEnabled()) + trace("Overwritten from " + sourcePath + " to " + targetPath); } protected void copyFailed(Path sourcePath, Path targetPath, Exception e) { - error("Cannot copy " + sourcePath + " to " + targetPath, e); + syncResult.addError(sourcePath, targetPath, e); + if (isTraceEnabled()) + error("Cannot copy " + sourcePath + " to " + targetPath, e); } protected void deleted(Path targetPath) { - if (isDebugEnabled()) - debug("Deleted " + targetPath); + syncResult.getDeleted().add(targetPath); + if (isTraceEnabled()) + trace("Deleted " + targetPath); } protected void deleteFailed(Path targetPath, Exception e) { - error("Cannot delete " + targetPath, e); + syncResult.addError(null, targetPath, e); + if (isTraceEnabled()) + error("Cannot delete " + targetPath, e); } /** Log error. */ @@ -129,11 +149,16 @@ public class BasicSyncFileVisitor extends SimpleFileVisitor { e.printStackTrace(); } - protected boolean isDebugEnabled() { - return debug; + protected boolean isTraceEnabled() { + return trace; } - protected void debug(Object obj) { + protected void trace(Object obj) { System.out.println(obj); } + + public SyncResult getSyncResult() { + return syncResult; + } + }