use std::{fs::{File, self}, io::{self, Read, BufReader, BufWriter, Seek, Write}, error::Error, fmt, thread, path::{PathBuf, Path}};
use indicatif::ProgressBar;
use zip::{ZipArchive, write::FileOptions, ZipWriter};
use crossbeam_channel::{bounded, Sender, Receiver};
use crate::{minify::{only_recompress, MinifyType}, blacklist, fop::{FileOp, pack_file}, errors::ErrorCollector};
pub fn optimize_archive(
in_path: PathBuf,
out_path: PathBuf,
pb: ProgressBar,
errors: &mut dyn ErrorCollector,
file_opts: &FileOptions,
use_blacklist: bool
) -> io::Result<i64> {
let (tx, rx) = bounded(2);
let t1 = thread::spawn(move || {
let fin = File::open(in_path)?;
read_archive_entries(fin, tx, use_blacklist)
});
let fout = File::create(out_path)?;
let rsum = save_archive_entries(fout, rx, file_opts, errors, pb);
t1.join().unwrap()?;
rsum
}
pub fn optimize_fs_copy(
in_path: PathBuf,
out_path: PathBuf,
pb: ProgressBar,
errors: &mut dyn ErrorCollector,
use_blacklist: bool
) -> io::Result<i64> {
if in_path == out_path {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "The paths are the same"))
}
let (tx, rx) = bounded(2);
let t1 = thread::spawn(move || {
read_fs_entries(&in_path, tx, use_blacklist)
});
let rsum = save_fs_entries(&out_path, rx, errors, pb);
t1.join().unwrap()?;
rsum
}
pub fn save_archive_entries<W: Write + Seek>(
w: W,
rx: Receiver<EntryType>,
file_opts: &FileOptions,
ev: &mut dyn ErrorCollector,
pb: ProgressBar
) -> io::Result<i64> {
let mut dsum = 0;
let mut zw = ZipWriter::new(BufWriter::new(w));
let mut cnt = 0;
let mut cv = Vec::new();
for et in rx {
match et {
EntryType::Count(u) => {
pb.set_length(u);
}
EntryType::Directory(d) => {
if d != ".cache/" {
zw.add_directory(d, file_opts.clone())?;
}
}
EntryType::File(fname, buf, fop) => {
use FileOp::*;
cnt += 1;
pb.set_position(cnt);
pb.set_message(fname.clone());
match fop {
Recompress(cmin) => {
pack_file(
&mut zw,
&fname,
file_opts,
&buf,
cmin
)?;
}
Minify(m) => {
let fsz = buf.len() as i64;
let buf = match m.minify(&buf, &mut cv) {
Ok(()) => &cv,
Err(e) => {
ev.collect(fname.to_string(), e);
&buf
}
};
dsum -= (buf.len() as i64) - fsz;
pack_file(&mut zw, &fname, file_opts, &buf, m.compress_min())?;
cv.clear();
}
Ignore => {
ev.collect(fname.to_string(), Box::new(blacklist::BlacklistedFile));
}
Warn(x) => {
ev.collect(fname.to_string(), Box::new(StrError(x)));
pack_file(&mut zw, &fname, file_opts, &buf, 0)?;
}
Signfile => {
ev.collect(fname.to_string(), Box::new(StrError(ERR_SIGNFILE.to_string())));
}
}
}
}
}
pb.finish_with_message("Saving...");
zw.finish()?;
Ok(dsum)
}
fn check_file_by_name(fname: &str, use_blacklist: bool) -> FileOp {
use FileOp::*;
if fname.starts_with(".cache/") { return Ignore }
if fname.starts_with("META-INF/") {
let sub = &fname[9..];
match sub {
"MANIFEST.MF" => {return Recompress(64) }
"SIGNFILE.SF" | "SIGNFILE.DSA" => { return Signfile }
x if x.starts_with("SIG-") || [".DSA", ".RSA", ".SF"].into_iter().any(|e| x.ends_with(e)) => {
return Signfile
}
x if x.starts_with("services/") => { return Recompress(64) }
_ => {}
}
}
let ftype = fname.rsplit_once('.').unzip().1.unwrap_or("");
if ftype == "class" {
return Recompress(64)
}
if only_recompress(ftype) {
return Recompress(4)
}
if let Some(x) = MinifyType::by_extension(ftype) {
return Minify(x)
}
if use_blacklist && blacklist::can_ignore_type(ftype) { Ignore } else { Recompress(2) }
}
pub fn read_archive_entries<R: Read + Seek>(
r: R,
tx: Sender<EntryType>,
use_blacklist: bool
) -> io::Result<()> {
let mut za = ZipArchive::new(BufReader::new(r))?;
let jfc = za.len() as u64;
tx.send(EntryType::Count(jfc)).unwrap();
for i in 0..jfc {
let mut jf = za.by_index(i as usize)?;
let fname = jf.name().to_string();
tx.send(if fname.ends_with('/') {
EntryType::Directory(fname)
} else {
let fop = check_file_by_name(&fname, use_blacklist);
let mut obuf = Vec::new();
match fop {
FileOp::Ignore => {}
_ => {
obuf.reserve_exact(jf.size() as usize);
jf.read_to_end(&mut obuf)?;
}
}
EntryType::File(fname, obuf, fop)
}).unwrap();
}
Ok(())
}
pub fn save_fs_entries(
dest_dir: &Path,
rx: Receiver<EntryType>,
ev: &mut dyn ErrorCollector,
pb: ProgressBar
) -> io::Result<i64> {
let mut dsum = 0;
let mut cnt = 0;
let mut cv = Vec::new();
for et in rx {
match et {
EntryType::Count(u) => {
pb.set_length(u);
}
EntryType::Directory(dir) => {
let mut dp = PathBuf::from(dest_dir);
dp.push(dir);
fs::create_dir(dp)?;
}
EntryType::File(fname, buf, fop) => {
use FileOp::*;
cnt += 1;
pb.set_position(cnt);
pb.set_message(fname.clone());
let mut fp = PathBuf::from(dest_dir);
fp.push(fname.clone());
match fop {
Recompress(_) => {
fs::write(fp, buf)?;
}
Minify(m) => {
let fsz = buf.len() as i64;
let buf = match m.minify(&buf, &mut cv) {
Ok(()) => &cv,
Err(e) => {
ev.collect(fname.to_string(), e);
&buf
}
};
dsum -= (buf.len() as i64) - fsz;
fs::write(fp, buf)?;
cv.clear();
}
Ignore => {
ev.collect(fname, Box::new(blacklist::BlacklistedFile));
}
Warn(x) => {
ev.collect(fname, Box::new(StrError(x)));
fs::write(fp, buf)?;
}
Signfile => {
ev.collect(fname, Box::new(StrError(ERR_SIGNFILE.to_string())));
}
}
}
}
}
Ok(dsum)
}
pub fn read_fs_entries(
src_dir: &Path,
tx: Sender<EntryType>,
use_blacklist: bool
) -> io::Result<()> {
let mut vdir = Vec::new();
vdir.push(src_dir.to_owned());
while let Some(px) = vdir.pop() {
let rd = fs::read_dir(px)?.collect::<Result<Vec<_>, _>>()?;
tx.send(EntryType::Count(rd.len() as u64)).unwrap();
for de in rd {
let meta = de.metadata()?;
if meta.is_dir() {
let dp = de.path();
let dname = dp.strip_prefix(src_dir).expect("Subdir not in source dir")
.to_string_lossy().to_string();
vdir.push(dp);
tx.send(EntryType::Directory(dname)).unwrap();
} else if meta.is_file() {
let fp = de.path();
let fname = fp.strip_prefix(src_dir).expect("File not in source dir")
.to_string_lossy().to_string();
let fop = check_file_by_name(&fname, use_blacklist);
let ff = fs::read(fp)?;
tx.send(EntryType::File(fname, ff, fop)).unwrap();
}
}
}
Ok(())
}
pub enum EntryType {
Count(u64),
Directory(String),
File(String, Vec<u8>, FileOp)
}
#[derive(Debug)]
pub(crate) struct StrError(pub String);
impl Error for StrError {}
impl fmt::Display for StrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
const ERR_SIGNFILE: &str = "This file cannot be repacked since it contains SHA-256 digests for zipped entries";