Files
skillshare/internal/install/source_write.go
T
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

54 lines
1.5 KiB
Go

package install
import (
"fmt"
"skillshare/internal/sourcefs"
"skillshare/internal/sourcewalk"
)
// removeInSource removes destPath, below the skills source sourceDir, through
// the source-write handle, so a link at or above it is refused instead of
// replaced.
func removeInSource(sourceDir, destPath string, follow *sourcewalk.Follow) error {
src, err := sourcefs.Open(sourceDir, follow)
if err != nil {
return err
}
defer src.Close()
rel, err := src.Rel(destPath)
if err != nil {
return err
}
return src.RemoveAll(rel)
}
// swapStagedIntoSource replaces destPath, below the skills source sourceDir,
// with the staged directory. The rename crosses the source's edge, so the
// in-source side is checked through the handle first. Only a cross-device
// rename falls back to a copy, and that copy also goes through the handle;
// every other failure, a refused link included, is returned as is.
func swapStagedIntoSource(sourceDir, staged, destPath string, follow *sourcewalk.Follow) error {
src, err := sourcefs.Open(sourceDir, follow)
if err != nil {
return err
}
defer src.Close()
rel, err := src.Rel(destPath)
if err != nil {
return err
}
if err := src.RemoveAll(rel); err != nil {
return fmt.Errorf("failed to remove existing skill: %w", err)
}
if err := src.MoveIn(staged, rel); err != nil {
if !sourcefs.IsCrossDevice(err) {
return fmt.Errorf("failed to move updated skill: %w", err)
}
if err := src.CopyIn(staged, rel); err != nil {
return fmt.Errorf("failed to copy updated skill: %w", err)
}
}
return nil
}