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

Add offset option to cat command #4538

Merged
merged 2 commits into from
Jan 2, 2018
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
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