mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
follow_source_links only follows a link the user made by hand, so the guards (source or parent, sync target overlap, link chain) surfaced in doctor only after the link existed, and Windows users had to pick between a junction and mklink /D themselves. link checks the prospective target with Follow.Allow, which shares the guard rules and reason strings with the walk, before creating a junction on Windows or an absolute symlink elsewhere. It warns when the target is not a git checkout and leaves follow_source_links alone unless --enable is given. unlink moves only the link entry to trash, the same way uninstall removes a first-level link, so the target is never touched and trash restore undoes it. internal/sourcelink holds both operations so the dashboard can call them too. Refs #419
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
// Package sourcelink creates and removes the first-level links under the
|
|
// skills source that follow_source_links follows. The CLI and the dashboard
|
|
// both call it, so a link is refused for the same reasons a walk would skip it.
|
|
package sourcelink
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/sourcefs"
|
|
"skillshare/internal/sourcewalk"
|
|
"skillshare/internal/sync"
|
|
"skillshare/internal/trash"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// Result describes a created link.
|
|
type Result struct {
|
|
Path string // the link, <source>/<name>
|
|
Target string // absolute path the link points at
|
|
Kind string // "symlink" or "junction"
|
|
Warning string // non-empty when the target is not a git checkout
|
|
}
|
|
|
|
// DefaultName is the link name used when none is given: "_" and the target's
|
|
// base name.
|
|
func DefaultName(target string) string {
|
|
return "_" + filepath.Base(filepath.Clean(target))
|
|
}
|
|
|
|
// Create links <source>/<name> to target, a directory. targets are the skills
|
|
// paths of the active sync targets. An empty name means DefaultName. It
|
|
// refuses a target a walk would not follow (see sourcewalk.Follow.Allow) and
|
|
// a name that already exists. On Windows it makes a junction, or a directory
|
|
// symlink when a junction cannot be made; elsewhere an absolute symlink.
|
|
func Create(source string, targets []string, target, name string) (Result, error) {
|
|
abs, err := filepath.Abs(target)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if name == "" {
|
|
name = DefaultName(abs)
|
|
}
|
|
if err := checkName(name); err != nil {
|
|
return Result{}, err
|
|
}
|
|
r, err := sourcefs.Open(source)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
defer r.Close()
|
|
if _, err := r.Lstat(name); err == nil {
|
|
return Result{}, fmt.Errorf("%s already exists", name)
|
|
} else if !os.IsNotExist(err) {
|
|
return Result{}, err
|
|
}
|
|
if err := sourcewalk.NewFollow(source, targets).Allow(abs); err != nil {
|
|
return Result{}, err
|
|
}
|
|
link := filepath.Join(source, name)
|
|
if err := sync.CreateSymlink(link, abs, ""); err != nil {
|
|
return Result{}, err
|
|
}
|
|
res := Result{Path: link, Target: abs, Kind: "junction"}
|
|
if info, err := os.Lstat(link); err == nil && info.Mode()&os.ModeSymlink != 0 {
|
|
res.Kind = "symlink"
|
|
}
|
|
if _, err := os.Stat(filepath.Join(abs, ".git")); err != nil {
|
|
res.Warning = "target is not a git checkout"
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// Remove moves the link <source>/<name> to trashDir, the way uninstall
|
|
// removes a first-level link: only the link entry moves, its target stays.
|
|
// It refuses a name that is not a first-level link.
|
|
func Remove(source, trashDir, name string) error {
|
|
name = strings.TrimRight(name, `/\`)
|
|
if err := checkName(name); err != nil {
|
|
return err
|
|
}
|
|
link := filepath.Join(source, name)
|
|
info, err := os.Lstat(link)
|
|
if os.IsNotExist(err) {
|
|
return fmt.Errorf("%s not found in source", name)
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
if !utils.IsLinkMode(link, info.Mode()) {
|
|
return fmt.Errorf("%s is not a link", name)
|
|
}
|
|
if err := sourcefs.CheckMoveOut(source, link); err != nil {
|
|
return err
|
|
}
|
|
if _, err := trash.MoveToTrash(link, name, trashDir); err != nil {
|
|
return fmt.Errorf("failed to move to trash: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkName refuses a name that is not a single entry directly under the
|
|
// source.
|
|
func checkName(name string) error {
|
|
if name == "" || name == "." || name == ".." || name != filepath.Base(name) || strings.ContainsAny(name, `/\`) {
|
|
return fmt.Errorf("%q is not a first-level name", name)
|
|
}
|
|
return nil
|
|
}
|