View Javadoc
1   /**
2    * Oshi (https://github.com/dblock/oshi)
3    * 
4    * Copyright (c) 2010 - 2015 The Oshi Project Team
5    * 
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   * 
11   * Contributors:
12   * dblock[at]dblock[dot]org
13   * alessandro[at]perucchi[dot]org
14   * widdis[at]gmail[dot]com
15   * https://github.com/dblock/oshi/graphs/contributors
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   * The Mac File System contains {@link OSFileStore}s which are a storage pool,
33   * device, partition, volume, concrete file system or other implementation
34   * specific means of file storage. In Mac OS X, these are found in the /Volumes
35   * directory.
36   * 
37   * @author widdis[at]gmail[dot]com
38   */
39  public class MacFileSystem {
40  	// Regexp matcher for /dev/disk1 etc.
41  	private static final Pattern localDisk = Pattern.compile("/dev/disk\\d");
42  
43  	/**
44  	 * Gets File System Information.
45  	 * 
46  	 * @return An array of {@link OSFileStore} objects representing mounted
47  	 *         volumes. May return disconnected volumes with
48  	 *         {@link OSFileStore#getTotalSpace()} = 0.
49  	 * @throws IOException
50  	 */
51  	public static OSFileStore[] getFileStores() {
52  		List<OSFileStore> fsList = new ArrayList<>();
53  		FileSystemView fsv = FileSystemView.getFileSystemView();
54  		// Mac file systems are mounted in /Volumes
55  		File volumes = new File("/Volumes");
56  		if (volumes != null && volumes.listFiles() != null)
57  			for (File f : volumes.listFiles()) {
58  				// Everyone hates DS Store
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  }