raw dag: make raw nodes work in cat and get, add tests
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Oct 18, 2016 at 11:07 UTC
3796e7020e3570c7574f9cb74a7dbb3e03511cbf
3 files changed
+45
-31
importer/helpers/helpers.go
+8
-1
@@ -105,7 +105,7 @@ func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds dag.DAGService) (*U
105
// the passed in DagBuilderHelper is used to store the child node an
106
// pin it locally so it doesnt get lost
107
func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
108
- n.ufmt.AddBlockSize(child.ufmt.FileSize())
108
+ n.ufmt.AddBlockSize(child.DataSize())
109
110
childnode, err := child.GetDagNode()
111
if err != nil {
@@ -137,6 +137,13 @@ func (n *UnixfsNode) SetData(data []byte) {
137
n.ufmt.Data = data
138
}
139
140
+func (n *UnixfsNode) DataSize() uint64 {
141
+ if n.raw {
142
+ return uint64(len(n.rawnode.RawData()))
143
+ }
144
+ return n.ufmt.FileSize()
145
+}
146
+
147
// getDagNode fills out the proper formatting for the unixfs node
148
// inside of a DAG node and returns the dag node
149
func (n *UnixfsNode) GetDagNode() (node.Node, error) {
test/sharness/t0040-add-and-cat.sh
+10
-6
@@ -87,6 +87,9 @@ test_add_cat_file() {
87
}
88
89
test_add_cat_5MB() {
90
+ ADD_FLAGS="$1"
91
+ EXP_HASH="$2"
92
+
93
test_expect_success "generate 5MB file using go-random" '
94
random 5242880 41 >mountdir/bigfile
95
'
@@ -98,17 +101,16 @@ test_add_cat_5MB() {
101
'
102
103
test_expect_success "'ipfs add bigfile' succeeds" '
101
- ipfs add mountdir/bigfile >actual ||
104
+ ipfs add $ADD_FLAGS mountdir/bigfile >actual ||
105
test_fsh cat daemon_err
106
'
107
108
test_expect_success "'ipfs add bigfile' output looks good" '
106
- HASH="QmSr7FqYkxYWGoSfy8ZiaMWQ5vosb18DQGCzjwEQnVHkTb" &&
107
- echo "added $HASH bigfile" >expected &&
109
+ echo "added $EXP_HASH bigfile" >expected &&
110
test_cmp expected actual
111
'
112
test_expect_success "'ipfs cat' succeeds" '
111
- ipfs cat "$HASH" >actual
113
+ ipfs cat "$EXP_HASH" >actual
114
'
115
116
test_expect_success "'ipfs cat' output looks good" '
@@ -116,7 +118,7 @@ test_add_cat_5MB() {
118
'
119
120
test_expect_success FUSE "cat ipfs/bigfile succeeds" '
119
- cat "ipfs/$HASH" >actual
121
+ cat "ipfs/$EXP_HASH" >actual
122
'
123
124
test_expect_success FUSE "cat ipfs/bigfile looks good" '
@@ -380,7 +382,9 @@ test_expect_success "go-random is installed" '
382
type random
383
'
384
383
-test_add_cat_5MB
385
+test_add_cat_5MB "" "QmSr7FqYkxYWGoSfy8ZiaMWQ5vosb18DQGCzjwEQnVHkTb"
386
+
387
+test_add_cat_5MB --raw-leaves "QmefsDaD3YVphd86mxjJfPLceKv8by98aB6J6sJxK13xS2"
388
389
test_add_cat_expensive
390
unixfs/io/dagreader.go
+27
-24
@@ -129,33 +129,36 @@ func (dr *DagReader) precalcNextBuf(ctx context.Context) error {
129
}
130
dr.linkPosition++
131
132
- nxtpb, ok := nxt.(*mdag.ProtoNode)
133
- if !ok {
134
- return mdag.ErrNotProtobuf
135
- }
136
-
137
- pb := new(ftpb.Data)
138
- err = proto.Unmarshal(nxtpb.Data(), pb)
139
- if err != nil {
140
- return fmt.Errorf("incorrectly formatted protobuf: %s", err)
141
- }
132
+ switch nxt := nxt.(type) {
133
+ case *mdag.ProtoNode:
134
+ pb := new(ftpb.Data)
135
+ err = proto.Unmarshal(nxt.Data(), pb)
136
+ if err != nil {
137
+ return fmt.Errorf("incorrectly formatted protobuf: %s", err)
138
+ }
139
143
- switch pb.GetType() {
144
- case ftpb.Data_Directory:
145
- // A directory should not exist within a file
146
- return ft.ErrInvalidDirLocation
147
- case ftpb.Data_File:
148
- dr.buf = NewDataFileReader(dr.ctx, nxtpb, pb, dr.serv)
149
- return nil
150
- case ftpb.Data_Raw:
151
- dr.buf = NewRSNCFromBytes(pb.GetData())
140
+ switch pb.GetType() {
141
+ case ftpb.Data_Directory:
142
+ // A directory should not exist within a file
143
+ return ft.ErrInvalidDirLocation
144
+ case ftpb.Data_File:
145
+ dr.buf = NewDataFileReader(dr.ctx, nxt, pb, dr.serv)
146
+ return nil
147
+ case ftpb.Data_Raw:
148
+ dr.buf = NewRSNCFromBytes(pb.GetData())
149
+ return nil
150
+ case ftpb.Data_Metadata:
151
+ return errors.New("shouldnt have had metadata object inside file")
152
+ case ftpb.Data_Symlink:
153
+ return errors.New("shouldnt have had symlink inside file")
154
+ default:
155
+ return ft.ErrUnrecognizedType
156
+ }
157
+ case *mdag.RawNode:
158
+ dr.buf = NewRSNCFromBytes(nxt.RawData())
159
return nil
153
- case ftpb.Data_Metadata:
154
- return errors.New("shouldnt have had metadata object inside file")
155
- case ftpb.Data_Symlink:
156
- return errors.New("shouldnt have had symlink inside file")
160
default:
158
- return ft.ErrUnrecognizedType
161
+ return errors.New("unrecognized node type in DagReader")
162
}
163
}
164