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 +}