Skip to content

Commit

Permalink
Merge pull request #34 from FLuzzi-csw/main
Browse files Browse the repository at this point in the history
Public export of `getExecutableRealPath` and refactoring loading the exe path
  • Loading branch information
Bluebugs authored Jan 20, 2024
2 parents 4910e90 + e12750b commit 24aee1e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: Update coverage
run: |
GO111MODULE=off go get github.com/mattn/goveralls
go get github.com/mattn/goveralls
set -e
go test -tags ci -covermode=atomic -coverprofile=coverage.out ./...
if: ${{ runner.os == 'Linux' }}
Expand Down
16 changes: 8 additions & 8 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ func apply(update io.Reader, opts *Options) error {

// get target path
var err error
opts.TargetPath, err = opts.getPath()
err = LoadPath()
if err != nil {
return err
}
// ignore err since handled after LoadPath
opts.TargetPath, _ = opts.getPath()

var newBytes []byte
if opts.Patcher != nil {
Expand All @@ -106,12 +108,9 @@ func apply(update io.Reader, opts *Options) error {
}
}

// get the directory the executable exists in
updateDir := filepath.Dir(opts.TargetPath)
filename := filepath.Base(opts.TargetPath)

// Copy the contents of newbinary to a new executable file
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
// ignore err since handled after LoadPath
newPath, _ := ExecutableNewPath()
fp, err := openFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, opts.TargetMode)
if err != nil {
return err
Expand All @@ -133,7 +132,8 @@ func apply(update io.Reader, opts *Options) error {
oldPath := opts.OldSavePath
removeOld := opts.OldSavePath == ""
if removeOld {
oldPath = filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
// ignore err since handled after LoadPath
oldPath, _ = ExecutableDefaultOldPath()
}

// delete any existing old exec file - this is necessary on Windows for two reasons:
Expand Down Expand Up @@ -277,7 +277,7 @@ func (o *Options) SetPublicKeyPEM(pembytes []byte) error {

func (o *Options) getPath() (string, error) {
if o.TargetPath == "" {
return getExecutableRealPath()
return ExecutableRealPath()
}
return o.TargetPath, nil
}
Expand Down
60 changes: 59 additions & 1 deletion executable.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,73 @@
package selfupdate

import (
"fmt"
"os"
"path/filepath"
"sync"
"time"

"github.com/fynelabs/selfupdate/internal/osext"
)

var (
exePath string
defaultOldExePath string
newExePath string
exeErr error
once sync.Once
)

// ExecutableRealPath returns the path to the original executable and an error if something went bad
func ExecutableRealPath() (string, error) {
if LoadPath() != nil {
return "", exeErr
}
return exePath, nil
}

// ExecutableDefaultOldPath returns the path to the old executable and an error if something went bad
func ExecutableDefaultOldPath() (string, error) {
if LoadPath() != nil {
return "", exeErr
}
return defaultOldExePath, nil
}

// ExecutableNewPath returns the path to the new executable and an error if something went bad
func ExecutableNewPath() (string, error) {
if LoadPath() != nil {
return "", exeErr
}
return newExePath, nil
}

// LoadPath loads the paths to the old, the current and the new executables,
// it returns an error if something went bad
func LoadPath() error {
once.Do(func() {
exePath, defaultOldExePath, newExePath, exeErr = loadPath()
})
return exeErr
}

func loadPath() (string, string, string, error) {
exePath, err := getExecutableRealPath()
if err != nil {
return "", "", "", err
}
// get the directory the executable exists in
updateDir := filepath.Dir(exePath)
filename := filepath.Base(exePath)

// get file paths to new and old executable file paths
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
oldPath := filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
return exePath, oldPath, newPath, nil
}

func lastModifiedExecutable() (time.Time, error) {
exe, err := getExecutableRealPath()
exe, err := ExecutableRealPath()
if err != nil {
return time.Time{}, err
}
Expand Down
27 changes: 26 additions & 1 deletion executable_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package selfupdate

import (
"fmt"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,7 +15,29 @@ func TestAlwaysFindExecutableTime(t *testing.T) {
}

func TestAlwaysFindExecutable(t *testing.T) {
exe, err := getExecutableRealPath()
exe, err := ExecutableRealPath()
ext := filepath.Ext(exe)
assert.Nil(t, err)
assert.NotEmpty(t, exe)
if runtime.GOOS == "windows" {
assert.Equal(t, ".exe", ext)
} else {
assert.True(t, ext == ".test" || ext == "", fmt.Sprintf("Linux extesion not correct, got '%s'", ext))
}
}

func TestAlwaysFindOldExecutable(t *testing.T) {
exe, err := ExecutableDefaultOldPath()
ext := filepath.Ext(exe)
assert.Nil(t, err)
assert.NotEmpty(t, exe)
assert.Equal(t, ".old", ext)
}

func TestAlwaysFindNewExecutable(t *testing.T) {
exe, err := ExecutableNewPath()
ext := filepath.Ext(exe)
assert.Nil(t, err)
assert.NotEmpty(t, exe)
assert.Equal(t, ".new", ext)
}
2 changes: 1 addition & 1 deletion http_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func replaceURLTemplate(base string) string {
Ext: ext,
}

exe, err := getExecutableRealPath()
exe, err := ExecutableRealPath()
if err != nil {
exe = filepath.Base(os.Args[0])
} else {
Expand Down

0 comments on commit 24aee1e

Please sign in to comment.