gix_index/file/
write.rs

1use gix_features::hash;
2
3use crate::{write, File, Version};
4
5/// The error produced by [`File::write()`].
6#[derive(Debug, thiserror::Error)]
7#[allow(missing_docs)]
8pub enum Error {
9    #[error(transparent)]
10    Io(#[from] std::io::Error),
11    #[error("Could not acquire lock for index file")]
12    AcquireLock(#[from] gix_lock::acquire::Error),
13    #[error("Could not commit lock for index file")]
14    CommitLock(#[from] gix_lock::commit::Error<gix_lock::File>),
15}
16
17impl File {
18    /// Write the index to `out` with `options`, to be readable by [`File::at()`], returning the version that was actually written
19    /// to retain all information of this index.
20    pub fn write_to(
21        &self,
22        mut out: impl std::io::Write,
23        options: write::Options,
24    ) -> std::io::Result<(Version, gix_hash::ObjectId)> {
25        let _span = gix_features::trace::detail!("gix_index::File::write_to()", skip_hash = options.skip_hash);
26        let (version, hash) = if options.skip_hash {
27            let out: &mut dyn std::io::Write = &mut out;
28            let version = self.state.write_to(out, options)?;
29            (version, self.state.object_hash.null())
30        } else {
31            let mut hasher = hash::Write::new(&mut out, self.state.object_hash);
32            let out: &mut dyn std::io::Write = &mut hasher;
33            let version = self.state.write_to(out, options)?;
34            (version, gix_hash::ObjectId::from(hasher.hash.digest()))
35        };
36        out.write_all(hash.as_slice())?;
37        Ok((version, hash))
38    }
39
40    /// Write ourselves to the path we were read from after acquiring a lock, using `options`.
41    ///
42    /// Note that the hash produced will be stored which is why we need to be mutable.
43    pub fn write(&mut self, options: write::Options) -> Result<(), Error> {
44        let _span = gix_features::trace::detail!("gix_index::File::write()", path = ?self.path);
45        let mut lock = std::io::BufWriter::with_capacity(
46            64 * 1024,
47            gix_lock::File::acquire_to_update_resource(&self.path, gix_lock::acquire::Fail::Immediately, None)?,
48        );
49        let (version, digest) = self.write_to(&mut lock, options)?;
50        match lock.into_inner() {
51            Ok(lock) => lock.commit()?,
52            Err(err) => return Err(err.into_error().into()),
53        };
54        self.state.version = version;
55        self.checksum = Some(digest);
56        Ok(())
57    }
58}