mirror of
https://github.com/bazel-contrib/bazel-lib
synced 2024-11-27 17:43:27 +00:00
43 lines
811 B
Go
43 lines
811 B
Go
package common
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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 {
|
|
return "", err
|
|
}
|
|
|
|
if !filepath.IsAbs(t) {
|
|
t = path.Join(path.Dir(p), t)
|
|
}
|
|
|
|
info, err := os.Lstat(t)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
return Realpath(t)
|
|
}
|
|
|
|
return t, nil
|
|
}
|