From 678ceee4ce158b0e660751234fe98651f28f1d54 Mon Sep 17 00:00:00 2001 From: liuhonglin9 Date: Fri, 15 Sep 2023 06:14:09 +0000 Subject: [PATCH] Reduce the number of lines of large functions Signed-off-by: liuhonglin9 Change-Id: Ieb334d9f0544ac7e78da4b0e0eec480618f3cfac Signed-off-by: liuhonglin9 --- .../file_info/napi_file_info_exporter.cpp | 63 +++----- .../napi_fileaccess_helper.cpp | 137 +++++------------- .../root_info/napi_root_info_exporter.cpp | 61 +++----- .../file_access/src/file_access_helper.cpp | 16 -- .../main/ets/FileExtensionAbility/Common.ts | 16 +- .../FileExtensionAbility.ts | 64 ++------ .../FileExtensionAbility/ListScanFileInfo.ts | 17 +-- 7 files changed, 107 insertions(+), 267 deletions(-) diff --git a/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp b/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp index cc40f38a..ea03c17b 100644 --- a/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp +++ b/frameworks/js/napi/file_access_module/file_info/napi_file_info_exporter.cpp @@ -79,51 +79,43 @@ napi_value NapiFileInfoExporter::Constructor(napi_env env, napi_callback_info in return funcArg.GetThisVar(); } +static napi_value ThrowErrorr(napi_env env, int code) { + NError(code).ThrowErr(env); + return nullptr; +} + napi_value NapiFileInfoExporter::ListFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, EINVAL); } - FileFilter filter({}, {}, {}, FileFilter::INVALID_SIZE, FileFilter::INVALID_MODIFY_AFTER, false, false); if (funcArg.GetArgc() == NARG_CNT::ONE) { auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, ret); } } - auto fileInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (fileInfoEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - if (IsDirectory(fileInfoEntity->fileInfo.mode) != ERR_OK) { HILOG_ERROR("current FileInfo's mode error"); return NVal::CreateUndefined(env).val_; } - if (fileInfoEntity->fileAccessHelper == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - auto objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); if (objFileIteratorExporter == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); if (fileIteratorEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - { std::lock_guard lock(fileIteratorEntity->entityOperateMutex); fileIteratorEntity->fileAccessHelper = fileInfoEntity->fileAccessHelper; @@ -136,11 +128,9 @@ napi_value NapiFileInfoExporter::ListFile(napi_env env, napi_callback_info info) auto ret = fileInfoEntity->fileAccessHelper->ListFile(fileInfoEntity->fileInfo, fileIteratorEntity->offset, MAX_COUNT, fileIteratorEntity->filter, fileIteratorEntity->fileInfoVec); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, ret); } } - return NVal(env, objFileIteratorExporter).val_; } @@ -148,47 +138,34 @@ napi_value NapiFileInfoExporter::ScanFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, EINVAL); } - FileFilter filter({}, {}, {}, FileFilter::INVALID_SIZE, FileFilter::INVALID_MODIFY_AFTER, false, false); if (funcArg.GetArgc() == NARG_CNT::ONE) { auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, ret); } } - auto fileInfoEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (fileInfoEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - if (IsDirectory(fileInfoEntity->fileInfo.mode) != ERR_OK) { HILOG_ERROR("current FileInfo's mode error"); return NVal::CreateUndefined(env).val_; } - if (fileInfoEntity->fileAccessHelper == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - auto objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); if (objFileIteratorExporter == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); if (fileIteratorEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, E_GETRESULT); } - { std::lock_guard lock(fileIteratorEntity->entityOperateMutex); fileIteratorEntity->fileAccessHelper = fileInfoEntity->fileAccessHelper; @@ -201,11 +178,9 @@ napi_value NapiFileInfoExporter::ScanFile(napi_env env, napi_callback_info info) auto ret = fileInfoEntity->fileAccessHelper->ScanFile(fileInfoEntity->fileInfo, fileIteratorEntity->offset, MAX_COUNT, fileIteratorEntity->filter, fileIteratorEntity->fileInfoVec); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ThrowErrorr(env, ret); } } - return NVal(env, objFileIteratorExporter).val_; } 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 cad00621..329eff87 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -52,31 +52,30 @@ namespace { std::list> g_fileAccessHelperList = {}; +static napi_value HelperThrowError(napi_env env, int code) { + NError(code).ThrowErr(env); + return nullptr; +} + static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - napi_value thisVar = funcArg.GetThisVar(); std::pair, int> createResult{nullptr, ERR_OK}; bool isStageMode = false; napi_status status = AbilityRuntime::IsStageContext(env, funcArg.GetArg(PARAM0), isStageMode); if (status != napi_ok || !isStageMode) { HILOG_INFO("No support FA Model"); - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - auto context = OHOS::AbilityRuntime::GetStageModeContext(env, funcArg.GetArg(PARAM0)); if (context == nullptr) { HILOG_ERROR("FileAccessHelperConstructor: failed to get native context"); - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } - if (funcArg.GetArgc() == NARG_CNT::ONE) { createResult = FileAccessHelper::Creator(context); } else if (funcArg.GetArgc() == NARG_CNT::TWO) { @@ -84,19 +83,16 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i bool suss = UnwrapArrayWantFromJS(env, funcArg.GetArg(PARAM1), wants); if (!suss) { HILOG_ERROR("UnwrapArrayWantFromJS failed to get native wants"); - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } createResult = FileAccessHelper::Creator(context, wants); } if (createResult.first == nullptr || createResult.second != ERR_OK) { HILOG_ERROR("FileAccessHelperConstructor: Creator failed ret=%{public}d", createResult.second); - NError(createResult.second).ThrowErr(env); - return nullptr; + return HelperThrowError(env, 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) { FileAccessHelper *objectInfo = static_cast(data); if (objectInfo != nullptr) { @@ -109,12 +105,16 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i }; if (napi_wrap(env, thisVar, createResult.first.get(), finalize, nullptr, nullptr) != napi_ok) { finalize(env, createResult.first.get(), nullptr); - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } return thisVar; } +napi_value LogAndReturnNull(const char* logMessage) { + HILOG_ERROR("%{public}s", logMessage); + return nullptr; +} + napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -122,7 +122,6 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - napi_value result = nullptr; napi_value cons = nullptr; if (funcArg.GetArgc() == NARG_CNT::ONE) { @@ -132,12 +131,10 @@ 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; } - if (napi_new_instance(env, cons, ARGS_ONE, args, &result) != napi_ok) { return nullptr; } @@ -148,33 +145,24 @@ 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; } - 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; + return LogAndReturnNull("IsTypeForNapiValue isn`t object"); } - FileAccessHelper *fileAccessHelper = nullptr; if (napi_unwrap(env, result, (void **)&fileAccessHelper) != napi_ok) { - HILOG_ERROR("Faild to get fileAccessHelper"); - return nullptr; + return LogAndReturnNull("Faild to get fileAccessHelper"); } - if (fileAccessHelper == nullptr) { - HILOG_ERROR("fileAccessHelper is nullptr"); - return nullptr; + return LogAndReturnNull("fileAccessHelper is nullptr"); } - HILOG_INFO("g_fileAccessHelperList size %{public}zu", g_fileAccessHelperList.size()); return result; } @@ -313,36 +301,27 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - bool succ = false; std::unique_ptr uri; std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - int flags; std::tie(succ, flags) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; } - auto result = std::make_shared(); if (!result) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } - string uriString(uri.get()); auto cbExec = [uriString, flags, result, fileAccessHelper]() -> NError { OHOS::Uri uri(uriString); @@ -355,17 +334,14 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info) } return { NVal::CreateInt32(env, *result) }; }; - const std::string procedureName = "openFile"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } - NVal cb(env, funcArg[NARG_POS::THIRD]); if (!cb.TypeIs(napi_function)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } @@ -542,10 +518,8 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - bool succ = false; std::unique_ptr sourceFile; std::unique_ptr targetParent; @@ -553,18 +527,14 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info) if (!succ) { return nullptr; } - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; } - auto result = std::make_shared(); if (!result) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } - string sourceFileString(sourceFile.get()); string targetParentString(targetParent.get()); auto cbExec = [sourceFileString, targetParentString, result, fileAccessHelper]() -> NError { @@ -587,11 +557,9 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info) if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } - NVal cb(env, funcArg[NARG_POS::THIRD]); if (!cb.TypeIs(napi_function)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } @@ -753,29 +721,23 @@ napi_value NAPI_Copy(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - bool retStatus = false; std::string srcPathStr; std::string destPathStr; bool force = false; std::tie(retStatus, srcPathStr, destPathStr, force) = GetCopyArguments(env, funcArg); if (!retStatus) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; } - auto result = std::make_shared>(); if (!result) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } int ret = ERR_OK; auto cbExec = [srcPathStr, destPathStr, force, result, &ret, fileAccessHelper]() -> NError { @@ -793,7 +755,6 @@ napi_value NAPI_Copy(napi_env env, napi_callback_info info) } return { env, CreateObjectArray(env, *result) }; }; - return AddCopyNAsyncWork(env, funcArg, cbExec, cbComplete); } @@ -1005,29 +966,22 @@ napi_value NAPI_GetFileInfoFromUri(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - bool succ = false; std::unique_ptr uri; std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; } - auto result = std::make_shared(); if (!result) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } - string uriString(uri.get()); auto cbExec = [uriString, result, fileAccessHelper]() -> NError { OHOS::Uri uri(uriString); @@ -1038,7 +992,6 @@ napi_value NAPI_GetFileInfoFromUri(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - NVal nVal; int ret = MakeFileInfoResult(env, fileAccessHelper, *result, nVal); if (ret != ERR_OK) { @@ -1046,10 +999,8 @@ napi_value NAPI_GetFileInfoFromUri(napi_env env, napi_callback_info info) return { ret, "Make FileInfo Result fail" }; }).GetNapiErr(env) }; } - return nVal; }; - const std::string procedureName = "getFileInfoFromUri"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { @@ -1057,8 +1008,7 @@ napi_value NAPI_GetFileInfoFromUri(napi_env env, napi_callback_info info) } NVal cb(env, funcArg[NARG_POS::SECOND]); if (!cb.TypeIs(napi_function)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } @@ -1067,29 +1017,22 @@ napi_value NAPI_GetFileInfoFromRelativePath(napi_env env, napi_callback_info inf { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - bool succ = false; std::unique_ptr uri; std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } - FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar()); if (fileAccessHelper == nullptr) { return nullptr; } - auto result = std::make_shared(); if (!result) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return HelperThrowError(env, E_GETRESULT); } - string uriString(uri.get()); auto cbExec = [uriString, result, fileAccessHelper]() -> NError { string relativePath(uriString); @@ -1100,7 +1043,6 @@ napi_value NAPI_GetFileInfoFromRelativePath(napi_env env, napi_callback_info inf if (err) { return { env, err.GetNapiErr(env) }; } - NVal nVal; int ret = MakeFileInfoResult(env, fileAccessHelper, *result, nVal); if (ret != ERR_OK) { @@ -1108,10 +1050,8 @@ napi_value NAPI_GetFileInfoFromRelativePath(napi_env env, napi_callback_info inf return { ret, "Make FileInfo Result fail" }; }).GetNapiErr(env) }; } - return nVal; }; - const std::string procedureName = "getFileInfoFromRelativePath"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { @@ -1119,8 +1059,7 @@ napi_value NAPI_GetFileInfoFromRelativePath(napi_env env, napi_callback_info inf } NVal cb(env, funcArg[NARG_POS::SECOND]); if (!cb.TypeIs(napi_function)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return HelperThrowError(env, EINVAL); } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } diff --git a/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp b/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp index 7cecb8c2..15544ddd 100644 --- a/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp +++ b/frameworks/js/napi/file_access_module/root_info/napi_root_info_exporter.cpp @@ -81,46 +81,39 @@ napi_value NapiRootInfoExporter::Constructor(napi_env env, napi_callback_info in return funcArg.GetThisVar(); } +static napi_value ExporterThrowError(napi_env env, int code) { + NError(code).ThrowErr(env); + return nullptr; +} + napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, EINVAL); } - FileFilter filter({}, {}, {}, FileFilter::INVALID_SIZE, FileFilter::INVALID_MODIFY_AFTER, false, false); if (funcArg.GetArgc() == NARG_CNT::ONE) { auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, ret); } } - auto rootEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (rootEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - if (rootEntity->fileAccessHelper == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); if (objFileIteratorExporter == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); if (fileIteratorEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - FileInfo fileInfo; fileInfo.uri = rootEntity->rootInfo.uri; fileInfo.mode = DOCUMENT_FLAG_REPRESENTS_DIR | DOCUMENT_FLAG_SUPPORTS_READ | DOCUMENT_FLAG_SUPPORTS_WRITE; @@ -136,11 +129,9 @@ napi_value NapiRootInfoExporter::ListFile(napi_env env, napi_callback_info info) auto ret = rootEntity->fileAccessHelper->ListFile(fileInfo, fileIteratorEntity->offset, MAX_COUNT, filter, fileIteratorEntity->fileInfoVec); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, ret); } } - return NVal(env, objFileIteratorExporter).val_; } @@ -148,42 +139,30 @@ napi_value NapiRootInfoExporter::ScanFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, EINVAL); } - FileFilter filter({}, {}, {}, FileFilter::INVALID_SIZE, FileFilter::INVALID_MODIFY_AFTER, false, false); if (funcArg.GetArgc() == NARG_CNT::ONE) { auto ret = GetFileFilterParam(NVal(env, funcArg.GetArg(NARG_POS::FIRST)), filter); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, ret); } } - auto rootEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (rootEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - if (rootEntity->fileAccessHelper == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {}); if (objFileIteratorExporter == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - auto fileIteratorEntity = NClass::GetEntityOf(env, objFileIteratorExporter); if (fileIteratorEntity == nullptr) { - NError(E_GETRESULT).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, E_GETRESULT); } - FileInfo fileInfo; fileInfo.uri = rootEntity->rootInfo.uri; fileInfo.mode = DOCUMENT_FLAG_REPRESENTS_DIR | DOCUMENT_FLAG_SUPPORTS_READ | DOCUMENT_FLAG_SUPPORTS_WRITE; @@ -199,11 +178,9 @@ napi_value NapiRootInfoExporter::ScanFile(napi_env env, napi_callback_info info) auto ret = rootEntity->fileAccessHelper->ScanFile(fileInfo, fileIteratorEntity->offset, MAX_COUNT, filter, fileIteratorEntity->fileInfoVec); if (ret != ERR_OK) { - NError(ret).ThrowErr(env); - return nullptr; + return ExporterThrowError(env, ret); } } - return NVal(env, objFileIteratorExporter).val_; } 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 133f7e4d..2c862925 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -175,16 +175,12 @@ std::pair, int> FileAccessHelper::Creator( StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Creator"); if (context == nullptr) { HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, EINVAL}; } - if (!IsSystemApp()) { HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_PERMISSION_SYS}; } - sptr bm = FileAccessHelper::GetBundleMgrProxy(); FileAccessHelper::wants_.clear(); std::unordered_map> cMap; @@ -193,39 +189,30 @@ std::pair, int> FileAccessHelper::Creator( AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, GetUserId(), extensionInfos); if (!ret) { HILOG_ERROR("FileAccessHelper::Creator QueryExtensionAbilityInfos failed"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_GETINFO}; } - for (size_t i = 0; i < extensionInfos.size(); i++) { AAFwk::Want wantTem; wantTem.SetElementName(extensionInfos[i].bundleName, extensionInfos[i].name); sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); if (fileAccessExtConnection == nullptr) { HILOG_ERROR("new fileAccessExtConnection fail"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_GETRESULT}; } - if (!fileAccessExtConnection->IsExtAbilityConnected()) { fileAccessExtConnection->ConnectFileExtAbility(wantTem, context->GetToken()); } - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { HILOG_ERROR("Creator get invalid fileExtProxy"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_CONNECT}; } - std::shared_ptr connectInfo = std::make_shared(); if (!connectInfo) { HILOG_ERROR("Creator, connectInfo == nullptr"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_GETRESULT}; } FileAccessHelper::wants_.push_back(wantTem); - connectInfo->want = wantTem; connectInfo->fileAccessExtConnection = fileAccessExtConnection; cMap.emplace(extensionInfos[i].bundleName, connectInfo); @@ -233,11 +220,8 @@ std::pair, int> FileAccessHelper::Creator( FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); if (ptrFileAccessHelper == nullptr) { HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {nullptr, E_GETRESULT}; } - - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; } diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts index 15152594..98f51703 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/Common.ts @@ -67,5 +67,19 @@ interface Fileinfo { mimeType: string } -export { getPath, checkUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG }; +function commonReturnObject(uri: string, code: number) { + return { + uri: uri, + code: code + }; +} + +function ListReturnObject(infos: any[], code: number) { + return { + infos: infos, + code: code + }; +} + +export { getPath, checkUri, commonReturnObject, ListReturnObject,BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG }; export type { Fileinfo }; \ No newline at end of file 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 83671a90..8a0ee7e2 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 @@ -20,7 +20,7 @@ import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; import hilog from '@ohos.hilog'; import { getFileInfos } from './ListScanFileInfo'; import type { Fileinfo } from './Common'; -import { getPath, checkUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG } from './Common'; +import { getPath, checkUri, commonReturnObject, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG } from './Common'; const deviceFlag = fileExtensionInfo.DeviceFlag; const documentFlag = fileExtensionInfo.DocumentFlag; const deviceType = fileExtensionInfo.DeviceType; @@ -379,17 +379,8 @@ export default class FileExtAbility extends Extension { move(sourceFileUri, targetParentUri): {string, number} { sourceFileUri = this.decode(sourceFileUri); targetParentUri = this.decode(targetParentUri); - if (sourceFileUri === '' || targetParentUri === '') { - return { - uri: '', - code: E_URIS, - }; - } - if (!checkUri(sourceFileUri) || !checkUri(targetParentUri)) { - return { - uri: '', - code: E_URIS, - }; + if (sourceFileUri === '' || targetParentUri === '' || !checkUri(sourceFileUri) || !checkUri(targetParentUri)) { + return commonReturnObject('', E_URIS); } let displayName = this.getFileName(sourceFileUri); let newFileUri = this.genNewFileUri(targetParentUri, displayName); @@ -397,56 +388,32 @@ export default class FileExtAbility extends Extension { let newPath = getPath(newFileUri); newFileUri = this.encode(newFileUri); if (newFileUri === '') { - return { - uri: '', - code: E_URIS, - }; + return commonReturnObject('', E_URIS); } if (oldPath === newPath) { - // move to the same directory - return { - uri: newFileUri, - code: ERR_OK, - }; + return commonReturnObject(newFileUri, ERR_OK); } else if (newPath.match(new RegExp('^' + oldPath + '(/|$)'))) { // move to a subdirectory of the source directory - return { - uri: '', - code: E_GETRESULT, - }; + return commonReturnObject('',E_GETRESULT); } try { // The source file does not exist or the destination is not a directory let isAccess = fs.accessSync(oldPath); if (!isAccess) { - return { - uri: '', - code: E_GETRESULT, - }; + return commonReturnObject('',E_GETRESULT); } let stat = fs.statSync(getPath(targetParentUri)); if (!stat || !stat.isDirectory()) { - return { - uri: '', - code: E_GETRESULT, - }; + return commonReturnObject('',E_GETRESULT); } - let statOld = fs.statSync(oldPath); if (!statOld) { - return { - uri: '', - code: E_GETRESULT, - } + return commonReturnObject('',E_GETRESULT); } - // isDir if (statOld.isDirectory()) { fs.moveDirSync(oldPath, getPath(targetParentUri), MOVE_MODLE_CODE); - return { - uri: newFileUri, - code: ERR_OK, - }; + return commonReturnObject(newFileUri, ERR_OK); } // when targetFile is exist, delete it let isAccessNewPath = fs.accessSync(newPath); @@ -454,17 +421,10 @@ export default class FileExtAbility extends Extension { fs.unlinkSync(newPath); } fs.moveFileSync(oldPath, newPath, 0); - - return { - uri: newFileUri, - code: ERR_OK, - }; + return commonReturnObject(newFileUri, ERR_OK); } catch (e) { hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); - return { - uri: '', - code: e.code, - }; + return commonReturnObject('',e.code); } } diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts index 26672add..1e078c9b 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/ListScanFileInfo.ts @@ -17,7 +17,7 @@ import fs from '@ohos.file.fs'; import type { Filter } from '@ohos.file.fs'; import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; import type { Fileinfo } from './Common'; -import { getPath, DOMAIN_CODE, TAG } from './Common'; +import { getPath, ListReturnObject, DOMAIN_CODE, TAG } from './Common'; const documentFlag = fileExtensionInfo.DocumentFlag; const ERR_OK = 0; @@ -136,10 +136,7 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt try { let statPath = fs.statSync(path); if (!statPath.isDirectory()) { - return { - infos: [], - code: E_GETRESULT, - }; + return ListReturnObject([], E_GETRESULT); } let options; let listNum = offset + count; @@ -178,14 +175,8 @@ function getFileInfos(sourceFileUri: string, offset: number, count: number, filt } } catch (e) { hilog.error(DOMAIN_CODE, TAG, `getFileInfos error: ${e.message},code: ${e.code}`); - return { - infos: [], - code: E_GETRESULT, - }; + return ListReturnObject([], E_GETRESULT); } - return { - infos: infos, - code: ERR_OK, - }; + return ListReturnObject(infos,ERR_OK); } export { getFileInfos }; \ No newline at end of file -- Gitee