2023-01-16 23:09:18 +00:00
|
|
|
package common
|
|
|
|
|
2023-01-16 23:41:39 +00:00
|
|
|
import (
|
2023-01-17 05:46:50 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2023-01-16 23:41:39 +00:00
|
|
|
"path/filepath"
|
|
|
|
)
|
2023-01-16 23:09:18 +00:00
|
|
|
|
|
|
|
// Same as filepath.Rel except that it normalizes result to forward slashes
|
|
|
|
// slashes since filepath.Rel will convert to system slashes
|
|
|
|
func FileRel(basepath, targpath string) (string, error) {
|
|
|
|
r, err := filepath.Rel(basepath, targpath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.ToSlash(r), nil
|
|
|
|
}
|
2023-01-17 05:46:50 +00:00
|
|
|
|
|
|
|
// github.com/yookoala/realpath has bugs on Windows;
|
|
|
|
// this function assumes that the path passed in is a symlink
|
|
|
|
func Realpath(p string) (string, error) {
|
|
|
|
t, err := os.Readlink(p)
|
|
|
|
if err != nil {
|
2024-05-28 16:42:06 +00:00
|
|
|
return "", err
|
2023-01-17 05:46:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 20:45:29 +00:00
|
|
|
if !filepath.IsAbs(t) {
|
2023-01-17 05:46:50 +00:00
|
|
|
t = path.Join(path.Dir(p), t)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Lstat(t)
|
|
|
|
if err != nil {
|
2024-05-28 16:42:06 +00:00
|
|
|
return "", err
|
2023-01-17 05:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
return Realpath(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|