Skip to content

Commit

Permalink
Merge pull request #72 from guidoschmidt/master
Browse files Browse the repository at this point in the history
FIX #71: old symlink should be removed on Windows
  • Loading branch information
tristanisham authored Apr 11, 2024
2 parents ac33ea9 + e87630f commit 25200dc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
25 changes: 12 additions & 13 deletions cli/meta/symlink_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"golang.org/x/sys/windows"
)


func becomeAdmin() error {
verb := "runas"
exe, _ := os.Executable()
Expand Down Expand Up @@ -51,23 +50,23 @@ func isAdmin() bool {
// but with automatic privilege escalation on windows
// for systems that do not support non-admin symlinks.
func Symlink(oldname, newname string) error {

// Check if already admin first
if isAdmin() {
if err := os.Symlink(oldname, newname); err != nil {
return errors.Join(ErrEscalatedSymlink, err)
}
return nil
} else {
// If not already admin, try to become admin
if err := becomeAdmin(); err != nil {
// Attempt to do a regular symlink if allowed by user's permissions
if err := os.Symlink(oldname, newname); err != nil {
// Check if already admin first
if isAdmin() {
if err := os.Symlink(oldname, newname); err != nil {
return errors.Join(ErrEscalatedSymlink, err)
}
return nil
} else {
// If not already admin, try to become admin
if err := becomeAdmin(); err != nil {
if err := os.Symlink(oldname, newname); err != nil {
return errors.Join(ErrEscalatedSymlink, err)
}
}
}
}



return nil
}
2 changes: 1 addition & 1 deletion cli/meta/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
VERSION = "v0.6.2"
VERSION = "v0.6.3"
)


Expand Down
11 changes: 7 additions & 4 deletions cli/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -40,13 +39,17 @@ func (z *ZVM) setBin(ver string) error {
version_path := filepath.Join(z.baseDir, ver)
bin_dir := filepath.Join(z.baseDir, "bin")

// Remove "bin" dir only if it already exists

if _, err := os.Stat(bin_dir); errors.Is(err, fs.ErrExist) {
// Came across https://pkg.go.dev/os#Lstat
// which is specifically to check symbolic links.
// Seemed like the more appropriate solution here
stat, err := os.Lstat(bin_dir);
if err == nil {
fmt.Printf("Removing old %s", bin_dir)
if err := os.Remove(bin_dir); err != nil {
return err
}
} else {
return fmt.Errorf("%w: %s", err, stat.Name())
}

if err := meta.Symlink(version_path, bin_dir); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions cli/use_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cli

import (
"os"
"testing"
)

func TestSymlinkExists(t *testing.T) {
if err := os.Symlink("use_test.go", "symlink.test"); err != nil {
t.Error(err)
}

stat, err := os.Lstat("symlink.test");
if err != nil {
t.Errorf("%q: %s", err, stat.Name())
}

defer os.Remove("symlink.test")
}
Empty file modified install.sh
100755 → 100644
Empty file.

0 comments on commit 25200dc

Please sign in to comment.