Files
skillshare/internal/utils/link.go
T
Willie 05b2f12023 fix(utils): detect junctions under GODEBUG=winsymlink=0
With winsymlink=0 Go reports a junction as ModeSymlink instead of
ModeIrregular, so IsJunction returned false and the trash fallback
recreated the junction with os.Symlink again. Read the reparse tag for
both modes.

Refs #420
2026-10-06 01:16:37 +08:00

57 lines
1.7 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, as opposed to a symlink.
// With GODEBUG=winsymlink=0 Go reports a junction as ModeSymlink, so check both.
func IsJunction(path string) bool {
info, err := os.Lstat(path)
return err == nil && info.Mode()&(fs.ModeIrregular|fs.ModeSymlink) != 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)
}