1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//go:build windows
package logx
import (
"os"
"golang.org/x/sys/windows"
)
// openLogAppend opens path for appending with delete sharing, so another
// process can delete an old log file while this one still holds it open;
// Go's os.OpenFile does not share delete access on Windows.
func openLogAppend(path string) (*os.File, error) {
name, err := windows.UTF16PtrFromString(path)
if err != nil {
return nil, err
}
h, err := windows.CreateFile(name,
windows.FILE_APPEND_DATA|windows.FILE_READ_ATTRIBUTES|windows.SYNCHRONIZE,
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
nil, windows.OPEN_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
return nil, &os.PathError{Op: "open", Path: path, Err: err}
}
return os.NewFile(uintptr(h), path), nil
}