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 18use asset_definition::{log_throw_error, ErrCode, Result}; 19use std::{fs, path::Path}; 20 21/// Suffix for backup database files. 22pub const BACKUP_SUFFIX: &str = ".backup"; 23/// Suffix for database files. 24pub const DB_SUFFIX: &str = ".db"; 25/// Name for data base key ciphertext file. 26pub const DB_KEY: &str = "db_key"; 27/// Root path to de user directories. 28pub const DE_ROOT_PATH: &str = "data/service/el1/public/asset_service"; 29/// Root path to ce user directories. 30pub const CE_ROOT_PATH: &str = "data/service/el2"; 31 32/// Get all db name in user directory. 33pub(crate) fn get_user_dbs(path_str: &str) -> Result<Vec<String>> { 34 let mut dbs = vec![]; 35 for db_path in fs::read_dir(path_str)? { 36 let db_path = db_path?; 37 let db_file_name = db_path.file_name().to_string_lossy().to_string(); 38 if db_file_name.ends_with(DB_SUFFIX) { 39 dbs.push(db_file_name.strip_suffix(DB_SUFFIX).unwrap_or(&db_file_name).to_string()) 40 } 41 } 42 Ok(dbs) 43} 44 45/// Check whether file exists. 46pub fn is_file_exist(path_str: &str) -> Result<bool> { 47 let path: &Path = Path::new(&path_str); 48 match path.try_exists() { 49 Ok(true) => Ok(true), 50 Ok(false) => Ok(false), 51 Err(e) => { 52 log_throw_error!( 53 ErrCode::FileOperationError, 54 "[FATAL][SA]]Checking existence of database key ciphertext file failed! error is [{}]", 55 e 56 ) 57 }, 58 } 59} 60