2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-11-25 11:32:33 +00:00
bazel-lib/tools/common/file.go

44 lines
897 B
Go
Raw Normal View History

package common
import (
"fmt"
"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 "", fmt.Errorf("readlink %s failed: %w", p, err)
}
if !filepath.IsAbs(t) {
t = path.Join(path.Dir(p), t)
}
info, err := os.Lstat(t)
if err != nil {
return "", fmt.Errorf("lstat %s failed: %w", p, err)
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
return Realpath(t)
}
return t, nil
}