-
Notifications
You must be signed in to change notification settings - Fork 12
/
fileapi.go
160 lines (149 loc) · 3.81 KB
/
fileapi.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//go:build windows
package winapi
import (
"errors"
"syscall"
"unsafe"
)
const _MAX_READ = 1 << 30
const (
CREATE_NEW = 1
CREATE_ALWAYS = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
TRUNCATE_EXISTING = 5
)
/*
打开文件的方法如下:
CreateFile("D:/123.txt")
*/
func CreateFile(FileName string, DesiredAccess uint32, ShareMode uint32,
sa *SECURITY_ATTRIBUTES,
CreationDisposition uint32, FlagsAndAttributes uint32,
TemplateFile HANDLE) (HANDLE, error) {
pFileName, err := syscall.UTF16PtrFromString(FileName)
if err != nil {
return 0, err
}
r1, _, e1 := syscall.Syscall9(procCreateFile.Addr(), 7,
uintptr(unsafe.Pointer(pFileName)),
uintptr(DesiredAccess),
uintptr(ShareMode),
uintptr(unsafe.Pointer(sa)),
uintptr(CreationDisposition),
uintptr(FlagsAndAttributes),
uintptr(TemplateFile),
0, 0)
if r1 == 0 {
wec := WinErrorCode(e1)
if wec != 0 {
return 0, wec
} else {
return 0, errors.New("CreateFile failed.")
}
} else {
return HANDLE(r1), nil
}
}
func _ReadFile(hFile HANDLE,
pBuffer *byte, NumberOfBytesToRead uint32,
pNumberOfBytesRead *uint32,
pOverlapped *OVERLAPPED) error {
r1, _, e1 := syscall.Syscall6(procReadFile.Addr(), 5,
uintptr(hFile), uintptr(unsafe.Pointer(pBuffer)), uintptr(NumberOfBytesToRead),
uintptr(unsafe.Pointer(pNumberOfBytesRead)), uintptr(unsafe.Pointer(pOverlapped)), 0)
if r1 == 0 {
wec := WinErrorCode(e1)
if wec != 0 {
return wec
} else {
return errors.New("winapi: ReadFile failed.")
}
} else {
return nil
}
}
func ReadFile(hFile HANDLE, buf []byte, pOverlapped *OVERLAPPED) (uint32, error) {
if buf == nil {
return 0, errors.New("ReadFile: 必须提供有效的缓冲区")
}
var Len int = len(buf)
if Len <= 0 || Len > _MAX_READ {
return 0, errors.New("ReadFile: 缓冲区长度必须大于零且不超过_MAX_READ")
}
var NumberOfBytesRead uint32 = 0
err := _ReadFile(hFile, &buf[0], uint32(Len), &NumberOfBytesRead, pOverlapped)
if err != nil {
return 0, err
} else {
return NumberOfBytesRead, nil
}
}
/*
BOOL WINAPI WriteFile(
_In_ HANDLE hFile,
_In_ LPCVOID lpBuffer,
_In_ DWORD nNumberOfBytesToWrite,
_Out_opt_ LPDWORD lpNumberOfBytesWritten,
_Inout_opt_ LPOVERLAPPED lpOverlapped
);
*/
func _WriteFile(hFile HANDLE,
pBuffer *byte, NumberOfBytesToWrite uint32,
pNumberOfBytesWritten *uint32,
pOverlapped *OVERLAPPED) error {
r1, _, e1 := syscall.Syscall6(procWriteFile.Addr(), 5,
uintptr(hFile), uintptr(unsafe.Pointer(pBuffer)), uintptr(NumberOfBytesToWrite),
uintptr(unsafe.Pointer(pNumberOfBytesWritten)), uintptr(unsafe.Pointer(pOverlapped)), 0)
if r1 == 0 {
wec := WinErrorCode(e1)
if wec != 0 {
return wec
} else {
return errors.New("winapi: WriteFile failed.")
}
} else {
return nil
}
}
func WriteFile(hFile HANDLE, buf []byte, pOverlapped *OVERLAPPED) (uint32, error) {
if buf == nil {
return 0, errors.New("WriteFile: 必须提供有效的缓冲区")
}
var Len int = len(buf)
if Len <= 0 || Len > _MAX_READ {
return 0, errors.New("WriteFile: 缓冲区长度必须大于零且不超过_MAX_READ")
}
var NumberOfBytesWritten uint32
err := _ReadFile(hFile, &buf[0], uint32(Len), &NumberOfBytesWritten, pOverlapped)
if err != nil {
return 0, err
} else {
return NumberOfBytesWritten, nil
}
}
const (
FILE_BEGIN = 0
FILE_CURRENT = 1
FILE_END = 2
)
func SetFilePointer(hFile HANDLE, DistanceToMove int64, MoveMethod uint32) (NewPointer int64, err error) {
var np int64
r1, _, e1 := syscall.Syscall6(procSetFilePointer.Addr(), 4,
uintptr(hFile),
uintptr(DistanceToMove),
uintptr(unsafe.Pointer(&np)),
uintptr(MoveMethod),
0, 0)
if r1 == 0 {
wec := WinErrorCode(e1)
if wec != 0 {
err = wec
} else {
err = errors.New("winapi: SetFilePointer failed.")
}
} else {
NewPointer = np
}
return
}