bug-coreutils
[Top][All Lists]
Advanced

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

Re: [Linux-NTFS-Dev] Re: cp --sparse might corrupt DEST


From: Jim Meyering
Subject: Re: [Linux-NTFS-Dev] Re: cp --sparse might corrupt DEST
Date: Wed, 10 Mar 2004 11:07:34 +0100

You noticed that this command

  cp --sparse=always F.img /dev/hda1

would mistakenly try to use lseek to create
holes in the non-regular destination.

Since --sparse=WHEN affects how sequences of zero bytes
in the source file are interpreted, and since --sparse=auto
is already restricted to regular files, I've done as you suggest
and restricted --sparse=always, too.

Here's the patch:


Index: src/copy.c
===================================================================
RCS file: /fetish/cu/src/copy.c,v
retrieving revision 1.157
retrieving revision 1.158
diff -u -p -u -r1.157 -r1.158
--- src/copy.c  7 Feb 2004 16:00:28 -0000       1.157
+++ src/copy.c  6 Mar 2004 17:40:56 -0000       1.158
@@ -214,7 +214,7 @@ copy_reg (const char *src_path, const ch
   int return_val = 0;
   off_t n_read_total = 0;
   int last_write_made_hole = 0;
-  int make_holes = (x->sparse_mode == SPARSE_ALWAYS);
+  int make_holes = 0;
 
   source_desc = open (src_path, O_RDONLY);
   if (source_desc < 0)
@@ -286,6 +286,11 @@ copy_reg (const char *src_path, const ch
 
   buf_size = ST_BLKSIZE (sb);
 
+  /* Even with --sparse=always, try to create holes only
+     if the destination is a regular file.  */
+  if (x->sparse_mode == SPARSE_ALWAYS && S_ISREG (sb.st_mode))
+    make_holes = 1;
+
 #if HAVE_STRUCT_STAT_ST_BLOCKS
   if (x->sparse_mode == SPARSE_AUTO && S_ISREG (sb.st_mode))
     {
Index: doc/coreutils.texi
===================================================================
RCS file: /fetish/cu/doc/coreutils.texi,v
retrieving revision 1.163
retrieving revision 1.164
diff -u -p -u -r1.163 -r1.164
--- doc/coreutils.texi  10 Mar 2004 06:50:59 -0000      1.163
+++ doc/coreutils.texi  10 Mar 2004 09:51:26 -0000      1.164
@@ -6348,17 +6348,25 @@ reads these as zeroes.  This can both sa
 increase speed, since many binary files contain lots of consecutive zero
 bytes.  By default, @command{cp} detects holes in input source files via a 
crude
 heuristic and makes the corresponding output file sparse as well.
+Only regular files may be sparse.
 
 The @var{when} value can be one of the following:
 @table @samp
 @item auto
-The default behavior: the output file is sparse if the input file is sparse.
+The default behavior: if the input file is sparse, attempt to make
+the output file sparse, too.  However, if an output file exists but
+refers to a non-regular file, then do not attempt to make it sparse.
 
 @item always
-Always make the output file sparse.  This is useful when the input
-file resides on a filesystem that does not support sparse files (the
-most notable example is @samp{efs} filesystems in SGI IRIX 5.3 and
-earlier), but the output file is on another type of filesystem.
+For each sufficiently long sequence of zero bytes in the input file,
+attempt to create a corresponding hole in the output file, even if the
+input file does not appear to be sparse.
+This is useful when the input file resides on a filesystem
+that does not support sparse files
+(for example,  @samp{efs} filesystems in SGI IRIX 5.3 and earlier),
+but the output file is on a type of filesystem that does support them.
+Holes may be created only in regular files, so if the destination file
+is of some other type, @command{cp} does not even try to make it sparse.
 
 @item never
 Never make the output file sparse.




reply via email to

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