-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_windows.go
64 lines (46 loc) · 1.46 KB
/
file_windows.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
// +build windows
package main
import (
"fmt"
"os"
"reflect"
"syscall"
)
func Executable() (string, error) {
return os.Executable()
}
func GetFileOsUniqueKey(path string) string {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return ""
}
// loading samefile function calls loadingFileId() on info.Sys().(*os.fileStat)
os.SameFile(info, info)
fileStat := reflect.ValueOf(info).Elem()
// the following fields unique identifies in file system despite the file name
idxhi := fileStat.FieldByName("idxhi").Uint()
idxlo := fileStat.FieldByName("idxlo").Uint()
vol := fileStat.FieldByName("vol").Uint()
key := fmt.Sprintf("%d_%d_%d", idxhi, idxlo, vol)
return key
}
func ReadOpen(path string) (*os.File, error) {
if len(path) == 0 {
return nil, fmt.Errorf("File '%s' not found. Error: %v", path, syscall.ERROR_FILE_NOT_FOUND)
}
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, fmt.Errorf("Error converting to UTF16: %v", err)
}
var access uint32
access = syscall.GENERIC_READ
sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE)
var sa *syscall.SecurityAttributes
var createmode uint32
createmode = syscall.OPEN_EXISTING
handle, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
return nil, fmt.Errorf("Error creating file '%s': %v", pathp, err)
}
return os.NewFile(uintptr(handle), path), nil
}