I would like to figure out the available free space on a USB flash drive in a C program in Linux. I have earlier used system("df -h /dev/sda1")
to figure it out but using system command in a Linux C application is causing some timing issues. Hence need to know how to determine available free space using any other kind of system call/library function.
Linux – Determine free space available on a USB flash drive in C (linux)
clinuxsystem-callsusbusb-drive
Best Answer
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of
df
(which also btw only operates on mounted filesystems):Note also that
statvfs()
takesconst char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g./dev/sda7
will return usage of/dev
filesystem ( because it is in fact one of virtual filesystems ), and not thesda7
partition of a device.Note that I am using
f_frsize
here, which is equivalent tof_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for detailsIf your C library (usually glibc but on an embedded platform might be something else) implements statvfs(3) in a way that's too slow for you, then you should probably use the underlying statfs(2) or fstatfs(2) system calls directly instead.