Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: file current cursor is 0 when open file as append mode with exist file(size>0) #12710

Closed
yuyi98 opened this issue Sep 22, 2015 · 6 comments
Closed

Comments

@yuyi98
Copy link

yuyi98 commented Sep 22, 2015

package main

import (
    "fmt"
    "os"
)

func main() {
    f, err := os.OpenFile("aaa.dat", os.O_WRONLY|os.O_APPEND, 0666)
    if err != nil {
        fmt.Println("Open aaa.dat failed")
        return
    }

    defer f.Close()

    aa, _ := f.Seek(0, os.SEEK_CUR)
    fmt.Println("cursor position : ", aa)
}

cursor position should be size of "aaa.dat", but it's 0.

@bradfitz bradfitz added this to the Go1.6 milestone Sep 22, 2015
@bradfitz
Copy link
Contributor

Which operating system do you see this on? I assume some Unix variant?

It's quite likely that this behavior is platform-specific.

Linux man pages say:

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 file systems 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.

Go's docs merely say:

        O_APPEND int = syscall.O_APPEND // append data to the file when writing.

Perhaps we're missing some words somewhere. We should also verify that Unix, Windows, and Plan 9 are consistent.

@yuyi98
Copy link
Author

yuyi98 commented Sep 22, 2015

system: windows 7 64bit, go 1.5.1 amd64

@bradfitz
Copy link
Contributor

I ran an experiment in https://go-review.googlesource.com/14807

Unix and Windows return 0 for the seek position on an O_APPEND file. Plan 9 returns the actual value. I imagine we should change Plan 9 to match the behavior of the popular platforms. (cc @0intro)

And I guess we should just document the behavior on Unix and Windows. (cc @robpike)

@minux
Copy link
Member

minux commented Sep 23, 2015 via email

@bradfitz
Copy link
Contributor

@minux, SGTM.

It's only a race if you ever called Write, right? But we can just say it's unspecified and leave it at that.

@gopherbot
Copy link

CL https://golang.org/cl/14881 mentions this issue.

@minux minux closed this as completed in 53c92f9 Sep 23, 2015
@golang golang locked and limited conversation to collaborators Sep 23, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants