/** * Get the file store type of a path. for example, /dev/sdd1(store name) /w2-gst-dev40d(mount * point) ext4(type) * * @param path * @return file store type */ public String getFileStoreType(final String path) { File diskFile = new File(path); if (!diskFile.exists()) { diskFile = diskFile.getParentFile(); } Path currentPath = diskFile.toPath(); if (currentPath.isAbsolute() && Files.exists(currentPath)) { try { FileStore store = Files.getFileStore(currentPath); return store.type(); } catch (IOException e) { return null; } } return null; }
/** * A constructor to create a Volume object from a FileStore object. * * @param root * @param fileStore */ public Volume(String root, FileStore fileStore) { label = fileStore.name(); //TODO VolumeLabel; name = fileStore.name(); type = fileStore.type(); format = fileStore.type(); // TODO DriveFormat path = root; // ex. C:\ try { size = fileStore.getTotalSpace(); free = fileStore.getUsableSpace(); } catch (IOException e) { e.printStackTrace(); } }
@SuppressForbidden(reason = "tries to determine if disk is spinning") // TODO: move PathUtils to be package-private here instead of // public+forbidden api! ESFileStore(FileStore in) { this.in = in; Boolean spins; // Lucene's IOUtils.spins only works on Linux today: if (Constants.LINUX) { try { spins = IOUtils.spins(PathUtils.get(getMountPointLinux(in))); } catch (Exception e) { spins = null; } } else { spins = null; } this.spins = spins; }
public static void main(String[] args) throws Exception { /** * * A FileSystem object encapsulates the fi le storage system on your computer. What this storage system consists of and how it is organized depends on the operating system you are using. */ FileSystem fileSystem = FileSystems.getDefault(); Iterable<FileStore> stores = fileSystem.getFileStores(); for(FileStore store : stores) { System.out.println(store.name() + ": " + store.type()); System.out.println(" -> free space: " + freeSpaceFormatted(store)); } }
@Test public void returnsResultFromThreshold() throws Exception { final long freeSpace = 128L; final long maxSpace = 1024L; FileStore fs = mock(FileStore.class); when(fs.getUsableSpace()).thenReturn(freeSpace); when(fs.getTotalSpace()).thenReturn(maxSpace); checker.setFileStore(fs); when(threshold.withinThreshold(freeSpace, maxSpace)).thenReturn(false); boolean result = checker.isSpaceAvailable(); assertThat(result).isFalse(); verify(fs).getUsableSpace(); verify(fs).getTotalSpace(); verify(threshold).withinThreshold(freeSpace, maxSpace); }
/** * Return {@code true} if paths {@code from} and {@code to} are located on same FileStore (volume or * partition). The {@code from} must exists, while {@code to} does not have to. */ private static boolean areOnSameFileStore(final Path from, final Path to) { try { final FileStore fromStore = Files.getFileStore(from); // from must exist Path toExistingParent = to.normalize(); // to usually does not exists, is about to be created as part of move while (toExistingParent != null && !Files.exists(toExistingParent)) { toExistingParent = toExistingParent.getParent(); } if (toExistingParent != null) { final FileStore toStore = Files.getFileStore(toExistingParent); return fromStore.equals(toStore); } else { log.warn("No ultimate parent path found for '{}'", to, new RuntimeException("marker")); // record the stack trace? return false; // no ultimate parent? be on safe side } } catch (IOException e) { return false; // be on safe side } }
private void fillFromStorage ( final Map<String, Object> model ) { if ( this.manager != null ) { final Path base = this.manager.getContext ().getBasePath (); try { final FileStore store = Files.getFileStore ( base ); model.put ( "storageTotal", store.getTotalSpace () ); model.put ( "storageFree", store.getUsableSpace () ); model.put ( "storageUsed", store.getTotalSpace () - store.getUsableSpace () ); model.put ( "storageName", store.name () ); } catch ( final Exception e ) { logger.warn ( "Failed to check storage space", e ); // ignore } } }
private static void showFileStoreInfo() throws IOException{ Iterable<FileStore> fs = fsys.getFileStores(); System.out.println("Available File Stores"); for(FileStore f:fs){ System.out.println("\t" + f); System.out.println("\t\tType: " + f.type()); System.out.println("\t\tRead-only: " + f.isReadOnly()); System.out.println("\t\tTotal Space: " + (f.getTotalSpace()) + " bytes"); System.out.println("\t\tUsable Space: " + f.getUsableSpace() + " bytes"); System.out.println("\t\tUnallocated Space: " + f.getUnallocatedSpace() + " bytes"); } System.out.println(); }
@Test public void testGetFileStores() { FileSystem fs = new TestFS().create(); Iterator< FileStore > defaultStores = DEFAULT_FS.getFileStores().iterator(); Iterator< FileStore > testStores = fs.getFileStores().iterator(); while (defaultStores.hasNext() && testStores.hasNext()) { FileStore defaultStore = defaultStores.next(); FileStore testStore = testStores.next(); assertEquals(defaultStore.name(), testStore.name()); } assertFalse(defaultStores.hasNext()); assertFalse(testStores.hasNext()); }
private Set<String> getSupportedFileAttributes(FileStore fs) { Set<String> attrs = new HashSet<String>(); if (fs.supportsFileAttributeView(AclFileAttributeView.class)) { attrs.add("acl"); } if (fs.supportsFileAttributeView(BasicFileAttributeView.class)) { attrs.add("basic"); } if (fs.supportsFileAttributeView(FileOwnerAttributeView.class)) { attrs.add("owner"); } if (fs.supportsFileAttributeView(UserDefinedFileAttributeView.class)) { attrs.add("user"); } if (fs.supportsFileAttributeView(DosFileAttributeView.class)) { attrs.add("dos"); } if (fs.supportsFileAttributeView(PosixFileAttributeView.class)) { attrs.add("posix"); } if (fs.supportsFileAttributeView(FileAttributeView.class)) { attrs.add("file"); } return attrs; }
private void sendDiskEvent(Path root) { try { FileStore store = Files.getFileStore(root); long totalSpace = store.getTotalSpace(); long usableSpace = store.getUsableSpace(); long unallocatedSpace = store.getUnallocatedSpace(); OsEventBuilder eventBuilder = OsEventBuilder.createDiskBuilder(this.getEventBuilderData()); eventBuilder.setDiskPath(root.toString()); eventBuilder.setTotalDiskSpace(totalSpace); eventBuilder.setUsableDiskSpace(usableSpace); eventBuilder.setUnallocatedDiskSpace(unallocatedSpace); eventBuilder.setUsedDiskSpace(totalSpace - unallocatedSpace); this.sendEvent(eventBuilder.toEvent()); } catch (Exception e) { // ignore } }
private static FileStore guessFileStore(String dir) throws IOException { Path path = Paths.get(dir); while (true) { try { return Files.getFileStore(path); } catch (IOException e) { if (e instanceof NoSuchFileException) path = path.getParent(); else throw e; } } }
@Test public void testFileStore() throws URISyntaxException, IOException { URI uri = clusterUri.resolve("/tmp/testFileStore"); Path path = Paths.get(uri); if (Files.exists(path)) Files.delete(path); assertFalse(Files.exists(path)); Files.createFile(path); assertTrue(Files.exists(path)); FileStore st = Files.getFileStore(path); assertNotNull(st); Assert.assertNotNull(st.name()); Assert.assertNotNull(st.type()); Assert.assertFalse(st.isReadOnly()); Assert.assertNotEquals(0, st.getTotalSpace()); Assert.assertNotEquals(0, st.getUnallocatedSpace()); Assert.assertNotEquals(0, st.getUsableSpace()); Assert .assertTrue(st.supportsFileAttributeView(BasicFileAttributeView.class)); Assert.assertTrue(st.supportsFileAttributeView("basic")); st.getAttribute("test"); }
/** * Test: File and FileStore attributes */ @Test public void testFileStoreAttributes() throws URISyntaxException, IOException { URI uri = clusterUri.resolve("/tmp/testFileStore"); Path path = Paths.get(uri); if (Files.exists(path)) Files.delete(path); assertFalse(Files.exists(path)); Files.createFile(path); assertTrue(Files.exists(path)); FileStore store1 = Files.getFileStore(path); assertNotNull(store1); assertTrue(store1.supportsFileAttributeView("basic")); assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("posix") == store1 .supportsFileAttributeView(PosixFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("dos") == store1 .supportsFileAttributeView(DosFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("acl") == store1 .supportsFileAttributeView(AclFileAttributeView.class)); assertTrue(store1.supportsFileAttributeView("user") == store1 .supportsFileAttributeView(UserDefinedFileAttributeView.class)); }
/** * Get the free disk space for a selected path. * * @return Free disk space in bytes. */ private long getFreeDiskSpace(final String strPath) { long usableSpace = 0; if (!strPath.isEmpty()) { try { Path path = Paths.get(strPath); if (!Files.exists(path)) { path = path.getParent(); } final FileStore fileStore = Files.getFileStore(path); usableSpace = fileStore.getUsableSpace(); } catch (Exception ignore) { } } return usableSpace; }
private void makeWritable(Path file) throws IOException { FileStore fileStore = Files.getFileStore(file); if (IS_WINDOWS && fileStore.supportsFileAttributeView(DosFileAttributeView.class)) { DosFileAttributeView dosAttribs = Files.getFileAttributeView(file, DosFileAttributeView.class); if (dosAttribs != null) { dosAttribs.setReadOnly(false); } } else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) { PosixFileAttributeView posixAttribs = Files.getFileAttributeView(file, PosixFileAttributeView.class); if (posixAttribs != null) { posixAttribs.setPermissions(EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)); } } }
@Override public TypedMap addContext(final TypedMap existing) { try { // Determine the file store for the directory the JVM was started in FileStore fileStore = Files.getFileStore(Paths.get(System.getProperty("user.dir"))); long size = fileStore.getTotalSpace(); long gb_size = size/(K*K*K); return ImmutableTypedMap.Builder.from(existing).add(DISK_SIZE, Long.valueOf(gb_size)).build(); } catch (IOException e) { // log? return existing; } }
@Override public void sense(final MetricRecorder.Context metricContext) throws SenseException { try { // Determine the file store for the directory the JVM was started in FileStore fileStore = Files.getFileStore(Paths.get(System.getProperty("user.dir"))); long total = fileStore.getTotalSpace(); long free = fileStore.getUsableSpace(); double percent_free = 100.0 * ((double)(total-free)/(double)total); metricContext.record(DISK_USED, percent_free, Unit.PERCENT); } catch (IOException e) { throw new SenseException("Problem reading disk space", e); } }
@Inject public ReadOnlyAdapter(@Named("root") Path root, FileStore fileStore, ReadOnlyDirectoryHandler dirHandler, ReadOnlyFileHandler fileHandler, FileAttributesUtil attrUtil) { this.root = root; this.fileStore = fileStore; this.dirHandler = dirHandler; this.fileHandler = fileHandler; this.attrUtil = attrUtil; }
@Inject public ReadWriteAdapter(@Named("root") Path root, FileStore fileStore, ReadWriteDirectoryHandler dirHandler, ReadWriteFileHandler fileHandler, FileAttributesUtil attrUtil, BitMaskEnumUtil bitMaskUtil) { super(root, fileStore, dirHandler, fileHandler, attrUtil); this.fileHandler = fileHandler; this.attrUtil = attrUtil; this.bitMaskUtil = bitMaskUtil; }
@Provides @PerAdapter protected FileStore provideRootFileStore() { try { return Files.getFileStore(root); } catch (IOException e) { throw new UncheckedIOException(e); } }
private static String getMountPointLinux(FileStore store) { String desc = store.toString(); int index = desc.lastIndexOf(" ("); if (index != -1) { return desc.substring(0, index); } else { return desc; } }
public void testNegativeSpace() throws Exception { FileStore mocked = mock(FileStore.class); when(mocked.getUsableSpace()).thenReturn(-1L); when(mocked.getTotalSpace()).thenReturn(-1L); when(mocked.getUnallocatedSpace()).thenReturn(-1L); assertEquals(-1, mocked.getUsableSpace()); FileStore store = new ESFileStore(mocked); assertEquals(Long.MAX_VALUE, store.getUsableSpace()); assertEquals(Long.MAX_VALUE, store.getTotalSpace()); assertEquals(Long.MAX_VALUE, store.getUnallocatedSpace()); }
@Override public FileStore getFileStore(Path path) throws IOException { if (path.toString().contains(aPathPart)) { return aFileStore; } else { return bFileStore; } }
/** * @return returns available disc space in GB * @throws IOException * */ public static Long getUsableDiscSpaceInGB() throws IOException { Path path = Paths.get(System.getProperty("user.dir")); //Retrieve the mounted file system on which vmidc files are stored FileStore store = Files.getFileStore(path); return store.getUsableSpace() / 1024 / 1024 / 1024; }
@Override public Space get() throws BackgroundException { final Path home = new DefaultHomeFinderService(session).find(); try { final FileStore store = Files.getFileStore(session.toPath(home)); return new Space(store.getTotalSpace() - store.getUnallocatedSpace(), store.getUnallocatedSpace()); } catch(IOException e) { throw new LocalExceptionMappingService().map("Failure to read attributes of {0}", e, home); } }
@Test public void testFileStore() throws Exception { Path path = Paths.get("."); //System.out.println(path.toAbsolutePath()); FileStore fStore = Files.getFileStore(path); //System.out.println(fStore); }
@Override public Iterable<FileStore> getFileStores() { FileStore store; try { store = Files.getFileStore(root); } catch (IOException ioe) { store = null; } return SoleIterable(store); }
static void checkFileStores(FileSystem fs) throws IOException { // sanity check method if (FileUtils.areFileSystemsAccessible()) { System.out.println("\n--- Begin FileStores ---"); for (FileStore store: fs.getFileStores()) { System.out.println(store); } System.out.println("--- EndFileStores ---\n"); } else { System.err.println ("Skipping FileStore check due to file system access failure"); } }
public final FileStore getFileStore() throws IOException { // each ZipFileSystem only has one root (as requested for now) if( exists() ) { return fs.createFileStore( (P) this ); } throw new NoSuchFileException( String.valueOf( path ) ); }
@Override public Iterable<FileStore> getFileStores() { ArrayList<FileStore> list = new ArrayList<>( 1 ); list.add( createFileStore( createPath( new char[]{ '/' } ) ) ); return list; }
private static boolean isPosixFileSystem() { if (supportsPosix == null) { supportsPosix = Boolean.FALSE; FileSystem fileSystem = FileSystems.getDefault(); Iterable<FileStore> fileStores = fileSystem.getFileStores(); for (FileStore fs : fileStores) { supportsPosix = fs.supportsFileAttributeView(PosixFileAttributeView.class); if (supportsPosix) { break; } } } return supportsPosix; }
public static String getFilePath(String derID, String derPath) throws IOException { MCRPath mcrPath = MCRPath.getPath(derID, derPath); Path physicalPath = mcrPath.toPhysicalPath(); for (FileStore fs : mcrPath.getFileSystem().getFileStores()) { if (fs instanceof MCRAbstractFileStore) { Path basePath = ((MCRAbstractFileStore) fs).getBaseDirectory(); if (physicalPath.startsWith(basePath)) { return basePath.relativize(physicalPath).toString(); } } } return physicalPath.toString(); }
public Path toPhysicalPath() throws IOException { if (isAbsolute()) { for (FileStore fs : getFileSystem().getFileStores()) { if (fs instanceof MCRAbstractFileStore) { Path physicalPath = ((MCRAbstractFileStore) fs).getPhysicalPath(this); if (physicalPath != null) { return physicalPath; } } } return null; } throw new IOException("Cannot get real path from relative path."); }
@Override public Iterable<FileStore> getFileStores() { return MCRContentStoreFactory .getAvailableStores() .keySet() .stream() .map(MCRIFSFileSystem::getFileStore)::iterator; }
private static FileStore getFileStore(String id) { try { return MCRFileStore.getInstance(id); } catch (IOException e) { throw new MCRException(e); } }