From 9e8de3311621dad2f95c855ed619b3e6cbab7d10 Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Tue, 20 Dec 2022 19:51:55 +0800 Subject: [PATCH 1/2] statfs_environment_securitylabel_add_errcode Signed-off-by: zhuhongtao66 --- interfaces/kits/js/BUILD.gn | 35 ++-- .../environment_n_exporter.cpp | 48 ++++-- .../mod_environment/environment_n_exporter.h | 6 +- .../src/mod_environment/environment_napi.cpp | 6 +- .../js/src/mod_securitylabel/security_label.h | 4 +- .../securitylabel_n_exporter.cpp | 74 +++++---- .../securitylabel_n_exporter.h | 6 +- .../mod_securitylabel/securitylabel_napi.cpp | 7 +- .../js/src/mod_statfs/statfs_n_exporter.cpp | 156 +++++++++++++++++- .../js/src/mod_statfs/statfs_n_exporter.h | 11 +- .../kits/js/src/mod_statfs/statfs_napi.cpp | 6 +- 11 files changed, 263 insertions(+), 96 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f93675280..aeb551d2c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -199,26 +199,23 @@ ohos_shared_library("environment") { relative_install_dir = "module" - include_dirs = [ - "//foundation/arkui/napi/interfaces/kits", - "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi/n_async", - "//third_party/node/src", - ] + 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 = [ + "access_token:libtokenid_sdk", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] } ohos_shared_library("securitylabel") { @@ -229,20 +226,16 @@ ohos_shared_library("securitylabel") { 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" ] } 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 780bdc2bc..009d8eb29 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.cpp @@ -17,31 +17,42 @@ #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" +#include "ipc_skeleton.h" +#include "tokenid_kit.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleEnvironment { +using namespace OHOS::FileManagement::LibN; namespace { const std::string STORAGE_DATA_PATH = "/data"; } + +bool IsSystemApp() +{ + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + + napi_value GetStorageDataDir(napi_env env, napi_callback_info info) { + if (IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } 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) }; } @@ -65,18 +76,23 @@ int GetUserId() napi_value GetUserDataDir(napi_env env, napi_callback_info info) { + if (IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } 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 +109,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 1b46a7b96..01a1fea4a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_n_exporter.h +++ b/interfaces/kits/js/src/mod_environment/environment_n_exporter.h @@ -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 f6ec658a4..cb03433e7 100644 --- a/interfaces/kits/js/src/mod_environment/environment_napi.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_napi.cpp @@ -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 5e47c8be9..e789b7261 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -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 88ee86e83..89d432b22 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp @@ -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,27 @@ 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); + return NError(errno).GetNapiErr(env); } return NVal::CreateUndefined(env).val_; @@ -126,7 +124,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 +133,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, 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 +167,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 +176,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 +185,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 39748659b..a08e20412 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.h @@ -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 48df4ce38..f38664770 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_napi.cpp @@ -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 066e1b601..162e44ace 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.cpp @@ -19,7 +19,7 @@ #include namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleStatfs { using namespace FileManagement::LibN; @@ -514,6 +514,158 @@ napi_value GetTotalBytes(napi_env env, napi_callback_info info) NVal cb(env, funcArg[NARG_POS::SECOND]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } + +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, 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, 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 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 dd25b1724..b7e949239 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h +++ b/interfaces/kits/js/src/mod_statfs/statfs_n_exporter.h @@ -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); @@ -41,7 +42,13 @@ 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); + +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 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 2c0f27dc2..5342dbfbf 100644 --- a/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp +++ b/interfaces/kits/js/src/mod_statfs/statfs_napi.cpp @@ -16,7 +16,7 @@ #include "statfs_n_exporter.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleStatfs { /*********************************************** * Module export and register @@ -26,6 +26,8 @@ napi_value StatfsExport(napi_env env, napi_value exports) static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getFreeBytes", GetFreeBytes), DECLARE_NAPI_FUNCTION("getTotalBytes", GetTotalBytes), + 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; @@ -33,5 +35,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 -- Gitee From 71f4884216cc10fa58f876726c50ee5108c3038b Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Thu, 22 Dec 2022 16:40:52 +0800 Subject: [PATCH 2/2] libn_add_innerkits Signed-off-by: zhuhongtao66 --- bundle.json | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bundle.json b/bundle.json index be6f41052..051ad6b08 100644 --- a/bundle.json +++ b/bundle.json @@ -54,6 +54,15 @@ ], "header_base": "//foundation/filemanagement/file_api/interfaces/kits/native/remote_uri" } + }, + { + "name": "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", + "header": { + "header_files": [ + "filemgmt_libn.h" + ], + "header_base": "//foundation/filemanagement/file_api/utils/filemgmt_libn/include" + } } ], "test": [ -- Gitee