updateSkillInPlace installs the resolved update into a staging directory alongside the existing skill directory and, on success, atomically swaps the staged contents into place via same-filesystem renames. This guarantees: - The skill directory's own inode is preserved, so symlinks, mounts, and ext
(opts *UpdateOptions, u pendingUpdate, apiClient *api.Client, gitRoot, homeDir string)
| 468 | // skill completely untouched: existing files are first moved aside into |
| 469 | // a backup directory and restored if any subsequent step fails. |
| 470 | func updateSkillInPlace(opts *UpdateOptions, u pendingUpdate, apiClient *api.Client, gitRoot, homeDir string) error { |
| 471 | if u.local.dir == "" { |
| 472 | return fmt.Errorf("cannot update %s: no install location recorded", u.local.name) |
| 473 | } |
| 474 | |
| 475 | parent := filepath.Dir(u.local.dir) |
| 476 | if err := os.MkdirAll(parent, 0o755); err != nil { |
| 477 | return fmt.Errorf("could not ensure parent directory %s: %w", parent, err) |
| 478 | } |
| 479 | |
| 480 | // Stage as a sibling of the existing skill directory so the swap stays |
| 481 | // on the same filesystem and every rename is atomic. |
| 482 | staging, err := os.MkdirTemp(parent, "."+u.skill.Name+".gh-skill-update-") |
| 483 | if err != nil { |
| 484 | return fmt.Errorf("could not create staging directory: %w", err) |
| 485 | } |
| 486 | defer os.RemoveAll(staging) |
| 487 | |
| 488 | installOpts := &installer.Options{ |
| 489 | Host: u.local.repoHost, |
| 490 | Owner: u.local.owner, |
| 491 | Repo: u.local.repo, |
| 492 | RepoID: u.repoID, |
| 493 | Ref: u.resolved.Ref, |
| 494 | SHA: u.resolved.SHA, |
| 495 | Skills: []discovery.Skill{u.skill}, |
| 496 | Dir: staging, |
| 497 | GitRoot: gitRoot, |
| 498 | HomeDir: homeDir, |
| 499 | Client: apiClient, |
| 500 | } |
| 501 | if _, err := installer.Install(installOpts); err != nil { |
| 502 | return err |
| 503 | } |
| 504 | |
| 505 | stagedSkillDir := filepath.Join(staging, u.skill.Name) |
| 506 | if _, err := os.Stat(stagedSkillDir); err != nil { |
| 507 | return fmt.Errorf("installer did not produce %s: %w", stagedSkillDir, err) |
| 508 | } |
| 509 | |
| 510 | if err := os.MkdirAll(u.local.dir, 0o755); err != nil { |
| 511 | return fmt.Errorf("could not ensure skill directory %s: %w", u.local.dir, err) |
| 512 | } |
| 513 | |
| 514 | return swapDirectoryContents(u.local.dir, stagedSkillDir) |
| 515 | } |
| 516 | |
| 517 | // swapDirectoryContents replaces the entries inside dest with the entries |
| 518 | // inside src, preserving dest's inode. It first moves every existing entry |