• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2025 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::path::Path;
15 
16 static AREA1: &str = "/data/storage/el1/base";
17 static AREA2: &str = "/data/storage/el2/base";
18 static AREA5: &str = "/data/storage/el5/base";
19 
path_exists<P: AsRef<Path>>(path: P) -> bool20 pub fn path_exists<P: AsRef<Path>>(path: P) -> bool {
21     Path::new(path.as_ref()).exists()
22 }
23 
belong_app_base(path: &str) -> bool24 pub fn belong_app_base(path: &str) -> bool {
25     path.starts_with(AREA1) || path.starts_with(AREA2) || path.starts_with(AREA5)
26 }
27 
check_standardized_path(path: &str) -> bool28 pub fn check_standardized_path(path: &str) -> bool {
29     if path.is_empty() || !path.starts_with('/') || path.ends_with('/') || path.contains("//") {
30         return false;
31     }
32     // The application side has been standardized and should not receive
33     // unstandardized paths.
34     static NOT_ALLOWED: [&str; 11] = [
35         r".", r".\", r"\.", r"..", r"\..", r"\.\.", r"\.\.\", r"\..\", r".\.", r".\.\", r"..\",
36     ];
37     for segment in path.split('/').filter(|s| !s.is_empty()) {
38         if NOT_ALLOWED.contains(&segment) {
39             return false;
40         }
41     }
42     true
43 }
44 
delete_base_for_list(v: &mut Vec<&str>)45 pub fn delete_base_for_list(v: &mut Vec<&str>) {
46     v.retain(|s| s.len() > AREA1.len());
47 }
48 
49 #[cfg(test)]
50 mod ut_file_control {
51     include!("../tests/ut/ut_file_control.rs");
52 }
53