diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp index 67a2d7543db24ae7728f76cca316c673fd70c7c5..fd63179a972bcb1c96e72746165d32b99e83c74a 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -34,6 +34,7 @@ #include "root_iterator_entity.h" #include "securec.h" #include "uri.h" +#include "file_access_check_util.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; @@ -67,25 +68,20 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i return NapiFileInfoExporter::ThrowError(env, EINVAL); } auto context = OHOS::AbilityRuntime::GetStageModeContext(env, funcArg.GetArg(PARAM0)); - if (context == nullptr) { - HILOG_ERROR("FileAccessHelperConstructor: failed to get native context"); - return NapiFileInfoExporter::ThrowError(env, E_GETRESULT); - } + CHECK_STATUS_RETURN(context != nullptr, NapiFileInfoExporter::ThrowError(env, E_GETRESULT), + "FileAccessHelperConstructor: failed to get native context"); if (funcArg.GetArgc() == NARG_CNT::ONE) { createResult = FileAccessHelper::Creator(context); } else if (funcArg.GetArgc() == NARG_CNT::TWO) { std::vector wants; bool suss = UnwrapArrayWantFromJS(env, funcArg.GetArg(PARAM1), wants); - if (!suss) { - HILOG_ERROR("UnwrapArrayWantFromJS failed to get native wants"); - return NapiFileInfoExporter::ThrowError(env, E_GETRESULT); - } + CHECK_STATUS_RETURN(suss, NapiFileInfoExporter::ThrowError(env, E_GETRESULT), + "UnwrapArrayWantFromJS failed to get native wants"); createResult = FileAccessHelper::Creator(context, wants); } - if (createResult.first == nullptr || createResult.second != ERR_OK) { - HILOG_ERROR("FileAccessHelperConstructor: Creator failed ret=%{public}d", createResult.second); - return NapiFileInfoExporter::ThrowError(env, createResult.second); - } + CHECK_STATUS_RETURN(!(createResult.first == nullptr || createResult.second != ERR_OK), + NapiFileInfoExporter::ThrowError(env, createResult.second), + "FileAccessHelperConstructor: Creator failed ret=%{public}d", createResult.second); g_fileAccessHelperList.emplace_back(createResult.first); HILOG_INFO("g_fileAccessHelperList size %{public}zu", g_fileAccessHelperList.size()); auto finalize = [](napi_env env, void *data, void *hint) { @@ -121,10 +117,8 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { return nullptr; } - if (argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok) { - HILOG_ERROR("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); - return nullptr; - } + CHECK_STATUS_RETURN(!(argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok), + nullptr, "Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); if (napi_new_instance(env, cons, ARGS_ONE, args, &result) != napi_ok) { return nullptr; } @@ -135,27 +129,18 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { return nullptr; } - if (argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok) { - HILOG_ERROR("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); - return nullptr; - } + CHECK_STATUS_RETURN(!(argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok), + nullptr, "Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); if (napi_new_instance(env, cons, ARGS_TWO, args, &result) != napi_ok) { return nullptr; } } - if (!IsTypeForNapiValue(env, result, napi_object)) { - HILOG_ERROR("IsTypeForNapiValue isn`t object"); - return nullptr; - } + CHECK_STATUS_RETURN(IsTypeForNapiValue(env, result, napi_object), nullptr, + "IsTypeForNapiValue isn't object"); FileAccessHelper *fileAccessHelper = nullptr; - if (napi_unwrap(env, result, (void **)&fileAccessHelper) != napi_ok) { - HILOG_ERROR("Faild to get fileAccessHelper"); - return nullptr; - } - if (fileAccessHelper == nullptr) { - HILOG_ERROR("fileAccessHelper is nullptr"); - return nullptr; - } + CHECK_STATUS_RETURN(napi_unwrap(env, result, (void **)&fileAccessHelper) == napi_ok, nullptr, + "Faild to get fileAccessHelper"); + CHECK_STATUS_RETURN(fileAccessHelper != nullptr, nullptr, "fileAccessHelper is nullptr"); HILOG_INFO("g_fileAccessHelperList size %{public}zu", g_fileAccessHelperList.size()); return result; } diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp index 2d0d4ec5981f4714648a99d37068837180d2cf2f..7e29c398562327c492b519fd4e9960d1621a4338 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp @@ -71,6 +71,42 @@ static napi_status SetValueArray(const napi_env& env, return status; } +void NapiObserver::NapiWorkScope(uv_work_t *work, int status) +{ + std::unique_ptr param(reinterpret_cast(work->data)); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(param->napiObserver->env_, &scope); + if (scope == nullptr) { + HILOG_ERROR("napi_open_handle_scope failed"); + return; + } + + NVal napiNotifyMessage = NVal::CreateObject(param->napiObserver->env_); + napiNotifyMessage.AddProp("type", + NVal::CreateInt32(param->napiObserver->env_, int((param->iNotifyMessage).notifyType_)).val_); + SetValueArray(param->napiObserver->env_, "uris", param->iNotifyMessage.uris_, napiNotifyMessage.val_); + + napi_value callback = nullptr; + napi_value args[ARGS_ONE] = {napiNotifyMessage.val_}; + napi_status ret = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, + &callback); + if (ret != napi_ok) { + HILOG_ERROR("Notify get reference failed, status:%{public}d.", status); + napi_close_handle_scope(param->napiObserver->env_, scope); + return; + } + napi_value global = nullptr; + napi_get_global(param->napiObserver->env_, &global); + napi_value result = nullptr; + ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); + if (ret != napi_ok) { + HILOG_ERROR("Notify failed, status:%{public}d.", ret); + napi_close_handle_scope(param->napiObserver->env_, scope); + return; + } + napi_close_handle_scope(param->napiObserver->env_, scope); +} + void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) { uv_loop_s *loop = nullptr; @@ -92,38 +128,8 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) int ret = uv_queue_work(loop, work_.get(), [](uv_work_t *work) {}, [](uv_work_t *work, int status) { - std::unique_ptr param(reinterpret_cast(work->data)); - napi_handle_scope scope = nullptr; - napi_open_handle_scope(param->napiObserver->env_, &scope); - if (scope == nullptr) { - HILOG_ERROR("napi_open_handle_scope failed"); - return; - } - - NVal napiNotifyMessage = NVal::CreateObject(param->napiObserver->env_); - napiNotifyMessage.AddProp("type", - NVal::CreateInt32(param->napiObserver->env_, int((param->iNotifyMessage).notifyType_)).val_); - SetValueArray(param->napiObserver->env_, "uris", param->iNotifyMessage.uris_, napiNotifyMessage.val_); - - napi_value callback = nullptr; - napi_value args[ARGS_ONE] = {napiNotifyMessage.val_}; - napi_status ret = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, - &callback); - if (ret != napi_ok) { - HILOG_ERROR("Notify get reference failed, status:%{public}d.", status); - napi_close_handle_scope(param->napiObserver->env_, scope); - return; - } - napi_value global = nullptr; - napi_get_global(param->napiObserver->env_, &global); - napi_value result = nullptr; - ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); - if (ret != napi_ok) { - HILOG_ERROR("Notify failed, status:%{public}d.", ret); - napi_close_handle_scope(param->napiObserver->env_, scope); - return; - } - napi_close_handle_scope(param->napiObserver->env_, scope); + NapiWorkScope(work, status); + return; }); if (ret == 0) { callbackParam.release(); diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.h b/frameworks/js/napi/file_access_module/napi_observer_callback.h index 87e3cb27c78818033e6e231fea5f8db0be84b93c..553b383b93dd08c56b13d83c02c1ea27a49f5e63 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.h +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.h @@ -42,6 +42,7 @@ private: }; napi_env env_; std::unique_ptr work_ = nullptr; + static void NapiWorkScope(uv_work_t *work, int status); }; class NapiObserverCallback : public ObserverCallbackStub { diff --git a/frameworks/js/napi/file_access_module/napi_utils.cpp b/frameworks/js/napi/file_access_module/napi_utils.cpp index 3257720dbd231b8e686987927488f66b50b2f8b6..969665d39c77e198cbcf2047d66c0f8939260feb 100644 --- a/frameworks/js/napi/file_access_module/napi_utils.cpp +++ b/frameworks/js/napi/file_access_module/napi_utils.cpp @@ -59,10 +59,8 @@ bool CheckSuffix(std::vector suffixs) return true; } -int GetFileFilterParam(const NVal &argv, FileFilter &filter) +int GetSuffixDisplayNameMimeType(const NVal &argv, FileFilter &filter, bool ret) { - bool ret = false; - filter.SetHasFilter(false); if (argv.TypeIs(napi_undefined)) { return ERR_OK; } @@ -104,6 +102,11 @@ int GetFileFilterParam(const NVal &argv, FileFilter &filter) filter.SetHasFilter(true); } + return ERR_OK; +} + +int GetFileSizeLastModified(const NVal &argv, FileFilter &filter, bool ret) +{ if (argv.HasProp("fileSizeOver")) { int64_t fileSizeOver; std::tie(ret, fileSizeOver) = argv.GetProp("fileSizeOver").ToInt64(); @@ -146,10 +149,28 @@ int GetFileFilterParam(const NVal &argv, FileFilter &filter) filter.SetHasFilter(true); } + return ERR_OK; +} + +int GetFileFilterParam(const NVal &argv, FileFilter &filter) +{ + bool ret = false; + filter.SetHasFilter(false); + + int ret1 = GetSuffixDisplayNameMimeType(argv, filter, ret); + if (ret1 != ERR_OK) { + return ret1; + } + int ret2 = GetFileSizeLastModified(argv, filter, ret); + if (ret2 != ERR_OK) { + return ret1; + } + if (!filter.GetHasFilter()) { HILOG_ERROR("FileFilter must have one property."); return EINVAL; } + return ERR_OK; } } // namespace FileAccessFwk diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index bf1e9ae43272cf030666e3c58c27f5020936ee42..5196649219f7f59ade393b0105d3c909987f58cc 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -68,6 +68,8 @@ public: Creator(const std::shared_ptr &context, const std::vector &wants); static std::shared_ptr Creator(const sptr &token, const std::vector &wants); + static std::pair, int> DoCreatorHelper(const sptr &token, + const std::vector &wants); bool Release(); int Access(Uri &uri, bool &isExist); diff --git a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h index c8a475a0b581216da32ad78906739a7fc93708ea..97644d5aacb79b419651824687f0badd6541a955 100644 --- a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h @@ -59,6 +59,15 @@ struct FileInfoNumParam { bool recursion; }; +struct FileFilterValue { + napi_value suffixArray = nullptr; + napi_value displayNameArray = nullptr; + napi_value mimeTypeArray = nullptr; + napi_value nativeFileSizeOver = nullptr; + napi_value nativeLastModifiedAfter = nullptr; + napi_value nativeExcludeMedia = nullptr; +}; + class JsFileAccessExtAbility : public FileAccessExtAbility { public: JsFileAccessExtAbility(JsRuntime &jsRuntime); @@ -117,6 +126,7 @@ private: Value> &results); static bool ParserFileInfoNumJsResult(napi_env &env, napi_value &nativeValue, bool &success, uint32_t &counts); static int MakeStringNativeArray(napi_env &env, std::vector &inputArray, napi_value resultArray); + static int CreateNativeValue(napi_env &env, const FileFilter &filter, struct FileFilterValue &fileFilter); static int MakeJsNativeFileFilter(napi_env &env, const FileFilter &filter, napi_value nativeFilter); static bool BuildFilterParam(napi_env &env, const FileFilter &filter, const FilterParam ¶m, napi_value *argv, size_t &argc); diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index cfd22d1d57e88be79978e17309fc137b4597137a..22f187cbfede02439d3de5b85b1b95b7aa3c3812 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -174,63 +174,79 @@ static bool IsSystemApp() return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx); } -std::pair, int> FileAccessHelper::Creator( - const std::shared_ptr &context) +std::pair, int> FileAccessHelper::DoCreatorHelper( + const sptr &token, const std::vector &wants) { - UserAccessTracer trace; - trace.Start("Creator"); - if (context == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); - return {nullptr, EINVAL}; - } - if (!IsSystemApp()) { - HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); - return {nullptr, E_PERMISSION_SYS}; - } - sptr bm = FileAccessHelper::GetBundleMgrProxy(); - FileAccessHelper::wants_.clear(); std::unordered_map> cMap; - std::vector extensionInfos; - bool ret = bm->QueryExtensionAbilityInfos( - AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, GetUserId(), extensionInfos); - if (!ret) { - HILOG_ERROR("FileAccessHelper::Creator QueryExtensionAbilityInfos failed"); - return {nullptr, E_GETINFO}; - } - for (size_t i = 0; i < extensionInfos.size(); i++) { - AAFwk::Want wantTem; - wantTem.SetElementName(extensionInfos[i].bundleName, extensionInfos[i].name); + for (size_t i = 0; i < wants.size(); i++) { sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); if (fileAccessExtConnection == nullptr) { HILOG_ERROR("new fileAccessExtConnection fail"); return {nullptr, E_GETRESULT}; } + if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wantTem, context->GetToken()); + fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); } + sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { HILOG_ERROR("Creator get invalid fileExtProxy"); return {nullptr, E_CONNECT}; } + std::shared_ptr connectInfo = std::make_shared(); if (!connectInfo) { HILOG_ERROR("Creator, connectInfo == nullptr"); return {nullptr, E_GETRESULT}; } - FileAccessHelper::wants_.push_back(wantTem); - connectInfo->want = wantTem; + + connectInfo->want = wants[i]; connectInfo->fileAccessExtConnection = fileAccessExtConnection; - cMap.emplace(extensionInfos[i].bundleName, connectInfo); + string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); + if (bundleName.length() == 0) { + HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); + return {nullptr, E_GETRESULT}; + } + cMap.insert(std::pair>(bundleName, connectInfo)); } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); + + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(token, cMap); if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed"); + HILOG_ERROR("Creator failed, create FileAccessHelper failed"); return {nullptr, E_GETRESULT}; } + return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; } +std::pair, int> FileAccessHelper::Creator( + const std::shared_ptr &context) +{ + UserAccessTracer trace; + trace.Start("Creator"); + if (context == nullptr) { + HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); + return {nullptr, EINVAL}; + } + if (!IsSystemApp()) { + HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); + return {nullptr, E_PERMISSION_SYS}; + } + if (GetRegisteredFileAccessExtAbilityInfo(FileAccessHelper::wants_) != ERR_OK) { + HILOG_ERROR("GetRegisteredFileAccessExtAbilityInfo failed"); + return {nullptr, E_GETINFO}; + } + + std::pair, int> + ptrFileAccessHelper = DoCreatorHelper(context->GetToken(), FileAccessHelper::wants_); + if (ptrFileAccessHelper.second != ERR_OK) { + return ptrFileAccessHelper; + } + + return {ptrFileAccessHelper.first, ERR_OK}; +} + std::pair, int> FileAccessHelper::Creator( const std::shared_ptr &context, const std::vector &wants) { @@ -256,46 +272,12 @@ std::pair, int> FileAccessHelper::Creator( return {nullptr, E_GETINFO}; } - std::unordered_map> cMap; - for (size_t i = 0; i < wants.size(); i++) { - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); - if (fileAccessExtConnection == nullptr) { - HILOG_ERROR("new fileAccessExtConnection fail"); - return {nullptr, E_GETRESULT}; - } - - if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wants[i], context->GetToken()); - } - - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); - if (fileExtProxy == nullptr) { - HILOG_ERROR("Creator get invalid fileExtProxy"); - return {nullptr, E_CONNECT}; - } - - std::shared_ptr connectInfo = std::make_shared(); - if (connectInfo == nullptr) { - HILOG_ERROR("Creator, connectInfo == nullptr"); - return {nullptr, E_GETRESULT}; - } - - connectInfo->want = wants[i]; - connectInfo->fileAccessExtConnection = fileAccessExtConnection; - string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); - if (bundleName.length() == 0) { - HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); - return {nullptr, E_GETRESULT}; - } - cMap.insert(std::pair>(bundleName, connectInfo)); - } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); - if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("Creator failed, create FileAccessHelper failed"); - return {nullptr, E_GETRESULT}; + std::pair, int> ptrFileAccessHelper = DoCreatorHelper(context->GetToken(), wants); + if (ptrFileAccessHelper.second != ERR_OK) { + return ptrFileAccessHelper; } - return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; + return {ptrFileAccessHelper.first, ERR_OK}; } std::shared_ptr FileAccessHelper::Creator(const sptr &token, @@ -323,46 +305,12 @@ std::shared_ptr FileAccessHelper::Creator(const sptr> cMap; - for (size_t i = 0; i < wants.size(); i++) { - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); - if (fileAccessExtConnection == nullptr) { - HILOG_ERROR("new fileAccessExtConnection fail"); - return nullptr; - } - - if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); - } - - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); - if (fileExtProxy == nullptr) { - HILOG_ERROR("Creator get invalid fileExtProxy"); - return nullptr; - } - - std::shared_ptr connectInfo = std::make_shared(); - if (!connectInfo) { - HILOG_ERROR("Creator, connectInfo == nullptr"); - return nullptr; - } - - connectInfo->want = wants[i]; - connectInfo->fileAccessExtConnection = fileAccessExtConnection; - string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); - if (bundleName.length() == 0) { - HILOG_ERROR("Creator GetKeyOfWants bundleName not found"); - return nullptr; - } - cMap.insert(std::pair>(bundleName, connectInfo)); - } - FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(token, cMap); - if (ptrFileAccessHelper == nullptr) { - HILOG_ERROR("Creator failed, create FileAccessHelper failed"); + std::pair, int> ptrFileAccessHelper = DoCreatorHelper(token, wants); + if (ptrFileAccessHelper.second != ERR_OK) { return nullptr; } - return std::shared_ptr(ptrFileAccessHelper); + return ptrFileAccessHelper.first; } bool FileAccessHelper::Release() diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 1250a6f30527621318971f451cff314fd8465313..94e764e247870a6840d404506539ced551456bbb 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -40,6 +40,7 @@ #include "parameter.h" #include "system_ability_definition.h" #include "user_access_tracer.h" +#include "file_access_check_util.h" namespace OHOS { namespace FileAccessFwk { @@ -862,97 +863,86 @@ int JsFileAccessExtAbility::MakeStringNativeArray(napi_env &env, std::vector suffixVec = filter.GetSuffix(); - int errorCode = MakeStringNativeArray(env, suffixVec, suffixArray); - if (errorCode != ERR_OK) { - HILOG_ERROR("Create Suffix native array value fail, code:%{public}d.", errorCode); - return errorCode; - } - - napi_value displayNameArray = nullptr; - napi_create_array_with_length(env, filter.GetDisplayName().size(), &displayNameArray); - if (displayNameArray == nullptr) { - HILOG_ERROR("Create DisplayName native array value fail."); - return E_GETRESULT; - } + int errorCode = MakeStringNativeArray(env, suffixVec, fileFilter.suffixArray); + CHECK_STATUS_RETURN(errorCode == ERR_OK, errorCode, + "Create Suffix native array value fail, code:%{public}d.", errorCode); + + napi_create_array_with_length(env, filter.GetDisplayName().size(), &fileFilter.displayNameArray); + CHECK_STATUS_RETURN(fileFilter.displayNameArray != nullptr, E_GETRESULT, + "Create DisplayName native array value fail."); std::vector displayNameVec = filter.GetDisplayName(); - errorCode = MakeStringNativeArray(env, displayNameVec, displayNameArray); - if (errorCode != ERR_OK) { - HILOG_ERROR("Create DisplayName native array value fail, code:%{public}d.", errorCode); - return errorCode; - } + errorCode = MakeStringNativeArray(env, displayNameVec, fileFilter.displayNameArray); + CHECK_STATUS_RETURN(errorCode == ERR_OK, errorCode, + "Create DisplayName native array value fail, code:%{public}d.", errorCode); - napi_value mimeTypeArray = nullptr; - napi_create_array_with_length(env, filter.GetMimeType().size(), &mimeTypeArray); - if (mimeTypeArray == nullptr) { - HILOG_ERROR("Create MimeType native array value fail."); - return E_GETRESULT; - } + napi_create_array_with_length(env, filter.GetMimeType().size(), &fileFilter.mimeTypeArray); + CHECK_STATUS_RETURN(fileFilter.mimeTypeArray != nullptr, E_GETRESULT, + "Create MimeType native array value fail."); std::vector mimeTypeVec = filter.GetMimeType(); - errorCode = MakeStringNativeArray(env, mimeTypeVec, mimeTypeArray); - if (errorCode != ERR_OK) { - HILOG_ERROR("Create MimeType native array value fail, code:%{public}d.", errorCode); - return errorCode; - } + errorCode = MakeStringNativeArray(env, mimeTypeVec, fileFilter.mimeTypeArray); + CHECK_STATUS_RETURN(errorCode == ERR_OK, errorCode, + "Create MimeType native array value fail, code:%{public}d.", errorCode); - napi_value nativeFileSizeOver = nullptr; - napi_create_int64(env, filter.GetFileSizeOver(), &nativeFileSizeOver); - if (nativeFileSizeOver == nullptr) { - HILOG_ERROR("Create NativeFileSizeOver native js value fail."); - return E_GETRESULT; - } + napi_create_int64(env, filter.GetFileSizeOver(), &fileFilter.nativeFileSizeOver); + CHECK_STATUS_RETURN(fileFilter.nativeFileSizeOver != nullptr, E_GETRESULT, + "Create NativeFileSizeOver native js value fail."); - napi_value nativeLastModifiedAfter = nullptr; - napi_create_double(env, filter.GetLastModifiedAfter(), &nativeLastModifiedAfter); - if (nativeLastModifiedAfter == nullptr) { - HILOG_ERROR("Create NativeLastModifiedAfter native js value fail."); - return E_GETRESULT; - } + napi_create_double(env, filter.GetLastModifiedAfter(), &fileFilter.nativeLastModifiedAfter); + CHECK_STATUS_RETURN(fileFilter.nativeLastModifiedAfter != nullptr, E_GETRESULT, + "Create NativeLastModifiedAfter native js value fail."); - napi_value nativeExcludeMedia = nullptr; - napi_get_boolean(env, filter.GetExcludeMedia(), &nativeExcludeMedia); - if (nativeExcludeMedia == nullptr) { - HILOG_ERROR("Create NativeExcludeMedia native js value fail."); - return E_GETRESULT; + napi_get_boolean(env, filter.GetExcludeMedia(), &fileFilter.nativeExcludeMedia); + CHECK_STATUS_RETURN(fileFilter.nativeExcludeMedia != nullptr, E_GETRESULT, + "Create NativeExcludeMedia native js value fail."); + + return ERR_OK; +} + +int JsFileAccessExtAbility::MakeJsNativeFileFilter(napi_env &env, const FileFilter &filter, napi_value nativeFilter) +{ + struct FileFilterValue fileFilter; + int ret = CreateNativeValue(env, filter, fileFilter); + if (ret != ERR_OK) { + return ret; } - if (napi_set_named_property(env, nativeFilter, "suffix", suffixArray) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "suffix", fileFilter.suffixArray) != napi_ok) { HILOG_ERROR("Set suffix property to Filter NativeValue fail."); return EINVAL; } - if (napi_set_named_property(env, nativeFilter, "displayName", displayNameArray) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "displayName", fileFilter.displayNameArray) != napi_ok) { HILOG_ERROR("Set displayName property to Filter NativeValue fail."); return EINVAL; } - if (napi_set_named_property(env, nativeFilter, "mimeType", mimeTypeArray) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "mimeType", fileFilter.mimeTypeArray) != napi_ok) { HILOG_ERROR("Set mimeType property to Filter NativeValue fail."); return EINVAL; } - if (napi_set_named_property(env, nativeFilter, "fileSizeOver", nativeFileSizeOver) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "fileSizeOver", fileFilter.nativeFileSizeOver) != napi_ok) { HILOG_ERROR("Set fileSizeOver property to Filter NativeValue fail."); return EINVAL; } - if (napi_set_named_property(env, nativeFilter, "lastModifiedAfter", nativeLastModifiedAfter) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "lastModifiedAfter", + fileFilter.nativeLastModifiedAfter) != napi_ok) { HILOG_ERROR("Set lastModifiedAfter property to Filter NativeValue fail."); return EINVAL; } - if (napi_set_named_property(env, nativeFilter, "excludeMedia", nativeExcludeMedia) != napi_ok) { + if (napi_set_named_property(env, nativeFilter, "excludeMedia", fileFilter.nativeExcludeMedia) != napi_ok) { HILOG_ERROR("Set excludeMedia property to Filter NativeValue fail."); return EINVAL; } @@ -1476,10 +1466,7 @@ int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo & UserAccessTracer trace; trace.Start("GetFileInfoFromUri"); auto value = std::make_shared>(); - if (value == nullptr) { - HILOG_ERROR("GetFileInfoFromUri value is nullptr."); - return E_GETRESULT; - } + CHECK_STATUS_RETURN(value != nullptr, E_GETRESULT, "GetFileInfoFromUri value is nullptr."); auto argParser = [selectFile](napi_env &env, napi_value *argv, size_t &argc) -> bool { napi_value nativeUri = nullptr; @@ -1517,15 +1504,9 @@ int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo & }; auto errCode = CallJsMethod("getFileInfoFromUri", jsRuntime_, jsObj_.get(), argParser, retParser); - if (errCode != ERR_OK) { - HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); - return errCode; - } + CHECK_STATUS_RETURN(errCode == ERR_OK, errCode, "CallJsMethod error, code:%{public}d.", errCode); - if (value->code != ERR_OK) { - HILOG_ERROR("fileio fail."); - return value->code; - } + CHECK_STATUS_RETURN(value->code == ERR_OK, value->code, "fileio fail."); fileInfo = std::move(value->data); return ERR_OK; @@ -1536,10 +1517,7 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec UserAccessTracer trace; trace.Start("GetFileInfoFromRelativePath"); auto value = std::make_shared>(); - if (value == nullptr) { - HILOG_ERROR("GetFileInfoFromRelativePath value is nullptr."); - return E_GETRESULT; - } + CHECK_STATUS_RETURN((value != nullptr), E_GETRESULT, "GetFileInfoFromRelativePath value is nullptr."); auto argParser = [selectFileRealtivePath](napi_env &env, napi_value *argv, size_t &argc) -> bool { napi_value nativePath = nullptr; @@ -1577,15 +1555,9 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec }; auto errCode = CallJsMethod("getFileInfoFromRelativePath", jsRuntime_, jsObj_.get(), argParser, retParser); - if (errCode != ERR_OK) { - HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); - return errCode; - } + CHECK_STATUS_RETURN((errCode == ERR_OK), errCode, "CallJsMethod error, code:%{public}d.", errCode); - if (value->code != ERR_OK) { - HILOG_ERROR("fileio fail."); - return value->code; - } + CHECK_STATUS_RETURN((value->code == ERR_OK), value->code, "fileio fail."); fileInfo = std::move(value->data); return ERR_OK; diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index f58a42d3e96f1f44d1d74cd3ad77500c280a1ee9..25a95484b563af09e8278307364c5f4ba8344916 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -1101,10 +1101,8 @@ export default class FileExtAbility extends Extension { uri: newFileUri, code: ERR_OK, }; - } else if (newPath.indexOf(oldPath) === 0 && newPath.charAt(oldPath.length) === '/') { - // move to a subdirectory of the source directory - return uriReturnObject('', E_GETRESULT); } + try { // The source file does not exist or the destination is not a directory let isAccess = fs.accessSync(oldPath); diff --git a/utils/file_access_check_util.h b/utils/file_access_check_util.h new file mode 100644 index 0000000000000000000000000000000000000000..9d02f5882e31dc4a3b343d617d3b4f9984829bd7 --- /dev/null +++ b/utils/file_access_check_util.h @@ -0,0 +1,29 @@ +/* + * 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 FILE_ACCESS_UTIL_H +#define FILE_ACCESS_UTIL_H + +#ifdef CHECK_STATUS_RETURN +#undef CHECK_STATUS_RETURN +#endif + +#define CHECK_STATUS_RETURN(status, returnContext, fmt, ...) \ + if (!(status)) { \ + HILOG_ERROR(fmt, ##__VA_ARGS__); \ + return returnContext; \ + } + +#endif // FILE_ACCESS_UTIL_H \ No newline at end of file