1
2
3
4
5 package oshi.util;
6
7 import java.nio.file.FileSystem;
8 import java.nio.file.FileSystems;
9 import java.nio.file.Path;
10 import java.nio.file.PathMatcher;
11 import java.nio.file.Paths;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import oshi.annotation.concurrent.ThreadSafe;
16
17
18
19
20 @ThreadSafe
21 public final class FileSystemUtil {
22
23 private static final String GLOB_PREFIX = "glob:";
24 private static final String REGEX_PREFIX = "regex:";
25
26 private FileSystemUtil() {
27 }
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public static boolean isFileStoreExcluded(String path, String volume, List<PathMatcher> pathIncludes,
45 List<PathMatcher> pathExcludes, List<PathMatcher> volumeIncludes, List<PathMatcher> volumeExcludes) {
46 Path p = Paths.get(path);
47 Path v = Paths.get(volume);
48 if (matches(p, pathIncludes) || matches(v, volumeIncludes)) {
49 return false;
50 }
51 return matches(p, pathExcludes) || matches(v, volumeExcludes);
52 }
53
54
55
56
57
58
59
60 public static List<PathMatcher> loadAndParseFileSystemConfig(String configPropertyName) {
61 String config = GlobalConfig.get(configPropertyName, "");
62 return parseFileSystemConfig(config);
63 }
64
65
66
67
68
69
70
71 public static List<PathMatcher> parseFileSystemConfig(String config) {
72 FileSystem fs = FileSystems.getDefault();
73 List<PathMatcher> patterns = new ArrayList<>();
74 for (String item : config.split(",")) {
75 if (item.length() > 0) {
76
77
78 if (!(item.startsWith(GLOB_PREFIX) || item.startsWith(REGEX_PREFIX))) {
79 item = GLOB_PREFIX + item;
80 }
81 patterns.add(fs.getPathMatcher(item));
82 }
83 }
84 return patterns;
85 }
86
87
88
89
90
91
92
93
94
95 public static boolean matches(Path text, List<PathMatcher> patterns) {
96 for (PathMatcher pattern : patterns) {
97 if (pattern.matches(text)) {
98 return true;
99 }
100 }
101 return false;
102 }
103
104 }