]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/fs/FsSyncUtils.java
Use runtime namespace context as default.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / fs / FsSyncUtils.java
1 package org.argeo.cms.acr.fs;
2
3 import java.io.IOException;
4 import java.nio.file.FileVisitResult;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.SimpleFileVisitor;
8 import java.nio.file.attribute.BasicFileAttributes;
9
10 public class FsSyncUtils {
11 /** Sync a source path with a target path. */
12 public static void sync(Path sourceBasePath, Path targetBasePath) {
13 sync(sourceBasePath, targetBasePath, false);
14 }
15
16 /** Sync a source path with a target path. */
17 public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) {
18 sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete, true));
19 }
20
21 public static void sync(BasicSyncFileVisitor syncFileVisitor) {
22 try {
23 Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor);
24 } catch (Exception e) {
25 throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with "
26 + syncFileVisitor.getTargetBasePath(), e);
27 }
28 }
29
30 /**
31 * Deletes this path, recursively if needed. Does nothing if the path does not
32 * exist.
33 */
34 public static void delete(Path path) {
35 try {
36 if (!Files.exists(path))
37 return;
38 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
39 @Override
40 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
41 if (e != null)
42 throw e;
43 Files.delete(directory);
44 return FileVisitResult.CONTINUE;
45 }
46
47 @Override
48 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
49 Files.delete(file);
50 return FileVisitResult.CONTINUE;
51 }
52 });
53 } catch (IOException e) {
54 throw new RuntimeException("Cannot delete " + path, e);
55 }
56 }
57
58 /** Singleton. */
59 private FsSyncUtils() {
60
61 }
62 }