unixfs: fix `dagTruncate` to preserve node type
Extract the original `FSNode` passed inside the `ipld.Node` argument and modify its `Blocksizes` (removing all of them and re-adding the ones that were not truncated). In contrast, the replaced code was creating a new `FSNode` that was not preserving some of the features of the original one. Change `TRUNC_HASH` values in `sharness` that were created with the bug to the correct values. License: MIT Signed-off-by: Lucas Molas <schomatis@gmail.com>
Lucas Molas committed
Jul 11, 2018 at 12:24 UTC
65a18ed669d64dc7a6f85baeda0de8e38e7add07
3 files changed
+19
-7
test/sharness/t0250-files-api.sh
+4
-4
@@ -613,7 +613,7 @@ tests_for_files_api() {
613
ROOT_HASH=QmcwKfTMCT7AaeiD92hWjnZn9b6eh9NxnhfSzN5x2vnDpt
614
CATS_HASH=Qma88m8ErTGkZHbBWGqy1C7VmEmX8wwNDWNpGyCaNmEgwC
615
FILE_HASH=QmQdQt9qooenjeaNhiKHF3hBvmNteB4MQBtgu3jxgf9c7i
616
- TRUNC_HASH=QmdaQZbLwK5ykweGdCVovNnvBom7QhikovDUVqTPHQG4L8
616
+ TRUNC_HASH=QmPVnT9gocPbqzN4G6SMp8vAPyzcjDbUJrNdKgzQquuDg4
617
test_files_api "($EXTRA)"
618
619
test_expect_success "can create some files for testing with raw-leaves ($EXTRA)" '
@@ -629,13 +629,13 @@ tests_for_files_api() {
629
ROOT_HASH=QmW3dMSU6VNd1mEdpk9S3ZYRuR1YwwoXjGaZhkyK6ru9YU
630
CATS_HASH=QmPqWDEg7NoWRX8Y4vvYjZtmdg5umbfsTQ9zwNr12JoLmt
631
FILE_HASH=QmRCgHeoKxCqK2Es6M6nPUDVWz19yNQPnsXGsXeuTkSKpN
632
- TRUNC_HASH=QmRFJEKWF5A5FyFYZgNhusLw2UziW9zBKYr4huyHjzcB6o
632
+ TRUNC_HASH=QmckstrVxJuecVD1FHUiURJiU9aPURZWJieeBVHJPACj8L
633
test_files_api "($EXTRA, raw-leaves)" '' --raw-leaves
634
635
ROOT_HASH=QmageRWxC7wWjPv5p36NeAgBAiFdBHaNfxAehBSwzNech2
636
CATS_HASH=zdj7WkEzPLNAr5TYJSQC8CFcBjLvWFfGdx6kaBrJXnBguwWeX
637
FILE_HASH=zdj7WYHvf5sBRgSBjYnq64QFr449CCbgupXfBvoYL3aHC1DzJ
638
- TRUNC_HASH=zdj7WYLYbka6Ydg8gZUJRLKnFBVehCADhQKBsFbNiMxZSB5Gj
638
+ TRUNC_HASH=zdj7Wjr8GHZonPFVCWvz2SLLo9H6MmqBxyeB34ArHfyCbmdJG
639
if [ "$EXTRA" = "offline" ]; then
640
test_files_api "($EXTRA, cidv1)" --cid-version=1
641
fi
@@ -660,7 +660,7 @@ tests_for_files_api() {
660
ROOT_HASH=zDMZof1kxEsAwSgCZsGQRVcHCMtHLjkUQoiZUbZ87erpPQJGUeW8
661
CATS_HASH=zDMZof1kuAhr3zBkxq48V7o9HJZCTVyu1Wd9wnZtVcPJLW8xnGft
662
FILE_HASH=zDMZof1kxbB9CvxgRioBzESbGnZUxtSCsZ18H1EUkxDdWt1DYEkK
663
- TRUNC_HASH=zDMZof1kxXqKdVsVo231qVdN3hCTF5a34UuQZpzmm5K7CbRJ4u2S
663
+ TRUNC_HASH=zDMZof1kpH1vxK3k2TeYc8w59atCbzMzrhZonsztMWSptVro2zQa
664
test_files_api "($EXTRA, blake2b-256 root)"
665
fi
666
unixfs/mod/dagmodifier.go
+9
-3
@@ -529,7 +529,13 @@ func dagTruncate(ctx context.Context, n ipld.Node, size uint64, ds ipld.DAGServi
529
var cur uint64
530
end := 0
531
var modified ipld.Node
532
- ndata := ft.NewFSNode(ft.TRaw)
532
+ ndata, err := ft.FSNodeFromBytes(nd.Data())
533
+ if err != nil {
534
+ return nil, err
535
+ }
536
+ // Reset the block sizes of the node to adjust them
537
+ // with the new values of the truncated children.
538
+ ndata.RemoveAllBlockSizes()
539
for i, lnk := range nd.Links() {
540
child, err := lnk.GetNode(ctx, ds)
541
if err != nil {
@@ -558,7 +564,7 @@ func dagTruncate(ctx context.Context, n ipld.Node, size uint64, ds ipld.DAGServi
564
ndata.AddBlockSize(childsize)
565
}
566
561
- err := ds.Add(ctx, modified)
567
+ err = ds.Add(ctx, modified)
568
if err != nil {
569
return nil, err
570
}
@@ -573,7 +579,7 @@ func dagTruncate(ctx context.Context, n ipld.Node, size uint64, ds ipld.DAGServi
579
if err != nil {
580
return nil, err
581
}
576
-
582
+ // Save the new block sizes to the original node.
583
nd.SetData(d)
584
585
// invalidate cache and recompute serialized data
unixfs/unixfs.go
+6
@@ -201,6 +201,12 @@ func (n *FSNode) BlockSize(i int) uint64 {
201
return n.format.Blocksizes[i]
202
}
203
204
+// RemoveAllBlockSizes removes all the child block sizes of this node.
205
+func (n *FSNode) RemoveAllBlockSizes() {
206
+ n.format.Blocksizes = []uint64{}
207
+ n.format.Filesize = proto.Uint64(uint64(len(n.Data())))
208
+}
209
+
210
// GetBytes marshals this node as a protobuf message.
211
func (n *FSNode) GetBytes() ([]byte, error) {
212
return proto.Marshal(&n.format)