X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.slc.runtime%2Fsrc%2Forg%2Fargeo%2Ffs%2FFsUtils.java;fp=org.argeo.slc.runtime%2Fsrc%2Forg%2Fargeo%2Ffs%2FFsUtils.java;h=c96f56ed26818dae6fc2f2e3f792326bb802c502;hb=f27e8fc8c485b839950728871671605b8666770e;hp=0000000000000000000000000000000000000000;hpb=68cb92496805e0649c3f8a540bf6a87fbc3b3812;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.runtime/src/org/argeo/fs/FsUtils.java b/org.argeo.slc.runtime/src/org/argeo/fs/FsUtils.java new file mode 100644 index 000000000..c96f56ed2 --- /dev/null +++ b/org.argeo.slc.runtime/src/org/argeo/fs/FsUtils.java @@ -0,0 +1,58 @@ +package org.argeo.fs; + +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; + +/** 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) { + try { + Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor); + } catch (Exception e) { + throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with " + + syncFileVisitor.getTargetBasePath(), e); + } + } + + /** Deletes this path, recursively if needed. */ + public static void delete(Path path) { + try { + Files.walkFileTree(path, new SimpleFileVisitor() { + @Override + public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException { + if (e != null) + throw e; + Files.delete(directory); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new RuntimeException("Cannot delete " + path, e); + } + } + + /** Singleton. */ + private FsUtils() { + } + +}