1use std::path::{Path, PathBuf};
2
3pub struct ZLayerDirs {
7 data_dir: PathBuf,
8}
9
10impl ZLayerDirs {
11 pub fn new(data_dir: impl Into<PathBuf>) -> Self {
13 Self {
14 data_dir: data_dir.into(),
15 }
16 }
17
18 pub fn system_default() -> Self {
20 Self::new(Self::default_data_dir())
21 }
22
23 pub fn default_data_dir() -> PathBuf {
32 #[cfg(target_os = "macos")]
33 {
34 home_dir_or_tmp().join(".zlayer")
35 }
36 #[cfg(target_os = "windows")]
37 {
38 if let Some(local_app_data) = std::env::var_os("LOCALAPPDATA") {
39 PathBuf::from(local_app_data).join("ZLayer")
40 } else {
41 PathBuf::from(r"C:\ProgramData\ZLayer")
42 }
43 }
44 #[cfg(not(any(target_os = "macos", target_os = "windows")))]
45 {
46 if is_root() {
47 PathBuf::from("/var/lib/zlayer")
48 } else {
49 home_dir_or_tmp().join(".zlayer")
50 }
51 }
52 }
53
54 pub fn default_run_dir() -> PathBuf {
60 #[cfg(not(any(target_os = "macos", target_os = "windows")))]
61 {
62 PathBuf::from("/var/run/zlayer")
63 }
64 #[cfg(any(target_os = "macos", target_os = "windows"))]
65 {
66 Self::default_data_dir().join("run")
67 }
68 }
69
70 pub fn default_log_dir() -> PathBuf {
76 #[cfg(not(any(target_os = "macos", target_os = "windows")))]
77 {
78 PathBuf::from("/var/log/zlayer")
79 }
80 #[cfg(any(target_os = "macos", target_os = "windows"))]
81 {
82 Self::default_data_dir().join("logs")
83 }
84 }
85
86 pub fn default_socket_path() -> String {
92 #[cfg(target_os = "windows")]
93 {
94 "tcp://127.0.0.1:3669".to_string()
95 }
96 #[cfg(not(target_os = "windows"))]
97 {
98 #[cfg(target_os = "macos")]
99 {
100 Self::default_data_dir()
101 .join("run")
102 .join("zlayer.sock")
103 .to_string_lossy()
104 .into_owned()
105 }
106 #[cfg(not(target_os = "macos"))]
107 {
108 "/var/run/zlayer.sock".to_string()
109 }
110 }
111 }
112
113 pub fn data_dir(&self) -> &Path {
117 &self.data_dir
118 }
119
120 pub fn containers(&self) -> PathBuf {
122 self.data_dir.join("containers")
123 }
124
125 pub fn rootfs(&self) -> PathBuf {
127 self.data_dir.join("rootfs")
128 }
129
130 pub fn bundles(&self) -> PathBuf {
132 self.data_dir.join("bundles")
133 }
134
135 pub fn cache(&self) -> PathBuf {
137 self.data_dir.join("cache")
138 }
139
140 pub fn volumes(&self) -> PathBuf {
142 self.data_dir.join("volumes")
143 }
144
145 pub fn wasm(&self) -> PathBuf {
147 self.data_dir.join("wasm")
148 }
149
150 pub fn wasm_compiled(&self) -> PathBuf {
152 self.data_dir.join("wasm").join("compiled")
153 }
154
155 pub fn secrets(&self) -> PathBuf {
157 self.data_dir.join("secrets")
158 }
159
160 pub fn certs(&self) -> PathBuf {
162 self.data_dir.join("certs")
163 }
164
165 pub fn raft(&self) -> PathBuf {
167 self.data_dir.join("raft")
168 }
169
170 pub fn admin_password(&self) -> PathBuf {
172 self.data_dir.join("admin_password")
173 }
174
175 pub fn daemon_json(&self) -> PathBuf {
177 self.data_dir.join("daemon.json")
178 }
179
180 pub fn logs(&self) -> PathBuf {
183 self.data_dir.join("logs")
184 }
185
186 pub fn vms(&self) -> PathBuf {
190 self.data_dir.join("vms")
191 }
192
193 pub fn images(&self) -> PathBuf {
195 self.data_dir.join("images")
196 }
197
198 pub fn bin(&self) -> PathBuf {
200 self.data_dir.join("bin")
201 }
202
203 pub fn toolchain_cache(&self) -> PathBuf {
205 self.data_dir.join("toolchain-cache")
206 }
207
208 pub fn tmp(&self) -> PathBuf {
210 self.data_dir.join("tmp")
211 }
212}
213
214fn home_dir_or_tmp() -> PathBuf {
217 std::env::var_os("HOME")
218 .map(PathBuf::from)
219 .unwrap_or_else(|| PathBuf::from("/tmp"))
220}
221
222#[cfg(not(any(target_os = "macos", target_os = "windows")))]
223fn is_root() -> bool {
224 #[cfg(unix)]
225 {
226 nix::unistd::geteuid().is_root()
227 }
228 #[cfg(not(unix))]
229 {
230 false
231 }
232}
233
234#[cfg(test)]
235mod tests {
236 use super::*;
237
238 #[test]
239 fn subdirectories_are_relative_to_data_dir() {
240 let dirs = ZLayerDirs::new("/test/data");
241 assert_eq!(dirs.containers(), PathBuf::from("/test/data/containers"));
242 assert_eq!(dirs.rootfs(), PathBuf::from("/test/data/rootfs"));
243 assert_eq!(dirs.bundles(), PathBuf::from("/test/data/bundles"));
244 assert_eq!(dirs.cache(), PathBuf::from("/test/data/cache"));
245 assert_eq!(dirs.volumes(), PathBuf::from("/test/data/volumes"));
246 assert_eq!(dirs.wasm(), PathBuf::from("/test/data/wasm"));
247 assert_eq!(
248 dirs.wasm_compiled(),
249 PathBuf::from("/test/data/wasm/compiled")
250 );
251 assert_eq!(dirs.secrets(), PathBuf::from("/test/data/secrets"));
252 assert_eq!(dirs.certs(), PathBuf::from("/test/data/certs"));
253 assert_eq!(dirs.raft(), PathBuf::from("/test/data/raft"));
254 assert_eq!(
255 dirs.admin_password(),
256 PathBuf::from("/test/data/admin_password")
257 );
258 assert_eq!(dirs.daemon_json(), PathBuf::from("/test/data/daemon.json"));
259 assert_eq!(dirs.logs(), PathBuf::from("/test/data/logs"));
260 assert_eq!(dirs.vms(), PathBuf::from("/test/data/vms"));
261 assert_eq!(dirs.images(), PathBuf::from("/test/data/images"));
262 assert_eq!(dirs.bin(), PathBuf::from("/test/data/bin"));
263 assert_eq!(
264 dirs.toolchain_cache(),
265 PathBuf::from("/test/data/toolchain-cache")
266 );
267 assert_eq!(dirs.tmp(), PathBuf::from("/test/data/tmp"));
268 }
269
270 #[test]
271 fn system_default_uses_default_data_dir() {
272 let dirs = ZLayerDirs::system_default();
273 assert_eq!(dirs.data_dir(), ZLayerDirs::default_data_dir().as_path());
274 }
275}