0006-Only-call-fstatat-if-defined.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. From e7711de591aed7e3a182880c3c8f5b527c0c67e8 Mon Sep 17 00:00:00 2001
  2. From: Stefan O'Rear <sorear@fastmail.com>
  3. Date: Thu, 3 Sep 2020 03:59:59 -0400
  4. Subject: [PATCH 06/16] Only call fstatat if defined
  5. riscv32 and future architectures lack it.
  6. ---
  7. src/stat/fchmodat.c | 23 ++++++++++++++++++++---
  8. src/stat/fstatat.c | 6 ++++++
  9. src/stdio/tempnam.c | 9 +++++++--
  10. src/stdio/tmpnam.c | 9 +++++++--
  11. src/time/__map_file.c | 19 +++++++++++++++----
  12. 5 files changed, 55 insertions(+), 11 deletions(-)
  13. diff --git a/src/stat/fchmodat.c b/src/stat/fchmodat.c
  14. index 4ee00b0a..857e84e5 100644
  15. --- a/src/stat/fchmodat.c
  16. +++ b/src/stat/fchmodat.c
  17. @@ -1,8 +1,10 @@
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <errno.h>
  21. +#include <stdint.h>
  22. #include "syscall.h"
  23. #include "kstat.h"
  24. +#include "statx.h"
  25. int fchmodat(int fd, const char *path, mode_t mode, int flag)
  26. {
  27. @@ -11,13 +13,22 @@ int fchmodat(int fd, const char *path, mode_t mode, int flag)
  28. if (flag != AT_SYMLINK_NOFOLLOW)
  29. return __syscall_ret(-EINVAL);
  30. - struct kstat st;
  31. int ret, fd2;
  32. char proc[15+3*sizeof(int)];
  33. +#ifdef SYS_fstatat
  34. + struct kstat st;
  35. if ((ret = __syscall(SYS_fstatat, fd, path, &st, flag)))
  36. return __syscall_ret(ret);
  37. - if (S_ISLNK(st.st_mode))
  38. + mode_t get_mode = st.st_mode;
  39. +#else
  40. + struct statx st;
  41. + if ((ret = __syscall(SYS_statx, fd, path, flag, STATX_TYPE, &st)))
  42. + return __syscall_ret(ret);
  43. + mode_t get_mode = st.stx_mode;
  44. +#endif
  45. +
  46. + if (S_ISLNK(get_mode))
  47. return __syscall_ret(-EOPNOTSUPP);
  48. if ((fd2 = __syscall(SYS_openat, fd, path, O_RDONLY|O_PATH|O_NOFOLLOW|O_NOCTTY|O_CLOEXEC)) < 0) {
  49. @@ -27,9 +38,15 @@ int fchmodat(int fd, const char *path, mode_t mode, int flag)
  50. }
  51. __procfdname(proc, fd2);
  52. +#ifdef SYS_fstatat
  53. ret = __syscall(SYS_fstatat, AT_FDCWD, proc, &st, 0);
  54. + get_mode = st.st_mode;
  55. +#else
  56. + ret = __syscall(SYS_statx, AT_FDCWD, proc, 0, STATX_TYPE, &st);
  57. + get_mode = st.stx_mode;
  58. +#endif
  59. if (!ret) {
  60. - if (S_ISLNK(st.st_mode)) ret = -EOPNOTSUPP;
  61. + if (S_ISLNK(get_mode)) ret = -EOPNOTSUPP;
  62. else ret = __syscall(SYS_fchmodat, AT_FDCWD, proc, mode);
  63. }
  64. diff --git a/src/stat/fstatat.c b/src/stat/fstatat.c
  65. index 230a83fc..0486f21a 100644
  66. --- a/src/stat/fstatat.c
  67. +++ b/src/stat/fstatat.c
  68. @@ -45,6 +45,7 @@ static int fstatat_statx(int fd, const char *restrict path, struct stat *restric
  69. return 0;
  70. }
  71. +#ifdef SYS_fstatat
  72. static int fstatat_kstat(int fd, const char *restrict path, struct stat *restrict st, int flag)
  73. {
  74. int ret;
  75. @@ -106,15 +107,20 @@ static int fstatat_kstat(int fd, const char *restrict path, struct stat *restric
  76. return 0;
  77. }
  78. +#endif
  79. int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag)
  80. {
  81. int ret;
  82. +#ifdef SYS_fstatat
  83. if (sizeof((struct kstat){0}.st_atime_sec) < sizeof(time_t)) {
  84. ret = fstatat_statx(fd, path, st, flag);
  85. if (ret!=-ENOSYS) return __syscall_ret(ret);
  86. }
  87. ret = fstatat_kstat(fd, path, st, flag);
  88. +#else
  89. + ret = fstatat_statx(fd, path, st, flag);
  90. +#endif
  91. return __syscall_ret(ret);
  92. }
  93. diff --git a/src/stdio/tempnam.c b/src/stdio/tempnam.c
  94. index 565df6b6..9469923b 100644
  95. --- a/src/stdio/tempnam.c
  96. +++ b/src/stdio/tempnam.c
  97. @@ -5,8 +5,10 @@
  98. #include <limits.h>
  99. #include <string.h>
  100. #include <stdlib.h>
  101. +#include <stdint.h>
  102. #include "syscall.h"
  103. #include "kstat.h"
  104. +#include "statx.h"
  105. #define MAXTRIES 100
  106. @@ -37,11 +39,14 @@ char *tempnam(const char *dir, const char *pfx)
  107. for (try=0; try<MAXTRIES; try++) {
  108. __randname(s+l-6);
  109. -#ifdef SYS_lstat
  110. +#if defined(SYS_lstat)
  111. r = __syscall(SYS_lstat, s, &(struct kstat){0});
  112. -#else
  113. +#elif defined(SYS_fstatat)
  114. r = __syscall(SYS_fstatat, AT_FDCWD, s,
  115. &(struct kstat){0}, AT_SYMLINK_NOFOLLOW);
  116. +#else
  117. + r = __syscall(SYS_statx, AT_FDCWD, s, AT_SYMLINK_NOFOLLOW, 0,
  118. + &(struct statx){0});
  119. #endif
  120. if (r == -ENOENT) return strdup(s);
  121. }
  122. diff --git a/src/stdio/tmpnam.c b/src/stdio/tmpnam.c
  123. index d667a836..9576241b 100644
  124. --- a/src/stdio/tmpnam.c
  125. +++ b/src/stdio/tmpnam.c
  126. @@ -4,8 +4,10 @@
  127. #include <sys/stat.h>
  128. #include <string.h>
  129. #include <stdlib.h>
  130. +#include <stdint.h>
  131. #include "syscall.h"
  132. #include "kstat.h"
  133. +#include "statx.h"
  134. #define MAXTRIES 100
  135. @@ -17,11 +19,14 @@ char *tmpnam(char *buf)
  136. int r;
  137. for (try=0; try<MAXTRIES; try++) {
  138. __randname(s+12);
  139. -#ifdef SYS_lstat
  140. +#if defined(SYS_lstat)
  141. r = __syscall(SYS_lstat, s, &(struct kstat){0});
  142. -#else
  143. +#elif defined(SYS_fstatat)
  144. r = __syscall(SYS_fstatat, AT_FDCWD, s,
  145. &(struct kstat){0}, AT_SYMLINK_NOFOLLOW);
  146. +#else
  147. + r = __syscall(SYS_statx, AT_FDCWD, s, AT_SYMLINK_NOFOLLOW, 0,
  148. + &(struct statx){0});
  149. #endif
  150. if (r == -ENOENT) return strcpy(buf ? buf : internal, s);
  151. }
  152. diff --git a/src/time/__map_file.c b/src/time/__map_file.c
  153. index d3cefa82..542c8ce4 100644
  154. --- a/src/time/__map_file.c
  155. +++ b/src/time/__map_file.c
  156. @@ -1,18 +1,29 @@
  157. +#define _BSD_SOURCE
  158. #include <sys/mman.h>
  159. #include <fcntl.h>
  160. #include <sys/stat.h>
  161. +#include <stdint.h>
  162. #include "syscall.h"
  163. #include "kstat.h"
  164. +#include "statx.h"
  165. const char unsigned *__map_file(const char *pathname, size_t *size)
  166. {
  167. - struct kstat st;
  168. const unsigned char *map = MAP_FAILED;
  169. int fd = sys_open(pathname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
  170. if (fd < 0) return 0;
  171. - if (!syscall(SYS_fstat, fd, &st)) {
  172. - map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  173. - *size = st.st_size;
  174. +#ifdef SYS_fstat
  175. + struct kstat st;
  176. + int r = syscall(SYS_fstat, fd, &st);
  177. + size_t fsize = st.st_size;
  178. +#else
  179. + struct statx st;
  180. + int r = syscall(SYS_statx, fd, "", AT_EMPTY_PATH, STATX_SIZE, &st);
  181. + size_t fsize = st.stx_size;
  182. +#endif
  183. + if (!r) {
  184. + map = __mmap(0, fsize, PROT_READ, MAP_SHARED, fd, 0);
  185. + *size = fsize;
  186. }
  187. __syscall(SYS_close, fd);
  188. return map == MAP_FAILED ? 0 : map;
  189. --
  190. 2.29.2