Skip to content

Commit

Permalink
Merge pull request #4538 from ipfs/feat/command/cat-offset
Browse files Browse the repository at this point in the history
Add offset option to cat command
  • Loading branch information
whyrusleeping authored Jan 2, 2018
2 parents 1a0d6ec + 8a8c9b5 commit f013a76
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
29 changes: 26 additions & 3 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"fmt"
"io"
"os"

Expand All @@ -23,6 +24,9 @@ var CatCmd = &cmds.Command{
Arguments: []cmdkit.Argument{
cmdkit.StringArg("ipfs-path", true, true, "The path to the IPFS object(s) to be outputted.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.IntOption("offset", "o", "Byte offset to begin reading from."),
},
Run: func(req cmds.Request, res cmds.ResponseEmitter) {
node, err := req.InvocContext().GetNode()
if err != nil {
Expand All @@ -36,8 +40,17 @@ var CatCmd = &cmds.Command{
return
}
}
offset, _, err := req.Option("offset").Int()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
if offset < 0 {
res.SetError(fmt.Errorf("Cannot specify negative offset."), cmdkit.ErrNormal)
return
}

readers, length, err := cat(req.Context(), node, req.Arguments())
readers, length, err := cat(req.Context(), node, req.Arguments(), int64(offset))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand Down Expand Up @@ -103,16 +116,26 @@ var CatCmd = &cmds.Command{
},
}

func cat(ctx context.Context, node *core.IpfsNode, paths []string) ([]io.Reader, uint64, error) {
func cat(ctx context.Context, node *core.IpfsNode, paths []string, offset int64) ([]io.Reader, uint64, error) {
readers := make([]io.Reader, 0, len(paths))
length := uint64(0)
for _, fpath := range paths {
read, err := coreunix.Cat(ctx, node, fpath)
if err != nil {
return nil, 0, err
}
if offset > int64(read.Size()) {
offset = offset - int64(read.Size())
continue
}
count, err := read.Seek(offset, io.SeekStart)
if err != nil {
return nil, 0, err
}
offset = 0

readers = append(readers, read)
length += uint64(read.Size())
length += uint64(read.Size() - uint64(count))
}
return readers, length, nil
}
33 changes: 33 additions & 0 deletions test/sharness/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,44 @@ test_add_cat_file() {
test_cmp expected actual
'

test_expect_success "ipfs cat with offset succeeds" '
ipfs cat --offset 10 "$HASH" >actual
'

test_expect_success "ipfs cat from offset output looks good" '
echo "ds!" >expected &&
test_cmp expected actual
'

test_expect_success "ipfs cat multiple hashes with offset succeeds" '
ipfs cat --offset 10 "$HASH" "$HASH" >actual
'

test_expect_success "ipfs cat from offset output looks good" '
echo "ds!" >expected &&
echo "Hello Worlds!" >>expected &&
test_cmp expected actual
'

test_expect_success "ipfs cat multiple hashes with offset succeeds" '
ipfs cat --offset 16 "$HASH" "$HASH" >actual
'

test_expect_success "ipfs cat from offset output looks good" '
echo "llo Worlds!" >expected &&
test_cmp expected actual
'

test_expect_success "ipfs cat from negitive offset should fail" '
test_expect_code 1 ipfs cat --offset -102 "$HASH" > actual
'

test_expect_success "ipfs cat /ipfs/file succeeds" '
ipfs cat /ipfs/$HASH >actual
'

test_expect_success "output looks good" '
echo "Hello Worlds!" >expected &&
test_cmp expected actual
'

Expand Down

0 comments on commit f013a76

Please sign in to comment.