-
Notifications
You must be signed in to change notification settings - Fork 51
Add NextReader to BlockReader #603
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |||||
| "errors" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "math" | ||||||
|
|
||||||
| blocks "github.com/ipfs/go-block-format" | ||||||
| "github.com/ipfs/go-cid" | ||||||
|
|
@@ -141,6 +142,23 @@ func (br *BlockReader) Next() (blocks.Block, error) { | |||||
| return blocks.NewBlockWithCid(data, c) | ||||||
| } | ||||||
|
|
||||||
| // NextReader returns a CID, io.Reader and length of what should be the next block | ||||||
| // the CID itself is not verified, and the reader is limited to the size of the block | ||||||
| // The user of this function HAS TO consume all of the bytes in the returned reader before using any other function | ||||||
| // on the BlockReader. | ||||||
| // The returned length might be larger than MaxAllowedSectionSize, it is up to the user to check before loading the data into memory. | ||||||
| func (br *BlockReader) NextReader() (cid.Cid, io.Reader, uint64, error) { | ||||||
| c, length, err := util.ReadNodeHeader(br.r, br.opts.ZeroLengthSectionAsEOF, math.MaxUint64) | ||||||
|
||||||
| c, length, err := util.ReadNodeHeader(br.r, br.opts.ZeroLengthSectionAsEOF, math.MaxUint64) | |
| c, length, err := util.ReadNodeHeader(br.r, br.opts.ZeroLengthSectionAsEOF, math.MaxInt64) |
Or is there a reason you're using MaxUint64 but then capping it at MaxInt64 inside ReadNodeHeader? Why not just use MaxInt64 all the way through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the maxsize param is uint64 everywhere, so from an external API viewpoint, I'm passing unlimited (maximum allowed value). Then the function internally caps to what it is able to handle.
Both perspectives make sense though
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ package util | |||||||
| import ( | ||||||||
| "errors" | ||||||||
| "io" | ||||||||
| "math" | ||||||||
|
|
||||||||
| internalio "github.com/ipld/go-car/v2/internal/io" | ||||||||
|
|
||||||||
|
|
@@ -33,6 +34,33 @@ func ReadNode(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (cid.Cid, []b | |||||||
| return c, data[n:], nil | ||||||||
| } | ||||||||
|
|
||||||||
| // ReadNodeHeader returns the specified CID of the node and the length of data to be read. | ||||||||
| func ReadNodeHeader(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (cid.Cid, uint64, error) { | ||||||||
| size, err := LdReadSize(r, zeroLenAsEOF, maxReadBytes) | ||||||||
| if err != nil { | ||||||||
| return cid.Cid{}, 0, err | ||||||||
| } | ||||||||
|
|
||||||||
| if size == 0 { | ||||||||
| _, _, err := cid.CidFromBytes([]byte{}) // generate zero-byte CID error | ||||||||
| if err == nil { | ||||||||
| panic("expected zero-byte CID error") | ||||||||
| } | ||||||||
| return cid.Undef, 0, err | ||||||||
| } | ||||||||
|
|
||||||||
| if size > math.MaxInt64 { | ||||||||
| return cid.Cid{}, 0, ErrHeaderTooLarge | ||||||||
| } | ||||||||
|
||||||||
| if size > math.MaxInt64 { | |
| return cid.Cid{}, 0, ErrHeaderTooLarge | |
| } |
this is unnecessary, it's what LdReadSize does with its third arg for you to generate a uniform error pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also if this really is necessary, ErrSectionTooLarge cause this isn't actually a header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this because the int64 cast for LimitReader (don't want to cast to negatives)
Uh oh!
There was an error while loading. Please reload this page.