Given a file CID is it possible to calculate its directory CID?

Hello people,
The title says it all really.

Assuming a directory containing multiple files was added, e.g. with ipfs.addAll(globSource(folder...))
each of the contained files will have a CID, and the directory itself will have a CID too.

Q1: Is it possible to get the directory CID, only with knowledge of one of the file CIDs?
Q2: How about with knowledge of all contained file CIDs?

Thanks!

Q1: Impossible, or complicated
Q2: Yes

1 Like

+1 with @planetoryd

I’ll also tell you how to do that.

Note in reality this could be really really hard (cryptograpically impossible hard), however go-ipfs is predictable and do easy things. However if someone used an other implementation than go-ipfs it might be impossible but 99%+ of people use go-ipfs in the end.

If you know the list of the all the files and their name in the directory, you can use the MFS to recreate the same dir.

(Note this assume your MFS is empty.)

ipfs files cp /ipfs/Qmfoo /0
ipfs files cp /ipfs/Qmbar /1
ipfs files cp /ipfs/Qmbaz /2
# add all the files ...

ipfs files stat / # Use that to get the CID of the dir

Directory have optional mtime and mode metadata. They can’t be created just from CIDs and names.

go-ipfs doesn’t store that optional metadata for now so it’s not an issue.