Skip to main content

gled_core/
paths.rs

1//! Where gled keeps its files. The desktop app uses the user's directories;
2//! the server is given one data directory and keeps everything below it.
3
4use std::{path::PathBuf, sync::OnceLock};
5
6use directories::BaseDirs;
7
8static DATA_DIR: OnceLock<PathBuf> = OnceLock::new();
9
10/// Keeps every file below `dir` from now on. Call before the services start;
11/// later calls are ignored. Returns `false` if a directory was already set.
12pub fn set_data_dir(dir: PathBuf) -> bool {
13    DATA_DIR.set(dir).is_ok()
14}
15
16/// The asset repository.
17pub(crate) fn storage_dir() -> PathBuf {
18    match DATA_DIR.get() {
19        Some(dir) => dir.join("assets"),
20        None => BaseDirs::new()
21            .map(|dirs| dirs.data_dir().join("gled2"))
22            .unwrap_or_else(|| PathBuf::from("gled2")),
23    }
24}
25
26/// The TLS certificate and key of the web server.
27pub fn tls_dir() -> PathBuf {
28    match DATA_DIR.get() {
29        Some(dir) => dir.join("tls"),
30        None => BaseDirs::new()
31            .map(|dirs| dirs.config_dir().join("gled").join("tls"))
32            .unwrap_or_else(|| PathBuf::from("gled-tls")),
33    }
34}
35
36/// Which GPU gled chose for which set of GPUs.
37pub(crate) fn gpu_choice_file() -> Option<PathBuf> {
38    match DATA_DIR.get() {
39        Some(dir) => Some(dir.join("gpu_choice.json")),
40        None => BaseDirs::new().map(|dirs| dirs.config_dir().join("gled").join("gpu_choice.json")),
41    }
42}
43
44/// The certificate fingerprints of the servers this machine connected to.
45pub fn known_servers_file() -> Option<PathBuf> {
46    BaseDirs::new().map(|dirs| dirs.config_dir().join("gled").join("known_servers.json"))
47}
48
49/// The machine's own settings, which are not part of any project.
50pub(crate) fn persistent_state_file() -> Option<PathBuf> {
51    match DATA_DIR.get() {
52        Some(dir) => Some(dir.join("settings.json")),
53        None => home::home_dir().map(|path| path.join(".gled")),
54    }
55}