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.

313 lines
6.6 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2016, 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. #pragma once
  16. #include "fs_base_futimesat.hpp"
  17. #include "fs_base_stat.hpp"
  18. #include <string>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <sys/time.h>
  22. #ifndef UTIME_NOW
  23. # define UTIME_NOW ((1l << 30) - 1l)
  24. #endif
  25. #ifndef UTIME_OMIT
  26. # define UTIME_OMIT ((1l << 30) - 2l)
  27. #endif
  28. static
  29. inline
  30. bool
  31. _can_call_lutimes(const int dirfd_,
  32. const std::string &path_,
  33. const int flags_)
  34. {
  35. return ((flags_ == AT_SYMLINK_NOFOLLOW) &&
  36. ((dirfd_ == AT_FDCWD) ||
  37. (path_[0] == '/')));
  38. }
  39. static
  40. inline
  41. bool
  42. _should_ignore(const struct timespec ts_[2])
  43. {
  44. return ((ts_ != NULL) &&
  45. (ts_[0].tv_nsec == UTIME_OMIT) &&
  46. (ts_[1].tv_nsec == UTIME_OMIT));
  47. }
  48. static
  49. inline
  50. bool
  51. _should_be_set_to_now(const struct timespec ts_[2])
  52. {
  53. return ((ts_ == NULL) ||
  54. ((ts_[0].tv_nsec == UTIME_NOW) &&
  55. (ts_[1].tv_nsec == UTIME_NOW)));
  56. }
  57. static
  58. inline
  59. bool
  60. _timespec_invalid(const struct timespec &ts_)
  61. {
  62. return (((ts_.tv_nsec < 0) ||
  63. (ts_.tv_nsec > 999999999)) &&
  64. ((ts_.tv_nsec != UTIME_NOW) &&
  65. (ts_.tv_nsec != UTIME_OMIT)));
  66. }
  67. static
  68. inline
  69. bool
  70. _timespec_invalid(const struct timespec ts_[2])
  71. {
  72. return ((ts_ != NULL) &&
  73. (_timespec_invalid(ts_[0]) ||
  74. _timespec_invalid(ts_[1])));
  75. }
  76. static
  77. inline
  78. bool
  79. _flags_invalid(const int flags)
  80. {
  81. return ((flags & ~AT_SYMLINK_NOFOLLOW) != 0);
  82. }
  83. static
  84. inline
  85. bool
  86. _any_timespec_is_utime_omit(const struct timespec ts_[2])
  87. {
  88. return ((ts_[0].tv_nsec == UTIME_OMIT) ||
  89. (ts_[1].tv_nsec == UTIME_OMIT));
  90. }
  91. static
  92. inline
  93. bool
  94. _any_timespec_is_utime_now(const struct timespec ts_[2])
  95. {
  96. return ((ts_[0].tv_nsec == UTIME_NOW) ||
  97. (ts_[1].tv_nsec == UTIME_NOW));
  98. }
  99. static
  100. inline
  101. int
  102. _set_utime_omit_to_current_value(const int dirfd,
  103. const std::string &path,
  104. const struct timespec ts_[2],
  105. struct timeval tv[2],
  106. const int flags)
  107. {
  108. int rv;
  109. struct stat st;
  110. timespec *atime;
  111. timespec *mtime;
  112. if(!_any_timespec_is_utime_omit(ts_))
  113. return 0;
  114. rv = ::fstatat(dirfd,path.c_str(),&st,flags);
  115. if(rv == -1)
  116. return -1;
  117. atime = fs::stat_atime(st);
  118. mtime = fs::stat_mtime(st);
  119. if(ts_[0].tv_nsec == UTIME_OMIT)
  120. TIMESPEC_TO_TIMEVAL(&tv[0],atime);
  121. if(ts_[1].tv_nsec == UTIME_OMIT)
  122. TIMESPEC_TO_TIMEVAL(&tv[1],mtime);
  123. return 0;
  124. }
  125. static
  126. inline
  127. int
  128. _set_utime_omit_to_current_value(const int fd,
  129. const struct timespec ts_[2],
  130. struct timeval tv[2])
  131. {
  132. int rv;
  133. struct stat st;
  134. timespec *atime;
  135. timespec *mtime;
  136. if(!_any_timespec_is_utime_omit(ts_))
  137. return 0;
  138. rv = ::fstat(fd,&st);
  139. if(rv == -1)
  140. return -1;
  141. atime = fs::stat_atime(st);
  142. mtime = fs::stat_mtime(st);
  143. if(ts_[0].tv_nsec == UTIME_OMIT)
  144. TIMESPEC_TO_TIMEVAL(&tv[0],atime);
  145. if(ts_[1].tv_nsec == UTIME_OMIT)
  146. TIMESPEC_TO_TIMEVAL(&tv[1],mtime);
  147. return 0;
  148. }
  149. static
  150. inline
  151. int
  152. _set_utime_now_to_now(const struct timespec ts_[2],
  153. struct timeval tv[2])
  154. {
  155. int rv;
  156. struct timeval now;
  157. if(_any_timespec_is_utime_now(ts_))
  158. return 0;
  159. rv = ::gettimeofday(&now,NULL);
  160. if(rv == -1)
  161. return -1;
  162. if(ts_[0].tv_nsec == UTIME_NOW)
  163. tv[0] = now;
  164. if(ts_[1].tv_nsec == UTIME_NOW)
  165. tv[1] = now;
  166. return 0;
  167. }
  168. static
  169. inline
  170. int
  171. _convert_timespec_to_timeval(const int dirfd,
  172. const std::string &path,
  173. const struct timespec ts_[2],
  174. struct timeval tv[2],
  175. struct timeval *&tvp,
  176. const int flags)
  177. {
  178. int rv;
  179. if(_should_be_set_to_now(ts_))
  180. return (tvp=NULL,0);
  181. TIMESPEC_TO_TIMEVAL(&tv[0],&ts_[0]);
  182. TIMESPEC_TO_TIMEVAL(&tv[1],&ts_[1]);
  183. rv = _set_utime_omit_to_current_value(dirfd,path,ts_,tv,flags);
  184. if(rv == -1)
  185. return -1;
  186. rv = _set_utime_now_to_now(ts_,tv);
  187. if(rv == -1)
  188. return -1;
  189. return (tvp=tv,0);
  190. }
  191. static
  192. inline
  193. int
  194. _convert_timespec_to_timeval(const int fd,
  195. const struct timespec ts_[2],
  196. struct timeval tv[2],
  197. struct timeval *&tvp)
  198. {
  199. int rv;
  200. if(_should_be_set_to_now(ts_))
  201. return (tvp=NULL,0);
  202. TIMESPEC_TO_TIMEVAL(&tv[0],&ts_[0]);
  203. TIMESPEC_TO_TIMEVAL(&tv[1],&ts_[1]);
  204. rv = _set_utime_omit_to_current_value(fd,ts_,tv);
  205. if(rv == -1)
  206. return -1;
  207. rv = _set_utime_now_to_now(ts_,tv);
  208. if(rv == -1)
  209. return -1;
  210. return (tvp=tv,0);
  211. }
  212. namespace fs
  213. {
  214. static
  215. inline
  216. int
  217. utime(const int dirfd,
  218. const std::string &path,
  219. const struct timespec ts_[2],
  220. const int flags)
  221. {
  222. int rv;
  223. struct timeval tv[2];
  224. struct timeval *tvp;
  225. if(_flags_invalid(flags))
  226. return (errno=EINVAL,-1);
  227. if(_timespec_invalid(ts_))
  228. return (errno=EINVAL,-1);
  229. if(_should_ignore(ts_))
  230. return 0;
  231. rv = _convert_timespec_to_timeval(dirfd,path,ts_,tv,tvp,flags);
  232. if(rv == -1)
  233. return -1;
  234. if((flags & AT_SYMLINK_NOFOLLOW) == 0)
  235. return fs::futimesat(dirfd,path.c_str(),tvp);
  236. if(_can_call_lutimes(dirfd,path,flags))
  237. return ::lutimes(path.c_str(),tvp);
  238. return (errno=ENOTSUP,-1);
  239. }
  240. static
  241. inline
  242. int
  243. futimens(const int fd_,
  244. const struct timespec ts_[2])
  245. {
  246. int rv;
  247. struct timeval tv[2];
  248. struct timeval *tvp;
  249. if(_timespec_invalid(ts_))
  250. return (errno=EINVAL,-1);
  251. if(_should_ignore(ts_))
  252. return 0;
  253. rv = _convert_timespec_to_timeval(fd_,ts_,tv,tvp);
  254. if(rv == -1)
  255. return -1;
  256. return ::futimes(fd_,tvp);
  257. }
  258. }