From 8deb21dc13c4260e98bf0e6ec2f663522c4d1ea8 Mon Sep 17 00:00:00 2001 From: Amit Barve Date: Wed, 11 May 2022 07:21:08 -0700 Subject: [PATCH] PR suggestions Signed-off-by: Amit Barve --- ea.go | 14 ++++---------- fileinfo.go | 1 - reparse.go | 22 ++++------------------ reparse_test.go | 17 ++++++++++++++++- sd_test.go | 4 ++-- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/ea.go b/ea.go index ef38922a..f5aa5de1 100644 --- a/ea.go +++ b/ea.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "errors" "fmt" - "syscall" "golang.org/x/sys/windows" ) @@ -45,14 +44,13 @@ func GetFileEA(handle windows.Handle) ([]ExtendedAttribute, error) { // keep increasing the buffer size until it is large enough for { status := getFileEA(handle, &iosb, &buf[0], uint32(bufLen), false, 0, 0, nil, true) - if status < 0 { + if status.Err() != nil { // convert ntstatus code to windows error - werr := rtlNtStatusToDosError(status) - if werr == syscall.ERROR_INSUFFICIENT_BUFFER || werr == syscall.ERROR_MORE_DATA { + if status.Err() == windows.ERROR_INSUFFICIENT_BUFFER || status.Err() == windows.ERROR_MORE_DATA { bufLen *= 2 buf = make([]byte, bufLen) } else { - return nil, fmt.Errorf("get file EA failed with: %w", werr) + return nil, fmt.Errorf("get file EA failed with: %w", status.Err()) } } else { break @@ -71,11 +69,7 @@ func SetFileEA(handle windows.Handle, attrs []ExtendedAttribute) error { var iosb ioStatusBlock - status := setFileEA(handle, &iosb, &encodedEA[0], uint32(len(encodedEA))) - if status < 0 { - return rtlNtStatusToDosError(status) - } - return nil + return setFileEA(handle, &iosb, &encodedEA[0], uint32(len(encodedEA))).Err() } func parseEa(b []byte) (ea ExtendedAttribute, nb []byte, err error) { diff --git a/fileinfo.go b/fileinfo.go index c3a6e94f..49b5a17b 100644 --- a/fileinfo.go +++ b/fileinfo.go @@ -43,7 +43,6 @@ func SetFileBasicInfoForHandle(f windows.Handle, bi *FileBasicInfo) error { if err := windows.SetFileInformationByHandle(f, windows.FileBasicInfo, (*byte)(unsafe.Pointer(bi)), uint32(unsafe.Sizeof(*bi))); err != nil { return &os.PathError{Op: "SetFileInformationByHandle", Err: err} } - runtime.KeepAlive(f) return nil } diff --git a/reparse.go b/reparse.go index 3b6211ad..d8fdc228 100644 --- a/reparse.go +++ b/reparse.go @@ -201,22 +201,8 @@ func GetReparsePoint(path string) ([]byte, error) { return outBuf[:outBufLen], nil } -// SetReparsePoint sets a reparse point at `path` with `data`. `data` should be -// an encoded ReparseDataBuffer struct. -func SetReparsePoint(path string, data []byte) error { - utf16DestPath := windows.StringToUTF16(path) - // We need to open the file with windows specific permissions so just using - // os.Open won't work. - fileHandle, err := windows.CreateFile(&utf16DestPath[0], windows.GENERIC_WRITE, 0, nil, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL, 0) - if err != nil { - return fmt.Errorf("open file %s failed: %w", path, err) - } - defer windows.Close(fileHandle) - - err = windows.DeviceIoControl(fileHandle, _FSCTL_SET_REPARSE_POINT, &data[0], uint32(len(data)), nil, 0, nil, nil) - if err != nil { - return fmt.Errorf("failed to create reparse point for file %s: %w", path, err) - } - - return nil +// SetReparsePoint sets a reparse point with `data` on the file/directory represented by +// `handle` . `data` should be an encoded ReparseDataBuffer struct. +func SetReparsePoint(handle windows.Handle, data []byte) error { + return windows.DeviceIoControl(handle, _FSCTL_SET_REPARSE_POINT, &data[0], uint32(len(data)), nil, 0, nil, nil) } diff --git a/reparse_test.go b/reparse_test.go index ea45ce61..6b4aba6c 100644 --- a/reparse_test.go +++ b/reparse_test.go @@ -6,6 +6,8 @@ import ( "path/filepath" "strings" "testing" + + "golang.org/x/sys/windows" ) func TestGetSymlinkReparsePoint(t *testing.T) { @@ -83,9 +85,22 @@ func TestSetReparsePoint(t *testing.T) { Reserved: 0, DataBuffer: []byte{0xde, 0xad, 0xbe, 0xef}, } - if err := SetReparsePoint(testfilePath, r.Encode()); err != nil { + + utf16Path, err := windows.UTF16PtrFromString(testfilePath) + if err != nil { + t.Fatalf("failed to convert path into utf16: %s", err) + } + + testfileHandle, err := windows.CreateFile(utf16Path, windows.GENERIC_WRITE|windows.GENERIC_READ, 0, nil, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL, 0) + if err != nil { + t.Fatalf("failed to open file handle: %s", err) + } + defer windows.Close(testfileHandle) + + if err := SetReparsePoint(testfileHandle, r.Encode()); err != nil { t.Fatalf("failed to set reparse point: %s", err) } + windows.Close(testfileHandle) reparseBuffer, err := GetReparsePoint(testfilePath) if err != nil { diff --git a/sd_test.go b/sd_test.go index 0d3e530b..ec336eda 100644 --- a/sd_test.go +++ b/sd_test.go @@ -31,8 +31,8 @@ func TestLookupEmptyNameFails(t *testing.T) { } func TestGetFileSDDL(t *testing.T) { - win32 := "C:\\Windows\\System32" - kern := "C:\\Windows\\System32\\kernel32.dll" + win32 := `C:\Windows\System32` + kern := `C:\Windows\System32\kernel32.dll` err := EnableProcessPrivileges([]string{SeBackupPrivilege}) if err != nil {