Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
qihexiang committed Sep 9, 2023
0 parents commit e366065
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
293 changes: 293 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "nissyncd"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
notify-debouncer-mini = "0.4.1"
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world
Empty file added index.js
Empty file.
53 changes: 53 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use notify_debouncer_mini::{
new_debouncer_opt, Config,
notify::{self, PollWatcher}, notify::{ErrorKind as NotifyErrorKind, RecursiveMode, Result}, Debouncer,
};
use std::env;
use std::io::ErrorKind as IoErrorKind;
use std::path::Path;
use std::process::Command;
use std::sync::mpsc::channel;
use std::time::Duration;

fn main() -> Result<()> {
let yp_directory = env::var("YP_DIRECTORY").unwrap_or("/var/yp".to_string());
let notify_config = notify::Config::default().with_poll_interval(Duration::from_secs(2));
let (tx, rx) = channel();
let mut debouncer: Debouncer<PollWatcher> = new_debouncer_opt(Config::default().with_timeout(Duration::from_secs(2)).with_notify_config(notify_config), tx)?;

let watcher = debouncer.watcher();

for filename in ["/etc/passwd", "/etc/group", "/etc/shadow", "/etc/gshadow"] {
match watcher.watch(Path::new(filename), RecursiveMode::NonRecursive) {
Ok(_) => println!("{} watched", filename),
Err(err) => match err.kind {
NotifyErrorKind::Io(err) => match err.kind() {
IoErrorKind::NotFound => println!(
"No such file {}, it may related to your distribution",
filename
),
_ => panic!("Unable to watch file: {}, reason: \n{:?}", filename, err),
},
_ => panic!("Filed to watch file {}, error: {:?}", filename, err),
},
}
}

for event in rx {
match event {
Ok(_) => {
println!("Files changed!");
Command::new("make")
.current_dir(&yp_directory)
.spawn()
.expect("failed to execute rebuild process, is make command and the yp directory exist?")
.wait()
.expect("failed to wait for the rebuild process");
println!("Rebuild finished!")
}
Err(err) => println!("Something is wrong: {:?}", err),
}
}

Ok(())
}

0 comments on commit e366065

Please sign in to comment.