dongmeng9048 2014-02-05 10:14
浏览 136
已采纳

Golang OpenFile O_APPEND不尊重Seek

When I open file in mode like this:

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 does not respect the position where I sought. It seems it just append to the tail of the file. Instead if I open the file like this:

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)

It works as I expected. io.CopyN writes to the file from the "start" point I sought. Not sure if this is a feature or a bug?

  • 写回答

1条回答 默认 最新

  • douhuang3740 2014-02-05 10:19
    关注

    It's definitely a feature (http://man7.org/linux/man-pages/man2/open.2.html) and it's controlled by underlying OS, not golang runtime.

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题