diff --git a/core/commands/get.go b/core/commands/get.go index 4c573928a28..443b53674b8 100644 --- a/core/commands/get.go +++ b/core/commands/get.go @@ -63,11 +63,12 @@ may also specify the level of compression by specifying '-l=<1-9>'. return } - reader, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl) + reader, length, err := get(req.Context().Context, node, req.Arguments()[0], cmplvl) if err != nil { res.SetError(err, cmds.ErrNormal) return } + res.SetLength(length) res.SetOutput(reader) }, PostRun: func(req cmds.Request, res cmds.Response) { @@ -105,8 +106,8 @@ may also specify the level of compression by specifying '-l=<1-9>'. } defer file.Close() - bar := pb.New(0).SetUnits(pb.U_BYTES) - bar.Output = os.Stderr + bar := pb.New64(int64(res.Length())).SetUnits(pb.U_BYTES) + bar.Output = res.Stderr() pbReader := bar.NewProxyReader(outReader) bar.Start() defer bar.Finish() @@ -122,9 +123,8 @@ may also specify the level of compression by specifying '-l=<1-9>'. fmt.Printf("Saving file(s) to %s\n", outPath) - // TODO: get total length of files - bar := pb.New(0).SetUnits(pb.U_BYTES) - bar.Output = os.Stderr + bar := pb.New64(int64(res.Length())).SetUnits(pb.U_BYTES) + bar.Output = res.Stderr() // wrap the reader with the progress bar proxy reader // if the output is compressed, also wrap it in a gzip.Reader @@ -144,6 +144,8 @@ may also specify the level of compression by specifying '-l=<1-9>'. bar.Start() defer bar.Finish() + //res.SetOutput(reader) + extractor := &tar.Extractor{outPath} err = extractor.Extract(reader) if err != nil { @@ -166,12 +168,20 @@ func getCompressOptions(req cmds.Request) (int, error) { return gzip.NoCompression, nil } -func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, error) { +func get(ctx context.Context, node *core.IpfsNode, p string, compression int) (io.Reader, uint64, error) { pathToResolve := path.Path(p) dagnode, err := core.Resolve(ctx, node, pathToResolve) if err != nil { - return nil, err + return nil, 0, err } - return utar.NewReader(pathToResolve, node.DAG, dagnode, compression) + reader, err := utar.NewReader(pathToResolve, node.DAG, dagnode, compression) + ns, err := dagnode.Stat() + if err != nil { + return nil, 0, err + } + + length := uint64(ns.CumulativeSize) + + return reader, length, err }