Java Code Examples for java.nio.file.FileStore#getUsableSpace()

The following examples show how to use java.nio.file.FileStore#getUsableSpace() . 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 File: FileBlobStore.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isStorageAvailable() {
  try {
    FileStore fileStore = Files.getFileStore(contentDir);
    long usableSpace = fileStore.getUsableSpace();
    boolean readOnly = fileStore.isReadOnly();
    boolean result = !readOnly && usableSpace > 0;
    if (!result) {
      log.warn("File blob store '{}' is not writable. Read only: {}. Usable space: {}",
          getBlobStoreConfiguration().getName(), readOnly, usableSpace);
    }
    return result;
  }
  catch (IOException e) {
    log.warn("File blob store '{}' is not writable.", getBlobStoreConfiguration().getName(), e);
    return false;
  }
}
 
Example 2
Source File: FileSingleStreamSpillerFactory.java    From presto with Apache License 2.0 5 votes vote down vote up
private boolean hasEnoughDiskSpace(Path path)
{
    try {
        FileStore fileStore = getFileStore(path);
        return fileStore.getUsableSpace() > fileStore.getTotalSpace() * (1.0 - maxUsedSpaceThreshold);
    }
    catch (IOException e) {
        throw new PrestoException(OUT_OF_SPILL_SPACE, "Cannot determine free space for spill", e);
    }
}
 
Example 3
Source File: ConsensusTracker.java    From nyzoVerifier with 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 4
Source File: DiskUsageSensor.java    From swage with Apache License 2.0 5 votes vote down vote up
@Override
public void sense(final MetricContext 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);
    }
}
 
Example 5
Source File: IndexFetcher.java    From lucene-solr with 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 6
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public long getFreeSpace() {
    long freeSpace = 0;
    try {
        FileStore fileStore = Files.getFileStore(Paths.get("."));
        freeSpace = fileStore.getUsableSpace();
    } catch (IOException ioEx) {
        logger.log(Level.WARNING, "Cannot calculate free/total disk space", ioEx);
    }

    return freeSpace;
}
 
Example 7
Source File: FileStoreMonitor.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void tick() {
   synchronized (monitorLock) {
      boolean over = false;
      long usableSpace = 0;
      long totalSpace = 0;

      for (FileStore store : stores) {
         try {
            usableSpace = store.getUsableSpace();
            totalSpace = getTotalSpace(store);
            over = calculateUsage(usableSpace, totalSpace) > maxUsage;
            if (over) {
               break;
            }
         } catch (IOException ioe) {
            ioCriticalErrorListener.onIOException(ioe, "IO Error while calculating disk usage", null);
         } catch (Exception e) {
            logger.warn(e.getMessage(), e);
         }
      }

      for (Callback callback : callbackList) {
         callback.tick(usableSpace, totalSpace);

         if (over) {
            callback.over(usableSpace, totalSpace);
         } else {
            callback.under(usableSpace, totalSpace);
         }
      }
   }
}
 
Example 8
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public long getFreeSpace() {
    long freeSpace = 0;
    try {
        FileStore fileStore = Files.getFileStore(Paths.get("."));
        freeSpace = fileStore.getUsableSpace();
    } catch (IOException ioEx) {
        logger.log(Level.WARNING, "Cannot calculate free/total disk space", ioEx);
    }

    return freeSpace;
}
 
Example 9
Source File: TestFileSystem.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Simple test to check {@link FileStore} support.
 *
 * @throws IOException
 */
@Test
public void getFileStores() throws IOException {
  Path pathToTest = Paths.get(clusterUri);

  Iterable<FileStore> fileStores = pathToTest.getFileSystem().getFileStores();
  for (FileStore store : fileStores) {
    store.getUsableSpace();
    assertNotNull(store.toString());
  }
}
 
Example 10
Source File: LocalFileSystem.java    From simple-nfs with Apache License 2.0 5 votes vote down vote up
@Override
public FsStat getFsStat() throws IOException {
    FileStore store = Files.getFileStore(_root);
    long total = store.getTotalSpace();
    long free = store.getUsableSpace();
    return new FsStat(total, Long.MAX_VALUE, total-free, pathToInode.size());
}
 
Example 11
Source File: FileSystemConfiguration.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public long freeDiskBytes() throws IOException {
    Iterable<FileStore> fileStores = localFileSystem.getFileStores();
    long totalUsableSpace = 0l;
    for(FileStore fs:fileStores){
        totalUsableSpace+=fs.getUsableSpace();
    }
    return totalUsableSpace;
}
 
Example 12
Source File: FileStoreBasics.java    From java-1-class-demos with MIT License 4 votes vote down vote up
public static String freeSpaceFormatted(FileStore store) throws Exception {
	long mb = store.getUsableSpace() / (1000*1000); // 10^6 converts to megabytes.
	return mb + " mb";
}
 
Example 13
Source File: GetAvailableSpace.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void get_available_space_nio () throws IOException {

	FileStore store = Files.getFileStore(source);

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