#![allow(unused)]
use std::path::{Path, PathBuf};
use memmap2::Mmap;
use crate::{decode, extension, File, State};
mod error {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("An IO error occurred while opening the index")]
Io(#[from] std::io::Error),
#[error(transparent)]
Decode(#[from] crate::decode::Error),
#[error(transparent)]
LinkExtension(#[from] crate::extension::link::decode::Error),
}
}
pub use error::Error;
impl File {
pub fn at_or_default(
path: impl Into<PathBuf>,
object_hash: gix_hash::Kind,
options: decode::Options,
) -> Result<Self, Error> {
let path = path.into();
Ok(match Self::at(&path, object_hash, options) {
Ok(f) => f,
Err(Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {
File::from_state(State::new(object_hash), path)
}
Err(err) => return Err(err),
})
}
pub fn at(path: impl Into<PathBuf>, object_hash: gix_hash::Kind, options: decode::Options) -> Result<Self, Error> {
let _span = gix_features::trace::detail!("gix_index::File::at()");
let path = path.into();
let (data, mtime) = {
let file = std::fs::File::open(&path)?;
#[allow(unsafe_code)]
let data = unsafe { Mmap::map(&file)? };
(data, filetime::FileTime::from_last_modification_time(&file.metadata()?))
};
let (state, checksum) = State::from_bytes(&data, mtime, object_hash, options)?;
let mut file = File {
state,
path,
checksum: Some(checksum),
};
if let Some(mut link) = file.link.take() {
link.dissolve_into(&mut file, object_hash, options)?;
}
Ok(file)
}
pub fn from_state(state: crate::State, path: impl Into<PathBuf>) -> Self {
File {
state,
path: path.into(),
checksum: None,
}
}
}