mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
filepath.Walk/WalkDir silently skip symlinked root directories because os.Lstat sees the root as a non-directory symlink and never enters it. This affected update, uninstall, reconcile, and server handlers when the source directory was a symlink (common with dotfiles managers). Extract ResolveSymlink() from sync.go into internal/utils/path.go and apply it at every filepath.Walk/WalkDir callsite on source/target dirs. Also add containment guards to resolveGroupSkills (uninstall) and resolveGroupUpdatable (update) to reject group directories that are symlinks pointing outside the source tree.
162 lines
4.3 KiB
Go
162 lines
4.3 KiB
Go
package install
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
func getUpdatableSkillsImpl(sourceDir string) ([]string, error) {
|
|
var skills []string
|
|
|
|
walkRoot := utils.ResolveSymlink(sourceDir)
|
|
err := filepath.Walk(walkRoot, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if path == walkRoot {
|
|
return nil
|
|
}
|
|
// Skip .git directories
|
|
if info.IsDir() && info.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
// Skip tracked repo directories (start with _)
|
|
if info.IsDir() && len(info.Name()) > 0 && info.Name()[0] == '_' {
|
|
return filepath.SkipDir
|
|
}
|
|
// Look for metadata files
|
|
if !info.IsDir() && info.Name() == MetaFileName {
|
|
skillDir := filepath.Dir(path)
|
|
relPath, relErr := filepath.Rel(walkRoot, skillDir)
|
|
if relErr != nil || relPath == "." {
|
|
return nil
|
|
}
|
|
meta, metaErr := ReadMeta(skillDir)
|
|
if metaErr != nil || meta == nil || meta.Source == "" {
|
|
return nil
|
|
}
|
|
skills = append(skills, relPath)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return skills, nil
|
|
}
|
|
|
|
// FindRepoInstalls scans sourceDir for skills whose meta repo_url matches
|
|
// cloneURL. Returns relative paths (e.g. "feature-radar/feature-radar-archive").
|
|
// Tracked repos (_-prefixed) are skipped.
|
|
func FindRepoInstalls(sourceDir, cloneURL string) []string {
|
|
if cloneURL == "" {
|
|
return nil
|
|
}
|
|
var matches []string
|
|
|
|
walkRoot := utils.ResolveSymlink(sourceDir)
|
|
filepath.Walk(walkRoot, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil || path == walkRoot {
|
|
return nil
|
|
}
|
|
if info.IsDir() && info.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
if info.IsDir() && len(info.Name()) > 0 && info.Name()[0] == '_' {
|
|
return filepath.SkipDir
|
|
}
|
|
if !info.IsDir() && info.Name() == MetaFileName {
|
|
skillDir := filepath.Dir(path)
|
|
relPath, relErr := filepath.Rel(walkRoot, skillDir)
|
|
if relErr != nil || relPath == "." {
|
|
return nil
|
|
}
|
|
meta, metaErr := ReadMeta(skillDir)
|
|
if metaErr != nil || meta == nil {
|
|
return nil
|
|
}
|
|
if repoURLsMatch(meta.RepoURL, cloneURL) {
|
|
matches = append(matches, relPath)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
return matches
|
|
}
|
|
|
|
// CheckCrossPathDuplicate checks if a repo is already installed at a different
|
|
// location in sourceDir. Returns a user-facing error when duplicates are found
|
|
// outside targetPrefix, or nil if safe to proceed.
|
|
// Callers should skip this check when force is true or cloneURL is empty.
|
|
func CheckCrossPathDuplicate(sourceDir, cloneURL, targetPrefix string) error {
|
|
existing := FindRepoInstalls(sourceDir, cloneURL)
|
|
if len(existing) == 0 {
|
|
return nil
|
|
}
|
|
var elsewhere []string
|
|
for _, rel := range existing {
|
|
sameLocation := false
|
|
if targetPrefix == "" {
|
|
sameLocation = !strings.Contains(rel, "/")
|
|
} else {
|
|
sameLocation = rel == targetPrefix || strings.HasPrefix(rel, targetPrefix+"/")
|
|
}
|
|
if !sameLocation {
|
|
elsewhere = append(elsewhere, rel)
|
|
}
|
|
}
|
|
if len(elsewhere) == 0 {
|
|
return nil
|
|
}
|
|
loc := elsewhere[0]
|
|
if len(elsewhere) > 1 {
|
|
loc = fmt.Sprintf("%s (and %d more)", loc, len(elsewhere)-1)
|
|
}
|
|
return fmt.Errorf(
|
|
"this repo is already installed at skills/%s\n"+
|
|
"Use 'skillshare update' to refresh, or reinstall with --force to allow duplicates",
|
|
loc)
|
|
}
|
|
|
|
// getTrackedReposImpl returns tracked repositories from the source directory.
|
|
// It walks subdirectories recursively so repos nested in organizational
|
|
// directories (e.g. category/_team-repo/) are found.
|
|
|
|
func getTrackedReposImpl(sourceDir string) ([]string, error) {
|
|
var repos []string
|
|
|
|
walkRoot := utils.ResolveSymlink(sourceDir)
|
|
err := filepath.Walk(walkRoot, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if path == walkRoot {
|
|
return nil
|
|
}
|
|
// Skip .git directories
|
|
if info.IsDir() && info.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
// Look for _-prefixed directories that are git repos
|
|
if info.IsDir() && len(info.Name()) > 0 && info.Name()[0] == '_' {
|
|
gitDir := filepath.Join(path, ".git")
|
|
if _, statErr := os.Stat(gitDir); statErr == nil {
|
|
relPath, relErr := filepath.Rel(walkRoot, path)
|
|
if relErr == nil {
|
|
repos = append(repos, relPath)
|
|
}
|
|
return filepath.SkipDir // Don't recurse into tracked repos
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return repos, nil
|
|
}
|