1 /*
2  * Copyright (c) 2024 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 
16 //! This file implements ce file operations.
17 
18 use asset_definition::{log_throw_error, ErrCode, Result};
19 use std::{fs, path::Path};
20 
21 use crate::common::{get_user_dbs, is_file_exist, DB_KEY};
22 
construct_ce_db_dir(user_id: i32) -> String23 fn construct_ce_db_dir(user_id: i32) -> String {
24     format!("data/service/el2/{}/asset_service", user_id)
25 }
26 
construct_db_key_cipher_path(user_id: i32) -> String27 fn construct_db_key_cipher_path(user_id: i32) -> String {
28     format!("data/service/el2/{}/asset_service/{}", user_id, DB_KEY)
29 }
30 
31 /// Check db key cipher file exists.
is_db_key_cipher_file_exist(user_id: i32) -> Result<bool>32 pub fn is_db_key_cipher_file_exist(user_id: i32) -> Result<bool> {
33     let path_str = construct_db_key_cipher_path(user_id);
34     is_file_exist(&path_str)
35 }
36 
37 /// Read db key cipher.
read_db_key_cipher(user_id: i32) -> Result<Vec<u8>>38 pub fn read_db_key_cipher(user_id: i32) -> Result<Vec<u8>> {
39     let path_str = construct_db_key_cipher_path(user_id);
40     let path: &Path = Path::new(&path_str);
41     match fs::read(path) {
42         Ok(db_key_cipher) => Ok(db_key_cipher),
43         Err(e) => {
44             log_throw_error!(
45                 ErrCode::FileOperationError,
46                 "[FATAL][SA]Read database key ciphertext failed! error is [{}]",
47                 e
48             )
49         },
50     }
51 }
52 
53 /// Write db key cipher. If path does not exist, create it automatically.
write_db_key_cipher(user_id: i32, db_key_cipher: &Vec<u8>) -> Result<()>54 pub fn write_db_key_cipher(user_id: i32, db_key_cipher: &Vec<u8>) -> Result<()> {
55     let path_str = construct_db_key_cipher_path(user_id);
56     let path: &Path = Path::new(&path_str);
57     match fs::write(path, db_key_cipher) {
58         Ok(_) => Ok(()),
59         Err(e) => {
60             log_throw_error!(
61                 ErrCode::FileOperationError,
62                 "[FATAL][SA]Write database key ciphertext failed! error is [{}]",
63                 e
64             )
65         },
66     }
67 }
68 
69 /// Remove all CE file in a specific user space.
remove_ce_files(user_id: i32) -> Result<()>70 pub fn remove_ce_files(user_id: i32) -> Result<()> {
71     let path_str = construct_ce_db_dir(user_id);
72     for file in fs::read_dir(path_str)? {
73         let file = &file?;
74         match fs::remove_file(file.path().to_string_lossy().to_string()) {
75             Ok(_) => (),
76             Err(e) => {
77                 return log_throw_error!(
78                     ErrCode::FileOperationError,
79                     "[FATAL]Remove [{}] failed, error code:[{}]",
80                     file.path().to_string_lossy().to_string(),
81                     e
82                 )
83             },
84         }
85     }
86     Ok(())
87 }
88 
89 /// Obtain ce user dbs
get_ce_user_dbs(user_id: i32) -> Result<Vec<String>>90 pub fn get_ce_user_dbs(user_id: i32) -> Result<Vec<String>> {
91     get_user_dbs(&construct_ce_db_dir(user_id))
92 }
93