Files
skillshare/internal/utils/link.go
T
Willie 97499ebcc4 fix: recognise Windows junctions wherever links are detected
Since Go 1.23, os.Lstat reports NTFS junctions as irregular files instead
of symlinks (winsymlink, active for go 1.25 in go.mod). Every check that
looked only at os.ModeSymlink therefore missed the junctions skillshare
itself creates on Windows: linked skills could be treated as local folders,
backed up, copied through, or left behind on prune.

Detect links through utils.IsLinkMode, which also asks the OS about the
reparse tag of irregular entries, and count only real symlinks and
junctions as links so OneDrive placeholders and deduplicated files stay
regular files. restore.go also used os.Stat for its link check, which
follows the link and never matched.

Refs #299
2026-09-28 13:53:16 +08:00

50 lines
1.4 KiB
Go

package utils
import (
"io/fs"
"os"
"path/filepath"
)
// IsSymlinkOrJunction checks whether path is a symlink or Windows junction.
func IsSymlinkOrJunction(path string) bool {
info, err := os.Lstat(path)
if err != nil {
return false
}
return IsLinkMode(path, info.Mode())
}
// IsLinkMode reports whether the entry at path, whose Lstat or DirEntry mode
// is m, is a symlink or a Windows junction. Since Go 1.23 a junction is
// reported as ModeIrregular rather than ModeSymlink (and not as a directory),
// so only such entries cost an extra system call to read the reparse tag.
func IsLinkMode(path string, m fs.FileMode) bool {
if m&fs.ModeSymlink != 0 {
return true
}
return m&fs.ModeIrregular != 0 && isJunction(path)
}
// isJunction reports whether path is a Windows junction (mount point).
// A variable so tests on other platforms can simulate one.
var isJunction = platformIsJunction
// ResolveLinkTarget resolves the target path of a symlink or junction.
func ResolveLinkTarget(path string) (string, error) {
link, err := os.Readlink(path)
if err == nil {
if !filepath.IsAbs(link) {
link = filepath.Join(filepath.Dir(path), link)
}
return filepath.Abs(link)
}
// On Windows, Readlink may fail for junctions. EvalSymlinks still resolves.
resolved, evalErr := filepath.EvalSymlinks(path)
if evalErr != nil {
return "", err
}
return filepath.Abs(resolved)
}