Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-chunker
Commits
397f536c
Commit
397f536c
authored
Jan 21, 2020
by
Peter Rabbitson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add various sanity checks for size specifications
parent
f076b1ef
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
4 deletions
+29
-4
parse.go
parse.go
+29
-1
splitting.go
splitting.go
+0
-3
No files found.
parse.go
View file @
397f536c
...
...
@@ -8,9 +8,25 @@ import (
"strings"
)
const
(
// DefaultBlockSize is the chunk size that splitters produce (or aim to).
DefaultBlockSize
int64
=
1024
*
256
// 1 MB, on-wire block size for "datablocks ( unixfs, etc )"
// copy of https://github.com/ipfs/go-unixfs/blob/v0.2.3/importer/helpers/helpers.go#L8
BlockSizeLimit
int
=
1048576
// in case we are using raw-leaves: this would match BlockSizeLimit, but we can't assume that
// be conservative and substract the PB wraping size of a full DAG-PB+UnixFS node describing 1M
// (2b(type2/file)+4b(data-field:3-byte-len-delimited)+4b(size-field:3-byte-varint))+(4b(DAG-type-1:3-byte-len-delimited))
// FIXME - this calculation will need an update for CBOR
BlockPayloadLimit
int
=
(
BlockSizeLimit
-
(
2
+
4
+
4
+
4
))
)
var
(
ErrRabinMin
=
errors
.
New
(
"rabin min must be greater than 16"
)
ErrSize
=
errors
.
New
(
"chunker size muster greater than 0"
)
ErrSize
=
errors
.
New
(
"chunker size must be greater than 0"
)
ErrSizeMax
=
fmt
.
Errorf
(
"chunker parameters may not exceed the maximum block payload size of %d"
,
BlockPayloadLimit
)
)
// FromString returns a Splitter depending on the given string:
...
...
@@ -28,6 +44,8 @@ func FromString(r io.Reader, chunker string) (Splitter, error) {
return
nil
,
err
}
else
if
size
<=
0
{
return
nil
,
ErrSize
}
else
if
size
>
BlockPayloadLimit
{
return
nil
,
ErrSizeMax
}
return
NewSizeSplitter
(
r
,
int64
(
size
)),
nil
...
...
@@ -51,6 +69,8 @@ func parseRabinString(r io.Reader, chunker string) (Splitter, error) {
size
,
err
:=
strconv
.
Atoi
(
parts
[
1
])
if
err
!=
nil
{
return
nil
,
err
}
else
if
int
(
float32
(
size
)
*
1.5
)
>
BlockPayloadLimit
{
// FIXME - there is probably a better way to bubble up this calculation from NewRabin()
return
nil
,
ErrSizeMax
}
return
NewRabin
(
r
,
uint64
(
size
)),
nil
case
4
:
...
...
@@ -84,6 +104,14 @@ func parseRabinString(r io.Reader, chunker string) (Splitter, error) {
return
nil
,
err
}
if
min
>=
avg
{
return
nil
,
errors
.
New
(
"incorrect format: rabin-min must be smaller than rabin-avg"
)
}
else
if
avg
>=
max
{
return
nil
,
errors
.
New
(
"incorrect format: rabin-avg must be smaller than rabin-max"
)
}
else
if
max
>
BlockPayloadLimit
{
return
nil
,
ErrSizeMax
}
return
NewRabinMinMax
(
r
,
uint64
(
min
),
uint64
(
avg
),
uint64
(
max
)),
nil
default
:
return
nil
,
errors
.
New
(
"incorrect format (expected 'rabin' 'rabin-[avg]' or 'rabin-[min]-[avg]-[max]'"
)
...
...
splitting.go
View file @
397f536c
...
...
@@ -13,9 +13,6 @@ import (
var
log
=
logging
.
Logger
(
"chunk"
)
// DefaultBlockSize is the chunk size that splitters produce (or aim to).
var
DefaultBlockSize
int64
=
1024
*
256
// A Splitter reads bytes from a Reader and creates "chunks" (byte slices)
// that can be used to build DAG nodes.
type
Splitter
interface
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment