Skip to content

Commit

Permalink
Remove log statements and return errors instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmd committed Aug 27, 2024
1 parent 93bc001 commit b491071
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions selfupdate/selfupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/url"
"os"
Expand All @@ -27,7 +25,8 @@ const (
)

var (
ErrHashMismatch = errors.New("new file hash mismatch after patch")
ErrPatchHashMismatch = errors.New("Hash mismatch from patched binary")
ErrBinHashMismatch = errors.New("Hash mismatch from full binary")

defaultHTTPRequester = HTTPRequester{}
)
Expand Down Expand Up @@ -206,22 +205,9 @@ func (u *Updater) Update() error {

bin, err := u.fetchAndVerifyPatch(old)
if err != nil {
if err == ErrHashMismatch {
log.Println("update: hash mismatch from patched binary")
} else {
if u.DiffURL != "" {
log.Println("update: patching binary,", err)
}
}

// if patch failed grab the full new bin
bin, err = u.fetchAndVerifyFullBin()
if err != nil {
if err == ErrHashMismatch {
log.Println("update: hash mismatch from full binary")
} else {
log.Println("update: fetching full binary,", err)
}
return err
}
}
Expand Down Expand Up @@ -253,7 +239,7 @@ func fromStream(updateWith io.Reader) (err error, errRecover error) {
}

var newBytes []byte
newBytes, err = ioutil.ReadAll(updateWith)
newBytes, err = io.ReadAll(updateWith)
if err != nil {
return
}
Expand All @@ -262,7 +248,7 @@ func fromStream(updateWith io.Reader) (err error, errRecover error) {
updateDir := filepath.Dir(updatePath)
filename := filepath.Base(updatePath)

// Copy the contents of of newbinary to a the new executable file
// Copy the contents of newbinary to a the new executable file
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
fp, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
Expand Down Expand Up @@ -332,7 +318,7 @@ func (u *Updater) fetchAndVerifyPatch(old io.Reader) ([]byte, error) {
return nil, err
}
if !verifySha(bin, u.Info.Sha256) {
return nil, ErrHashMismatch
return nil, ErrPatchHashMismatch
}
return bin, nil
}
Expand All @@ -355,7 +341,7 @@ func (u *Updater) fetchAndVerifyFullBin() ([]byte, error) {
}
verified := verifySha(bin, u.Info.Sha256)
if !verified {
return nil, ErrHashMismatch
return nil, ErrBinHashMismatch
}
return bin, nil
}
Expand Down Expand Up @@ -396,7 +382,7 @@ func (u *Updater) fetch(url string) (io.ReadCloser, error) {
}

func readTime(path string) time.Time {
p, err := ioutil.ReadFile(path)
p, err := os.ReadFile(path)
if os.IsNotExist(err) {
return time.Time{}
}
Expand All @@ -417,5 +403,5 @@ func verifySha(bin []byte, sha []byte) bool {
}

func writeTime(path string, t time.Time) bool {
return ioutil.WriteFile(path, []byte(t.Format(time.RFC3339)), 0644) == nil
return os.WriteFile(path, []byte(t.Format(time.RFC3339)), 0644) == nil
}

0 comments on commit b491071

Please sign in to comment.