我试图为文件操作预先分配磁盘空间,但是,我遇到了一个奇怪的问题,当我调用它来为以追加模式打开的文件分配磁盘空间时,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;
}预期的结果应该是,
## Test loop 0
## Test loop 1
## Test loop 2
## Test loop 3
## Test loop 4
hello world然而,上述程序的结果是,
## Test loop 0
## Test loop 1
## Test loop 2
## Test loop 3
## Test loop 4
^@hello world那么,"^@“是什么?
信息显示,
INFO: sizeof 'append.txt' is 75 Bytes.
INFO: sizeof 'append.txt' is 76 Bytes.有什么线索吗?
谢谢
发布于 2013-06-20 23:49:45
快速回答
是的,posix_fallocate确实可以处理以附加模式打开的文件。如果您的文件系统支持fallocate系统调用。如果您的文件系统不支持它,那么glibc仿真将在追加模式的末尾添加一个0字节。
更多信息
这是个奇怪的故事,让我感到很困惑。我通过使用strace程序找到了答案,该程序显示正在执行哪些系统调用。
看看这个:
假定位(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库想在这里帮助你。fallocate系统调用显然没有在您的文件系统上实现,所以GLibC是通过使用pwrite在请求的分配结束时写入0字节来模拟它,从而扩展了该文件。
这在正常的写入模式下工作得很好。但是在附加模式下,写入总是在文件的末尾完成,因此pwrite在文件末尾写入一个0字节。
不是故意的。可能是GNU C库的错误。
看起来ext4确实支持fallocate。如果我将文件写入/tmp,它就能工作。它在我的主目录中失败,因为我在Ubuntu中使用了一个加密的主目录,其中包含了ecryptfs文件系统。
发布于 2013-06-20 23:41:20
每POSIX
如果offset+ len超出了当前文件大小,那么posix_fallocate()将将文件大小调整为offset+ len。否则,不应更改文件大小。
因此,在附加模式下使用posix_fallocate是没有意义的,因为它将扩展文件的大小(填充为空字节),随后的写入将在那些空字节之后进行,在尚未保留的空间中进行。
至于为什么它只将文件扩展一个字节,您确定这是正确的吗?你量过身了吗?这听起来像是实现中的一个bug。
https://stackoverflow.com/questions/17225384
复制相似问题