Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Nov 1999 13:48:38 -0500 (EST)
From:      Zhihui Zhang <zzhang@cs.binghamton.edu>
To:        freebsd-hackers@freebsd.org
Subject:   A file with holes - a bug?
Message-ID:  <Pine.GSO.3.96.991122133521.4039A-100000@sol.cs.binghamton.edu>

next in thread | raw e-mail | index | archive | help

Please take a look at the following piece of code that creates a large
hole in a file named hole.dat.  It tries to write 0x30-0x39 both at the
front and the tail of that file, the hole is therefore in the middle.

main()
{
	char c;
	FILE * fp;

	fp = fopen("hole.dat", "w");

	for (c=0x30; c<0x3a; c++) fputc(c, fp);
	fputc('\n',fp);
	fflush(fp);                             /* XXX */
	lseek(fileno(fp),  3 * 8192, SEEK_CUR);
	for (c=0x30; c<0x3a; c++) fputc(c, fp);
	fputc('\n',fp);
	fclose(fp);
}

If I remove the fflush(fp), then the characters 0x30-0x39 will be all
written at the end of the file (use hexdump to find out), not as expected
(one at the beginning and the other at the end).  It seems to me that the
first for loop happens AFTER the lseek() statement without fflush().  Can
anyone explain this to me?  I am using FreeBSD 3.3-Release.

By the way, I also find out if you copy a file with holes into another
file, the holes in the first file will be replaced with 0s in the second
file, taking more disk space (check with du). Is there a better solution
for this? 

Any help is appreciated.

-Zhihui



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.3.96.991122133521.4039A-100000>