Skip to content

Commit

Permalink
fix: improved sync error handling and fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Jul 4, 2022
1 parent af40145 commit 32752b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
20 changes: 5 additions & 15 deletions aw-sync/src/main.rs
Expand Up @@ -13,6 +13,7 @@ extern crate chrono;
extern crate serde;
extern crate serde_json;

use std::error::Error;
use std::path::Path;

use chrono::{DateTime, Datelike, TimeZone, Utc};
Expand Down Expand Up @@ -74,7 +75,7 @@ enum Commands {
List {},
}

fn main() -> std::io::Result<()> {
fn main() -> Result<(), Box<dyn Error>> {
let opts: Opts = Opts::parse();

info!("Started aw-sync...");
Expand Down Expand Up @@ -132,23 +133,12 @@ fn main() -> std::io::Result<()> {
_ => panic!("Invalid mode"),
};

sync::sync_run(client, &sync_spec, mode_enum).map_err(|e| {
println!("Error: {}", e);
std::io::Error::new(std::io::ErrorKind::Other, e)
})?;
Ok(())
sync::sync_run(client, &sync_spec, mode_enum)
}

// List all buckets
Commands::List {} => {
sync::list_buckets(&client, sync_directory);
Ok(())
}
}
.map_err(|e: String| {
println!("Error: {}", e);
std::io::Error::new(std::io::ErrorKind::Other, e)
})?;
Commands::List {} => sync::list_buckets(&client, sync_directory),
}?;

// Needed to give the datastores some time to commit before program is shut down.
// 100ms isn't actually needed, seemed to work fine with as little as 10ms, but I'd rather give
Expand Down
35 changes: 17 additions & 18 deletions aw-sync/src/sync.rs
Expand Up @@ -9,6 +9,7 @@ extern crate chrono;
extern crate reqwest;
extern crate serde_json;

use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};

Expand All @@ -20,6 +21,13 @@ use aw_models::{Bucket, Event};

use crate::accessmethod::AccessMethod;

#[derive(PartialEq)]
pub enum SyncMode {
Push,
Pull,
Both,
}

pub struct SyncSpec {
/// Path of sync folder
pub path: PathBuf,
Expand All @@ -29,13 +37,6 @@ pub struct SyncSpec {
pub start: Option<DateTime<Utc>>,
}

#[derive(PartialEq)]
pub enum SyncMode {
Push,
Pull,
Both,
}

impl Default for SyncSpec {
fn default() -> Self {
// TODO: Better default path
Expand All @@ -52,7 +53,7 @@ impl Default for SyncSpec {
pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Result<(), String> {
let ds_localremote = setup_local_remote(&client, sync_spec.path.as_path())?;

let info = client.get_info().unwrap();
let info = client.get_info().map_err(|e| e.to_string())?;
let remote_dbfiles = find_remotes_nonlocal(sync_spec.path.as_path(), info.device_id.as_str());

// Log if remotes found
Expand Down Expand Up @@ -119,10 +120,10 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul
}

#[allow(dead_code)]
pub fn list_buckets(client: &AwClient, sync_directory: &Path) {
let ds_localremote = setup_local_remote(client, sync_directory).unwrap();
pub fn list_buckets(client: &AwClient, sync_directory: &Path) -> Result<(), String> {
let ds_localremote = setup_local_remote(client, sync_directory)?;

let info = client.get_info().unwrap();
let info = client.get_info().map_err(|e| e.to_string())?;
let remote_dbfiles = find_remotes_nonlocal(sync_directory, info.device_id.as_str());
info!("Found remotes: {:?}", remote_dbfiles);

Expand All @@ -138,13 +139,15 @@ pub fn list_buckets(client: &AwClient, sync_directory: &Path) {
for ds_from in &ds_remotes {
log_buckets(ds_from);
}

Ok(())
}

fn setup_local_remote(client: &AwClient, path: &Path) -> Result<Datastore, String> {
// FIXME: Don't run twice if already exists
fs::create_dir_all(path).unwrap();

let info = client.get_info().unwrap();
let info = client.get_info().map_err(|e| e.to_string())?;
let remotedir = path.join(info.device_id.as_str());
fs::create_dir_all(&remotedir).unwrap();

Expand All @@ -161,16 +164,12 @@ fn setup_local_remote(client: &AwClient, path: &Path) -> Result<Datastore, Strin

/// Returns a list of all remote dbs
fn find_remotes(sync_directory: &Path) -> std::io::Result<Vec<PathBuf>> {
//info!("Using sync dir: {}", sync_directory.display());
let dbs = fs::read_dir(sync_directory)?
.map(|res| res.ok().unwrap().path())
.filter(|p| p.is_dir())
.flat_map(|d| {
//println!("{}", d.to_str().unwrap());
fs::read_dir(d).unwrap()
})
.flat_map(|d| fs::read_dir(d).unwrap())
.map(|res| res.ok().unwrap().path())
.filter(|path| path.extension().unwrap() == "db") // FIXME: Is this the correct file ext?
.filter(|path| path.extension().unwrap_or_else(|| OsStr::new("")) == "db")
.collect();
Ok(dbs)
}
Expand Down

0 comments on commit 32752b6

Please sign in to comment.