1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 use std::fs::{create_dir_all, remove_dir_all, File};
16 use std::io::{Read, Write};
17 use std::path::Path;
18 use cxx::let_cxx_string;
19 use utils_rust::directory_ex;
20 // rw-r--r--
21 const PROFILE_FILE_MODE: u32 = 0o644;
22 // rw-r-xr-x
23 const PROFILE_PATH_MODE: u32 = 0o655;
24 /// code sign file error
25 pub enum CodeSignFileError {
26         /// change file mode error
27         ChangeFileModError,
28         /// change path mode error
29         ChangePathModError,
30     }
31 /// change default mode of file
change_default_mode_file(path_file: &str) -> Result<(), CodeSignFileError>32 pub fn change_default_mode_file(path_file: &str) -> Result<(), CodeSignFileError> {
33     let_cxx_string!(dirpath = path_file);
34     let mode = PROFILE_FILE_MODE;
35     let ret = directory_ex::ffi::ChangeModeFile(&dirpath, &mode);
36     if !ret {
37         return Err(CodeSignFileError::ChangeFileModError);
38     }
39     Ok(())
40 }
41 /// change default mode of directory
change_default_mode_directory(path_file: &str) -> Result<(), CodeSignFileError>42 pub fn change_default_mode_directory(path_file: &str) -> Result<(), CodeSignFileError> {
43     let_cxx_string!(dirpath = path_file);
44     let mode = PROFILE_PATH_MODE;
45     let ret = directory_ex::ffi::ChangeModeDirectory(&dirpath, &mode);
46     if !ret {
47         return Err(CodeSignFileError::ChangePathModError);
48     }
49     Ok(())
50 }
51 /// format storage file path
fmt_store_path(prefix: &str, tail: &str) -> String52 pub fn fmt_store_path(prefix: &str, tail: &str) -> String {
53     format!("{}/{}", prefix, tail)
54 }
55 /// create file path with path name
create_file_path(path_name: &str) -> Result<(), std::io::Error>56 pub fn create_file_path(path_name: &str) -> Result<(), std::io::Error> {
57     let path = Path::new(path_name);
58     create_dir_all(path)?;
59     Ok(())
60 }
61 /// write file buffer to disk
write_bytes_to_file(filename: &str, data: &[u8]) -> Result<(), std::io::Error>62 pub fn write_bytes_to_file(filename: &str, data: &[u8]) -> Result<(), std::io::Error> {
63     let mut file = File::create(filename)?;
64     file.write_all(data)?;
65     Ok(())
66 }
67 /// loads file buffer from disk
load_bytes_from_file(filename: &str, buffer: &mut Vec<u8>) -> Result<(), std::io::Error>68 pub fn load_bytes_from_file(filename: &str, buffer: &mut Vec<u8>) -> Result<(), std::io::Error> {
69     let mut file = File::open(filename)?;
70     file.read_to_end(buffer)?;
71     Ok(())
72 }
73 /// find file
file_exists(file_path: &str) -> bool74 pub fn file_exists(file_path: &str) -> bool {
75     Path::new(file_path).exists()
76 }
77 /// delete file path
delete_file_path(file_path: &str) -> Result<(), std::io::Error>78 pub fn delete_file_path(file_path: &str) -> Result<(), std::io::Error> {
79     remove_dir_all(file_path)?;
80     Ok(())
81 }
82