Files
skillshare/internal/sync/filelink.go
T
Willie a2d75300f6 fix(sync): prune only agent copies skillshare wrote in copy mode
Copy and extension modes pruned every unexpected .md (or output-extension)
file in the agents target, so agent files a user kept there were deleted
on the next sync, unlike skills copy mode, which prunes only manifest
entries.

Copy and transform outputs are now tracked with the same checksum
manifest the merge fallback already uses, and prune removes only tracked
copies the user has not edited. Existing copies are adopted while their
agent is still synced, and an untracked pre-transform .md that still
matches its agent is treated as a stale copy, so switching to an
extension keeps cleaning it up. Copies already orphaned before upgrading
are kept and must be removed by hand.

copyTracker.pruneOrphans now continues past a failed removal and joins
the errors, so one locked file no longer leaves the other orphans behind.
2026-10-01 22:10:08 +08:00

135 lines
4.0 KiB
Go

package sync
import (
"errors"
"fmt"
"os"
"path/filepath"
"skillshare/internal/utils"
)
// FileLinkFallbackWarning is reported when a mode that links single files
// syncs copies instead.
const FileLinkFallbackWarning = "file links need Windows Developer Mode; copying instead"
// canCreateFileLink reports whether this system can create symlinks to files.
// Windows junctions only work for directories, so without Developer Mode file
// link modes fall back to copy. A variable so tests can simulate that.
var canCreateFileLink = platformCanCreateFileLink
// CanCreateFileLink reports whether this system can create symlinks to files
// (false on Windows without Developer Mode). The probe runs once.
func CanCreateFileLink() bool { return canCreateFileLink() }
// SetFileLinksForTest overrides whether file links can be created and returns
// a func that restores the probe. Only for tests in other packages.
func SetFileLinksForTest(ok bool) (restore func()) {
prev := canCreateFileLink
canCreateFileLink = func() bool { return ok }
return func() { canCreateFileLink = prev }
}
// EffectiveAgentMode returns the mode agents actually sync with. Merge links
// each agent file, so it copies when file links are unavailable; symlink mode
// links the whole directory and is unaffected.
func EffectiveAgentMode(mode string) string {
m := EffectiveMode(mode)
if m == "merge" && !canCreateFileLink() {
return "copy"
}
return m
}
// ExtraTargetMode returns the mode an extras target actually syncs with.
// Merge links each file, and symlink mode of a single-file extra links that
// file, so both copy when file links are unavailable. Symlink mode of a
// directory extra links the directory and is unaffected.
func ExtraTargetMode(mode string, singleFile bool) string {
m := EffectiveMode(mode)
if !canCreateFileLink() && (m == "merge" || (singleFile && m == "symlink")) {
return "copy"
}
return m
}
// copyTracker records files copied in place of file links in the target's
// manifest, so later syncs can tell them from the user's own files. A tracked
// file that still matches its recorded hash belongs to skillshare and may be
// updated, relinked, or pruned; an edited one is left alone.
type copyTracker struct {
dir string
m *Manifest
changed bool
}
func loadCopyTracker(dir string) *copyTracker {
m, err := ReadManifest(dir)
if err != nil {
m = &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64)}
}
return &copyTracker{dir: dir, m: m}
}
// owns reports whether rel is a tracked copy the user has not edited.
func (t *copyTracker) owns(rel string) bool {
sum, ok := t.m.Managed[filepath.ToSlash(rel)]
if !ok {
return false
}
h, err := utils.FileHash(filepath.Join(t.dir, rel))
return err == nil && h == sum
}
func (t *copyTracker) record(rel string) {
key := filepath.ToSlash(rel)
if h, err := utils.FileHash(filepath.Join(t.dir, rel)); err == nil && t.m.Managed[key] != h {
t.m.Managed[key] = h
t.changed = true
}
}
func (t *copyTracker) forget(rel string) {
key := filepath.ToSlash(rel)
if _, ok := t.m.Managed[key]; ok {
delete(t.m.Managed, key)
t.changed = true
}
}
// pruneOrphans removes tracked copies whose name is not in expected, keeping
// edited ones, and forgets them. It returns the removed names.
func (t *copyTracker) pruneOrphans(expected map[string]bool, dryRun bool) ([]string, error) {
var removed []string
var errs []error
for key := range t.m.Managed {
rel := filepath.FromSlash(key)
if expected[rel] {
continue
}
if t.owns(rel) {
if !dryRun {
if err := os.Remove(filepath.Join(t.dir, rel)); err != nil {
errs = append(errs, fmt.Errorf("failed to remove orphaned copy %s: %w", rel, err))
continue
}
}
removed = append(removed, rel)
}
if !dryRun {
t.forget(rel)
}
}
return removed, errors.Join(errs...)
}
func (t *copyTracker) save() error {
if !t.changed {
return nil
}
if len(t.m.Managed) == 0 {
return RemoveManifest(t.dir)
}
return WriteManifest(t.dir, t.m)
}