Files
skillshare/internal/install/source_write.go
Willie 92183d5a09 test(windows): run audit, config, install, mcp and cmd tests on Windows CI
These packages were left out of the windows-test job because of failures
that came from test assumptions (POSIX paths, permission bits, HOME,
bash shims posing as git), not product behavior. Fix them and add the
packages to the list so they stay green.

Two failures were real product bugs:
- swapStagedIntoSource named its backup like the temp that
  sourcefs.MoveIn creates beside it. On a coarse clock (Windows) both
  names matched, the failed move removed the temp and with it the
  backup, and the old skill was lost.
- MetadataStore.GetByPath looked up OS-native paths in slash-form keys,
  so `update --group` missed nested skills on Windows.

The test sandbox now also sets USERPROFILE, which os.UserHomeDir reads
on Windows; tests were reading the real profile.

Refs #344
2026-10-06 10:46:37 +08:00

82 lines
2.5 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 {
// ".old." keeps the backup apart from the ".skillshare-<name>.<time>" temp that
// sourcefs.MoveIn creates beside it; a coarse clock (Windows) made both names equal,
// and the failed move then deleted the backup.
backup = filepath.Join(filepath.Dir(rel), ".skillshare-"+filepath.Base(rel)+".old."+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
}