diff --git a/Cargo.lock b/Cargo.lock index 582203b..38382c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,16 +130,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11f1667b8320afa80d69d8bbe40830df2c8a06003d86f73d8e003b2c48df416d" dependencies = [ "async-trait", - "json5", "lazy_static", "nom", "pathdiff", - "ron", - "rust-ini", "serde", - "serde_json", "toml", - "yaml-rust", ] [[package]] @@ -363,17 +358,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "json5" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" -dependencies = [ - "pest", - "pest_derive", - "serde", -] - [[package]] name = "kqueue" version = "1.0.6" @@ -406,12 +390,6 @@ version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "log" version = "0.4.17" @@ -526,6 +504,12 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" +[[package]] +name = "online" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7680985bd550795c0161707f51f9abada87c63a5409114ed818a8618d18ec5e5" + [[package]] name = "ordered-multimap" version = "0.4.3" @@ -548,50 +532,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" -[[package]] -name = "pest" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b0560d531d1febc25a3c9398a62a71256c0178f2e3443baedd9ad4bb8c9deb4" -dependencies = [ - "thiserror", - "ucd-trie", -] - -[[package]] -name = "pest_derive" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "905708f7f674518498c1f8d644481440f476d39ca6ecae83319bba7c6c12da91" -dependencies = [ - "pest", - "pest_generator", -] - -[[package]] -name = "pest_generator" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5803d8284a629cc999094ecd630f55e91b561a1d1ba75e233b00ae13b91a69ad" -dependencies = [ - "pest", - "pest_meta", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "pest_meta" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1538eb784f07615c6d9a8ab061089c6c54a344c5b4301db51990ca1c241e8c04" -dependencies = [ - "once_cell", - "pest", - "sha-1", -] - [[package]] name = "pin-project-lite" version = "0.2.9" @@ -651,17 +591,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "ron" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" -dependencies = [ - "base64", - "bitflags", - "serde", -] - [[package]] name = "rsink" version = "0.1.0" @@ -671,6 +600,7 @@ dependencies = [ "dirs", "lazy_static", "notify", + "online", "rust-s3", "spinners", ] @@ -819,17 +749,6 @@ dependencies = [ "serde", ] -[[package]] -name = "sha-1" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "sha2" version = "0.10.2" @@ -993,12 +912,6 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" -[[package]] -name = "ucd-trie" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89570599c4fe5585de2b388aab47e99f7fa4e9238a1399f707a02e356058141c" - [[package]] name = "unicode-bidi" version = "0.3.8" @@ -1229,12 +1142,3 @@ name = "xml-rs" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" - -[[package]] -name = "yaml-rust" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] diff --git a/Cargo.toml b/Cargo.toml index 91c1c46..03f49c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,10 @@ edition = "2021" [dependencies] anyhow = "1.0.62" -config = "0.13.2" +config = { version = "0.13.2", default-features = false, features = ["toml"] } dirs = "4.0.0" lazy_static = "1.4.0" notify = "5.0.0" rust-s3 = { version = "0.32.3", default-features = false, features = ["sync", "tags", "sync-rustls-tls"] } spinners = "4.1.0" +online = { version = "3.0.1", default-features = false, features = ["sync"] } \ No newline at end of file diff --git a/config.example.toml b/config.example.toml index ad2c824..8ba2a2a 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,5 +1,6 @@ [main] -interval = 60000 +provider = "s3" +interval = 600000 path = "/home/abdulrahman/Sync" [s3] diff --git a/src/main.rs b/src/main.rs index f4ef745..5bd14c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,10 +7,11 @@ mod util; use cloud::{adapters::*, CloudAdapter}; use config::Config; -use notify::{event::*, EventKind, RecursiveMode, Watcher}; +use notify::{event::*, recommended_watcher, RecursiveMode, Watcher}; use std::{ collections::HashSet, path::PathBuf, + process::exit, sync::{mpsc::channel, Arc, Mutex}, thread, time::Duration, @@ -28,35 +29,60 @@ lazy_static! { .expect("Missing main.path env") .parse() .expect("Invalid path string"); + pub static ref IS_INTERNET_AVAILABLE: Mutex = Mutex::new(false); + pub static ref SYNCING: Mutex = Mutex::new(false); } fn main() -> Result<()> { - let cloud_ref = Arc::new(s3::Cloud::new()); - let syncing_ref = Arc::new(Mutex::new(false)); + let cloud_ref = Arc::new( + match SETTINGS + .get_string("main.provider") + .expect("Missing cloud provider in config/settings") + .replace('_', "") + .replace('-', "") + .as_str() + { + "s3" => s3::Cloud::new(), + // TODO: Support more cloud + // "googledrive" | "gdrive" => googledrive::Cloud::new(), + // "dropbox" => dropbox::Cloud::new(), + // "mega" => mega::Cloud::new(), + // "onedrive" => onedrive::Cloud::new(), + // "protondrive" => protondrive::Cloud::new(), + x => { + println!("Unspported cloud provider: {}", x); + exit(1); + } + }, + ); + let interval: u64 = SETTINGS .get("main.interval") .expect("Missing/Invalid interval value"); let cloud = cloud_ref.clone(); - let syncing = syncing_ref.clone(); let online_task = thread::spawn(move || loop { - *syncing.lock().unwrap() = true; - spinner("Syncing...", "Synced!", || cloud.sync().map(|_| {})); - *syncing.lock().unwrap() = false; - thread::sleep(Duration::from_millis(interval)); + check_connectivity(); + + if *IS_INTERNET_AVAILABLE.lock().unwrap() { + *SYNCING.lock().unwrap() = true; + spinner("Syncing...", "Synced!", || cloud.sync().map(|_| {})); + *SYNCING.lock().unwrap() = false; + thread::sleep(Duration::from_millis(interval)); + } else { + println!("Skip syncing.. there are no internet connection"); + } }); let cloud = cloud_ref; - let syncing = syncing_ref; let local_task = thread::spawn(move || { let (tx, rx) = channel(); - let mut watcher = notify::recommended_watcher(tx)?; + let mut watcher = recommended_watcher(tx)?; + let mut changes = HashSet::new(); watcher.watch(&SYNC_DIR, RecursiveMode::Recursive)?; - let mut changes = HashSet::::new(); - while let Ok(event) = rx.recv() { let event = match event { Ok(e) => e, @@ -66,11 +92,17 @@ fn main() -> Result<()> { } }; - if *syncing.lock().unwrap() { + println!("{:?}", event); + + if !*IS_INTERNET_AVAILABLE.lock().unwrap() { + println!("Skip local syncing.. there are no internet connection"); continue; } - println!("{:?}", event); + if *SYNCING.lock().unwrap() { + println!("Ignore event since online syncing is working"); + continue; + } match event.kind { EventKind::Create(CreateKind::File) => { diff --git a/src/util.rs b/src/util.rs index 9cc4180..55faa0a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use crate::SYNC_DIR; +use crate::{IS_INTERNET_AVAILABLE, SYNC_DIR}; pub use anyhow::Result; use spinners::{Spinner, Spinners}; use std::{ @@ -94,3 +94,7 @@ pub fn settings_file() -> Result { Ok(path) } + +pub fn check_connectivity() { + *IS_INTERNET_AVAILABLE.lock().unwrap() = online::sync::check(None).is_ok(); +}