MacOS下pwrite无法O_APPEND的问题
这个问题来自Swoole
的一个issue
。
有如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <sys/file.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h>
int main(int argc, char const *argv[]) { int flags = 0; int fd;
flags = O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); pwrite(fd, "first line\n", strlen("first line\n"), 0);
flags = O_APPEND | O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); pwrite(fd, "second line\n", strlen("second line\n"), 0);
flags = O_APPEND | O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); pwrite(fd, "third line\n", strlen("third line\n"), 0);
return 0; }
|
此时,test.txt
文件里面的内容是:
我们发现,这实际上没有追加,而是覆盖了之前写入的内容。也就意味着pwrite
的offset
和O_APPEND
没有一起起到作用。
我们换成write
来测试追加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <sys/file.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h>
int main(int argc, char const *argv[]) { int flags = 0; int fd;
flags = O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); write(fd, "first line\n", strlen("first line\n"));
flags = O_APPEND | O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); write(fd, "second line\n", strlen("second line\n"));
flags = O_APPEND | O_CREAT | O_WRONLY; fd = open("test.txt", flags, 0644); write(fd, "third line\n", strlen("third line\n"));
return 0; }
|
此时,test.txt
文件里面的内容是:
1 2 3 4
| first line second line third line
|
追加成功了。