views:

251

answers:

3

The ext2/ext3 filesystem automatically allocate blocks when you write a sparse file, but when I no longer want some blocks of them, I found no ways to do it. It feels like using malloc() without free(). Is it possible to "free" some blocks of a sparse file? If it is, how? Don't tell me to copy it to a new file. It's too boring and needs a lot of disk space.

+2  A: 

Filesystems only allocate blocks for those parts of the sparse file that do in fact have any content. Removing those blocks would be pretty stupid because that’s your data. The other blocks can not be removed because they do not exist.

Bombe
But there really exist situations that I don't want some blocks of a file any more and want to give the space back to the filesystem.
hpsMouse
+2  A: 

The only thing you can do is call ftruncate(), to remove blocks at the end of the file.

A: 

Write zeroes to the portions you don't want.

erk