Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Barve <ambarve@microsoft.com>
  • Loading branch information
ambarve committed May 11, 2022
1 parent 53c0717 commit 8deb21d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
14 changes: 4 additions & 10 deletions ea.go
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"syscall"

"golang.org/x/sys/windows"
)
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion fileinfo.go
Expand Up @@ -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
}

Expand Down
22 changes: 4 additions & 18 deletions reparse.go
Expand Up @@ -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)
}
17 changes: 16 additions & 1 deletion reparse_test.go
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"
"testing"

"golang.org/x/sys/windows"
)

func TestGetSymlinkReparsePoint(t *testing.T) {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions sd_test.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 8deb21d

Please sign in to comment.