aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-05-03 03:30:59 -0400
committerJesse Luehrs <doy@tozt.net>2020-05-03 03:30:59 -0400
commit6ebf7d55e4c553870306a70092cfb677c17429b9 (patch)
tree2d20288e44fa627159d0a4e578f0fb506d16f921
parent36b5107239b75136ce74799d87a645e153d95948 (diff)
downloadrbw-6ebf7d55e4c553870306a70092cfb677c17429b9.tar.gz
rbw-6ebf7d55e4c553870306a70092cfb677c17429b9.zip
simplify
-rw-r--r--src/bin/rbw-agent/daemon.rs15
-rw-r--r--src/bin/rbw-agent/sock.rs6
-rw-r--r--src/bin/rbw/actions.rs3
-rw-r--r--src/bin/rbw/sock.rs2
-rw-r--r--src/config.rs10
-rw-r--r--src/db.rs14
-rw-r--r--src/dirs.rs51
-rw-r--r--src/error.rs3
8 files changed, 61 insertions, 43 deletions
diff --git a/src/bin/rbw-agent/daemon.rs b/src/bin/rbw-agent/daemon.rs
index f87d0ec..923a217 100644
--- a/src/bin/rbw-agent/daemon.rs
+++ b/src/bin/rbw-agent/daemon.rs
@@ -1,5 +1,3 @@
-use anyhow::Context as _;
-
pub struct StartupAck {
writer: std::os::unix::io::RawFd,
}
@@ -20,25 +18,20 @@ impl Drop for StartupAck {
}
pub fn daemonize() -> anyhow::Result<StartupAck> {
- let runtime_dir = rbw::dirs::runtime_dir();
- std::fs::create_dir_all(&runtime_dir)
- .context("failed to create runtime directory")?;
+ rbw::dirs::make_all()?;
- let data_dir = rbw::dirs::data_dir();
- std::fs::create_dir_all(&data_dir)
- .context("failed to create data directory")?;
let stdout = std::fs::OpenOptions::new()
.append(true)
.create(true)
- .open(data_dir.join("agent.out"))?;
+ .open(rbw::dirs::agent_stdout_file())?;
let stderr = std::fs::OpenOptions::new()
.append(true)
.create(true)
- .open(data_dir.join("agent.err"))?;
+ .open(rbw::dirs::agent_stderr_file())?;
let (r, w) = nix::unistd::pipe()?;
let res = daemonize::Daemonize::new()
- .pid_file(runtime_dir.join("pidfile"))
+ .pid_file(rbw::dirs::pid_file())
.stdout(stdout)
.stderr(stderr)
.exit_action(move || {
diff --git a/src/bin/rbw-agent/sock.rs b/src/bin/rbw-agent/sock.rs
index bb2d6fb..311176c 100644
--- a/src/bin/rbw-agent/sock.rs
+++ b/src/bin/rbw-agent/sock.rs
@@ -43,11 +43,7 @@ impl Sock {
}
pub fn listen() -> anyhow::Result<tokio::net::UnixListener> {
- let runtime_dir = rbw::dirs::runtime_dir();
- std::fs::create_dir_all(&runtime_dir)
- .context("failed to create runtime dir")?;
-
- let path = runtime_dir.join("socket");
+ let path = rbw::dirs::socket_file();
// if the socket already doesn't exist, that's fine
let _ = std::fs::remove_file(&path);
let sock = tokio::net::UnixListener::bind(&path)
diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs
index 6de2fc6..01f448f 100644
--- a/src/bin/rbw/actions.rs
+++ b/src/bin/rbw/actions.rs
@@ -20,8 +20,7 @@ pub fn lock() -> anyhow::Result<()> {
pub fn quit() -> anyhow::Result<()> {
match crate::sock::Sock::connect() {
Ok(mut sock) => {
- let runtime_dir = rbw::dirs::runtime_dir();
- let pidfile = runtime_dir.join("pidfile");
+ let pidfile = rbw::dirs::pid_file();
let mut pid = String::new();
std::fs::File::open(pidfile)?.read_to_string(&mut pid)?;
let pid = nix::unistd::Pid::from_raw(pid.parse()?);
diff --git a/src/bin/rbw/sock.rs b/src/bin/rbw/sock.rs
index 1ba13ec..b3ff1d6 100644
--- a/src/bin/rbw/sock.rs
+++ b/src/bin/rbw/sock.rs
@@ -6,7 +6,7 @@ pub struct Sock(std::os::unix::net::UnixStream);
impl Sock {
pub fn connect() -> std::io::Result<Self> {
Ok(Self(std::os::unix::net::UnixStream::connect(
- rbw::dirs::runtime_dir().join("socket"),
+ rbw::dirs::socket_file(),
)?))
}
diff --git a/src/config.rs b/src/config.rs
index 55f6806..5b3cfd3 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -22,7 +22,7 @@ impl Config {
}
pub fn load() -> Result<Self> {
- let mut fh = std::fs::File::open(Self::filename())
+ let mut fh = std::fs::File::open(crate::dirs::config_file())
.context(crate::error::LoadConfig)?;
let mut json = String::new();
fh.read_to_string(&mut json)
@@ -33,7 +33,7 @@ impl Config {
}
pub async fn load_async() -> Result<Self> {
- let mut fh = tokio::fs::File::open(Self::filename())
+ let mut fh = tokio::fs::File::open(crate::dirs::config_file())
.await
.context(crate::error::LoadConfigAsync)?;
let mut json = String::new();
@@ -46,7 +46,7 @@ impl Config {
}
pub fn save(&self) -> Result<()> {
- let filename = Self::filename();
+ let filename = crate::dirs::config_file();
// unwrap is safe here because Self::filename is explicitly
// constructed as a filename in a directory
std::fs::create_dir_all(filename.parent().unwrap())
@@ -77,8 +77,4 @@ impl Config {
)
})
}
-
- fn filename() -> std::path::PathBuf {
- crate::dirs::config_dir().join("config.json")
- }
}
diff --git a/src/db.rs b/src/db.rs
index 5f200ab..b61f1bd 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -44,7 +44,7 @@ impl Db {
}
pub fn load(email: &str) -> Result<Self> {
- let mut fh = std::fs::File::open(Self::filename(email))
+ let mut fh = std::fs::File::open(crate::dirs::db_file(email))
.context(crate::error::LoadDb)?;
let mut json = String::new();
fh.read_to_string(&mut json).context(crate::error::LoadDb)?;
@@ -54,7 +54,7 @@ impl Db {
}
pub async fn load_async(email: &str) -> Result<Self> {
- let mut fh = tokio::fs::File::open(Self::filename(email))
+ let mut fh = tokio::fs::File::open(crate::dirs::db_file(email))
.await
.context(crate::error::LoadDbAsync)?;
let mut json = String::new();
@@ -68,7 +68,7 @@ impl Db {
// XXX need to make this atomic
pub fn save(&self, email: &str) -> Result<()> {
- let filename = Self::filename(email);
+ let filename = crate::dirs::db_file(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())
@@ -86,7 +86,7 @@ impl Db {
// XXX need to make this atomic
pub async fn save_async(&self, email: &str) -> Result<()> {
- let filename = Self::filename(email);
+ let filename = crate::dirs::db_file(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())
@@ -106,7 +106,7 @@ impl Db {
}
pub fn remove(email: &str) -> Result<()> {
- let filename = Self::filename(email);
+ let filename = crate::dirs::db_file(email);
let res = std::fs::remove_file(filename);
if let Err(e) = &res {
if e.kind() == std::io::ErrorKind::NotFound {
@@ -123,8 +123,4 @@ impl Db {
|| self.iterations.is_none()
|| self.protected_key.is_none()
}
-
- fn filename(email: &str) -> std::path::PathBuf {
- crate::dirs::cache_dir().join(format!("{}.json", email))
- }
}
diff --git a/src/dirs.rs b/src/dirs.rs
index fe610a7..5dd2c35 100644
--- a/src/dirs.rs
+++ b/src/dirs.rs
@@ -1,23 +1,58 @@
-#[must_use]
-pub fn config_dir() -> std::path::PathBuf {
+use crate::prelude::*;
+
+pub fn make_all() -> Result<()> {
+ std::fs::create_dir_all(&cache_dir())
+ .context(crate::error::CreateDirectory)?;
+
+ std::fs::create_dir_all(&runtime_dir())
+ .context(crate::error::CreateDirectory)?;
+
+ std::fs::create_dir_all(&data_dir())
+ .context(crate::error::CreateDirectory)?;
+
+ Ok(())
+}
+
+pub fn config_file() -> std::path::PathBuf {
+ config_dir().join("config.json")
+}
+
+pub fn db_file(email: &str) -> std::path::PathBuf {
+ cache_dir().join(format!("{}.json", email))
+}
+
+pub fn pid_file() -> std::path::PathBuf {
+ runtime_dir().join("pidfile")
+}
+
+pub fn agent_stdout_file() -> std::path::PathBuf {
+ data_dir().join("agent.out")
+}
+
+pub fn agent_stderr_file() -> std::path::PathBuf {
+ data_dir().join("agent.err")
+}
+
+pub fn socket_file() -> std::path::PathBuf {
+ runtime_dir().join("socket")
+}
+
+fn config_dir() -> std::path::PathBuf {
let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap();
project_dirs.config_dir().to_path_buf()
}
-#[must_use]
-pub fn cache_dir() -> std::path::PathBuf {
+fn cache_dir() -> std::path::PathBuf {
let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap();
project_dirs.cache_dir().to_path_buf()
}
-#[must_use]
-pub fn data_dir() -> std::path::PathBuf {
+fn data_dir() -> std::path::PathBuf {
let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap();
project_dirs.data_dir().to_path_buf()
}
-#[must_use]
-pub fn runtime_dir() -> std::path::PathBuf {
+fn runtime_dir() -> std::path::PathBuf {
let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap();
project_dirs.runtime_dir().unwrap().to_path_buf()
}
diff --git a/src/error.rs b/src/error.rs
index 8517581..a6e6a84 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -6,6 +6,9 @@ pub enum Error {
source: block_modes::InvalidKeyIvLength,
},
+ #[snafu(display("failed to create directory: {}", source))]
+ CreateDirectory { source: std::io::Error },
+
#[snafu(display("failed to decrypt: {}", source))]
Decrypt { source: block_modes::BlockModeError },