Simplify FileOperation by splitting into BasicFileOperation and bool reopen

This commit is contained in:
Chris Hennick 2023-05-13 15:49:54 -07:00
parent ff4dee28d7
commit 089f194fd6
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74

View file

@ -13,63 +13,53 @@ pub struct File {
} }
#[derive(Arbitrary,Debug)] #[derive(Arbitrary,Debug)]
pub enum FileOperation { pub enum BasicFileOperation {
WriteNormalFile { WriteNormalFile {
file: File, file: File,
options: zip_next::write::FileOptions, options: zip_next::write::FileOptions,
reopen: bool,
}, },
WriteDirectory { WriteDirectory {
name: String, name: String,
options: zip_next::write::FileOptions, options: zip_next::write::FileOptions,
reopen: bool,
}, },
WriteSymlink { WriteSymlink {
name: String, name: String,
target: Box<PathBuf>, target: Box<PathBuf>,
options: zip_next::write::FileOptions, options: zip_next::write::FileOptions,
reopen: bool,
}, },
ShallowCopy { ShallowCopy {
base: Box<FileOperation>, base: Box<FileOperation>,
new_name: String, new_name: String,
reopen: bool,
}, },
DeepCopy { DeepCopy {
base: Box<FileOperation>, base: Box<FileOperation>,
new_name: String, new_name: String,
reopen: bool,
} }
} }
#[derive(Arbitrary,Debug)]
pub struct FileOperation {
basic: BasicFileOperation,
reopen: bool,
}
impl FileOperation { impl FileOperation {
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
match self { match &self.basic {
FileOperation::WriteNormalFile {file, ..} => &file.name, BasicFileOperation::WriteNormalFile {file, ..} => &file.name,
FileOperation::WriteDirectory {name, ..} => name, BasicFileOperation::WriteDirectory {name, ..} => name,
FileOperation::WriteSymlink {name, ..} => name, BasicFileOperation::WriteSymlink {name, ..} => name,
FileOperation::ShallowCopy {new_name, ..} => new_name, BasicFileOperation::ShallowCopy {new_name, ..} => new_name,
FileOperation::DeepCopy {new_name, ..} => new_name BasicFileOperation::DeepCopy {new_name, ..} => new_name
}.to_owned() }.to_owned()
} }
pub fn should_reopen(&self) -> bool {
match self {
FileOperation::WriteNormalFile {reopen, ..} => *reopen,
FileOperation::ShallowCopy {reopen, ..} => *reopen,
FileOperation::DeepCopy {reopen, ..} => *reopen,
FileOperation::WriteDirectory {reopen, ..} => *reopen,
FileOperation::WriteSymlink {reopen, ..} => *reopen,
}
}
} }
fn do_operation<T>(writer: &mut RefCell<zip_next::ZipWriter<T>>, fn do_operation<T>(writer: &mut RefCell<zip_next::ZipWriter<T>>,
operation: FileOperation) -> Result<(), Box<dyn std::error::Error>> operation: FileOperation) -> Result<(), Box<dyn std::error::Error>>
where T: Read + Write + Seek { where T: Read + Write + Seek {
let should_reopen = operation.should_reopen(); match operation.basic {
match operation { BasicFileOperation::WriteNormalFile {file, mut options, ..} => {
FileOperation::WriteNormalFile {file, mut options, ..} => {
if file.contents.iter().map(Vec::len).sum::<usize>() >= u32::MAX as usize { if file.contents.iter().map(Vec::len).sum::<usize>() >= u32::MAX as usize {
options = options.large_file(true); options = options.large_file(true);
} }
@ -78,24 +68,24 @@ fn do_operation<T>(writer: &mut RefCell<zip_next::ZipWriter<T>>,
writer.borrow_mut().write_all(chunk.as_slice())?; writer.borrow_mut().write_all(chunk.as_slice())?;
} }
} }
FileOperation::WriteDirectory {name, options, ..} => { BasicFileOperation::WriteDirectory {name, options, ..} => {
writer.borrow_mut().add_directory(name, options)?; writer.borrow_mut().add_directory(name, options)?;
} }
FileOperation::WriteSymlink {name, target, options, ..} => { BasicFileOperation::WriteSymlink {name, target, options, ..} => {
writer.borrow_mut().add_symlink(name, target.to_string_lossy(), options)?; writer.borrow_mut().add_symlink(name, target.to_string_lossy(), options)?;
} }
FileOperation::ShallowCopy {base, ref new_name, .. } => { BasicFileOperation::ShallowCopy {base, ref new_name, .. } => {
let base_name = base.get_name(); let base_name = base.get_name();
do_operation(writer, *base)?; do_operation(writer, *base)?;
writer.borrow_mut().shallow_copy_file(&base_name, new_name)?; writer.borrow_mut().shallow_copy_file(&base_name, new_name)?;
} }
FileOperation::DeepCopy {base, ref new_name, .. } => { BasicFileOperation::DeepCopy {base, ref new_name, .. } => {
let base_name = base.get_name(); let base_name = base.get_name();
do_operation(writer, *base)?; do_operation(writer, *base)?;
writer.borrow_mut().deep_copy_file(&base_name, new_name)?; writer.borrow_mut().deep_copy_file(&base_name, new_name)?;
} }
} }
if should_reopen { if operation.reopen {
let new_writer = zip_next::ZipWriter::new_append(writer.borrow_mut().finish().unwrap()).unwrap(); let new_writer = zip_next::ZipWriter::new_append(writer.borrow_mut().finish().unwrap()).unwrap();
*writer = new_writer.into(); *writer = new_writer.into();
} }