sparse files

Fred Fish fnf at estinc.UUCP
Wed Dec 13 05:25:09 AEST 1989


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.

-Fred
-- 
# Fred Fish, 1835 E. Belmont Drive, Tempe, AZ 85284,  USA
# 1-602-491-0048           asuvax!{nud,mcdphx}!estinc!fnf



More information about the Comp.unix.questions mailing list