...
1
2
3
4 package winio
5
6 import (
7 "os"
8 "runtime"
9 "unsafe"
10
11 "golang.org/x/sys/windows"
12 )
13
14
15 type FileBasicInfo struct {
16 CreationTime, LastAccessTime, LastWriteTime, ChangeTime windows.Filetime
17 FileAttributes uint32
18 _ uint32
19 }
20
21
22 func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
23 bi := &FileBasicInfo{}
24 if err := windows.GetFileInformationByHandleEx(
25 windows.Handle(f.Fd()),
26 windows.FileBasicInfo,
27 (*byte)(unsafe.Pointer(bi)),
28 uint32(unsafe.Sizeof(*bi)),
29 ); err != nil {
30 return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
31 }
32 runtime.KeepAlive(f)
33 return bi, nil
34 }
35
36
37 func SetFileBasicInfo(f *os.File, bi *FileBasicInfo) error {
38 if err := windows.SetFileInformationByHandle(
39 windows.Handle(f.Fd()),
40 windows.FileBasicInfo,
41 (*byte)(unsafe.Pointer(bi)),
42 uint32(unsafe.Sizeof(*bi)),
43 ); err != nil {
44 return &os.PathError{Op: "SetFileInformationByHandle", Path: f.Name(), Err: err}
45 }
46 runtime.KeepAlive(f)
47 return nil
48 }
49
50
51
52
53 type FileStandardInfo struct {
54 AllocationSize, EndOfFile int64
55 NumberOfLinks uint32
56 DeletePending, Directory bool
57 }
58
59
60 func GetFileStandardInfo(f *os.File) (*FileStandardInfo, error) {
61 si := &FileStandardInfo{}
62 if err := windows.GetFileInformationByHandleEx(windows.Handle(f.Fd()),
63 windows.FileStandardInfo,
64 (*byte)(unsafe.Pointer(si)),
65 uint32(unsafe.Sizeof(*si))); err != nil {
66 return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
67 }
68 runtime.KeepAlive(f)
69 return si, nil
70 }
71
72
73
74 type FileIDInfo struct {
75 VolumeSerialNumber uint64
76 FileID [16]byte
77 }
78
79
80 func GetFileID(f *os.File) (*FileIDInfo, error) {
81 fileID := &FileIDInfo{}
82 if err := windows.GetFileInformationByHandleEx(
83 windows.Handle(f.Fd()),
84 windows.FileIdInfo,
85 (*byte)(unsafe.Pointer(fileID)),
86 uint32(unsafe.Sizeof(*fileID)),
87 ); err != nil {
88 return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
89 }
90 runtime.KeepAlive(f)
91 return fileID, nil
92 }
93
View as plain text