Java Code Examples for java.nio.file.Files#getFileStore()

The following examples show how to use java.nio.file.Files#getFileStore() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jsr203-hadoop   File: TestFileStore.java    License: Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 2
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
        }
    }
}
 
Example 3
Source Project: jsr203-hadoop   File: TestFileSystem.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source Project: jsr203-hadoop   File: TestFileStore.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testHadoopFileStoreAttributeView() throws 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);

  Assert.assertNull(
      store1.getFileStoreAttributeView(FakeFileStoreAttributeView.class));
}
 
Example 5
Source Project: LagMonitor   File: NativeManager.java    License: MIT License 5 votes vote down vote up
public long getTotalSpace() {
    long totalSpace = 0;
    try {
        FileStore fileStore = Files.getFileStore(Paths.get("."));
        totalSpace = fileStore.getTotalSpace();
    } catch (IOException ioEx) {
        logger.log(Level.WARNING, "Cannot calculate free disk space", ioEx);
    }

    return totalSpace;
}
 
Example 6
private static String getContentPath(String derivateId, String path) {
    try {
        MCRPath mcrPath = MCRPath.getPath(derivateId, path);
        MCRAbstractFileStore fileStore = (MCRAbstractFileStore) Files.getFileStore(mcrPath);
        java.nio.file.Path absolutePath = fileStore.getPhysicalPath(mcrPath);
        java.nio.file.Path relativePath = fileStore.getBaseDirectory().relativize(absolutePath);
        LogManager.getLogger().info("{} -> {} -> {}", mcrPath, absolutePath, relativePath);
        return relativePath.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source Project: lucene-solr   File: IndexFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private static Long getUsableSpace(String dir) {
  try {
    File file = new File(dir);
    if (!file.exists()) {
      file = file.getParentFile();
      if (!file.exists()) {//this is not a disk directory . so just pretend that there is enough space
        return Long.MAX_VALUE;
      }
    }
    FileStore fileStore = Files.getFileStore(file.toPath());
    return fileStore.getUsableSpace();
  } catch (IOException e) {
    throw new SolrException(ErrorCode.SERVER_ERROR, "Could not free disk space", e);
  }
}
 
Example 8
Source Project: datacollector   File: RuntimeInfo.java    License: Apache License 2.0 5 votes vote down vote up
private String sizeOfDir(String directory) {
  try {
    Path path = Paths.get(directory);
    FileStore store = Files.getFileStore(path.getRoot());
    return Utils.format("(total {}, unallocated {}, root {})", FileUtils.byteCountToDisplaySize(store.getTotalSpace()), FileUtils.byteCountToDisplaySize(store.getUnallocatedSpace()), path.getRoot());
  } catch (Exception e) {
    return "";
  }
}
 
Example 9
Source Project: nyzoVerifier   File: ConsensusTracker.java    License: The Unlicense 5 votes vote down vote up
private static long getUsableSpace() {

        // Ensure that the directory exists.
        rootDirectory.mkdirs();

        // Get the usable space.
        long freeSpace = 0L;
        Path path = Paths.get(rootDirectory.getAbsolutePath());
        try {
            FileStore store = Files.getFileStore(path);
            freeSpace = store.getUsableSpace();
        } catch (Exception ignored) { }

        return freeSpace;
    }
 
Example 10
public ZipFileStoreAttributes(ZipFileStore fileStore)
    throws IOException
{
    Path path = FileSystems.getDefault().getPath(fileStore.name());
    this.size = Files.size(path);
    this.fstore = Files.getFileStore(path);
}
 
Example 11
@Override
public Iterable<FileStore> getFileStores() {
    FileStore store;
    try {
        store = Files.getFileStore(root);
    } catch (IOException ioe) {
        store = null;
    }
    return SoleIterable(store);
}
 
Example 12
public ZipFileStoreAttributes(ZipFileStore fileStore)
    throws IOException
{
    Path path = FileSystems.getDefault().getPath(fileStore.name());
    this.size = Files.size(path);
    this.fstore = Files.getFileStore(path);
}
 
Example 13
public ZipFileStoreAttributes(ZipFileStore fileStore)
    throws IOException
{
    Path path = FileSystems.getDefault().getPath(fileStore.name());
    this.size = Files.size(path);
    this.fstore = Files.getFileStore(path);
}
 
Example 14
@Override
public Iterable<FileStore> getFileStores() {
    FileStore store;
    try {
        store = Files.getFileStore(root);
    } catch (IOException ioe) {
        store = null;
    }
    return SoleIterable(store);
}
 
Example 15
@Override
public FileStore getFileStore(Path file) throws IOException {
    triggerEx(file, "getFileStore");
    return Files.getFileStore(unwrap(file));
}
 
Example 16
@Override
public FileStore getFileStore(Path file) throws IOException {
    triggerEx(file, "getFileStore");
    return Files.getFileStore(unwrap(file));
}
 
Example 17
@Override
public FileStore getFileStore(Path file) throws IOException {
    triggerEx(file, "getFileStore");
    return Files.getFileStore(unwrap(file));
}
 
Example 18
@Override
public FileStore getFileStore(Path file) throws IOException {
    triggerEx(file, "getFileStore");
    return Files.getFileStore(unwrap(file));
}
 
Example 19
Source Project: crate   File: Environment.java    License: Apache License 2.0 4 votes vote down vote up
public static FileStore getFileStore(final Path path) throws IOException {
    return new ESFileStore(Files.getFileStore(path));
}
 
Example 20
@Test
public void get_available_space_nio () throws IOException {

	FileStore store = Files.getFileStore(source);

	long availableSpace = store.getUsableSpace() / 1024;
	
	assertTrue(availableSpace > 0);
}