sparse files

D'Arcy J.M. Cain darcy at druid.uucp
Thu Dec 14 00:30:53 AEST 1989


In article <244 at estinc.UUCP> fnf at estinc.UUCP (Fred Fish) writes:
>In article <1989Dec10.170841.26798 at druid.uucp> darcy at druid.UUCP (D'Arcy J.M. Cain) writes:
>>In article <2700 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
>>>Not in general, anyway.  At least the first version of AIX for the RT PC
>>>claimed, in its documentation, that it had an "fclear()" call to punch
>>>holes in files; I think this may show up in future releases of other
>>>UNIXes as well.
>>>
>>Seems simple enough to write a utility.  The core would be something like
>>the following:
>
>I think Guy (and others) were talking about punching holes in-place without
>having to make a copy of the file.  Recreating sparse files by copying is
>much easier.
>
>The necessary changes to add preservation of sparseness (or creation of
>sparseness from nonsparse files) are fairly trivial and can be probably
>be added to cp, tar, cpio, etc in a matter of a few minutes.  Here is the
>relevant code from BRU (Backup and Restore Utility) with some minor changes
>to simplify variable names:
>
>	if (!allnulls (buffer, nbytes)) {
>	    iobytes = write (fildes, buffer, nbytes);
>	} else {
>	    if (lseek (fildes, nbytes, 1) != -1) {
>		iobytes = nbytes;
>	    } else {
>		bru_message (MSG_SEEK, fname);
>		iobytes = write (fildes, buffer, nbytes);
>	    }
>	}
>
>Note that the file starts off truncated to zero length, so the lseeks only
>extend the file from the current last written position.  By falling back
>to doing writes if the seek fails, the code is portable to systems where
>the files cannot be extended with holes (or nulls) by seeking, at the
>expense of performing occasional failing lseeks.
>
In my example, I only intended to show a method of creating a sparse file
given an input stream of characters.  This is why I left out any mention
of opening the file in_fp.  It could in fact be standard input and in fact
that is how I thought of it myself.  Your example doesn't seem to do it any
differently except that you assume that the input stream is hard coded to
come from a particular program (tar, cpio etc.)  You also flesh it out a
little better to test for seek failures but you use low-level routines.  I
used stdio to make it more portable across systems that didn't support low
level stuff but did support sparse files.

-- 
D'Arcy J.M. Cain (darcy at druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
No disclaimers.  I agree with me   |



More information about the Comp.unix.questions mailing list