Skip to content

Commit

Permalink
Enable updater for windows (#1790)
Browse files Browse the repository at this point in the history
Fixes #1789

RELEASE_NOTES=[BUGFIX] Enable updater on Windows
RELEASE_NOTES=[BUGFIX] Fix progress bar nil pointer access

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Feb 15, 2021
1 parent 8baebe2 commit ce8498c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 43 deletions.
5 changes: 1 addition & 4 deletions internal/store/leaf/reencrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ func (s *Store) reencrypt(ctx context.Context) error {
ctx := ctxutil.WithGitCommit(ctx, false)

// progress bar
bar := termio.NewProgressBar(int64(len(entries)), ctxutil.IsHidden(ctx))
if !ctxutil.IsTerminal(ctx) || ctxutil.IsHidden(ctx) {
bar = nil
}
bar := termio.NewProgressBar(int64(len(entries)), !ctxutil.IsTerminal(ctx) || ctxutil.IsHidden(ctx))
var wg sync.WaitGroup
jobs := make(chan string)
// We use a logger to write without race condition on stdout
Expand Down
23 changes: 11 additions & 12 deletions internal/updater/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,31 @@ func download(ctx context.Context, url string) ([]byte, error) {
// we want binary data, please
req.Header.Set("Accept", "application/octet-stream")

t0 := time.Now()
resp, err := ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil {
return nil, err
}

var body io.ReadCloser
bar := termio.NewProgressBar(resp.ContentLength, ctxutil.IsHidden(ctx))
// do not show progress bar for small assets, like SHA256SUMS
if resp.ContentLength > 10000 {
body = &passThru{
ReadCloser: resp.Body,
Bar: bar,
}

} else {
body = resp.Body
bar := termio.NewProgressBar(resp.ContentLength, ctxutil.IsHidden(ctx) || resp.ContentLength < 10000)
body = &passThru{
ReadCloser: resp.Body,
Bar: bar,
}

buf := &bytes.Buffer{}

count, err := io.Copy(buf, body)
if err != nil {
return nil, err
}

bar.Set(resp.ContentLength)
bar.Done()
debug.Log("Transferred %d bytes from %s", count, url)

elapsed := time.Since(t0)
debug.Log("Transferred %d bytes from %q in %s", count, url, elapsed)
return buf.Bytes(), nil
}

Expand All @@ -84,7 +83,7 @@ type passThru struct {

func (pt *passThru) Read(p []byte) (int, error) {
n, err := pt.ReadCloser.Read(p)
if pt.Bar != nil {
if pt.Bar != nil && n > 0 {
pt.Bar.Set(int64(n))
}
return n, err
Expand Down
5 changes: 4 additions & 1 deletion internal/updater/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"os"
"runtime"

"github.com/gopasspw/gopass/internal/out"
Expand Down Expand Up @@ -39,7 +40,9 @@ func Update(ctx context.Context, currentVersion semver.Version) error {
// binary is newer or equal to the latest release -> nothing to do
if currentVersion.GTE(rel.Version) {
out.Print(ctx, "gopass is up to date (%s)", currentVersion.String())
return nil
if gfu := os.Getenv("GOPASS_FORCE_UPDATE"); gfu == "" {
return nil
}
}

debug.Log("downloading SHA256SUMS ...")
Expand Down
16 changes: 0 additions & 16 deletions internal/updater/update_windows.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !windows

package updater

import (
Expand All @@ -9,8 +7,6 @@ import (
"path/filepath"
"strings"

"golang.org/x/sys/unix"

"github.com/gopasspw/gopass/pkg/debug"
)

Expand Down Expand Up @@ -46,13 +42,24 @@ func IsUpdateable(ctx context.Context) error {
if !fi.Mode().IsRegular() {
return fmt.Errorf("not a regular file")
}
if err := unix.Access(fn, unix.W_OK); err != nil {
return err
if err := canWrite(fn); err != nil {
return fmt.Errorf("can not write %q: %w", fn, err)
}

// no need to check the directory since we'll be writing to the destination file directly
return nil
}

func canWrite(path string) error {
// check if we can write to this file. we open it for writing in
// append mode to not truncate it and immediately close the filehandle since
// we're only interested in the error.
fh, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0755)
if e := fh.Close(); err != nil {
return e
}

// check dir
fdir := filepath.Dir(fn)
return unix.Access(fdir, unix.W_OK)
return err
}

var executable = func(ctx context.Context) (string, error) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/termio/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (p *ProgressBar) Set(v int64) {

// Done finalizes the progress bar
func (p *ProgressBar) Done() {
if p.hidden {
return
}
fmt.Fprintln(Stdout, "")
}

Expand All @@ -85,7 +88,7 @@ func (p *ProgressBar) print() {

func (p *ProgressBar) tryPrint() {
ts := now()
if p.current == 0 || p.current == p.total || ts.Sub(p.lastUpd) > time.Second/fps {
if p.current == 0 || p.current >= p.total-1 || ts.Sub(p.lastUpd) > time.Second/fps {
p.lastUpd = ts
p.doPrint()
}
Expand Down

0 comments on commit ce8498c

Please sign in to comment.