如何从ext2 / ext3文件系统的稀疏文件中删除一些块 [英] How to remove some blocks from a sparse file on a ext2/ext3 filesystem
问题描述
是否可以释放稀疏文件的某些块?如果是,怎么样?
不要告诉我把它复制到一个新的文件。它太无聊了,需要大量的磁盘空间。
自Linux 2.6.38以来,有一个标志 fallocate
所谓的 FALLOC_FL_PUNCH_HOLE
它应该做你想做的,即释放任意位置的文件空间。
fallocate(fd,FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,offset,len);
会在描述符fd指示的文件中打一个洞。该洞将从 offset
开始,长度 len
,均以字节为单位。只有整块将被删除,部分块将被清零。
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.
Since Linux 2.6.38, there is a flag to fallocate
called FALLOC_FL_PUNCH_HOLE
which should do what you want, i.e. deallocate file space at arbitrary locations.
fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, len);
will punch a hole into the file indicated by descriptor fd. The hole will start at offset
and have length len
, both measured in bytes. Only whole blocks will actually be removed, partial blocks will be zeroed out instead.
这篇关于如何从ext2 / ext3文件系统的稀疏文件中删除一些块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!