use crate::shell::{CommandOutput, Shell};
use std::io::{Read, Write};
impl Shell {
pub fn cmd_gzip(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
let mut to_stdout = false;
let mut decompress = false;
let mut keep = false;
let mut level = flate2::Compression::default();
let mut files = Vec::new();
for arg in args {
match *arg {
"--stdout" => to_stdout = true,
"--decompress" => decompress = true,
"--keep" => keep = true,
_ if arg.starts_with("--") => {}
_ if arg.starts_with('-') && arg.len() > 1 => {
for ch in arg[1..].chars() {
match ch {
'c' => to_stdout = true,
'd' => decompress = true,
'k' => keep = true,
'1'..='9' => level = flate2::Compression::new(ch.to_digit(10).unwrap()),
_ => {}
}
}
}
_ => files.push(arg.to_string()),
}
}
if files.is_empty() {
let input_bytes = match self
.take_binary_in()
.or_else(|| stdin.map(|s| s.as_bytes().to_vec()))
{
Some(b) => b,
None => return CommandOutput::error("gzip: missing file operand\n".to_string(), 1),
};
let result_bytes = if decompress {
let mut decoder = flate2::read::GzDecoder::new(&input_bytes[..]);
let mut d = Vec::new();
if let Err(e) = decoder.read_to_end(&mut d) {
return CommandOutput::error(format!("gzip: {}\n", e), 1);
}
d
} else {
let mut enc = flate2::write::GzEncoder::new(Vec::new(), level);
if let Err(e) = enc.write_all(&input_bytes) {
return CommandOutput::error(format!("gzip: {}\n", e), 1);
}
match enc.finish() {
Ok(c) => c,
Err(e) => return CommandOutput::error(format!("gzip: {}\n", e), 1),
}
};
if !result_bytes.is_empty() {
self.set_binary_out(result_bytes.clone());
}
return CommandOutput::success(String::from_utf8_lossy(&result_bytes).to_string());
}
let mut output_bytes = Vec::new();
for file in &files {
let input_bytes = match self.vfs.read(file, &self.cwd) {
Ok(b) => b,
Err(e) => {
return CommandOutput::error(format!("gzip: {}: {}\n", file, e), 1);
}
};
let result_bytes = if decompress {
let mut decoder = flate2::read::GzDecoder::new(&input_bytes[..]);
let mut decompressed = Vec::new();
if let Err(e) = decoder.read_to_end(&mut decompressed) {
return CommandOutput::error(format!("gzip: {}: {}\n", file, e), 1);
}
decompressed
} else {
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), level);
if let Err(e) = encoder.write_all(&input_bytes) {
return CommandOutput::error(format!("gzip: {}: {}\n", file, e), 1);
}
match encoder.finish() {
Ok(c) => c,
Err(e) => {
return CommandOutput::error(format!("gzip: {}: {}\n", file, e), 1);
}
}
};
if to_stdout {
output_bytes.extend_from_slice(&result_bytes);
} else {
let out_name = if decompress {
file.strip_suffix(".gz")
.or_else(|| file.strip_suffix(".GZ"))
.unwrap_or(file)
.to_string()
} else {
format!("{}.gz", file)
};
if out_name.is_empty() {
return CommandOutput::error(format!("gzip: {}: empty output name\n", file), 1);
}
if let Err(e) = self.vfs.write_bytes(&out_name, &self.cwd, &result_bytes) {
return CommandOutput::error(format!("gzip: {}: {}\n", out_name, e), 1);
}
if !keep {
let _ = self.vfs.remove_file(file, &self.cwd);
}
}
}
if to_stdout {
if !output_bytes.is_empty() {
self.set_binary_out(output_bytes.clone());
}
CommandOutput::success(String::from_utf8_lossy(&output_bytes).to_string())
} else {
CommandOutput::success(String::new())
}
}
}