From f180f2231bc8d4db8c0631e6a8b2b537f1fb028c Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Sat, 6 Aug 2022 22:00:07 +0300 Subject: [PATCH 1/3] tail: fix for events that happened before watcher started watching files --- src/uu/tail/src/tail.rs | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 7ac33f311e8..3e04c2fc171 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -748,12 +748,86 @@ fn follow(files: &mut FileHandling, settings: &mut Settings) -> UResult<()> { } else { watcher.watch_with_parent(path.parent().unwrap())?; } + } else if settings.follow_name() { + // retry == false + if !path.exists() { + show_error!( + "cannot watch {}: No such file or directory", + files.get_display_name(path).quote() + ); + } } else { // TODO: [2022-05; jhscheer] do we need to handle this case? unimplemented!(); } } + // handle all events that could happen after last tail and before watcher started to watch it + if !settings.use_polling && settings.follow_name() { + let all_paths: Vec<_> = files.keys().map(|path| path.to_owned()).collect(); + for path in &all_paths { + let data = files.get(path); + let display_name = files.get_display_name(path); + if let Ok(new_md) = path.metadata() { + if !path.is_tailable() { + if let Some(old_md) = &data.metadata { + if old_md.is_tailable() { + if settings.retry { + show_error!( + "{} has been replaced with an untailable file", + display_name.quote() + ); + } else { + show_error!( + "{} has been replaced with an untailable file; giving up on this name", + display_name.quote() + ); + } + } + } else { + // nothing changed + } + } else { + if let Some(old_md) = &data.metadata { + if !old_md.is_tailable() { + show_error!("{} has become accessible", display_name.quote()); + files.update_reader(path)?; + } else if data.reader.is_none() { + show_error!( + "{} has appeared; following new file", + display_name.quote() + ); + files.update_reader(path)?; + } else if !old_md.file_id_eq(&new_md) { + show_error!( + "{} has been replaced; following new file", + display_name.quote() + ); + files.update_reader(path)?; + } + } else { + show_error!("{} has appeared; following new file", display_name.quote()); + files.update_reader(path)?; + } + } + files.update_metadata(path, Some(new_md)); + files.tail_file(path, settings.verbose)?; + } else { + if let Some(old_md) = &data.metadata { + if old_md.is_tailable() && data.reader.is_some() { + show_error!( + "{} {}: {}", + display_name.quote(), + text::BECOME_INACCESSIBLE, + text::NO_SUCH_FILE + ); + } + } + files.update_metadata(path, None); + } + } + } + // TODO: [2021-10; jhscheer] let mut _event_counter = 0; let mut _timeout_counter = 0; From 33e8c2a61c49b5f01157e35eb7c0f71212cc8efa Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Sat, 6 Aug 2022 22:06:10 +0300 Subject: [PATCH 2/3] tail: fix clippy advice --- src/uu/tail/src/tail.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 3e04c2fc171..1947b6bd356 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -787,28 +787,26 @@ fn follow(files: &mut FileHandling, settings: &mut Settings) -> UResult<()> { } else { // nothing changed } - } else { - if let Some(old_md) = &data.metadata { - if !old_md.is_tailable() { - show_error!("{} has become accessible", display_name.quote()); - files.update_reader(path)?; - } else if data.reader.is_none() { - show_error!( - "{} has appeared; following new file", - display_name.quote() - ); - files.update_reader(path)?; - } else if !old_md.file_id_eq(&new_md) { - show_error!( - "{} has been replaced; following new file", - display_name.quote() - ); - files.update_reader(path)?; - } - } else { - show_error!("{} has appeared; following new file", display_name.quote()); + } else if let Some(old_md) = &data.metadata { + if !old_md.is_tailable() { + show_error!("{} has become accessible", display_name.quote()); + files.update_reader(path)?; + } else if data.reader.is_none() { + show_error!( + "{} has appeared; following new file", + display_name.quote() + ); + files.update_reader(path)?; + } else if !old_md.file_id_eq(&new_md) { + show_error!( + "{} has been replaced; following new file", + display_name.quote() + ); files.update_reader(path)?; } + } else { + show_error!("{} has appeared; following new file", display_name.quote()); + files.update_reader(path)?; } files.update_metadata(path, Some(new_md)); files.tail_file(path, settings.verbose)?; From 6cccb89802b9becf86dc46ca3be0ef9da414a3d9 Mon Sep 17 00:00:00 2001 From: Niyaz Nigmatullin Date: Sat, 6 Aug 2022 22:10:03 +0300 Subject: [PATCH 3/3] tail: reformat with rustfmt --- src/uu/tail/src/tail.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 1947b6bd356..976711a14eb 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -792,10 +792,7 @@ fn follow(files: &mut FileHandling, settings: &mut Settings) -> UResult<()> { show_error!("{} has become accessible", display_name.quote()); files.update_reader(path)?; } else if data.reader.is_none() { - show_error!( - "{} has appeared; following new file", - display_name.quote() - ); + show_error!("{} has appeared; following new file", display_name.quote()); files.update_reader(path)?; } else if !old_md.file_id_eq(&new_md) { show_error!(