Files
magpie/internal/library/dirlink_windows.go
T
yetone ce9fc6758a library: on Windows without the right to make symlinks, skills are linked with junctions (#973) — with Developer Mode off and magpie not elevated, os.Symlink fails with "A required privilege is not held by the client": a skill installed from a folder became a detached copy, and bringing in a skill from ~/.agents/skills failed outright. The library's folder links (an agent's link to the library's skill, a folder source, a skill brought in from a link or the shared folder, a carried skill's folder, relink) now go through dirLink, which on that error makes a directory junction instead: one every user may make, followed by every program as a symlink to a folder is, made natively (FSCTL_SET_REPARSE_POINT, no mklink). A target a junction can't reach (a network share) still gets the old copy. Go reports a junction as irregular, not a symlink, so ours, unlink, carry's clearing of the library, a project's place and the CC Switch update now tell a link by linkEntry (symlink or junction) and take it away with os.Remove, leaving what it points at, even when where it points can't be read. On the Windows box with every privilege stripped from the token (CreateRestrictedToken DISABLE_MAX_PRIVILEGE, whoami /priv shows SeChangeNotify only) and an explicit sandbox environment, the library's tests went from 16 failures to none (TestSkillHow, TestProjectSkillsLinked, TestCarry, TestSkillCopyEditReachesTheOthers, TestImportSharedSkillForKimi …); with the privilege they pass too. TestJunctionIsALink (Windows: magpie's own, read through, unlinked without its target, none to a share) is new; the folder fixtures that ignored a failed os.Symlink now use dirLink, and the tests' link checks accept a junction. go test -race ./internal/library/ on the Mac, go vet, windows/linux(nogui) builds pass.
2026-10-06 13:32:03 +08:00

82 lines
2.5 KiB
Go

//go:build windows
package library
import (
"encoding/binary"
"errors"
"os"
"path/filepath"
"unicode/utf16"
"golang.org/x/sys/windows"
)
// dirLink makes link a link to the folder target. Windows lets a user make
// a symlink only with Developer Mode on or as an administrator (#973);
// without that it is a junction, which every user can make and every
// program follows as it does a symlink to a folder. A junction can't point
// at a network share: that is still the symlink's error.
func dirLink(target, link string) error {
err := os.Symlink(target, link)
if err == nil || !errors.Is(err, windows.ERROR_PRIVILEGE_NOT_HELD) {
return err
}
if jerr := junction(target, link); jerr != nil {
return err
}
return nil
}
// junction makes link a junction to the folder target: an empty folder
// given a mount-point reparse point. Nothing is left at link when it fails.
func junction(target, link string) error {
abs, err := filepath.Abs(target)
if err != nil {
return err
}
if v := filepath.VolumeName(abs); len(v) != 2 || v[1] != ':' {
return errors.New("a junction needs a local drive path: " + abs)
}
if err := os.Mkdir(link, 0o755); err != nil {
return err
}
if err := setMountPoint(link, abs); err != nil {
os.Remove(link)
return err
}
return nil
}
func setMountPoint(dir, target string) error {
p, err := windows.UTF16PtrFromString(dir)
if err != nil {
return err
}
h, err := windows.CreateFile(p, windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING,
windows.FILE_FLAG_OPEN_REPARSE_POINT|windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
if err != nil {
return err
}
defer windows.CloseHandle(h)
sub := utf16.Encode([]rune(`\??\` + target))
shown := utf16.Encode([]rune(target))
// REPARSE_DATA_BUFFER, MountPointReparseBuffer: both names end in a NUL
path := make([]uint16, 0, len(sub)+len(shown)+2)
path = append(append(append(append(path, sub...), 0), shown...), 0)
data := make([]byte, 8+2*len(path))
binary.LittleEndian.PutUint16(data[0:], 0)
binary.LittleEndian.PutUint16(data[2:], uint16(2*len(sub)))
binary.LittleEndian.PutUint16(data[4:], uint16(2*(len(sub)+1)))
binary.LittleEndian.PutUint16(data[6:], uint16(2*len(shown)))
for i, c := range path {
binary.LittleEndian.PutUint16(data[8+2*i:], c)
}
buf := make([]byte, 8+len(data))
binary.LittleEndian.PutUint32(buf[0:], windows.IO_REPARSE_TAG_MOUNT_POINT)
binary.LittleEndian.PutUint16(buf[4:], uint16(len(data)))
copy(buf[8:], data)
var n uint32
return windows.DeviceIoControl(h, windows.FSCTL_SET_REPARSE_POINT, &buf[0], uint32(len(buf)), nil, 0, &n, nil)
}