0

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?

user438383
  • 5,716
  • 8
  • 28
  • 43
user1538798
  • 1,075
  • 3
  • 17
  • 42
  • 2
    Yes, so I don't think `long` is the right type for the return value of your function. Afaik it should be `off_t`. – CherryDT Sep 13 '21 at 08:34
  • 1
    The block size is limited (usually 4096 on Linux). But sure, if you have a file system with more than 18 EB free, you may run into the limits-of-64-bit problem. –  Sep 13 '21 at 09:28
  • @dratenik Does this mean that the maximum disk size that could be accessed by Linux would be 16 TB? – user1538798 Sep 13 '21 at 09:34
  • `EB`, not `TB`, different prefix. If it was TB, people would be running into it already. –  Sep 13 '21 at 09:40
  • @dratenik just to check my understanding... if max stat.f_bsize is 4096, then wouldnt the product stat.f_bsize * f_frsize be only 16TB or is there some missing gaps in my understanding? – user1538798 Sep 13 '21 at 10:01
  • 1
    You are assuming that `fsfilcnt_t` is 32 bits? I'd hope it's 64. Let me check with a small program. It's 32 bits long on 32-bit Linux and 64 on 64b. So the 16 TB limit exists and applies to 32-bit Linux. –  Sep 13 '21 at 10:38
  • @dratenik ok, i get what you mean. Thanks – user1538798 Sep 13 '21 at 10:46

0 Answers0