Garbage collect expired history files
This commit is contained in:
parent
05517f64d3
commit
b984999120
3 changed files with 47 additions and 0 deletions
|
@ -2,6 +2,8 @@
|
|||
This tool has some additional compilation requirements besides the Rust
|
||||
toolchain.
|
||||
|
||||
- Rust 1.20 or above (verify this)
|
||||
|
||||
## Requirements
|
||||
- OpenSSL development files
|
||||
- Ubuntu package: `libssl-dev`
|
||||
|
|
|
@ -157,6 +157,17 @@ impl RemoteFile {
|
|||
&self.id
|
||||
}
|
||||
|
||||
/// Check whether this file has expired, based on it's expiry property.
|
||||
///
|
||||
/// If no expiry time is set (known) for this file,
|
||||
/// the `def` value is returned instead.
|
||||
pub fn has_expired(&self, def: bool) -> bool {
|
||||
match self.expire_at {
|
||||
Some(time) => time < Utc::now(),
|
||||
None => def,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the file URL, provided by the server.
|
||||
pub fn url(&self) -> &Url {
|
||||
&self.url
|
||||
|
|
|
@ -50,6 +50,9 @@ impl History {
|
|||
let mut history: Self = toml::from_str(&data)?;
|
||||
history.autosave = Some(path);
|
||||
|
||||
// Garbage collect
|
||||
history.gc();
|
||||
|
||||
Ok(history)
|
||||
}
|
||||
|
||||
|
@ -67,6 +70,9 @@ impl History {
|
|||
|
||||
/// Save the history to the internal autosave file.
|
||||
pub fn save(&mut self) -> Result<(), SaveError> {
|
||||
// Garbage collect
|
||||
self.gc();
|
||||
|
||||
// Get the path
|
||||
let path = self.autosave
|
||||
.as_ref()
|
||||
|
@ -123,6 +129,34 @@ impl History {
|
|||
pub fn files(&self) -> &Vec<RemoteFile> {
|
||||
&self.files
|
||||
}
|
||||
|
||||
/// Garbage collect (remove) all files that have been expired,
|
||||
/// as defined by their `expire_at` property.
|
||||
///
|
||||
/// If the expiry property is None (thus unknown), the file will be kept.
|
||||
///
|
||||
/// The number of exired files is returned.
|
||||
pub fn gc(&mut self) -> usize {
|
||||
// Get the indices of files that have expired
|
||||
let expired_indices: Vec<usize> = self.files.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| f.has_expired(false))
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
|
||||
// Remove these specific files
|
||||
for i in &expired_indices {
|
||||
self.files.remove(*i);
|
||||
}
|
||||
|
||||
// Set the changed flag
|
||||
if !expired_indices.is_empty() {
|
||||
self.changed = true;
|
||||
}
|
||||
|
||||
// Return the number of expired files
|
||||
expired_indices.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for History {
|
||||
|
|
Loading…
Reference in a new issue