-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pga: safe downloads #69
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package cmd | |
|
||
import ( | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os/user" | ||
|
@@ -17,32 +18,83 @@ const ( | |
|
||
// updateCache checks whether a new version of the file in url exists and downloads it | ||
// to dest. It returns an error when it was not possible to update it. | ||
func updateCache(dest, source FileSystem, name string) error { | ||
func updateCache(ctx context.Context, dest, source FileSystem, name string) error { | ||
logrus.Debugf("syncing %s to %s", source.Abs(name), dest.Abs(name)) | ||
if upToDate(dest, source, name) { | ||
logrus.Debugf("local copy is up to date") | ||
return nil | ||
} | ||
|
||
logrus.Debugf("local copy is outdated or non existent") | ||
wc, err := dest.Create(name) | ||
tmpName := name + ".tmp" | ||
if err := copy(ctx, source, dest, name, tmpName); err != nil { | ||
if cerr := dest.Remove(tmpName); cerr != nil { | ||
logrus.Warningf("error removing temporary file %s: %v", | ||
dest.Abs(tmpName), cerr) | ||
} | ||
|
||
return fmt.Errorf("could not copy to temporary file %s: %v", | ||
dest.Abs(tmpName), err) | ||
} | ||
|
||
if err := dest.Rename(tmpName, name); err != nil { | ||
return fmt.Errorf("rename %s to %s failed: %v", | ||
dest.Abs(tmpName), dest.Abs(name), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func copy(ctx context.Context, source, dest FileSystem, | ||
sourceName, destName string) (err error) { | ||
|
||
wc, err := dest.Create(destName) | ||
if err != nil { | ||
return fmt.Errorf("could not create %s: %v", dest.Abs(name), err) | ||
return fmt.Errorf("could not create %s: %v", dest.Abs(destName), err) | ||
} | ||
|
||
rc, err := source.Open(name) | ||
rc, err := source.Open(sourceName) | ||
if err != nil { | ||
_ = wc.Close() | ||
return err | ||
} | ||
defer rc.Close() | ||
|
||
if _, err = io.Copy(wc, rc); err != nil { | ||
return fmt.Errorf("could not copy %s to %s: %v", source.Abs(name), dest.Abs(name), err) | ||
if _, err = cancelableCopy(ctx, wc, rc); err != nil { | ||
_ = rc.Close() | ||
_ = wc.Close() | ||
return fmt.Errorf("could not copy %s to %s: %v", | ||
source.Abs(sourceName), dest.Abs(destName), err) | ||
} | ||
if err := wc.Close(); err != nil { | ||
return fmt.Errorf("could not close %s: %v", dest.Abs(name), err) | ||
|
||
if err := rc.Close(); err != nil { | ||
_ = wc.Close() | ||
return err | ||
} | ||
|
||
return wc.Close() | ||
} | ||
|
||
const copyBufferSize = 512 * 1024 | ||
|
||
func cancelableCopy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm almost tempted to propose this function to Go's stdlib. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be great, but I doubt it gets accepted, since nothing in |
||
var written int64 | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return written, fmt.Errorf("download interrupted") | ||
default: | ||
} | ||
|
||
w, err := io.CopyN(dst, src, copyBufferSize) | ||
written += w | ||
if err == io.EOF { | ||
return written, nil | ||
} | ||
|
||
if err != nil { | ||
return written, err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func upToDate(dest, source FileSystem, name string) bool { | ||
|
@@ -79,15 +131,15 @@ func matchHash(dest, source FileSystem, name string) (bool, error) { | |
return localHash == remoteHash, nil | ||
} | ||
|
||
func getIndex() (io.ReadCloser, error) { | ||
func getIndex(ctx context.Context) (io.ReadCloser, error) { | ||
usr, err := user.Current() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dest := localFS(filepath.Join(usr.HomeDir, ".pga")) | ||
source := urlFS(indexURL) | ||
|
||
if err := updateCache(dest, source, indexName); err != nil { | ||
if err := updateCache(ctx, dest, source, indexName); err != nil { | ||
return nil, err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say that failing to close a reader is not important, so it's ok to simply do
defer rc.Close()
,but I'll let you decide on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That depends on the reader implementation. Some of them signal unexpected EOF on
Close
. You could argue that it is their fault to not useUnexpectedEOF
or some other errors onRead
though...