-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_file_windows.go
More file actions
41 lines (34 loc) · 1.11 KB
/
open_file_windows.go
File metadata and controls
41 lines (34 loc) · 1.11 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//go:build windows
package main
import (
"os"
"syscall"
)
// FILE_FLAG_SEQUENTIAL_SCAN is a performance hint for the Windows file system.
const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000
// openFile on Windows uses the CreateFile syscall to provide the SEQUENTIAL_SCAN hint.
func openFile(name string) (*os.File, error) {
// We need to convert the name to a UTF-16 pointer for the Windows syscall.
namePtr, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, err
}
// GENERIC_READ: Read access
// FILE_SHARE_READ: Allow other processes to read the file
// OPEN_EXISTING: Open the file only if it exists
// FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN: Normal attributes + performance hint
handle, err := syscall.CreateFile(
namePtr,
syscall.GENERIC_READ,
syscall.FILE_SHARE_READ,
nil, // Default security attributes
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,
0, // No template file
)
if err != nil {
return nil, err
}
// The syscall returns a handle, which we convert into a standard *os.File.
return os.NewFile(uintptr(handle), name), nil
}