From dbdd8a78951f8a44630abcceb7b13d0d823ee074 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 3 May 2020 04:07:03 -0400 Subject: allow storing password databases from different servers this should allow easy switching of servers by just `rbw config set base_url` --- src/db.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index b61f1bd..e5c7257 100644 --- a/src/db.rs +++ b/src/db.rs @@ -43,8 +43,8 @@ impl Db { Self::default() } - pub fn load(email: &str) -> Result { - let mut fh = std::fs::File::open(crate::dirs::db_file(email)) + pub fn load(server: &str, email: &str) -> Result { + let mut fh = std::fs::File::open(crate::dirs::db_file(server, email)) .context(crate::error::LoadDb)?; let mut json = String::new(); fh.read_to_string(&mut json).context(crate::error::LoadDb)?; @@ -53,10 +53,11 @@ impl Db { Ok(slf) } - pub async fn load_async(email: &str) -> Result { - let mut fh = tokio::fs::File::open(crate::dirs::db_file(email)) - .await - .context(crate::error::LoadDbAsync)?; + pub async fn load_async(server: &str, email: &str) -> Result { + let mut fh = + tokio::fs::File::open(crate::dirs::db_file(server, email)) + .await + .context(crate::error::LoadDbAsync)?; let mut json = String::new(); fh.read_to_string(&mut json) .await @@ -67,8 +68,8 @@ impl Db { } // XXX need to make this atomic - pub fn save(&self, email: &str) -> Result<()> { - let filename = crate::dirs::db_file(email); + pub fn save(&self, server: &str, email: &str) -> Result<()> { + let filename = crate::dirs::db_file(server, email); // unwrap is safe here because Self::filename is explicitly // constructed as a filename in a directory std::fs::create_dir_all(filename.parent().unwrap()) @@ -85,8 +86,8 @@ impl Db { } // XXX need to make this atomic - pub async fn save_async(&self, email: &str) -> Result<()> { - let filename = crate::dirs::db_file(email); + pub async fn save_async(&self, server: &str, email: &str) -> Result<()> { + let filename = crate::dirs::db_file(server, email); // unwrap is safe here because Self::filename is explicitly // constructed as a filename in a directory tokio::fs::create_dir_all(filename.parent().unwrap()) @@ -105,8 +106,8 @@ impl Db { Ok(()) } - pub fn remove(email: &str) -> Result<()> { - let filename = crate::dirs::db_file(email); + pub fn remove(server: &str, email: &str) -> Result<()> { + let filename = crate::dirs::db_file(server, email); let res = std::fs::remove_file(filename); if let Err(e) = &res { if e.kind() == std::io::ErrorKind::NotFound { -- cgit v1.2.3-54-g00ecf