Skip to content

Commit

Permalink
fix: correctly include skipped files in the final statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Jan 6, 2025
1 parent e46805e commit 5b26a4f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
19 changes: 19 additions & 0 deletions crates/applesauce/src/info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{cstr_from_bytes_until_null, vol_supports_compression_cap, xattr};
use applesauce_core::{decmpfs, round_to_block_size};
use std::ffi::{CStr, CString};
use std::fmt;
use std::fs::Metadata;
use std::io;
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -46,6 +47,24 @@ pub enum IncompressibleReason {
HasRequiredXattr,
}

impl fmt::Display for IncompressibleReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IncompressibleReason::Empty => write!(f, "empty file"),
IncompressibleReason::TooLarge(size) => {
write!(f, "file too large to compress: {} bytes", size)
}
IncompressibleReason::IoError(e) => e.fmt(f),
IncompressibleReason::FsNotSupported => {
write!(f, "filesystem does not support compression")
}
IncompressibleReason::HasRequiredXattr => {
write!(f, "file has a required xattr for compression already")
}
}
}
}

pub enum FileCompressionState {
Compressed,
Compressible,
Expand Down
1 change: 1 addition & 0 deletions crates/applesauce/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![warn(clippy::debug_assert_with_mut_call)]
#![warn(clippy::filetype_is_file)]
#![warn(clippy::match_same_arms)]
extern crate core;

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
compile_error!("applesauce only works on macos/ios");
Expand Down
13 changes: 13 additions & 0 deletions crates/applesauce/src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::info::IncompressibleReason;
use std::path::Path;
use std::{fmt, io};

Expand All @@ -14,6 +15,18 @@ pub enum SkipReason {
FsNotSupported,
}

impl From<IncompressibleReason> for SkipReason {
fn from(reason: IncompressibleReason) -> SkipReason {
match reason {
IncompressibleReason::Empty => SkipReason::EmptyFile,
IncompressibleReason::TooLarge(size) => SkipReason::TooLarge(size),
IncompressibleReason::IoError(err) => SkipReason::ReadError(err),
IncompressibleReason::FsNotSupported => SkipReason::FsNotSupported,
IncompressibleReason::HasRequiredXattr => SkipReason::HasRequiredXattr,
}
}
}

pub trait Progress {
type Task: Task;

Expand Down
81 changes: 51 additions & 30 deletions crates/applesauce/src/threads/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::info::FileCompressionState;
use crate::info::{FileCompressionState, IncompressibleReason};
use crate::progress::{self, Progress, SkipReason};
use crate::tmpdir_paths::TmpdirPaths;
use crate::{info, scan, times, Stats};
Expand Down Expand Up @@ -178,41 +178,62 @@ impl BackgroundThreads {
return;
}
};
let file_info = info::get_file_info(&path, &metadata);
let mut file_info = info::get_file_info(&path, &metadata);
stats.add_start_file(&metadata, &file_info);

let send = match file_info.compression_state {
FileCompressionState::Compressed if !mode.is_compressing() => true,
FileCompressionState::Compressible if mode.is_compressing() => true,
FileCompressionState::Incompressible(_) => {
return;
let skip_reason: Option<SkipReason> = match &mut file_info.compression_state {
FileCompressionState::Compressed => {
if mode.is_compressing() {
Some(SkipReason::AlreadyCompressed)
} else {
None
}
}
_ => false,
};
if send {
let saved_times = match times::save_times(path.as_path()) {
Ok(saved_times) => saved_times,
Err(e) => {
progress.file_skipped(&path, SkipReason::ReadError(e));
return;
FileCompressionState::Compressible => {
if mode.is_compressing() {
None
} else {
Some(SkipReason::NotCompressed)
}
}
FileCompressionState::Incompressible(reason) => {
if mode.is_compressing() {
// We don't actually need the real reason, so we'll steal the reason here
Some(SkipReason::from(mem::replace(
reason,
IncompressibleReason::Empty,
)))
} else {
None
}
};

let inner_progress = Box::new(progress.file_task(&path, metadata.len()));
chan.send(reader::WorkItem {
context: Arc::new(Context {
operation: Arc::clone(&operation),
path,
progress: inner_progress,
orig_metadata: metadata,
parent_resetter: dir_reset,
orig_times: saved_times,
}),
})
.unwrap();
} else {
}
};
if let Some(skip_reason) = skip_reason {
progress.file_skipped(&path, skip_reason);
stats.add_end_file(&metadata, &file_info);
return;
}
let saved_times = match times::save_times(path.as_path()) {
Ok(saved_times) => saved_times,
Err(e) => {
progress.file_skipped(&path, SkipReason::ReadError(e));
stats.add_end_file(&metadata, &file_info);
return;
}
};

let inner_progress = Box::new(progress.file_task(&path, metadata.len()));
chan.send(reader::WorkItem {
context: Arc::new(Context {
operation: Arc::clone(&operation),
path,
progress: inner_progress,
orig_metadata: metadata,
parent_resetter: dir_reset,
orig_times: saved_times,
}),
})
.unwrap();
});
drop(operation);

Expand Down

0 comments on commit 5b26a4f

Please sign in to comment.