I'm currently working on program, which must display information about mounted flash drive. I want to display full space, free space, file system type and volume name. But problem is that, i can't find any API through which i can get volume name(volume label). Is there any api to do this?
p.s. full space, free space and file system type i'm getting via statfs
function
Assuming that you work on a recent desktop-like distribution (Fedora, Ubuntu, etc.), you have HAL daemon running and a D-Bus session.
Within
org.freedesktop.UDisks
namespace you can find the object that represents this drive (sayorg/freedekstop/UDisks/devices/sdb/
. It implementsorg.freedesktop.UDisks.interface
. This interface has all the properties that you can dream of, including UUID (IdUuid
), FAT label (IdLabel
), all the details about filesystem, SMART status (if the drive supports that) etc. etc.How to use D-Bus API in C is a topic for another question. I assume that's been already discussed in detail -- just search [dbus] and [c] tags.
To call the methods:
and the methods:
Flash drives are generally FAT32, which means the "name" that you're looking for is probably the FAT drive label. The most common linux command to retrieve that information is
mlabel
from the mtools package.The command looks like this:
This program works by reading the raw FAT header of the filesystem and retrieving the label from that data. You can look at the source code for the applciation to see how you can replicate the parsing of FAT data in your own application... or you can simply execute run the
mlabel
binary and read the result into your program. The latter sounds simpler to me.