3

I need to get the free available disk space for all disks in system, or all partitions, I don't mind that. (I dont have to use Sigar, but I am using it already on the project for some other processes, so I can use it for this as well) I am using Sigar API and got this

public double getFreeHdd() throws SigarException{
        FileSystemUsage f= sigar.getFileSystemUsage("/");
        return ( f.getAvail()); 
    }

But this only gives me the system partition (root), how can i get a list of all partition and loop them to get their free space? I tried this

FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] roots = fsv.getRoots();
        for (int i = 0; i < roots.length; i++) {
            System.out.println("Root: " + roots[i]);
        }

But it only returns the root dir

Root: /

Thanks

Edit it seems that I could use FileSystem[] fslist = sigar.getFileSystemList();
But the results i am getting do not match the ones i get from the terminal. On the other hand on this system I am working on, i have 3 disks with a total 12 partitions, so i might be loosing something there. Will try it on some other system in case i can make something useful out of the results.

Skaros Ilias
  • 1,008
  • 12
  • 40
  • Windows has `fsutil fsinfo drives` that gives `Drives: C:\ E:\ F:\ G:\ ` and `fsutil fsinfo drivetype G:` that gives `G: - CD-ROM Drive` – Marichyasana Feb 17 '16 at 12:53
  • @Marichyasana I would prefer to use something system independent. for now this is going to be executed in Linux, but still dont know if a Win box might be used in the future. Thats why I went for Sigar in the first place – Skaros Ilias Feb 17 '16 at 14:05

2 Answers2

2

We use SIGAR extensively for cross-platform monitoring. This is the code we use to get the file system list:

/**
* @return a list of directory path names of file systems that are local or network - not removable media
*/
public static Set<String> getLocalOrNetworkFileSystemDirectoryNames() {
  Set<String> ret = new HashSet<String>();
  try {
    FileSystem[] fileSystemList = getSigarProxy().getFileSystemList();

    for (FileSystem fs : fileSystemList) {
      if ((fs.getType() == FileSystem.TYPE_LOCAL_DISK) || (fs.getType() == FileSystem.TYPE_NETWORK)) {
        ret.add(fs.getDirName());
      }
    }
  }
  catch (SigarException e) {
    // log or rethrow as appropriate
  }

  return ret;
}

You can then use that as the input to other SIGAR methods:

FileSystemUsage usageStats = getSigarProxy().getFileSystemUsage(fileSystemDirectoryPath);

The getSigarProxy() is just a convenience base method:

// The Humidor handles thread safety for a single instance of a Sigar object
static final private SigarProxy sigarProxy = Humidor.getInstance().getSigar();

static final protected SigarProxy getSigarProxy() {
  return sigarProxy;
}
Martin Serrano
  • 3,727
  • 1
  • 35
  • 48
  • Thanks for your hints, but for some reason it is not reading all my partitions. It was missing some, so i went with marcospereira's solution – Skaros Ilias Feb 18 '16 at 20:37
  • weird... do the other sigar methods work with paths for partitions that weren't found by `getFileSystemList`? – Martin Serrano Feb 18 '16 at 21:28
  • no, dont have the time to see what might be the problem, especially since I got it working with the other method. Thanks again – Skaros Ilias Feb 19 '16 at 10:00
0

You can use java.nio.file.FileSystems to get a list of java.nio.file.FileStorages and then see the usable/available space. Per instance (assuming that you are using Java 7+):

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.function.Consumer;

public static void main(String[] args) {
    FileSystem fs = FileSystems.getDefault();
    fs.getFileStores().forEach(new Consumer<FileStore>() {
        @Override
        public void accept(FileStore store) {
            try {
                System.out.println(store.getTotalSpace());
                System.out.println(store.getUsableSpace());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

Also, keep in mind that FileStore.getUsableSpace() returns the size in bytes. See the docs for more information.

marcospereira
  • 12,045
  • 3
  • 46
  • 52