Hi Kenb4
Thanks for the confirmation and clarifications. You helped isolate where my problem might be.
It turned out that my conundrum came from my using the wrong data type specifier in printf and also my alternate logging tools not handling the long long values properly. Once I sorted out my bad data logging I could see the simplest naked call to statvfs was actually working OK just as you said and I didn't need a specific filesystem instance after all.
Here's what I used in case it helps someone else... for a USB drive pass in "/usb/" as the ld parameter.
struct statvfs vfsbuf;
void Dump_statvfs(const char *ld) {
int ret;
ret = statvfs(ld, &vfsbuf); // This naked call to statvfs works OK
if (ret != 0) printf("statvfs ERROR: %d\n", ret);
printf("f_bsize: %8.8lu\n", vfsbuf.f_bsize); // unsigned long f_bsize; ///< Filesystem block size
printf("f_frsize:%8.8lu\n", vfsbuf.f_frsize); // unsigned long f_frsize; ///< Fragment size (block size)
printf("f_blocks:%16.16llu\n", vfsbuf.f_blocks); // fsblkcnt_t f_blocks; ///< Number of blocks
printf("f_bfree: %16.16llu\n", vfsbuf.f_bfree); // fsblkcnt_t f_bfree; ///< Number of free blocks
printf("f_bavail:%16.16llu\n", vfsbuf.f_bavail); // fsblkcnt_t f_bavail; ///< Number of free blocks for unprivileged users
printf("f_fsid: %8.8lu\n", vfsbuf.f_fsid); // unsigned long f_fsid; ///< Filesystem ID
printf("f_namemax:%8.8lu\n", vfsbuf.f_namemax); // unsigned long f_namemax; ///< Maximum filename length
printf("fsTotalSize:%llu fsUsedSize:%llu fsFreeBytes:%llu\n", totalSize(ld), usedSize(ld), bytesFree(ld));
}
//******************************************************
// Total byte capacity of the volume.
//******************************************************
uint64_t totalSize(const char *ld) {
statvfs(ld, &vfsbuf);
return (uint64_t)(vfsbuf.f_blocks * vfsbuf.f_bsize);
}
//********************************************************
// How much space in bytes currently used on the volume. *
//********************************************************
uint64_t usedSize(const char *ld) {
statvfs(ld, &vfsbuf);
return (uint64_t)((vfsbuf.f_blocks * vfsbuf.f_bsize) - (vfsbuf.f_bfree * vfsbuf.f_bsize));
}
//******************************************************
// How much space is left on a volume (Logical Drive). *
//******************************************************
uint64_t bytesFree(const char *ld) {
statvfs(ld, &vfsbuf);
return (uint64_t)(vfsbuf.f_bfree * vfsbuf.f_bsize);
}