From 9d5d130038ed90564c3acfb2fd2ff64e3d7b0bd9 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Mon, 9 Aug 2021 21:11:52 -0400 Subject: Fix up cli. Continue fixing up the library and web app. Initial API spec --- steam/extract.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'steam/extract.go') diff --git a/steam/extract.go b/steam/extract.go index 9a7a930..d93428d 100644 --- a/steam/extract.go +++ b/steam/extract.go @@ -43,9 +43,9 @@ func (l *Library) ExtractSmart(uri string) (*Game, error) { return nil, E_BadURI } -// ExtractFile is a wrapper around Extract that handles an HTTP endpoint. -// this spawns an "extractFile" on the library. Status will be updated there -// as this goes along. Non fatal and fatal errors will be populated there +// ExtractFile is a wrapper around Extract that handles local files. this +// spawns an "extractFile" on the library. Status will be updated there as this +// goes along. Non fatal and fatal errors will be populated there func (l *Library) ExtractFile(fn string) (*Game, error) { g := &Game{} j := newJob("extractFile", g) @@ -66,10 +66,13 @@ func (l *Library) ExtractFile(fn string) (*Game, error) { return g, err } - return l.extractUpdate(j, g, fh) + g, err = l.extractUpdate(j, g, fh) + fh.Close() + + return g, err } -// Extract will read a tarball from the io.Reader and install the game into +// Extract will read a tarball from the io.ReadCloser and install the game into // the current library path. This offers no visibility into the progress, // as it does not update the job status on the progress, though it will // populate errors. @@ -87,25 +90,23 @@ func (l *Library) Extract(r io.Reader) (*Game, error) { // extractUpdate takes care of updating the job as it goes along at updateEveryNBytes // it will be reported back to the Job's status. -func (l *Library) extractUpdate(j *Job, g *Game, rdr io.ReadCloser) (*Game, error) { - rdr, wrtr := io.Pipe() +func (l *Library) extractUpdate(j *Job, g *Game, rdr io.Reader) (*Game, error) { + prdr, pwrtr := io.Pipe() go func() { var err error - g, err = l.extractPrimitive(j, g, rdr) + g, err = l.extractPrimitive(j, g, prdr) if err != nil { j.addError(fmt.Errorf("Installer: extracting %s", err)) } - // resp.Body.Close() - rdr.Close() }() var total int64 var err error + var n int64 for { - var n int64 - n, err = io.CopyN(wrtr, rdr, updateEveryNBytes) + n, err = io.CopyN(pwrtr, rdr, updateEveryNBytes) if err == io.EOF { break } else if err != nil { @@ -143,6 +144,7 @@ func (l *Library) extractUpdate(j *Job, g *Game, rdr io.ReadCloser) (*Game, erro } func (l *Library) extractPrimitive(j *Job, g *Game, r io.Reader) (*Game, error) { + treader := tar.NewReader(r) for { @@ -189,6 +191,7 @@ func (l *Library) extractPrimitive(j *Job, g *Game, r io.Reader) (*Game, error) // Create a file handle to work with f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, defaultFileMode) + if err != nil { j.addError(err) return nil, err -- cgit v1.2.3