-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhfind.go
62 lines (53 loc) · 1.76 KB
/
hfind.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
package wingo
import (
"github.com/rogeecn/wingo/proc"
"syscall"
"unsafe"
"github.com/rogeecn/wingo/errco"
)
// A handle returned by FindFirstFile() function.
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew
type HFIND HANDLE
// Returns true if a file was found.
//
// ⚠️ You must defer HFIND.FindClose().
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew
func FindFirstFile(fileName string,
findFileData *WIN32_FIND_DATA) (HFIND, bool, error) {
ret, _, err := syscall.Syscall(proc.FindFirstFile.Addr(), 2,
uintptr(unsafe.Pointer(Str.ToNativePtr(fileName))),
uintptr(unsafe.Pointer(findFileData)), 0)
if int(ret) == _INVALID_HANDLE_VALUE {
if wErr := errco.ERROR(err); wErr == errco.FILE_NOT_FOUND || wErr == errco.PATH_NOT_FOUND {
return HFIND(0), false, nil // no matching files, not an error
} else {
return HFIND(0), false, wErr
}
}
return HFIND(ret), true, nil // a file was found
}
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findclose
func (hFind HFIND) FindClose() {
ret, _, err := syscall.Syscall(proc.FindClose.Addr(), 1,
uintptr(hFind), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// Returns true if a file was found.
//
// 📑 https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextfilew
func (hFind HFIND) FindNextFile(findFileData *WIN32_FIND_DATA) (bool, error) {
ret, _, err := syscall.Syscall(proc.FindNextFile.Addr(), 2,
uintptr(hFind), uintptr(unsafe.Pointer(findFileData)), 0)
if ret == 0 {
if wErr := errco.ERROR(err); wErr == errco.NO_MORE_FILES {
return false, nil // not an error, search ended
} else {
return false, wErr
}
}
return true, nil // a file was found
}