gpt4 book ai didi

Golang OpenFile O_APPEND 不尊重 Seek

转载 作者:IT王子 更新时间:2023-10-29 00:49:48 25 4
gpt4 key购买 nike

当我以这样的模式打开文件时:

file, _ := os.OpenFile("/path/to/my/file", os.O_RDWR|os.O_APPEND, os.FileMode(0666))
file.Seek(start, os.SEEK_SET)
io.CopyN(file, resp.Body, length)

io.CopyN 不尊重我寻求的位置。它似乎只是 append 到文件的尾部。相反,如果我像这样打开文件:

file, _ := os.OpenFile("/path/to/my/file", os.O_RDWR, os.FileMode(0666))
file.Seek(start, os.SEEK_SET)
io.CopyN(file, resp.Body, length)

它按我预期的那样工作。 io.CopyN 从我寻找的“开始”点写入文件。不确定这是功能还是错误?

最佳答案

这绝对是一个功能 (http://man7.org/linux/man-pages/man2/open.2.html),它由底层操作系统控制,而不是 golang 运行时。

O_APPEND
The file is opened in append mode. Before each write(2), the
file offset is positioned at the end of the file, as if with
lseek(2). O_APPEND may lead to corrupted files on NFS
filesystems if more than one process appends data to a file at
once. This is because NFS does not support appending to a
file, so the client kernel has to simulate it, which can't be
done without a race condition.

关于Golang OpenFile O_APPEND 不尊重 Seek,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21574288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com