]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/FsUtils.java
Optimise JNI compilation (-O3)
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / FsUtils.java
1 package org.argeo.util;
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 /**
32 * Deletes this path, recursively if needed. Does nothing if the path does not
33 * exist.
34 */
35 public static void delete(Path path) {
36 try {
37 if (!Files.exists(path))
38 return;
39 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
40 @Override
41 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
42 if (e != null)
43 throw e;
44 Files.delete(directory);
45 return FileVisitResult.CONTINUE;
46 }
47
48 @Override
49 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
50 Files.delete(file);
51 return FileVisitResult.CONTINUE;
52 }
53 });
54 } catch (IOException e) {
55 throw new RuntimeException("Cannot delete " + path, e);
56 }
57 }
58
59 /** Singleton. */
60 private FsUtils() {
61 }
62
63 }