Skip to content

Commit

Permalink
Fix bug that terminates in initialization phase
Browse files Browse the repository at this point in the history
if the destination directory pointed to by .lenv/current does not exist,
that symlink must be deleted.
  • Loading branch information
mah0x211 committed Mar 3, 2023
1 parent e23c313 commit a8e7470
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,6 @@ func createSymlink(oldname, newname string) error {
return os.Rename(tmpName, newname)
}

var ErrNotSymlink = fmt.Errorf("not a symlink")

func evalSymlink(file string) (string, error) {
if mode, err := getFileMode(file); err != nil {
return "", err
} else if mode == 0 {
return "", nil
} else if mode&os.ModeSymlink == 0 {
return "", ErrNotSymlink
} else if pathname, err := filepath.EvalSymlinks(file); err != nil {
return "", err
} else {
return pathname, nil
}
}

func readSymlink(file string) (string, error) {
if mode, err := getFileMode(file); err != nil {
return "", err
Expand Down Expand Up @@ -313,7 +297,23 @@ func start() {
}
}

func init() {
var ErrNotSymlink = fmt.Errorf("not a symlink")

func evalSymlink(file string) (string, error) {
if mode, err := getFileMode(file); err != nil {
return "", err
} else if mode == 0 {
return "", nil
} else if mode&os.ModeSymlink == 0 {
return "", ErrNotSymlink
} else if pathname, err := filepath.EvalSymlinks(file); err != nil {
return "", err
} else {
return pathname, nil
}
}

func ResolveCurrentDir() {
abortf := func(format string, v ...interface{}) {
eprintf(format, v...)
os.Exit(1)
Expand All @@ -323,8 +323,10 @@ func init() {
if dir, err := evalSymlink(CurrentDir); err != nil {
if errors.Is(err, ErrNotSymlink) {
abortf("%q is not symlink.\nplease remove it yourself", CurrentDir)
} else if !os.IsNotExist(err) {
abortf("failed to evalSymlink(): %#v", err)
}
abortf("failed to evalSymlink(): %v", err)
os.Remove(CurrentDir)
} else if dir != "" {
if ok, err := isDir(dir); err != nil {
abortf("failed to isDir(): %v", err)
Expand All @@ -345,6 +347,10 @@ func init() {
}
}

func init() {
ResolveCurrentDir()
}

func main() {
wg := sync.WaitGroup{}
wg.Add(1)
Expand Down

0 comments on commit a8e7470

Please sign in to comment.