|
10 | 10 | // See the License for the specific language governing permissions and |
11 | 11 | // limitations under the License. |
12 | 12 |
|
| 13 | +use std::env; |
13 | 14 | use std::fs; |
14 | | -use std::path::{Path, PathBuf}; |
| 15 | +use std::path::PathBuf; |
| 16 | +use std::sync::OnceLock; |
15 | 17 |
|
16 | | -fn get_exe_dir() -> Option<PathBuf> { |
17 | | - std::env::current_exe().ok()?.parent().map(PathBuf::from) |
| 18 | +pub const ENV_HOME: &str = "FUNCTION_STREAM_HOME"; |
| 19 | +pub const ENV_CONF: &str = "FUNCTION_STREAM_CONF"; |
| 20 | + |
| 21 | +static PROJECT_ROOT: OnceLock<PathBuf> = OnceLock::new(); |
| 22 | + |
| 23 | +pub fn get_project_root() -> &'static PathBuf { |
| 24 | + PROJECT_ROOT |
| 25 | + .get_or_init(|| resolve_project_root().expect("CRITICAL: Failed to resolve project root")) |
18 | 26 | } |
19 | 27 |
|
20 | | -fn search_paths<F>(mut check: F) -> Option<PathBuf> |
21 | | -where |
22 | | - F: FnMut(&Path) -> bool, |
23 | | -{ |
24 | | - let cwd = PathBuf::from("."); |
25 | | - if check(cwd.as_path()) { |
26 | | - return Some(cwd); |
| 28 | +fn resolve_project_root() -> std::io::Result<PathBuf> { |
| 29 | + if let Ok(home) = env::var(ENV_HOME) { |
| 30 | + let path = PathBuf::from(&home); |
| 31 | + return path.canonicalize().or(Ok(path)); |
27 | 32 | } |
28 | 33 |
|
29 | | - if let Some(exe_dir) = get_exe_dir() { |
30 | | - if check(exe_dir.as_path()) { |
31 | | - return Some(exe_dir); |
32 | | - } |
| 34 | + if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") { |
| 35 | + return Ok(PathBuf::from(manifest_dir)); |
| 36 | + } |
33 | 37 |
|
34 | | - if let Some(parent) = exe_dir.parent() { |
35 | | - if check(parent) { |
36 | | - return Some(parent.to_path_buf()); |
37 | | - } |
| 38 | + if let Ok(exe_path) = env::current_exe() { |
| 39 | + let mut path = exe_path; |
| 40 | + path.pop(); |
| 41 | + if path.file_name().map_or(false, |n| n == "bin") { |
| 42 | + path.pop(); |
38 | 43 | } |
| 44 | + return Ok(path); |
39 | 45 | } |
40 | 46 |
|
41 | | - None |
| 47 | + env::current_dir() |
42 | 48 | } |
43 | 49 |
|
44 | | -pub fn find_config_file(config_name: &str) -> Option<PathBuf> { |
45 | | - let config_path = PathBuf::from(config_name); |
46 | | - if config_path.exists() { |
47 | | - return Some(config_path); |
| 50 | +pub fn resolve_path(input_path: &str) -> PathBuf { |
| 51 | + let path = PathBuf::from(input_path); |
| 52 | + if path.is_absolute() { |
| 53 | + path |
| 54 | + } else { |
| 55 | + get_project_root().join(path) |
48 | 56 | } |
| 57 | +} |
49 | 58 |
|
50 | | - search_paths(|base| base.join(config_name).exists()) |
51 | | - .or_else(|| { |
52 | | - get_exe_dir().and_then(|exe_dir| { |
53 | | - exe_dir.parent().and_then(|parent| { |
54 | | - let path = parent.join("conf").join(config_name); |
55 | | - path.exists().then_some(path) |
56 | | - }) |
57 | | - }) |
58 | | - }) |
59 | | - .or_else(|| { |
60 | | - std::env::var("FUNCTION_STREAM_CONFIG") |
61 | | - .ok() |
62 | | - .map(PathBuf::from) |
63 | | - .filter(|p| p.exists()) |
64 | | - }) |
65 | | -} |
66 | | - |
67 | | -fn find_or_create_dir(dir_name: &str) -> std::io::Result<PathBuf> { |
68 | | - let cwd_dir = PathBuf::from(dir_name); |
69 | | - if cwd_dir.exists() { |
70 | | - return Ok(cwd_dir); |
71 | | - } |
| 59 | +fn to_absolute_path(input_path: &str) -> PathBuf { |
| 60 | + resolve_path(input_path) |
| 61 | +} |
72 | 62 |
|
73 | | - if let Some(exe_dir) = get_exe_dir() { |
74 | | - if let Some(parent) = exe_dir.parent() { |
75 | | - let dir = parent.join(dir_name); |
76 | | - if dir.exists() { |
77 | | - return Ok(dir); |
| 63 | +pub fn find_config_file(config_name: &str) -> Option<PathBuf> { |
| 64 | + if let Ok(conf_env) = env::var(ENV_CONF) { |
| 65 | + let path = to_absolute_path(&conf_env); |
| 66 | + if path.is_file() { |
| 67 | + return Some(path); |
| 68 | + } |
| 69 | + if path.is_dir() { |
| 70 | + let full = path.join(config_name); |
| 71 | + if full.exists() { |
| 72 | + return Some(full); |
78 | 73 | } |
79 | | - fs::create_dir_all(&dir)?; |
80 | | - return Ok(dir); |
81 | 74 | } |
| 75 | + } |
| 76 | + |
| 77 | + let search_paths = vec![ |
| 78 | + get_conf_dir().join(config_name), |
| 79 | + get_project_root().join(config_name), |
| 80 | + ]; |
82 | 81 |
|
83 | | - let dir = exe_dir.join(dir_name); |
84 | | - if dir.exists() { |
85 | | - return Ok(dir); |
| 82 | + for path in search_paths { |
| 83 | + if path.exists() { |
| 84 | + return Some(path.canonicalize().unwrap_or(path)); |
86 | 85 | } |
87 | 86 | } |
88 | 87 |
|
89 | | - fs::create_dir_all(&cwd_dir)?; |
90 | | - Ok(cwd_dir) |
| 88 | + None |
| 89 | +} |
| 90 | + |
| 91 | +fn get_or_create_sub_dir(name: &str) -> PathBuf { |
| 92 | + let dir = get_project_root().join(name); |
| 93 | + if !dir.exists() { |
| 94 | + let _ = fs::create_dir_all(&dir); |
| 95 | + } |
| 96 | + dir |
| 97 | +} |
| 98 | + |
| 99 | +pub fn get_data_dir() -> PathBuf { |
| 100 | + get_or_create_sub_dir("data") |
| 101 | +} |
| 102 | + |
| 103 | +pub fn get_logs_dir() -> PathBuf { |
| 104 | + get_or_create_sub_dir("logs") |
| 105 | +} |
| 106 | + |
| 107 | +pub fn get_conf_dir() -> PathBuf { |
| 108 | + get_or_create_sub_dir("conf") |
| 109 | +} |
| 110 | + |
| 111 | +pub fn get_task_dir() -> PathBuf { |
| 112 | + get_or_create_sub_dir("data/task") |
| 113 | +} |
| 114 | + |
| 115 | +pub fn get_state_dir() -> PathBuf { |
| 116 | + get_or_create_sub_dir("data/state") |
| 117 | +} |
| 118 | + |
| 119 | +pub fn get_state_dir_for_base(base: &str) -> PathBuf { |
| 120 | + resolve_path(base).join("state") |
| 121 | +} |
| 122 | + |
| 123 | +pub fn get_app_log_path() -> PathBuf { |
| 124 | + get_logs_dir().join("app.log") |
| 125 | +} |
| 126 | + |
| 127 | +pub fn get_log_path(relative: &str) -> PathBuf { |
| 128 | + get_logs_dir().join(relative) |
| 129 | +} |
| 130 | + |
| 131 | +pub fn get_wasm_cache_dir() -> PathBuf { |
| 132 | + get_or_create_sub_dir("data/cache/wasm-incremental") |
91 | 133 | } |
92 | 134 |
|
93 | | -pub fn find_or_create_data_dir() -> std::io::Result<PathBuf> { |
94 | | - find_or_create_dir("data") |
| 135 | +pub fn get_python_cache_dir() -> PathBuf { |
| 136 | + get_or_create_sub_dir("data/cache/python-runner") |
95 | 137 | } |
96 | 138 |
|
97 | | -pub fn find_or_create_conf_dir() -> std::io::Result<PathBuf> { |
98 | | - find_or_create_dir("conf") |
| 139 | +pub fn get_python_wasm_path() -> PathBuf { |
| 140 | + get_python_cache_dir().join("functionstream-python-runtime.wasm") |
99 | 141 | } |
100 | 142 |
|
101 | | -pub fn find_or_create_logs_dir() -> std::io::Result<PathBuf> { |
102 | | - find_or_create_dir("logs") |
| 143 | +pub fn get_python_cwasm_path() -> PathBuf { |
| 144 | + get_python_cache_dir().join("functionstream-python-runtime.cwasm") |
103 | 145 | } |
0 commit comments