Files
skillshare/internal/install/source_write_linux_test.go
Willie a502e9316b feat(sourcefs): write through followed first-level source links
With follow_source_links on, discovery reports <source>/_dev-skills/foo,
but every write to it was refused: sourcefs rejects a link in the path, and
its source-root os.Root refuses descendants outside the source. Dashboard
edits, nested uninstall, regular skill replacement, and tracked installs
under such a link all failed, although writing through a link the user
placed is the agreed behavior (#410).

sourcefs.Open, Create, MkdirAllIn, and CheckMoveOut take the operation's
optional sourcewalk.Follow. A clean name <link>/<rest> whose first
component the policy follows is handled by a second os.Root opened at the
link's canonical target, with the same per-component link check on <rest>,
so a nested link or ".." still cannot escape the target. Names with ".."
keep the source root, which refuses them through the link. Without a
policy nothing changes.

The link entry itself stays a link: RemoveAll, Rename, and writes on it are
refused, install --track --force still refuses to replace it, and
CheckMoveOut still lets trash rename the link without following it.
MkdirAll on a followed link is a no-op, since it already is that directory;
install --into needs that. A rename across the link boundary is refused.

The install, update, uninstall, and server callers pass the same policy
their discovery uses, through InstallOptions.SourceFollow and the update
context. No raw-write allowance was needed.

Refs #274
2026-10-05 14:42:09 +08:00

55 lines
1.6 KiB
Go

//go:build linux
package install
import (
"os"
"path/filepath"
"syscall"
"testing"
)
// TestSwapStagedIntoSourceCrossDevice stages on /dev/shm, a different
// filesystem from the test temp dir, so MoveIn's rename fails with EXDEV and
// the copy fallback runs through the source handle.
func TestSwapStagedIntoSourceCrossDevice(t *testing.T) {
source := t.TempDir()
staged, err := os.MkdirTemp("/dev/shm", "skillshare-stage-")
if err != nil {
t.Skip("/dev/shm not available:", err)
}
t.Cleanup(func() { os.RemoveAll(staged) })
if dev(t, source) == dev(t, staged) {
t.Skip("/dev/shm shares a filesystem with the temp dir")
}
if err := os.WriteFile(filepath.Join(staged, "SKILL.md"), []byte("staged"), 0o644); err != nil {
t.Fatal(err)
}
external := t.TempDir()
if err := os.Symlink(external, filepath.Join(source, "linked")); err != nil {
t.Fatal(err)
}
if err := swapStagedIntoSource(source, staged, filepath.Join(source, "plain"), nil); err != nil {
t.Fatalf("cross-device swap: %v", err)
}
if data, _ := os.ReadFile(filepath.Join(source, "plain", "SKILL.md")); string(data) != "staged" {
t.Fatalf("plain skill = %q, want staged content", data)
}
if err := swapStagedIntoSource(source, staged, filepath.Join(source, "linked", "child"), nil); err == nil {
t.Fatal("cross-device swap below a link must be refused")
}
if entries, _ := os.ReadDir(external); len(entries) != 0 {
t.Fatalf("external tree changed: %v", entries)
}
}
func dev(t *testing.T, path string) uint64 {
t.Helper()
var st syscall.Stat_t
if err := syscall.Stat(path, &st); err != nil {
t.Fatal(err)
}
return uint64(st.Dev)
}