(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.
--- /dev/null
+/* 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 ();
+}
#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"
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)
{
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);
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)
{
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;
}
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;
: 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)
{
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);
#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
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
-#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.
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
Files:
lib/openat.c
lib/openat.h
+lib/openat-die.c
m4/openat.m4
Depends-on:
save-cwd
gettext-h
-error
-exitfail
+dirname
extensions
configure.ac: