From 959d9cb463d992f9c5cc2fbeb54bf12eee2346f7 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 9 Oct 2009 16:05:19 -0600 Subject: [PATCH] futimens: new module Provides futimens where it is missing, and rpl_futimens to work around bugs in older Linux kernels. * modules/futimens: New file. * lib/futimens.c (futimens): Likewise. * m4/futimens.m4 (gl_FUNC_FUTIMENS): Likewise. * lib/utimens.c (futimens): Avoid recursion into rpl_futimens, so we can work around Linux bugs. * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Add witnesses. * modules/sys_stat (Makefile.am): Substitute them. * lib/sys_stat.in.h (futimens): Declare it. * MODULES.html.sh (systems lacking POSIX:2008): Mention module. * doc/posix-functions/futimens.texi (futimens): Likewise. * modules/futimens-tests: New test. * tests/test-futimens.c: Likewise. Signed-off-by: Eric Blake --- ChangeLog | 14 ++++++++++ MODULES.html.sh | 1 + doc/posix-functions/futimens.texi | 15 +++++----- lib/futimens.c | 37 +++++++++++++++++++++++++ lib/sys_stat.in.h | 17 ++++++++++++ lib/utimens.c | 3 ++ m4/futimens.m4 | 41 +++++++++++++++++++++++++++ m4/sys_stat_h.m4 | 5 +++- modules/futimens | 28 +++++++++++++++++++ modules/futimens-tests | 15 ++++++++++ modules/sys_stat | 3 ++ tests/test-futimens.c | 58 +++++++++++++++++++++++++++++++++++++++ 12 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 lib/futimens.c create mode 100644 m4/futimens.m4 create mode 100644 modules/futimens create mode 100644 modules/futimens-tests create mode 100644 tests/test-futimens.c diff --git a/ChangeLog b/ChangeLog index be1bac99a..b46babd08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ 2009-10-10 Eric Blake + futimens: new module + * modules/futimens: New file. + * lib/futimens.c (futimens): Likewise. + * m4/futimens.m4 (gl_FUNC_FUTIMENS): Likewise. + * lib/utimens.c (futimens): Avoid recursion into rpl_futimens, so + we can work around Linux bugs. + * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Add witnesses. + * modules/sys_stat (Makefile.am): Substitute them. + * lib/sys_stat.in.h (futimens): Declare it. + * MODULES.html.sh (systems lacking POSIX:2008): Mention module. + * doc/posix-functions/futimens.texi (futimens): Likewise. + * modules/futimens-tests: New test. + * tests/test-futimens.c: Likewise. + utimens: introduce fdutimens * lib/utimens.h (fdutimens): New prototype. * lib/utimens.c (gl_futimens): Move guts... diff --git a/MODULES.html.sh b/MODULES.html.sh index 53b0b5dac..7966717fd 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2262,6 +2262,7 @@ func_all_modules () func_module ftell func_module ftello func_module ftruncate + func_module futimens func_module getaddrinfo func_module getcwd func_module getgroups diff --git a/doc/posix-functions/futimens.texi b/doc/posix-functions/futimens.texi index 2a1a77918..1434033ea 100644 --- a/doc/posix-functions/futimens.texi +++ b/doc/posix-functions/futimens.texi @@ -4,14 +4,10 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/futimens.html} -Gnulib module: --- +Gnulib module: futimens Portability problems fixed by Gnulib: @itemize -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item This function is missing on some platforms: glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX @@ -26,9 +22,12 @@ When using @code{UTIME_OMIT} or @code{UTIME_NOW}, some systems require the @code{tv_sec} argument to be 0, and don't necessarily handle all file permissions in the manner required by POSIX: Linux kernel 2.6.25. +@end itemize + +Portability problems not fixed by Gnulib: +@itemize @item Some platforms lack the ability to change the timestamps of a file -descriptor, so this function can fail with @code{ENOSYS}. +descriptor, so the replacement can fail with @code{ENOSYS}; the gnulib +module @samp{utimens} provides a more reliable interface @code{gl_futimens}. @end itemize - -The gnulib module utimens provides a similar interface. diff --git a/lib/futimens.c b/lib/futimens.c new file mode 100644 index 000000000..f25374705 --- /dev/null +++ b/lib/futimens.c @@ -0,0 +1,37 @@ +/* Set the access and modification time of an open fd. + Copyright (C) 2009 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* written by Eric Blake */ + +#include + +#include + +#include "utimens.h" + +/* Set the access and modification time stamps of FD to be + TIMESPEC[0] and TIMESPEC[1], respectively. + Fail with ENOSYS on systems without futimes (or equivalent). + If TIMESPEC is null, set theh time stamps to the current time. + Return 0 on success, -1 (setting errno) on failure. */ +int +futimens (int fd, const struct timespec times[2]) +{ + /* fdutimens also works around bugs in native futimens, when running + with glibc compiled against newer headers but on a Linux kernel + older than 2.6.26. */ + return fdutimens (NULL, fd, times); +} diff --git a/lib/sys_stat.in.h b/lib/sys_stat.in.h index 7051d53bb..e54377096 100644 --- a/lib/sys_stat.in.h +++ b/lib/sys_stat.in.h @@ -355,6 +355,23 @@ extern int fstatat (int fd, char const *name, struct stat *st, int flags); #endif +#if @GNULIB_FUTIMENS@ +# if @REPLACE_FUTIMENS@ +# undef futimens +# define futimens rpl_futimens +# endif +# if !@HAVE_FUTIMENS@ || @REPLACE_FUTIMENS@ +extern int futimens (int fd, struct timespec const times[2]); +# endif +#elif defined GNULIB_POSIXCHECK +# undef futimens +# define futimens(f,t) \ + (GL_LINK_WARNING ("futimens is not portable - " \ + "use gnulib module futimens for portability"), \ + futimens (f, t)) +#endif + + #if @GNULIB_MKDIRAT@ # if !@HAVE_MKDIRAT@ extern int mkdirat (int fd, char const *file, mode_t mode); diff --git a/lib/utimens.c b/lib/utimens.c index 9119bc430..043f49744 100644 --- a/lib/utimens.c +++ b/lib/utimens.c @@ -49,6 +49,9 @@ struct utimbuf }; #endif +/* Avoid recursion with rpl_futimens. */ +#undef futimens + /* Validate the requested timestamps. Return 0 if the resulting timespec can be used for utimensat (after possibly modifying it to work around bugs in utimensat). Return 1 if the timespec needs diff --git a/m4/futimens.m4 b/m4/futimens.m4 new file mode 100644 index 000000000..0547e7ad5 --- /dev/null +++ b/m4/futimens.m4 @@ -0,0 +1,41 @@ +# serial 1 +# See if we need to provide futimens replacement. + +dnl Copyright (C) 2009 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +# Written by Eric Blake. + +AC_DEFUN([gl_FUNC_FUTIMENS], +[ + AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS]) + AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + AC_CHECK_FUNCS_ONCE([futimens]) + if test $ac_cv_func_futimens = no; then + HAVE_FUTIMENS=0 + AC_LIBOBJ([futimens]) + else + AC_CACHE_CHECK([whether futimens works], + [gl_cv_func_futimens_works], + [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ +#include +#include +#ifdef __linux__ +/* The Linux kernel added futimens in 2.6.22, but it had bugs until 2.6.26. + Always replace futimens to support older kernels. */ +choke me +#endif +]], [[struct timespec ts[2] = { { 1, UTIME_OMIT }, { 1, UTIME_OMIT } }; + if (futimens (AT_FDCWD, NULL)) return 1; + return futimens (open (".", O_RDONLY), ts);]])], + [gl_cv_func_futimens_works=yes], + [gl_cv_func_futimens_works="needs runtime check"], + [gl_cv_func_futimens_works="guessing no"])]) + if test "$gl_cv_func_futimens_works" != yes; then + REPLACE_FUTIMENS=1 + AC_LIBOBJ([futimens]) + fi + fi +]) diff --git a/m4/sys_stat_h.m4 b/m4/sys_stat_h.m4 index d37fea827..600489075 100644 --- a/m4/sys_stat_h.m4 +++ b/m4/sys_stat_h.m4 @@ -1,4 +1,4 @@ -# sys_stat_h.m4 serial 17 -*- Autoconf -*- +# sys_stat_h.m4 serial 18 -*- Autoconf -*- dnl Copyright (C) 2006-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -41,6 +41,7 @@ AC_DEFUN([gl_SYS_STAT_H_DEFAULTS], AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for REPLACE_FCHDIR GNULIB_FCHMODAT=0; AC_SUBST([GNULIB_FCHMODAT]) GNULIB_FSTATAT=0; AC_SUBST([GNULIB_FSTATAT]) + GNULIB_FUTIMENS=0; AC_SUBST([GNULIB_FUTIMENS]) GNULIB_LCHMOD=0; AC_SUBST([GNULIB_LCHMOD]) GNULIB_LSTAT=0; AC_SUBST([GNULIB_LSTAT]) GNULIB_MKDIRAT=0; AC_SUBST([GNULIB_MKDIRAT]) @@ -50,6 +51,7 @@ AC_DEFUN([gl_SYS_STAT_H_DEFAULTS], dnl Assume proper GNU behavior unless another module says otherwise. HAVE_FCHMODAT=1; AC_SUBST([HAVE_FCHMODAT]) HAVE_FSTATAT=1; AC_SUBST([HAVE_FSTATAT]) + HAVE_FUTIMENS=1; AC_SUBST([HAVE_FUTIMENS]) HAVE_LCHMOD=1; AC_SUBST([HAVE_LCHMOD]) HAVE_LSTAT=1; AC_SUBST([HAVE_LSTAT]) HAVE_MKDIRAT=1; AC_SUBST([HAVE_MKDIRAT]) @@ -57,6 +59,7 @@ AC_DEFUN([gl_SYS_STAT_H_DEFAULTS], HAVE_MKNODAT=1; AC_SUBST([HAVE_MKNODAT]) REPLACE_FSTAT=0; AC_SUBST([REPLACE_FSTAT]) REPLACE_FSTATAT=0; AC_SUBST([REPLACE_FSTATAT]) + REPLACE_FUTIMENS=0; AC_SUBST([REPLACE_FUTIMENS]) REPLACE_LSTAT=0; AC_SUBST([REPLACE_LSTAT]) REPLACE_MKDIR=0; AC_SUBST([REPLACE_MKDIR]) REPLACE_STAT=0; AC_SUBST([REPLACE_STAT]) diff --git a/modules/futimens b/modules/futimens new file mode 100644 index 000000000..46f62302c --- /dev/null +++ b/modules/futimens @@ -0,0 +1,28 @@ +Description: +Set file access and modification times of a file descriptor. + +Files: +lib/futimens.c +m4/futimens.m4 + +Depends-on: +sys_stat +utimens + +configure.ac: +gl_FUNC_FUTIMENS +gl_SYS_STAT_MODULE_INDICATOR([futimens]) + +Makefile.am: + +Include: + + +Link: +$(LIB_CLOCK_GETTIME) + +License: +GPL + +Maintainer: +Eric Blake diff --git a/modules/futimens-tests b/modules/futimens-tests new file mode 100644 index 000000000..50756da1f --- /dev/null +++ b/modules/futimens-tests @@ -0,0 +1,15 @@ +Files: +tests/test-futimens.h +tests/test-futimens.c + +Depends-on: +timespec +utimecmp + +configure.ac: +AC_CHECK_FUNCS_ONCE([usleep]) + +Makefile.am: +TESTS += test-futimens +check_PROGRAMS += test-futimens +test_futimens_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME) @LIBINTL@ diff --git a/modules/sys_stat b/modules/sys_stat index a4abafe99..5d8b0ef05 100644 --- a/modules/sys_stat +++ b/modules/sys_stat @@ -28,6 +28,7 @@ sys/stat.h: sys_stat.in.h -e 's|@''NEXT_SYS_STAT_H''@|$(NEXT_SYS_STAT_H)|g' \ -e 's|@''GNULIB_FCHMODAT''@|$(GNULIB_FCHMODAT)|g' \ -e 's|@''GNULIB_FSTATAT''@|$(GNULIB_FSTATAT)|g' \ + -e 's|@''GNULIB_FUTIMENS''@|$(GNULIB_FUTIMENS)|g' \ -e 's|@''GNULIB_LCHMOD''@|$(GNULIB_LCHMOD)|g' \ -e 's|@''GNULIB_LSTAT''@|$(GNULIB_LSTAT)|g' \ -e 's|@''GNULIB_MKDIRAT''@|$(GNULIB_MKDIRAT)|g' \ @@ -36,6 +37,7 @@ sys/stat.h: sys_stat.in.h -e 's|@''GNULIB_STAT''@|$(GNULIB_STAT)|g' \ -e 's|@''HAVE_FCHMODAT''@|$(HAVE_FCHMODAT)|g' \ -e 's|@''HAVE_FSTATAT''@|$(HAVE_FSTATAT)|g' \ + -e 's|@''HAVE_FUTIMENS''@|$(HAVE_FUTIMENS)|g' \ -e 's|@''HAVE_LCHMOD''@|$(HAVE_LCHMOD)|g' \ -e 's|@''HAVE_LSTAT''@|$(HAVE_LSTAT)|g' \ -e 's|@''HAVE_MKDIRAT''@|$(HAVE_MKDIRAT)|g' \ @@ -43,6 +45,7 @@ sys/stat.h: sys_stat.in.h -e 's|@''HAVE_MKNODAT''@|$(HAVE_MKNODAT)|g' \ -e 's|@''REPLACE_FSTAT''@|$(REPLACE_FSTAT)|g' \ -e 's|@''REPLACE_FSTATAT''@|$(REPLACE_FSTATAT)|g' \ + -e 's|@''REPLACE_FUTIMENS''@|$(REPLACE_FUTIMENS)|g' \ -e 's|@''REPLACE_LSTAT''@|$(REPLACE_LSTAT)|g' \ -e 's|@''REPLACE_MKDIR''@|$(REPLACE_MKDIR)|g' \ -e 's|@''REPLACE_STAT''@|$(REPLACE_STAT)|g' \ diff --git a/tests/test-futimens.c b/tests/test-futimens.c new file mode 100644 index 000000000..3eac64c4c --- /dev/null +++ b/tests/test-futimens.c @@ -0,0 +1,58 @@ +/* Tests of futimens. + Copyright (C) 2009 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Eric Blake , 2009. */ + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "stat-time.h" +#include "timespec.h" +#include "utimecmp.h" + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define BASE "test-futimens.t" + +#include "test-futimens.h" + +int +main () +{ + /* Clean up any trash from prior testsuite runs. */ + ASSERT (system ("rm -rf " BASE "*") == 0); + + return test_futimens (futimens, true); +} -- 2.11.0