@@ -5,29 +5,22 @@ package winio
5
5
import (
6
6
"os"
7
7
"runtime"
8
- "syscall"
9
8
"unsafe"
10
- )
11
-
12
- //sys getFileInformationByHandleEx(h syscall.Handle, class uint32, buffer *byte, size uint32) (err error) = GetFileInformationByHandleEx
13
- //sys setFileInformationByHandle(h syscall.Handle, class uint32, buffer *byte, size uint32) (err error) = SetFileInformationByHandle
14
9
15
- const (
16
- fileBasicInfo = 0
17
- fileIDInfo = 0x12
10
+ "golang.org/x/sys/windows"
18
11
)
19
12
20
13
// FileBasicInfo contains file access time and file attributes information.
21
14
type FileBasicInfo struct {
22
- CreationTime , LastAccessTime , LastWriteTime , ChangeTime syscall .Filetime
15
+ CreationTime , LastAccessTime , LastWriteTime , ChangeTime windows .Filetime
23
16
FileAttributes uint32
24
17
pad uint32 // padding
25
18
}
26
19
27
20
// GetFileBasicInfo retrieves times and attributes for a file.
28
21
func GetFileBasicInfo (f * os.File ) (* FileBasicInfo , error ) {
29
22
bi := & FileBasicInfo {}
30
- if err := getFileInformationByHandleEx ( syscall .Handle (f .Fd ()), fileBasicInfo , (* byte )(unsafe .Pointer (bi )), uint32 (unsafe .Sizeof (* bi ))); err != nil {
23
+ if err := windows . GetFileInformationByHandleEx ( windows .Handle (f .Fd ()), windows . FileBasicInfo , (* byte )(unsafe .Pointer (bi )), uint32 (unsafe .Sizeof (* bi ))); err != nil {
31
24
return nil , & os.PathError {Op : "GetFileInformationByHandleEx" , Path : f .Name (), Err : err }
32
25
}
33
26
runtime .KeepAlive (f )
@@ -36,13 +29,32 @@ func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
36
29
37
30
// SetFileBasicInfo sets times and attributes for a file.
38
31
func SetFileBasicInfo (f * os.File , bi * FileBasicInfo ) error {
39
- if err := setFileInformationByHandle ( syscall .Handle (f .Fd ()), fileBasicInfo , (* byte )(unsafe .Pointer (bi )), uint32 (unsafe .Sizeof (* bi ))); err != nil {
32
+ if err := windows . SetFileInformationByHandle ( windows .Handle (f .Fd ()), windows . FileBasicInfo , (* byte )(unsafe .Pointer (bi )), uint32 (unsafe .Sizeof (* bi ))); err != nil {
40
33
return & os.PathError {Op : "SetFileInformationByHandle" , Path : f .Name (), Err : err }
41
34
}
42
35
runtime .KeepAlive (f )
43
36
return nil
44
37
}
45
38
39
+ // FileStandardInfo contains extended information for the file.
40
+ // FILE_STANDARD_INFO in WinBase.h
41
+ // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_standard_info
42
+ type FileStandardInfo struct {
43
+ AllocationSize , EndOfFile int64
44
+ NumberOfLinks uint32
45
+ DeletePending , Directory bool
46
+ }
47
+
48
+ // GetFileStandardInfo retrieves ended information for the file.
49
+ func GetFileStandardInfo (f * os.File ) (* FileStandardInfo , error ) {
50
+ si := & FileStandardInfo {}
51
+ if err := windows .GetFileInformationByHandleEx (windows .Handle (f .Fd ()), windows .FileStandardInfo , (* byte )(unsafe .Pointer (si )), uint32 (unsafe .Sizeof (* si ))); err != nil {
52
+ return nil , & os.PathError {Op : "GetFileInformationByHandleEx" , Path : f .Name (), Err : err }
53
+ }
54
+ runtime .KeepAlive (f )
55
+ return si , nil
56
+ }
57
+
46
58
// FileIDInfo contains the volume serial number and file ID for a file. This pair should be
47
59
// unique on a system.
48
60
type FileIDInfo struct {
@@ -53,7 +65,7 @@ type FileIDInfo struct {
53
65
// GetFileID retrieves the unique (volume, file ID) pair for a file.
54
66
func GetFileID (f * os.File ) (* FileIDInfo , error ) {
55
67
fileID := & FileIDInfo {}
56
- if err := getFileInformationByHandleEx ( syscall .Handle (f .Fd ()), fileIDInfo , (* byte )(unsafe .Pointer (fileID )), uint32 (unsafe .Sizeof (* fileID ))); err != nil {
68
+ if err := windows . GetFileInformationByHandleEx ( windows .Handle (f .Fd ()), windows . FileIdInfo , (* byte )(unsafe .Pointer (fileID )), uint32 (unsafe .Sizeof (* fileID ))); err != nil {
57
69
return nil , & os.PathError {Op : "GetFileInformationByHandleEx" , Path : f .Name (), Err : err }
58
70
}
59
71
runtime .KeepAlive (f )
0 commit comments