statx.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #define _GNU_SOURCE
  2. #include <sys/stat.h>
  3. #include <string.h>
  4. #include <syscall.h>
  5. #include <sys/sysmacros.h>
  6. #include <errno.h>
  7. int statx(int dirfd, const char *restrict path, int flags, unsigned mask, struct statx *restrict stx)
  8. {
  9. int ret = __syscall(SYS_statx, dirfd, path, flags, mask, stx);
  10. #ifndef SYS_fstatat
  11. return __syscall_ret(ret);
  12. #endif
  13. if (ret != -ENOSYS) return __syscall_ret(ret);
  14. struct stat st;
  15. ret = fstatat(dirfd, path, &st, flags);
  16. if (ret) return ret;
  17. *stx = (struct statx){0};
  18. stx->stx_dev_major = major(st.st_dev);
  19. stx->stx_dev_minor = minor(st.st_dev);
  20. stx->stx_rdev_major = major(st.st_rdev);
  21. stx->stx_rdev_minor = minor(st.st_rdev);
  22. stx->stx_ino = st.st_ino;
  23. stx->stx_mode = st.st_mode;
  24. stx->stx_nlink = st.st_nlink;
  25. stx->stx_uid = st.st_uid;
  26. stx->stx_gid = st.st_gid;
  27. stx->stx_size = st.st_size;
  28. stx->stx_blksize = st.st_blksize;
  29. stx->stx_blocks = st.st_blocks;
  30. stx->stx_atime.tv_sec = st.st_atim.tv_sec;
  31. stx->stx_atime.tv_nsec = st.st_atim.tv_nsec;
  32. stx->stx_mtime.tv_sec = st.st_mtim.tv_sec;
  33. stx->stx_mtime.tv_nsec = st.st_mtim.tv_nsec;
  34. stx->stx_ctime.tv_sec = st.st_ctim.tv_sec;
  35. stx->stx_ctime.tv_nsec = st.st_ctim.tv_nsec;
  36. stx->stx_mask = STATX_BASIC_STATS;
  37. return 0;
  38. }