3
3
const {
4
4
DAGNode
5
5
} = require ( 'ipld-dag-pb' )
6
- const Bucket = require ( 'hamt-sharding/src/bucket' )
6
+ const {
7
+ Bucket,
8
+ createHAMT
9
+ } = require ( 'hamt-sharding' )
7
10
const DirSharded = require ( 'ipfs-unixfs-importer/src/dir-sharded' )
11
+ const defaultImporterOptions = require ( 'ipfs-unixfs-importer/src/options' )
8
12
const log = require ( 'debug' ) ( 'ipfs:mfs:core:utils:hamt-utils' )
9
13
const UnixFS = require ( 'ipfs-unixfs' )
10
14
const mc = require ( 'multicodec' )
11
15
const mh = require ( 'multihashing-async' ) . multihash
12
16
const last = require ( 'it-last' )
13
17
14
18
const updateHamtDirectory = async ( context , links , bucket , options ) => {
19
+ const importerOptions = defaultImporterOptions ( )
20
+
15
21
// update parent with new bit field
16
22
const data = Uint8Array . from ( bucket . _children . bitField ( ) . reverse ( ) )
17
23
const node = UnixFS . unmarshal ( options . parent . Data )
18
24
const dir = new UnixFS ( {
19
25
type : 'hamt-sharded-directory' ,
20
26
data,
21
27
fanout : bucket . tableSize ( ) ,
22
- hashType : DirSharded . hashFn . code ,
28
+ hashType : importerOptions . hamtHashCode ,
23
29
mode : node . mode ,
24
30
mtime : node . mtime
25
31
} )
@@ -40,14 +46,20 @@ const updateHamtDirectory = async (context, links, bucket, options) => {
40
46
}
41
47
42
48
const recreateHamtLevel = async ( links , rootBucket , parentBucket , positionAtParent ) => {
43
- // recreate this level of the HAMT
44
- const bucket = new Bucket ( {
45
- hashFn : DirSharded . hashFn ,
46
- hash : parentBucket ? parentBucket . _options . hash : undefined
47
- } , parentBucket , positionAtParent )
49
+ const importerOptions = defaultImporterOptions ( )
50
+ let bucket
48
51
52
+ // recreate this level of the HAMT
49
53
if ( parentBucket ) {
54
+ bucket = new Bucket ( {
55
+ hash : parentBucket . _options . hash ,
56
+ bits : parentBucket . _options . bits
57
+ } , parentBucket , positionAtParent )
50
58
parentBucket . _putObjectAt ( positionAtParent , bucket )
59
+ } else {
60
+ bucket = createHAMT ( {
61
+ hashFn : importerOptions . hamtHashFn
62
+ } )
51
63
}
52
64
53
65
await addLinksToHamtBucket ( links , bucket , rootBucket )
@@ -62,7 +74,8 @@ const addLinksToHamtBucket = async (links, bucket, rootBucket) => {
62
74
const pos = parseInt ( link . Name , 16 )
63
75
64
76
bucket . _putObjectAt ( pos , new Bucket ( {
65
- hashFn : DirSharded . hashFn
77
+ hash : bucket . _options . hash ,
78
+ bits : bucket . _options . bits
66
79
} , bucket , pos ) )
67
80
68
81
return Promise . resolve ( )
@@ -102,6 +115,7 @@ const generatePath = async (context, fileName, rootNode) => {
102
115
prefix : toPrefix ( currentBucket . _posAtParent )
103
116
} )
104
117
118
+ // @ts -ignore
105
119
currentBucket = currentBucket . _parent
106
120
}
107
121
0 commit comments