Skip to content

Commit bfd5468

Browse files
committed
Replace local *FileInformationByHandleEx with x/sys/windows
We also get pre-defined constants instead of carrying our own copies. However, this changes the public API winio.FileBasicInfo from using syscall.Filetime to using windows.Filetime, as visible in backuptar/tar.go. Signed-off-by: Paul "TBBle" Hampson <[email protected]>
1 parent bc48b61 commit bfd5468

3 files changed

Lines changed: 11 additions & 35 deletions

File tree

backuptar/tar.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/Microsoft/go-winio"
19+
"golang.org/x/sys/windows"
1920
)
2021

2122
const (
@@ -297,11 +298,11 @@ func FileInfoFromHeader(hdr *tar.Header) (name string, size int64, fileInfo *win
297298
size = hdr.Size
298299
}
299300
fileInfo = &winio.FileBasicInfo{
300-
LastAccessTime: syscall.NsecToFiletime(hdr.AccessTime.UnixNano()),
301-
LastWriteTime: syscall.NsecToFiletime(hdr.ModTime.UnixNano()),
302-
ChangeTime: syscall.NsecToFiletime(hdr.ChangeTime.UnixNano()),
301+
LastAccessTime: windows.NsecToFiletime(hdr.AccessTime.UnixNano()),
302+
LastWriteTime: windows.NsecToFiletime(hdr.ModTime.UnixNano()),
303+
ChangeTime: windows.NsecToFiletime(hdr.ChangeTime.UnixNano()),
303304
// Default to ModTime, we'll pull hdrCreationTime below if present
304-
CreationTime: syscall.NsecToFiletime(hdr.ModTime.UnixNano()),
305+
CreationTime: windows.NsecToFiletime(hdr.ModTime.UnixNano()),
305306
}
306307
if attrStr, ok := hdr.PAXRecords[hdrFileAttributes]; ok {
307308
attr, err := strconv.ParseUint(attrStr, 10, 32)
@@ -319,7 +320,7 @@ func FileInfoFromHeader(hdr *tar.Header) (name string, size int64, fileInfo *win
319320
if err != nil {
320321
return "", 0, nil, err
321322
}
322-
fileInfo.CreationTime = syscall.NsecToFiletime(creationTime.UnixNano())
323+
fileInfo.CreationTime = windows.NsecToFiletime(creationTime.UnixNano())
323324
}
324325
return
325326
}

fileinfo.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,22 @@ package winio
55
import (
66
"os"
77
"runtime"
8-
"syscall"
98
"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
149

15-
const (
16-
fileBasicInfo = 0
17-
fileIDInfo = 0x12
10+
"golang.org/x/sys/windows"
1811
)
1912

2013
// FileBasicInfo contains file access time and file attributes information.
2114
type FileBasicInfo struct {
22-
CreationTime, LastAccessTime, LastWriteTime, ChangeTime syscall.Filetime
15+
CreationTime, LastAccessTime, LastWriteTime, ChangeTime windows.Filetime
2316
FileAttributes uint32
2417
pad uint32 // padding
2518
}
2619

2720
// GetFileBasicInfo retrieves times and attributes for a file.
2821
func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
2922
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 {
3124
return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
3225
}
3326
runtime.KeepAlive(f)
@@ -36,7 +29,7 @@ func GetFileBasicInfo(f *os.File) (*FileBasicInfo, error) {
3629

3730
// SetFileBasicInfo sets times and attributes for a file.
3831
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 {
4033
return &os.PathError{Op: "SetFileInformationByHandle", Path: f.Name(), Err: err}
4134
}
4235
runtime.KeepAlive(f)
@@ -53,7 +46,7 @@ type FileIDInfo struct {
5346
// GetFileID retrieves the unique (volume, file ID) pair for a file.
5447
func GetFileID(f *os.File) (*FileIDInfo, error) {
5548
fileID := &FileIDInfo{}
56-
if err := getFileInformationByHandleEx(syscall.Handle(f.Fd()), fileIDInfo, (*byte)(unsafe.Pointer(fileID)), uint32(unsafe.Sizeof(*fileID))); err != nil {
49+
if err := windows.GetFileInformationByHandleEx(windows.Handle(f.Fd()), windows.FileIdInfo, (*byte)(unsafe.Pointer(fileID)), uint32(unsafe.Sizeof(*fileID))); err != nil {
5750
return nil, &os.PathError{Op: "GetFileInformationByHandleEx", Path: f.Name(), Err: err}
5851
}
5952
runtime.KeepAlive(f)

zsyscall_windows.go

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)