Sparse Files ?

Jim Frost madd at bu-cs.BU.EDU
Mon May 1 07:16:42 AEST 1989


In article <19342 at adm.BRL.MIL> mark at ria-emh2.army.mil (Mark D. McKamey IM SA) writes:
|     What is the definition of a "Sparse file" in the UNIX world?

UNIX stores data in files by maintaining pointers to data blocks.  By
allocating only those blocks which have actually been written to, you
can create files which appear to be larger than they actually are.
These are usually created by lseek()ing and write()ing.

When you create an empty file, the system allocates a file information
block (called an inode) which contains a small list of block pointers.
This list is initially blank.  When we write into the file, the system
gets data blocks and sets the appropriate block pointer to point to
the block.

When we just create a file we get seomthing like this:

	ptr1 -> null
	ptr2 -> null
	ptr3 -> null

When we write to that file we get something like this:

	ptr1 -> block1
	ptr2 -> null
	ptr3 -> null

We deposit data into block1 until block1 is filled, then get another
block and set ptr2 to point to it.  If instead of just opening and
writing the file you open, seek into the file somewhere, and then
write, you can get something like:

	ptr1 -> null
	ptr2 -> block1
	ptr3 -> null

To the user it looks like he has a two-block file which has one block
of zeros (the system returns zeros for reads into null blocks), but to
the system he has only a one-block file.  This difference can add up
to a considerable savings in some cases.  For the normal case, this
behavior affects nothing.

jim frost
madd at bu-it.bu.edu



More information about the Comp.unix.questions mailing list