summaryrefslogtreecommitdiff
path: root/src/copy.c
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2012-03-01 11:56:41 +0000
committerPádraig Brady <P@draigBrady.com>2012-03-06 23:43:21 +0000
commit4b4a465798a212925670cc4fef7610678d221d69 (patch)
treef0e2bac1736f4294a17e43b874883f0647e2c574 /src/copy.c
parent8195165839877eece7984dd08613323e96424cea (diff)
downloadcoreutils-4b4a465798a212925670cc4fef7610678d221d69.tar.xz
maint: refactor copy to use is_nul()
* src/dd.c: Move is_nul() from here to ... * src/system.h: ... here * src/copy.c (sparse_copy): Adjust to use the refactored is_nul()
Diffstat (limited to 'src/copy.c')
-rw-r--r--src/copy.c33
1 files changed, 7 insertions, 26 deletions
diff --git a/src/copy.c b/src/copy.c
index 541ca20cb..f63a72696 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -152,13 +152,12 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
uintmax_t max_n_read, off_t *total_n_read,
bool *last_write_made_hole)
{
- typedef uintptr_t word;
*last_write_made_hole = false;
*total_n_read = 0;
while (max_n_read)
{
- word *wp = NULL;
+ bool make_hole = false;
ssize_t n_read = read (src_fd, buf, MIN (max_n_read, buf_size));
if (n_read < 0)
@@ -175,11 +174,10 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
if (make_holes)
{
- char *cp;
-
- /* Sentinel to stop loop. */
+ /* Sentinel required by is_nul(). */
buf[n_read] = '\1';
#ifdef lint
+ typedef uintptr_t word;
/* Usually, buf[n_read] is not the byte just before a "word"
(aka uintptr_t) boundary. In that case, the word-oriented
test below (*wp++ == 0) would read some uninitialized bytes
@@ -189,35 +187,17 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
memset (buf + n_read + 1, 0, sizeof (word) - 1);
#endif
- /* Find first nonzero *word*, or the word with the sentinel. */
-
- wp = (word *) buf;
- while (*wp++ == 0)
- continue;
-
- /* Find the first nonzero *byte*, or the sentinel. */
-
- cp = (char *) (wp - 1);
- while (*cp++ == 0)
- continue;
-
- if (cp <= buf + n_read)
- /* Clear to indicate that a normal write is needed. */
- wp = NULL;
- else
+ if ((make_hole = is_nul (buf, n_read)))
{
- /* We found the sentinel, so the whole input block was zero.
- Make a hole. */
if (lseek (dest_fd, n_read, SEEK_CUR) < 0)
{
error (0, errno, _("cannot lseek %s"), quote (dst_name));
return false;
}
- *last_write_made_hole = true;
}
}
- if (!wp)
+ if (!make_hole)
{
size_t n = n_read;
if (full_write (dest_fd, buf, n) != n)
@@ -225,13 +205,14 @@ sparse_copy (int src_fd, int dest_fd, char *buf, size_t buf_size,
error (0, errno, _("writing %s"), quote (dst_name));
return false;
}
- *last_write_made_hole = false;
/* It is tempting to return early here upon a short read from a
regular file. That would save the final read syscall for each
file. Unfortunately that doesn't work for certain files in
/proc with linux kernels from at least 2.6.9 .. 2.6.29. */
}
+
+ *last_write_made_hole = make_hole;
}
return true;