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 c8edcf9b34785e3385a7b6a1288230c0f82be584..c2b1e570d96e0b52219871a0fc5290bb9ad80d87 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,14 @@ 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 +334,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 +366,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 +415,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 +435,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 +468,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 +488,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 +520,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 +569,14 @@ 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) @@ -591,13 +606,17 @@ 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_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; } - return NVal::CreateUndefined(env).val_; + + NVal cb(env, funcArg[NARG_POS::FIRST]); + if (!cb.TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env, "not function type"); + return nullptr; + } + 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 a1dfd5e25e16d0cc6b8dbd2f7c28275fe8532abf..2f540d78459c44a4c20537d9657b18b3867b8588 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 1676be2cdda1f0203e18f52b2c029e1a339a2fe4..085b9e308cc4e8b245a48e50f06c40170ee679f5 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 = {