Skip to content

Commit

Permalink
Also delete empty compressed files
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyBF committed Mar 9, 2022
1 parent 54f3c1e commit b433128
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions ext/src/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,50 +204,53 @@ impl<T: Read> std::ops::Drop for ChecksumReader<T> {

/// Open the file pointed to by `path` as a `Box<dyn Read>`. If the file does not exist, look for
/// compressed versions.
// When compiling to wasm we don't mutate path
#[allow(unused_mut)]
fn open_file(mut path: PathBuf) -> Option<Box<dyn Read>> {
// We should try in decreasing order of access speed.
match File::open(&path) {
Ok(f) => {
let mut reader = BufReader::new(f);
if reader
.fill_buf()
.unwrap_or_else(|e| panic!("Error when reading from {path:?}: {e}"))
.is_empty()
{
// The file is empty. Delete the file and proceed as if it didn't exist
std::fs::remove_file(&path)
.unwrap_or_else(|e| panic!("Error when deleting empty file {path:?}: {e}"));
return None;
}
return Some(Box::new(ChecksumReader::new(reader)));
}
Err(e) => {
if e.kind() != ErrorKind::NotFound {
panic!("Error when opening {path:?}: {e}");
}
}
}

#[cfg(feature = "zstd")]
fn open_file(path: PathBuf) -> Option<Box<dyn Read>> {
fn try_open<F>(path: &Path, wrapper: F) -> Option<Box<dyn Read>>
where
F: FnOnce(BufReader<File>) -> Box<dyn Read>,
{
path.set_extension("zst");
// We should try in decreasing order of access speed.
match File::open(&path) {
Ok(f) => {
return Some(Box::new(ChecksumReader::new(
zstd::stream::Decoder::new(f).unwrap(),
)))
let mut reader = BufReader::new(f);
if reader
.fill_buf()
.unwrap_or_else(|e| panic!("Error when reading from {path:?}: {e}"))
.is_empty()
{
// The file is empty. Delete the file and proceed as if it didn't exist
std::fs::remove_file(&path)
.unwrap_or_else(|e| panic!("Error when deleting empty file {path:?}: {e}"));
None
} else {
Some(wrapper(reader))
}
}
Err(e) => {
if e.kind() != ErrorKind::NotFound {
panic!("Error when opening {path:?}");
panic!("Error when opening {path:?}: {e}");
}
None
}
}
}

None
#[cfg(feature = "zstd")]
let try_zstd = || {
let mut path = path.clone();
path.set_extension("zst");
try_open(&path, |f| {
Box::new(ChecksumReader::new(
zstd::stream::Decoder::new(f).unwrap_or_else(|e| {
panic!("Failed to create zstd stream for file {path:?}: {e}")
}),
))
})
};
#[cfg(not(feature = "zstd"))]
let try_zstd = || None;

try_open(&path, |f| Box::new(ChecksumReader::new(f))).or_else(try_zstd)
}

pub struct SaveFile<A: Algebra> {
Expand Down

0 comments on commit b433128

Please sign in to comment.