cacache_sync/
rm.rs

1//! Functions for removing things from the cache.
2use std::fs;
3use std::path::Path;
4
5use ssri::Integrity;
6
7use crate::content::rm;
8use crate::errors::{Internal, Result};
9use crate::index;
10
11/// Removes an individual index entry synchronously. The associated content
12/// will be left in the cache.
13///
14/// ## Example
15/// ```no_run
16/// use std::io::Read;
17///
18/// fn main() -> cacache_sync::Result<()> {
19///     let sri = cacache_sync::write("./my-cache", "my-key", b"hello")?;
20///
21///     cacache_sync::remove("./my-cache", "my-key")?;
22///
23///     // This fails:
24///     cacache_sync::read("./my-cache", "my-key")?;
25///
26///     // But this succeeds:
27///     cacache_sync::read_hash("./my-cache", &sri)?;
28///
29///     Ok(())
30/// }
31/// ```
32pub fn remove<P, K>(cache: P, key: K) -> Result<()>
33where
34    P: AsRef<Path>,
35    K: AsRef<str>,
36{
37    index::delete(cache.as_ref(), key.as_ref())
38}
39
40/// Removes an individual content entry synchronously. Any index entries
41/// pointing to this content will become invalidated.
42///
43/// ## Example
44/// ```no_run
45/// use std::io::Read;
46///
47/// fn main() -> cacache_sync::Result<()> {
48///     let sri = cacache_sync::write("./my-cache", "my-key", b"hello")?;
49///
50///     cacache_sync::remove_hash("./my-cache", &sri)?;
51///
52///     // These fail:
53///     cacache_sync::read("./my-cache", "my-key")?;
54///     cacache_sync::read_hash("./my-cache", &sri)?;
55///
56///     // But this succeeds:
57///     cacache_sync::metadata("./my-cache", "my-key")?;
58///
59///     Ok(())
60/// }
61/// ```
62pub fn remove_hash<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()> {
63    rm::rm(cache.as_ref(), sri)
64}
65
66/// Removes entire contents of the cache synchronously, including temporary
67/// files, the entry index, and all content data.
68///
69/// ## Example
70/// ```no_run
71/// use std::io::Read;
72///
73/// fn main() -> cacache_sync::Result<()> {
74///     let sri = cacache_sync::write("./my-cache", "my-key", b"hello")?;
75///
76///     cacache_sync::clear("./my-cache")?;
77///
78///     // These all fail:
79///     cacache_sync::read("./my-cache", "my-key")?;
80///     cacache_sync::read_hash("./my-cache", &sri)?;
81///     cacache_sync::metadata("./my-cache", "my-key")?;
82///
83///     Ok(())
84/// }
85/// ```
86pub fn clear<P: AsRef<Path>>(cache: P) -> Result<()> {
87    for entry in (cache.as_ref().read_dir().to_internal()?).flatten() {
88        fs::remove_dir_all(entry.path()).to_internal()?;
89    }
90    Ok(())
91}
92
93#[cfg(test)]
94mod tests {
95
96    #[test]
97    fn test_remove() {
98        let tmp = tempfile::tempdir().unwrap();
99        let dir = tmp.path().to_owned();
100        let sri = crate::write(&dir, "key", b"my-data").unwrap();
101
102        crate::remove(&dir, "key").unwrap();
103
104        let new_entry = crate::metadata(&dir, "key").unwrap();
105        assert!(new_entry.is_none());
106
107        let data_exists = crate::exists(&dir, &sri);
108        assert!(data_exists);
109    }
110
111    #[test]
112    fn test_remove_data() {
113        let tmp = tempfile::tempdir().unwrap();
114        let dir = tmp.path().to_owned();
115        let sri = crate::write(&dir, "key", b"my-data").unwrap();
116
117        crate::remove_hash(&dir, &sri).unwrap();
118
119        let entry = crate::metadata(&dir, "key").unwrap();
120        assert!(entry.is_some());
121
122        let data_exists = crate::exists(&dir, &sri);
123        assert!(!data_exists);
124    }
125
126    #[test]
127    fn test_clear() {
128        let tmp = tempfile::tempdir().unwrap();
129        let dir = tmp.path().to_owned();
130        let sri = crate::write(&dir, "key", b"my-data").unwrap();
131
132        crate::clear(&dir).unwrap();
133
134        let entry = crate::metadata(&dir, "key").unwrap();
135        assert_eq!(entry, None);
136
137        let data_exists = crate::exists(&dir, &sri);
138        assert!(!data_exists);
139    }
140}