From d97f6d88b7e74288eb0a41fa652292b4b84bd3d0 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 00:32:38 +0800 Subject: [PATCH 1/7] fix(trash): keep junctions as junctions when moving across volumes When the skills source and the trash live on different volumes, the cross-device fallback in moveLink and copyDir recreated a Windows junction with os.Symlink. That needs Developer Mode or elevation, which the junction was chosen to avoid, so `unlink` and `trash restore` could fail after `link` had succeeded. Recreate a junction as a junction through a shared helper, and keep the original link in place when creation fails. CreateJunction moves from internal/sync to internal/utils so trash can reuse it. Refs #420 --- internal/sourcefs/testdata/raw_writes.tsv | 3 +- internal/sync/extras_backup.go | 2 +- internal/sync/file_backups.go | 2 +- internal/sync/symlink_unix.go | 6 -- internal/sync/symlink_windows.go | 17 +---- internal/trash/trash.go | 16 ++++- internal/trash/trash_junction_windows_test.go | 66 +++++++++++++++++++ internal/utils/link.go | 6 ++ internal/utils/link_unix.go | 7 ++ internal/utils/link_windows.go | 20 +++++- 10 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 internal/trash/trash_junction_windows_test.go diff --git a/internal/sourcefs/testdata/raw_writes.tsv b/internal/sourcefs/testdata/raw_writes.tsv index a1a98f96c..ce455a87d 100644 --- a/internal/sourcefs/testdata/raw_writes.tsv +++ b/internal/sourcefs/testdata/raw_writes.tsv @@ -488,13 +488,12 @@ internal/trash/trash.go RestoreAgent os.Rename srcPath, destPath agents internal/trash/trash.go RestoreAgent os.WriteFile destPath agents internal/trash/trash.go cleanEmptyParents os.Remove dir trash internal/trash/trash.go copyDir os.MkdirAll dst source-unmigrated -internal/trash/trash.go copyDir os.Symlink target, dstPath source-unmigrated internal/trash/trash.go copyFile os.OpenFile dst source-unmigrated internal/trash/trash.go moveLink os.Remove dst source-unmigrated internal/trash/trash.go moveLink os.Remove src source-checked -internal/trash/trash.go moveLink os.Symlink target, dst source-unmigrated internal/trash/trash.go moveToTrash os.MkdirAll filepath.Dir(trashPath) trash internal/trash/trash.go moveToTrash os.RemoveAll srcPath source-checked +internal/trash/trash.go recreateLink os.Symlink target, dst source-unmigrated internal/trash/trash.go restore os.RemoveAll entry.Path trash internal/uidist/uidist.go ClearCache os.RemoveAll dir config internal/uidist/uidist.go ClearVersion os.RemoveAll dir config diff --git a/internal/sync/extras_backup.go b/internal/sync/extras_backup.go index 8978a73a8..5845edb43 100644 --- a/internal/sync/extras_backup.go +++ b/internal/sync/extras_backup.go @@ -243,7 +243,7 @@ func putBackExtraRestorePoint(path string) error { case fileExists(filepath.Join(dir, attachRestoreJunction)): dest, err := os.ReadFile(filepath.Join(dir, attachRestoreJunction)) if err == nil { - err = createJunction(path, string(dest)) + err = utils.CreateJunction(path, string(dest)) } if err != nil { return fmt.Errorf("failed to restore %s: %w", path, err) diff --git a/internal/sync/file_backups.go b/internal/sync/file_backups.go index 554ab6f02..ac2276d6c 100644 --- a/internal/sync/file_backups.go +++ b/internal/sync/file_backups.go @@ -200,7 +200,7 @@ func RestoreFileBackup(path, id string, unlink bool) (string, error) { return saved, err } if v.junction { - err = createJunction(path, v.LinkTo) + err = utils.CreateJunction(path, v.LinkTo) } else { err = os.Symlink(v.LinkTo, path) } diff --git a/internal/sync/symlink_unix.go b/internal/sync/symlink_unix.go index 92069f22a..a9115e00b 100644 --- a/internal/sync/symlink_unix.go +++ b/internal/sync/symlink_unix.go @@ -3,7 +3,6 @@ package sync import ( - "fmt" "os" "path/filepath" ) @@ -43,8 +42,3 @@ func isJunctionOrSymlink(path string) bool { } return info.Mode()&os.ModeSymlink != 0 } - -// createJunction cannot recreate a Windows junction on another platform. -func createJunction(linkPath, sourcePath string) error { - return fmt.Errorf("cannot restore Windows junction %s to %s on this platform", linkPath, sourcePath) -} diff --git a/internal/sync/symlink_windows.go b/internal/sync/symlink_windows.go index ba667c11f..59c17c278 100644 --- a/internal/sync/symlink_windows.go +++ b/internal/sync/symlink_windows.go @@ -3,13 +3,10 @@ package sync import ( - "bytes" "errors" "fmt" "os" - "os/exec" "path/filepath" - "strings" gosync "sync" "skillshare/internal/utils" @@ -64,7 +61,7 @@ func createLink(linkPath, sourcePath string, relative bool, sourceRoot ...string } // Try junction (no admin required, but requires absolute paths) - junctionErr := createJunction(absTarget, absSource) + junctionErr := utils.CreateJunction(absTarget, absSource) if junctionErr == nil { return nil } @@ -81,18 +78,6 @@ func createLink(linkPath, sourcePath string, relative bool, sourceRoot ...string return errors.New(errMsg) } -// createJunction is also used to restore an original junction exactly, without -// requiring symlink privileges or falling back to a different link kind. -func createJunction(linkPath, sourcePath string) error { - var stderr bytes.Buffer - cmd := exec.Command("cmd", "/c", "mklink", "/J", linkPath, sourcePath) - cmd.Stderr = &stderr - if err := cmd.Run(); err != nil { - return fmt.Errorf("mklink /J: %w: %s", err, strings.TrimSpace(stderr.String())) - } - return nil -} - // canCreateRelativeLink probes whether the OS can create relative symlinks. // On Windows this requires Developer Mode; without it createLink falls back // to junctions which are always absolute. diff --git a/internal/trash/trash.go b/internal/trash/trash.go index d8bf1e073..ee0735170 100644 --- a/internal/trash/trash.go +++ b/internal/trash/trash.go @@ -469,7 +469,7 @@ func moveLink(src, dst string, rename func(string, string) error) error { return err } } - if err := os.Symlink(target, dst); err != nil { + if err := recreateLink(src, target, dst); err != nil { return err } if err := os.Remove(src); err != nil { @@ -480,6 +480,16 @@ func moveLink(src, dst string, rename func(string, string) error) error { return nil } +// recreateLink creates at dst a link to target of the same kind as the one at +// src. A junction stays a junction: a symlink would need Developer Mode or +// elevation on Windows, which the junction was chosen to avoid. +func recreateLink(src, target, dst string) error { + if utils.IsJunction(src) { + return utils.CreateJunction(dst, target) + } + return os.Symlink(target, dst) +} + // RestoreAgent restores agent files from a trashed directory back to the agent source. // Unlike Restore (which moves the whole directory), this copies individual files // from the trashed directory to destDir (since agents are file-based, not directory-based). @@ -601,7 +611,7 @@ func copyDir(src, dst string) error { } // Preserve link text without copying the target. Junctions may need - // ResolveLinkTarget, as in moveLink, then a symlink fallback. + // ResolveLinkTarget, as in moveLink, and are recreated as junctions. if utils.IsLinkMode(srcPath, info.Mode()) { target, err := os.Readlink(srcPath) if err != nil { @@ -610,7 +620,7 @@ func copyDir(src, dst string) error { if err != nil { return err } - if err := os.Symlink(target, dstPath); err != nil { + if err := recreateLink(srcPath, target, dstPath); err != nil { return err } continue diff --git a/internal/trash/trash_junction_windows_test.go b/internal/trash/trash_junction_windows_test.go new file mode 100644 index 000000000..3c62da44c --- /dev/null +++ b/internal/trash/trash_junction_windows_test.go @@ -0,0 +1,66 @@ +//go:build windows + +package trash + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "skillshare/internal/utils" +) + +func crossDevice(string, string) error { return fmt.Errorf("cross-device rename") } + +func TestMoveJunctionAcrossVolumesStaysJunction(t *testing.T) { + base := t.TempDir() + target := filepath.Join(base, "checkout") + source := filepath.Join(base, "source") + os.MkdirAll(target, 0755) + os.MkdirAll(source, 0755) + link := filepath.Join(source, "_dev-skills") + if err := utils.CreateJunction(link, target); err != nil { + t.Skip(err) + } + + trashed, err := moveToTrash(link, "_dev-skills", filepath.Join(base, "trash"), crossDevice) + if err != nil { + t.Fatal(err) + } + if !utils.IsJunction(trashed) { + t.Fatalf("trashed entry is not a junction") + } + items := List(filepath.Join(base, "trash")) + if len(items) != 1 { + t.Fatalf("list = %+v", items) + } + if err := restore(&items[0], source, crossDevice); err != nil { + t.Fatal(err) + } + if !utils.IsJunction(link) { + t.Fatalf("restored entry is not a junction") + } + if got, err := utils.ResolveLinkTarget(link); err != nil || got != target { + t.Fatalf("restored target = %q, %v; want %q", got, err, target) + } +} + +func TestMoveJunctionFailureKeepsOriginal(t *testing.T) { + base := t.TempDir() + target := filepath.Join(base, "checkout") + os.MkdirAll(target, 0755) + link := filepath.Join(base, "link") + if err := utils.CreateJunction(link, target); err != nil { + t.Skip(err) + } + dst := filepath.Join(base, "occupied") + os.WriteFile(dst, nil, 0644) // mklink /J refuses an existing destination + + if err := moveLink(link, dst, crossDevice); err == nil { + t.Fatal("expected an error") + } + if !utils.IsJunction(link) { + t.Fatal("original junction was not kept") + } +} diff --git a/internal/utils/link.go b/internal/utils/link.go index ebf6c1c61..a7ff603b8 100644 --- a/internal/utils/link.go +++ b/internal/utils/link.go @@ -26,6 +26,12 @@ func IsLinkMode(path string, m fs.FileMode) bool { return m&fs.ModeIrregular != 0 && isJunction(path) } +// IsJunction reports whether path is a Windows junction, as opposed to a symlink. +func IsJunction(path string) bool { + info, err := os.Lstat(path) + return err == nil && info.Mode()&fs.ModeIrregular != 0 && isJunction(path) +} + // isJunction reports whether path is a Windows junction (mount point). // A variable so tests on other platforms can simulate one. var isJunction = platformIsJunction diff --git a/internal/utils/link_unix.go b/internal/utils/link_unix.go index 12a6ecce1..cd9c33029 100644 --- a/internal/utils/link_unix.go +++ b/internal/utils/link_unix.go @@ -2,5 +2,12 @@ package utils +import "fmt" + // platformIsJunction returns false: junctions exist only on Windows. func platformIsJunction(string) bool { return false } + +// CreateJunction cannot recreate a Windows junction on another platform. +func CreateJunction(linkPath, sourcePath string) error { + return fmt.Errorf("cannot restore Windows junction %s to %s on this platform", linkPath, sourcePath) +} diff --git a/internal/utils/link_windows.go b/internal/utils/link_windows.go index 505eee802..20f2d6b96 100644 --- a/internal/utils/link_windows.go +++ b/internal/utils/link_windows.go @@ -2,7 +2,13 @@ package utils -import "syscall" +import ( + "bytes" + "fmt" + "os/exec" + "strings" + "syscall" +) // ioReparseTagMountPoint is the reparse tag of a junction (IO_REPARSE_TAG_MOUNT_POINT). const ioReparseTagMountPoint = 0xA0000003 @@ -23,3 +29,15 @@ func platformIsJunction(path string) bool { // For a reparse point, FindFirstFile reports its tag in Reserved0. return data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 && data.Reserved0 == ioReparseTagMountPoint } + +// CreateJunction creates a directory junction (no admin required). It never +// falls back to a symlink, so callers can restore a junction exactly. +func CreateJunction(linkPath, sourcePath string) error { + var stderr bytes.Buffer + cmd := exec.Command("cmd", "/c", "mklink", "/J", linkPath, sourcePath) + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("mklink /J: %w: %s", err, strings.TrimSpace(stderr.String())) + } + return nil +} From 377eae3d89501e11d90723b3e2278126d0e8fb9d Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 00:44:26 +0800 Subject: [PATCH 2/7] fix(utils): quote junction paths passed to cmd.exe CreateJunction passed both paths to `cmd /c mklink` as plain exec arguments. Go quotes an argument only when it has whitespace, so a path with a cmd metacharacter such as `&` was split into a second command: the junction was not created, and a crafted name could run a command. Build the command line ourselves and quote both paths. A Windows path cannot contain a double quote, so the quoting cannot be broken out of. The regression test uses unspaced paths with `&`, which fail without this change. Refs #420 --- internal/utils/link_windows.go | 8 +++++++- internal/utils/link_windows_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 internal/utils/link_windows_test.go diff --git a/internal/utils/link_windows.go b/internal/utils/link_windows.go index 20f2d6b96..7e920f868 100644 --- a/internal/utils/link_windows.go +++ b/internal/utils/link_windows.go @@ -34,7 +34,13 @@ func platformIsJunction(path string) bool { // falls back to a symlink, so callers can restore a junction exactly. func CreateJunction(linkPath, sourcePath string) error { var stderr bytes.Buffer - cmd := exec.Command("cmd", "/c", "mklink", "/J", linkPath, sourcePath) + cmd := exec.Command("cmd") + // Go quotes an argument only when it has whitespace, so a path with + // cmd metacharacters such as & would split into a second command. A + // Windows path cannot contain a double quote, so quoting both is safe. + cmd.SysProcAttr = &syscall.SysProcAttr{ + CmdLine: `cmd /c mklink /J "` + linkPath + `" "` + sourcePath + `"`, + } cmd.Stderr = &stderr if err := cmd.Run(); err != nil { return fmt.Errorf("mklink /J: %w: %s", err, strings.TrimSpace(stderr.String())) diff --git a/internal/utils/link_windows_test.go b/internal/utils/link_windows_test.go new file mode 100644 index 000000000..9fb711d66 --- /dev/null +++ b/internal/utils/link_windows_test.go @@ -0,0 +1,26 @@ +//go:build windows + +package utils + +import ( + "os" + "path/filepath" + "testing" +) + +func TestCreateJunctionPathWithCmdMetacharacters(t *testing.T) { + base := t.TempDir() + target := filepath.Join(base, "target&more") + os.MkdirAll(target, 0755) + link := filepath.Join(base, "link&echo") + + if err := CreateJunction(link, target); err != nil { + t.Fatal(err) + } + if !IsJunction(link) { + t.Fatal("link is not a junction") + } + if got, err := ResolveLinkTarget(link); err != nil || got != target { + t.Fatalf("target = %q, %v; want %q", got, err, target) + } +} From eb359a762627c5d58cb92fe3fb985f0145ee2172 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 00:52:54 +0800 Subject: [PATCH 3/7] fix(utils): create junctions through the reparse-point API Quoting the paths for `cmd /c mklink` stops `&` from splitting the command, but cmd.exe still expands `%VAR%` inside quotes, so a path with a token such as `%TEMP%` pointed the junction somewhere else or failed. Create the directory and set its mount-point reparse data with FSCTL_SET_REPARSE_POINT instead. No shell parses the paths, which also removes the need for quoting. Like mklink /J it refuses an existing link path, and it removes the directory it made if setting the reparse data fails. Refs #420 --- internal/utils/link_windows.go | 71 +++++++++++++++++++++++------ internal/utils/link_windows_test.go | 4 +- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/internal/utils/link_windows.go b/internal/utils/link_windows.go index 7e920f868..cb0d72b7a 100644 --- a/internal/utils/link_windows.go +++ b/internal/utils/link_windows.go @@ -3,10 +3,9 @@ package utils import ( - "bytes" "fmt" - "os/exec" - "strings" + "os" + "path/filepath" "syscall" ) @@ -30,20 +29,62 @@ func platformIsJunction(path string) bool { return data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 && data.Reserved0 == ioReparseTagMountPoint } -// CreateJunction creates a directory junction (no admin required). It never -// falls back to a symlink, so callers can restore a junction exactly. +// fsctlSetReparsePoint is FSCTL_SET_REPARSE_POINT. +const fsctlSetReparsePoint = 0x000900A4 + +// CreateJunction creates a directory junction (no admin required) through the +// reparse-point API, so no shell ever parses the paths. It never falls back to +// a symlink, so callers can restore a junction exactly. Like mklink /J it fails +// when linkPath already exists. func CreateJunction(linkPath, sourcePath string) error { - var stderr bytes.Buffer - cmd := exec.Command("cmd") - // Go quotes an argument only when it has whitespace, so a path with - // cmd metacharacters such as & would split into a second command. A - // Windows path cannot contain a double quote, so quoting both is safe. - cmd.SysProcAttr = &syscall.SysProcAttr{ - CmdLine: `cmd /c mklink /J "` + linkPath + `" "` + sourcePath + `"`, + target, err := filepath.Abs(sourcePath) + if err != nil { + return fmt.Errorf("create junction %s: %w", linkPath, err) } - cmd.Stderr = &stderr - if err := cmd.Run(); err != nil { - return fmt.Errorf("mklink /J: %w: %s", err, strings.TrimSpace(stderr.String())) + if err := os.Mkdir(linkPath, 0777); err != nil { + return err + } + if err := setJunctionTarget(linkPath, target); err != nil { + os.Remove(linkPath) + return fmt.Errorf("create junction %s: %w", linkPath, err) } return nil } + +func setJunctionTarget(linkPath, target string) error { + name, err := syscall.UTF16PtrFromString(linkPath) + if err != nil { + return err + } + h, err := syscall.CreateFile(name, syscall.GENERIC_WRITE, 0, nil, syscall.OPEN_EXISTING, + syscall.FILE_FLAG_BACKUP_SEMANTICS|syscall.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + return err + } + defer syscall.CloseHandle(h) + + sub, err := syscall.UTF16FromString(`\??\` + target) + if err != nil { + return err + } + print, err := syscall.UTF16FromString(target) + if err != nil { + return err + } + // sub and print include their NUL terminators; the name lengths exclude them. + subBytes, printBytes := (len(sub)-1)*2, (len(print)-1)*2 + pathBytes := len(sub)*2 + len(print)*2 + buf := make([]byte, 16+pathBytes) + put16 := func(off int, v int) { buf[off], buf[off+1] = byte(v), byte(v>>8) } + buf[0], buf[1], buf[2], buf[3] = 0x03, 0x00, 0x00, 0xA0 // IO_REPARSE_TAG_MOUNT_POINT + put16(4, 8+pathBytes) // ReparseDataLength + put16(8, 0) // SubstituteNameOffset + put16(10, subBytes) + put16(12, len(sub)*2) // PrintNameOffset + put16(14, printBytes) + for i, c := range append(append([]uint16{}, sub...), print...) { + put16(16+i*2, int(c)) + } + var returned uint32 + return syscall.DeviceIoControl(h, fsctlSetReparsePoint, &buf[0], uint32(len(buf)), nil, 0, &returned, nil) +} diff --git a/internal/utils/link_windows_test.go b/internal/utils/link_windows_test.go index 9fb711d66..636bb53bd 100644 --- a/internal/utils/link_windows_test.go +++ b/internal/utils/link_windows_test.go @@ -10,9 +10,9 @@ import ( func TestCreateJunctionPathWithCmdMetacharacters(t *testing.T) { base := t.TempDir() - target := filepath.Join(base, "target&more") + target := filepath.Join(base, "target&more%TEMP%") os.MkdirAll(target, 0755) - link := filepath.Join(base, "link&echo") + link := filepath.Join(base, "link&echo%TEMP%") if err := CreateJunction(link, target); err != nil { t.Fatal(err) From 3375419c6330190d00b6846c931d2371176b6d11 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 00:54:38 +0800 Subject: [PATCH 4/7] test(sourcefs): allow the raw writes in CreateJunction The previous commit moved junction creation onto os.Mkdir and os.Remove inside CreateJunction. The raw-write ratchet lists every such call, so record them with the same reason as the neighbouring link helpers. Refs #420 --- internal/sourcefs/testdata/raw_writes.tsv | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/sourcefs/testdata/raw_writes.tsv b/internal/sourcefs/testdata/raw_writes.tsv index ce455a87d..8f34c6c71 100644 --- a/internal/sourcefs/testdata/raw_writes.tsv +++ b/internal/sourcefs/testdata/raw_writes.tsv @@ -509,6 +509,8 @@ internal/uidist/uidist.go extractTarGz os.OpenFile target config internal/utils/frontmatter_write.go SetFrontmatterList os.WriteFile filePath source-unmigrated internal/utils/frontmatter_write.go ToggleFrontmatterFlag os.WriteFile filePath source-unmigrated internal/utils/frontmatter_write.go ToggleFrontmatterFlag os.WriteFile filePath source-unmigrated +internal/utils/link_windows.go CreateJunction os.Mkdir linkPath source-unmigrated +internal/utils/link_windows.go CreateJunction os.Remove linkPath source-unmigrated internal/version/check.go ClearCache os.Remove getCachePath() config internal/version/check.go cleanupLegacyCache os.Remove legacyPath config internal/version/check.go cleanupLegacyCache os.Remove parentDir config From 4b2da26f9a256c07a18a984d73a5cdca97379781 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 01:02:09 +0800 Subject: [PATCH 5/7] fix(utils): accept extended-length paths as junction targets The junction substitute name is an NT path (\??\C:\x). A target given as a Win32 extended path (\\?\C:\x or \\?\Volume{GUID}\x) kept its prefix, so prepending \??\ produced an invalid name and the junction could not be read through. Swap the prefix instead of stacking it. Refs #420 --- internal/utils/link_windows.go | 8 ++++++-- internal/utils/link_windows_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/internal/utils/link_windows.go b/internal/utils/link_windows.go index cb0d72b7a..36c1f7119 100644 --- a/internal/utils/link_windows.go +++ b/internal/utils/link_windows.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "syscall" ) @@ -63,11 +64,14 @@ func setJunctionTarget(linkPath, target string) error { } defer syscall.CloseHandle(h) - sub, err := syscall.UTF16FromString(`\??\` + target) + // The substitute name is an NT path: \??\C:\x. A Win32 extended path such + // as \\?\C:\x or \\?\Volume{GUID}\x only needs its prefix swapped. + display := strings.TrimPrefix(target, `\\?\`) + sub, err := syscall.UTF16FromString(`\??\` + display) if err != nil { return err } - print, err := syscall.UTF16FromString(target) + print, err := syscall.UTF16FromString(display) if err != nil { return err } diff --git a/internal/utils/link_windows_test.go b/internal/utils/link_windows_test.go index 636bb53bd..3e7bbb8cb 100644 --- a/internal/utils/link_windows_test.go +++ b/internal/utils/link_windows_test.go @@ -24,3 +24,18 @@ func TestCreateJunctionPathWithCmdMetacharacters(t *testing.T) { t.Fatalf("target = %q, %v; want %q", got, err, target) } } + +func TestCreateJunctionExtendedPathTarget(t *testing.T) { + base := t.TempDir() + target := filepath.Join(base, "target") + os.MkdirAll(target, 0755) + os.WriteFile(filepath.Join(target, "marker"), []byte("x"), 0644) + link := filepath.Join(base, "link") + + if err := CreateJunction(link, `\\?\`+target); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(link, "marker")); err != nil { + t.Fatalf("cannot read through the junction: %v", err) + } +} From 05b2f12023cdf9cb8bc5dc23974e426b05429db9 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 01:16:37 +0800 Subject: [PATCH 6/7] fix(utils): detect junctions under GODEBUG=winsymlink=0 With winsymlink=0 Go reports a junction as ModeSymlink instead of ModeIrregular, so IsJunction returned false and the trash fallback recreated the junction with os.Symlink again. Read the reparse tag for both modes. Refs #420 --- internal/utils/link.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/utils/link.go b/internal/utils/link.go index a7ff603b8..bfaad7580 100644 --- a/internal/utils/link.go +++ b/internal/utils/link.go @@ -27,9 +27,10 @@ func IsLinkMode(path string, m fs.FileMode) bool { } // IsJunction reports whether path is a Windows junction, as opposed to a symlink. +// With GODEBUG=winsymlink=0 Go reports a junction as ModeSymlink, so check both. func IsJunction(path string) bool { info, err := os.Lstat(path) - return err == nil && info.Mode()&fs.ModeIrregular != 0 && isJunction(path) + return err == nil && info.Mode()&(fs.ModeIrregular|fs.ModeSymlink) != 0 && isJunction(path) } // isJunction reports whether path is a Windows junction (mount point). From e031274207d4bf99b3b1daef714c85ce10923c42 Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 01:28:55 +0800 Subject: [PATCH 7/7] fix(utils): refuse a network path as a junction target A junction cannot point at a UNC path; mklink /J exits 1 for one. The reparse-point API would instead write an unusable mount point, so return an error up front and leave nothing behind, as mklink did. Refs #420 --- internal/utils/link_windows.go | 4 ++++ internal/utils/link_windows_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/internal/utils/link_windows.go b/internal/utils/link_windows.go index 36c1f7119..65373c1cd 100644 --- a/internal/utils/link_windows.go +++ b/internal/utils/link_windows.go @@ -42,6 +42,10 @@ func CreateJunction(linkPath, sourcePath string) error { if err != nil { return fmt.Errorf("create junction %s: %w", linkPath, err) } + // Like mklink /J, refuse a network path: a junction cannot point at one. + if strings.HasPrefix(target, `\\`) && !strings.HasPrefix(target, `\\?\`) { + return fmt.Errorf("create junction %s: a junction cannot target the network path %s", linkPath, target) + } if err := os.Mkdir(linkPath, 0777); err != nil { return err } diff --git a/internal/utils/link_windows_test.go b/internal/utils/link_windows_test.go index 3e7bbb8cb..bebf8d86e 100644 --- a/internal/utils/link_windows_test.go +++ b/internal/utils/link_windows_test.go @@ -39,3 +39,14 @@ func TestCreateJunctionExtendedPathTarget(t *testing.T) { t.Fatalf("cannot read through the junction: %v", err) } } + +func TestCreateJunctionRefusesNetworkPath(t *testing.T) { + link := filepath.Join(t.TempDir(), "link") + + if err := CreateJunction(link, `\\localhost\C$\Users`); err == nil { + t.Fatal("expected an error for a UNC target") + } + if _, err := os.Lstat(link); !os.IsNotExist(err) { + t.Fatalf("link was left behind: %v", err) + } +}