hash_directory

Function hash_directory 

Source
pub fn hash_directory(path: PathBuf) -> Result<Vec<u8>, HashError>
Expand description

Given a path to a directory, as a PathBuf, computes the directory hash and returns it as a byte array.

§Examples

no

§Scheme

The hashing scheme is, in essence, generating a Merkle tree, but with extra steps. Each node in the directory tree has its name hashed, then its contents, then those hashes are concatenated with a separator byte based on the node’s type, and that data is hashed again to generate the node’s hash. This process is repeated, from the bottom up in the directory tree, until all nodes have been hashed and a final hash for the entire directory can be returned.

For normal files, the node hash is simply: hash(hash(name) + byte + hash(content))

For directories, the node hash includes arbitrarily many content hashes, one per sub-node: hash(hash(name) + byte + hash(content_1) + byte + hash(content_2) + ... + byte + hash(content_n))

Finally, for symlinks, the link isn’t followed. Instead, the content hash is the hash of the path to the file the link points to. hash(hash(name) + byte + hash(path))