Commit d3678595 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Show file change notifications in stdout

parent f91fce1d
...@@ -16,11 +16,16 @@ use std::sync::Mutex; ...@@ -16,11 +16,16 @@ use std::sync::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::time::Duration; use std::time::Duration;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread; use std::thread;
use std::process; use std::process;
use std::process::Command; use std::process::Command;
static DEBOUNCING_DONE: AtomicBool = AtomicBool::new(true);
lazy_static! { lazy_static! {
static ref IPFS_HASHES: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new()); static ref IPFS_HASHES: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
} }
...@@ -67,11 +72,13 @@ fn add_files_to_ipfs() { ...@@ -67,11 +72,13 @@ fn add_files_to_ipfs() {
(clean_path.to_string(), hash.to_string()) (clean_path.to_string(), hash.to_string())
})); }));
println!("https://www.eternum.io/ipfs/{}", hashes.get("").unwrap());
} }
fn notify_ipfs(event: notify::DebouncedEvent) { fn files_changed(event: notify::DebouncedEvent, add_to_ipfs: bool) {
// This function gets triggered when inotify notices files having changed.
//
// add_to_ipfs will be true when we actually need to add the files to IPFS (ie when enough time
// has passed after debouncing).
match event { match event {
notify::DebouncedEvent::Create(_) => {} notify::DebouncedEvent::Create(_) => {}
notify::DebouncedEvent::Remove(_) => {} notify::DebouncedEvent::Remove(_) => {}
...@@ -79,7 +86,18 @@ fn notify_ipfs(event: notify::DebouncedEvent) { ...@@ -79,7 +86,18 @@ fn notify_ipfs(event: notify::DebouncedEvent) {
notify::DebouncedEvent::Write(_) => {} notify::DebouncedEvent::Write(_) => {}
_ => return, _ => return,
} }
add_files_to_ipfs();
// If the variable wasn't updated, there's nothing for us to do.
if DEBOUNCING_DONE.compare_and_swap(!add_to_ipfs, add_to_ipfs, Ordering::Relaxed) == add_to_ipfs {
return;
}
if add_to_ipfs {
add_files_to_ipfs();
println!("Finished adding files to IPFS.\n");
} else {
println!("Change detected in filesystem.");
}
} }
fn start_ipfs() { fn start_ipfs() {
...@@ -103,47 +121,63 @@ fn start_ipfs() { ...@@ -103,47 +121,63 @@ fn start_ipfs() {
add_files_to_ipfs(); add_files_to_ipfs();
}); });
Command::new(IPFS_EXECUTABLE) let output = Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH) .env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"]) .args(&["daemon", "--writable", "--enable-gc", "--migrate"])
.output() .output()
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
println!("Could not launch IPFS daemon."); println!("Could not launch IPFS daemon.");
process::exit(1) process::exit(1)
}); });
println!("IPFS daemon died, exiting...");
println!("{:?}", output);
process::exit(1)
} }
fn main() { fn create_debounced_watcher() {
println!("Starting hearth..."); let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(5)).unwrap();
thread::spawn(start_ipfs); watcher
thread::spawn(start_api); .watch(&*HEARTH_DIR, RecursiveMode::Recursive)
.expect("There was an error when trying to watch the Hearth directory for changes.");
match fs::create_dir(&*HEARTH_DIR) { loop {
Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => { match rx.recv() {
println!("Could not create the Hearth directory, for some reason."); Ok(e) => files_changed(e, true),
process::exit(1) Err(e) => println!("watch error: {:?}", e),
} }
_ => {}
} }
}
// Create a channel to receive the events. fn create_immediate_watcher() {
let (tx, rx) = channel(); let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(0)).unwrap();
// Create a watcher object, delivering debounced events.
// The notification back-end is selected based on the platform.
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(5)).unwrap();
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher watcher
.watch(&*HEARTH_DIR, RecursiveMode::Recursive) .watch(&*HEARTH_DIR, RecursiveMode::Recursive)
.expect("There was an error when trying to watch the Hearth directory for changes."); .expect("There was an error when trying to watch the Hearth directory for changes.");
loop { loop {
match rx.recv() { match rx.recv() {
Ok(e) => notify_ipfs(e), Ok(e) => files_changed(e, false),
Err(e) => println!("watch error: {:?}", e), Err(e) => println!("watch error: {:?}", e),
} }
} }
} }
fn main() {
println!("Starting hearth...");
thread::spawn(start_ipfs);
thread::spawn(start_api);
match fs::create_dir(&*HEARTH_DIR) {
Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => {
println!("Could not create the Hearth directory, for some reason.");
process::exit(1)
}
_ => {}
}
thread::spawn(create_immediate_watcher);
create_debounced_watcher();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment