mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Keep the canonical skills source root and logical relative tail when creating or reformatting project links, including Windows relative links and junction fallback. Shared agent and extra links retain full canonicalization, and global Unix link text stays unchanged. Allow group walks outside the source only for an enabled first-level link validated by Follow.Resolve, remapping children to logical paths and metadata keys while retaining nested-link guards and root-skipping semantics. Resolve copy and checksum operation roots before walking to avoid false cycles, and preserve followed source links during forced collect with per-item warnings. Regression coverage verifies second-sync ownership, status, prune, unlink, format changes, linked-root copying, group guards, and forced collect preservation. Linux build, vet, sync and command tests, sourcewalk ratchet, server collect tests, context validation, and Windows cross-build passed; Windows runtime was not verified. No new raw-walk allowance was needed.
117 lines
4.0 KiB
Go
117 lines
4.0 KiB
Go
package sync
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// shouldUseRelative returns true if both sourcePath and targetPath
|
|
// are under the given projectRoot, meaning a relative symlink
|
|
// between them would be portable across machines.
|
|
// Returns false if projectRoot is empty (global mode).
|
|
// Paths are resolved through EvalSymlinks so that symlinked ancestors
|
|
// do not fool the prefix check.
|
|
func shouldUseRelative(projectRoot, sourcePath, targetPath string) bool {
|
|
if projectRoot == "" {
|
|
return false
|
|
}
|
|
cleaned := evalOrClean(projectRoot)
|
|
prefix := cleaned
|
|
if prefix != string(filepath.Separator) {
|
|
prefix += string(filepath.Separator)
|
|
}
|
|
src := evalOrClean(sourcePath)
|
|
tgt := evalOrClean(targetPath)
|
|
|
|
srcUnder := utils.PathHasPrefix(src, prefix) || utils.PathsEqual(src, cleaned)
|
|
tgtUnder := utils.PathHasPrefix(tgt, prefix) || utils.PathsEqual(tgt, cleaned)
|
|
return srcUnder && tgtUnder
|
|
}
|
|
|
|
// evalOrClean resolves symlinks in path. When the full path does not exist yet
|
|
// (e.g. the target directory on the first sync), it resolves the deepest
|
|
// existing ancestor and re-appends the missing tail, so a symlinked ancestor
|
|
// (macOS /var → /private/var) is canonicalized consistently whether or not the
|
|
// leaf exists. Without this, shouldUseRelative would flip between runs — raw on
|
|
// the first sync (leaf missing) and resolved on the second (leaf present) —
|
|
// causing a needless absolute→relative reformat on every second sync.
|
|
func evalOrClean(p string) string {
|
|
p = filepath.Clean(p)
|
|
if resolved, err := filepath.EvalSymlinks(p); err == nil {
|
|
return resolved
|
|
}
|
|
tail := ""
|
|
cur := p
|
|
for {
|
|
parent := filepath.Dir(cur)
|
|
if parent == cur {
|
|
break // reached root without an existing ancestor
|
|
}
|
|
tail = filepath.Join(filepath.Base(cur), tail)
|
|
if resolved, err := filepath.EvalSymlinks(parent); err == nil {
|
|
return filepath.Join(resolved, tail)
|
|
}
|
|
cur = parent
|
|
}
|
|
return p
|
|
}
|
|
|
|
// resolveReadlink converts a raw os.Readlink result to an absolute path,
|
|
// resolving relative targets against the link's parent directory (not CWD).
|
|
func resolveReadlink(dest, linkPath string) string {
|
|
if filepath.IsAbs(dest) {
|
|
return dest
|
|
}
|
|
return filepath.Clean(filepath.Join(filepath.Dir(linkPath), dest))
|
|
}
|
|
|
|
// linkNeedsReformat returns true if dest (the raw os.Readlink result)
|
|
// uses the wrong format (relative vs absolute) for the desired mode.
|
|
func linkNeedsReformat(dest string, wantRelative bool) bool {
|
|
if dest == "" {
|
|
return false
|
|
}
|
|
if wantRelative && !canCreateRelativeLink() {
|
|
// Platform cannot create relative symlinks (e.g. Windows without
|
|
// Developer Mode falls back to junctions, which are always absolute).
|
|
// Skip reformat to avoid remove→recreate loop on every sync.
|
|
return false
|
|
}
|
|
return wantRelative == filepath.IsAbs(dest)
|
|
}
|
|
|
|
// reformatLink atomically replaces an existing symlink with a new one
|
|
// using the specified format. It creates a temp link and renames it
|
|
// over the original so the link is never missing. Falls back to
|
|
// remove→create when rename fails (e.g. Windows junctions).
|
|
func reformatLink(linkPath, sourcePath string, relative bool, sourceRoot ...string) error {
|
|
tmpPath := linkPath + ".ss-reformat"
|
|
os.Remove(tmpPath) // clean up stale temp
|
|
if err := createLink(tmpPath, sourcePath, relative, sourceRoot...); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(tmpPath, linkPath); err == nil {
|
|
return nil
|
|
}
|
|
// Rename failed (Windows junction, cross-device, etc.); fall back
|
|
os.Remove(tmpPath)
|
|
if err := os.Remove(linkPath); err != nil {
|
|
return fmt.Errorf("failed to remove old link: %w", err)
|
|
}
|
|
return createLink(linkPath, sourcePath, relative, sourceRoot...)
|
|
}
|
|
|
|
// linkSourcePath canonicalizes a skills source root without resolving its
|
|
// logical descendants. Other resource links retain full canonicalization.
|
|
func linkSourcePath(sourcePath string, sourceRoot []string) string {
|
|
if len(sourceRoot) > 0 {
|
|
if tail, err := filepath.Rel(sourceRoot[0], sourcePath); err == nil {
|
|
return filepath.Join(evalOrClean(sourceRoot[0]), tail)
|
|
}
|
|
}
|
|
return evalOrClean(sourcePath)
|
|
}
|