如何从ext2 / ext3文件系统的稀疏文件中删除一些块 [英] How to remove some blocks from a sparse file on a ext2/ext3 filesystem

查看:183
本文介绍了如何从ext2 / ext3文件系统的稀疏文件中删除一些块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ext2 / ext3文件系统在你编写一个稀疏文件的时候会自动分配块,但是当我不再需要它们的块的时候,我发现没有办法做到这一点。感觉就像使用malloc()没有free()。
是否可以释放稀疏文件的某些块?如果是,怎么样?
不要告诉我把它复制到一个新的文件。它太无聊了,需要大量的磁盘空间。

解决方案

自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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆