Implement garbage collect on the server

This commit is contained in:
2019-05-08 16:34:55 +02:00
parent 195b352cd2
commit 77cecebe52
2 changed files with 57 additions and 19 deletions

View File

@ -182,11 +182,47 @@ impl DataStore {
self.textures.iter()
}
pub fn garbage_collect(&mut self) -> io::Result<()> {
//unimplemented!()
pub fn extract_hash(filename: &std::ffi::OsStr) -> Option<Sha256> {
// directly return None for invalidly encoded file names
let str_name = filename.to_str()?;
let hash = Sha256::from_hex(str_name)?;
/// VERY TODO:
eprintln!("WARNING: We are sorry the GC isn't implemented yet :'( ");
// check back to ignore names with lowercase letters
if hash.as_hex_string() == str_name {
Some(hash)
} else {
None
}
}
pub fn garbage_collect(&mut self) -> io::Result<()> {
let texture_dir = std::fs::read_dir(self.texture_base_path())?;
let mut hashs_on_disk = HashSet::new();
for result_direntry in texture_dir {
let texture_path = result_direntry?.path();
let filename = match texture_path.file_name() {
Some(name) => name,
None => continue,
};
match Self::extract_hash(filename) {
Some(hash) => {
hashs_on_disk.insert(hash);
}
None => (), // ignore other files
};
}
let mut unused_files = hashs_on_disk;
for texture in &self.textures {
unused_files.remove(&texture.texture_hash);
}
// remove what is still contained in the HashSet
for entry in unused_files {
let path = self.texture_file_path(&entry);
std::fs::remove_file(path)?;
}
Ok(())
}
}