Statvfs () not working correctly C ++ linux

I am trying to get available disk space. However, the size that I return does not change when I write more files to disk. What am I doing wrong?

I confirmed that I am writing /dev/sda1by running df -hand seeing that the available space is really decreasing when I write files to disk.

double size; //size of aval disk space
struct statvfs buf;

if((statvfs("/dev/sda1", &buf)) < 0)
{
   printf("Failed to get statvfs\n");
}
else
{
    size = (((double)buf.f_bavail * buf.f_bsize) / 1048576);
    printf("You have: %.0f MB free\n", size);
}
+4
source share
1 answer

You need to call statvfs along the path that is on the mounted file system that you want to check, for example

statvfs("/", &buf)

Any path in the file system will be executed, for example. "/home/user/foo/tmp/file.txt"

statvfs on/dev/sda1, , /dev.

+2

All Articles