From 6c454e8af5a16a5489e9da2a65a82f65a74ac184 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 1 Feb 2022 07:00:58 +0000 Subject: [PATCH] fix: do not add metadata to leaves When an imported file is big enough to need leaf nodes, and that file is imported with metadata, we should only add that metadata to the root of the file DAG and not to every leaf in the graph. --- .../test/importer.spec.js | 28 +++++++++++++++++++ .../src/dag-builder/file/buffer-importer.js | 4 +-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/ipfs-unixfs-exporter/test/importer.spec.js b/packages/ipfs-unixfs-exporter/test/importer.spec.js index 28dd168b..ce1c4e06 100644 --- a/packages/ipfs-unixfs-exporter/test/importer.spec.js +++ b/packages/ipfs-unixfs-exporter/test/importer.spec.js @@ -1045,6 +1045,34 @@ strategies.forEach((strategy) => { const node2 = await exporter(entries[1].cid, block) expect(node2).to.have.nested.property('unixfs.mode', 0o0755) }) + + it('should only add metadata to the root node of a file', async () => { + this.timeout(60 * 1000) + + const mtime = { secs: 5000, nsecs: 0 } + + const entries = await all(importer([{ + path: '/foo/file1.txt', + content: asAsyncIterable(bigFile), + mtime + }], block)) + + const root = await exporter(entries[0].cid, block) + expect(root).to.have.deep.nested.property('unixfs.mtime', mtime) + + if (root.node instanceof Uint8Array) { + throw new Error('Root node was not large enough to have children') + } + + const child = await exporter(root.node.Links[0].Hash, block) + + if (child.type !== 'file') { + throw new Error('Child node was wrong type') + } + + expect(child).to.have.property('unixfs') + expect(child).to.not.have.nested.property('unixfs.mtime') + }) }) }) diff --git a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js index abfcf676..44f64a47 100644 --- a/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js +++ b/packages/ipfs-unixfs-importer/src/dag-builder/file/buffer-importer.js @@ -30,9 +30,7 @@ async function * bufferImporter (file, block, options) { } else { unixfs = new UnixFS({ type: options.leafType, - data: buffer, - mtime: file.mtime, - mode: file.mode + data: buffer }) buffer = dagPb.encode({