Skip to content
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

Fix ipfs get progress bar #3758

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ func (r *clearlineReader) Read(p []byte) (n int, err error) {
}

func progressBarForReader(out io.Writer, r io.Reader, l int64) (*pb.ProgressBar, io.Reader) {
bar := makeProgressBar(out, l)
barR := bar.NewProxyReader(r)
return bar, &clearlineReader{barR, out}
}

func makeProgressBar(out io.Writer, l int64) *pb.ProgressBar {
// setup bar reader
// TODO: get total length of files
bar := pb.New64(l).SetUnits(pb.U_BYTES)
Expand All @@ -155,8 +161,7 @@ func progressBarForReader(out io.Writer, r io.Reader, l int64) (*pb.ProgressBar,
bar.Callback = nil
log.Infof("terminal width: %v\n", terminalWidth)
}
barR := bar.NewProxyReader(r)
return bar, &clearlineReader{barR, out}
return bar
}

type getWriter struct {
Expand Down Expand Up @@ -208,12 +213,13 @@ func (gw *getWriter) writeArchive(r io.Reader, fpath string) error {

func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
fmt.Fprintf(gw.Out, "Saving file(s) to %s\n", fpath)
bar, barR := progressBarForReader(gw.Err, r, gw.Size)
bar := makeProgressBar(gw.Err, gw.Size)
bar.Start()
defer bar.Finish()
defer bar.Set64(gw.Size)

extractor := &tar.Extractor{fpath}
return extractor.Extract(barR)
extractor := &tar.Extractor{fpath, bar.Add64}
return extractor.Extract(r)
}

func getCompressOptions(req cmds.Request) (int, error) {
Expand Down
25 changes: 23 additions & 2 deletions thirdparty/tar/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type Extractor struct {
Path string
Path string
Progress func(int64) int64
}

func (te *Extractor) Extract(reader io.Reader) error {
Expand Down Expand Up @@ -111,10 +112,30 @@ func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootEx
}
defer file.Close()

_, err = io.Copy(file, r)
err = copyWithProgress(file, r, te.Progress)
if err != nil {
return err
}

return nil
}

func copyWithProgress(to io.Writer, from io.Reader, cb func(int64) int64) error {
buf := make([]byte, 4096)
for {
n, err := from.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}

cb(int64(n))
_, err = to.Write(buf[:n])
if err != nil {
return err
}
}

}