diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f9367528036ae13520ce8b16158379ba6f340fae..18948e6db2ce20f5a0ca258ef4f0beaf347e12d0 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -193,56 +193,68 @@ 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 = [ + "access_token:libtokenid_sdk", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] } 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" ] } @@ -284,5 +296,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..d98d0d281e79ae5274d57af03ad6fed2a8dda467 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,40 @@ #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 +74,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 +107,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..01a1fea4af0af9f02328d1389510f68694f7aa43 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 f6ec658a4fa60d99183e649250c0ee3a5446e5c0..cb03433e7751d6e500242e4021210a03aaf3ba1c 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 5e47c8be9845b45d9b01733a065ce69532a38137..e789b7261bb3ad7cb7f14a6566cfbcb53696f718 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 88ee86e8355434ac23486371078b5728640ecab5..da979a107c285269f044eb4d4d411ec018b167d0 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,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..a08e204121cd7f19d3f0ac96ca57dc47ff8d8ca4 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 48df4ce3801ca3338819189a99a58588d063525c..f38664770aeb413f353fa76abf4b8f6d83344c23 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 066e1b601f83f10504cf5daa6d27b40a45b5601e..a36854b6977d27250c00b5025eb30c7e4461b60d 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; @@ -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..01d0bb616fb8d0a38c0e455ca21022e6a1b6af2a 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); @@ -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..95c03e01501ab36a739f2fc670cb299853d7c59e 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 @@ -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..ce9b223095f29422adc37f1371d0f665941bcd8c --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2022 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..87378fce0451994f7ff9d6b935c14bf35cd07669 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_n_exporter.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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..c2b968b23b62d5b70beea74e239999089a829402 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_napi.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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