mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
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
This commit is contained in:
+1
-2
@@ -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
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 23 and column 89.
|
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
+13
-3
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user