From e656508f5443bb138f3717a90421ec79279c670a Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Thu, 21 Jul 2022 17:28:13 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=84=E5=8C=BA?= =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../napi_fileaccess_helper.cpp | 148 ++++++++++-------- .../FileExtensionAbility.ts | 10 +- .../ets/FileExtensionAbility/VolumeManager.ts | 10 +- 3 files changed, 100 insertions(+), 68 deletions(-) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index c8edcf9b..061e17a1 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -194,6 +194,28 @@ static FileAccessHelper *GetFileAccessHelper(napi_env env, napi_value thisVar) return fileAccessHelper; } +static std::tuple, std::unique_ptr> GetReadArg(napi_env env, + napi_value sourceFile, + napi_value targetParent) +{ + bool succ = false; + std::unique_ptr uri = nullptr; + std::unique_ptr name = nullptr; + std::tie(succ, uri, std::ignore) = NVal(env, sourceFile).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "first parameter is Invalid"); + return { false, std::move(uri), std::move(name) }; + } + + std::tie(succ, name, std::ignore) = NVal(env, targetParent).ToUTF8String(); + if (!succ) { + NError(EINVAL).ThrowErr(env, "second parameter is Invalid"); + return { false, std::move(uri), std::move(name) }; + } + + return { true, std::move(uri), std::move(name) }; +} + napi_value NAPI_OpenFile(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -240,11 +262,14 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NAPI_CreateFile(napi_env env, napi_callback_info info) @@ -257,16 +282,9 @@ napi_value NAPI_CreateFile(napi_env env, napi_callback_info info) 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, "Invalid uri"); - return nullptr; - } - std::unique_ptr displayName; - std::tie(succ, displayName, std::ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + std::tie(succ, uri, displayName) = GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - NError(EINVAL).ThrowErr(env, "Invalid displayName"); return nullptr; } @@ -296,11 +314,15 @@ napi_value NAPI_CreateFile(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } napi_value NAPI_Mkdir(napi_env env, napi_callback_info info) @@ -313,16 +335,9 @@ napi_value NAPI_Mkdir(napi_env env, napi_callback_info info) 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, "Invalid uri"); - return nullptr; - } - std::unique_ptr displayName; - std::tie(succ, displayName, std::ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + std::tie(succ, uri, displayName) = GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - NError(EINVAL).ThrowErr(env, "Invalid displayName"); return nullptr; } @@ -352,11 +367,14 @@ napi_value NAPI_Mkdir(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NAPI_Delete(napi_env env, napi_callback_info info) @@ -398,11 +416,14 @@ napi_value NAPI_Delete(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::SECOND]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NAPI_Move(napi_env env, napi_callback_info info) @@ -415,16 +436,9 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info) bool succ = false; std::unique_ptr sourceFile; - std::tie(succ, sourceFile, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { - NError(EINVAL).ThrowErr(env, "Invalid sourceFile"); - return nullptr; - } - std::unique_ptr targetParent; - std::tie(succ, targetParent, std::ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + std::tie(succ, sourceFile, targetParent) = GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - NError(EINVAL).ThrowErr(env, "Invalid targetParent"); return nullptr; } @@ -455,11 +469,14 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NAPI_Rename(napi_env env, napi_callback_info info) @@ -472,16 +489,9 @@ napi_value NAPI_Rename(napi_env env, napi_callback_info info) 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, "Invalid uri"); - return nullptr; - } - std::unique_ptr displayName; - std::tie(succ, displayName, std::ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + std::tie(succ, uri, displayName) = GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - NError(EINVAL).ThrowErr(env, "Invalid displayName"); return nullptr; } @@ -511,11 +521,14 @@ napi_value NAPI_Rename(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::THIRD]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } napi_value NAPI_ListFile(napi_env env, napi_callback_info info) @@ -557,11 +570,15 @@ napi_value NAPI_ListFile(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::SECOND]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) @@ -593,11 +610,14 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ZERO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } + + NVal cb(env, funcArg[NARG_POS::FIRST]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; } - return NVal::CreateUndefined(env).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } } // namespace AppExecFwk } // namespace OHOS \ 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 a1dfd5e2..2f540d78 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 @@ -347,8 +347,14 @@ export default class FileExtAbility extends Extension { displayName: 'storage_daemon', deviceId: '', type: DEVICE_TYPE.SHARED_DISK, - flags: FLAG.SUPPORTS_WRITE | FLAG.SUPPORTS_DELETE | FLAG.SUPPORTS_RENAME | FLAG.SUPPORTS_COPY | - FLAG.SUPPORTS_MOVE | FLAG.SUPPORTS_REMOVE | FLAG.DIR_SUPPORTS_CREATE | FLAG.DIR_PREFERS_LAST_MODIFIED, + flags: FLAG.SUPPORTS_WRITE | + FLAG.SUPPORTS_DELETE | + FLAG.SUPPORTS_RENAME | + FLAG.SUPPORTS_COPY | + FLAG.SUPPORTS_MOVE | + FLAG.SUPPORTS_REMOVE | + FLAG.DIR_SUPPORTS_CREATE | + FLAG.DIR_PREFERS_LAST_MODIFIED, }); return roots; } diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts index 1676be2c..8047d2a7 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts @@ -21,8 +21,14 @@ const FLAG = fileExtensionInfo.FLAG; const DEVICE_TYPE = fileExtensionInfo.DeviceType; function init() { volumeManager.getAllVolumes().then((volumes) => { - let flags = FLAG.SUPPORTS_WRITE | FLAG.SUPPORTS_DELETE | FLAG.SUPPORTS_RENAME | FLAG.SUPPORTS_COPY | - FLAG.SUPPORTS_MOVE | FLAG.SUPPORTS_REMOVE | FLAG.DIR_SUPPORTS_CREATE | FLAG.DIR_PREFERS_LAST_MODIFIED; + let flags = FLAG.SUPPORTS_WRITE | + FLAG.SUPPORTS_DELETE | + FLAG.SUPPORTS_RENAME | + FLAG.SUPPORTS_COPY | + FLAG.SUPPORTS_MOVE | + FLAG.SUPPORTS_REMOVE | + FLAG.DIR_SUPPORTS_CREATE | + FLAG.DIR_PREFERS_LAST_MODIFIED; for (let i = 0; i < volumes.length; i++) { let volume = volumes[i]; let volumeInfo = { -- Gitee From dffacea52490ecdc5e1f8e94ee90841041f8d7ad Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Thu, 21 Jul 2022 17:36:01 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9codecheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../napi/file_access_module/napi_fileaccess_helper.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index 061e17a1..b8de48bc 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -322,7 +322,6 @@ napi_value NAPI_CreateFile(napi_env env, napi_callback_info info) return nullptr; } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } napi_value NAPI_Mkdir(napi_env env, napi_callback_info info) @@ -578,7 +577,6 @@ napi_value NAPI_ListFile(napi_env env, napi_callback_info info) return nullptr; } return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; - } napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) @@ -608,11 +606,12 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) }; std::string procedureName = "getRoots"; NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } - - NVal cb(env, funcArg[NARG_POS::FIRST]); + + NVal cb(env, funcArg[NARG_POS::FIRST]); if (!cb.TypeIs(napi_function)) { NError(EINVAL).ThrowErr(env, "not function type"); return nullptr; -- Gitee From 4c337c014abd32345273de47a35fc47030194a4a Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Thu, 21 Jul 2022 17:40:58 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../kits/napi/file_access_module/napi_fileaccess_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index b8de48bc..c2b1e570 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -609,7 +609,7 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info) if (funcArg.GetArgc() == NARG_CNT::ZERO) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; - } + } NVal cb(env, funcArg[NARG_POS::FIRST]); if (!cb.TypeIs(napi_function)) { -- Gitee From 04594744a352f8ba8c195c69458f6e3af9b85ecf Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Fri, 22 Jul 2022 11:36:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangjianqiang --- .../main/ets/FileExtensionAbility/VolumeManager.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts index 8047d2a7..085b9e30 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/VolumeManager.ts @@ -22,13 +22,13 @@ const DEVICE_TYPE = fileExtensionInfo.DeviceType; function init() { volumeManager.getAllVolumes().then((volumes) => { let flags = FLAG.SUPPORTS_WRITE | - FLAG.SUPPORTS_DELETE | - FLAG.SUPPORTS_RENAME | - FLAG.SUPPORTS_COPY | - FLAG.SUPPORTS_MOVE | - FLAG.SUPPORTS_REMOVE | - FLAG.DIR_SUPPORTS_CREATE | - FLAG.DIR_PREFERS_LAST_MODIFIED; + FLAG.SUPPORTS_DELETE | + FLAG.SUPPORTS_RENAME | + FLAG.SUPPORTS_COPY | + FLAG.SUPPORTS_MOVE | + FLAG.SUPPORTS_REMOVE | + FLAG.DIR_SUPPORTS_CREATE | + FLAG.DIR_PREFERS_LAST_MODIFIED; for (let i = 0; i < volumes.length; i++) { let volume = volumes[i]; let volumeInfo = { -- Gitee