Did you ever wonder how some files got bigger when copied?

These files are called sparse files. Sparse files are files with embedded null data, instead of allocating disk blocks for these files an offset pointer is used. This mechanism allows the system to conserve on disk allocations and a speedup on read operations.

The following is a C program that demonstrate how a sparse is created.

/* Sparse file creation */

#include <stdio.h>

void main()

{

FILE *fp;

fp = fopen("sparse.file", "w");

fseek(fp,102396,SEEK_CUR); /* 104 KB – 4 bytes */

fprintf(fp,"text");

fclose(fp);

}

$ cc -o sparse sparse.c

$ ./sparse

$ ls -l sparse.file

-rw-r--r-- 1 hgo staff 102400 Jan 13 15:52 sparse.file

$ du -h sparse.file

8K sparse.file

The ls command shows a file size of 102400 bytes, but the du command shows only of 8 KB of allocated disk space.

$ cp sparse.file sparse.file-copied

$ ls -l sparse.file*

-rw-r--r-- 1 hgo staff 102400 Jan 13 15:52 sparse.file

-rw-r--r-- 1 hgo staff 102400 Jan 13 15:54 sparse.file-copied

$ du -h sparse.file*

8K sparse.file

104K sparse.file-copied

The disk allocations of the copied files balloons to 104 KB. This happens since the cp commands treats the file as a byte stream.

If you want to duplicate this file as a sparse file, you have to use some other utilities such as GNU tar. The GNU tar has the option –S or –sparse. This option will allow you to duplicate a sparse file with fidelity.

$ mkdir copy

$ /usr/local/bin/tar -Scf - sparse.file | ( cd copy; /usr/local/bin/tar -xf -)

$ ls -l copy/sparse.file

-rw-r--r-- 1 hgo staff 102400 Jan 13 15:52 copy/sparse.file

$ du -h copy/sparse.file

8K copy/sparse.file

$ tar -cf - sparse.file | ( cd copy; tar -xf -)

$ ls -l copy/sparse.file

-rw-r--r-- 1 hgo staff 102400 Jan 13 15:52 copy/sparse.file

$ du -h copy/sparse.file

104K copy/sparse.file

The first example with the –S options allocated only 4KB of disk space while the second example allocated the full 104 KB.


  • re: query on sparse file
    By : Hermelito Go ( Fri Nov 4 09:53:55 2005 )

  • query on sparse file
    By : anonymous ( Fri Nov 4 02:32:42 2005 )

  • *****
    By : Laurent Schneider ( Thu Apr 28 09:45:32 2005 )


  • Name :
    Password :
    E-mail :
    Subject :
    Comments :



    UNIXguide.net
    Suggest a Site