FileStore... getRootPath() ?

John Hendrikx hjohn at xs4all.nl
Wed Jun 30 10:51:22 PDT 2010


Rémi Forax wrote:
> Le 30/06/2010 18:00, John Hendrikx a écrit :
>> Dear list,
>>
>> I have a use case for NIO2, where I wish to present the user with a 
>> list of FileStore's (or roots) from which to select one depending on 
>> available space on the store.  A loop as follows gives me the 
>> relevant information in Java at the moment:
>>
>> for(FileStore store : FileSystems.getDefault().getFileStores()) {
>>  System.out.println(store);
>> }
>>
>> This will print a list like this:
>>
>> (C:)
>> data (D:)
>> New Volume (F:)
>> etc...
>>
>> or on Linux:
>>
>> / (/dev/hda2)
>> /lib/init/rw (tmpfs)
>> /proc (proc)
>> /sys (sysfs)
>> /proc/bus/usb (procbususb)
>> /dev (udev)
>> /dev/shm (tmpfs)
>> /dev/pts (devpts)
>> /sys/fs/fuse/connections (fusectl)
>> /data (/dev/md0)
>> /temp (/dev/mapper/temp)
>> /raid1 (/dev/mapper/raid1)
>> /raid2 (/dev/mapper/raid2)
>>
>> It is fairly trivial to filter this list to only the stores useful to 
>> the user (by discarding items with "non-sensical" 
>> useableSpace()/totalSpace() values, ie. both being zero).
>>
>> However, it turns out that after finding this treasure of 
>> information, I cannot make use of it without parsing toString() of 
>> FileStore, as there is no method to go from a FileStore to a (root) 
>> Path.
>>
>> Is this an oversight or is there perhaps an even more elegant way to 
>> find this information?
>>
>> Note that even on Linux, with just "one" root (/), the added choice 
>> for other volumes (which have different free space values) is 
>> invaluable for the user.  In other words, I'd like to present the 
>> user with the choice of for example: /, /data, /temp, /raid1 and /raid2.
>>
>> Thanks,
>> --John
>>
>
>
> You can use fileSystem.getRootDirectories() and use getFileStore()
> on the resulting paths.
>
> Rémi
>
This unfortunately only returns "/" on Linux (b99), which is correct I 
suppose for a function with such a name.  It still does not allow a list 
of distinct volumes that are available (like df -h would show).  ie:

Filesystem            Size  Used Avail Use% Mounted on
/dev/hda2              19G   13G  5.1G  72% /
tmpfs                 2.0G   12K  2.0G   1% /lib/init/rw
udev                   10M  396K  9.7M   4% /dev
tmpfs                 2.0G     0  2.0G   0% /dev/shm
/dev/md0              9.2G  902M  8.3G  10% /data
/dev/mapper/temp      345G  344G  532M 100% /temp
/dev/mapper/raid1     3.7T  2.5T  1.2T  68% /raid1
/dev/mapper/raid2     3.7T  2.2T  1.5T  60% /raid2

In other words, I'd present the user with only "/" which has 5.1 GB 
free, not quite an accurate state of affairs for the above system.

--John


More information about the nio-dev mailing list