aboutsummaryrefslogtreecommitdiffstats
path: root/src/dirs.rs
blob: e838e12d2da131bbf56873333083df41387ac4be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::prelude::*;
use std::os::unix::fs::PermissionsExt as _;

pub fn make_all() -> Result<()> {
    let cache_dir = cache_dir();
    std::fs::create_dir_all(&cache_dir).map_err(|source| {
        Error::CreateDirectory {
            source,
            file: cache_dir,
        }
    })?;

    let runtime_dir = runtime_dir();
    std::fs::create_dir_all(&runtime_dir).map_err(|source| {
        Error::CreateDirectory {
            source,
            file: runtime_dir.clone(),
        }
    })?;
    std::fs::set_permissions(
        &runtime_dir,
        std::fs::Permissions::from_mode(0o700),
    )
    .map_err(|source| Error::CreateDirectory {
        source,
        file: runtime_dir,
    })?;

    let data_dir = data_dir();
    std::fs::create_dir_all(&data_dir).map_err(|source| {
        Error::CreateDirectory {
            source,
            file: data_dir,
        }
    })?;

    Ok(())
}

#[must_use]
pub fn config_file() -> std::path::PathBuf {
    config_dir().join("config.json")
}

const INVALID_PATH: &percent_encoding::AsciiSet =
    &percent_encoding::CONTROLS.add(b'/').add(b'%').add(b':');
#[must_use]
pub fn db_file(server: &str, email: &str) -> std::path::PathBuf {
    let server =
        percent_encoding::percent_encode(server.as_bytes(), INVALID_PATH)
            .to_string();
    cache_dir().join(format!("{server}:{email}.json"))
}

#[must_use]
pub fn pid_file() -> std::path::PathBuf {
    runtime_dir().join("pidfile")
}

#[must_use]
pub fn agent_stdout_file() -> std::path::PathBuf {
    data_dir().join("agent.out")
}

#[must_use]
pub fn agent_stderr_file() -> std::path::PathBuf {
    data_dir().join("agent.err")
}

#[must_use]
pub fn device_id_file() -> std::path::PathBuf {
    data_dir().join("device_id")
}

#[must_use]
pub fn socket_file() -> std::path::PathBuf {
    runtime_dir().join("socket")
}

#[must_use]
fn config_dir() -> std::path::PathBuf {
    let project_dirs =
        directories::ProjectDirs::from("", "", &profile()).unwrap();
    project_dirs.config_dir().to_path_buf()
}

#[must_use]
fn cache_dir() -> std::path::PathBuf {
    let project_dirs =
        directories::ProjectDirs::from("", "", &profile()).unwrap();
    project_dirs.cache_dir().to_path_buf()
}

#[must_use]
fn data_dir() -> std::path::PathBuf {
    let project_dirs =
        directories::ProjectDirs::from("", "", &profile()).unwrap();
    project_dirs.data_dir().to_path_buf()
}

#[must_use]
fn runtime_dir() -> std::path::PathBuf {
    let project_dirs =
        directories::ProjectDirs::from("", "", &profile()).unwrap();
    project_dirs.runtime_dir().map_or_else(
        || {
            format!(
                "{}/{}-{}",
                std::env::temp_dir().to_string_lossy(),
                &profile(),
                nix::unistd::getuid().as_raw()
            )
            .into()
        },
        std::path::Path::to_path_buf,
    )
}

#[must_use]
pub fn profile() -> String {
    match std::env::var("RBW_PROFILE") {
        Ok(profile) if !profile.is_empty() => format!("rbw-{profile}"),
        _ => "rbw".to_string(),
    }
}