1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:34:48 -07:00

os: make Open() O_APPEND flag work on windows

Fixes #1124.
Implementation is suggested by Skip.
Test is suggested by PeterGo.

R=r, PeterGo, rsc
CC=golang-dev, skip.tavakkolian
https://golang.org/cl/2256041
This commit is contained in:
Alex Brainman 2010-09-22 13:12:25 +10:00
parent 8ee986570a
commit a071853015
3 changed files with 36 additions and 0 deletions

View File

@ -742,3 +742,33 @@ func TestWriteAt(t *testing.T) {
t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n") t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n")
} }
} }
func writeFile(t *testing.T, fname string, flag int, text string) string {
f, err := Open(fname, flag, 0666)
if err != nil {
t.Fatalf("Open: %v", err)
}
n, err := io.WriteString(f, text)
if err != nil {
t.Fatalf("WriteString: %d, %v", n, err)
}
f.Close()
data, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
return string(data)
}
func TestAppend(t *testing.T) {
const f = "append.txt"
defer Remove(f)
s := writeFile(t, f, O_CREAT|O_TRUNC|O_RDWR, "new")
if s != "new" {
t.Fatalf("writeFile: have %q want %q", s, "new")
}
s = writeFile(t, f, O_APPEND|O_RDWR, "|append")
if s != "new|append" {
t.Fatalf("writeFile: have %q want %q", s, "new|append")
}
}

View File

@ -184,6 +184,10 @@ func Open(path string, mode int, perm uint32) (fd int, errno int) {
if mode&O_CREAT != 0 { if mode&O_CREAT != 0 {
access |= GENERIC_WRITE access |= GENERIC_WRITE
} }
if mode&O_APPEND != 0 {
access &^= GENERIC_WRITE
access |= FILE_APPEND_DATA
}
sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE) sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
var createmode uint32 var createmode uint32
switch { switch {

View File

@ -56,6 +56,8 @@ const (
GENERIC_EXECUTE = 0x20000000 GENERIC_EXECUTE = 0x20000000
GENERIC_ALL = 0x10000000 GENERIC_ALL = 0x10000000
FILE_APPEND_DATA = 0x00000004
FILE_SHARE_READ = 0x00000001 FILE_SHARE_READ = 0x00000001
FILE_SHARE_WRITE = 0x00000002 FILE_SHARE_WRITE = 0x00000002
FILE_SHARE_DELETE = 0x00000004 FILE_SHARE_DELETE = 0x00000004