This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not lose files when writing files into subshards that contain…
… other subshards (#3936) When writing a file into a hamt shard we hash the filename to figure out where in the shard to place the file. If the hash means that we end up adding the file into an existing subshard that also contains another subshard, we should populate the other subshard's children otherwise they will not be there when we calculate the new CID for the subshard and they will be lost. Fixes #3921
- Loading branch information
1 parent
15184bf
commit 8a3ed19
Showing
6 changed files
with
130 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { UnixFS } from 'ipfs-unixfs' | ||
|
||
/** | ||
* @param {string} path | ||
* @param {import('ipfs-core-types').IPFS} ipfs | ||
*/ | ||
export default async function dumpShard (path, ipfs) { | ||
const stats = await ipfs.files.stat(path) | ||
const { value: node } = await ipfs.dag.get(stats.cid) | ||
const entry = UnixFS.unmarshal(node.Data) | ||
|
||
if (entry.type !== 'hamt-sharded-directory') { | ||
throw new Error('Not a shard') | ||
} | ||
|
||
await dumpSubShard(stats.cid, ipfs) | ||
} | ||
|
||
/** | ||
* @param {import('multiformats/cid').CID} cid | ||
* @param {import('ipfs-core-types').IPFS} ipfs | ||
* @param {string} prefix | ||
*/ | ||
async function dumpSubShard (cid, ipfs, prefix = '') { | ||
const { value: node } = await ipfs.dag.get(cid) | ||
const entry = UnixFS.unmarshal(node.Data) | ||
|
||
if (entry.type !== 'hamt-sharded-directory') { | ||
throw new Error('Not a shard') | ||
} | ||
|
||
for (const link of node.Links) { | ||
const { value: subNode } = await ipfs.dag.get(link.Hash) | ||
const subEntry = UnixFS.unmarshal(subNode.Data) | ||
console.info(`${prefix}${link.Name}`, ' ', subEntry.type) // eslint-disable-line no-console | ||
|
||
if (link.Name.length === 2) { | ||
await dumpSubShard(link.Hash, ipfs, `${prefix} `) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters