Path: utzoo!attcan!telly!nebulus!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: comp.unix.questions Subject: Re: sparse files Message-ID: <1989Dec13.133053.7974@druid.uucp> Date: 13 Dec 89 13:30:53 GMT References: <21581@adm.BRL.MIL> <235@dg.dg.com> <2700@auspex.auspex.com> <1989Dec10.170841.26798@druid.uucp> <244@estinc.UUCP> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 53 In article <244@estinc.UUCP> fnf@estinc.UUCP (Fred Fish) writes: >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. > 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@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 |