bug-coreutils
[Top][All Lists]
Advanced

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

Re: rm (remove.c): Rewrite to use fts: request for review


From: Eric Blake
Subject: Re: rm (remove.c): Rewrite to use fts: request for review
Date: Thu, 03 Sep 2009 22:51:41 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Jim Meyering on 9/1/2009 10:34 AM:
>> Would you mind pushing it to savannah's coreutils.git next branch, to make 
>> the
>> overall series easier to review, even though it won't be merged prior to 7.6?
> 
> Sure.  Just did that.
> Note that the first change set won't be pushed to master.

Now that I've pushed faccessat to gnulib, we can benefit from O(N) even on
older kernels that lack faccessat but have decent /proc/self/fd support.
Plus a simplification and compilation warning fix.  So, as long as we're
going to be rebasing the next branch anyway, I thought I'd push this now
to make it easier to review.

>     http://git.sv.gnu.org/cgit/coreutils.git/log/?h=next
> 

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqgnN0ACgkQ84KuGfSFAYA0TwCdEXPtWTUkBqq6FHNGipFU6m9H
0AkAoLevcDQAOeauFM8wgqYYUqOKFo+g
=kPT/
-----END PGP SIGNATURE-----
diff --git a/ChangeLog b/ChangeLog
index efda62b..fe02dd9 100644
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 3f13fa3..277b09d 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2192,6 +2192,7 @@ func_all_modules ()
 
   func_begin_table
   func_module close-stream
+  func_module closein
   func_module closeout
   func_module fopen-safer
   func_module fpending
diff --git a/lib/closein.c b/lib/closein.c
new file mode 100644
index 0000000..1aade1d
--- /dev/null
+++ b/lib/closein.c
@@ -0,0 +1,100 @@
+/* Close standard input, rewinding seekable stdin if necessary.
+
+   Copyright (C) 2007 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.  */
+
+#include <config.h>
+
+#include "closein.h"
+#include "closeout.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+#include "close-stream.h"
+#include "error.h"
+#include "exitfail.h"
+#include "quotearg.h"
+
+static const char *file_name;
+
+/* Set the file name to be reported in the event an error is detected
+   on stdin by close_stdin.  See also close_stdout_set_file_name, if
+   an error is detected when closing stdout.  */
+void
+close_stdin_set_file_name (const char *file)
+{
+  file_name = file;
+}
+
+/* Close standard input, rewinding any unused input if stdin is
+   seekable.  On error, issue a diagnostic and _exit with status
+   'exit_failure'.  Then call close_stdout.
+
+   Most programs can get by with close_stdout.  close_stdin is only
+   needed when a program wants to guarantee that partially read input
+   from seekable stdin is not consumed, for any subsequent clients.
+   For example, POSIX requires that these two commands behave alike:
+
+     (sed -ne 1q; cat) < file
+     tail -n 1 file
+
+   Since close_stdin is commonly registered via 'atexit', POSIX
+   and the C standard both say that it should not call 'exit',
+   because the behavior is undefined if 'exit' is called more than
+   once.  So it calls '_exit' instead of 'exit'.  If close_stdin
+   is registered via atexit before other functions are registered,
+   the other functions can act before this _exit is invoked.
+
+   Applications that use close_stdout should flush any streams other
+   than stdin, stdout, and stderr before exiting, since the call to
+   _exit will bypass other buffer flushing.  Applications should be
+   flushing and closing other streams anyway, to check for I/O errors.
+   Also, applications should not use tmpfile, since _exit can bypass
+   the removal of these files.
+
+   It's important to detect such failures and exit nonzero because many
+   tools (most notably `make' and other build-management systems) depend
+   on being able to detect failure in other tools via their exit status.  */
+
+void
+close_stdin (void)
+{
+  bool fail = false;
+  if (close_stream (stdin) != 0)
+    {
+      char const *close_error = _("error closing file");
+      if (file_name)
+       error (0, errno, "%s: %s", quotearg_colon (file_name),
+              close_error);
+      else
+       error (0, errno, "%s", close_error);
+
+      /* Defer failure until after closing stdout, since the output
+        can still usefully be flushed.  */
+      fail = true;
+    }
+
+  close_stdout ();
+
+  if (fail)
+    _exit (exit_failure);
+}
diff --git a/lib/closein.h b/lib/closein.h
new file mode 100644
index 0000000..43a713d
--- /dev/null
+++ b/lib/closein.h
@@ -0,0 +1,33 @@
+/* Close standard input, rewinding seekable stdin if necessary.
+
+   Copyright (C) 2007 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.  */
+
+#ifndef _GL_CLOSEIN_H
+# define _GL_CLOSEIN_H 1
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+void close_stdin_set_file_name (const char *file);
+void close_stdin (void);
+
+# ifdef __cplusplus
+}
+# endif
+
+#endif
diff --git a/m4/closein.m4 b/m4/closein.m4
new file mode 100644
index 0000000..bf8ff38
--- /dev/null
+++ b/m4/closein.m4
@@ -0,0 +1,13 @@
+# closein.m4 serial 1
+dnl Copyright (C) 2007 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_CLOSEIN],
+[
+  AC_LIBOBJ([closein])
+
+  dnl Prerequisites of lib/closein.c.
+  :
+])
diff --git a/modules/closein b/modules/closein
new file mode 100644
index 0000000..fefeae3
--- /dev/null
+++ b/modules/closein
@@ -0,0 +1,26 @@
+Description:
+Close all standard streams, resetting seekable stdin if needed, and exiting 
with a diagnostic on error.
+
+Files:
+lib/closein.h
+lib/closein.c
+m4/closein.m4
+
+Depends-on:
+closeout
+fflush
+stdbool
+
+configure.ac:
+gl_CLOSEIN
+
+Makefile.am:
+
+Include:
+"closein.h"
+
+License:
+GPL
+
+Maintainer:
+Eric Blake

reply via email to

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