sys event delete refactor

Match-id-cc2d7ea76b27e648157b34daf6a66abe491438c9
This commit is contained in:
authName 2023-10-20 08:50:27 +08:00 committed by userName
parent 18ddad4f66
commit 7ddbb08b6a
11 changed files with 94 additions and 67 deletions

View File

@ -0,0 +1,31 @@
# Copyright (C) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
COMPONENT_DIR = "//base/security/asset"
ohos_rust_static_library("asset_file_operator") {
sources = [ "src/lib.rs" ]
deps = [
"$COMPONENT_DIR/frameworks/common:asset_common",
]
crate_name = "asset_file_operator"
crate_type = "rlib"
install_images = [ system_base_dir ]
subsystem_name = "security"
part_name = "asset"
}

View File

@ -0,0 +1,9 @@
[package]
name = "asset_file_operator"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
asset_common = { path = "../../../frameworks/common" }

View File

@ -22,11 +22,11 @@ use asset_common::{
loge,
};
// todo: yyd 该文件挪到framework/os_denpendency下
const ROOT_PATH: &str = "data/service/el1/public/asset_service";
pub(crate) fn create_user_db_dir(user_id: i32) -> Result<()> {
/// the function to create user database directory
pub fn create_user_db_dir(user_id: i32) -> Result<()> {
let path = format!("{}/{}", ROOT_PATH, user_id);
let path = Path::new(&path);
if !path.exists() {
@ -43,3 +43,25 @@ pub(crate) fn create_user_db_dir(user_id: i32) -> Result<()> {
}
Ok(())
}
/// the function to delete user directory
pub fn delete_user_db_dir(user_id: i32) -> bool {
let path_str = format!("{}/{}", ROOT_PATH, user_id);
let path = Path::new(&path_str);
if path.exists() {
match fs::remove_dir_all(path) {
Ok(_) => {
return true
},
Err(e) if e.kind() != std::io::ErrorKind::NotFound => {
return true
},
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
loge!("remove dir failed! permission denied");
return false
},
_ => { return true }
}
}
true
}

View File

@ -23,6 +23,7 @@ ohos_rust_shared_library("asset_service") {
"$COMPONENT_DIR/frameworks/common:asset_common",
"$COMPONENT_DIR/frameworks/ipc_interface:asset_ipc_interface",
"$COMPONENT_DIR/frameworks/ipc_proxy:asset_ipc_proxy",
"$COMPONENT_DIR/frameworks/os_dependency/file:asset_file_operator",
"$COMPONENT_DIR/services/crypto_manager:asset_crypto_manager",
"$COMPONENT_DIR/services/db_operator:asset_db_operator",
"$COMPONENT_DIR/services/os_dependency:asset_os_dependency",

View File

@ -11,4 +11,5 @@ asset_ipc_interface = { path = "../../frameworks/ipc_interface" }
asset_ipc_proxy = { path = "../../frameworks/ipc_proxy" }
asset_db_operator = { path = "../db_operator" }
asset_crypto_manager = { path = "../crypto_manager" }
asset_sdk = { path = "../../interfaces/inner_api/rs" }
asset_sdk = { path = "../../interfaces/inner_api/rs" }
asset_file_operator = { path = "../../frameworks/os_dependency/file" }

View File

@ -17,11 +17,9 @@
mod argument_check;
mod crypto_adapter;
mod file_operator;
pub(crate) use argument_check::{check_tag_validity, check_required_tags, check_value_validity};
pub(crate) use crypto_adapter::{decrypt, encrypt, init_decrypt};
pub(crate) use file_operator::create_user_db_dir;
use std::time::{SystemTime, UNIX_EPOCH};

View File

@ -118,7 +118,7 @@ pub(crate) fn add(attributes: &AssetMap, calling_info: &CallingInfo) -> Result<(
check_arguments(attributes)?;
// Create database directory if not exists.
common::create_user_db_dir(calling_info.user_id())?;
asset_file_operator::create_user_db_dir(calling_info.user_id())?;
// Fill all attributes to DbMap.
let mut db_data = common::into_db_map(attributes);

View File

@ -50,6 +50,7 @@ ohos_rust_shared_ffi("asset_service_ffi") {
sources = [ "src/lib.rs" ]
deps = [
"$COMPONENT_DIR/frameworks/common:asset_common",
"$COMPONENT_DIR/frameworks/os_dependency/file:asset_file_operator",
"$COMPONENT_DIR/services/db_operator:asset_db_operator",
"$COMPONENT_DIR/services/crypto_manager:asset_crypto_manager",
"//third_party/rust/crates/libc:lib",

View File

@ -8,4 +8,5 @@ edition = "2021"
[dependencies]
asset_db_operator = { path = "../../services/db_operator" }
asset_crypto_manager = { path = "../../services/crypto_manager" }
asset_common = { path = "../../frameworks/common" }
asset_common = { path = "../../frameworks/common" }
asset_file_operator = { path = "../../frameworks/os_dependency/file" }

View File

@ -15,11 +15,7 @@
//! This module is used to adapt to the functions on which assets depend.
use std::{
ffi::{c_char, CString},
fs,
path::Path,
};
use std::ffi::{c_char, CString};
use asset_common::{
definition::{Accessibility, AuthType, Value},
@ -30,6 +26,7 @@ use asset_db_operator::{
database_table_helper::{DefaultDatabaseHelper, COLUMN_OWNER},
types::DbMap,
};
use asset_file_operator::delete_user_db_dir;
fn delete_key(user_id: i32, owner: &Vec<u8>, auth_type: AuthType, access_type: Accessibility) {
let secret_key = SecretKey::new(user_id, owner, auth_type, access_type);
@ -43,7 +40,7 @@ fn delete_key(user_id: i32, owner: &Vec<u8>, auth_type: AuthType, access_type: A
/// Function called from C programming language to Rust programming language for delete hap Asset.
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn delete_hap_asset(user_id: i32, owner: *const c_char) -> i32 {
pub unsafe extern "C" fn delete_asset_by_owner(user_id: i32, owner: *const c_char) -> i32 {
// 1 delete data in db
let owner = CString::from_raw(owner as *mut c_char).into_string().unwrap();
let cond = DbMap::from([(COLUMN_OWNER, Value::Bytes(owner.as_bytes().to_vec()))]);
@ -61,33 +58,8 @@ pub unsafe extern "C" fn delete_hap_asset(user_id: i32, owner: *const c_char) ->
}
}
const ROOT_PATH: &str = "data/service/el1/public/asset_service";
/// Function called from C programming language to Rust programming language for delete user Asset.
/// # Safety
/// dereference pointer // todo: yyd delete
#[no_mangle]
pub extern "C" fn delete_user_asset(user_id: i32) {
// todo: yyd 该文件实现挪到file_operator文件里
let path_str = format!("{}/{}", ROOT_PATH, user_id);
let path = Path::new(&path_str);
if !path.exists() {
// toto: yyd: 此处是文件存在
match fs::remove_dir_all(path) {
Ok(_) => {
logi!("remove dir success!");
},
Err(e) if e.kind() != std::io::ErrorKind::NotFound => {
logi!("remove dir failed! not found dir"); // 返回成功
},
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
// todo: yyd 确认什么情况下返回什么错误码,如果数据库正在使用,删除目录需要重试
logi!("remove dir failed! permission denied");
},
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {
logi!("remove dir failed! interrupted");
},
_ => (),
}
}
pub extern "C" fn delete_asset_user_dir(user_id: i32) -> bool {
delete_user_db_dir(user_id)
}

View File

@ -24,8 +24,8 @@
#include "asset_log.h"
extern "C" {
int32_t delete_hap_asset(int32_t user_id, const char* owner); // todo 切换命名
void delete_user_asset(int32_t user_id);
int32_t delete_asset_by_owner(int32_t user_id, const char* owner);
bool delete_asset_user_dir(int32_t user_id);
}
namespace {
@ -40,44 +40,34 @@ public:
{
auto want = data.GetWant();
std::string action = want.GetAction();
LOGE("receive event!!!!!"); // todo 要删掉
if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
// get userId
int uid = want.GetIntParam(OHOS::AppExecFwk::Constants::UID, -1);
int userId = -1;
OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); // todo: 从want中获取
LOGE("userId %{public}i", userId); // todo 要删掉
// get appId
int userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
const char *APP_ID = "appId";
std::string appId = want.GetStringParam(APP_ID);
LOGE("appId %{public}s", appId.c_str()); // todo 要删掉
int appIndex = 0;
if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
appIndex = want.GetIntParam(OHOS::AppExecFwk::Constants::SANDBOX_APP_INDEX, -1);
if (appIndex < 0) {
LOGE("sandbox package appIndex = %{public}d is invalid.", appIndex);
return;
}
}
if (appId.empty() || userId == -1) {
LOGE("get wrong appId/userId");
int appIndex = action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED ?
want.GetIntParam(OHOS::AppExecFwk::Constants::SANDBOX_APP_INDEX, -1) : 0;
if (appId.empty() || userId == -1 || appIndex == -1) {
LOGE("wrong appId %{public}s/userId %{public}i/appIndex %{public}d", appId.c_str(), userId, appIndex);
return;
}
LOGE("appIndex %{public}i", appIndex); // todo 要删掉
LOGI("AssetService app removed");
std::string owner = appId + '_' + std::to_string(appIndex);
int totalDeleteNum = delete_hap_asset(userId, owner.c_str());
int totalDeleteNum = delete_asset_by_owner(userId, owner.c_str());
LOGI("delete finish! total delete line: %{public}i", totalDeleteNum); // todo 要删掉
// TODO: 增加判断os_account
// do DeleteByAppID
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
int userId = data.GetCode();
LOGE("AssetService user removed: userId is %{public}i", userId); // todo 要删掉
// delete data
delete_user_asset(userId); // todo 这里直接把user下对应的文件夹删除了 谨慎使用
} // todo: 监听锁屏广播中止session
if (delete_asset_user_dir(userId)) {
LOGI("delete user %{public}i dir finish!", userId); // todo 要删掉
};
} else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
// todo: 监听锁屏广播中止session
LOGE("AssetService screen off"); // todo 要删掉
}
}
};
}
@ -90,6 +80,7 @@ bool SubscribeSystemEvent(void)
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
matchingSkills.AddEvent(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
g_eventHandler = std::make_shared<SystemEventHandler>(subscriberInfo);