-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathutil.go
More file actions
125 lines (101 loc) · 2.72 KB
/
Copy pathutil.go
File metadata and controls
125 lines (101 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package util
import (
"errors"
"io"
"math"
internalio "github.com/ipld/go-car/v2/internal/io"
"github.com/multiformats/go-varint"
cid "github.com/ipfs/go-cid"
)
var ErrSectionTooLarge = errors.New("invalid section data, length of read beyond allowable maximum")
var ErrHeaderTooLarge = errors.New("invalid header data, length of read beyond allowable maximum")
type BytesReader interface {
io.Reader
io.ByteReader
}
func ReadNode(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (cid.Cid, []byte, error) {
data, err := LdRead(r, zeroLenAsEOF, maxReadBytes)
if err != nil {
return cid.Cid{}, nil, err
}
n, c, err := cid.CidFromBytes(data)
if err != nil {
return cid.Cid{}, nil, err
}
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) {
maxReadBytes = min(maxReadBytes, math.MaxInt64) // io.LimitReader doesn't support uint64
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
}
limitReader := io.LimitReader(r, int64(size)) // safe due to the `min` above
n, c, err := cid.CidFromReader(limitReader)
if err != nil {
return cid.Cid{}, 0, err
}
return c, size - uint64(n), nil
}
func LdWrite(w io.Writer, d ...[]byte) error {
var sum uint64
for _, s := range d {
sum += uint64(len(s))
}
buf := make([]byte, 8)
n := varint.PutUvarint(buf, sum)
_, err := w.Write(buf[:n])
if err != nil {
return err
}
for _, s := range d {
_, err = w.Write(s)
if err != nil {
return err
}
}
return nil
}
func LdSize(d ...[]byte) uint64 {
var sum uint64
for _, s := range d {
sum += uint64(len(s))
}
s := varint.UvarintSize(sum)
return sum + uint64(s)
}
func LdReadSize(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) (uint64, error) {
l, err := varint.ReadUvarint(internalio.ToByteReader(r))
if err != nil {
// If the length of bytes read is non-zero when the error is EOF then signal an unclean EOF.
if l > 0 && err == io.EOF {
return 0, io.ErrUnexpectedEOF
}
return 0, err
} else if l == 0 && zeroLenAsEOF {
return 0, io.EOF
}
if l > maxReadBytes { // Don't OOM
return 0, ErrSectionTooLarge
}
return l, nil
}
func LdRead(r io.Reader, zeroLenAsEOF bool, maxReadBytes uint64) ([]byte, error) {
l, err := LdReadSize(r, zeroLenAsEOF, maxReadBytes)
if err != nil {
return nil, err
}
buf := make([]byte, l)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, err
}
return buf, nil
}