New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Name all threads #383
Name all threads #383
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,106 +76,113 @@ impl PollWatcher { | |
let event_handler = self.event_handler.clone(); | ||
let event_handler = move |res| emit_event(&event_handler, res); | ||
|
||
thread::spawn(move || { | ||
// In order of priority: | ||
// TODO: handle metadata events | ||
// TODO: handle renames | ||
// TODO: DRY it up | ||
let _ = thread::Builder::new() | ||
.name("notify-rs poll".to_string()) | ||
.spawn(move || { | ||
// In order of priority: | ||
// TODO: handle metadata events | ||
// TODO: handle renames | ||
// TODO: DRY it up | ||
|
||
loop { | ||
if !open.load(Ordering::SeqCst) { | ||
break; | ||
} | ||
loop { | ||
if !open.load(Ordering::SeqCst) { | ||
break; | ||
} | ||
|
||
if let Ok(mut watches) = watches.lock() { | ||
let current_time = Instant::now(); | ||
if let Ok(mut watches) = watches.lock() { | ||
let current_time = Instant::now(); | ||
|
||
for ( | ||
watch, | ||
&mut WatchData { | ||
is_recursive, | ||
ref mut paths, | ||
}, | ||
) in watches.iter_mut() | ||
{ | ||
match fs::metadata(watch) { | ||
Err(e) => { | ||
let err = Err(Error::io(e).add_path(watch.clone())); | ||
event_handler(err); | ||
continue; | ||
} | ||
Ok(metadata) => { | ||
if !metadata.is_dir() { | ||
let mtime = | ||
FileTime::from_last_modification_time(&metadata).seconds(); | ||
match paths.insert( | ||
watch.clone(), | ||
PathData { | ||
mtime, | ||
last_check: current_time, | ||
}, | ||
) { | ||
None => { | ||
unreachable!(); | ||
} | ||
Some(PathData { | ||
mtime: old_mtime, .. | ||
}) => { | ||
if mtime > old_mtime { | ||
let kind = MetadataKind::WriteTime; | ||
let meta = ModifyKind::Metadata(kind); | ||
let kind = EventKind::Modify(meta); | ||
let ev = Event::new(kind).add_path(watch.clone()); | ||
event_handler(Ok(ev)); | ||
for ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is all this indentation noise from rustfmt ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah - it just jumps in a bit because the scope block indents one further. |
||
watch, | ||
&mut WatchData { | ||
is_recursive, | ||
ref mut paths, | ||
}, | ||
) in watches.iter_mut() | ||
{ | ||
match fs::metadata(watch) { | ||
Err(e) => { | ||
let err = Err(Error::io(e).add_path(watch.clone())); | ||
event_handler(err); | ||
continue; | ||
} | ||
Ok(metadata) => { | ||
if !metadata.is_dir() { | ||
let mtime = | ||
FileTime::from_last_modification_time(&metadata) | ||
.seconds(); | ||
match paths.insert( | ||
watch.clone(), | ||
PathData { | ||
mtime, | ||
last_check: current_time, | ||
}, | ||
) { | ||
None => { | ||
unreachable!(); | ||
} | ||
Some(PathData { | ||
mtime: old_mtime, .. | ||
}) => { | ||
if mtime > old_mtime { | ||
let kind = MetadataKind::WriteTime; | ||
let meta = ModifyKind::Metadata(kind); | ||
let kind = EventKind::Modify(meta); | ||
let ev = | ||
Event::new(kind).add_path(watch.clone()); | ||
event_handler(Ok(ev)); | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
let depth = if is_recursive { usize::max_value() } else { 1 }; | ||
for entry in WalkDir::new(watch) | ||
.follow_links(true) | ||
.max_depth(depth) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
{ | ||
let path = entry.path(); | ||
} else { | ||
let depth = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here for the newline |
||
if is_recursive { usize::max_value() } else { 1 }; | ||
for entry in WalkDir::new(watch) | ||
.follow_links(true) | ||
.max_depth(depth) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
{ | ||
let path = entry.path(); | ||
|
||
match entry.metadata() { | ||
Err(e) => { | ||
let err = Error::io(e.into()) | ||
.add_path(path.to_path_buf()); | ||
event_handler(Err(err)); | ||
} | ||
Ok(m) => { | ||
let mtime = | ||
FileTime::from_last_modification_time(&m) | ||
.seconds(); | ||
match paths.insert( | ||
path.to_path_buf(), | ||
PathData { | ||
mtime, | ||
last_check: current_time, | ||
}, | ||
) { | ||
None => { | ||
let kind = | ||
EventKind::Create(CreateKind::Any); | ||
let ev = Event::new(kind) | ||
.add_path(path.to_path_buf()); | ||
event_handler(Ok(ev)); | ||
} | ||
Some(PathData { | ||
mtime: old_mtime, .. | ||
}) => { | ||
if mtime > old_mtime { | ||
let kind = MetadataKind::WriteTime; | ||
let meta = ModifyKind::Metadata(kind); | ||
let kind = EventKind::Modify(meta); | ||
// TODO add new mtime as attr | ||
match entry.metadata() { | ||
Err(e) => { | ||
let err = Error::io(e.into()) | ||
.add_path(path.to_path_buf()); | ||
event_handler(Err(err)); | ||
} | ||
Ok(m) => { | ||
let mtime = | ||
FileTime::from_last_modification_time(&m) | ||
.seconds(); | ||
match paths.insert( | ||
path.to_path_buf(), | ||
PathData { | ||
mtime, | ||
last_check: current_time, | ||
}, | ||
) { | ||
None => { | ||
let kind = | ||
EventKind::Create(CreateKind::Any); | ||
let ev = Event::new(kind) | ||
.add_path(path.to_path_buf()); | ||
event_handler(Ok(ev)); | ||
} | ||
Some(PathData { | ||
mtime: old_mtime, .. | ||
}) => { | ||
if mtime > old_mtime { | ||
let kind = MetadataKind::WriteTime; | ||
let meta = | ||
ModifyKind::Metadata(kind); | ||
let kind = EventKind::Modify(meta); | ||
// TODO add new mtime as attr | ||
let ev = Event::new(kind) | ||
.add_path(path.to_path_buf()); | ||
event_handler(Ok(ev)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -184,27 +191,26 @@ impl PollWatcher { | |
} | ||
} | ||
} | ||
} | ||
|
||
for (_, &mut WatchData { ref mut paths, .. }) in watches.iter_mut() { | ||
let mut removed = Vec::new(); | ||
for (path, &PathData { last_check, .. }) in paths.iter() { | ||
if last_check < current_time { | ||
let ev = Event::new(EventKind::Remove(RemoveKind::Any)) | ||
.add_path(path.clone()); | ||
event_handler(Ok(ev)); | ||
removed.push(path.clone()); | ||
for (_, &mut WatchData { ref mut paths, .. }) in watches.iter_mut() { | ||
let mut removed = Vec::new(); | ||
for (path, &PathData { last_check, .. }) in paths.iter() { | ||
if last_check < current_time { | ||
let ev = Event::new(EventKind::Remove(RemoveKind::Any)) | ||
.add_path(path.clone()); | ||
event_handler(Ok(ev)); | ||
removed.push(path.clone()); | ||
} | ||
} | ||
for path in removed { | ||
(*paths).remove(&path); | ||
} | ||
} | ||
for path in removed { | ||
(*paths).remove(&path); | ||
} | ||
} | ||
} | ||
|
||
thread::sleep(delay); | ||
} | ||
}); | ||
thread::sleep(delay); | ||
} | ||
}); | ||
} | ||
|
||
fn watch_inner(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe "notify-rs fsevent loop" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.