Skip to content

Commit 761d715

Browse files
authored
fix(remove_fully): Remove the key content when set remove_fully to true (#63)
Fixes: #61 BREAKING CHANGE: this is technically a fix, but maybe people didn't expect this.
1 parent ffa1ab7 commit 761d715

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/index.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use walkdir::WalkDir;
2020

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

@@ -407,6 +408,11 @@ impl RemoveOpts {
407408
if !self.remove_fully {
408409
delete(cache.as_ref(), key.as_ref())
409410
} else {
411+
if let Some(meta) = crate::metadata_sync(cache.as_ref(), key.as_ref())? {
412+
let content = content_path(cache.as_ref(), &meta.integrity);
413+
fs::remove_file(&content)
414+
.with_context(|| format!("Failed to remove content at {content:?}"))?;
415+
}
410416
let bucket = bucket_path(cache.as_ref(), key.as_ref());
411417
fs::remove_file(&bucket)
412418
.with_context(|| format!("Failed to remove bucket at {bucket:?}"))
@@ -423,6 +429,12 @@ impl RemoveOpts {
423429
if !self.remove_fully {
424430
delete_async(cache.as_ref(), key.as_ref()).await
425431
} else {
432+
if let Some(meta) = crate::metadata(cache.as_ref(), key.as_ref()).await? {
433+
let content = content_path(cache.as_ref(), &meta.integrity);
434+
crate::async_lib::remove_file(&content)
435+
.await
436+
.with_context(|| format!("Failed to remove content at {content:?}"))?;
437+
}
426438
let bucket = bucket_path(cache.as_ref(), key.as_ref());
427439
crate::async_lib::remove_file(&bucket)
428440
.await
@@ -536,6 +548,44 @@ mod tests {
536548
assert_eq!(find(&dir, "hello").unwrap(), None);
537549
}
538550

551+
#[test]
552+
fn delete_fully() {
553+
let tmp = tempfile::tempdir().unwrap();
554+
let dir = tmp.path().to_owned();
555+
let content = content_path(&dir, &"sha1-deadbeef".parse().unwrap());
556+
fs::create_dir_all(content.parent().unwrap()).unwrap();
557+
fs::write(content.as_path(), "hello").unwrap();
558+
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
559+
let time = 1_234_567;
560+
insert(&dir, "hello", WriteOpts::new().integrity(sri).time(time)).unwrap();
561+
RemoveOpts::new()
562+
.remove_fully(true)
563+
.remove_sync(&dir, "hello")
564+
.unwrap();
565+
assert_eq!(find(&dir, "hello").unwrap(), None);
566+
assert!(!content.exists());
567+
}
568+
569+
#[cfg(any(feature = "async-std", feature = "tokio"))]
570+
#[async_test]
571+
async fn delete_fully_async() {
572+
let tmp = tempfile::tempdir().unwrap();
573+
let dir = tmp.path().to_owned();
574+
let content = content_path(&dir, &"sha1-deadbeef".parse().unwrap());
575+
fs::create_dir_all(content.parent().unwrap()).unwrap();
576+
fs::write(content.as_path(), "hello").unwrap();
577+
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
578+
let time = 1_234_567;
579+
insert(&dir, "hello", WriteOpts::new().integrity(sri).time(time)).unwrap();
580+
RemoveOpts::new()
581+
.remove_fully(true)
582+
.remove(&dir, "hello")
583+
.await
584+
.unwrap();
585+
assert_eq!(find(&dir, "hello").unwrap(), None);
586+
assert!(!content.exists());
587+
}
588+
539589
#[test]
540590
fn round_trip() {
541591
let tmp = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)