2014-02-05 72 views
3

當我的模式打開文件是這樣的:Golang的OpenFile O_APPEND不尊重尋求

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不尊重,我所尋求的位置。它似乎只是追加到文件的尾部。相反,如果我打開這樣的文件:

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從我尋求的「開始」點寫入文件。不確定這是功能還是錯誤?

回答

10

這絕對是一個功能(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.