diff --git a/core/commands/get.go b/core/commands/get.go index 4c573928a28..a4b46c273c7 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,11 +106,10 @@ 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() _, err = io.Copy(file, pbReader) if err != nil { @@ -122,9 +122,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 @@ -142,7 +141,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) @@ -166,12 +166,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 } diff --git a/test/sharness/t0090-get.sh b/test/sharness/t0090-get.sh index 393ba5b5b4d..8de61069db5 100755 --- a/test/sharness/t0090-get.sh +++ b/test/sharness/t0090-get.sh @@ -23,7 +23,7 @@ test_get_cmd() { ' test_expect_success "ipfs get succeeds" ' - echo "Hello Worlds!" >data && + echo "Hello Worlds\!" >data && HASH=`ipfs add -q data` && ipfs get "$HASH" >actual ' @@ -80,7 +80,7 @@ test_get_cmd() { mkdir -p dir && touch dir/a && mkdir -p dir/b && - echo "Hello, Worlds!" >dir/b/c && + echo "Hello, Worlds\!" >dir/b/c && HASH2=`ipfs add -r -q dir | tail -n 1` && ipfs get "$HASH2" >actual '