Files
skillshare/internal/install/source_write.go
T
Willie 51f2beadd1 fix(install): retain existing skills until replacement succeeds
Copy staged directory links as concrete content through the source root, skip dangling links, and reject directory cycles. Preserve the existing skill under a sibling name and restore it if moving or copying the replacement fails so followed checkout updates cannot discard the old content.
2026-10-05 22:28:43 +08:00

79 lines
2.3 KiB
Go

package install
import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"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. The old
// skill is kept under a sibling name until replacement succeeds and restored
// if copying or moving fails.
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.CheckNoLink(rel); err != nil {
return err
}
backup := ""
if _, err := src.Lstat(rel); err == nil {
backup = filepath.Join(filepath.Dir(rel), ".skillshare-"+filepath.Base(rel)+"."+strconv.FormatInt(time.Now().UnixNano(), 36))
if err := src.Rename(rel, backup); err != nil {
return fmt.Errorf("failed to preserve existing skill: %w", err)
}
} else if !os.IsNotExist(err) {
return err
}
moveErr := src.MoveIn(staged, rel)
if sourcefs.IsCrossDevice(moveErr) {
moveErr = src.CopyIn(staged, rel)
}
if moveErr != nil {
if err := src.RemoveAll(rel); err != nil {
return fmt.Errorf("failed to move updated skill: %w; cleanup failed: %v", moveErr, err)
}
if backup != "" {
if err := src.Rename(backup, rel); err != nil {
return fmt.Errorf("failed to move updated skill: %w; restore failed: %v", moveErr, err)
}
}
return fmt.Errorf("failed to move updated skill: %w", moveErr)
}
if backup != "" {
return src.RemoveAll(backup)
}
return nil
}