posix_fallocate是否可以使用追加模式打开文件?

时间:2013-06-20 23:15:55

标签: c++ c linux

我正在尝试为文件操作预分配磁盘空间,但是,我遇到一个奇怪的问题,即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.

任何线索?

由于

2 个答案:

答案 0 :(得分:5)

快速回答

是的,posix_fallocate 可以处理在APPEND模式下打开的文件。如果您的文件系统支持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文件系统加密的主目录

答案 1 :(得分:1)

POSIX

  

如果offset + len超出当前文件大小,则posix_fallocate()应将文件大小调整为offset + len。否则,不得更改文件大小。

因此将posix_fallocate与append模式一起使用是没有意义的,因为它将扩展文件的大小(用空字节填充),后续写入将在那些空字节之后,在空间中进行尚未保留。

至于为什么它只将文件扩展一个字节,你确定这是正确的吗?你测量过吗?这听起来像是实施中的一个错误。