From f1789491ed34dc7d55250e142503571b5fc552f5 Mon Sep 17 00:00:00 2001 From: WangKe Date: Sun, 27 Apr 2025 14:17:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?environment=20ani=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: WangKe Change-Id: Iea30a7d877292b4a1546e66138c4c1c298127858 --- interfaces/kits/js/BUILD.gn | 65 ++++++ .../ani/bind_function_class.cpp | 71 ++++++ .../mod_environment/ani/environment_ani.cpp | 182 ++++++++++++++++ .../src/mod_environment/ani/environment_ani.h | 42 ++++ .../ani/ets/@ohos.file.environment.ets | 103 +++++++++ .../src/mod_environment/environment_core.cpp | 205 ++++++++++++++++++ .../js/src/mod_environment/environment_core.h | 38 ++++ 7 files changed, 706 insertions(+) create mode 100644 interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.h create mode 100644 interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.cpp create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f12c564b2..80b2a3735 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -989,3 +989,68 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { part_name = "file_api" deps = [ ":ohos_file_securityLabel_abc" ] } + +ohos_shared_library("ani_environment_class") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_environment/ani", + "src/mod_environment", + ] + sources = [ + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_fs/fs_utils.cpp", + "src/mod_environment/ani/bind_function_class.cpp", + "src/mod_environment/ani/environment_ani.cpp", + "src/mod_environment/environment_core.cpp", + ] + + deps = [ + ":ohos_file_environment_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "hilog:libhilog", + "libuv:uv", + "ipc:ipc_core", + "init:libbegetutil", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:libarkruntime", + ] + use_exceptions = true + + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_environment_abc") { + base_url = "./src/mod_environment/ani/ets" + files = [ "./src/mod_environment/ani/ets/@ohos.file.environment.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_environment_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_environment_abc_etc") { + source = "$target_out_dir/ohos_file_environment_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_environment_abc" ] +} diff --git a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp new file mode 100644 index 000000000..86e43e7e1 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 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. + */ + +#include +#include "bind_function.h" +#include "environment_ani.h" + +using namespace OHOS::FileManagement::ModuleFileIO::ANI; + +static ani_status BindStaticMethods(ani_env *env) +{ + static const char *className = "L@ohos/file/environment/EnvironmentImpl;"; + std::array methods = { + ani_native_function { + "getStorageDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetStorageDataDirSync) }, + ani_native_function { + "getUserDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDataDirSync) }, + ani_native_function { + "getUserDownloadDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDownloadDirSync) }, + ani_native_function { + "getUserDesktopDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDesktopDirSync) }, + ani_native_function { + "getUserDocumentDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDocumentDirSync) }, + ani_native_function { + "getExternalStorageDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetExternalStorageDirSync) }, + ani_native_function { + "getUserHomeDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserHomeDirSync) }, + }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for environment!"); + return status; + }; + + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp new file mode 100644 index 000000000..fd3f5c084 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "environment_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "environment_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleEnvironment; + +ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetStorageDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get storage data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDownloadDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user download dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDesktopDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user desktop dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDocumentDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user document dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetExternalStorageDir(); + if (!ret.IsSuccess()) { + HILOGE("Get external storage dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserHomeDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user home dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.h b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h new file mode 100644 index 000000000..d14a78c69 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class EnvironmentAni final { +public: + static ani_string GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets new file mode 100644 index 000000000..5155ebd3a --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 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 { BusinessError, AsyncCallback } from '@ohos.base'; + +namespace Environment { + export function getStorageDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function getStorageDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); + } + + export function getUserDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + export function getUserDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); + } + + export function getUserDownloadDir(): string { + return EnvironmentImpl.getUserDownloadDirSync(); + } + + export function getUserDesktopDir(): string { + return EnvironmentImpl.getUserDesktopDirSync(); + } + + export function getUserDocumentDir(): string { + return EnvironmentImpl.getUserDocumentDirSync(); + } + + export function getExternalStorageDir(): string { + return EnvironmentImpl.getExternalStorageDirSync(); + } + + export function getUserHomeDir(): string { + return EnvironmentImpl.getUserHomeDirSync(); + } + +} + +export default Environment; + +class EnvironmentImpl { + + static { + loadLibrary("ani_environment_class"); + } + + static native getStorageDataDirSync(): string; + static native getUserDataDirSync(): string; + static native getUserDownloadDirSync(): string; + static native getUserDesktopDirSync(): string; + static native getUserDocumentDirSync(): string; + static native getExternalStorageDirSync(): string; + static native getUserHomeDirSync(): string; +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp new file mode 100644 index 000000000..c46fe5066 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "environment_core.h" +#include "accesstoken_kit.h" +#include "account_error_no.h" +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "os_account_manager.h" +#include "parameter.h" +#include "tokenid_kit.h" +#include +#include +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +namespace { +const std::string STORAGE_DATA_PATH = "/data"; +const std::string PC_STORAGE_PATH = "/storage/Users/"; +const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; +const std::string USER_APP_DATA_PATH = "/appdata"; +const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string FILE_ACCESS_MANAGER_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; +const std::string DOWNLOAD_PATH = "/Download"; +const std::string DESKTOP_PATH = "/Desktop"; +const std::string DOCUMENTS_PATH = "/Documents"; +const std::string DEFAULT_USERNAME = "currentUser"; +const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +static bool IsSystemApp() +{ + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("ModuleUserDirectory::CheckCallingPermission have no fileAccess permission"); + return false; + } + return true; +} + +static std::string GetUserName() +{ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK || userName.empty()) { + HILOGE("Get userName Failed"); + } + userName = DEFAULT_USERNAME; + return userName; +} + +static std::string GetPublicPath(const std::string &directoryName) +{ + return PC_STORAGE_PATH + GetUserName() + directoryName; +} + +static bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + return true; + } + HILOGE("Not supporting all mounts"); + return false; +} + +static int CheckInvalidAccess(const std::string &permission) +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return E_DEVICENOTSUPPORT; + } + if (permission == FILE_ACCESS_MANAGER_PERMISSION) { + if (!IsSystemApp()) { + return E_PERMISSION_SYS; + } + } + if (!CheckCallingPermission(permission)) { + HILOGE("No Permission"); + return E_PERMISSION; + } + return ERRNO_NOERR; +} +} + +FsResult DoGetStorageDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + return FsResult::Success(std::move(STORAGE_DATA_PATH)); +} + +int GetUserId() +{ + return 0; +} + +FsResult DoGetUserDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + + auto userDataPath = std::make_shared(); + (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); + return FsResult::Success(std::move(*userDataPath)); + +} + +FsResult DoGetUserDownloadDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string downloadPath = GetPublicPath(DOWNLOAD_PATH); + if (downloadPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(downloadPath)); + +} + +FsResult DoGetUserDesktopDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string desktopPath = GetPublicPath(DESKTOP_PATH); + if (desktopPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(desktopPath)); +} + +FsResult DoGetUserDocumentDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string documentsPath = GetPublicPath(DOCUMENTS_PATH); + if (documentsPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(documentsPath)); +} + +FsResult DoGetExternalStorageDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); + +} + +FsResult DoGetUserHomeDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + + static std::string userName = GetUserName(); + if (userName.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + std::string result = PC_STORAGE_PATH + userName; + return FsResult::Success(std::move(result)); + +} +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_core.h b/interfaces/kits/js/src/mod_environment/environment_core.h new file mode 100644 index 000000000..2ac81134f --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +using namespace ModuleFileIO; + +FsResult DoGetStorageDataDir(); +FsResult DoGetUserDataDir(); +FsResult DoGetUserDownloadDir(); +FsResult DoGetUserDesktopDir(); +FsResult DoGetUserDocumentDir(); +FsResult DoGetExternalStorageDir(); +FsResult DoGetUserHomeDir(); + +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H \ No newline at end of file -- Gitee From 3c70757163f3a6b0d9b548c0fd848f3e44d7b165 Mon Sep 17 00:00:00 2001 From: WangKe Date: Mon, 28 Apr 2025 21:40:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E9=A3=8E=E6=A0=BC=E4=B8=BAg=5F=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E7=9A=84=E5=B0=8F=E9=A9=BC=E5=B3=B0=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: WangKe Change-Id: Icd658605c79c7b3d8d1f714cb445e0998b757c4d --- interfaces/kits/js/BUILD.gn | 2 +- .../src/mod_environment/ani/environment_ani.cpp | 9 +-------- .../ani/ets/@ohos.file.environment.ets | 2 +- .../js/src/mod_environment/environment_core.cpp | 15 +++++++-------- .../js/src/mod_environment/environment_core.h | 2 -- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 80b2a3735..8109beceb 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -990,7 +990,7 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { deps = [ ":ohos_file_securityLabel_abc" ] } -ohos_shared_library("ani_environment_class") { +ohos_shared_library("ani_file_environment") { public_configs = [ ":ani_config" ] include_dirs = [ "src/mod_environment/ani", diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp index fd3f5c084..da95c9a2a 100644 --- a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -15,9 +15,9 @@ #include "environment_ani.h" +#include "environment_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" -#include "environment_core.h" #include "type_converter.h" namespace OHOS { @@ -46,7 +46,6 @@ ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -67,7 +66,6 @@ ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -88,7 +86,6 @@ ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -109,7 +106,6 @@ ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -130,7 +126,6 @@ ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -151,7 +146,6 @@ ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unuse ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -172,7 +166,6 @@ ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets index 5155ebd3a..9ccc27495 100644 --- a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -90,7 +90,7 @@ export default Environment; class EnvironmentImpl { static { - loadLibrary("ani_environment_class"); + loadLibrary("ani_file_environment"); } static native getStorageDataDirSync(): string; diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index c46fe5066..f13e9b942 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -14,6 +14,10 @@ */ #include "environment_core.h" + +#include +#include + #include "accesstoken_kit.h" #include "account_error_no.h" #include "filemgmt_libhilog.h" @@ -21,8 +25,7 @@ #include "os_account_manager.h" #include "parameter.h" #include "tokenid_kit.h" -#include -#include + namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { @@ -39,7 +42,7 @@ const std::string DOWNLOAD_PATH = "/Download"; const std::string DESKTOP_PATH = "/Desktop"; const std::string DOCUMENTS_PATH = "/Documents"; const std::string DEFAULT_USERNAME = "currentUser"; -const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +const char *FILE_MANAMER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; static bool IsSystemApp() { uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); @@ -76,7 +79,7 @@ static std::string GetPublicPath(const std::string &directoryName) static bool CheckFileManagerFullMountEnable() { char value[] = "false"; - int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + int retSystem = GetParameter(FILE_MANAMER_FULL_MOUNT_ENABLE_PARAMETER, "false", value, sizeof(value)); if (retSystem > 0 && !std::strcmp(value, "true")) { return true; } @@ -125,7 +128,6 @@ FsResult DoGetUserDataDir() auto userDataPath = std::make_shared(); (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); return FsResult::Success(std::move(*userDataPath)); - } FsResult DoGetUserDownloadDir() @@ -141,7 +143,6 @@ FsResult DoGetUserDownloadDir() return FsResult::Error(E_UNKNOWN_ERROR); } return FsResult::Success(std::move(downloadPath)); - } FsResult DoGetUserDesktopDir() @@ -181,7 +182,6 @@ FsResult DoGetExternalStorageDir() return FsResult::Error(res); } return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); - } FsResult DoGetUserHomeDir() @@ -198,7 +198,6 @@ FsResult DoGetUserHomeDir() } std::string result = PC_STORAGE_PATH + userName; return FsResult::Success(std::move(result)); - } } // namespace ModuleEnvironment } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_environment/environment_core.h b/interfaces/kits/js/src/mod_environment/environment_core.h index 2ac81134f..0962c1f4a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.h +++ b/interfaces/kits/js/src/mod_environment/environment_core.h @@ -23,7 +23,6 @@ namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { using namespace ModuleFileIO; - FsResult DoGetStorageDataDir(); FsResult DoGetUserDataDir(); FsResult DoGetUserDownloadDir(); @@ -31,7 +30,6 @@ FsResult DoGetUserDesktopDir(); FsResult DoGetUserDocumentDir(); FsResult DoGetExternalStorageDir(); FsResult DoGetUserHomeDir(); - } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS -- Gitee