skip empty files

This commit is contained in:
Robin Appelman 2025-11-25 17:19:12 +01:00
commit 4c0ebc138d

View file

@ -121,6 +121,10 @@ fn handle_watch_event(
match FileInfo::load(path) { match FileInfo::load(path) {
Ok(file) => { Ok(file) => {
if file.size == 0 {
info!(file = file.path, "skipping empty file");
return;
}
if let Some(new_path) = handle_file(&file, rules, remove_duplicates) { if let Some(new_path) = handle_file(&file, rules, remove_duplicates) {
maybe_link_target(&new_path, link_target); maybe_link_target(&new_path, link_target);
if notify { if notify {
@ -251,8 +255,6 @@ fn match_file(file: &FileInfo, rules: &[Rule]) -> Option<RuleResult> {
#[instrument(skip_all, fields(file = file.path))] #[instrument(skip_all, fields(file = file.path))]
fn handle_file(file: &FileInfo, rules: &[Rule], remove_duplicates: bool) -> Option<PathBuf> { fn handle_file(file: &FileInfo, rules: &[Rule], remove_duplicates: bool) -> Option<PathBuf> {
let Some(result) = match_file(file, rules) else { let Some(result) = match_file(file, rules) else {
info!(url = file.url, "removing duplicate");
if remove_duplicates { if remove_duplicates {
let parent = Path::new(&file.path).parent().unwrap(); let parent = Path::new(&file.path).parent().unwrap();
if let Some(duplicate) = has_duplicate(file, parent) { if let Some(duplicate) = has_duplicate(file, parent) {
@ -277,6 +279,7 @@ fn handle_file(file: &FileInfo, rules: &[Rule], remove_duplicates: bool) -> Opti
if remove_duplicates { if remove_duplicates {
if let Some(duplicate) = has_duplicate(file, parent) { if let Some(duplicate) = has_duplicate(file, parent) {
dbg!(file.size);
info!(url = file.url, diplicate = %duplicate.display(), "removing duplicate"); info!(url = file.url, diplicate = %duplicate.display(), "removing duplicate");
if let Err(error) = remove_file(&file.path) { if let Err(error) = remove_file(&file.path) {
error!(%error, "failed to remove duplicate"); error!(%error, "failed to remove duplicate");
@ -300,6 +303,11 @@ fn handle_file(file: &FileInfo, rules: &[Rule], remove_duplicates: bool) -> Opti
} }
fn has_duplicate(file: &FileInfo, dir: impl AsRef<Path>) -> Option<PathBuf> { fn has_duplicate(file: &FileInfo, dir: impl AsRef<Path>) -> Option<PathBuf> {
if file.size == 0 {
return None;
}
dbg!(file.size);
let dir = match read_dir(dir) { let dir = match read_dir(dir) {
Ok(dir) => dir, Ok(dir) => dir,
Err(error) => { Err(error) => {
@ -312,7 +320,7 @@ fn has_duplicate(file: &FileInfo, dir: impl AsRef<Path>) -> Option<PathBuf> {
if path.to_str() == Some(file.path.as_str()) { if path.to_str() == Some(file.path.as_str()) {
continue; continue;
} }
if !path.is_file() { if path.is_symlink() || !path.is_file() {
continue; continue;
} }