O_APPEND 一个文件上的标志使得 read() 系统调用在其他文件上表现得很奇怪
O_APPEND flag on one file makes read() system call behave weirdly on other files
我有一个 C 程序,它在目录中创建指定数量的文件 (name-myfiles)。然后删除所有文件。然后创建一个非常大的文件(名称 -appfile),追加它,截断它。
以上几轮操作循环进行。
为了验证每个 write
,我从写入数据的相同偏移量读取目标文件。如果测试不对大文件使用 O_APPEND 标志,则此验证部分(read())进行得非常顺利。但否则,read 开始表现出奇怪的行为。第一轮测试完成后,read 读取的字节数为 0 或小于缓冲区大小,或者如果字节数等于缓冲区大小,则两个缓冲区的内容不匹配。
给定 destflg=O_RDWR | O_APPEND | O_CREAT;
,我假设两件事:
- 您正在以追加模式打开文件
- 您 运行 在 Linux。
pwrite()
在 Linux 上坏了。
来自the Linux pwrite()
man page:
BUGS
POSIX requires that opening a file with the O_APPEND
flag should have
no effect on the location at which pwrite()
writes data. However, on
Linux, if a file is opened with O_APPEND
, pwrite()
appends data to
the end of the file, regardless of the value of offset
.
我有一个 C 程序,它在目录中创建指定数量的文件 (name-myfiles)。然后删除所有文件。然后创建一个非常大的文件(名称 -appfile),追加它,截断它。 以上几轮操作循环进行。
为了验证每个 write
,我从写入数据的相同偏移量读取目标文件。如果测试不对大文件使用 O_APPEND 标志,则此验证部分(read())进行得非常顺利。但否则,read 开始表现出奇怪的行为。第一轮测试完成后,read 读取的字节数为 0 或小于缓冲区大小,或者如果字节数等于缓冲区大小,则两个缓冲区的内容不匹配。
给定 destflg=O_RDWR | O_APPEND | O_CREAT;
,我假设两件事:
- 您正在以追加模式打开文件
- 您 运行 在 Linux。
pwrite()
在 Linux 上坏了。
来自the Linux pwrite()
man page:
BUGS
POSIX requires that opening a file with the
O_APPEND
flag should have no effect on the location at whichpwrite()
writes data. However, on Linux, if a file is opened withO_APPEND
,pwrite()
appends data to the end of the file, regardless of the value ofoffset
.