stat.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * stat definition for NOLIBC
  4. * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
  5. */
  6. /* make sure to include all global symbols */
  7. #include "../nolibc.h"
  8. #ifndef _NOLIBC_SYS_STAT_H
  9. #define _NOLIBC_SYS_STAT_H
  10. #include "../arch.h"
  11. #include "../types.h"
  12. #include "../sys.h"
  13. /*
  14. * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
  15. * int stat(const char *path, struct stat *buf);
  16. * int fstatat(int fd, const char *path, struct stat *buf, int flag);
  17. * int fstat(int fildes, struct stat *buf);
  18. * int lstat(const char *path, struct stat *buf);
  19. */
  20. static __attribute__((unused))
  21. int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
  22. {
  23. #ifdef __NR_statx
  24. return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
  25. #else
  26. return __nolibc_enosys(__func__, fd, path, flags, mask, buf);
  27. #endif
  28. }
  29. static __attribute__((unused))
  30. int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
  31. {
  32. return __sysret(sys_statx(fd, path, flags, mask, buf));
  33. }
  34. static __attribute__((unused))
  35. int fstatat(int fd, const char *path, struct stat *buf, int flag)
  36. {
  37. struct statx statx;
  38. long ret;
  39. ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
  40. if (ret == -1)
  41. return ret;
  42. buf->st_dev = ((statx.stx_dev_minor & 0xff)
  43. | (statx.stx_dev_major << 8)
  44. | ((statx.stx_dev_minor & ~0xff) << 12));
  45. buf->st_ino = statx.stx_ino;
  46. buf->st_mode = statx.stx_mode;
  47. buf->st_nlink = statx.stx_nlink;
  48. buf->st_uid = statx.stx_uid;
  49. buf->st_gid = statx.stx_gid;
  50. buf->st_rdev = ((statx.stx_rdev_minor & 0xff)
  51. | (statx.stx_rdev_major << 8)
  52. | ((statx.stx_rdev_minor & ~0xff) << 12));
  53. buf->st_size = statx.stx_size;
  54. buf->st_blksize = statx.stx_blksize;
  55. buf->st_blocks = statx.stx_blocks;
  56. buf->st_atim.tv_sec = statx.stx_atime.tv_sec;
  57. buf->st_atim.tv_nsec = statx.stx_atime.tv_nsec;
  58. buf->st_mtim.tv_sec = statx.stx_mtime.tv_sec;
  59. buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec;
  60. buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec;
  61. buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec;
  62. return 0;
  63. }
  64. static __attribute__((unused))
  65. int stat(const char *path, struct stat *buf)
  66. {
  67. return fstatat(AT_FDCWD, path, buf, 0);
  68. }
  69. static __attribute__((unused))
  70. int fstat(int fildes, struct stat *buf)
  71. {
  72. return fstatat(fildes, "", buf, AT_EMPTY_PATH);
  73. }
  74. static __attribute__((unused))
  75. int lstat(const char *path, struct stat *buf)
  76. {
  77. return fstatat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
  78. }
  79. #endif /* _NOLIBC_SYS_STAT_H */