Mercurial > hg > octave-kai > gnulib-hg
changeset 6272:3baa574549ae
* modules/openat (Files): Add lib/openat-die.c.
(Depends-on): Remove error, exitfail.
Add dirname.
* lib/openat.c (fdopendir): Be sure to close the supplied
file descriptor before returning. This makes our replacement
implementation a little closer to Solaris's, where fdopendir
ties the file descriptor to the returned DIR* pointer.
* lib/openat.c (unlinkat): New function.
* lib/openat.h (unlinkat): Add prototype.
* lib/openat-die.c (openat_save_fail): Rename from openat_save_die.
(openat_restore_fail): Rename from openat_restore_die.
* lib/openat.c, openat.h: Reflect s/_die/_fail/ renaming.
Provide an alternative to exiting immediately upon save_cwd or
restore_cwd failure. Now, an application can arrange e.g.,
to perform a longjump in that case.
* lib/openat.c: Include dirname.h.
Use IS_ABSOLUTE_FILE_NAME rather than testing for leading slash.
(rpl_openat, fdopendir, fstatat): Call openat_save_die
and openat_restore_die rather than calling error directly.
Don't include "error.h" or "exitfail.h"; they're no longer needed.
* lib/openat-die.c (openat_save_die, openat_restore_die): New file.
* lib/openat.h (openat_save_die, openat_restore_die): Declare and define.
* m4/openat.m4 (gl_FUNC_OPENAT): Add openat-die.c.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 22 Sep 2005 23:30:37 +0000 |
parents | fdd622fe6d47 |
children | 52428341346b |
files | lib/openat-die.c lib/openat.c lib/openat.h m4/openat.m4 modules/openat |
diffstat | 5 files changed, 135 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/lib/openat-die.c @@ -0,0 +1,53 @@ +/* Report a save- or restore-cwd failure in our openat replacement and then exit. + + Copyright (C) 2005 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#if HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdlib.h> + +#include "error.h" +#include "exitfail.h" + +#include "gettext.h" +#define _(msgid) gettext (msgid) +#define N_(msgid) msgid + +void +openat_save_fail (int errno) +{ + error (exit_failure, errno, + _("unable to record current working directory")); + + /* The `noreturn' attribute cannot be applied to error, since it returns + when its first argument is 0. To help compilers understand that this + function does not return, call abort. Also, the abort is a + safety feature if exit_failure is 0 (which shouldn't happen). */ + abort (); +} + +void +openat_restore_fail (int errno) +{ + error (exit_failure, errno, + _("failed to return to initial working directory")); + + /* As above. */ + abort (); +}
--- a/lib/openat.c +++ b/lib/openat.c @@ -29,8 +29,7 @@ #include <errno.h> #include <fcntl.h> -#include "error.h" -#include "exitfail.h" +#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */ #include "save-cwd.h" #include "gettext.h" @@ -64,12 +63,11 @@ va_end (arg); } - if (fd == AT_FDCWD || *file == '/') + if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file)) return open (file, flags, mode); if (save_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("openat: unable to record current working directory")); + openat_save_fail (errno); if (fchdir (fd) != 0) { @@ -83,8 +81,7 @@ saved_errno = errno; if (restore_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("openat: unable to restore working directory")); + openat_restore_fail (errno); free_cwd (&saved_cwd); @@ -98,7 +95,12 @@ If either the save_cwd or the restore_cwd fails (relatively unlikely, and usually indicative of a problem that deserves close attention), then give a diagnostic and exit nonzero. - Otherwise, this function works just like Solaris' fdopendir. */ + Otherwise, this function works just like Solaris' fdopendir. + + W A R N I N G: + Unlike the other fd-related functions here, this one + effectively consumes its FD parameter. The caller should not + close or otherwise manipulate FD after calling this function. */ DIR * fdopendir (int fd) { @@ -110,13 +112,13 @@ return opendir ("."); if (save_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("fdopendir: unable to record current working directory")); + openat_save_fail (errno); if (fchdir (fd) != 0) { saved_errno = errno; free_cwd (&saved_cwd); + close (fd); errno = saved_errno; return NULL; } @@ -125,10 +127,10 @@ saved_errno = errno; if (restore_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("fdopendir: unable to restore working directory")); + openat_restore_fail (errno); free_cwd (&saved_cwd); + close (fd); errno = saved_errno; return dir; @@ -154,8 +156,7 @@ : stat (file, st)); if (save_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("fstatat: unable to record current working directory")); + openat_save_fail (errno); if (fchdir (fd) != 0) { @@ -171,11 +172,50 @@ saved_errno = errno; if (restore_cwd (&saved_cwd) != 0) - error (exit_failure, errno, - _("fstatat: unable to restore working directory")); + openat_restore_fail (errno); free_cwd (&saved_cwd); errno = saved_errno; return err; } + +/* Replacement for Solaris' function by the same name. + <http://www.google.com/search?q=unlinkat+site:docs.sun.com> + Simulate it by doing save_cwd/fchdir/(unlink|rmdir)/restore_cwd. + If either the save_cwd or the restore_cwd fails (relatively unlikely, + and usually indicative of a problem that deserves close attention), + then give a diagnostic and exit nonzero. + Otherwise, this function works just like Solaris' unlinkat. */ +int +unlinkat (int fd, char const *file, int flag) +{ + struct saved_cwd saved_cwd; + int saved_errno; + int err; + + if (fd == AT_FDCWD) + return (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file)); + + if (save_cwd (&saved_cwd) != 0) + openat_save_fail (errno); + + if (fchdir (fd) != 0) + { + saved_errno = errno; + free_cwd (&saved_cwd); + errno = saved_errno; + return -1; + } + + err = (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file)); + saved_errno = errno; + + if (restore_cwd (&saved_cwd) != 0) + openat_restore_fail (errno); + + free_cwd (&saved_cwd); + + errno = saved_errno; + return err; +}
--- a/lib/openat.h +++ b/lib/openat.h @@ -24,9 +24,20 @@ #include <dirent.h> #include <unistd.h> +#ifndef __attribute__ +# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ +# define __attribute__(x) /* empty */ +# endif +#endif + +#ifndef ATTRIBUTE_NORETURN +# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) +#endif + #ifndef AT_FDCWD -# define AT_FDCWD (-3041965) /* same value as Solaris 9 */ -# define AT_SYMLINK_NOFOLLOW 4096 /* same value as Solaris 9 */ +# define AT_FDCWD (-3041965) /* same value as Solaris 9 */ +# define AT_SYMLINK_NOFOLLOW 4096 /* same value as Solaris 9 */ +# define AT_REMOVEDIR (0x1) /* same value as Solaris 9 */ # ifdef __OPENAT_PREFIX # undef openat @@ -39,6 +50,13 @@ DIR *fdopendir (int fd); # define fstatat __OPENAT_ID (fstatat) int fstatat (int fd, char const *file, struct stat *st, int flag); +# define unlinkat __OPENAT_ID (unlinkat) +int unlinkat (int fd, char const *file, int flag); +void openat_restore_fail (int) ATTRIBUTE_NORETURN; +void openat_save_fail (int) ATTRIBUTE_NORETURN; +# else +# define openat_restore_fail(Errno) /* empty */ +# define openat_save_fail(Errno) /* empty */ # endif #endif
--- a/m4/openat.m4 +++ b/m4/openat.m4 @@ -1,7 +1,7 @@ -#serial 3 +#serial 4 # See if we need to use our replacement for Solaris' openat function. -dnl Copyright (C) 2004 Free Software Foundation, Inc. +dnl Copyright (C) 2004, 2005 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. @@ -10,7 +10,8 @@ AC_DEFUN([gl_FUNC_OPENAT], [ - AC_LIBSOURCES([openat.c, openat.h]) + AC_LIBSOURCES([openat.c, openat.h, openat-die.c]) + AC_LIBOBJ([openat-die]) AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) AC_REPLACE_FUNCS(openat) case $ac_cv_func_openat in