Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 4add865

Browse files
committed
fix: export all files from hamt sharded directories
When you pass the `maxDepth` option, if your sharded directory contains DAGLinks to other shards, you do not get the full directory contents back from the exporter. This PR fixes that.
1 parent a37808a commit 4add865

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/dir-hamt-sharded.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
3333
}
3434
if (accept) {
3535
return {
36-
depth: depth + 1,
36+
depth: p ? depth + 1 : depth,
3737
name: p,
3838
path: pp,
3939
multihash: link.cid.buffer,

test/exporter-sharded.spec.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const importer = require('ipfs-unixfs-importer')
1717

1818
const SHARD_SPLIT_THRESHOLD = 1000
1919

20-
describe('exporter sharded', () => {
20+
describe('exporter sharded', function () {
21+
this.timeout(30000)
22+
2123
let ipld
2224

2325
before((done) => {
@@ -77,6 +79,7 @@ describe('exporter sharded', () => {
7779
const dir = exported.shift()
7880

7981
expect(dir.hash).to.deep.equal(directory.buffer)
82+
expect(exported.length).to.equal(Object.keys(files).length)
8083

8184
parallel(
8285
exported.map(exported => (cb) => {
@@ -102,4 +105,48 @@ describe('exporter sharded', () => {
102105
}
103106
], done)
104107
})
108+
109+
it('exports all the files from a sharded directory with maxDepth', (done) => {
110+
const files = {}
111+
let dirCid
112+
113+
for (let i = 0; i < (SHARD_SPLIT_THRESHOLD + 1); i++) {
114+
files[`file-${Math.random()}.txt`] = {
115+
content: randomBytes(100)
116+
}
117+
}
118+
119+
waterfall([
120+
(cb) => pull(
121+
pull.values(
122+
Object.keys(files).map(path => ({
123+
path,
124+
content: files[path].content
125+
}))
126+
),
127+
importer(ipld, {
128+
wrap: true
129+
}),
130+
pull.collect(cb)
131+
),
132+
(imported, cb) => {
133+
dirCid = new CID(imported.pop().multihash)
134+
135+
pull(
136+
exporter(dirCid, ipld, {
137+
maxDepth: 1
138+
}),
139+
pull.collect(cb)
140+
)
141+
},
142+
(exported, cb) => {
143+
const dir = exported.shift()
144+
145+
expect(dir.hash).to.deep.equal(dirCid.buffer)
146+
expect(exported.length).to.equal(Object.keys(files).length)
147+
148+
cb()
149+
}
150+
], done)
151+
})
105152
})

0 commit comments

Comments
 (0)