Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ func update(ctx context.Context, p *core.Printer, timeout time.Duration, silent
func updateInner(ctx context.Context, p *core.Printer, silent bool) error {
c := client.NewClient(client.ClientConfig{})

// Get the current version by calling `fetch --version` so that if the
// executable was updated while we were waiting for the update lock,
// we have the most up-to-date local version.
// Get the current executable path and verify that we have write
// permission in order to replace the file.
exePath, err := getExecutablePath()
if err != nil {
return err
}
if !canReplaceFile(exePath) {
return errNoWritePermission(exePath)
}

// Get the current version by calling `fetch --version` so that if the
// executable was updated while we were waiting for the update lock,
// we have the most up-to-date local version.
version, err := getExeVersion(ctx, exePath)
if err != nil {
return err
Expand Down Expand Up @@ -459,3 +465,17 @@ func (err errNoReleaseArtifact) PrintTo(p *core.Printer) {
p.Reset()
p.WriteString("'")
}

type errNoWritePermission string

func (err errNoWritePermission) Error() string {
return fmt.Sprintf("the current process does not have write permission to '%s'", string(err))
}

func (err errNoWritePermission) PrintTo(p *core.Printer) {
p.WriteString("the current process does not have write permission to '")
p.Set(core.Dim)
p.WriteString(string(err))
p.Reset()
p.WriteString("'")
}
6 changes: 6 additions & 0 deletions internal/update/update_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ func tryLockFile(f *os.File) (bool, error) {
func unlockFile(f *os.File) error {
return unix.Flock(int(f.Fd()), unix.LOCK_UN)
}

// canReplaceFile returns true if this process can replace the file at the
// provided location.
func canReplaceFile(path string) bool {
return unix.Access(path, unix.W_OK) == nil
}
5 changes: 5 additions & 0 deletions internal/update/update_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ func unlockFile(f *os.File) error {
var ol windows.Overlapped
return windows.UnlockFileEx(windows.Handle(f.Fd()), 0, allBytes, allBytes, &ol)
}

// canReplaceFile always returns true on windows.
func canReplaceFile(path string) bool {
return true
}
Loading