Skip to content

Commit

Permalink
fix symlink issue on windows for goctl (#1034)
Browse files Browse the repository at this point in the history
* fix symlink issue on windows for goctl

* move readlink into separate file
  • Loading branch information
kevwan authored Sep 13, 2021
1 parent 9e2f07a commit 71c0288
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 47 deletions.
1 change: 1 addition & 0 deletions core/proc/goroutines.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux || darwin
// +build linux darwin

package proc
Expand Down
1 change: 1 addition & 0 deletions core/proc/profile.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux || darwin
// +build linux darwin

package proc
Expand Down
47 changes: 0 additions & 47 deletions tools/goctl/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions tools/goctl/util/readlink+polyfill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build windows
// +build windows

package util

func ReadLink(name string) (string, error) {
return name, nil
}
56 changes: 56 additions & 0 deletions tools/goctl/util/readlink.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 71c0288

Please sign in to comment.