utimens.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* Set file access and modification times.
  2. Copyright (C) 2003-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3 of the License, or any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. /* derived from a function in touch.c */
  15. #include <config.h>
  16. #define _GL_UTIMENS_INLINE _GL_EXTERN_INLINE
  17. #include "utimens.h"
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdbool.h>
  21. #include <sys/stat.h>
  22. #include <sys/time.h>
  23. #include <unistd.h>
  24. #include <utime.h>
  25. #include "stat-time.h"
  26. #include "timespec.h"
  27. /* On native Windows, use SetFileTime; but avoid this when compiling
  28. GNU Emacs, which arranges for this in some other way and which
  29. defines WIN32_LEAN_AND_MEAN itself. */
  30. #if ((defined _WIN32 || defined __WIN32__) \
  31. && ! defined __CYGWIN__ && ! defined EMACS_CONFIGURATION)
  32. # define USE_SETFILETIME
  33. # define WIN32_LEAN_AND_MEAN
  34. # include <windows.h>
  35. # if GNULIB_MSVC_NOTHROW
  36. # include "msvc-nothrow.h"
  37. # else
  38. # include <io.h>
  39. # endif
  40. #endif
  41. /* Avoid recursion with rpl_futimens or rpl_utimensat. */
  42. #undef futimens
  43. #undef utimensat
  44. /* Solaris 9 mistakenly succeeds when given a non-directory with a
  45. trailing slash. Force the use of rpl_stat for a fix. */
  46. #ifndef REPLACE_FUNC_STAT_FILE
  47. # define REPLACE_FUNC_STAT_FILE 0
  48. #endif
  49. #if HAVE_UTIMENSAT || HAVE_FUTIMENS
  50. /* Cache variables for whether the utimensat syscall works; used to
  51. avoid calling the syscall if we know it will just fail with ENOSYS,
  52. and to avoid unnecessary work in massaging timestamps if the
  53. syscall will work. Multiple variables are needed, to distinguish
  54. between the following scenarios on Linux:
  55. utimensat doesn't exist, or is in glibc but kernel 2.6.18 fails with ENOSYS
  56. kernel 2.6.22 and earlier rejects AT_SYMLINK_NOFOLLOW
  57. kernel 2.6.25 and earlier reject UTIME_NOW/UTIME_OMIT with non-zero tv_sec
  58. kernel 2.6.32 used with xfs or ntfs-3g fail to honor UTIME_OMIT
  59. utimensat completely works
  60. For each cache variable: 0 = unknown, 1 = yes, -1 = no. */
  61. static int utimensat_works_really;
  62. static int lutimensat_works_really;
  63. #endif /* HAVE_UTIMENSAT || HAVE_FUTIMENS */
  64. /* Validate the requested timestamps. Return 0 if the resulting
  65. timespec can be used for utimensat (after possibly modifying it to
  66. work around bugs in utimensat). Return a positive value if the
  67. timespec needs further adjustment based on stat results: 1 if any
  68. adjustment is needed for utimes, and 2 if any adjustment is needed
  69. for Linux utimensat. Return -1, with errno set to EINVAL, if
  70. timespec is out of range. */
  71. static int
  72. validate_timespec (struct timespec timespec[2])
  73. {
  74. int result = 0;
  75. int utime_omit_count = 0;
  76. if ((timespec[0].tv_nsec != UTIME_NOW
  77. && timespec[0].tv_nsec != UTIME_OMIT
  78. && ! (0 <= timespec[0].tv_nsec
  79. && timespec[0].tv_nsec < TIMESPEC_RESOLUTION))
  80. || (timespec[1].tv_nsec != UTIME_NOW
  81. && timespec[1].tv_nsec != UTIME_OMIT
  82. && ! (0 <= timespec[1].tv_nsec
  83. && timespec[1].tv_nsec < TIMESPEC_RESOLUTION)))
  84. {
  85. errno = EINVAL;
  86. return -1;
  87. }
  88. /* Work around Linux kernel 2.6.25 bug, where utimensat fails with
  89. EINVAL if tv_sec is not 0 when using the flag values of tv_nsec.
  90. Flag a Linux kernel 2.6.32 bug, where an mtime of UTIME_OMIT
  91. fails to bump ctime. */
  92. if (timespec[0].tv_nsec == UTIME_NOW
  93. || timespec[0].tv_nsec == UTIME_OMIT)
  94. {
  95. timespec[0].tv_sec = 0;
  96. result = 1;
  97. if (timespec[0].tv_nsec == UTIME_OMIT)
  98. utime_omit_count++;
  99. }
  100. if (timespec[1].tv_nsec == UTIME_NOW
  101. || timespec[1].tv_nsec == UTIME_OMIT)
  102. {
  103. timespec[1].tv_sec = 0;
  104. result = 1;
  105. if (timespec[1].tv_nsec == UTIME_OMIT)
  106. utime_omit_count++;
  107. }
  108. return result + (utime_omit_count == 1);
  109. }
  110. /* Normalize any UTIME_NOW or UTIME_OMIT values in *TS, using stat
  111. buffer STATBUF to obtain the current timestamps of the file. If
  112. both times are UTIME_NOW, set *TS to NULL (as this can avoid some
  113. permissions issues). If both times are UTIME_OMIT, return true
  114. (nothing further beyond the prior collection of STATBUF is
  115. necessary); otherwise return false. */
  116. static bool
  117. update_timespec (struct stat const *statbuf, struct timespec *ts[2])
  118. {
  119. struct timespec *timespec = *ts;
  120. if (timespec[0].tv_nsec == UTIME_OMIT
  121. && timespec[1].tv_nsec == UTIME_OMIT)
  122. return true;
  123. if (timespec[0].tv_nsec == UTIME_NOW
  124. && timespec[1].tv_nsec == UTIME_NOW)
  125. {
  126. *ts = NULL;
  127. return false;
  128. }
  129. if (timespec[0].tv_nsec == UTIME_OMIT)
  130. timespec[0] = get_stat_atime (statbuf);
  131. else if (timespec[0].tv_nsec == UTIME_NOW)
  132. gettime (&timespec[0]);
  133. if (timespec[1].tv_nsec == UTIME_OMIT)
  134. timespec[1] = get_stat_mtime (statbuf);
  135. else if (timespec[1].tv_nsec == UTIME_NOW)
  136. gettime (&timespec[1]);
  137. return false;
  138. }
  139. /* Set the access and modification timestamps of FD (a.k.a. FILE) to be
  140. TIMESPEC[0] and TIMESPEC[1], respectively.
  141. FD must be either negative -- in which case it is ignored --
  142. or a file descriptor that is open on FILE.
  143. If FD is nonnegative, then FILE can be NULL, which means
  144. use just futimes (or equivalent) instead of utimes (or equivalent),
  145. and fail if on an old system without futimes (or equivalent).
  146. If TIMESPEC is null, set the timestamps to the current time.
  147. Return 0 on success, -1 (setting errno) on failure. */
  148. int
  149. fdutimens (int fd, char const *file, struct timespec const timespec[2])
  150. {
  151. struct timespec adjusted_timespec[2];
  152. struct timespec *ts = timespec ? adjusted_timespec : NULL;
  153. int adjustment_needed = 0;
  154. struct stat st;
  155. if (ts)
  156. {
  157. adjusted_timespec[0] = timespec[0];
  158. adjusted_timespec[1] = timespec[1];
  159. adjustment_needed = validate_timespec (ts);
  160. }
  161. if (adjustment_needed < 0)
  162. return -1;
  163. /* Require that at least one of FD or FILE are potentially valid, to avoid
  164. a Linux bug where futimens (AT_FDCWD, NULL) changes "." rather
  165. than failing. */
  166. if (fd < 0 && !file)
  167. {
  168. errno = EBADF;
  169. return -1;
  170. }
  171. /* Some Linux-based NFS clients are buggy, and mishandle timestamps
  172. of files in NFS file systems in some cases. We have no
  173. configure-time test for this, but please see
  174. <http://bugs.gentoo.org/show_bug.cgi?id=132673> for references to
  175. some of the problems with Linux 2.6.16. If this affects you,
  176. compile with -DHAVE_BUGGY_NFS_TIME_STAMPS; this is reported to
  177. help in some cases, albeit at a cost in performance. But you
  178. really should upgrade your kernel to a fixed version, since the
  179. problem affects many applications. */
  180. #if HAVE_BUGGY_NFS_TIME_STAMPS
  181. if (fd < 0)
  182. sync ();
  183. else
  184. fsync (fd);
  185. #endif
  186. /* POSIX 2008 added two interfaces to set file timestamps with
  187. nanosecond resolution; newer Linux implements both functions via
  188. a single syscall. We provide a fallback for ENOSYS (for example,
  189. compiling against Linux 2.6.25 kernel headers and glibc 2.7, but
  190. running on Linux 2.6.18 kernel). */
  191. #if HAVE_UTIMENSAT || HAVE_FUTIMENS
  192. if (0 <= utimensat_works_really)
  193. {
  194. int result;
  195. # if __linux__ || __sun
  196. /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
  197. systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
  198. but work if both times are either explicitly specified or
  199. UTIME_NOW. Work around it with a preparatory [f]stat prior
  200. to calling futimens/utimensat; fortunately, there is not much
  201. timing impact due to the extra syscall even on file systems
  202. where UTIME_OMIT would have worked.
  203. The same bug occurs in Solaris 11.1 (Apr 2013).
  204. FIXME: Simplify this for Linux in 2016 and for Solaris in
  205. 2024, when file system bugs are no longer common. */
  206. if (adjustment_needed == 2)
  207. {
  208. if (fd < 0 ? stat (file, &st) : fstat (fd, &st))
  209. return -1;
  210. if (ts[0].tv_nsec == UTIME_OMIT)
  211. ts[0] = get_stat_atime (&st);
  212. else if (ts[1].tv_nsec == UTIME_OMIT)
  213. ts[1] = get_stat_mtime (&st);
  214. /* Note that st is good, in case utimensat gives ENOSYS. */
  215. adjustment_needed++;
  216. }
  217. # endif
  218. # if HAVE_UTIMENSAT
  219. if (fd < 0)
  220. {
  221. result = utimensat (AT_FDCWD, file, ts, 0);
  222. # ifdef __linux__
  223. /* Work around a kernel bug:
  224. http://bugzilla.redhat.com/442352
  225. http://bugzilla.redhat.com/449910
  226. It appears that utimensat can mistakenly return 280 rather
  227. than -1 upon ENOSYS failure.
  228. FIXME: remove in 2010 or whenever the offending kernels
  229. are no longer in common use. */
  230. if (0 < result)
  231. errno = ENOSYS;
  232. # endif /* __linux__ */
  233. if (result == 0 || errno != ENOSYS)
  234. {
  235. utimensat_works_really = 1;
  236. return result;
  237. }
  238. }
  239. # endif /* HAVE_UTIMENSAT */
  240. # if HAVE_FUTIMENS
  241. if (0 <= fd)
  242. {
  243. result = futimens (fd, ts);
  244. # ifdef __linux__
  245. /* Work around the same bug as above. */
  246. if (0 < result)
  247. errno = ENOSYS;
  248. # endif /* __linux__ */
  249. if (result == 0 || errno != ENOSYS)
  250. {
  251. utimensat_works_really = 1;
  252. return result;
  253. }
  254. }
  255. # endif /* HAVE_FUTIMENS */
  256. }
  257. utimensat_works_really = -1;
  258. lutimensat_works_really = -1;
  259. #endif /* HAVE_UTIMENSAT || HAVE_FUTIMENS */
  260. #ifdef USE_SETFILETIME
  261. /* On native Windows, use SetFileTime(). See
  262. <https://msdn.microsoft.com/en-us/library/ms724933.aspx>
  263. <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
  264. if (0 <= fd)
  265. {
  266. HANDLE handle;
  267. FILETIME current_time;
  268. FILETIME last_access_time;
  269. FILETIME last_write_time;
  270. handle = (HANDLE) _get_osfhandle (fd);
  271. if (handle == INVALID_HANDLE_VALUE)
  272. {
  273. errno = EBADF;
  274. return -1;
  275. }
  276. if (ts == NULL || ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW)
  277. {
  278. /* GetSystemTimeAsFileTime
  279. <https://msdn.microsoft.com/en-us/library/ms724397.aspx>.
  280. It would be overkill to use
  281. GetSystemTimePreciseAsFileTime
  282. <https://msdn.microsoft.com/en-us/library/hh706895.aspx>. */
  283. GetSystemTimeAsFileTime (&current_time);
  284. }
  285. if (ts == NULL || ts[0].tv_nsec == UTIME_NOW)
  286. {
  287. last_access_time = current_time;
  288. }
  289. else if (ts[0].tv_nsec == UTIME_OMIT)
  290. {
  291. last_access_time.dwLowDateTime = 0;
  292. last_access_time.dwHighDateTime = 0;
  293. }
  294. else
  295. {
  296. ULONGLONG time_since_16010101 =
  297. (ULONGLONG) ts[0].tv_sec * 10000000 + ts[0].tv_nsec / 100 + 116444736000000000LL;
  298. last_access_time.dwLowDateTime = (DWORD) time_since_16010101;
  299. last_access_time.dwHighDateTime = time_since_16010101 >> 32;
  300. }
  301. if (ts == NULL || ts[1].tv_nsec == UTIME_NOW)
  302. {
  303. last_write_time = current_time;
  304. }
  305. else if (ts[1].tv_nsec == UTIME_OMIT)
  306. {
  307. last_write_time.dwLowDateTime = 0;
  308. last_write_time.dwHighDateTime = 0;
  309. }
  310. else
  311. {
  312. ULONGLONG time_since_16010101 =
  313. (ULONGLONG) ts[1].tv_sec * 10000000 + ts[1].tv_nsec / 100 + 116444736000000000LL;
  314. last_write_time.dwLowDateTime = (DWORD) time_since_16010101;
  315. last_write_time.dwHighDateTime = time_since_16010101 >> 32;
  316. }
  317. if (SetFileTime (handle, NULL, &last_access_time, &last_write_time))
  318. return 0;
  319. else
  320. {
  321. DWORD sft_error = GetLastError ();
  322. #if 0
  323. fprintf (stderr, "fdutimens SetFileTime error 0x%x\n", (unsigned int) sft_error);
  324. #endif
  325. switch (sft_error)
  326. {
  327. case ERROR_ACCESS_DENIED: /* fd was opened without O_RDWR */
  328. errno = EACCES; /* not specified by POSIX */
  329. break;
  330. default:
  331. errno = EINVAL;
  332. break;
  333. }
  334. return -1;
  335. }
  336. }
  337. #endif
  338. /* The platform lacks an interface to set file timestamps with
  339. nanosecond resolution, so do the best we can, discarding any
  340. fractional part of the timestamp. */
  341. if (adjustment_needed || (REPLACE_FUNC_STAT_FILE && fd < 0))
  342. {
  343. if (adjustment_needed != 3
  344. && (fd < 0 ? stat (file, &st) : fstat (fd, &st)))
  345. return -1;
  346. if (ts && update_timespec (&st, &ts))
  347. return 0;
  348. }
  349. {
  350. #if HAVE_FUTIMESAT || HAVE_WORKING_UTIMES
  351. struct timeval timeval[2];
  352. struct timeval *t;
  353. if (ts)
  354. {
  355. timeval[0].tv_sec = ts[0].tv_sec;
  356. timeval[0].tv_usec = ts[0].tv_nsec / 1000;
  357. timeval[1].tv_sec = ts[1].tv_sec;
  358. timeval[1].tv_usec = ts[1].tv_nsec / 1000;
  359. t = timeval;
  360. }
  361. else
  362. t = NULL;
  363. if (fd < 0)
  364. {
  365. # if HAVE_FUTIMESAT
  366. return futimesat (AT_FDCWD, file, t);
  367. # endif
  368. }
  369. else
  370. {
  371. /* If futimesat or futimes fails here, don't try to speed things
  372. up by returning right away. glibc can incorrectly fail with
  373. errno == ENOENT if /proc isn't mounted. Also, Mandrake 10.0
  374. in high security mode doesn't allow ordinary users to read
  375. /proc/self, so glibc incorrectly fails with errno == EACCES.
  376. If errno == EIO, EPERM, or EROFS, it's probably safe to fail
  377. right away, but these cases are rare enough that they're not
  378. worth optimizing, and who knows what other messed-up systems
  379. are out there? So play it safe and fall back on the code
  380. below. */
  381. # if (HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG) || HAVE_FUTIMES
  382. # if HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG
  383. # undef futimes
  384. # define futimes(fd, t) futimesat (fd, NULL, t)
  385. # endif
  386. if (futimes (fd, t) == 0)
  387. {
  388. # if __linux__ && __GLIBC__
  389. /* Work around a longstanding glibc bug, still present as
  390. of 2010-12-27. On older Linux kernels that lack both
  391. utimensat and utimes, glibc's futimes rounds instead of
  392. truncating when falling back on utime. The same bug
  393. occurs in futimesat with a null 2nd arg. */
  394. if (t)
  395. {
  396. bool abig = 500000 <= t[0].tv_usec;
  397. bool mbig = 500000 <= t[1].tv_usec;
  398. if ((abig | mbig) && fstat (fd, &st) == 0)
  399. {
  400. /* If these two subtractions overflow, they'll
  401. track the overflows inside the buggy glibc. */
  402. time_t adiff = st.st_atime - t[0].tv_sec;
  403. time_t mdiff = st.st_mtime - t[1].tv_sec;
  404. struct timeval *tt = NULL;
  405. struct timeval truncated_timeval[2];
  406. truncated_timeval[0] = t[0];
  407. truncated_timeval[1] = t[1];
  408. if (abig && adiff == 1 && get_stat_atime_ns (&st) == 0)
  409. {
  410. tt = truncated_timeval;
  411. tt[0].tv_usec = 0;
  412. }
  413. if (mbig && mdiff == 1 && get_stat_mtime_ns (&st) == 0)
  414. {
  415. tt = truncated_timeval;
  416. tt[1].tv_usec = 0;
  417. }
  418. if (tt)
  419. futimes (fd, tt);
  420. }
  421. }
  422. # endif
  423. return 0;
  424. }
  425. # endif
  426. }
  427. #endif /* HAVE_FUTIMESAT || HAVE_WORKING_UTIMES */
  428. if (!file)
  429. {
  430. #if ! ((HAVE_FUTIMESAT && !FUTIMESAT_NULL_BUG) \
  431. || (HAVE_WORKING_UTIMES && HAVE_FUTIMES))
  432. errno = ENOSYS;
  433. #endif
  434. return -1;
  435. }
  436. #ifdef USE_SETFILETIME
  437. return _gl_utimens_windows (file, ts);
  438. #elif HAVE_WORKING_UTIMES
  439. return utimes (file, t);
  440. #else
  441. {
  442. struct utimbuf utimbuf;
  443. struct utimbuf *ut;
  444. if (ts)
  445. {
  446. utimbuf.actime = ts[0].tv_sec;
  447. utimbuf.modtime = ts[1].tv_sec;
  448. ut = &utimbuf;
  449. }
  450. else
  451. ut = NULL;
  452. return utime (file, ut);
  453. }
  454. #endif /* !HAVE_WORKING_UTIMES */
  455. }
  456. }
  457. /* Set the access and modification timestamps of FILE to be
  458. TIMESPEC[0] and TIMESPEC[1], respectively. */
  459. int
  460. utimens (char const *file, struct timespec const timespec[2])
  461. {
  462. return fdutimens (-1, file, timespec);
  463. }
  464. /* Set the access and modification timestamps of FILE to be
  465. TIMESPEC[0] and TIMESPEC[1], respectively, without dereferencing
  466. symlinks. Fail with ENOSYS if the platform does not support
  467. changing symlink timestamps, but FILE was a symlink. */
  468. int
  469. lutimens (char const *file, struct timespec const timespec[2])
  470. {
  471. struct timespec adjusted_timespec[2];
  472. struct timespec *ts = timespec ? adjusted_timespec : NULL;
  473. int adjustment_needed = 0;
  474. struct stat st;
  475. if (ts)
  476. {
  477. adjusted_timespec[0] = timespec[0];
  478. adjusted_timespec[1] = timespec[1];
  479. adjustment_needed = validate_timespec (ts);
  480. }
  481. if (adjustment_needed < 0)
  482. return -1;
  483. /* The Linux kernel did not support symlink timestamps until
  484. utimensat, in version 2.6.22, so we don't need to mimic
  485. fdutimens' worry about buggy NFS clients. But we do have to
  486. worry about bogus return values. */
  487. #if HAVE_UTIMENSAT
  488. if (0 <= lutimensat_works_really)
  489. {
  490. int result;
  491. # if __linux__ || __sun
  492. /* As recently as Linux kernel 2.6.32 (Dec 2009), several file
  493. systems (xfs, ntfs-3g) have bugs with a single UTIME_OMIT,
  494. but work if both times are either explicitly specified or
  495. UTIME_NOW. Work around it with a preparatory lstat prior to
  496. calling utimensat; fortunately, there is not much timing
  497. impact due to the extra syscall even on file systems where
  498. UTIME_OMIT would have worked.
  499. The same bug occurs in Solaris 11.1 (Apr 2013).
  500. FIXME: Simplify this for Linux in 2016 and for Solaris in
  501. 2024, when file system bugs are no longer common. */
  502. if (adjustment_needed == 2)
  503. {
  504. if (lstat (file, &st))
  505. return -1;
  506. if (ts[0].tv_nsec == UTIME_OMIT)
  507. ts[0] = get_stat_atime (&st);
  508. else if (ts[1].tv_nsec == UTIME_OMIT)
  509. ts[1] = get_stat_mtime (&st);
  510. /* Note that st is good, in case utimensat gives ENOSYS. */
  511. adjustment_needed++;
  512. }
  513. # endif
  514. result = utimensat (AT_FDCWD, file, ts, AT_SYMLINK_NOFOLLOW);
  515. # ifdef __linux__
  516. /* Work around a kernel bug:
  517. http://bugzilla.redhat.com/442352
  518. http://bugzilla.redhat.com/449910
  519. It appears that utimensat can mistakenly return 280 rather
  520. than -1 upon ENOSYS failure.
  521. FIXME: remove in 2010 or whenever the offending kernels
  522. are no longer in common use. */
  523. if (0 < result)
  524. errno = ENOSYS;
  525. # endif
  526. if (result == 0 || errno != ENOSYS)
  527. {
  528. utimensat_works_really = 1;
  529. lutimensat_works_really = 1;
  530. return result;
  531. }
  532. }
  533. lutimensat_works_really = -1;
  534. #endif /* HAVE_UTIMENSAT */
  535. /* The platform lacks an interface to set file timestamps with
  536. nanosecond resolution, so do the best we can, discarding any
  537. fractional part of the timestamp. */
  538. if (adjustment_needed || REPLACE_FUNC_STAT_FILE)
  539. {
  540. if (adjustment_needed != 3 && lstat (file, &st))
  541. return -1;
  542. if (ts && update_timespec (&st, &ts))
  543. return 0;
  544. }
  545. /* On Linux, lutimes is a thin wrapper around utimensat, so there is
  546. no point trying lutimes if utimensat failed with ENOSYS. */
  547. #if HAVE_LUTIMES && !HAVE_UTIMENSAT
  548. {
  549. struct timeval timeval[2];
  550. struct timeval *t;
  551. int result;
  552. if (ts)
  553. {
  554. timeval[0].tv_sec = ts[0].tv_sec;
  555. timeval[0].tv_usec = ts[0].tv_nsec / 1000;
  556. timeval[1].tv_sec = ts[1].tv_sec;
  557. timeval[1].tv_usec = ts[1].tv_nsec / 1000;
  558. t = timeval;
  559. }
  560. else
  561. t = NULL;
  562. result = lutimes (file, t);
  563. if (result == 0 || errno != ENOSYS)
  564. return result;
  565. }
  566. #endif /* HAVE_LUTIMES && !HAVE_UTIMENSAT */
  567. /* Out of luck for symlinks, but we still handle regular files. */
  568. if (!(adjustment_needed || REPLACE_FUNC_STAT_FILE) && lstat (file, &st))
  569. return -1;
  570. if (!S_ISLNK (st.st_mode))
  571. return fdutimens (-1, file, ts);
  572. errno = ENOSYS;
  573. return -1;
  574. }