]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/fs/FsUtils.java
Centralises .gitignore configuration.
[lgpl/argeo-commons.git] / org.argeo.core / src / org / argeo / fs / FsUtils.java
1 package org.argeo.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 /** Utilities around the standard Java file abstractions. */
11 public class FsUtils {
12 /** Sync a source path with a target path. */
13 public static void sync(Path sourceBasePath, Path targetBasePath) {
14 sync(sourceBasePath, targetBasePath, false);
15 }
16
17 /** Sync a source path with a target path. */
18 public static void sync(Path sourceBasePath, Path targetBasePath, boolean delete) {
19 sync(new BasicSyncFileVisitor(sourceBasePath, targetBasePath, delete, true));
20 }
21
22 public static void sync(BasicSyncFileVisitor syncFileVisitor) {
23 try {
24 Files.walkFileTree(syncFileVisitor.getSourceBasePath(), syncFileVisitor);
25 } catch (Exception e) {
26 throw new RuntimeException("Cannot sync " + syncFileVisitor.getSourceBasePath() + " with "
27 + syncFileVisitor.getTargetBasePath(), e);
28 }
29 }
30
31 /** Deletes this path, recursively if needed. */
32 public static void delete(Path path) {
33 try {
34 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
35 @Override
36 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
37 if (e != null)
38 throw e;
39 Files.delete(directory);
40 return FileVisitResult.CONTINUE;
41 }
42
43 @Override
44 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
45 Files.delete(file);
46 return FileVisitResult.CONTINUE;
47 }
48 });
49 } catch (IOException e) {
50 throw new RuntimeException("Cannot delete " + path, e);
51 }
52 }
53
54 /** Singleton. */
55 private FsUtils() {
56 }
57
58 }