我正在尝试为文件操作预分配磁盘空间,但是,我遇到一个奇怪的问题,即posix_fallocate只调用一个字节,当我调用它来为使用追加模式打开的文件分配磁盘空间时,文件内容也是意外的.有谁知道这个问题?我的测试代码是,
#include <cstdio>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cerrno>
int main(int argc, char **argv)
{
FILE *fp = fopen("append.txt", "w");
for (int i = 0; i < 5; ++i)
fprintf(fp, "## Test loop %d\n", i);
fclose(fp);
sleep(1);
int fid = open("append.txt", O_WRONLY | O_APPEND);
struct stat status;
fstat(fid, &status);
printf("INFO: sizeof 'append.txt' is %ld Bytes.\n", status.st_size);
int ret = posix_fallocate(fid, (off_t)status.st_size, 1024);
if (ret) {
switch (ret) {
case EBADF:
fprintf(stderr, "ERROR: %d is not a valid file descriptor, or is not opened for writing.\n", fid);
break;
case EFBIG:
fprintf(stderr, "ERROR: exceed the maximum file size.\n");
break;
case ENOSPC:
fprintf(stderr, "ERROR: There is not enough space left on the device\n");
break;
default:
break;
}
}
fstat(fid, &status);
printf("INFO: sizeof 'append.txt' is %ld Bytes.\n", status.st_size);
char *hello = "hello world\n";
write(fid, hello, 12);
close(fid);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期的结果应该是,
## Test loop 0
## Test loop 1
## Test loop 2
## Test loop 3
## Test loop 4
hello world
Run Code Online (Sandbox Code Playgroud)
但是,上述程序的结果是,
## Test loop 0
## Test loop 1
## Test loop 2
## Test loop 3
## Test loop 4
^@hello world
Run Code Online (Sandbox Code Playgroud)
那么,什么是"^ @"?
消息显示,
INFO: sizeof 'append.txt' is 75 Bytes.
INFO: sizeof 'append.txt' is 76 Bytes.
Run Code Online (Sandbox Code Playgroud)
有线索吗?
谢谢
是的,posix_fallocate 并在追加模式打开文件.如果您的文件系统支持fallocate系统调用.如果您的文件系统不支持它,则glibc仿真会在APPEND模式下向结尾添加单个0字节.
这是一个奇怪的,真的让我很困惑.我通过使用strace程序来找到答案,该程序显示正在进行的系统调用.
看一下这个:
fallocate(3,0,74,1000)= -1 EOPNOTSUPP(不支持操作)
fstat(3,{st_mode = S_IFREG | 0664,st_size = 75,...})= 0
fstatfs(3,{f_type = 0xf15f, f_bsize = 4096,f_blocks = 56777565,f_bfree = 30435527,f_bavail = 27551380,f_files = 14426112,f_ffree = 13172614,f_fsid = {1863489073,-1456395543},f_namelen = 143,f_frsize = 4096})= 0
pwrite(3,"\ 0",1,1073)= 1
看起来GNU C Library正试图在这里帮助你.该fallocate系统调用显然不是在你的文件系统实现的,因此glibc的是通过使用模拟它pwrite给了在分配请求的最后写上0字节,从而延长该文件.
这在正常写入模式下工作正常.但是在APPEND模式下,写操作始终在文件末尾完成,因此最后pwrite写入一个0字节.
不是故意的.可能是GNU C库的错误.
看起来ext4确实支持fallocate.如果我将文件写入/ tmp它可以工作.它在我的主目录中失败,因为我在Ubuntu中使用ecryptfs文件系统的加密主目录