Skip to content

Commit

Permalink
fix apply regression and made LoadPath private
Browse files Browse the repository at this point in the history
  • Loading branch information
FLuzzi-csw committed Jan 22, 2024
1 parent 24aee1e commit f225fde
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 46 deletions.
16 changes: 8 additions & 8 deletions apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ func apply(update io.Reader, opts *Options) error {

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

var newBytes []byte
if opts.Patcher != nil {
Expand All @@ -108,9 +106,12 @@ 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
// ignore err since handled after LoadPath
newPath, _ := ExecutableNewPath()
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
fp, err := openFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, opts.TargetMode)
if err != nil {
return err
Expand All @@ -132,8 +133,7 @@ func apply(update io.Reader, opts *Options) error {
oldPath := opts.OldSavePath
removeOld := opts.OldSavePath == ""
if removeOld {
// ignore err since handled after LoadPath
oldPath, _ = ExecutableDefaultOldPath()
oldPath = filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
}

// delete any existing old exec file - this is necessary on Windows for two reasons:
Expand Down Expand Up @@ -203,7 +203,7 @@ type rollbackErr struct {
// Options give additional parameters when calling Apply
type Options struct {
// TargetPath defines the path to the file to update.
// The emptry string means 'the executable file of the running program'.
// The empty string means 'the executable file of the running program'.
TargetPath string

// Create TargetPath replacement with this file mode. If zero, defaults to 0755.
Expand Down
43 changes: 13 additions & 30 deletions executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,42 @@ import (
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 {
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 {
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 {
func loadPath() error {
once.Do(func() {
exePath, defaultOldExePath, newExePath, exeErr = loadPath()
exePath, exeErr = getExecutableRealPath()
if exeErr != nil {
return
}
// get the directory the executable exists in
updateDir := filepath.Dir(exePath)
filename := filepath.Base(exePath)

// get file path to the old executable
defaultOldExePath = filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
})
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 := ExecutableRealPath()
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,3 @@ func TestAlwaysFindOldExecutable(t *testing.T) {
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)
}

0 comments on commit f225fde

Please sign in to comment.