| 1 |
# Detect some bugs in glibc's implementation of utimes. |
| 2 |
# serial 9 |
| 3 |
|
| 4 |
dnl Copyright (C) 2003-2005, 2009-2024 Free Software Foundation, Inc. |
| 5 |
dnl This file is free software; the Free Software Foundation |
| 6 |
dnl gives unlimited permission to copy and/or distribute it, |
| 7 |
dnl with or without modifications, as long as this notice is preserved. |
| 8 |
dnl This file is offered as-is, without any warranty. |
| 9 |
|
| 10 |
# See if we need to work around bugs in glibc's implementation of |
| 11 |
# utimes from 2003-07-12 to 2003-09-17. |
| 12 |
# First, there was a bug that would make utimes set mtime |
| 13 |
# and atime to zero (1970-01-01) unconditionally. |
| 14 |
# Then, there was code to round rather than truncate. |
| 15 |
# Then, there was an implementation (sparc64, Linux-2.4.28, glibc-2.3.3) |
| 16 |
# that didn't honor the NULL-means-set-to-current-time semantics. |
| 17 |
# Finally, there was also a version of utimes that failed on read-only |
| 18 |
# files, while utime worked fine (linux-2.2.20, glibc-2.2.5). |
| 19 |
# |
| 20 |
# From Jim Meyering, with suggestions from Paul Eggert. |
| 21 |
|
| 22 |
AC_DEFUN([gl_FUNC_UTIMES], |
| 23 |
[ |
| 24 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 25 |
AC_CACHE_CHECK([whether the utimes function works], |
| 26 |
[gl_cv_func_working_utimes], |
| 27 |
[AC_RUN_IFELSE([AC_LANG_SOURCE([[ |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#include <time.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <utime.h> |
| 37 |
#include <errno.h> |
| 38 |
]GL_MDA_DEFINES[ |
| 39 |
|
| 40 |
static int |
| 41 |
inorder (time_t a, time_t b, time_t c) |
| 42 |
{ |
| 43 |
return a <= b && b <= c; |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
main () |
| 48 |
{ |
| 49 |
int result = 0; |
| 50 |
char const *file = "conftest.utimes"; |
| 51 |
/* On OS/2, file timestamps must be on or after 1980 in local time, |
| 52 |
with an even number of seconds. */ |
| 53 |
static struct timeval timeval[2] = {{315620000 + 10, 10}, |
| 54 |
{315620000 + 1000000, 999998}}; |
| 55 |
|
| 56 |
/* Test whether utimes() essentially works. */ |
| 57 |
{ |
| 58 |
struct stat sbuf; |
| 59 |
FILE *f = fopen (file, "w"); |
| 60 |
if (f == NULL) |
| 61 |
result |= 1; |
| 62 |
else if (fclose (f) != 0) |
| 63 |
result |= 1; |
| 64 |
else if (utimes (file, timeval) != 0) |
| 65 |
result |= 2; |
| 66 |
else if (lstat (file, &sbuf) != 0) |
| 67 |
result |= 1; |
| 68 |
else if (!(sbuf.st_atime == timeval[0].tv_sec |
| 69 |
&& sbuf.st_mtime == timeval[1].tv_sec)) |
| 70 |
result |= 4; |
| 71 |
if (unlink (file) != 0) |
| 72 |
result |= 1; |
| 73 |
} |
| 74 |
|
| 75 |
/* Test whether utimes() with a NULL argument sets the file's timestamp |
| 76 |
to the current time. Use 'fstat' as well as 'time' to |
| 77 |
determine the "current" time, to accommodate NFS file systems |
| 78 |
if there is a time skew between the host and the NFS server. */ |
| 79 |
{ |
| 80 |
int fd = open (file, O_WRONLY|O_CREAT, 0644); |
| 81 |
if (fd < 0) |
| 82 |
result |= 1; |
| 83 |
else |
| 84 |
{ |
| 85 |
time_t t0, t2; |
| 86 |
struct stat st0, st1, st2; |
| 87 |
if (time (&t0) == (time_t) -1) |
| 88 |
result |= 1; |
| 89 |
else if (fstat (fd, &st0) != 0) |
| 90 |
result |= 1; |
| 91 |
else if (utimes (file, timeval) != 0 |
| 92 |
&& (errno != EACCES |
| 93 |
/* OS/2 kLIBC utimes fails on opened files. */ |
| 94 |
|| close (fd) != 0 |
| 95 |
|| utimes (file, timeval) != 0 |
| 96 |
|| (fd = open (file, O_WRONLY)) < 0)) |
| 97 |
result |= 2; |
| 98 |
else if (utimes (file, NULL) != 0 |
| 99 |
&& (errno != EACCES |
| 100 |
/* OS/2 kLIBC utimes fails on opened files. */ |
| 101 |
|| close (fd) != 0 |
| 102 |
|| utimes (file, NULL) != 0 |
| 103 |
|| (fd = open (file, O_WRONLY)) < 0)) |
| 104 |
result |= 8; |
| 105 |
else if (fstat (fd, &st1) != 0) |
| 106 |
result |= 1; |
| 107 |
else if (write (fd, "\n", 1) != 1) |
| 108 |
result |= 1; |
| 109 |
else if (fstat (fd, &st2) != 0) |
| 110 |
result |= 1; |
| 111 |
else if (time (&t2) == (time_t) -1) |
| 112 |
result |= 1; |
| 113 |
else |
| 114 |
{ |
| 115 |
int m_ok_POSIX = inorder (t0, st1.st_mtime, t2); |
| 116 |
int m_ok_NFS = inorder (st0.st_mtime, st1.st_mtime, st2.st_mtime); |
| 117 |
if (! (st1.st_atime == st1.st_mtime)) |
| 118 |
result |= 16; |
| 119 |
if (! (m_ok_POSIX || m_ok_NFS)) |
| 120 |
result |= 32; |
| 121 |
} |
| 122 |
if (close (fd) != 0) |
| 123 |
result |= 1; |
| 124 |
} |
| 125 |
if (unlink (file) != 0) |
| 126 |
result |= 1; |
| 127 |
} |
| 128 |
|
| 129 |
/* Test whether utimes() with a NULL argument works on read-only files. */ |
| 130 |
{ |
| 131 |
int fd = open (file, O_WRONLY|O_CREAT, 0444); |
| 132 |
if (fd < 0) |
| 133 |
result |= 1; |
| 134 |
else if (close (fd) != 0) |
| 135 |
result |= 1; |
| 136 |
else if (utimes (file, NULL) != 0) |
| 137 |
result |= 64; |
| 138 |
if (unlink (file) != 0) |
| 139 |
result |= 1; |
| 140 |
} |
| 141 |
|
| 142 |
return result; |
| 143 |
} |
| 144 |
]])], |
| 145 |
[gl_cv_func_working_utimes=yes], |
| 146 |
[gl_cv_func_working_utimes=no], |
| 147 |
[case "$host_os" in |
| 148 |
# Guess yes on musl systems. |
| 149 |
*-musl*) gl_cv_func_working_utimes="guessing yes" ;; |
| 150 |
# Guess no on native Windows. |
| 151 |
mingw* | windows*) gl_cv_func_working_utimes="guessing no" ;; |
| 152 |
*) gl_cv_func_working_utimes="$gl_cross_guess_normal" ;; |
| 153 |
esac |
| 154 |
]) |
| 155 |
]) |
| 156 |
|
| 157 |
case "$gl_cv_func_working_utimes" in |
| 158 |
*yes) |
| 159 |
AC_DEFINE([HAVE_WORKING_UTIMES], [1], [Define if utimes works properly.]) |
| 160 |
;; |
| 161 |
esac |
| 162 |
]) |