From 153bb681300cc98e4646179d8d7ac9b1c34197cc Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 5 Jan 2018 18:20:55 -0800 Subject: [PATCH 1/3] implement an ipfs cat --length flag fixes #4085 License: MIT Signed-off-by: Steven Allen --- core/commands/cat.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/core/commands/cat.go b/core/commands/cat.go index 6ecb29fc725..778f8047841 100644 --- a/core/commands/cat.go +++ b/core/commands/cat.go @@ -26,6 +26,7 @@ var CatCmd = &cmds.Command{ }, Options: []cmdkit.Option{ cmdkit.IntOption("offset", "o", "Byte offset to begin reading from."), + cmdkit.IntOption("length", "l", "Maximum number of bytes to read."), }, Run: func(req cmds.Request, res cmds.ResponseEmitter) { node, err := req.InvocContext().GetNode() @@ -50,7 +51,20 @@ var CatCmd = &cmds.Command{ return } - readers, length, err := cat(req.Context(), node, req.Arguments(), int64(offset)) + max, found, err := req.Option("length").Int() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + if max < 0 { + res.SetError(fmt.Errorf("Cannot specify negative length."), cmdkit.ErrNormal) + return + } + if !found { + max = -1 + } + + readers, length, err := cat(req.Context(), node, req.Arguments(), int64(offset), int64(max)) if err != nil { res.SetError(err, cmdkit.ErrNormal) return @@ -116,9 +130,12 @@ var CatCmd = &cmds.Command{ }, } -func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64) ([]io.Reader, uint64, error) { +func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) { readers := make([]io.Reader, 0, len(paths)) length := uint64(0) + if max == 0 { + return nil, 0, nil + } for _, fpath := range paths { read, err := coreunix.Cat(ctx, node, fpath) if err != nil { @@ -134,8 +151,18 @@ func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64) } offset = 0 + size := uint64(read.Size() - uint64(count)) + length += size + if max > 0 && length >= uint64(max) { + var r io.Reader = read + if overshoot := int64(length - uint64(max)); overshoot != 0 { + r = io.LimitReader(read, int64(size)-overshoot) + length = uint64(max) + } + readers = append(readers, r) + break + } readers = append(readers, read) - length += uint64(read.Size() - uint64(count)) } return readers, length, nil } From 089496b918e495cf0599bcce87acfb94395f6de0 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 5 Jan 2018 18:22:03 -0800 Subject: [PATCH 2/3] test the ipfs cat --length flag License: MIT Signed-off-by: Steven Allen --- test/sharness/t0040-add-and-cat.sh | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index fc787eb5487..00419cc95b0 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -78,6 +78,56 @@ test_add_cat_file() { test_expect_code 1 ipfs cat --offset -102 "$HASH" > actual ' + test_expect_success "ipfs cat with length succeeds" ' + ipfs cat --length 8 "$HASH" >actual + ' + + test_expect_success "ipfs cat with length output looks good" ' + echo -n "Hello Wo" >expected && + test_cmp expected actual + ' + + test_expect_success "ipfs cat multiple hashes with offset and length succeeds" ' + ipfs cat --offset 5 --length 15 "$HASH" "$HASH" "$HASH" >actual + ' + + test_expect_success "ipfs cat multiple hashes with offset and length looks good" ' + echo " Worlds!" >expected && + echo -n "Hello " >>expected && + test_cmp expected actual + ' + + test_expect_success "ipfs cat with exact length succeeds" ' + ipfs cat --length $(ipfs cat "$HASH" | wc -c) "$HASH" >actual + ' + + test_expect_success "ipfs cat with exact length looks good" ' + echo "Hello Worlds!" >expected && + test_cmp expected actual + ' + + test_expect_success "ipfs cat with 0 length succeeds" ' + ipfs cat --length 0 "$HASH" >actual + ' + + test_expect_success "ipfs cat with 0 length looks good" ' + : >expected && + test_cmp expected actual + ' + + test_expect_success "ipfs cat with oversized length succeeds" ' + ipfs cat --length 100 "$HASH" >actual + ' + + test_expect_success "ipfs cat with oversized length looks good" ' + echo "Hello Worlds!" >expected && + test_cmp expected actual + ' + + test_expect_success "ipfs cat with negitive length should fail" ' + test_expect_code 1 ipfs cat --length -102 "$HASH" > actual + ' + test_expect_success "ipfs cat /ipfs/file succeeds" ' ipfs cat /ipfs/$HASH >actual ' From 3e0f7cb60003feced005d4ae475a39c556310485 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sat, 6 Jan 2018 15:52:39 -0800 Subject: [PATCH 3/3] fix add-and-cat sharness test on osx License: MIT Signed-off-by: Steven Allen --- test/sharness/t0040-add-and-cat.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index 00419cc95b0..e863353b6e9 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -83,7 +83,7 @@ test_add_cat_file() { ' test_expect_success "ipfs cat with length output looks good" ' - echo -n "Hello Wo" >expected && + printf "Hello Wo" >expected && test_cmp expected actual ' @@ -92,8 +92,7 @@ test_add_cat_file() { ' test_expect_success "ipfs cat multiple hashes with offset and length looks good" ' - echo " Worlds!" >expected && - echo -n "Hello " >>expected && + printf " Worlds!\nHello " >expected && test_cmp expected actual '