Skip to content

Commit 19bb45f

Browse files
authored
fix: only ignore raw-leaves when file is small and metadata is present (#44)
Following further conversation in ipfs/kubo#6940 it seems for small files it would be better to only ignore the `--raw-leaves` flag when metadata is actually present.
1 parent fb81742 commit 19bb45f

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

packages/ipfs-unixfs-importer/src/dag-builder/file/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ const reduce = (file, ipld, options) => {
5353
if (leaves.length === 1 && leaves[0].single && options.reduceSingleLeafToSelf) {
5454
const leaf = leaves[0]
5555

56-
if (leaf.cid.codec === 'raw') {
57-
// only one leaf node which is a buffer
56+
if (leaf.cid.codec === 'raw' && (file.mtime !== undefined || file.mode !== undefined)) {
57+
// only one leaf node which is a buffer - we have metadata so convert it into a
58+
// UnixFS entry otherwise we'll have nowhere to store the metadata
5859
const buffer = await ipld.get(leaf.cid)
5960

6061
leaf.unixfs = new UnixFS({

packages/ipfs-unixfs-importer/test/chunker-custom.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ describe('custom chunker', function () {
6969
cid: await inmem.put(Buffer.from('hello world'), mc.RAW)
7070
}
7171
}
72-
it('works with single part', fromPartsTest(single, 19))
72+
it('works with single part', fromPartsTest(single, 11))
7373
})

packages/ipfs-unixfs-importer/test/importer.spec.js

+79-6
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,22 @@ const strategyOverrides = {
140140
size: 2669627,
141141
type: 'directory'
142142
},
143-
'200Bytes.txt with raw leaves': {
143+
'200Bytes.txt with raw leaves': extend({}, baseFiles['200Bytes.txt'], {
144144
cid: 'QmagyRwMfYhczYNv5SvcJc8xxXjZQBTTHS2jEqNMva2mYT',
145-
size: 200,
146-
path: '200Bytes.txt',
147-
type: 'file'
148-
},
145+
size: 200
146+
}),
147+
'200Bytes.txt with raw leaves and mode': extend({}, baseFiles['200Bytes.txt'], {
148+
cid: 'QmRYYSoRkL9bh5gzbgHndWjt81TYnM4W7MjzTp8WWioLGB',
149+
size: 200
150+
}),
151+
'200Bytes.txt with raw leaves and mtime': extend({}, baseFiles['200Bytes.txt'], {
152+
cid: 'QmQ1QHqXqgxJ4qjJZouRdYG7pdS6yzdhSAq7dYAu9bN6h4',
153+
size: 200
154+
}),
155+
'200Bytes.txt with raw leaves and metadata': extend({}, baseFiles['200Bytes.txt'], {
156+
cid: 'QmWUpftnvHN1Ey5iGoaWwMUZPnViXeJctDSUkcvunkahFo',
157+
size: 200
158+
}),
149159
'foo/bar': {
150160
cid: 'QmTGMxKPzSGNBDp6jhTwnZxGW6w1S9ciyycRJ4b2qcQaHK',
151161
size: 0,
@@ -260,7 +270,18 @@ strategies.forEach((strategy) => {
260270
type: 'directory'
261271
},
262272
'200Bytes.txt with raw leaves': extend({}, baseFiles['200Bytes.txt'], {
263-
cid: 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8',
273+
cid: 'zb2rhXrz1gkCv8p4nUDZRohY6MzBE9C3HVTVDP72g6Du3SD9Q'
274+
}),
275+
'200Bytes.txt with raw leaves and mode': extend({}, baseFiles['200Bytes.txt'], {
276+
cid: 'QmWXbKV9BKJqd8x1NUw1myH987bURrn9Rna3rszYJgQwtX',
277+
size: 200
278+
}),
279+
'200Bytes.txt with raw leaves and mtime': extend({}, baseFiles['200Bytes.txt'], {
280+
cid: 'QmYfLToWgeJwrFFKideGNaS1zkmrow1a9o862sUL43NapC',
281+
size: 200
282+
}),
283+
'200Bytes.txt with raw leaves and metadata': extend({}, baseFiles['200Bytes.txt'], {
284+
cid: 'QmVfHowk2oKuWFyVwSRt8H1dQ3v272jyWSwhfQnTtWNmfw',
264285
size: 200
265286
})
266287
}, strategyOverrides[strategy])
@@ -443,6 +464,58 @@ strategies.forEach((strategy) => {
443464
])
444465
})
445466

467+
it('small file (smaller than a chunk) with raw leaves and mode', async () => {
468+
const files = await all(importer([{
469+
path: '200Bytes.txt',
470+
content: smallFile,
471+
mode: 0o123
472+
}], ipld, {
473+
...options,
474+
rawLeaves: true
475+
}))
476+
477+
expectFiles(files, [
478+
'200Bytes.txt with raw leaves and mode'
479+
])
480+
})
481+
482+
it('small file (smaller than a chunk) with raw leaves and mtime', async () => {
483+
const files = await all(importer([{
484+
path: '200Bytes.txt',
485+
content: smallFile,
486+
mtime: {
487+
secs: 10,
488+
nsecs: 0
489+
}
490+
}], ipld, {
491+
...options,
492+
rawLeaves: true
493+
}))
494+
495+
expectFiles(files, [
496+
'200Bytes.txt with raw leaves and mtime'
497+
])
498+
})
499+
500+
it('small file (smaller than a chunk) with raw leaves and metadata', async () => {
501+
const files = await all(importer([{
502+
path: '200Bytes.txt',
503+
content: smallFile,
504+
mode: 0o123,
505+
mtime: {
506+
secs: 10,
507+
nsecs: 0
508+
}
509+
}], ipld, {
510+
...options,
511+
rawLeaves: true
512+
}))
513+
514+
expectFiles(files, [
515+
'200Bytes.txt with raw leaves and metadata'
516+
])
517+
})
518+
446519
it('small file (smaller than a chunk) inside a dir', async () => {
447520
const files = await all(importer([{
448521
path: 'foo/bar/200Bytes.txt',

0 commit comments

Comments
 (0)