You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
6.7 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fs_fstat.hpp"
  16. #include "fs_fstatat.hpp"
  17. #include "fs_futimesat.hpp"
  18. #include "fs_lutimens.hpp"
  19. #include "fs_stat_utils.hpp"
  20. #include <string>
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #ifndef UTIME_NOW
  25. # define UTIME_NOW ((1l << 30) - 1l)
  26. #endif
  27. #ifndef UTIME_OMIT
  28. # define UTIME_OMIT ((1l << 30) - 2l)
  29. #endif
  30. namespace l
  31. {
  32. static
  33. inline
  34. bool
  35. can_call_lutimes(const int dirfd_,
  36. const std::string &path_,
  37. const int flags_)
  38. {
  39. return ((flags_ == AT_SYMLINK_NOFOLLOW) &&
  40. ((dirfd_ == AT_FDCWD) ||
  41. (path_[0] == '/')));
  42. }
  43. static
  44. inline
  45. bool
  46. should_ignore(const struct timespec ts_[2])
  47. {
  48. return ((ts_ != NULL) &&
  49. (ts_[0].tv_nsec == UTIME_OMIT) &&
  50. (ts_[1].tv_nsec == UTIME_OMIT));
  51. }
  52. static
  53. inline
  54. bool
  55. should_be_set_to_now(const struct timespec ts_[2])
  56. {
  57. return ((ts_ == NULL) ||
  58. ((ts_[0].tv_nsec == UTIME_NOW) &&
  59. (ts_[1].tv_nsec == UTIME_NOW)));
  60. }
  61. static
  62. inline
  63. bool
  64. timespec_invalid(const struct timespec &ts_)
  65. {
  66. return (((ts_.tv_nsec < 0) ||
  67. (ts_.tv_nsec > 999999999)) &&
  68. ((ts_.tv_nsec != UTIME_NOW) &&
  69. (ts_.tv_nsec != UTIME_OMIT)));
  70. }
  71. static
  72. inline
  73. bool
  74. timespec_invalid(const struct timespec ts_[2])
  75. {
  76. return ((ts_ != NULL) &&
  77. (l::timespec_invalid(ts_[0]) ||
  78. l::timespec_invalid(ts_[1])));
  79. }
  80. static
  81. inline
  82. bool
  83. flags_invalid(const int flags_)
  84. {
  85. return ((flags_ & ~AT_SYMLINK_NOFOLLOW) != 0);
  86. }
  87. static
  88. inline
  89. bool
  90. any_timespec_is_utime_omit(const struct timespec ts_[2])
  91. {
  92. return ((ts_[0].tv_nsec == UTIME_OMIT) ||
  93. (ts_[1].tv_nsec == UTIME_OMIT));
  94. }
  95. static
  96. inline
  97. bool
  98. any_timespec_is_utime_now(const struct timespec ts_[2])
  99. {
  100. return ((ts_[0].tv_nsec == UTIME_NOW) ||
  101. (ts_[1].tv_nsec == UTIME_NOW));
  102. }
  103. static
  104. inline
  105. int
  106. set_utime_omit_to_current_value(const int dirfd_,
  107. const std::string &path_,
  108. const struct timespec ts_[2],
  109. struct timeval tv_[2],
  110. const int flags_)
  111. {
  112. int rv;
  113. struct stat st;
  114. timespec *atime;
  115. timespec *mtime;
  116. if(!l::any_timespec_is_utime_omit(ts_))
  117. return 0;
  118. rv = fs::fstatat(dirfd_,path_,&st,flags_);
  119. if(rv == -1)
  120. return -1;
  121. atime = fs::stat_atime(st);
  122. mtime = fs::stat_mtime(st);
  123. if(ts_[0].tv_nsec == UTIME_OMIT)
  124. TIMESPEC_TO_TIMEVAL(&tv[0],atime);
  125. if(ts_[1].tv_nsec == UTIME_OMIT)
  126. TIMESPEC_TO_TIMEVAL(&tv[1],mtime);
  127. return 0;
  128. }
  129. static
  130. inline
  131. int
  132. set_utime_omit_to_current_value(const int fd_,
  133. const struct timespec ts_[2],
  134. struct timeval tv_[2])
  135. {
  136. int rv;
  137. struct stat st;
  138. timespec *atime;
  139. timespec *mtime;
  140. if(!l::any_timespec_is_utime_omit(ts_))
  141. return 0;
  142. rv = fs::fstat(fd_,&st);
  143. if(rv == -1)
  144. return -1;
  145. atime = fs::stat_atime(st);
  146. mtime = fs::stat_mtime(st);
  147. if(ts_[0].tv_nsec == UTIME_OMIT)
  148. TIMESPEC_TO_TIMEVAL(&tv_[0],atime);
  149. if(ts_[1].tv_nsec == UTIME_OMIT)
  150. TIMESPEC_TO_TIMEVAL(&tv_[1],mtime);
  151. return 0;
  152. }
  153. static
  154. inline
  155. int
  156. set_utime_now_to_now(const struct timespec ts_[2],
  157. struct timeval tv_[2])
  158. {
  159. int rv;
  160. struct timeval now;
  161. if(l::any_timespec_is_utime_now(ts_))
  162. return 0;
  163. rv = time::gettimeofday(&now,NULL);
  164. if(rv == -1)
  165. return -1;
  166. if(ts_[0].tv_nsec == UTIME_NOW)
  167. tv_[0] = now;
  168. if(ts_[1].tv_nsec == UTIME_NOW)
  169. tv_[1] = now;
  170. return 0;
  171. }
  172. static
  173. inline
  174. int
  175. convert_timespec_to_timeval(const int dirfd_,
  176. const std::string &path_,
  177. const struct timespec ts_[2],
  178. struct timeval tv_[2],
  179. struct timeval **tvp_,
  180. const int flags_)
  181. {
  182. int rv;
  183. if(l::should_be_set_to_now(ts_))
  184. return (tvp=NULL,0);
  185. TIMESPEC_TO_TIMEVAL(&tv_[0],&ts_[0]);
  186. TIMESPEC_TO_TIMEVAL(&tv_[1],&ts_[1]);
  187. rv = l::set_utime_omit_to_current_value(dirfd_,path_,ts_,tv_,flags_);
  188. if(rv == -1)
  189. return -1;
  190. rv = l::set_utime_now_to_now(ts_,tv_);
  191. if(rv == -1)
  192. return -1;
  193. return (*tvp_=tv_,0);
  194. }
  195. static
  196. inline
  197. int
  198. convert_timespec_to_timeval(const int fd_,
  199. const struct timespec ts_[2],
  200. struct timeval tv_[2],
  201. struct timeval **tvp_)
  202. {
  203. int rv;
  204. if(l::should_be_set_to_now(ts_))
  205. return (*tvp=NULL,0);
  206. TIMESPEC_TO_TIMEVAL(&tv_[0],&ts_[0]);
  207. TIMESPEC_TO_TIMEVAL(&tv_[1],&ts_[1]);
  208. rv = l::set_utime_omit_to_current_value(fd_,ts_,tv_);
  209. if(rv == -1)
  210. return -1;
  211. rv = l::set_utime_now_to_now(ts_,tv_);
  212. if(rv == -1)
  213. return -1;
  214. return (*tvp=tv,0);
  215. }
  216. }
  217. namespace fs
  218. {
  219. static
  220. inline
  221. int
  222. utimensat(const int dirfd_,
  223. const std::string &path_,
  224. const struct timespec ts_[2],
  225. const int flags_)
  226. {
  227. int rv;
  228. struct timeval tv[2];
  229. struct timeval *tvp;
  230. if(l::flags_invalid(flags))
  231. return (errno=EINVAL,-1);
  232. if(l::timespec_invalid(ts_))
  233. return (errno=EINVAL,-1);
  234. if(l::should_ignore(ts_))
  235. return 0;
  236. rv = l::convert_timespec_to_timeval(dirfd_,path_,ts_,tv,&tvp,flags_);
  237. if(rv == -1)
  238. return -1;
  239. if((flags_ & AT_SYMLINK_NOFOLLOW) == 0)
  240. return fs::futimesat(dirfd_,path_,tvp);
  241. if(l::can_call_lutimes(dirfd_,path_,flags))
  242. return fs::lutimes(path_,tvp);
  243. return (errno=ENOTSUP,-1);
  244. }
  245. }