Here's a revised version of my patch.

Is there no way to deal with error messages normally, i.e. via
gnulib's strerror? Then the error-returning copy_file_preserving could
replace the aborting version, and users who want to could check the
return code and issue errors in the usual way.

If that's not possible, then I suggest the unwieldy but less
misleading name copy_file_preserving_with_error rather than
copy_file_preserving_error for the error-returning version.

-- 
http://rrt.sc3d.org
From 0216e90bc352358669bab03dede33e2be9f36149 Mon Sep 17 00:00:00 2001
From: Reuben Thomas <r...@sc3d.org>
Date: Tue, 10 Jan 2012 19:45:12 +0000
Subject: [PATCH] copy-file: add error-code-returning version.

* lib/copy-file.c: Add code.
* lib/copy-file.h: Add copy_file_preserving_error and return
codes.
* tests/test-copy-file.c: Add tests.
---
 ChangeLog              |    8 ++++
 lib/copy-file.c        |  109 +++++++++++++++++++++++++++++++++++++++++------
 lib/copy-file.h        |   13 ++++++
 tests/test-copy-file.c |   13 ++++++
 4 files changed, 129 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 212aeaf..7faafdf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2012-01-10  Reuben Thomas  <r...@sc3d.org>
 
+	copy-file: add error-code-returning version.
+	* lib/copy-file.c: Add code.
+	* lib/copy-file.h: Add copy_file_preserving_error and return
+	codes.
+	* tests/test-copy-file.c: Add tests.
+
+2012-01-10  Reuben Thomas  <r...@sc3d.org>
+
 	users.txt: order package names lexicographically.
 	* users.txt: Order package names lexicographically.
 
diff --git a/lib/copy-file.c b/lib/copy-file.c
index 8e79091..3f5b4f7 100644
--- a/lib/copy-file.c
+++ b/lib/copy-file.c
@@ -53,47 +53,70 @@
 
 enum { IO_SIZE = 32 * 1024 };
 
-void
-copy_file_preserving (const char *src_filename, const char *dest_filename)
+
+int
+_copy_file_preserving (const char *src_filename, const char *dest_filename)
 {
   int src_fd;
   struct stat statbuf;
   int mode;
   int dest_fd;
+  int err = 0;
   char *buf = xmalloc (IO_SIZE);
 
   src_fd = open (src_filename, O_RDONLY | O_BINARY);
-  if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
-    error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
-           src_filename);
+  if (src_fd < 0)
+    {
+      err = GL_COPY_ERR_OPEN_READ;
+      goto error;
+    }
+  if (fstat (src_fd, &statbuf) < 0)
+    {
+      err = GL_COPY_ERR_OPEN_READ;
+      goto error_src;
+    }
 
   mode = statbuf.st_mode & 07777;
 
   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
   if (dest_fd < 0)
-    error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
-           dest_filename);
+    {
+      err = GL_COPY_ERR_OPEN_BACKUP_WRITE;
+      goto error_src;
+    }
 
   /* Copy the file contents.  */
   for (;;)
     {
       size_t n_read = safe_read (src_fd, buf, IO_SIZE);
       if (n_read == SAFE_READ_ERROR)
-        error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
+        {
+          err = GL_COPY_ERR_READ;
+          goto error_src_dest;
+        }
       if (n_read == 0)
         break;
 
       if (full_write (dest_fd, buf, n_read) < n_read)
-        error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
+        {
+          err = GL_COPY_ERR_WRITE;
+          goto error_src_dest;
+        }
     }
 
   free (buf);
 
 #if !USE_ACL
   if (close (dest_fd) < 0)
-    error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
+    {
+      err = GL_COPY_ERR_WRITE;
+      goto error_src;
+    }
   if (close (src_fd) < 0)
-    error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
+    {
+      err = GL_COPY_ERR_AFTER_READ;
+      goto error;
+    }
 #endif
 
   /* Preserve the access and modification times.  */
@@ -123,15 +146,73 @@ copy_file_preserving (const char *src_filename, const char *dest_filename)
   /* Preserve the access permissions.  */
 #if USE_ACL
   if (copy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
-    exit (EXIT_FAILURE);
+    {
+      err = GL_COPY_ERR_ACL;
+      goto error_src_dest;
+    }
 #else
   chmod (dest_filename, mode);
 #endif
 
 #if USE_ACL
   if (close (dest_fd) < 0)
-    error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
+    {
+      err = GL_COPY_ERR_WRITE;
+      goto error_src;
+    }
   if (close (src_fd) < 0)
-    error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
+    {
+      err = GL_COPY_ERR_AFTER_READ;
+      goto error;
+    }
 #endif
+
+  return 0;
+
+ error_src_dest:
+  close (dest_fd);
+ error_src:
+  close (src_fd);
+ error:
+  return err;
+}
+
+void
+copy_file_preserving (const char *src_filename, const char *dest_filename)
+{
+  switch (_copy_file_preserving (src_filename, dest_filename))
+    {
+    case 0:
+      return;
+
+    case GL_COPY_ERR_OPEN_READ:
+      error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
+             src_filename);
+
+    case GL_COPY_ERR_OPEN_BACKUP_WRITE:
+      error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
+             dest_filename);
+
+    case GL_COPY_ERR_READ:
+      error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
+
+    case GL_COPY_ERR_WRITE:
+      error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
+
+    case GL_COPY_ERR_AFTER_READ:
+      error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
+
+    case GL_COPY_ERR_ACL:
+      error (EXIT_FAILURE, errno, _("error copying the ACL to \"%s\""), dest_filename);
+
+    default:
+      exit (EXIT_FAILURE);
+    }
+}
+
+
+int
+copy_file_preserving_error (const char *src_filename, const char *dest_filename)
+{
+  return _copy_file_preserving (src_filename, dest_filename);
 }
diff --git a/lib/copy-file.h b/lib/copy-file.h
index b28ea37..b577d01 100644
--- a/lib/copy-file.h
+++ b/lib/copy-file.h
@@ -28,6 +28,19 @@ extern "C" {
    Exit upon failure.  */
 extern void copy_file_preserving (const char *src_filename, const char *dest_filename);
 
+/* Error-returning version of copy_file_preserving. */
+extern int copy_file_preserving_error (const char *src_filename, const char *dest_filename);
+
+/* Error codes returned by copy_file_preserving_error. */
+enum {
+  GL_COPY_ERR_OPEN_READ = -1,
+  GL_COPY_ERR_OPEN_BACKUP_WRITE = -2,
+  GL_COPY_ERR_READ = -3,
+  GL_COPY_ERR_WRITE = -4,
+  GL_COPY_ERR_AFTER_READ = -5,
+  GL_COPY_ERR_ACL = -6
+};
+
 
 #ifdef __cplusplus
 }
diff --git a/tests/test-copy-file.c b/tests/test-copy-file.c
index 23c7408..ae3cf42 100644
--- a/tests/test-copy-file.c
+++ b/tests/test-copy-file.c
@@ -20,6 +20,10 @@
 
 #include "copy-file.h"
 
+#include <assert.h>
+#include <sys/stat.h>
+#include <stdio.h>
+
 #include "progname.h"
 #include "macros.h"
 
@@ -38,5 +42,14 @@ main (int argc, char *argv[])
 
   copy_file_preserving (file1, file2);
 
+  assert (chmod (file2, 0400) == 0);
+  assert (copy_file_preserving_error (file1, file2) == GL_COPY_ERR_OPEN_BACKUP_WRITE);
+  assert (remove (file2) == 0);
+  assert (copy_file_preserving_error (file2, file1) == GL_COPY_ERR_OPEN_READ);
+  assert (copy_file_preserving_error (file1, "/dev/full") == GL_COPY_ERR_WRITE);
+
+  /* FIXME: The following return codes have yet to be tested:
+     GL_COPY_ERR_READ, GL_COPY_ERR_AFTER_READ, GL_COPY_ERR_ACL */
+
   return 0;
 }
-- 
1.7.5.4

Reply via email to