Debian Codegraph @

lib/utimens.c

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