reducefunctionlines

Signed-off-by: liuhonglin9 <liuhonglin9@huawei.com>
Change-Id: Id4d5dd1f2928483b7815886621991af8cb21338b
This commit is contained in:
liuhonglin9 2023-10-09 03:05:37 +00:00
parent 1b442972f3
commit 6d1629f556
8 changed files with 171 additions and 453 deletions

View File

@ -79,51 +79,43 @@ napi_value NapiFileInfoExporter::Constructor(napi_env env, napi_callback_info in
return funcArg.GetThisVar();
}
napi_value NapiFileInfoExporter::ThrowError(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 ThrowError(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 ThrowError(env, ret);
}
}
auto fileInfoEntity = NClass::GetEntityOf<FileInfoEntity>(env, funcArg.GetThisVar());
if (fileInfoEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(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 ThrowError(env, E_GETRESULT);
}
auto objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
if (objFileIteratorExporter == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(env, E_GETRESULT);
}
auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
if (fileIteratorEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(env, E_GETRESULT);
}
{
std::lock_guard<std::mutex> 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 ThrowError(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 ThrowError(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 ThrowError(env, ret);
}
}
auto fileInfoEntity = NClass::GetEntityOf<FileInfoEntity>(env, funcArg.GetThisVar());
if (fileInfoEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(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 ThrowError(env, E_GETRESULT);
}
auto objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
if (objFileIteratorExporter == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(env, E_GETRESULT);
}
auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
if (fileIteratorEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return ThrowError(env, E_GETRESULT);
}
{
std::lock_guard<std::mutex> 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 ThrowError(env, ret);
}
}
return NVal(env, objFileIteratorExporter).val_;
}

View File

@ -43,6 +43,7 @@ public:
static napi_value GetSize(napi_env env, napi_callback_info info);
static napi_value GetMtime(napi_env env, napi_callback_info info);
static napi_value GetMimeType(napi_env env, napi_callback_info info);
static napi_value ThrowError(napi_env env, int code);
bool Export() override;
std::string GetClassName() override;

View File

@ -56,27 +56,21 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
napi_value thisVar = funcArg.GetThisVar();
std::pair<std::shared_ptr<FileAccessHelper>, 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 NapiFileInfoExporter::ThrowError(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 NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
if (funcArg.GetArgc() == NARG_CNT::ONE) {
createResult = FileAccessHelper::Creator(context);
} else if (funcArg.GetArgc() == NARG_CNT::TWO) {
@ -84,19 +78,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 NapiFileInfoExporter::ThrowError(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 NapiFileInfoExporter::ThrowError(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<FileAccessHelper *>(data);
if (objectInfo != nullptr) {
@ -109,8 +100,7 @@ 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 NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
return thisVar;
}
@ -122,7 +112,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 +121,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 +135,27 @@ 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;
}
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;
}
HILOG_INFO("g_fileAccessHelperList size %{public}zu", g_fileAccessHelperList.size());
return result;
}
@ -313,36 +294,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
bool succ = false;
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
int flags;
std::tie(succ, flags) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
if (!succ) {
NError(EINVAL).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar());
if (fileAccessHelper == nullptr) {
return nullptr;
}
auto result = std::make_shared<int>();
if (!result) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
string uriString(uri.get());
auto cbExec = [uriString, flags, result, fileAccessHelper]() -> NError {
OHOS::Uri uri(uriString);
@ -355,17 +327,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}
@ -542,10 +511,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
bool succ = false;
std::unique_ptr<char[]> sourceFile;
std::unique_ptr<char[]> targetParent;
@ -553,18 +520,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<string>();
if (!result) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
string sourceFileString(sourceFile.get());
string targetParentString(targetParent.get());
auto cbExec = [sourceFileString, targetParentString, result, fileAccessHelper]() -> NError {
@ -587,11 +550,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}
@ -753,29 +714,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 NapiFileInfoExporter::ThrowError(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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar());
if (fileAccessHelper == nullptr) {
return nullptr;
}
auto result = std::make_shared<std::vector<CopyResult>>();
if (!result) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
int ret = ERR_OK;
auto cbExec = [srcPathStr, destPathStr, force, result, &ret, fileAccessHelper]() -> NError {
@ -793,7 +748,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 +959,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
bool succ = false;
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar());
if (fileAccessHelper == nullptr) {
return nullptr;
}
auto result = std::make_shared<FileInfo>();
if (!result) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
string uriString(uri.get());
auto cbExec = [uriString, result, fileAccessHelper]() -> NError {
OHOS::Uri uri(uriString);
@ -1038,7 +985,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 +992,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 +1001,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}
@ -1067,29 +1010,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
bool succ = false;
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, EINVAL);
}
FileAccessHelper *fileAccessHelper = GetFileAccessHelper(env, funcArg.GetThisVar());
if (fileAccessHelper == nullptr) {
return nullptr;
}
auto result = std::make_shared<FileInfo>();
if (!result) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
string uriString(uri.get());
auto cbExec = [uriString, result, fileAccessHelper]() -> NError {
string relativePath(uriString);
@ -1100,7 +1036,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 +1043,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 +1052,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 NapiFileInfoExporter::ThrowError(env, EINVAL);
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
}

View File

@ -25,6 +25,7 @@
#include "napi_file_iterator_exporter.h"
#include "napi_utils.h"
#include "root_info_entity.h"
#include "napi_file_info_exporter.h"
namespace OHOS {
namespace FileAccessFwk {
@ -85,42 +86,30 @@ 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 NapiFileInfoExporter::ThrowError(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 NapiFileInfoExporter::ThrowError(env, ret);
}
}
auto rootEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
if (rootEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
if (rootEntity->fileAccessHelper == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
if (objFileIteratorExporter == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
if (fileIteratorEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(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 +125,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 NapiFileInfoExporter::ThrowError(env, ret);
}
}
return NVal(env, objFileIteratorExporter).val_;
}
@ -148,42 +135,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 NapiFileInfoExporter::ThrowError(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 NapiFileInfoExporter::ThrowError(env, ret);
}
}
auto rootEntity = NClass::GetEntityOf<RootInfoEntity>(env, funcArg.GetThisVar());
if (rootEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
if (rootEntity->fileAccessHelper == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
napi_value objFileIteratorExporter = NClass::InstantiateClass(env, NapiFileIteratorExporter::className_, {});
if (objFileIteratorExporter == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(env, E_GETRESULT);
}
auto fileIteratorEntity = NClass::GetEntityOf<FileIteratorEntity>(env, objFileIteratorExporter);
if (fileIteratorEntity == nullptr) {
NError(E_GETRESULT).ThrowErr(env);
return nullptr;
return NapiFileInfoExporter::ThrowError(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 +174,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 NapiFileInfoExporter::ThrowError(env, ret);
}
}
return NVal(env, objFileIteratorExporter).val_;
}

View File

@ -179,12 +179,10 @@ std::pair<std::shared_ptr<FileAccessHelper>, int> FileAccessHelper::Creator(
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<AppExecFwk::IBundleMgr> bm = FileAccessHelper::GetBundleMgrProxy();
FileAccessHelper::wants_.clear();
std::unordered_map<std::string, std::shared_ptr<ConnectInfo>> cMap;
@ -195,7 +193,6 @@ std::pair<std::shared_ptr<FileAccessHelper>, int> FileAccessHelper::Creator(
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);
@ -204,24 +201,20 @@ std::pair<std::shared_ptr<FileAccessHelper>, int> FileAccessHelper::Creator(
HILOG_ERROR("new fileAccessExtConnection fail");
return {nullptr, E_GETRESULT};
}
if (!fileAccessExtConnection->IsExtAbilityConnected()) {
fileAccessExtConnection->ConnectFileExtAbility(wantTem, context->GetToken());
}
sptr<IFileAccessExtBase> fileExtProxy = fileAccessExtConnection->GetFileExtProxy();
if (fileExtProxy == nullptr) {
HILOG_ERROR("Creator get invalid fileExtProxy");
return {nullptr, E_CONNECT};
}
std::shared_ptr<ConnectInfo> connectInfo = std::make_shared<ConnectInfo>();
if (!connectInfo) {
HILOG_ERROR("Creator, connectInfo == nullptr");
return {nullptr, E_GETRESULT};
}
FileAccessHelper::wants_.push_back(wantTem);
connectInfo->want = wantTem;
connectInfo->fileAccessExtConnection = fileAccessExtConnection;
cMap.emplace(extensionInfos[i].bundleName, connectInfo);
@ -231,7 +224,6 @@ std::pair<std::shared_ptr<FileAccessHelper>, int> FileAccessHelper::Creator(
HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed");
return {nullptr, E_GETRESULT};
}
return {std::shared_ptr<FileAccessHelper>(ptrFileAccessHelper), ERR_OK};
}

View File

@ -19,9 +19,9 @@ const DOMAIN_CODE = 0x0001;
const TAG = 'ExternalFileManager';
const FILE_PREFIX_NAME = 'file://';
function checkUri(uri): boolean {
function checkUri(uri: string): boolean {
try {
if (uri.indexOf(FILE_PREFIX_NAME) === 0) {
if (uri?.indexOf(FILE_PREFIX_NAME) === 0) {
hilog.info(DOMAIN_CODE, TAG, 'uri is ' + uri);
return true;
} else {
@ -67,5 +67,58 @@ interface Fileinfo {
mimeType: string
}
export { getPath, checkUri, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG };
function uriReturnObject(uri: string, code: number) {
return {
uri: uri,
code: code
};
}
function infosReturnObject(infos: any[], code: number) {
return {
infos: infos,
code: code
};
}
function fdReturnObject(fd: number, code: number) {
return {
fd: fd,
code: code
};
}
function boolReturnObject(isExist: boolean, code: number) {
return {
isExist: isExist,
code: code
};
}
function fileinfoReturnObject(fileInfo: object, code: number) {
return {
fileInfo: fileInfo,
code: code
};
}
function resultsResultObject(results: any, code: number) {
return {
results: results,
code: code
};
}
function rootsReturnObject(roots: any, code: number) {
return {
roots: roots,
code: code
};
}
export {
getPath, checkUri, uriReturnObject, infosReturnObject, fdReturnObject, boolReturnObject, resultsResultObject,
fileinfoReturnObject, rootsReturnObject, BUNDLE_NAME, DOMAIN_CODE, FILE_PREFIX_NAME, TAG
};
export type { Fileinfo };

View File

@ -20,7 +20,11 @@ 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, uriReturnObject, BUNDLE_NAME, DOMAIN_CODE, fileinfoReturnObject } from './Common';
import { FILE_PREFIX_NAME, TAG, fdReturnObject, boolReturnObject, rootsReturnObject } from './Common';
import { infosReturnObject, resultsResultObject } from './Common';
const deviceFlag = fileExtensionInfo.DeviceFlag;
const documentFlag = fileExtensionInfo.DocumentFlag;
const deviceType = fileExtensionInfo.DeviceType;
@ -178,103 +182,56 @@ export default class FileExtAbility extends Extension {
openFile(sourceFileUri, flags): {number, number} {
sourceFileUri = this.decode(sourceFileUri);
if (sourceFileUri === '') {
return {
fd: ERR_ERROR,
code: E_URIS,
};
}
if (!checkUri(sourceFileUri)) {
return {
fd: ERR_ERROR,
code: E_URIS,
};
return fdReturnObject(ERR_ERROR, E_URIS);
}
try {
let path = getPath(sourceFileUri);
let file = fs.openSync(path, flags);
return {
fd: file.fd,
code: ERR_OK,
};
return fdReturnObject(file.fd, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'openFile error ' + e.message);
return {
fd: ERR_ERROR,
code: e.code,
};
return fdReturnObject(ERR_ERROR, e.code);
}
}
createFile(parentUri, displayName): {string, number} {
parentUri = this.decode(parentUri);
if (parentUri === '') {
return {
uri: '',
code: E_URIS
};
}
if (!checkUri(parentUri)) {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
try {
hilog.info(DOMAIN_CODE, TAG, 'createFile, uri is ' + parentUri);
let newFileUri = this.genNewFileUri(parentUri, displayName);
if (this.access(newFileUri).isExist) {
return {
uri: '',
code: E_EXIST,
};
return uriReturnObject('', E_EXIST);
}
let path = getPath(newFileUri);
let file = fs.openSync(path, fs.OpenMode.CREATE);
fs.closeSync(file);
newFileUri = encodeURI(newFileUri);
return {
uri: newFileUri,
code: ERR_OK,
};
return uriReturnObject(newFileUri, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'createFile error ' + e.message);
return {
uri: '',
code: e.code,
};
return uriReturnObject('', e.code);
}
}
mkdir(parentUri, displayName): {string, number} {
parentUri = this.decode(parentUri);
if (parentUri === '') {
return {
uri: '',
code: E_URIS
};
}
if (!checkUri(parentUri)) {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
try {
let newFileUri = this.genNewFileUri(parentUri, displayName);
let path = getPath(newFileUri);
fs.mkdirSync(path);
newFileUri = encodeURI(newFileUri);
return {
uri: newFileUri,
code: ERR_OK,
};
return uriReturnObject(newFileUri, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'mkdir error ' + e.message);
return {
uri: '',
code: e.code,
};
return uriReturnObject('', e.code);
}
}
@ -348,9 +305,6 @@ export default class FileExtAbility extends Extension {
delete(selectFileUri): number {
selectFileUri = this.decode(selectFileUri);
if (selectFileUri === '') {
return E_URIS;
}
if (!checkUri(selectFileUri)) {
return E_URIS;
}
@ -379,17 +333,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,
};
return uriReturnObject('', E_URIS);
}
let displayName = this.getFileName(sourceFileUri);
let newFileUri = this.genNewFileUri(targetParentUri, displayName);
@ -397,10 +342,7 @@ export default class FileExtAbility extends Extension {
let newPath = getPath(newFileUri);
newFileUri = this.encode(newFileUri);
if (newFileUri === '') {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
if (oldPath === newPath) {
// move to the same directory
@ -410,43 +352,20 @@ export default class FileExtAbility extends Extension {
};
} else if (newPath.indexOf(oldPath) === 0 && newPath.charAt(oldPath.length) === '/') {
// move to a subdirectory of the source directory
return {
uri: '',
code: E_GETRESULT,
};
return uriReturnObject('', 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,
};
}
let stat = fs.statSync(getPath(targetParentUri));
if (!stat || !stat.isDirectory()) {
return {
uri: '',
code: E_GETRESULT,
};
}
let statOld = fs.statSync(oldPath);
if (!statOld) {
return {
uri: '',
code: E_GETRESULT,
}
if (!isAccess || !stat || !stat.isDirectory() || !statOld) {
return uriReturnObject('', E_GETRESULT);
}
// isDir
if (statOld.isDirectory()) {
fs.moveDirSync(oldPath, getPath(targetParentUri), MOVE_MODLE_CODE);
return {
uri: newFileUri,
code: ERR_OK,
};
return uriReturnObject(newFileUri, ERR_OK);
}
// when targetFile is exist, delete it
let isAccessNewPath = fs.accessSync(newPath);
@ -454,34 +373,17 @@ export default class FileExtAbility extends Extension {
fs.unlinkSync(newPath);
}
fs.moveFileSync(oldPath, newPath, 0);
return {
uri: newFileUri,
code: ERR_OK,
};
return uriReturnObject(newFileUri, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message);
return {
uri: '',
code: e.code,
};
return uriReturnObject('', e.code);
}
}
rename(sourceFileUri, displayName): {string, number} {
sourceFileUri = this.decode(sourceFileUri);
if (sourceFileUri === '') {
return {
uri: '',
code: E_URIS,
};
}
if (!checkUri(sourceFileUri)) {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
try {
let newFileUri = this.renameUri(sourceFileUri, displayName);
@ -489,23 +391,14 @@ export default class FileExtAbility extends Extension {
let newPath = getPath(newFileUri);
let isAccess = fs.accessSync(newPath);
if (isAccess) {
return {
uri: '',
code: E_EXIST,
};
return uriReturnObject('', E_EXIST);
}
fs.renameSync(oldPath, newPath);
newFileUri = encodeURI(newFileUri);
return {
uri: newFileUri,
code: ERR_OK,
};
return uriReturnObject(newFileUri, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'rename error ' + e.message);
return {
uri: '',
code: e.code,
};
return uriReturnObject('', e.code);
}
}
@ -518,10 +411,7 @@ export default class FileExtAbility extends Extension {
errMsg: errMsg,
},
];
return {
results: copyResult,
code: ret,
};
return resultsResultObject(copyResult, ret);
}
checkCopyArguments(sourceFileUri, targetParentUri): {[], number} {
@ -558,10 +448,7 @@ export default class FileExtAbility extends Extension {
hilog.error(DOMAIN_CODE, TAG, 'copy error ' + e.message);
return this.getCopyReturnValue(sourceFileUri, targetParentUri, e.code, '', COPY_EXCEPTION);
}
return {
results: [],
code: ERR_OK,
};
return resultsResultObject([], ERR_OK);
}
processCopyReturnValue(ret, copyRet): void {
@ -659,56 +546,32 @@ export default class FileExtAbility extends Extension {
access(sourceFileUri): {boolean, number} {
sourceFileUri = this.decode(sourceFileUri);
if (sourceFileUri === '') {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
if (!checkUri(sourceFileUri)) {
hilog.error(DOMAIN_CODE, TAG, 'access checkUri fail');
return {
isExist: false,
code: E_URIS,
};
return boolReturnObject(false, E_URIS);
}
let isAccess = false;
try {
let path = getPath(sourceFileUri);
isAccess = fs.accessSync(path);
if (!isAccess) {
return {
isExist: false,
code: ERR_OK,
};
return boolReturnObject(false, ERR_OK);
}
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'access error ' + e.message);
return {
isExist: false,
code: e.code,
};
return boolReturnObject(false, e.code);
}
return {
isExist: true,
code: ERR_OK,
};
return boolReturnObject(true, ERR_OK);
}
listFile(sourceFileUri: string, offset: number, count: number, filter: Filter) :
{infos: Fileinfo[], code: number} {
let infos : Fileinfo[] = [];
sourceFileUri = this.decode(sourceFileUri);
if (sourceFileUri === '') {
return {
infos: [],
code: E_URIS,
};
}
if (!checkUri(sourceFileUri)) {
return {
infos: [],
code: E_URIS,
};
return infosReturnObject([], E_URIS);
}
return getFileInfos(sourceFileUri, offset, count, filter, false);
}
@ -717,17 +580,8 @@ export default class FileExtAbility extends Extension {
{infos: Fileinfo[], code: number} {
let infos : Fileinfo[] = [];
sourceFileUri = this.decode(sourceFileUri);
if (sourceFileUri === '') {
return {
infos: [],
code: E_URIS,
};
}
if (!checkUri(sourceFileUri)) {
return {
infos: [],
code: E_URIS,
};
return infosReturnObject([], E_URIS);
}
return getFileInfos(sourceFileUri, offset, count, filter, true);
@ -736,17 +590,11 @@ export default class FileExtAbility extends Extension {
getFileInfoFromUri(selectFileUri) {
selectFileUri = this.decode(selectFileUri);
if (selectFileUri === '') {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
if (!checkUri(selectFileUri)) {
return {
fileInfo: {},
code: E_URIS,
};
return fileinfoReturnObject({}, E_URIS);
}
let fileInfo = {};
try {
@ -771,15 +619,9 @@ export default class FileExtAbility extends Extension {
};
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'getFileInfoFromUri error ' + e.message);
return {
fileInfo: {},
code: e.code,
};
return fileinfoReturnObject({}, e.code);
}
return {
fileInfo: fileInfo,
code: ERR_OK,
};
return fileinfoReturnObject(fileInfo, ERR_OK);
}
checkRelativePath(selectFileRelativePath): boolean {
@ -804,10 +646,7 @@ export default class FileExtAbility extends Extension {
getFileInfoFromRelativePath(selectFileRelativePath): {fileInfo:object, code:number} {
let fileInfo = {};
if (!this.checkRelativePath(selectFileRelativePath)) {
return {
fileInfo: {},
code: E_INVAL,
};
return fileinfoReturnObject({}, E_INVAL);
}
try {
// Processing format: Delete the last '/'
@ -834,15 +673,9 @@ export default class FileExtAbility extends Extension {
};
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'getFileInfoFromRelativePath error ' + e.message);
return {
fileInfo: {},
code: e.code,
};
return fileinfoReturnObject({}, e.code);
}
return {
fileInfo: fileInfo,
code: ERR_OK,
};
return fileinfoReturnObject(fileInfo, ERR_OK);
}
relativePath2uri(path): string {
@ -885,16 +718,10 @@ export default class FileExtAbility extends Extension {
volumeInfoList.push(volumeInfo);
}
roots = roots.concat(volumeInfoList);
return {
roots: roots,
code: ERR_OK,
};
return rootsReturnObject(roots, ERR_OK);
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'getRoots errorcode: ' + e.code, ' message: ' + e.message);
return {
roots: [],
code: e.code,
};
return rootsReturnObject([], e.code);
}
}
@ -928,24 +755,15 @@ export default class FileExtAbility extends Extension {
query(uri, columns): {[], number} {
uri = this.decode(uri);
if (uri === '') {
return {
uri: '',
code: E_URIS,
};
return uriReturnObject('', E_URIS);
}
if (!checkUri(uri)) {
return {
results: [],
code: E_URIS,
};
return resultsResultObject([], E_URIS);
}
if (!this.access(uri).isExist) {
return {
results: [],
code: E_NOEXIST,
};
return resultsResultObject([], E_NOEXIST);
}
let queryResults = [];
@ -962,24 +780,13 @@ export default class FileExtAbility extends Extension {
}
} catch (e) {
hilog.error(DOMAIN_CODE, TAG, 'query error ' + e.message);
return {
results: [],
code: E_GETRESULT,
};
return resultsResultObject([], E_GETRESULT);
}
return {
results: queryResults,
code: ERR_OK,
};
return resultsResultObject(queryResults, ERR_OK);
}
startWatcher(uri, callback): number {
uri = this.decode(uri);
if (uri === '') {
return {
code: E_URIS,
};
}
if (!checkUri(uri)) {
return E_URIS;
}
@ -1022,12 +829,6 @@ export default class FileExtAbility extends Extension {
stopWatcher(uri, isUnregisterAll): number {
uri = this.decode(uri);
if (uri === '') {
return {
code: E_URIS,
};
}
if (!checkUri(uri)) {
return E_URIS;
}

View File

@ -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, uriReturnObject, DOMAIN_CODE, TAG, infosReturnObject } 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 infosReturnObject([], 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 infosReturnObject([], E_GETRESULT);
}
return {
infos: infos,
code: ERR_OK,
};
return infosReturnObject(infos, ERR_OK);
}
export { getFileInfos };