Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!asuvax!mcdphx!estinc!fnf From: fnf@estinc.UUCP (Fred Fish) Newsgroups: comp.unix.questions Subject: Re: sparse files Message-ID: <244@estinc.UUCP> Date: 12 Dec 89 18:25:09 GMT References: <21581@adm.BRL.MIL> <235@dg.dg.com> <2700@auspex.auspex.com> <1989Dec10.170841.26798@druid.uucp> Reply-To: fnf@estinc.UUCP (Fred Fish) Organization: Enhanced Software Technologies, Inc. Lines: 41 In article <1989Dec10.170841.26798@druid.uucp> darcy@druid.UUCP (D'Arcy J.M. Cain) writes: >In article <2700@auspex.auspex.com> guy@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