Skip to content

Commit

Permalink
feat(links): add support for hard linking from the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 5, 2023
1 parent 80e5b41 commit 7e81626
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/content/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ pub async fn copy_async<'a>(cache: &'a Path, sri: &'a Integrity, to: &'a Path) -
Ok(size as u64)
}

pub fn hard_link_unchecked(cache: &Path, sri: &Integrity, to: &Path) -> Result<()> {
let cpath = path::content_path(cache, sri);
std::fs::hard_link(cpath, to).with_context(|| {
format!(
"Failed to link cache contents from {} to {}",
path::content_path(cache, sri).display(),
to.display()
)
})?;
Ok(())
}

pub fn has_content(cache: &Path, sri: &Integrity) -> Option<Integrity> {
if path::content_path(cache, sri).exists() {
Some(sri.clone())
Expand Down
32 changes: 32 additions & 0 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,38 @@ where
read::copy_unchecked(cache.as_ref(), sri, to.as_ref())
}

/// Hard links a cache entry by key to a specified location. The cache entry
/// contents will not be checked, and all the usual caveats of hard links
/// apply: The potentially-shared cache might be corrupted if the hard link is
/// modified.
pub fn hard_link_unchecked_sync<P, K, Q>(cache: P, key: K, to: Q) -> Result<()>
where
P: AsRef<Path>,
K: AsRef<str>,
Q: AsRef<Path>,
{
fn inner(cache: &Path, key: &str, to: &Path) -> Result<()> {
if let Some(entry) = index::find(cache, key)? {
hard_link_hash_unchecked_sync(cache, &entry.integrity, to)
} else {
Err(Error::EntryNotFound(cache.to_path_buf(), key.into()))
}
}
inner(cache.as_ref(), key.as_ref(), to.as_ref())
}

/// Hard links a cache entry by integrity address to a specified location. The
/// cache entry contents will not be checked, and all the usual caveats of
/// hard links apply: The potentially-shared cache might be corrupted if the
/// hard link is modified.
pub fn hard_link_hash_unchecked_sync<P, Q>(cache: P, sri: &Integrity, to: Q) -> Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
read::hard_link_unchecked(cache.as_ref(), sri, to.as_ref())
}

/// Gets metadata for a certain key.
///
/// Note that the existence of a metadata entry is not a guarantee that the
Expand Down

0 comments on commit 7e81626

Please sign in to comment.