user1538798
user1538798

Reputation: 1135

Remaining disk space in linux and the limits of long

I understand that the disk space in linux could be programmatically retrieved using:

// header for statvfs
#include <sys/statvfs.h>

long GetAvailableSpace(const char* path)
{
  struct statvfs stat;

  if (statvfs(path, &stat) != 0) {
    // error happens, just quits here
    return -1;
  }

  // the available size is f_bsize * f_bavail
  return stat.f_bsize * stat.f_bavail;
}

int main(int argc, const char *argv[])
{
   // assuming input is the directory which one is interested
   printf(" The remaining size is %ld \n", GetAvailableSpace(argv[1]));
}

However on checking the file structure for struct statvfs, they are

struct statvfs {
    unsigned long  f_bsize;    /* filesystem block size */
    unsigned long  f_frsize;   /* fragment size */
    fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
    fsblkcnt_t     f_bfree;    /* # free blocks */
    fsblkcnt_t     f_bavail;   /* # free blocks for unprivileged users */
    fsfilcnt_t     f_files;    /* # inodes */
    fsfilcnt_t     f_ffree;    /* # free inodes */
    fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */
    unsigned long  f_fsid;     /* filesystem ID */
    unsigned long  f_flag;     /* mount flags */
    unsigned long  f_namemax;  /* maximum filename length */
};

Theoretically the size of stat.f_bavail, could be also an unsigned long (I suspect). Wouldn't the multiplication of two unsigned long be much more than a long return type would hold?

Upvotes: 0

Views: 300

Answers (0)

Related Questions