From 71c0288023df9320cf5d49ebfcce63d90dee23b1 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 13 Sep 2021 11:49:07 +0800 Subject: [PATCH] fix symlink issue on windows for goctl (#1034) * fix symlink issue on windows for goctl * move readlink into separate file --- core/proc/goroutines.go | 1 + core/proc/profile.go | 1 + tools/goctl/util/path.go | 47 ---------------------- tools/goctl/util/readlink+polyfill.go | 8 ++++ tools/goctl/util/readlink.go | 56 +++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 tools/goctl/util/readlink+polyfill.go create mode 100644 tools/goctl/util/readlink.go diff --git a/core/proc/goroutines.go b/core/proc/goroutines.go index f35e4de7a0c6..0f4a283ba00f 100644 --- a/core/proc/goroutines.go +++ b/core/proc/goroutines.go @@ -1,3 +1,4 @@ +//go:build linux || darwin // +build linux darwin package proc diff --git a/core/proc/profile.go b/core/proc/profile.go index 387406de1ba8..4b4a862b8d2c 100644 --- a/core/proc/profile.go +++ b/core/proc/profile.go @@ -1,3 +1,4 @@ +//go:build linux || darwin // +build linux darwin package proc diff --git a/tools/goctl/util/path.go b/tools/goctl/util/path.go index d4ae2c2442f1..671c75447f88 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/path.go @@ -113,53 +113,6 @@ func FindProjectPath(loc string) (string, bool) { return "", false } -// ReadLink returns the destination of the named symbolic link recursively. -func ReadLink(name string) (string, error) { - name, err := filepath.Abs(name) - if err != nil { - return "", err - } - - if _, err := os.Lstat(name); err != nil { - return name, nil - } - - // uncheck condition: ignore file path /var, maybe be temporary file path - if name == "/" || name == "/var" { - return name, nil - } - - isLink, err := isLink(name) - if err != nil { - return "", err - } - - if !isLink { - dir, base := filepath.Split(name) - dir = filepath.Clean(dir) - dir, err := ReadLink(dir) - if err != nil { - return "", err - } - - return filepath.Join(dir, base), nil - } - - link, err := os.Readlink(name) - if err != nil { - return "", err - } - - dir, base := filepath.Split(link) - dir = filepath.Dir(dir) - dir, err = ReadLink(dir) - if err != nil { - return "", err - } - - return filepath.Join(dir, base), nil -} - func isLink(name string) (bool, error) { fi, err := os.Lstat(name) if err != nil { diff --git a/tools/goctl/util/readlink+polyfill.go b/tools/goctl/util/readlink+polyfill.go new file mode 100644 index 000000000000..4022dbd61168 --- /dev/null +++ b/tools/goctl/util/readlink+polyfill.go @@ -0,0 +1,8 @@ +//go:build windows +// +build windows + +package util + +func ReadLink(name string) (string, error) { + return name, nil +} diff --git a/tools/goctl/util/readlink.go b/tools/goctl/util/readlink.go new file mode 100644 index 000000000000..255b955f8b35 --- /dev/null +++ b/tools/goctl/util/readlink.go @@ -0,0 +1,56 @@ +//go:build linux || darwin +// +build linux darwin + +package util + +import ( + "os" + "path/filepath" +) + +// ReadLink returns the destination of the named symbolic link recursively. +func ReadLink(name string) (string, error) { + name, err := filepath.Abs(name) + if err != nil { + return "", err + } + + if _, err := os.Lstat(name); err != nil { + return name, nil + } + + // uncheck condition: ignore file path /var, maybe be temporary file path + if name == "/" || name == "/var" { + return name, nil + } + + isLink, err := isLink(name) + if err != nil { + return "", err + } + + if !isLink { + dir, base := filepath.Split(name) + dir = filepath.Clean(dir) + dir, err := ReadLink(dir) + if err != nil { + return "", err + } + + return filepath.Join(dir, base), nil + } + + link, err := os.Readlink(name) + if err != nil { + return "", err + } + + dir, base := filepath.Split(link) + dir = filepath.Dir(dir) + dir, err = ReadLink(dir) + if err != nil { + return "", err + } + + return filepath.Join(dir, base), nil +}