Skip to content

Commit

Permalink
Skip output file when it exists within the input path
Browse files Browse the repository at this point in the history
  • Loading branch information
Systemcluster committed Nov 21, 2023
1 parent bf48339 commit 7bca05c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/compress.rs
Expand Up @@ -66,14 +66,16 @@ pub fn copy_encode<R: Read, W: Write>(
pub fn compress<
T: AsRef<Path>,
W: Write + Seek + Sync + Send,
X: AsRef<Path>,
P: Fn() + Sync + Send,
E: Fn(&str) + Sync + Send,
I: Fn(&str) + Sync + Send,
>(
source: T, target: &mut W, compression: u32, progress_callback: P, error_callback: E,
info_callback: I,
source: T, target: &mut W, exclude: X, compression: u32, progress_callback: P,
error_callback: E, info_callback: I,
) -> (u64, u64, u64) {
let source: &Path = source.as_ref();
let exclude: &Path = exclude.as_ref();

let num_cpus = num_cpus::get() as u64;
let system = System::new_with_specifics(sysinfo::RefreshKind::new().with_memory());
Expand Down Expand Up @@ -112,6 +114,10 @@ pub fn compress<
return None;
}
let entry = entry.path();
if entry == exclude {
error_callback(&format!("skipping excluded file: {}", entry.display()));
return None;
}
let entry = entry.strip_prefix(source).ok()?;

if entry.file_name()?.len() > NAME_SIZE {
Expand Down Expand Up @@ -170,6 +176,10 @@ pub fn compress<
return None;
}
let entry = entry.path();
if entry == exclude {
error_callback(&format!("skipping excluded file: {}", entry.display()));
return None;
}

if entry.file_name()?.len() > NAME_SIZE {
error_callback(&format!(
Expand Down Expand Up @@ -359,6 +369,10 @@ pub fn compress<
return None;
}
let entry = entry.path();
if entry == exclude {
error_callback(&format!("skipping excluded file: {}", entry.display()));
return None;
}

if entry.file_name()?.len() > NAME_SIZE {
error_callback(&format!(
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Expand Up @@ -100,6 +100,14 @@ fn main() {

let mut show_console = get_show_console(&args.console, runner_name);

if output == source {
println!(
"{}: {}",
style("output file can't be the input file").red(),
output.display()
);
std::process::exit(-1);
}
let file = File::create(&output).unwrap_or_else(|_| {
println!(
"{}: {}",
Expand Down Expand Up @@ -226,13 +234,14 @@ fn main() {
let (compressed, read, written) = compress(
&source,
&mut writer,
&output,
args.compression,
|| {
bar_progress.inc(1);
},
|message| {
bar_progress.inc(1);
bar_progress.println(format!(" {}{}", Emoji("⚠ ", ""), style(message).red()));
bar_progress.println(format!(" {}{}", Emoji("⚠ ", ""), style(message).red()));
},
|message| {
bar_progress.set_message(format!("{}", style(message).blue().bright()));
Expand Down Expand Up @@ -304,7 +313,7 @@ fn main() {
set_permissions(&output, PermissionsExt::from_mode(mode | 0o111)).unwrap_or_else(|e| {
eprintln!(
" {} failed to set permissions for {}: {}",
Emoji("⚠ ", ""),
Emoji("⚠ ", ""),
output.display(),
e
)
Expand Down

0 comments on commit 7bca05c

Please sign in to comment.