mirror of
https://github.com/golang/go
synced 2024-11-21 15:54:43 -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:
parent
8ee986570a
commit
a071853015
@ -742,3 +742,33 @@ func TestWriteAt(t *testing.T) {
|
||||
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) {
|
||||
if mode&O_CREAT != 0 {
|
||||
access |= GENERIC_WRITE
|
||||
}
|
||||
if mode&O_APPEND != 0 {
|
||||
access &^= GENERIC_WRITE
|
||||
access |= FILE_APPEND_DATA
|
||||
}
|
||||
sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
|
||||
var createmode uint32
|
||||
switch {
|
||||
|
@ -56,6 +56,8 @@ const (
|
||||
GENERIC_EXECUTE = 0x20000000
|
||||
GENERIC_ALL = 0x10000000
|
||||
|
||||
FILE_APPEND_DATA = 0x00000004
|
||||
|
||||
FILE_SHARE_READ = 0x00000001
|
||||
FILE_SHARE_WRITE = 0x00000002
|
||||
FILE_SHARE_DELETE = 0x00000004
|
||||
|
Loading…
Reference in New Issue
Block a user