mirror of
https://github.com/rwinkhart/go-winio.git
synced 2026-09-05 16:47:31 -04:00
Addressed Feedback
Signed-off-by: Varun Gokulnath <gvarun22@outlook.com>
This commit is contained in:
+6
-2
@@ -16,6 +16,8 @@ const (
|
|||||||
reparseTagMountPoint = 0xA0000003
|
reparseTagMountPoint = 0xA0000003
|
||||||
reparseTagSymlink = 0xA000000C
|
reparseTagSymlink = 0xA000000C
|
||||||
reparseTagLxSymlink = 0xA000001D // WSL/MSYS2 native symlinks
|
reparseTagLxSymlink = 0xA000001D // WSL/MSYS2 native symlinks
|
||||||
|
|
||||||
|
lxSymlinkVersion = 2 // LX symlink format version
|
||||||
)
|
)
|
||||||
|
|
||||||
type reparseDataBuffer struct {
|
type reparseDataBuffer struct {
|
||||||
@@ -102,6 +104,9 @@ func isDriveLetter(c byte) bool {
|
|||||||
// EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink,
|
// EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink,
|
||||||
// mount point, or LX symlink.
|
// mount point, or LX symlink.
|
||||||
func EncodeReparsePoint(rp *ReparsePoint) []byte {
|
func EncodeReparsePoint(rp *ReparsePoint) []byte {
|
||||||
|
if rp == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if rp.IsLxSymlink {
|
if rp.IsLxSymlink {
|
||||||
return encodeLxReparsePoint(rp)
|
return encodeLxReparsePoint(rp)
|
||||||
}
|
}
|
||||||
@@ -110,7 +115,6 @@ func EncodeReparsePoint(rp *ReparsePoint) []byte {
|
|||||||
|
|
||||||
func encodeLxReparsePoint(rp *ReparsePoint) []byte {
|
func encodeLxReparsePoint(rp *ReparsePoint) []byte {
|
||||||
// LX symlink: 4-byte version + UTF-8 target
|
// LX symlink: 4-byte version + UTF-8 target
|
||||||
version := uint32(2)
|
|
||||||
targetBytes := []byte(rp.Target)
|
targetBytes := []byte(rp.Target)
|
||||||
dataLength := 4 + len(targetBytes)
|
dataLength := 4 + len(targetBytes)
|
||||||
|
|
||||||
@@ -118,7 +122,7 @@ func encodeLxReparsePoint(rp *ReparsePoint) []byte {
|
|||||||
_ = binary.Write(&b, binary.LittleEndian, uint32(reparseTagLxSymlink))
|
_ = binary.Write(&b, binary.LittleEndian, uint32(reparseTagLxSymlink))
|
||||||
_ = binary.Write(&b, binary.LittleEndian, uint16(dataLength))
|
_ = binary.Write(&b, binary.LittleEndian, uint16(dataLength))
|
||||||
_ = binary.Write(&b, binary.LittleEndian, uint16(0))
|
_ = binary.Write(&b, binary.LittleEndian, uint16(0))
|
||||||
_ = binary.Write(&b, binary.LittleEndian, version)
|
_ = binary.Write(&b, binary.LittleEndian, uint32(lxSymlinkVersion))
|
||||||
_, _ = b.Write(targetBytes)
|
_, _ = b.Write(targetBytes)
|
||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|||||||
+95
-2
@@ -6,10 +6,17 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
testLxSymlinkAbsolutePath = "/usr/bin/bash"
|
||||||
|
testWindowsSymlinkPath = `C:\Windows\System32`
|
||||||
|
testLxSymlinkRelativePath = "../bin/sh"
|
||||||
|
testLxSymlinkSpecialCharsPath = "/path/with spaces/and-special!@#$%/файл.txt"
|
||||||
|
)
|
||||||
|
|
||||||
func TestLxSymlinkRoundTrip(t *testing.T) {
|
func TestLxSymlinkRoundTrip(t *testing.T) {
|
||||||
// Test LX symlink encode/decode
|
// Test LX symlink encode/decode
|
||||||
original := &ReparsePoint{
|
original := &ReparsePoint{
|
||||||
Target: "/usr/bin/bash",
|
Target: testLxSymlinkAbsolutePath,
|
||||||
IsMountPoint: false,
|
IsMountPoint: false,
|
||||||
IsLxSymlink: true,
|
IsLxSymlink: true,
|
||||||
}
|
}
|
||||||
@@ -38,7 +45,7 @@ func TestLxSymlinkRoundTrip(t *testing.T) {
|
|||||||
func TestWindowsSymlinkNotLx(t *testing.T) {
|
func TestWindowsSymlinkNotLx(t *testing.T) {
|
||||||
// Test that regular Windows symlinks are not marked as LX
|
// Test that regular Windows symlinks are not marked as LX
|
||||||
original := &ReparsePoint{
|
original := &ReparsePoint{
|
||||||
Target: `C:\Windows\System32`,
|
Target: testWindowsSymlinkPath,
|
||||||
IsMountPoint: false,
|
IsMountPoint: false,
|
||||||
IsLxSymlink: false,
|
IsLxSymlink: false,
|
||||||
}
|
}
|
||||||
@@ -57,3 +64,89 @@ func TestWindowsSymlinkNotLx(t *testing.T) {
|
|||||||
t.Errorf("Windows symlink incorrectly marked as LX symlink")
|
t.Errorf("Windows symlink incorrectly marked as LX symlink")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLxSymlinkEmptyTarget(t *testing.T) {
|
||||||
|
// Test LX symlink with empty target
|
||||||
|
original := &ReparsePoint{
|
||||||
|
Target: "",
|
||||||
|
IsMountPoint: false,
|
||||||
|
IsLxSymlink: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode
|
||||||
|
encoded := EncodeReparsePoint(original)
|
||||||
|
|
||||||
|
// Decode
|
||||||
|
decoded, err := DecodeReparsePoint(encoded)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to decode: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
if decoded.Target != original.Target {
|
||||||
|
t.Errorf("Target mismatch: got %q, want %q", decoded.Target, original.Target)
|
||||||
|
}
|
||||||
|
if !decoded.IsLxSymlink {
|
||||||
|
t.Errorf("IsLxSymlink should be true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLxSymlinkRelativePath(t *testing.T) {
|
||||||
|
// Test LX symlink with relative path
|
||||||
|
original := &ReparsePoint{
|
||||||
|
Target: testLxSymlinkRelativePath,
|
||||||
|
IsMountPoint: false,
|
||||||
|
IsLxSymlink: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode
|
||||||
|
encoded := EncodeReparsePoint(original)
|
||||||
|
|
||||||
|
// Decode
|
||||||
|
decoded, err := DecodeReparsePoint(encoded)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to decode: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
if decoded.Target != original.Target {
|
||||||
|
t.Errorf("Target mismatch: got %q, want %q", decoded.Target, original.Target)
|
||||||
|
}
|
||||||
|
if !decoded.IsLxSymlink {
|
||||||
|
t.Errorf("IsLxSymlink should be true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLxSymlinkSpecialCharacters(t *testing.T) {
|
||||||
|
// Test LX symlink with special characters and Unicode
|
||||||
|
original := &ReparsePoint{
|
||||||
|
Target: testLxSymlinkSpecialCharsPath,
|
||||||
|
IsMountPoint: false,
|
||||||
|
IsLxSymlink: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode
|
||||||
|
encoded := EncodeReparsePoint(original)
|
||||||
|
|
||||||
|
// Decode
|
||||||
|
decoded, err := DecodeReparsePoint(encoded)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to decode: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
if decoded.Target != original.Target {
|
||||||
|
t.Errorf("Target mismatch: got %q, want %q", decoded.Target, original.Target)
|
||||||
|
}
|
||||||
|
if !decoded.IsLxSymlink {
|
||||||
|
t.Errorf("IsLxSymlink should be true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncodeReparsePointNil(t *testing.T) {
|
||||||
|
// Test encoding a nil ReparsePoint
|
||||||
|
encoded := EncodeReparsePoint(nil)
|
||||||
|
if encoded != nil {
|
||||||
|
t.Errorf("Expected nil result for nil input, got %v", encoded)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user