statvfs.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <sys/statvfs.h>
  2. #include <sys/statfs.h>
  3. #include "syscall.h"
  4. static int __statfs(const char *path, struct statfs *buf)
  5. {
  6. *buf = (struct statfs){0};
  7. #ifdef SYS_statfs64
  8. return syscall(SYS_statfs64, path, sizeof *buf, buf);
  9. #else
  10. return syscall(SYS_statfs, path, buf);
  11. #endif
  12. }
  13. static int __fstatfs(int fd, struct statfs *buf)
  14. {
  15. *buf = (struct statfs){0};
  16. #ifdef SYS_fstatfs64
  17. return syscall(SYS_fstatfs64, fd, sizeof *buf, buf);
  18. #else
  19. return syscall(SYS_fstatfs, fd, buf);
  20. #endif
  21. }
  22. weak_alias(__statfs, statfs);
  23. weak_alias(__fstatfs, fstatfs);
  24. static void fixup(struct statvfs *out, const struct statfs *in)
  25. {
  26. *out = (struct statvfs){0};
  27. out->f_bsize = in->f_bsize;
  28. out->f_frsize = in->f_frsize ? in->f_frsize : in->f_bsize;
  29. out->f_blocks = in->f_blocks;
  30. out->f_bfree = in->f_bfree;
  31. out->f_bavail = in->f_bavail;
  32. out->f_files = in->f_files;
  33. out->f_ffree = in->f_ffree;
  34. out->f_favail = in->f_ffree;
  35. out->f_fsid = in->f_fsid.__val[0];
  36. out->f_flag = in->f_flags;
  37. out->f_namemax = in->f_namelen;
  38. out->f_type = in->f_type;
  39. }
  40. int statvfs(const char *restrict path, struct statvfs *restrict buf)
  41. {
  42. struct statfs kbuf;
  43. if (__statfs(path, &kbuf)<0) return -1;
  44. fixup(buf, &kbuf);
  45. return 0;
  46. }
  47. int fstatvfs(int fd, struct statvfs *buf)
  48. {
  49. struct statfs kbuf;
  50. if (__fstatfs(fd, &kbuf)<0) return -1;
  51. fixup(buf, &kbuf);
  52. return 0;
  53. }