bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

openat changes imported from gnulib


From: Paul Eggert
Subject: openat changes imported from gnulib
Date: Thu, 22 Sep 2005 16:31:11 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

I installed this:

2005-09-22  Paul Eggert  <address@hidden>

        * modules/openat (Files): Add lib/openat-die.c.
        (Depends-on): Remove error, exitfail.
        Add dirname.

2005-09-22  Jim Meyering  <address@hidden>

        * 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.

Index: modules/openat
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/openat,v
retrieving revision 1.3
diff -p -u -r1.3 openat
--- modules/openat      6 Jul 2005 15:58:47 -0000       1.3
+++ modules/openat      22 Sep 2005 23:25:20 -0000
@@ -4,13 +4,13 @@ Open a file at a directory.
 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:
Index: lib/openat.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/openat.c,v
retrieving revision 1.5
diff -p -u -r1.5 openat.c
--- lib/openat.c        19 Sep 2005 17:28:14 -0000      1.5
+++ lib/openat.c        22 Sep 2005 23:25:20 -0000
@@ -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 @@ rpl_openat (int fd, char const *file, in
       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 @@ rpl_openat (int fd, char const *file, in
   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 @@ rpl_openat (int fd, char const *file, in
    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 @@ 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;
     }
@@ -125,10 +127,10 @@ fdopendir (int fd)
   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 @@ fstatat (int fd, char const *file, struc
            : 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,8 +172,47 @@ fstatat (int fd, char const *file, struc
   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);
 
Index: lib/openat.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/openat.h,v
retrieving revision 1.5
diff -p -u -r1.5 openat.h
--- lib/openat.h        2 Jul 2005 09:45:08 -0000       1.5
+++ lib/openat.h        22 Sep 2005 23:25:20 -0000
@@ -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 @@ int openat (int fd, char const *file, in
 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
Index: m4/openat.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/openat.m4,v
retrieving revision 1.1
diff -p -u -r1.1 openat.m4
--- m4/openat.m4        18 Jan 2005 21:58:11 -0000      1.1
+++ m4/openat.m4        22 Sep 2005 23:25:20 -0000
@@ -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 @@ dnl with or without modifications, as lo
 
 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
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/openat-die.c    2005-09-22 15:23:23.000000000 -0700
@@ -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 ();
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]