/** * Checks whether a path is the mounting point of a RAM disk volume. * * @param path a string represents the path to be checked * @param fsTypes an array of strings represents expected file system type * @return true if the path is the mounting point of volume with one of the given fsTypes, * false otherwise * @throws IOException if the function fails to get the mount information of the system */ public static boolean isMountingPoint(String path, String[] fsTypes) throws IOException { List<UnixMountInfo> infoList = ShellUtils.getUnixMountInfo(); for (UnixMountInfo info : infoList) { Optional<String> mountPoint = info.getMountPoint(); Optional<String> fsType = info.getFsType(); if (mountPoint.isPresent() && mountPoint.get().equals(path) && fsType.isPresent()) { for (String expectedType : fsTypes) { if (fsType.get().equalsIgnoreCase(expectedType)) { return true; } } } } return false; }
Optional<String> mountPointOption = mountInfo.getMountPoint(); Optional<String> fsTypeOption = mountInfo.getFsType(); Optional<Long> sizeOption = mountInfo.getOptions().getSize();
@Test public void parseRamfsMountInfoWithType() throws Exception { // Linux mount info. UnixMountInfo info = ShellUtils.parseMountInfo("ramfs on /mnt/ramdisk type ramfs (rw,relatime,size=1gb)"); assertEquals(Optional.of("ramfs"), info.getDeviceSpec()); assertEquals(Optional.of("/mnt/ramdisk"), info.getMountPoint()); assertEquals(Optional.of("ramfs"), info.getFsType()); assertEquals(Optional.of(Long.valueOf(Constants.GB)), info.getOptions().getSize()); }
@Test public void parseTmpfsMountInfo() throws Exception { // Docker VM mount info. UnixMountInfo info = ShellUtils .parseMountInfo("shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)"); assertEquals(Optional.of("shm"), info.getDeviceSpec()); assertEquals(Optional.of("/dev/shm"), info.getMountPoint()); assertEquals(Optional.of("tmpfs"), info.getFsType()); assertEquals(Optional.of(Long.valueOf(65536 * Constants.KB)), info.getOptions().getSize()); }
@Test public void parseMountInfoWithoutType() throws Exception { // OS X mount info. UnixMountInfo info = ShellUtils.parseMountInfo("devfs on /dev (devfs, local, nobrowse)"); assertEquals(Optional.of("devfs"), info.getDeviceSpec()); assertEquals(Optional.of("/dev"), info.getMountPoint()); assertFalse(info.getFsType().isPresent()); assertFalse(info.getOptions().getSize().isPresent()); }
@Test public void parseMountInfoSpaceInPath() throws Exception { UnixMountInfo info = ShellUtils.parseMountInfo("/dev/disk4s1 on /Volumes/Space Path " + "(hfs, local, nodev, nosuid, read-only, noowners, quarantine)"); assertEquals(Optional.of("/dev/disk4s1"), info.getDeviceSpec()); assertEquals(Optional.of("/Volumes/Space Path"), info.getMountPoint()); assertFalse(info.getFsType().isPresent()); assertFalse(info.getOptions().getSize().isPresent()); }
/** * Checks whether a path is the mounting point of a RAM disk volume. * * @param path a string represents the path to be checked * @param fsTypes an array of strings represents expected file system type * @return true if the path is the mounting point of volume with one of the given fsTypes, * false otherwise * @throws IOException if the function fails to get the mount information of the system */ public static boolean isMountingPoint(String path, String[] fsTypes) throws IOException { List<UnixMountInfo> infoList = ShellUtils.getUnixMountInfo(); for (UnixMountInfo info : infoList) { Optional<String> mountPoint = info.getMountPoint(); Optional<String> fsType = info.getFsType(); if (mountPoint.isPresent() && mountPoint.get().equals(path) && fsType.isPresent()) { for (String expectedType : fsTypes) { if (fsType.get().equalsIgnoreCase(expectedType)) { return true; } } } } return false; }