coreutils
[Top][All Lists]
Advanced

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

[PATCH] copy: don't let a failed lseek go undiagnosed


From: Jim Meyering
Subject: [PATCH] copy: don't let a failed lseek go undiagnosed
Date: Sat, 05 Feb 2011 22:42:05 +0100

FYI,

>From 9f618068755b51d19b22c52bc4a2f8084946948e Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sat, 5 Feb 2011 22:40:57 +0100
Subject: [PATCH] copy: don't let a failed lseek go undiagnosed

Upon failed lseek, sparse_copy_finalize would mistakenly return true.
Admittedly, that is very unlikely, since that particular lseek
is attempted only if the preceding call to sparse_copy induced
a hole at EOF (via lseek on the destination FD).  However, now
that sparse_copy has an output parameter, N_READ, there is no
longer any reason to call lseek (fd, 0, SEEK_CUR), so...
* src/copy.c (sparse_copy_finalize): Remove the function.
(copy_reg): Call ftruncate with n_read, rather than
sparse_copy_finalize with its now-unnecessary lseek.
Lasse Collin spotted the bug in sparse_copy_finalize.
---
 src/copy.c |   18 ++----------------
 1 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/src/copy.c b/src/copy.c
index f429c00..9182c16 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -233,21 +233,6 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t 
buf_size,
   return true;
 }

-/* If the file ends with a `hole' (i.e., if sparse_copy set wrote_hole_at_eof),
-   call this function to record the length of the output file.  */
-static bool
-sparse_copy_finalize (int dest_fd, char const *dst_name)
-{
-  off_t len = lseek (dest_fd, 0, SEEK_CUR);
-  if (0 <= len && ftruncate (dest_fd, len) < 0)
-    {
-      error (0, errno, _("truncating %s"), quote (dst_name));
-      return false;
-    }
-
-  return true;
-}
-
 /* Perform the O(1) btrfs clone operation, if possible.
    Upon success, return 0.  Otherwise, return -1 and set errno.  */
 static inline int
@@ -1000,8 +985,9 @@ copy_reg (char const *src_name, char const *dst_name,
                           UINTMAX_MAX, &n_read,
                           &wrote_hole_at_eof)
            || (wrote_hole_at_eof &&
-               ! sparse_copy_finalize (dest_desc, dst_name)))
+               ftruncate (dest_desc, n_read) < 0))
         {
+          error (0, errno, _("failed to extend %s"), quote (dst_name));
           return_val = false;
           goto close_src_and_dst_desc;
         }
--
1.7.4.2.g597a6



reply via email to

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