Skip to main content

uv_extract/
dirhash.rs

1//! Dirhash is a scheme for hashing directory trees, and by extension the contents of archive
2//! files.
3//!
4//! The underlying hash function is BLAKE3, and the dirhash of a file is the regular `blake3::hash`
5//! of its content bytes. To compute the dirhash of a directory, we sort and concatenate its
6//! entries. Each entry has three components, which are also concatenated:
7//!
8//! - the UTF-8 filename or subdirectory name
9//! - a terminator byte, `0xff`, which cannot occur in UTF-8
10//! - the 32-byte dirhash (recursive) of the entry's contents
11//!
12//! To avoid collisions between files and directories, we compute the hash of those sorted,
13//! concatenated directory entries with `blake3::derive_key("directory", ...)`. The implementation
14//! checks that directory entries are sorted and that their names are unique and valid UTF-8. It
15//! also checks that names don't contain `/` and aren't equal to `.` or `..`. Empty directories are
16//! represented as the empty hash rather than omitted (as in Git).
17//!
18//! Symlinks aren't encoded, and we hash symlinks as the files or directories they points to. That
19//! means we can't compute the dirhash of a symlink cycle. When the implementation is reading the
20//! filesystem, it detects cycles and reports an error in that case.
21//!
22//! We don't hash any metadata about a file besides its name. In particular, that means that
23//! (unlike Git) we don't hash the Unix executable bit. Two archives that encode the same files
24//! with different executable bits could have the same dirhash, and its possible that could cause
25//! bugs in some cases. On this other hand, this gives us the property that the dirhash of an
26//! archive is the same as the dirhash of its unpacked files, even if the archive was prepared on
27//! Unix and unpacked on Windows. Note that Python wheel installers [already include
28//! heuristics][heuristics] for these cross-platform problems.
29//!
30//! [heuristics]: https://packaging.python.org/en/latest/specifications/binary-distribution-format/#recommended-installer-features
31//!
32//! There are two separate implementations in this module:
33//!
34//! - [`dirhash_path`] reads a directory tree from the filesystem and hashes it using Rayon.
35//! - [`DirhashTree`] is an in-memory representation of a directory tree, which accepts entries in
36//!   any order. This is intended for inspecting or unpacking archives, so that we can hash file
37//!   bytes while they're in memory instead of extracting the whole tree and reading it back. The
38//!   `blake3_copy` function helps with the common case of extracting a `Read` implementation (like
39//!   `ZipEntryReader`) to a `Write` implementation (like `std::fs::File`).
40//!
41//! # Example
42//!
43//! ```
44//! # use uv_extract::dirhash::{dirhash_path, DirhashTree};
45//! # fn main() -> anyhow::Result<()> {
46//! // Create a small tree of files and hash it by path.
47//! let tempdir = tempfile::tempdir()?;
48//! std::fs::create_dir(tempdir.path().join("a"))?;
49//! std::fs::write(tempdir.path().join("a/b.txt"), b"hello")?;
50//! std::fs::write(tempdir.path().join("c.txt"), b"goodbye")?;
51//! let fs_hash = dirhash_path(tempdir.path())?;
52//!
53//! // Recompute the same hash in memory.
54//! let mut tree = DirhashTree::new();
55//! tree.add_file("a/b.txt", blake3::hash(b"hello"))?;
56//! tree.add_file("c.txt", blake3::hash(b"goodbye"))?;
57//! let in_memory_hash = tree.hash();
58//!
59//! // Both approaches should give the same result.
60//! assert_eq!(fs_hash, in_memory_hash);
61//! # Ok(())
62//! # }
63//! ```
64use std::borrow::Cow;
65use std::collections::BTreeMap;
66use std::collections::btree_map::Entry;
67use std::io;
68use std::path::{Path, PathBuf};
69use std::pin::{Pin, pin};
70
71use rayon::prelude::*;
72use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
73
74// Read repeatedly until the whole buffer is full, similar to `read_exact`. But if EOF is
75// encountered, return `Ok(n)` with a short length instead of reporting an error.
76async fn read_exact_or_eof(
77    mut reader: Pin<&mut impl AsyncRead>,
78    mut buf: &mut [u8],
79) -> io::Result<usize> {
80    let mut bytes_read = 0;
81    loop {
82        match reader.read(buf).await {
83            Ok(0) => return Ok(bytes_read),
84            Ok(n) => {
85                bytes_read += n;
86                if n == buf.len() {
87                    return Ok(bytes_read);
88                }
89                buf = &mut buf[n..];
90            }
91            Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
92            Err(e) => return Err(e),
93        }
94    }
95}
96
97/// Copy all the bytes from an async reader to an async writer while computing their BLAKE3 hash.
98/// This uses the same buffer for reading, writing, and hashing, to avoid unnecessary re-reads or
99/// intermediate copies. Return the number of bytes copied and the resulting hash.
100pub async fn blake3_copy<R, W>(reader: R, writer: W) -> io::Result<(u64, blake3::Hash)>
101where
102    R: AsyncRead,
103    W: AsyncWrite,
104{
105    let mut reader = pin!(reader);
106    let mut writer = pin!(writer);
107    let mut hasher = blake3::Hasher::new();
108    let mut buffer = [0; 1 << 16]; // 64 KiB
109    let mut total = 0u64;
110    // BLAKE3 is fastest when hashing power-of-two sized buffers. That maximizes the time we spend
111    // in the wide SIMD part of the implementation (which wants between 4 and 16 KiB at a time
112    // depending on the platform) and minimizes the time we spend in the slower part that handles
113    // short inputs. Hash as many full 64 KiB buffers as we can, and then one possibly-short buffer
114    // when we reach EOF.
115    loop {
116        let bytes_read = read_exact_or_eof(reader.as_mut(), &mut buffer).await?;
117        if bytes_read == 0 {
118            break; // EOF reached with no bytes. Skip unnecessary calls to `update` and `write_all`.
119        }
120        total += bytes_read as u64;
121        let bytes = &buffer[..bytes_read];
122        hasher.update(bytes);
123        writer.write_all(bytes).await?;
124        if bytes_read < buffer.len() {
125            break; // EOF
126        }
127    }
128    writer.flush().await?;
129    Ok((total, hasher.finalize()))
130}
131
132#[derive(Debug, thiserror::Error)]
133pub enum DirhashError {
134    #[error("Invalid path for directory hashing: {path:?}")]
135    InvalidPath { path: PathBuf },
136    #[error("Archive path is missing from the directory hash tree: {path:?}")]
137    MissingPath { path: PathBuf },
138    #[error("Archive contains duplicate entries for path: {path:?}")]
139    DuplicatePath { path: PathBuf },
140    #[error("Archive path is used as both a file and a directory: {path:?}")]
141    FileDirectoryConflict { path: PathBuf },
142    #[error("Encountered a symlink cycle while hashing a directory: {paths:?}")]
143    SymlinkCycle { paths: Vec<PathBuf> },
144    #[error(transparent)]
145    Io(#[from] io::Error),
146}
147
148// Seen symlinks form a linked list on the stack as we recurse.
149struct SeenSymlinkNode<'a> {
150    canonical_path: PathBuf,
151    previous: Option<&'a Self>,
152}
153
154struct SeenSymlinks<'a> {
155    node: Option<SeenSymlinkNode<'a>>,
156}
157
158impl<'a> SeenSymlinks<'a> {
159    fn new() -> Self {
160        Self { node: None }
161    }
162
163    fn iter(&self) -> impl Iterator<Item = &Path> {
164        let mut node = self.node.as_ref();
165        std::iter::from_fn(move || {
166            if let Some(next_node) = node {
167                let next_path = &next_node.canonical_path;
168                node = next_node.previous;
169                Some(next_path.as_path())
170            } else {
171                None
172            }
173        })
174    }
175
176    fn push(&'a self, symlink_path: &Path) -> Result<Self, DirhashError> {
177        let canonical_path = canonical_path_to_symlink(symlink_path)?;
178        // Walk the seen symlinks list and error out if we've seen this one before.
179        for seen in self.iter() {
180            if canonical_path == seen {
181                let mut paths: Vec<PathBuf> = self.iter().map(Path::to_owned).collect();
182                paths.reverse();
183                paths.push(canonical_path);
184                return Err(DirhashError::SymlinkCycle { paths });
185            }
186        }
187        Ok(Self {
188            node: Some(SeenSymlinkNode {
189                canonical_path,
190                previous: self.node.as_ref(),
191            }),
192        })
193    }
194}
195
196// The canonical path *to a link itself*, not the canonical path the link *points to*. For a
197// regular file or directory, this is the same as its canonical path.
198fn canonical_path_to_symlink(symlink_path: &Path) -> Result<PathBuf, DirhashError> {
199    let Some(filename) = symlink_path.file_name() else {
200        return Err(DirhashError::InvalidPath {
201            path: symlink_path.to_path_buf(),
202        });
203    };
204    let parent = symlink_path
205        .parent()
206        .filter(|parent| !parent.as_os_str().is_empty())
207        .unwrap_or(Path::new("."));
208    Ok(fs_err::canonicalize(parent)?.join(filename))
209}
210
211/// Compute the dirhash of a file or directory tree on disk.
212///
213/// `path` itself can be any existing path on the system, and it's not required to be valid
214/// Unicode. However, if `path` is a directory, its contents need to have Unicode names, otherwise
215/// `dirhash_path` returns an error.
216///
217/// `dirhash_path` will traverse symlinks, including links that lead outside of `path`. However, if
218/// it encounters a symlink cycle, it will return an error.
219pub fn dirhash_path(path: &Path) -> Result<blake3::Hash, DirhashError> {
220    uv_configuration::initialize_rayon_once();
221    let seen_symlinks = SeenSymlinks::new();
222    dirhash_path_inner(path, &seen_symlinks)
223}
224
225// Recurse to compute a dirhash, handling symlink cycles.
226fn dirhash_path_inner(
227    path: &Path,
228    seen_symlinks: &SeenSymlinks,
229) -> Result<blake3::Hash, DirhashError> {
230    let metadata = fs_err::symlink_metadata(path)?;
231    if metadata.is_symlink() {
232        let seen_symlinks = seen_symlinks.push(path)?;
233        dirhash_path_inner_resolved(path, &fs_err::metadata(path)?, &seen_symlinks)
234    } else {
235        dirhash_path_inner_resolved(path, &metadata, seen_symlinks)
236    }
237}
238
239// Recurse to compute a dirhash, after symlinks are resolved.
240fn dirhash_path_inner_resolved(
241    path: &Path,
242    metadata: &std::fs::Metadata,
243    seen_symlinks: &SeenSymlinks,
244) -> Result<blake3::Hash, DirhashError> {
245    if metadata.is_dir() {
246        // This is a directory. Recurse over its contents.
247        let mut dir_contents = Vec::new();
248        for entry in fs_err::read_dir(path)? {
249            let entry = entry?;
250            let path = entry.path();
251            // Prior components of the `path` can be non-Unicode, but names in the hashed directory
252            // tree are required to be Unicode, otherwise we report an error.
253            let Ok(name) = entry.file_name().into_string() else {
254                return Err(DirhashError::InvalidPath { path });
255            };
256            dir_contents.push((name, path));
257        }
258        // Sort the directory contents by name, in lexicographic/UTF-8 order.
259        dir_contents.sort_unstable();
260        // Iterate over the contents in parallel using Rayon, hashing each one recursively.
261        let hashes = dir_contents
262            .par_iter()
263            // Recurse back to `dirhash_path_inner` for symlink handling.
264            .map(|(_, path)| dirhash_path_inner(path, seen_symlinks))
265            .collect::<Result<Vec<blake3::Hash>, _>>()?;
266        let dirhash_entries = dir_contents
267            .iter()
268            .zip(hashes)
269            .map(|((name, _), hash)| (name.as_str(), hash));
270        Ok(hash_dir_entries(dirhash_entries))
271    } else {
272        // This is not a directory, so treat it like a file and hash it. `update_mmap_rayon` shares
273        // the same thread pool as `par_iter` above.
274        Ok(blake3::Hasher::new().update_mmap_rayon(path)?.finalize())
275    }
276}
277
278#[derive(Debug, Clone)]
279enum DirhashEntry {
280    File(blake3::Hash),
281    Directory(DirhashTree),
282}
283
284/// An in-memory directory structure for computing a dirhash from an archive as we unpack it, when
285/// the entries might come out in any order.
286///
287/// See the [module-level example](index.html#example) for usage.
288#[derive(Debug, Clone, Default)]
289pub struct DirhashTree {
290    children: BTreeMap<String, DirhashEntry>,
291}
292
293impl DirhashTree {
294    /// Create an new, empty `DirhashTree`.
295    pub fn new() -> Self {
296        Self::default()
297    }
298
299    fn insertion_entry(
300        &mut self,
301        normalized_path: &str,
302        original_path: &str,
303        create_dirs: bool,
304    ) -> Result<Entry<'_, String, DirhashEntry>, DirhashError> {
305        if let Some((component, rest)) = normalized_path.split_once('/') {
306            // There are further path components after this one, so this one is a directory.
307            if self.children.contains_key(component) {
308                // This entry already exists.
309                //
310                // We have to do a double lookup here because of borrowck limitations. The
311                // alternative is using the `.entry()` API and always allocating a temporary
312                // `String` key. Polonius can't come soon enough, but also `BTreeMap` needs a "raw
313                // entry" API.
314                match self.children.get_mut(component).unwrap() {
315                    DirhashEntry::Directory(child) => {
316                        child.insertion_entry(rest, original_path, create_dirs)
317                    }
318                    DirhashEntry::File(_) => Err(DirhashError::FileDirectoryConflict {
319                        path: PathBuf::from(original_path),
320                    }),
321                }
322            } else {
323                // We need to create this directory, or error if `create_dirs` is false.
324                if create_dirs {
325                    let child = self
326                        .children
327                        .entry(String::from(component))
328                        .or_insert(DirhashEntry::Directory(Self::default()));
329                    let DirhashEntry::Directory(child) = child else {
330                        unreachable!()
331                    };
332                    child.insertion_entry(rest, original_path, create_dirs)
333                } else {
334                    Err(DirhashError::MissingPath {
335                        path: PathBuf::from(original_path),
336                    })
337                }
338            }
339        } else {
340            // This is the final path component.
341            Ok(self.children.entry(String::from(normalized_path)))
342        }
343    }
344
345    /// Add a pre-hashed file to the tree. It's an error if the filepath already exists.
346    ///
347    /// `path` is a Unix-style, `/`-separated relative path. This is the format used in ZIP
348    /// archives. Trailing slashes are ignored.
349    ///
350    /// This function creates parent directories as needed.
351    ///
352    /// The `hash` of a file is the standard [`blake3::hash`] of its contents. If the file is
353    /// already on disk, the typical way to hash it is either [`blake3::Hasher::update_reader`]
354    /// (ordinary serial reads) or [`blake3::Hasher::update_mmap_rayon`] (memory mapping plus
355    /// multithreading). However, note that if you're also writing the file to disk, it's more
356    /// efficient to hash it as you write it than to read it back again. See [`blake3_copy`].
357    pub fn add_file(&mut self, path: &str, hash: blake3::Hash) -> Result<(), DirhashError> {
358        let normalized_path = normalize_dirhash_path(path)?;
359        let entry = self.insertion_entry(&normalized_path, path, true)?;
360        match entry {
361            Entry::Vacant(vacant) => {
362                vacant.insert(DirhashEntry::File(hash));
363                Ok(())
364            }
365            Entry::Occupied(_) => Err(DirhashError::DuplicatePath {
366                path: PathBuf::from(path),
367            }),
368        }
369    }
370
371    /// Update the hash of a file in the tree. It's an error if the filepath doesn't exist or if it
372    /// refers to a directory.
373    ///
374    /// `path` is a Unix-style, `/`-separated relative path. This is the format used in ZIP
375    /// archives. Trailing slashes are ignored.
376    ///
377    /// The separation between [`add_file`](Self::add_file) and `update_file` is intended to catch
378    /// cases where an archive has duplicate entries for the same filepath. Those cases should
379    /// ideally produce an error during unpacking, rather than arbitrarily picking a winner.
380    /// `update_file` is intended for callers who are deliberately changing the contents of a path
381    /// that already exists.
382    pub fn update_file(&mut self, path: &str, hash: blake3::Hash) -> Result<(), DirhashError> {
383        let normalized_path = normalize_dirhash_path(path)?;
384        let entry = self.insertion_entry(&normalized_path, path, false)?;
385        match entry {
386            Entry::Vacant(_) => Err(DirhashError::MissingPath {
387                path: PathBuf::from(path),
388            }),
389            Entry::Occupied(mut occupied) => match occupied.get_mut() {
390                DirhashEntry::File(prev_hash) => {
391                    *prev_hash = hash;
392                    Ok(())
393                }
394                DirhashEntry::Directory(_) => Err(DirhashError::FileDirectoryConflict {
395                    path: PathBuf::from(path),
396                }),
397            },
398        }
399    }
400
401    /// Add an empty directory to the tree. This succeeds if the directory already exists, but it's
402    /// an error if the path refers to a file.
403    ///
404    /// `path` is a Unix-style, `/`-separated relative path. This is the format used in ZIP
405    /// archives. Trailing slashes are ignored.
406    ///
407    /// This function creates parent directories as needed.
408    pub fn add_empty_dir(&mut self, path: &str) -> Result<(), DirhashError> {
409        let normalized_path = normalize_dirhash_path(path)?;
410        let entry = self.insertion_entry(&normalized_path, path, true)?;
411        match entry {
412            Entry::Vacant(vacant) => {
413                vacant.insert(DirhashEntry::Directory(Self::default()));
414                Ok(())
415            }
416            Entry::Occupied(occupied) => match occupied.get() {
417                DirhashEntry::Directory(_) => Ok(()),
418                DirhashEntry::File(_) => Err(DirhashError::FileDirectoryConflict {
419                    path: PathBuf::from(path),
420                }),
421            },
422        }
423    }
424
425    /// Compute the root dirhash of the assembled tree.
426    ///
427    /// This method is idempotent, and you can call it again after adding more files or updating
428    /// existing ones. However, `DirhashTree` doesn't currently cache the subtree hashes of
429    /// unchanged directories, so calling `hash` repeatedly can be wasteful.
430    pub fn hash(&self) -> blake3::Hash {
431        hash_dir_entries(self.children.iter().map(|(name, entry)| {
432            let hash = match entry {
433                DirhashEntry::File(hash) => *hash,
434                DirhashEntry::Directory(child) => child.hash(),
435            };
436            (name.as_str(), hash)
437        }))
438    }
439}
440
441fn component_needs_normalization(component: &str) -> bool {
442    matches!(component, "" | "." | "..")
443}
444
445fn normalize_dirhash_path(mut path: &str) -> Result<Cow<'_, str>, DirhashError> {
446    if path.starts_with('/') {
447        return Err(DirhashError::InvalidPath {
448            path: PathBuf::from(path),
449        });
450    }
451    path = path.trim_start_matches("./");
452    path = path.trim_end_matches('/');
453    if !path.split('/').any(component_needs_normalization) {
454        return Ok(Cow::Borrowed(path));
455    }
456    let mut components = Vec::new();
457    for component in path.split('/') {
458        match component {
459            "" | "." => {}
460            ".." => {
461                if components.pop().is_none() {
462                    return Err(DirhashError::InvalidPath {
463                        path: PathBuf::from(path),
464                    });
465                }
466            }
467            component => components.push(component),
468        }
469    }
470    if components.is_empty() {
471        return Err(DirhashError::InvalidPath {
472            path: PathBuf::from(path),
473        });
474    }
475    Ok(Cow::Owned(components.join("/")))
476}
477
478fn hash_dir_entries<'a, Iter>(entries: Iter) -> blake3::Hash
479where
480    Iter: IntoIterator<Item = (&'a str, blake3::Hash)>,
481{
482    // File hashes are the normal BLAKE3 hash of the file's contents. A directory hash shouldn't
483    // collide with a file hash, no matter what bytes the file happens contain. BLAKE3's derive-key
484    // mode with a context string guarantees that.
485    let mut hasher = blake3::Hasher::new_derive_key("directory");
486    for (name, hash) in entries {
487        hasher.update(name.as_bytes());
488        hasher.update(&[0xff]);
489        hasher.update(hash.as_bytes());
490    }
491    hasher.finalize()
492}
493
494#[cfg(test)]
495mod tests {
496    use super::*;
497    use std::cmp;
498    use std::task::{Context, Poll};
499
500    #[test]
501    fn test_normalize() {
502        let success_cases = [
503            ("foo", Cow::Borrowed("foo")),
504            ("foo", Cow::Borrowed("foo")),
505            ("foo/", Cow::Borrowed("foo")),
506            ("./foo", Cow::Borrowed("foo")),
507            ("././foo/bar///", Cow::Borrowed("foo/bar")),
508            ("foo//bar", Cow::Owned("foo/bar".to_string())),
509            ("foo/./bar", Cow::Owned("foo/bar".to_string())),
510            ("foo/.///./bar", Cow::Owned("foo/bar".to_string())),
511            ("foo/bar/..", Cow::Owned("foo".to_string())),
512            ("foo/bar/../../baz", Cow::Owned("baz".to_string())),
513        ];
514        for (path, expected) in success_cases {
515            let normalized = super::normalize_dirhash_path(path).unwrap();
516            assert_eq!(normalized, expected);
517        }
518        let error_cases = [
519            "",
520            "/",
521            "/foo",
522            "///foo",
523            "..",
524            "foo/..",
525            "foo/bar/../../../baz",
526        ];
527        for path in error_cases {
528            super::normalize_dirhash_path(path).unwrap_err();
529        }
530    }
531
532    #[test]
533    fn test_add_update_and_add_empty_dir() {
534        // Hash the following tree:
535        //
536        // a.txt      <-- "hello"
537        // b
538        // ├── c.txt  <-- "goodbye"
539        // └── d      <-- empty dir
540        //
541        // First, assemble the whole hash tree manually.
542        let a_hash = blake3::hash(b"hello");
543        let c_hash = blake3::hash(b"goodbye");
544        let d_hash = blake3::derive_key("directory", b"");
545        let mut b_input = Vec::new();
546        b_input.extend_from_slice(b"c.txt\xff");
547        b_input.extend_from_slice(c_hash.as_bytes());
548        b_input.extend_from_slice(b"d\xff");
549        b_input.extend_from_slice(&d_hash);
550        let b_hash = blake3::derive_key("directory", &b_input);
551        let mut root_input = Vec::new();
552        root_input.extend_from_slice(b"a.txt\xff");
553        root_input.extend_from_slice(a_hash.as_bytes());
554        root_input.extend_from_slice(b"b\xff");
555        root_input.extend_from_slice(&b_hash);
556        let root_hash = blake3::derive_key("directory", &root_input);
557        // Pin the specific value of the dirhash. TODO: a full set of test vectors
558        assert_eq!(
559            blake3::Hash::from_bytes(root_hash).to_hex().as_str(),
560            "e508467d129e0d19cefa96527f5f6cb3760530be4d931c527f2818a0dff5d517"
561        );
562
563        // Now, confirm that `DirhashTree` gives the same answer.
564        let mut tree = super::DirhashTree::default();
565        tree.add_file("a.txt", a_hash).unwrap();
566        tree.add_file("b/c.txt", c_hash).unwrap();
567        tree.add_empty_dir("b/d").unwrap();
568        assert_eq!(tree.hash(), root_hash);
569
570        // Changing the hash of a file changes the root hash.
571        tree.update_file("b/c.txt", [0; 32].into()).unwrap();
572        assert_ne!(tree.hash(), root_hash);
573        // But we can change it back and recover the original.
574        tree.update_file("b/c.txt", c_hash).unwrap();
575        assert_eq!(tree.hash(), root_hash);
576
577        // Reinserting an existing empty directory is a no-op.
578        tree.add_empty_dir("b").unwrap(); // no-op
579        assert_eq!(tree.hash(), root_hash);
580        // But inserting a new empty directory changes the hash.
581        tree.add_empty_dir("e").unwrap(); // no-op
582        assert_ne!(tree.hash(), root_hash);
583    }
584
585    #[test]
586    fn test_dirhash_path() -> Result<(), super::DirhashError> {
587        // Hash the following tree:
588        //
589        // a.txt      <-- "hello"
590        // b
591        // ├── c.txt  <-- "goodbye"
592        // └── d      <-- empty dir
593        //
594        // Compare both `DirhashTree` (in memory) and `dirhash_path` (on disk) to make sure we get
595        // the same hash from both.
596        let temp_dir = tempfile::tempdir()?;
597        let root = temp_dir.path();
598        fs_err::write(root.join("a.txt"), b"hello")?;
599        fs_err::create_dir(root.join("b"))?;
600        fs_err::write(root.join("b/c.txt"), b"goodbye")?;
601        fs_err::create_dir(root.join("b/d"))?;
602
603        let mut expected = super::DirhashTree::default();
604        expected.add_file("a.txt", blake3::hash(b"hello"))?;
605        expected.add_file("b/c.txt", blake3::hash(b"goodbye"))?;
606        expected.add_empty_dir("b/d")?;
607
608        assert_eq!(super::dirhash_path(root)?, expected.hash());
609        // Asking for the dirhash of a file is also valid, and it's equivalent to the regular
610        // BLAKE3 hash.
611        assert_eq!(
612            super::dirhash_path(&root.join("a.txt"))?,
613            blake3::hash(b"hello")
614        );
615        Ok(())
616    }
617
618    #[cfg(unix)]
619    #[test]
620    fn test_dirhash_path_symlinks() -> Result<(), super::DirhashError> {
621        use fs_err::os::unix::fs::symlink;
622
623        // Start with the following tree, which does not have a cycle:
624        //
625        // dir1
626        // ├── file.txt  <-- "hello"
627        // └── dir_link  <-- ../dir2
628        // dir2
629        // └── file_link <-- ../dir1/file.txt
630        //
631        // Make sure we get the same answer from both `DirhashTree` (in memory) and `dirhash_path`
632        // (on disk).
633        let temp_dir = tempfile::tempdir()?;
634        let root = temp_dir.path();
635        fs_err::create_dir(root.join("dir1"))?;
636        fs_err::create_dir(root.join("dir2"))?;
637        fs_err::write(root.join("dir1/file.txt"), b"hello")?;
638        symlink("../dir2", root.join("dir1/dir_link"))?;
639        symlink("../dir1/file.txt", root.join("dir2/file_link"))?;
640
641        let mut in_memory = super::DirhashTree::default();
642        in_memory.add_file("dir1/file.txt", blake3::hash(b"hello"))?;
643        in_memory.add_file("dir1/dir_link/file_link", blake3::hash(b"hello"))?;
644        in_memory.add_file("dir2/file_link", blake3::hash(b"hello"))?;
645        let from_disk = super::dirhash_path(root)?;
646        assert_eq!(in_memory.hash(), from_disk);
647
648        // Now add another symlink to make a proper cycle. This should error.
649        fs_err::create_dir(root.join("dir2/inner"))?;
650        symlink("../../dir1", root.join("dir2/inner/dir_link"))?;
651        let error = super::dirhash_path(root).unwrap_err();
652        std::assert_matches!(error, super::DirhashError::SymlinkCycle { .. });
653        Ok(())
654    }
655
656    /// Write a test input byte pattern that doesn't repeat at regular power-of-two boundaries.
657    /// This is more likely to catch mistakes than hashing a buffer of e.g. all zeros.
658    fn paint_input(buf: &mut [u8]) {
659        let mut value = 0u8;
660        for byte in buf {
661            *byte = value;
662            value = if value == 250 { 0 } else { value + 1 };
663        }
664    }
665
666    #[tokio::test]
667    async fn test_blake3_copy() -> io::Result<()> {
668        let input = b"hello";
669        let mut output = Vec::new();
670        let (bytes_read, hash) = Box::pin(super::blake3_copy(&input[..], &mut output)).await?;
671        assert_eq!(bytes_read, input.len() as u64);
672        assert_eq!(input, &output[..]);
673        assert_eq!(hash, blake3::hash(input));
674
675        let mut big_input = vec![0; 64_000 * 3];
676        paint_input(&mut big_input);
677        let mut big_output = Vec::new();
678        let (big_bytes_read, big_hash) =
679            Box::pin(super::blake3_copy(&big_input[..], &mut big_output)).await?;
680        assert_eq!(big_bytes_read, big_input.len() as u64);
681        assert_eq!(big_input, big_output);
682        assert_eq!(big_hash, blake3::hash(&big_input));
683        Ok(())
684    }
685
686    /// A reader that always returns short reads, even if it holds lots of input.
687    struct ShortReader<'a>(&'a [u8]);
688
689    impl AsyncRead for ShortReader<'_> {
690        fn poll_read(
691            mut self: Pin<&mut Self>,
692            _cx: &mut Context<'_>,
693            buf: &mut tokio::io::ReadBuf<'_>,
694        ) -> Poll<io::Result<()>> {
695            const SHORT_READ_LEN: usize = 251; // any small prime will do
696            let want = cmp::min(self.0.len(), buf.remaining());
697            let take = cmp::min(want, SHORT_READ_LEN);
698            buf.put_slice(&self.0[..take]);
699            self.0 = &self.0[take..];
700            Poll::Ready(Ok(()))
701        }
702    }
703
704    /// Exercise the buffer filling logic with a reader that always returns short reads.
705    #[tokio::test]
706    async fn test_blake3_copy_short_reader() -> io::Result<()> {
707        let mut input = vec![0; 64_000 * 3];
708        paint_input(&mut input);
709        let mut output = Vec::new();
710        let (bytes_read, hash) =
711            Box::pin(super::blake3_copy(ShortReader(&input), &mut output)).await?;
712        assert_eq!(bytes_read, input.len() as u64);
713        assert_eq!(input, &output[..]);
714        assert_eq!(hash, blake3::hash(&input));
715        Ok(())
716    }
717
718    // Populate both a `DirhashTree` and a temp dir on the filesystem, by recursively walking the
719    // input tree of a JSON test vector. See `../test_vectors/test_vectors.json`.
720    fn walk_test_vector_input(
721        input_dir: &serde_json::Map<String, serde_json::Value>,
722        dirhash_tree: &mut DirhashTree,
723        tempdir: &tempfile::TempDir,
724        // The relative path starts as `None` and grows as we descend recursively into the input tree.
725        relative_path: Option<&str>,
726    ) -> anyhow::Result<()> {
727        for (name, file_or_dir) in input_dir {
728            // Use Unix-style forward slashes for the relative path, because that's what
729            // `DirhashTree` expects. `Path::join` can handle these, even on Windows.
730            let entry_path = match relative_path {
731                Some(parent) => &format!("{parent}/{name}"),
732                None => name,
733            };
734            match file_or_dir {
735                // a file
736                serde_json::Value::String(file_text) => {
737                    // Write this file under the temp dir.
738                    fs_err::write(tempdir.path().join(entry_path), file_text)?;
739                    // Add this file as an entry in the `DirhashTree`.
740                    dirhash_tree.add_file(entry_path, blake3::hash(file_text.as_bytes()))?;
741                }
742                // a subdirectory
743                serde_json::Value::Object(input_subdir) => {
744                    // Create this directory under the temp dir.
745                    fs_err::create_dir(tempdir.path().join(entry_path))?;
746                    // Non-empty subdirs get added to the `DirhashTree` automatically when we
747                    // populate their contents, but there's no harm in calling `add_empty_dir` for
748                    // every directory.
749                    dirhash_tree.add_empty_dir(entry_path)?;
750                    // Recurse!
751                    walk_test_vector_input(input_subdir, dirhash_tree, tempdir, Some(entry_path))?;
752                }
753                _ => panic!("unexpected JSON type"),
754            }
755        }
756        Ok(())
757    }
758
759    #[derive(Debug, serde::Deserialize)]
760    struct JsonTestVector {
761        // A tree of directories and the files they contain.
762        input: serde_json::Map<String, serde_json::Value>,
763        // The hexadecimal dirhash of the input tree.
764        dirhash: String,
765    }
766
767    // `../test_vectors/test_vectors.json` contains a series of input trees and hashes, which is
768    // generated by `../test_vectors/generate.py`. The hashes come from an independent Python
769    // implementation in that script, so here we're testing *both* that we don't drift from the
770    // checked-in values, *and* that implementations in two different languages agree. This tests
771    // both `dirhash_path` and `DirhashTree`, so we're really testing that three different
772    // implementations agree.
773    #[tokio::test]
774    async fn test_vectors_json() -> anyhow::Result<()> {
775        let test_vectors: Vec<JsonTestVector> =
776            serde_json::from_str(include_str!("../test_vectors/test_vectors.json"))?;
777        for JsonTestVector { input, dirhash } in &test_vectors {
778            let mut tree = DirhashTree::new();
779            let tempdir = tempfile::tempdir()?;
780            // `walk_test_vector_input` populates both the `DirhashTree` and the temp dir on disk.
781            walk_test_vector_input(
782                input, &mut tree, &tempdir, None, /* the relative path starts empty */
783            )?;
784            assert_eq!(dirhash.as_str(), tree.hash().to_hex().as_str());
785            assert_eq!(
786                dirhash.as_str(),
787                dirhash_path(tempdir.path())?.to_hex().as_str(),
788            );
789        }
790        Ok(())
791    }
792}