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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::path::{Path, PathBuf};
use std::sync::Mutex;


pub const CFYS_ROOT_NAME: &str = "cyfs";

fn default_cyfs_root_path() -> PathBuf {
    #[cfg(target_os = "windows")]
    {
        PathBuf::from(&format!("C:\\{}", CFYS_ROOT_NAME))
    }

    #[cfg(any(target_os = "linux", target_os = "android", target_os = "ios"))]
    {
        PathBuf::from(&format!("/{}", CFYS_ROOT_NAME))
    }

    #[cfg(target_os = "macos")]
    {
        match dirs::data_dir() {
            Some(dir) => {
                let root = dir.join(&format!("../{}", CFYS_ROOT_NAME));
                if root.is_dir() {
                    root.canonicalize().unwrap()
                } else {
                    root
                }
            }
            None => {
                error!("get user dir failed!");
                PathBuf::from(&format!("/{}", CFYS_ROOT_NAME))
            }
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        PathBuf::new()
    }
}

lazy_static::lazy_static! {
    pub static ref CYFS_ROOT: Mutex<PathBuf> = {
        let path = default_cyfs_root_path();
        if !path.is_dir() {
            if let Err(e) = std::fs::create_dir_all(&path) {
                error!(
                    "create cyfs root dir failed! dir={}, err={}",
                    path.display(),
                    e
                );
            }
        }

        info!("cyfs root: {}", path.display());
        Mutex::new(PathBuf::from(path))
    };
}

// 初始化时候调用一次
pub fn bind_cyfs_root_path(root_path: impl Into<PathBuf>) {
    let root_path: PathBuf = root_path.into();
    info!("change cyfs root path to: {}", root_path.display());

    *CYFS_ROOT.lock().unwrap() = root_path;
}

pub fn get_cyfs_root_path() -> PathBuf {
    CYFS_ROOT.lock().unwrap().clone()
}

pub fn get_temp_path() -> PathBuf {
    let tmp = get_cyfs_root_path().join("tmp");
    if let Err(e) = std::fs::create_dir_all(&tmp) {
        error!("create tmp dir failed! dir={}, err={}", tmp.display(), e);
    }

    tmp
}

pub fn get_log_dir(service_name: &str) -> PathBuf {
    return get_cyfs_root_path().join("log").join(service_name);
}

pub fn get_app_log_dir(app_name: &str) -> PathBuf {
    let mut path = get_cyfs_root_path();
    path.push("log");
    path.push("app");
    path.push(app_name);
    path
}

pub fn get_app_dir(app_id: &str) -> PathBuf {
    get_cyfs_root_path().join("app").join(app_id)
}

pub fn get_app_web_dir(app_id: &str) -> PathBuf {
    get_cyfs_root_path().join("app").join("web").join(app_id)
}

pub fn get_app_acl_dir(app_id: &str) -> PathBuf {
    get_cyfs_root_path().join("app").join("acl").join(app_id)
}

pub fn get_app_dep_dir(app_id: &str, version: &str) -> PathBuf {
    get_cyfs_root_path()
        .join("app")
        .join("dependent")
        .join(app_id)
        .join(version)
}

pub fn get_app_dockerfile_dir(app_id: &str) -> PathBuf {
    get_cyfs_root_path()
        .join("app")
        .join("dockerfile")
        .join(app_id)
}

pub fn get_app_data_dir(app_name: &str) -> PathBuf {
    let mut base_dir = get_cyfs_root_path();
    base_dir.push("data");
    base_dir.push("app");
    base_dir.push(app_name);
    if let Err(e) = std::fs::create_dir_all(&base_dir) {
        error!(
            "create app data dir failed! dir={}, err={}",
            base_dir.display(),
            e
        );
    }

    base_dir
}

pub fn get_app_data_dir_ex(app_name: &str, base: &Path) -> PathBuf {
    let mut base_dir = base.to_owned();
    base_dir.push("data");
    base_dir.push("app");
    base_dir.push(app_name);
    base_dir
}

// {CFYS_ROOT_NAME}/etc/{service_name}/
pub fn get_service_config_dir(service_name: &str) -> PathBuf {
    let mut base_dir = get_cyfs_root_path();
    base_dir.push("etc");
    base_dir.push(service_name);
    base_dir
}

pub fn get_service_data_dir(service_name: &str) -> PathBuf {
    let mut base_dir = get_cyfs_root_path();
    base_dir.push("data");
    base_dir.push(service_name);
    if let Err(e) = std::fs::create_dir_all(&base_dir) {
        error!(
            "create service data dir failed! dir={}, err={}",
            base_dir.display(),
            e
        );
    }
    base_dir
}

pub fn get_named_data_root(isolate: &str) -> PathBuf {
    let mut base_dir = get_cyfs_root_path();
    base_dir.push("data");
    if isolate.len() > 0 {
        base_dir.push(isolate)
    }
    base_dir.push("named-data-cache");

    if !base_dir.is_dir() {
        if let Err(e) = std::fs::create_dir_all(&base_dir) {
            error!(
                "create bdt storage dir failed! dir={}, err={}",
                base_dir.display(),
                e
            );
        } else {
            info!(
                "create named-data-cache dir success! {}",
                base_dir.display()
            );
        }
    }

    base_dir
}

/*
pub fn get_bdt_named_data_peer_root(desc_name: &str) -> PathBuf {
    let mut base_dir = get_cyfs_root_path();
    base_dir.push("bdt");
    base_dir.push("named_data_cache");
    base_dir.push("chunk");
    base_dir.push(desc_name);

    if let Err(e) = std::fs::create_dir_all(&base_dir) {
        error!(
            "create bdt storage dir failed! dir={}, err={}",
            base_dir.display(),
            e
        );
    }

    base_dir
}
*/