1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package oshi.software.os.mac.local;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.file.FileStore;
22 import java.nio.file.Files;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Pattern;
26
27 import javax.swing.filechooser.FileSystemView;
28
29 import oshi.software.os.OSFileStore;
30
31
32
33
34
35
36
37
38
39 public class MacFileSystem {
40
41 private static final Pattern localDisk = Pattern.compile("/dev/disk\\d");
42
43
44
45
46
47
48
49
50
51 public static OSFileStore[] getFileStores() {
52 List<OSFileStore> fsList = new ArrayList<>();
53 FileSystemView fsv = FileSystemView.getFileSystemView();
54
55 File volumes = new File("/Volumes");
56 if (volumes != null && volumes.listFiles() != null)
57 for (File f : volumes.listFiles()) {
58
59 if (f.getName().endsWith(".DS_Store"))
60 continue;
61 String name = fsv.getSystemDisplayName(f);
62 String description = "Volume";
63 try {
64 if (f.getCanonicalPath().equals("/"))
65 name = name + " (/)";
66 FileStore fs = Files.getFileStore(f.toPath());
67 if (localDisk.matcher(fs.name()).matches())
68 description = "Local Disk";
69 if (fs.name().startsWith("localhost:")
70 || fs.name().startsWith("//"))
71 description = "Network Drive";
72 } catch (IOException e) {
73 continue;
74 }
75 fsList.add(new OSFileStore(name, description, f
76 .getUsableSpace(), f.getTotalSpace()));
77 }
78 return fsList.toArray(new OSFileStore[fsList.size()]);
79 }
80 }