mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 07:12:44 +00:00
Merge pull request #3830 from niyaznigmatullin/fix_descriptor_vs_rename
tail: reuse opened file --follow=descriptor and renamed
This commit is contained in:
commit
563de8bab3
1 changed files with 16 additions and 9 deletions
|
@ -1037,21 +1037,28 @@ fn handle_event(
|
||||||
if settings.follow_descriptor() {
|
if settings.follow_descriptor() {
|
||||||
let new_path = event.paths.last().unwrap();
|
let new_path = event.paths.last().unwrap();
|
||||||
paths.push(new_path.to_owned());
|
paths.push(new_path.to_owned());
|
||||||
// Open new file and seek to End:
|
// Remove old reader
|
||||||
let mut file = File::open(&new_path)?;
|
let old_reader = files.remove(event_path).reader;
|
||||||
file.seek(SeekFrom::End(0))?;
|
let reader = if old_reader.is_some() {
|
||||||
|
// Use old reader with the same file descriptor if there is one
|
||||||
|
old_reader
|
||||||
|
} else if let Ok(file) = File::open(&new_path) {
|
||||||
|
// Open new file tail from start
|
||||||
|
Some(Box::new(BufReader::new(file)) as Box<dyn BufRead>)
|
||||||
|
} else {
|
||||||
|
// Probably file was renamed/moved or removed again
|
||||||
|
None
|
||||||
|
};
|
||||||
// Add new reader but keep old display name
|
// Add new reader but keep old display name
|
||||||
files.insert(
|
files.insert(
|
||||||
new_path,
|
new_path,
|
||||||
PathData {
|
PathData {
|
||||||
metadata: file.metadata().ok(),
|
metadata: new_path.metadata().ok(),
|
||||||
reader: Some(Box::new(BufReader::new(file))),
|
reader,
|
||||||
display_name, // mimic GNU's tail and show old name in header
|
display_name, // mimic GNU's tail and show old name in header
|
||||||
},
|
},
|
||||||
files.get_last().unwrap() == event_path
|
files.get_last().unwrap() == event_path
|
||||||
);
|
);
|
||||||
// Remove old reader
|
|
||||||
files.remove(event_path);
|
|
||||||
// Unwatch old path and watch new path
|
// Unwatch old path and watch new path
|
||||||
let _ = watcher.unwatch(event_path);
|
let _ = watcher.unwatch(event_path);
|
||||||
watcher.watch_with_parent(new_path)?;
|
watcher.watch_with_parent(new_path)?;
|
||||||
|
@ -1103,8 +1110,8 @@ mod files {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper for HashMap::remove using Path::canonicalize
|
/// Wrapper for HashMap::remove using Path::canonicalize
|
||||||
pub fn remove(&mut self, k: &Path) {
|
pub fn remove(&mut self, k: &Path) -> PathData {
|
||||||
self.map.remove(&Self::canonicalize_path(k)).unwrap();
|
self.map.remove(&Self::canonicalize_path(k)).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper for HashMap::get using Path::canonicalize
|
/// Wrapper for HashMap::get using Path::canonicalize
|
||||||
|
|
Loading…
Reference in a new issue