132 lines
4.1 KiB
Rust
132 lines
4.1 KiB
Rust
//! Where Medlar keeps its files.
|
|
|
|
use std::ffi::OsString;
|
|
use std::io;
|
|
use std::os::unix::fs::DirBuilderExt;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// A file with this name beside the binary turns portable mode on.
|
|
pub const PORTABLE_MARK: &str = "portable";
|
|
/// The folder beside the binary that holds everything in portable mode.
|
|
pub const PORTABLE_DIR: &str = "medlar-data";
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct Paths {
|
|
pub config: PathBuf,
|
|
pub data: PathBuf,
|
|
/// Logs.
|
|
pub state: PathBuf,
|
|
pub cache: PathBuf,
|
|
pub portable: bool,
|
|
}
|
|
|
|
impl Paths {
|
|
/// `MEDLAR_HOME` wins, then portable mode, then the XDG folders.
|
|
pub fn detect() -> Paths {
|
|
Paths::choose(std::env::var_os("MEDLAR_HOME"), binary())
|
|
}
|
|
|
|
fn choose(home: Option<OsString>, binary: Option<PathBuf>) -> Paths {
|
|
if let Some(home) = home.filter(|h| !h.is_empty()) {
|
|
return Paths::under(Path::new(&home), false);
|
|
}
|
|
if let Some(dir) = binary.as_deref().and_then(Path::parent)
|
|
&& dir.join(PORTABLE_MARK).exists()
|
|
{
|
|
return Paths::under(&dir.join(PORTABLE_DIR), true);
|
|
}
|
|
// Every XDG folder is missing only when there is no home at all.
|
|
let base = |dir: Option<PathBuf>, name: &str| match dir {
|
|
Some(dir) => dir.join("medlar"),
|
|
None => std::env::temp_dir().join("medlar").join(name),
|
|
};
|
|
Paths {
|
|
config: base(dirs::config_dir(), "config"),
|
|
data: base(dirs::data_dir(), "data"),
|
|
state: base(dirs::state_dir(), "state"),
|
|
cache: base(dirs::cache_dir(), "cache"),
|
|
portable: false,
|
|
}
|
|
}
|
|
|
|
fn under(root: &Path, portable: bool) -> Paths {
|
|
Paths {
|
|
config: root.join("config"),
|
|
data: root.join("data"),
|
|
state: root.join("state"),
|
|
cache: root.join("cache"),
|
|
portable,
|
|
}
|
|
}
|
|
|
|
/// Makes the folders, readable by this user only.
|
|
pub fn create(&self) -> io::Result<()> {
|
|
for dir in [&self.config, &self.data, &self.state, &self.cache] {
|
|
std::fs::DirBuilder::new().recursive(true).mode(0o700).create(dir)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn settings(&self) -> PathBuf {
|
|
self.config.join("settings.json")
|
|
}
|
|
|
|
pub fn database(&self) -> PathBuf {
|
|
self.data.join("medlar.db")
|
|
}
|
|
|
|
/// The keys of SSH servers.
|
|
pub fn known_hosts(&self) -> PathBuf {
|
|
self.data.join("known_hosts")
|
|
}
|
|
|
|
pub fn log(&self) -> PathBuf {
|
|
self.state.join("medlar.log")
|
|
}
|
|
|
|
/// Where the user drops language files.
|
|
pub fn languages(&self) -> PathBuf {
|
|
self.config.join("languages")
|
|
}
|
|
}
|
|
|
|
/// The running binary, or the AppImage it was mounted from.
|
|
fn binary() -> Option<PathBuf> {
|
|
let exe = std::env::current_exe().ok()?;
|
|
// Another AppImage that started us leaves its own variables behind.
|
|
let ours = std::env::var_os("APPDIR").is_some_and(|dir| !dir.is_empty() && exe.starts_with(dir));
|
|
match std::env::var_os("APPIMAGE") {
|
|
Some(image) if ours => Some(image.into()),
|
|
_ => Some(exe),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn home_override_wins_over_portable() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
std::fs::write(dir.path().join(PORTABLE_MARK), "").unwrap();
|
|
let exe = Some(dir.path().join("medlar"));
|
|
let p = Paths::choose(Some("/x".into()), exe);
|
|
assert_eq!(p.database(), Path::new("/x/data/medlar.db"));
|
|
assert!(!p.portable);
|
|
}
|
|
|
|
#[test]
|
|
fn portable_file_keeps_everything_beside_the_binary() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let exe = Some(dir.path().join("medlar"));
|
|
assert!(!Paths::choose(None, exe.clone()).portable);
|
|
std::fs::write(dir.path().join(PORTABLE_MARK), "").unwrap();
|
|
let p = Paths::choose(Some("".into()), exe);
|
|
assert!(p.portable);
|
|
for path in [&p.config, &p.data, &p.state, &p.cache] {
|
|
assert!(path.starts_with(dir.path().join(PORTABLE_DIR)));
|
|
}
|
|
p.create().unwrap();
|
|
assert!(p.state.is_dir());
|
|
}
|
|
}
|