cyfs_util/util/
path_util.rs

1use std::path::{Path, PathBuf};
2
3pub const CFYS_ROOT_NAME: &str = "cyfs";
4
5pub fn default_cyfs_root_path() -> PathBuf {
6    #[cfg(target_os = "windows")]
7    {
8        PathBuf::from(&format!("C:\\{}", CFYS_ROOT_NAME))
9    }
10
11    #[cfg(any(target_os = "linux", target_os = "android", target_os = "ios"))]
12    {
13        PathBuf::from(&format!("/{}", CFYS_ROOT_NAME))
14    }
15
16    #[cfg(target_os = "macos")]
17    {
18        match dirs::data_dir() {
19            Some(dir) => {
20                let root = dir.join(&format!("../{}", CFYS_ROOT_NAME));
21                if root.is_dir() {
22                    root.canonicalize().unwrap()
23                } else {
24                    root
25                }
26            }
27            None => {
28                error!("get user dir failed!");
29                PathBuf::from(&format!("/{}", CFYS_ROOT_NAME))
30            }
31        }
32    }
33
34    #[cfg(target_arch = "wasm32")]
35    {
36        PathBuf::new()
37    }
38}
39
40static CYFS_ROOT: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
41
42// 初始化时候调用一次
43pub fn bind_cyfs_root_path(root_path: impl Into<PathBuf>) {
44    let root_path: PathBuf = root_path.into();
45    println!("bind cyfs_root dir: {}", root_path.display());
46
47    match CYFS_ROOT.set(root_path.clone()) {
48        Ok(_) => {
49            info!("change cyfs root path to: {}", root_path.display());
50            if !root_path.is_dir() {
51                if let Err(e) = std::fs::create_dir_all(&root_path) {
52                    error!(
53                        "create cyfs root dir failed! dir={}, err={}",
54                        root_path.display(),
55                        e
56                    );
57                }
58            }
59        }
60        Err(_) => {
61            unreachable!("change cyfs root after been inited! {}", root_path.display());
62        }
63    }
64}
65
66pub fn get_cyfs_root_path_ref() -> &'static Path {
67    CYFS_ROOT.get_or_init(|| {
68        let path = default_cyfs_root_path();
69        if !path.is_dir() {
70            if let Err(e) = std::fs::create_dir_all(&path) {
71                error!(
72                    "create cyfs root dir failed! dir={}, err={}",
73                    path.display(),
74                    e
75                );
76            }
77        }
78        path
79    })
80}
81
82pub fn get_cyfs_root_path() -> PathBuf {
83    get_cyfs_root_path_ref().to_owned()
84}
85
86pub fn get_temp_path() -> PathBuf {
87    let tmp = get_cyfs_root_path().join("tmp");
88    if let Err(e) = std::fs::create_dir_all(&tmp) {
89        error!("create tmp dir failed! dir={}, err={}", tmp.display(), e);
90    }
91
92    tmp
93}
94
95pub fn get_log_dir(service_name: &str) -> PathBuf {
96    return get_cyfs_root_path().join("log").join(service_name);
97}
98
99pub fn get_app_log_dir(app_name: &str) -> PathBuf {
100    let mut path = get_cyfs_root_path();
101    path.push("log");
102    path.push("app");
103    path.push(app_name);
104    path
105}
106
107pub fn get_app_dir(app_id: &str) -> PathBuf {
108    get_cyfs_root_path().join("app").join(app_id)
109}
110
111pub fn get_app_web_dir(app_id: &str) -> PathBuf {
112    get_cyfs_root_path().join("app").join("web").join(app_id)
113}
114
115pub fn get_app_web_dir2(app_id: &str, version: &str) -> PathBuf {
116    get_cyfs_root_path().join("app").join("web2").join(app_id).join(version)
117}
118
119pub fn get_app_acl_dir(app_id: &str) -> PathBuf {
120    get_cyfs_root_path().join("app").join("acl").join(app_id)
121}
122
123pub fn get_app_dep_dir(app_id: &str) -> PathBuf {
124    get_cyfs_root_path()
125        .join("app")
126        .join("dependent")
127        .join(app_id)
128}
129
130pub fn get_app_dockerfile_dir(app_id: &str) -> PathBuf {
131    get_cyfs_root_path()
132        .join("app")
133        .join("dockerfile")
134        .join(app_id)
135}
136
137pub fn get_app_data_dir(app_name: &str) -> PathBuf {
138    let mut base_dir = get_cyfs_root_path();
139    base_dir.push("data");
140    base_dir.push("app");
141    base_dir.push(app_name);
142    if let Err(e) = std::fs::create_dir_all(&base_dir) {
143        error!(
144            "create app data dir failed! dir={}, err={}",
145            base_dir.display(),
146            e
147        );
148    }
149
150    base_dir
151}
152
153pub fn get_app_data_dir_ex(app_name: &str, base: &Path) -> PathBuf {
154    let mut base_dir = base.to_owned();
155    base_dir.push("data");
156    base_dir.push("app");
157    base_dir.push(app_name);
158    base_dir
159}
160
161// {CFYS_ROOT_NAME}/etc/{service_name}/
162pub fn get_service_config_dir(service_name: &str) -> PathBuf {
163    let mut base_dir = get_cyfs_root_path();
164    base_dir.push("etc");
165    base_dir.push(service_name);
166    base_dir
167}
168
169pub fn get_service_data_dir(service_name: &str) -> PathBuf {
170    let mut base_dir = get_cyfs_root_path();
171    base_dir.push("data");
172    base_dir.push(service_name);
173    if let Err(e) = std::fs::create_dir_all(&base_dir) {
174        error!(
175            "create service data dir failed! dir={}, err={}",
176            base_dir.display(),
177            e
178        );
179    }
180    base_dir
181}
182
183pub fn get_named_data_root(isolate: &str) -> PathBuf {
184    let mut base_dir = get_cyfs_root_path();
185    base_dir.push("data");
186    if isolate.len() > 0 {
187        base_dir.push(isolate)
188    }
189    base_dir.push("named-data-cache");
190
191    if !base_dir.is_dir() {
192        if let Err(e) = std::fs::create_dir_all(&base_dir) {
193            error!(
194                "create bdt storage dir failed! dir={}, err={}",
195                base_dir.display(),
196                e
197            );
198        } else {
199            info!(
200                "create named-data-cache dir success! {}",
201                base_dir.display()
202            );
203        }
204    }
205
206    base_dir
207}
208
209/*
210pub fn get_bdt_named_data_peer_root(desc_name: &str) -> PathBuf {
211    let mut base_dir = get_cyfs_root_path();
212    base_dir.push("bdt");
213    base_dir.push("named_data_cache");
214    base_dir.push("chunk");
215    base_dir.push(desc_name);
216
217    if let Err(e) = std::fs::create_dir_all(&base_dir) {
218        error!(
219            "create bdt storage dir failed! dir={}, err={}",
220            base_dir.display(),
221            e
222        );
223    }
224
225    base_dir
226}
227*/