考虑一个稀疏文件,其中 1 写入文件的一部分。
我想为这些 1 回收磁盘上的实际空间,因为我不再需要稀疏文件的那部分。包含这些 1 的文件部分应该成为一个“洞”,就像在 1 本身被写入之前一样。
为此,我将该区域清除为 0。这不会回收磁盘上的块。
我实际上如何使稀疏文件再次变得稀疏?
这个问题与这个问题类似,但该问题没有公认的答案。
考虑在普通 Linux 服务器上运行的以下事件序列:
$ cat /tmp/test.c
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char **argv) {
    int fd;
    char c[1024];
    memset(c,argc==1,1024);
    fd = open("test",O_CREAT|O_WRONLY,0777);
    lseek(fd,10000,SEEK_SET);
    write(fd,c,1024);
    close(fd);
    return 0;
}
$ gcc -o /tmp/test /tmp/test.c
$ /tmp/test
$ hexdump -C ./test
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002710  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|
*
00002b10
$ du -B1 test; du -B1 --apparent-size test
4096        test
11024       test
$ /tmp/test clear
$ hexdump -C ./test
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002b10
$ du -B1 test; du -B1 --apparent-size test
4096        test
11024       test
# NO CHANGE IN SIZE.... HMM....
编辑 -
让我进一步说明我不想重写文件、复制文件等。如果无法以某种方式就地释放先前分配的块,那就这样吧,但我想确定这是否真的可行或不是。在这一点上似乎“不,不是”。我想我正在寻找sys_punchholeLinux(我刚刚偶然发现的讨论)。