Skip to content

Commit

Permalink
fix(remove_fully): Remove the key content when set remove_fully to …
Browse files Browse the repository at this point in the history
…`true`

Closes #61
  • Loading branch information
zkat authored and TheAwiteb committed Jan 27, 2024
1 parent 7a4b16e commit eff2967
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cacache"
version = "11.7.1"
version = "12.0.0"
authors = ["Kat Marchán <[email protected]>"]
edition = "2021"
description = "Content-addressable, key-value, high-performance, on-disk cache."
Expand Down
50 changes: 50 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use walkdir::WalkDir;

#[cfg(any(feature = "async-std", feature = "tokio"))]
use crate::async_lib::{AsyncBufReadExt, AsyncWriteExt};
use crate::content::path::content_path;
use crate::errors::{IoErrorExt, Result};
use crate::put::WriteOpts;

Expand Down Expand Up @@ -407,6 +408,11 @@ impl RemoveOpts {
if !self.remove_fully {
delete(cache.as_ref(), key.as_ref())
} else {
if let Some(meta) = crate::metadata_sync(cache.as_ref(), key.as_ref())? {
let content = content_path(cache.as_ref(), &meta.integrity);
fs::remove_file(&content)
.with_context(|| format!("Failed to remove content at {content:?}"))?;
}
let bucket = bucket_path(cache.as_ref(), key.as_ref());
fs::remove_file(&bucket)
.with_context(|| format!("Failed to remove bucket at {bucket:?}"))
Expand All @@ -422,6 +428,12 @@ impl RemoveOpts {
if !self.remove_fully {
delete_async(cache.as_ref(), key.as_ref()).await
} else {
if let Some(meta) = crate::metadata(cache.as_ref(), key.as_ref()).await? {
let content = content_path(cache.as_ref(), &meta.integrity);
crate::async_lib::remove_file(&content)
.await
.with_context(|| format!("Failed to remove content at {content:?}"))?;
}
let bucket = bucket_path(cache.as_ref(), key.as_ref());
crate::async_lib::remove_file(&bucket)
.await
Expand Down Expand Up @@ -535,6 +547,44 @@ mod tests {
assert_eq!(find(&dir, "hello").unwrap(), None);
}

#[test]
fn delete_fully() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let content = content_path(&dir, &"sha1-deadbeef".parse().unwrap());
fs::create_dir_all(content.parent().unwrap()).unwrap();
fs::write(content.as_path(), "hello").unwrap();
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
let time = 1_234_567;
insert(&dir, "hello", WriteOpts::new().integrity(sri).time(time)).unwrap();
RemoveOpts::new()
.remove_fully(true)
.remove_sync(&dir, "hello")
.unwrap();
assert_eq!(find(&dir, "hello").unwrap(), None);
assert!(!content.exists());
}

#[cfg(any(feature = "async-std", feature = "tokio"))]
#[async_test]
async fn delete_fully_async() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let content = content_path(&dir, &"sha1-deadbeef".parse().unwrap());
fs::create_dir_all(content.parent().unwrap()).unwrap();
fs::write(content.as_path(), "hello").unwrap();
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
let time = 1_234_567;
insert(&dir, "hello", WriteOpts::new().integrity(sri).time(time)).unwrap();
RemoveOpts::new()
.remove_fully(true)
.remove(&dir, "hello")
.await
.unwrap();
assert_eq!(find(&dir, "hello").unwrap(), None);
assert!(!content.exists());
}

#[test]
fn round_trip() {
let tmp = tempfile::tempdir().unwrap();
Expand Down

0 comments on commit eff2967

Please sign in to comment.