diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index dc04f46b13e880c2d7ea8c4602ef11a5edb09fcf..01f46ac7a40e90affe536db6abf633c5c2044e56 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -129,12 +129,8 @@ ohos_shared_library("fs") { ] external_deps = [ - "ability_base:want", "ability_base:zuri", - "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", - "ability_runtime:napi_base_context", - "ability_runtime:static_subscriber_extension", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", @@ -197,56 +193,66 @@ ohos_shared_library("statfs") { external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } -ohos_shared_library("environment") { +ohos_shared_library("statvfs") { subsystem_name = "filemanagement" part_name = "file_api" - relative_install_dir = "module" + relative_install_dir = "module/file" - include_dirs = [ - "//foundation/arkui/napi/interfaces/kits", - "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi/n_async", - "//third_party/node/src", + sources = [ + "src/mod_statvfs/statvfs_n_exporter.cpp", + "src/mod_statvfs/statvfs_napi.cpp", + ] + + deps = [ + "//foundation/filemanagement/file_api/utils/filemgmt_libhilog", + "//foundation/filemanagement/file_api/utils/filemgmt_libn", ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] +} + +ohos_shared_library("environment") { + subsystem_name = "filemanagement" + part_name = "file_api" + + relative_install_dir = "module/file" + + include_dirs = [ "//third_party/node/src" ] + sources = [ - "src/common/napi/n_async/n_async_work_callback.cpp", - "src/common/napi/n_async/n_async_work_promise.cpp", - "src/common/napi/n_async/n_ref.cpp", - "src/common/napi/n_func_arg.cpp", - "src/common/napi/n_val.cpp", - "src/common/uni_error.cpp", "src/mod_environment/environment_n_exporter.cpp", "src/mod_environment/environment_napi.cpp", ] - deps = [ "//foundation/arkui/napi:ace_napi" ] + deps = [ + "//foundation/filemanagement/file_api/utils/filemgmt_libhilog", + "//foundation/filemanagement/file_api/utils/filemgmt_libn", + ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] } ohos_shared_library("securitylabel") { subsystem_name = "filemanagement" part_name = "file_api" - relative_install_dir = "module" + relative_install_dir = "module/file" cflags = [ "-Wno-format" ] - include_dirs = [ - "//foundation/arkui/napi/interfaces/kits", - "//foundation/arkui/ace_engine/frameworks/base/utils", - "//foundation/arkui/ace_engine/frameworks", - "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi/n_async", - ] - sources = file_common_src sources += [ "src/mod_securitylabel/securitylabel_n_exporter.cpp", "src/mod_securitylabel/securitylabel_napi.cpp", ] - deps = [ "//foundation/arkui/napi:ace_napi" ] + deps = [ + "//foundation/filemanagement/file_api/utils/filemgmt_libhilog", + "//foundation/filemanagement/file_api/utils/filemgmt_libn", + ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } @@ -288,5 +294,6 @@ group("build_kits_js") { ":fs", ":securitylabel", ":statfs", + ":statvfs", ] } diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp index 780bdc2bc80e5fcc3c985fca78076eebb6357652..f72e9f5c408ddbd27b0f81467a5bc5d66b61507b 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -17,31 +17,29 @@ #include -#include "../common/napi/n_async/n_async_work_callback.h" -#include "../common/napi/n_async/n_async_work_promise.h" -#include "../common/napi/n_class.h" -#include "../common/napi/n_func_arg.h" -#include "../common/napi/n_val.h" -#include "../common/uni_error.h" +#include "filemgmt_libhilog.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleEnvironment { +using namespace OHOS::FileManagement::LibN; namespace { const std::string STORAGE_DATA_PATH = "/data"; } + napi_value GetStorageDataDir(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } - auto cbExec = [](napi_env env) -> UniError { - return UniError(ERRNO_NOERR); + auto cbExec = []() -> NError { + return NError(ERRNO_NOERR); }; - auto cbComplete = [](napi_env env, UniError err) -> NVal { + auto cbComplete = [](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } @@ -67,16 +65,17 @@ napi_value GetUserDataDir(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto userDataPath = std::make_shared(); - auto cbExec = [userDataPath](napi_env env) -> UniError { + auto cbExec = [userDataPath]() -> NError { (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); - return UniError(ERRNO_NOERR); + return NError(ERRNO_NOERR); }; - auto cbComplete = [userDataPath](napi_env env, UniError err) -> NVal { + auto cbComplete = [userDataPath](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } @@ -93,5 +92,5 @@ napi_value GetUserDataDir(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } } // namespace ModuleEnvironment -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h index 1b46a7b96924b89f17d889df4f1436be00dd2a53..f3b6516bc2b0d2624eeadeb3d05e2e6bc3242af7 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,14 +16,14 @@ #ifndef ENVIRONMENT_N_EXPORTER_H #define ENVIRONMENT_N_EXPORTER_H -#include "../common/napi/n_exporter.h" +#include "filemgmt_libn.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleEnvironment { napi_value GetStorageDataDir(napi_env env, napi_callback_info info); napi_value GetUserDataDir(napi_env env, napi_callback_info info); } // namespace ModuleEnvironment -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS #endif // ENVIRONMENT_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_napi.cpp b/interfaces/kits/js/src/mod_environment/environment_napi.cpp index f6ec658a4fa60d99183e649250c0ee3a5446e5c0..3a5c007bbe286787ffd2ceb93b33b607f376e191 100644 --- a/interfaces/kits/js/src/mod_environment/environment_napi.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_napi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -14,11 +14,9 @@ */ #include "environment_n_exporter.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleEnvironment { /*********************************************** * Module export and register @@ -35,5 +33,5 @@ napi_value EnvironmentExport(napi_env env, napi_value exports) NAPI_MODULE(environment, EnvironmentExport) } // namespace ModuleEnvironment -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index 5e47c8be9845b45d9b01733a065ce69532a38137..02b6c36172194d0a2e7e61a2f15923b949994198 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -23,7 +23,7 @@ #include namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleSecurityLabel { const char XATTR_KEY[] = {"user.security"}; const std::string DEFAULT_DATA_LEVEL = "s3"; @@ -66,6 +66,6 @@ public: } }; } // namespace ModuleSecurityLabel -} // namespace FileIO +} // namespace FileManagement } // namespace OHOS #endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp index 88ee86e8355434ac23486371078b5728640ecab5..99a564fd3e08f45456590f9c88af9ee8ecd9e7d7 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -18,24 +18,21 @@ #include #include -#include "../common/napi/n_class.h" -#include "../common/napi/n_func_arg.h" -#include "../common/napi/n_val.h" -#include "../common/uni_error.h" -#include "n_async_work_callback.h" -#include "n_async_work_promise.h" +#include "filemgmt_libhilog.h" #include "security_label.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleSecurityLabel { +using namespace OHOS::FileManagement::LibN; using namespace std; napi_value SetSecurityLabel(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(static_cast(NARG_CNT::TWO), static_cast(NARG_CNT::THREE))) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -44,34 +41,34 @@ napi_value SetSecurityLabel(napi_env env, napi_callback_info info) std::unique_ptr dataLevel; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); return nullptr; } tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); + HILOGE("Invalid dataLevel"); + NError(EINVAL).ThrowErr(env); return nullptr; } - std::string pathString(path.get()); std::string dataLevelString(dataLevel.get()); if (DATA_LEVEL.find(dataLevelString) == DATA_LEVEL.end()) { - UniError(EINVAL).ThrowErr(env, "Invalid Argument of dataLevelEnum"); + HILOGE("Invalid Argument of dataLevelEnum"); + NError(EINVAL).ThrowErr(env); return nullptr; } - auto cbExec = [pathString, dataLevelString](napi_env env) -> UniError { + auto cbExec = [pathString = string(path.get()), dataLevelString]() -> NError { bool ret = SecurityLabel::SetSecurityLabel(pathString, dataLevelString); if (!ret) { - return UniError(errno); - } else { - return UniError(ERRNO_NOERR); + return NError(errno); } + return NError(ERRNO_NOERR); }; - auto cbComplete = [](napi_env env, UniError err) -> NVal { + auto cbComplete = [](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; - } else { - return NVal::CreateUndefined(env); } + return NVal::CreateUndefined(env); }; static const std::string procedureName = "SetSecurityLabel"; NVal thisVar(env, funcArg.GetThisVar()); @@ -79,18 +76,16 @@ napi_value SetSecurityLabel(napi_env env, napi_callback_info info) return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[static_cast(NARG_POS::THIRD)]); - if (cb.TypeIs(napi_function)) { - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; } napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(static_cast(NARG_CNT::TWO), static_cast(NARG_CNT::THREE))) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -99,24 +94,28 @@ napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) std::unique_ptr dataLevel; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); return nullptr; } tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); + HILOGE("Invalid dataLevel"); + NError(EINVAL).ThrowErr(env); return nullptr; } if (DATA_LEVEL.find(dataLevel.get()) == DATA_LEVEL.end()) { - UniError(EINVAL).ThrowErr(env, "Invalid Argument of dataLevelEnum"); + HILOGE("Invalid Argument of dataLevelEnum"); + NError(EINVAL).ThrowErr(env); return nullptr; } bool ret = SecurityLabel::SetSecurityLabel(path.get(), dataLevel.get()); if (!ret) { - return UniError(errno).GetNapiErr(env); + NError(errno).ThrowErr(env); + return nullptr; } return NVal::CreateUndefined(env).val_; @@ -126,7 +125,8 @@ napi_value GetSecurityLabel(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(static_cast(NARG_CNT::ONE), static_cast(NARG_CNT::TWO))) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -134,16 +134,17 @@ napi_value GetSecurityLabel(napi_env env, napi_callback_info info) std::unique_ptr path; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto result = std::make_shared(); std::string pathString(path.get()); - auto cbExec = [pathString, result](napi_env env) -> UniError { + auto cbExec = [pathString = move(pathString), result]() -> NError { *result = SecurityLabel::GetSecurityLabel(pathString); - return UniError(ERRNO_NOERR); + return NError(ERRNO_NOERR); }; - auto cbComplete = [result](napi_env env, UniError err) -> NVal { + auto cbComplete = [result](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } @@ -167,7 +168,8 @@ napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(static_cast(NARG_CNT::ONE), static_cast(NARG_CNT::TWO))) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -175,7 +177,8 @@ napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) std::unique_ptr path; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -183,5 +186,5 @@ napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, result).val_; } } // namespace ModuleSecurityLabel -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h index 39748659bd7bed3029563304c48650e8228ae6b7..1dc7c49b0c8be8fa8d5d9cd0fa29141408bed2be 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,16 +16,16 @@ #ifndef SECURITYLABEL_N_EXPORTER_H #define SECURITYLABEL_N_EXPORTER_H -#include "../common/napi/n_exporter.h" +#include "filemgmt_libn.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleSecurityLabel { napi_value SetSecurityLabel(napi_env env, napi_callback_info info); napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info); napi_value GetSecurityLabel(napi_env env, napi_callback_info info); napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info); } // namespace ModuleSecurityLabel -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS #endif // SECURITYLABEL_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp index 48df4ce3801ca3338819189a99a58588d063525c..1ae73e1ee5ad1cecad19c0e4cb8d081f1cfbb08e 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -13,13 +13,10 @@ * limitations under the License. */ -#include "napi/native_api.h" -#include "napi/native_node_api.h" - #include "securitylabel_n_exporter.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleSecurityLabel { /*********************************************** * Module export and register @@ -38,5 +35,5 @@ napi_value SecurityLabelExport(napi_env env, napi_value exports) NAPI_MODULE(securitylabel, SecurityLabelExport) } // namespace ModuleSecurityLabel -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp index 066e1b601f83f10504cf5daa6d27b40a45b5601e..89cdfc3ec87f34802cacf84e29c9951960021b11 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -19,7 +19,7 @@ #include namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleStatfs { using namespace FileManagement::LibN; @@ -515,5 +515,5 @@ napi_value GetTotalBytes(napi_env env, napi_callback_info info) return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } } // namespace ModuleStatfs -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h index dd25b1724926781155cab9fd431acff1d11309bf..39abb61f3961908aadda14270e3a8196e90221bc 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -17,9 +17,10 @@ #define STATFS_N_EXPORTER_H #include "filemgmt_libn.h" +#include "filemgmt_libhilog.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleStatfs { napi_value GetFrSizeSync(napi_env env, napi_callback_info info); napi_value GetFrSize(napi_env env, napi_callback_info info); @@ -42,6 +43,6 @@ napi_value GetFreeBytes(napi_env env, napi_callback_info info); napi_value GetTotalBytesSync(napi_env env, napi_callback_info info); napi_value GetTotalBytes(napi_env env, napi_callback_info info); } // namespace ModuleStatfs -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS #endif // STATFS_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp b/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp index 2c0f27dc2674bd5e3b381ae0dd3c5e7f87e14109..2c17619287ca0a603fe1b8f1906b510e2c4fa3fa 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp +++ b/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,7 +16,7 @@ #include "statfs_n_exporter.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleStatfs { /*********************************************** * Module export and register @@ -33,5 +33,5 @@ napi_value StatfsExport(napi_env env, napi_value exports) NAPI_MODULE(statfs, StatfsExport) } // namespace ModuleStatfs -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.cpp b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..782419b71913287455a0baa050f619301c182c6d --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2022-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. + */ + +#include "statvfs_n_exporter.h" + +#include +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleStatvfs { +using namespace FileManagement::LibN; + +napi_value GetFreeSizeSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + unsigned long long freeSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bfree); + return NVal::CreateInt64(env, freeSize).val_; +} + +napi_value GetFreeSize(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString = move(pathString), resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bfree); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return NVal::CreateInt64(env, *resultSize); + }; + + static const std::string procedureName = "GetFreeSize"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value GetTotalSizeSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + struct statvfs diskInfo; + int ret = statvfs(path.get(), &diskInfo); + if (ret != 0) { + NError(errno).ThrowErr(env); + return nullptr; + } + unsigned long long totalSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_blocks); + return NVal::CreateInt64(env, totalSize).val_; +} + +napi_value GetTotalSize(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + bool succ = false; + std::unique_ptr path; + tie(succ, path, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto resultSize = std::make_shared(); + std::string pathString(path.get()); + auto cbExec = [pathString = move(pathString), resultSize]() -> NError { + struct statvfs diskInfo; + int ret = statvfs(pathString.c_str(), &diskInfo); + if (ret != 0) { + return NError(errno); + } + *resultSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_blocks); + return NError(ERRNO_NOERR); + }; + auto cbComplete = [resultSize](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateInt64(env, *resultSize) }; + }; + + static const std::string procedureName = "GetTotalSize"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; +} +} // namespace ModuleStatfs +} // namespace FileManagement +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.h b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..5d24348eedccdba7f6571c86802fc9b86c218a42 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022-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. + */ + +#ifndef STATVFS_N_EXPORTER_H +#define STATVFS_N_EXPORTER_H + +#include "filemgmt_libn.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleStatvfs { +napi_value GetFreeSizeSync(napi_env env, napi_callback_info info); +napi_value GetFreeSize(napi_env env, napi_callback_info info); + +napi_value GetTotalSizeSync(napi_env env, napi_callback_info info); +napi_value GetTotalSize(napi_env env, napi_callback_info info); +} // namespace ModuleStatvfs +} // namespace FileManagement +} // namespace OHOS +#endif // STATVFS_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/statvfs_napi.cpp b/interfaces/kits/js/src/mod_statvfs/statvfs_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ece7c040a46d51f31527ce5afe382f10b703e78 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_napi.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-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. + */ + +#include "statvfs_n_exporter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleStatvfs { + +napi_value StatvfsExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getFreeSize", GetFreeSize), + DECLARE_NAPI_FUNCTION("getTotalSize", GetTotalSize), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +NAPI_MODULE(statvfs, StatvfsExport) +} // namespace ModuleStatvfs +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/utils/filemgmt_libhilog/filemgmt_libhilog.h b/utils/filemgmt_libhilog/filemgmt_libhilog.h index 0cdf4267e4d82450aa3773bfdd8c8ea8dbd39eb0..6b21e84706d82c02f73fda3cb2e0a5d60fe854d8 100644 --- a/utils/filemgmt_libhilog/filemgmt_libhilog.h +++ b/utils/filemgmt_libhilog/filemgmt_libhilog.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -21,12 +21,13 @@ #include namespace OHOS { +namespace FileManagement { #ifndef LOG_DOMAIN -#define LOG_DOMAIN 0xD001600 +#define LOG_DOMAIN 0xD004388 #endif #ifndef LOG_TAG -#define LOG_TAG "FileManagement" +#define LOG_TAG "file_api" #endif static constexpr HiviewDFX::HiLogLabel FILEMGMT_LOG_LABEL = {LOG_CORE, LOG_DOMAIN, LOG_TAG}; @@ -42,11 +43,28 @@ static constexpr HiviewDFX::HiLogLabel FILEMGMT_LOG_LABEL = {LOG_CORE, LOG_DOMAI HiviewDFX::HiLog::Level(FILEMGMT_LOG_LABEL, "[%{public}s:%{public}d->%{public}s] " fmt, FILEMGMT_FILE_NAME, \ __LINE__, __FUNCTION__, ##__VA_ARGS__) +#ifdef HILOGF +#undef HILOGF +#endif + +#ifdef HILOGE +#undef HILOGE +#endif + +#ifdef HILOGW +#undef HILOGW +#endif + +#ifdef HILOGI +#undef HILOGI +#endif + #define HILOGD(fmt, ...) FILEMGMT_PRINT_LOG(Debug, fmt, ##__VA_ARGS__) #define HILOGI(fmt, ...) FILEMGMT_PRINT_LOG(Info, fmt, ##__VA_ARGS__) #define HILOGW(fmt, ...) FILEMGMT_PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define HILOGE(fmt, ...) FILEMGMT_PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define HILOGF(fmt, ...) FILEMGMT_PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) +} // namespace FileManagement } // namespace OHOS #endif // FILEMGMT_LIBHILOG_H \ No newline at end of file diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index 82343d36715d30f58d70e29ff0b4b73f669280fd..589e996e06cac1f977e2e77bc185bb2a41353d20 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -19,13 +19,236 @@ #include #include #include +#include #include "n_napi.h" namespace OHOS { namespace FileManagement { namespace LibN { + +constexpr int UNKROWN_ERR = -1; constexpr int ERRNO_NOERR = 0; +constexpr int STORAGE_SERVICE_SYS_CAP_TAG = 13600000; +constexpr int FILEIO_SYS_CAP_TAG = 13900000; +constexpr int USER_FILE_MANAGER_SYS_CAP_TAG = 14000000; +constexpr int USER_FILE_SERVICE_SYS_CAP_TAG = 14300000; +const std::string FILEIO_TAG_ERR_CODE = "code"; + +enum ErrCodeSuffixOfFileIO { + E_PERM = 1, + E_NOENT, + E_SRCH, + E_INTR, + E_IO, + E_NXIO, + E_2BIG, + E_BADF, + E_CHILD, + E_AGAIN, + E_NOMEM, + E_ACCES, + E_FAULT, + E_BUSY, + E_EXIST, + E_XDEV, + E_NODEV, + E_NOTDIR, + E_ISDIR, + E_INVAL, + E_NFILE, + E_MFILE, + E_TXTBSY, + E_FBIG, + E_NOSPC, + E_SPIPE, + E_ROFS, + E_MLINK, + E_DEADLK, + E_NAMETOOLONG, + E_NOSYS, + E_NOTEMPTY, + E_LOOP, + E_WOULDBLOCK, + E_BADR, + E_NOSTR, + E_NODATA, + E_OVERFLOW, + E_BADFD, + E_RESTART, + E_DQUOT, + E_UKERR +}; + +enum ErrCodeSuffixOfUserFileManager { + E_DISPLAYNAME = 1, + E_URIM, + E_SUFFIX, + E_TRASH +}; + +enum ErrCodeSuffixOfStorageService { + E_IPCSS = 1, + E_NOTSUPPORTEDFS, + E_MOUNT, + E_UNMOUNT, + E_VOLUMESTATE, + E_PREPARE, + E_DELETE, + E_NOOBJECT, + E_OUTOFRANGE +}; + +enum ErrCodeSuffixOfUserFileService { + E_IPCS = 1, + E_URIS, + E_GETINFO, + E_GETRESULT, + E_REGISTER, + E_REMOVE, + E_INIT, + E_NOTIFY, + E_CONNECT +}; + +enum CommonErrCode { + E_PERMISSION = 201, + E_PERMISSION_SYS = 202, + E_PARAMS = 401, + E_DEVICENOTSUPPORT = 801, + E_OSNOTSUPPORT = 901 +}; + +const std::unordered_map> errCodeTable { + { ERRNO_NOERR, { ERRNO_NOERR, "No error imformation" } }, + { EPERM, { FILEIO_SYS_CAP_TAG + E_PERM, "Operation not permitted" } }, + { ENOENT, { FILEIO_SYS_CAP_TAG + E_NOENT, "No such file or directory" } }, + { ESRCH, { FILEIO_SYS_CAP_TAG + E_SRCH, "No such process" } }, + { EINTR, { FILEIO_SYS_CAP_TAG + E_INTR, "Interrupted system call" } }, + { EIO, { FILEIO_SYS_CAP_TAG + E_IO, "I/O error" } }, + { ENXIO, { FILEIO_SYS_CAP_TAG + E_NXIO, "No such device or address" } }, + { E2BIG, { FILEIO_SYS_CAP_TAG + E_2BIG, "Arg list too long" } }, + { EBADF, { FILEIO_SYS_CAP_TAG + E_BADF, "Bad file descriptor" } }, + { ECHILD, { FILEIO_SYS_CAP_TAG + E_CHILD, "No child processes" } }, + { EAGAIN, { FILEIO_SYS_CAP_TAG + E_AGAIN, "Try again" } }, + { ENOMEM, { FILEIO_SYS_CAP_TAG + E_NOMEM, "Out of memory" } }, + { EACCES, { FILEIO_SYS_CAP_TAG + E_ACCES, "Permission denied" } }, + { EFAULT, { FILEIO_SYS_CAP_TAG + E_FAULT, "Bad address" } }, + { EBUSY, { FILEIO_SYS_CAP_TAG + E_BUSY, "Device or resource busy" } }, + { EEXIST, { FILEIO_SYS_CAP_TAG + E_EXIST, "File exists" } }, + { EXDEV, { FILEIO_SYS_CAP_TAG + E_XDEV, "Cross-device link" } }, + { ENODEV, { FILEIO_SYS_CAP_TAG + E_NODEV, "No such device" } }, + { ENOTDIR, { FILEIO_SYS_CAP_TAG + E_NOTDIR, "Not a directory" } }, + { EISDIR, { FILEIO_SYS_CAP_TAG + E_ISDIR, "Is a directory" } }, + { EINVAL, { FILEIO_SYS_CAP_TAG + E_INVAL, "Invalid argument" } }, + { ENFILE, { FILEIO_SYS_CAP_TAG + E_NFILE, "File table overflow" } }, + { EMFILE, { FILEIO_SYS_CAP_TAG + E_MFILE, "Too many open files" } }, + { ETXTBSY, { FILEIO_SYS_CAP_TAG + E_TXTBSY, "Text file busy" } }, + { EFBIG, { FILEIO_SYS_CAP_TAG + E_FBIG, "File too large" } }, + { ENOSPC, { FILEIO_SYS_CAP_TAG + E_NOSPC, "No space left on device" } }, + { ESPIPE, { FILEIO_SYS_CAP_TAG + E_SPIPE, "Illegal seek" } }, + { EROFS, { FILEIO_SYS_CAP_TAG + E_ROFS, "Read-only file system" } }, + { EMLINK, { FILEIO_SYS_CAP_TAG + E_MLINK, "Too many links" } }, + { EDEADLK, { FILEIO_SYS_CAP_TAG + E_DEADLK, "Resource deadlock would occur" } }, + { ENAMETOOLONG, { FILEIO_SYS_CAP_TAG + E_NAMETOOLONG, "File name too long" } }, + { ENOSYS, { FILEIO_SYS_CAP_TAG + E_NOSYS, "Function not implemented" } }, + { ENOTEMPTY, { FILEIO_SYS_CAP_TAG + E_NOTEMPTY, "Directory not empty" } }, + { ELOOP, { FILEIO_SYS_CAP_TAG + E_LOOP, "Too many symbolic links encountered" } }, + { EWOULDBLOCK, { FILEIO_SYS_CAP_TAG + E_WOULDBLOCK, "Operation would block" } }, + { EBADR, { FILEIO_SYS_CAP_TAG + E_BADR, "Invalid request descriptor" } }, + { ENOSTR, { FILEIO_SYS_CAP_TAG + E_NOSTR, "Device not a stream" } }, + { ENODATA, { FILEIO_SYS_CAP_TAG + E_NODATA, "No data available" } }, + { EOVERFLOW, { FILEIO_SYS_CAP_TAG + E_OVERFLOW, "Value too large for defined data type" } }, + { EBADFD, { FILEIO_SYS_CAP_TAG + E_BADFD, "File descriptor in bad state" } }, + { ERESTART, { FILEIO_SYS_CAP_TAG + E_RESTART, "Interrupted system call should be restarted" } }, + { EDQUOT, { FILEIO_SYS_CAP_TAG + E_DQUOT, "Quota exceeded" } }, + { UNKROWN_ERR, { FILEIO_SYS_CAP_TAG + E_UKERR, "Unknown error" } }, + { FILEIO_SYS_CAP_TAG + E_PERM, { FILEIO_SYS_CAP_TAG + E_PERM, "Operation not permitted" } }, + { FILEIO_SYS_CAP_TAG + E_NOENT, { FILEIO_SYS_CAP_TAG + E_NOENT, "No such file or directory" } }, + { FILEIO_SYS_CAP_TAG + E_SRCH, { FILEIO_SYS_CAP_TAG + E_SRCH, "No such process" } }, + { FILEIO_SYS_CAP_TAG + E_INTR, { FILEIO_SYS_CAP_TAG + E_INTR, "Interrupted system call" } }, + { FILEIO_SYS_CAP_TAG + E_IO, { FILEIO_SYS_CAP_TAG + E_IO, "I/O error" } }, + { FILEIO_SYS_CAP_TAG + E_NXIO, { FILEIO_SYS_CAP_TAG + E_NXIO, "No such device or address" } }, + { FILEIO_SYS_CAP_TAG + E_2BIG, { FILEIO_SYS_CAP_TAG + E_2BIG, "Arg list too long" } }, + { FILEIO_SYS_CAP_TAG + E_BADF, { FILEIO_SYS_CAP_TAG + E_BADF, "Bad file descriptor" } }, + { FILEIO_SYS_CAP_TAG + E_CHILD, { FILEIO_SYS_CAP_TAG + E_CHILD, "No child processes" } }, + { FILEIO_SYS_CAP_TAG + E_AGAIN, { FILEIO_SYS_CAP_TAG + E_AGAIN, "Try again" } }, + { FILEIO_SYS_CAP_TAG + E_NOMEM, { FILEIO_SYS_CAP_TAG + E_NOMEM, "Out of memory" } }, + { FILEIO_SYS_CAP_TAG + E_ACCES, { FILEIO_SYS_CAP_TAG + E_ACCES, "Permission denied" } }, + { FILEIO_SYS_CAP_TAG + E_FAULT, { FILEIO_SYS_CAP_TAG + E_FAULT, "Bad address" } }, + { FILEIO_SYS_CAP_TAG + E_BUSY, { FILEIO_SYS_CAP_TAG + E_BUSY, "Device or resource busy" } }, + { FILEIO_SYS_CAP_TAG + E_EXIST, { FILEIO_SYS_CAP_TAG + E_EXIST, "File exists" } }, + { FILEIO_SYS_CAP_TAG + E_XDEV, { FILEIO_SYS_CAP_TAG + E_XDEV, "Cross-device link" } }, + { FILEIO_SYS_CAP_TAG + E_NODEV, { FILEIO_SYS_CAP_TAG + E_NODEV, "No such device" } }, + { FILEIO_SYS_CAP_TAG + E_NOTDIR, { FILEIO_SYS_CAP_TAG + E_NOTDIR, "Not a directory" } }, + { FILEIO_SYS_CAP_TAG + E_ISDIR, { FILEIO_SYS_CAP_TAG + E_ISDIR, "Is a directory" } }, + { FILEIO_SYS_CAP_TAG + E_INVAL, { FILEIO_SYS_CAP_TAG + E_INVAL, "Invalid argument" } }, + { FILEIO_SYS_CAP_TAG + E_NFILE, { FILEIO_SYS_CAP_TAG + E_NFILE, "File table overflow" } }, + { FILEIO_SYS_CAP_TAG + E_MFILE, { FILEIO_SYS_CAP_TAG + E_MFILE, "Too many open files" } }, + { FILEIO_SYS_CAP_TAG + E_TXTBSY, { FILEIO_SYS_CAP_TAG + E_TXTBSY, "Text file busy" } }, + { FILEIO_SYS_CAP_TAG + E_FBIG, { FILEIO_SYS_CAP_TAG + E_FBIG, "File too large" } }, + { FILEIO_SYS_CAP_TAG + E_NOSPC, { FILEIO_SYS_CAP_TAG + E_NOSPC, "No space left on device" } }, + { FILEIO_SYS_CAP_TAG + E_SPIPE, { FILEIO_SYS_CAP_TAG + E_SPIPE, "Illegal seek" } }, + { FILEIO_SYS_CAP_TAG + E_ROFS, { FILEIO_SYS_CAP_TAG + E_ROFS, "Read-only file system" } }, + { FILEIO_SYS_CAP_TAG + E_MLINK, { FILEIO_SYS_CAP_TAG + E_MLINK, "Too many links" } }, + { FILEIO_SYS_CAP_TAG + E_DEADLK, { FILEIO_SYS_CAP_TAG + E_DEADLK, "Resource deadlock would occur" } }, + { FILEIO_SYS_CAP_TAG + E_NAMETOOLONG, { FILEIO_SYS_CAP_TAG + E_NAMETOOLONG, "File name too long" } }, + { FILEIO_SYS_CAP_TAG + E_NOSYS, { FILEIO_SYS_CAP_TAG + E_NOSYS, "Function not implemented" } }, + { FILEIO_SYS_CAP_TAG + E_NOTEMPTY, { FILEIO_SYS_CAP_TAG + E_NOTEMPTY, "Directory not empty" } }, + { FILEIO_SYS_CAP_TAG + E_LOOP, { FILEIO_SYS_CAP_TAG + E_LOOP, "Too many symbolic links encountered" } }, + { FILEIO_SYS_CAP_TAG + E_WOULDBLOCK, { FILEIO_SYS_CAP_TAG + E_WOULDBLOCK, "Operation would block" } }, + { FILEIO_SYS_CAP_TAG + E_BADR, { FILEIO_SYS_CAP_TAG + E_BADR, "Invalid request descriptor" } }, + { FILEIO_SYS_CAP_TAG + E_NOSTR, { FILEIO_SYS_CAP_TAG + E_NOSTR, "Device not a stream" } }, + { FILEIO_SYS_CAP_TAG + E_NODATA, { FILEIO_SYS_CAP_TAG + E_NODATA, "No data available" } }, + { FILEIO_SYS_CAP_TAG + E_OVERFLOW, { FILEIO_SYS_CAP_TAG + E_OVERFLOW, "Value too large for defined data type" } }, + { FILEIO_SYS_CAP_TAG + E_BADFD, { FILEIO_SYS_CAP_TAG + E_BADFD, "File descriptor in bad state" } }, + { FILEIO_SYS_CAP_TAG + E_RESTART, { FILEIO_SYS_CAP_TAG + E_RESTART, + "Interrupted system call should be restarted" } }, + { FILEIO_SYS_CAP_TAG + E_DQUOT, { FILEIO_SYS_CAP_TAG + E_DQUOT, "Quota exceeded" } }, + { FILEIO_SYS_CAP_TAG + E_UKERR, { FILEIO_SYS_CAP_TAG + E_UKERR, "Unknown error" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, + "Invalid display name" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, "Invalid uri" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_SUFFIX, { USER_FILE_MANAGER_SYS_CAP_TAG + E_SUFFIX, + "Invalid file extension" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_TRASH, { USER_FILE_MANAGER_SYS_CAP_TAG + E_TRASH, + "File has been put into trash bin" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_IPCSS, { STORAGE_SERVICE_SYS_CAP_TAG + E_IPCSS, "IPC error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_NOTSUPPORTEDFS, { STORAGE_SERVICE_SYS_CAP_TAG + E_NOTSUPPORTEDFS, + "Not supported filesystem" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_MOUNT, { STORAGE_SERVICE_SYS_CAP_TAG + E_MOUNT, "Failed to mount" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_UNMOUNT, { STORAGE_SERVICE_SYS_CAP_TAG + E_UNMOUNT, "Failed to unmount" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_VOLUMESTATE, { STORAGE_SERVICE_SYS_CAP_TAG + E_VOLUMESTATE, + "Incorrect volume state" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_PREPARE, { STORAGE_SERVICE_SYS_CAP_TAG + E_PREPARE, + "Prepare directory or node error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_DELETE, { STORAGE_SERVICE_SYS_CAP_TAG + E_DELETE, + "Delete directory or node error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, "No such object" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_OUTOFRANGE, { STORAGE_SERVICE_SYS_CAP_TAG + E_OUTOFRANGE, + "User id out of range" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, "No such object" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_IPCS, { USER_FILE_SERVICE_SYS_CAP_TAG + E_IPCS, "IPC error" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_URIS, { USER_FILE_SERVICE_SYS_CAP_TAG + E_URIS, "Invalid uri" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETINFO, { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETINFO, + "Fail to get fileextension info" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETRESULT, { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETRESULT, + "Get wrong result" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_REGISTER, { USER_FILE_SERVICE_SYS_CAP_TAG + E_REGISTER, + "Fail to register notification" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_REMOVE, { USER_FILE_SERVICE_SYS_CAP_TAG + E_REMOVE, + "Fail to remove notification" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_INIT, { USER_FILE_SERVICE_SYS_CAP_TAG + E_INIT, + "Fail to init notification agent" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_NOTIFY, { USER_FILE_SERVICE_SYS_CAP_TAG + E_NOTIFY, "Fail to notify agent" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_CONNECT, { USER_FILE_SERVICE_SYS_CAP_TAG + E_CONNECT, + "Fail to connect file access extension ability" } }, + { E_PERMISSION, { E_PERMISSION, "Permission verification failed" } }, + { E_PERMISSION_SYS, { E_PERMISSION_SYS, "The caller is not a system application" } }, + { E_PARAMS, { E_PARAMS, "The input parameter is invalid" } }, + { E_DEVICENOTSUPPORT, { E_DEVICENOTSUPPORT, "The device doesn't support this api" } }, + { E_OSNOTSUPPORT, { E_OSNOTSUPPORT, "The os doesn't support this api" } }, +}; class NError { public: @@ -35,7 +258,9 @@ public: ~NError() = default; explicit operator bool() const; napi_value GetNapiErr(napi_env env); + napi_value GetNapiErr(napi_env env, int code); void ThrowErr(napi_env env); + void ThrowErr(napi_env env, int code); void ThrowErr(napi_env env, std::string errMsg); private: diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index ccafe2a5365ef6b0260d6829c433fd6de52c8d86..538c7cad7fffc9872fa206906159cd8a45f2bd3e 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -52,10 +52,12 @@ public: std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); + std::tuple ToUint64() const; std::tuple ToDouble() const; /* Static helpers to create js objects */ static NVal CreateUndefined(napi_env env); + static NVal CreateBigInt64(napi_env env, int64_t val); static NVal CreateInt64(napi_env env, int64_t val); static NVal CreateInt32(napi_env env, int32_t val); static NVal CreateObject(napi_env env); @@ -63,6 +65,7 @@ public: static NVal CreateUTF8String(napi_env env, std::string str); static NVal CreateUTF8String(napi_env env, const char *str, ssize_t len); static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen); + static NVal CreateArrayString(napi_env env, std::vector strs); static std::tuple CreateArrayBuffer(napi_env env, size_t len); /* SHOULD ONLY BE USED FOR OBJECT */ diff --git a/utils/filemgmt_libn/src/n_error.cpp b/utils/filemgmt_libn/src/n_error.cpp index 88fff79c06db7d291464396d61cbb1a55167c408..698f0f5abb1ef3e016c4febc8eaa02464cb9d91a 100644 --- a/utils/filemgmt_libn/src/n_error.cpp +++ b/utils/filemgmt_libn/src/n_error.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -17,17 +17,47 @@ #include -#include "n_val.h" #include "filemgmt_libhilog.h" +#include "n_val.h" namespace OHOS { namespace FileManagement { namespace LibN { using namespace std; +static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string errMsg) +{ + napi_value businessError = nullptr; + napi_value code = nullptr; + napi_value msg = nullptr; + napi_status status; + code = NVal::CreateInt32(env, errCode).val_; + msg = NVal::CreateUTF8String(env, errMsg).val_; + status = napi_create_error(env, nullptr, msg, &businessError); + if (status != napi_ok) { + HILOGE("Failed to create a BusinessError, error message is %{public}s", errMsg.c_str()); + return nullptr; + } + status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code); + if (status != napi_ok) { + HILOGE("Failed to set code property on Error, error message is %{public}s", errMsg.c_str()); + return nullptr; + } + return businessError; +} + NError::NError() {} -NError::NError(int ePosix) : errno_(ePosix), errMsg_(strerror(errno_)) {} +NError::NError(int errCode) +{ + if (errCodeTable.find(errCode) != errCodeTable.end()) { + errno_ = errCodeTable.at(errCode).first; + errMsg_ = errCodeTable.at(errCode).second; + } else { + errno_ = errCode; + errMsg_ = "Unknown error"; + } +} NError::NError(std::function()> errGen) { @@ -41,15 +71,48 @@ NError::operator bool() const napi_value NError::GetNapiErr(napi_env env) { - napi_value code = NVal::CreateUTF8String(env, to_string(errno_)).val_; - napi_value msg = NVal::CreateUTF8String(env, errMsg_).val_; + if (errno_ == ERRNO_NOERR) { + return nullptr; + } + return GenerateBusinessError(env, errno_, errMsg_); +} + +napi_value NError::GetNapiErr(napi_env env, int errCode) +{ + if (errCode == ERRNO_NOERR) { + return nullptr; + } + int32_t code; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(UNKROWN_ERR).first; + msg = errCodeTable.at(UNKROWN_ERR).second; + } + errno_ = code; + errMsg_ = msg; + return GenerateBusinessError(env, code, msg); +} - napi_value res = nullptr; - napi_status createRes = napi_create_error(env, code, msg, &res); - if (createRes) { - HILOGE("Failed to create an exception, msg = %{public}s", errMsg_.c_str()); +void NError::ThrowErr(napi_env env, int errCode) +{ + int32_t code; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(UNKROWN_ERR).first; + msg = errCodeTable.at(UNKROWN_ERR).second; + } + errno_ = code; + errMsg_ = msg; + napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg)); + if (status != napi_ok) { + HILOGE("Failed to throw a BusinessError, error message is %{public}s", msg.c_str()); } - return res; } void NError::ThrowErr(napi_env env, string errMsg) @@ -59,13 +122,18 @@ void NError::ThrowErr(napi_env env, string errMsg) // Note that ace engine cannot thow errors created by napi_create_error so far napi_status throwStatus = napi_throw_error(env, nullptr, errMsg.c_str()); if (throwStatus != napi_ok) { - HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, errMsg.c_str()); + HILOGE("Failed to throw an Error, error message is %{public}s", errMsg.c_str()); } } void NError::ThrowErr(napi_env env) { - ThrowErr(env, errMsg_); + napi_value tmp = nullptr; + napi_get_and_clear_last_exception(env, &tmp); + napi_status status = napi_throw(env, GenerateBusinessError(env, errno_, errMsg_)); + if (status != napi_ok) { + HILOGE("Failed to throw a BusinessError, error message is %{public}s", errMsg_.c_str()); + } } } // namespace LibN } // namespace FileManagement diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 3c367a1beca8e1be3518d534a1cbca686f1bafce..0c87c93bd8a40023e82ec5c18c3e431a0b43e285 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -131,6 +131,14 @@ tuple NVal::ToDouble() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToUint64() const +{ + uint64_t res = 0; + bool lossless = false; + napi_status status = napi_get_value_bigint_uint64(env_, val_, &res, &lossless); + return make_tuple(status == napi_ok, res, lossless); +} + tuple, uint32_t> NVal::ToStringArray() { napi_status status; @@ -229,6 +237,13 @@ NVal NVal::CreateUndefined(napi_env env) return {env, res}; } +NVal NVal::CreateBigInt64(napi_env env, int64_t val) +{ + napi_value res = nullptr; + napi_create_bigint_int64(env, val, &res); + return { env, res }; +} + NVal NVal::CreateInt64(napi_env env, int64_t val) { napi_value res = nullptr; @@ -282,6 +297,18 @@ NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen) return {env, output_array}; } +NVal NVal::CreateArrayString(napi_env env, vector strs) +{ + napi_value res = nullptr; + napi_create_array(env, &res); + for (size_t i = 0; i < strs.size(); i++) { + napi_value filename; + napi_create_string_utf8(env, strs[i].c_str(), strs[i].length(), &filename); + napi_set_element(env, res, i, filename); + } + return {env, res}; +} + tuple NVal::CreateArrayBuffer(napi_env env, size_t len) { napi_value val;