Last active
August 29, 2015 14:12
-
-
Save daboross/20e6c0bf2a517073b1ce to your computer and use it in GitHub Desktop.
Just a bunch of code to debug inotify events!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn watch_binary() -> Result<thread::JoinGuard<()>, InitializationError> { | |
let mut watch = try!(inotify::INotify::init()); | |
let args = os::args(); | |
let program = match Path::new_opt(args[0].as_slice()) { | |
Some(v) => v, | |
None => return Err(InitializationError::from_string( | |
format!("Failed to create Path from args[0] ({})", args[0]))), | |
}; | |
let filename = match program.filename_str() { | |
Some(v) => v.to_string(), | |
None => return Err(InitializationError::from_string( | |
format!("Failed to get filename from program Path ({})", program.display()))), | |
}; | |
try!(watch.add_watch(&program.dir_path(), | |
// inotify::ffi::IN_ALL_EVENTS & | |
// !inotify::ffi::IN_ACCESS & | |
// !inotify::ffi::IN_MODIFY | |
inotify::ffi::IN_CLOSE_WRITE | | |
inotify::ffi::IN_MOVED_TO | | |
inotify::ffi::IN_CREATE | |
)); | |
let guard = thread::Thread::spawn(move || { | |
loop { | |
let events = match watch.wait_for_events() { | |
Ok(v) => v, | |
Err(e) => { | |
warning!("INotify error: {}. Exiting.", e); | |
return; | |
}, | |
}; | |
for event in events.iter() { | |
if event.is_ignored() { | |
warning!( | |
"File watch on binary removed due to a deleted directory or unmounted \ | |
filesystem. Exiting watch thread, bot will no longer watch binary for \ | |
restarting.") | |
} | |
if filename != event.name { | |
if event.name.is_empty() { | |
if event.is_dir() { | |
continue; | |
} | |
println!("Empty event"); | |
} else { | |
println!("Ignoring event for unknown file: {}", event.name); | |
} | |
} else { | |
} | |
println!("Event! \"{}\"", event.name); | |
if event.is_attrib() { | |
println!("\tevent is: attrib"); | |
} | |
if event.is_close_write() { | |
println!("\tevent is: close_write"); | |
} | |
if event.is_moved_to() { | |
println!("\tevent is: moved_to"); | |
} | |
if event.is_create() { | |
println!("\tevent is: create"); | |
} | |
if event.is_unmount() { | |
println!("\tevent is: unmount"); | |
} | |
if event.is_queue_overflow() { | |
println!("\tevent is: queue_overflow"); | |
} | |
if event.is_ignored() { | |
println!("\tevent is: ignored"); | |
} | |
} | |
} | |
}); | |
return Ok(guard); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loop { | |
let events = match watch.wait_for_events() { | |
Ok(v) => v, | |
Err(e) => { | |
warning!("INotify error: {}. Exiting.", e); | |
return; | |
}, | |
}; | |
for event in events.iter() { | |
println!("Event! \"{}\"", event.name); | |
if event.is_access() { | |
println!("\tevent is: access"); | |
} | |
if event.is_modify() { | |
println!("\tevent is: modify"); | |
} | |
if event.is_attrib() { | |
println!("\tevent is: attrib"); | |
} | |
if event.is_close_write() { | |
println!("\tevent is: close_write"); | |
} | |
if event.is_close_nowrite() { | |
println!("\tevent is: close_nowrite"); | |
} | |
if event.is_open() { | |
println!("\tevent is: open"); | |
} | |
if event.is_moved_from() { | |
println!("\tevent is: moved_from"); | |
} | |
if event.is_moved_to() { | |
println!("\tevent is: moved_to"); | |
} | |
if event.is_create() { | |
println!("\tevent is: create"); | |
} | |
if event.is_delete() { | |
println!("\tevent is: delete"); | |
} | |
if event.is_delete_self() { | |
println!("\tevent is: delete_self"); | |
} | |
if event.is_move_self() { | |
println!("\tevent is: move_self"); | |
} | |
if event.is_move() { | |
println!("\tevent is: move"); | |
} | |
if event.is_close() { | |
println!("\tevent is: close"); | |
} | |
if event.is_dir() { | |
println!("\tevent is: dir"); | |
} | |
if event.is_unmount() { | |
println!("\tevent is: unmount"); | |
} | |
if event.is_queue_overflow() { | |
println!("\tevent is: queue_overflow"); | |
} | |
if event.is_ignored() { | |
println!("\tevent is: ignored"); | |
} | |
// println!("Event! {}", event.mask) | |
// match event.mask { | |
// inotify::ffi::IN_MODIFY => println!("Modify event!"), | |
// inotify::ffi::IN_CLOSE_WRITE => println!("Close write event!"), | |
// inotify::ffi::IN_MOVE_SELF => println!("Move self event!"), | |
// inotify::ffi::IN_MOVED_TO => println!("Moved to event!"), | |
// inotify::ffi::IN_CREATE => println!("Create event!"), | |
// _ => println!("Unexpected event! {}", event), | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment