Commit a0718530 authored by Alex Brainman's avatar Alex Brainman

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
parent 8ee98657
...@@ -742,3 +742,33 @@ func TestWriteAt(t *testing.T) { ...@@ -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")
}
}
...@@ -184,6 +184,10 @@ func Open(path string, mode int, perm uint32) (fd int, errno int) { ...@@ -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 {
......
...@@ -56,6 +56,8 @@ const ( ...@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment