-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUtils.java
More file actions
108 lines (98 loc) · 3.75 KB
/
FileUtils.java
File metadata and controls
108 lines (98 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.codejive.jpm.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** Utility class for file operations. */
public class FileUtils {
/**
* Synchronizes a list of artifacts with a target directory.
*
* @param artifacts list of artifacts to synchronize
* @param directory target directory
* @param noLinks if true, copy artifacts instead of creating symbolic links
* @param noDelete if true, do not delete artifacts that are no longer needed
* @return An instance of {@link SyncResult} with statistics about the synchronization
* @throws IOException if an error occurred during the synchronization
*/
public static SyncResult syncArtifacts(
List<Path> artifacts, Path directory, boolean noLinks, boolean noDelete)
throws IOException {
SyncResult stats = new SyncResult();
// Make sure the target directory exists
Files.createDirectories(directory);
// Remember current artifact names in target directory (if any)
Set<String> artifactsToDelete = new HashSet<>();
if (!noDelete) {
File[] files = directory.toFile().listFiles(File::isFile);
if (files != null) {
for (File file : files) {
artifactsToDelete.add(file.getName());
}
}
}
// Copy artifacts
for (Path artifact : artifacts) {
String artifactName = artifact.getFileName().toString();
artifactsToDelete.remove(artifactName);
Path target = directory.resolve(artifactName);
stats.files.add(target);
if (!Files.exists(target)) {
copyDependency(artifact, directory, noLinks);
stats.copied++;
} else if (Files.isSymbolicLink(target) == noLinks) {
copyDependency(artifact, directory, noLinks);
stats.updated++;
} else if (Files.size(target) != Files.size(artifact)
|| !Files.getLastModifiedTime(target)
.equals(Files.getLastModifiedTime(artifact))) {
copyDependency(artifact, directory, noLinks);
stats.updated++;
}
}
// Now remove any artifacts that are no longer needed
if (!noDelete) {
for (String existingArtifact : artifactsToDelete) {
Path target = directory.resolve(existingArtifact);
Files.delete(target);
stats.deleted++;
}
}
return stats;
}
private static void copyDependency(Path artifact, Path directory, boolean noLinks)
throws IOException {
Path target = directory.resolve(artifact.getFileName().toString());
if (!noLinks) {
Files.deleteIfExists(target);
try {
Files.createSymbolicLink(target, artifact);
return;
} catch (IOException e) {
// Creating a symlink might fail (eg on Windows) so we
// fall through and try again by simply copying the file
}
}
Files.copy(
artifact,
target,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES);
}
public static Path safePath(String w) {
if (w == null) {
return null;
}
try {
return Paths.get(w);
} catch (InvalidPathException e) {
return null;
}
}
}