diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000000000000000000000000000000000000..ba951e1bac6b909ac9093b93444fab96887eb63b --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++17", + "intelliSenseMode": "macos-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..ce81378ac002847f1fd596164e5fb80de7913154 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "string_view": "cpp" + }, + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 7772979959f91cdd3f21702a5df9ccbe1b1da98a..365ea5de234c1186289e84f53f4b98a5c539b87a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -326,25 +326,59 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); - if (!succPath) { - HILOGE("Invalid path"); + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + std::string pathStr; + if (valueType == napi_string) { + auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); + if (!succPath) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + pathStr = std::string(path.get()); + } else if (valueType == napi_object) { + bool isArrayBuffer = false; + status = napi_is_arraybuffer(env, funcArg[NARG_POS::FIRST], &isArrayBuffer); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!isArrayBuffer) { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succPath, buf, bufLen] = NVal(env, funcArg[NARG_POS::FIRST]).ToArraybuffer(); + if (!succPath) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + pathStr = std::string(static_cast(buf), bufLen); + } else { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!GetOptionArg(env, funcArg, g_optionArgs, string(path.get()))) { + if (!GetOptionArg(env, funcArg, g_optionArgs, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; } vector direntsRes; int ret = 0; - ret = g_optionArgs.recursion ? RecursiveFunc(path.get(), direntsRes) : FilterFileRes(path.get(), direntsRes); + ret = g_optionArgs.recursion ? RecursiveFunc(pathStr, direntsRes) : FilterFileRes(pathStr, direntsRes); if (ret) { NError(ret).ThrowErr(env); return nullptr; } - auto res = DoListFileVector2NV(env, string(path.get()), direntsRes, g_optionArgs.recursion); + auto res = DoListFileVector2NV(env, pathStr, direntsRes, g_optionArgs.recursion); g_optionArgs.Clear(); return res; } diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 81efa811273698161d2039ce15a0107d964aeb4f..58de4cebd07ec774717aa3f3e54a744f602e7520 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -229,17 +229,69 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); - if (!succPath) { - HILOGE("Invalid path"); + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + unsigned int mode = 0; + if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succMode, mode] = GetJsFlags(env, funcArg); - if (!succMode) { + std::string pathStr; + if (valueType == napi_string) { + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); + if (!succPath) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succMode, JSmode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + pathStr = std::string(path.get()); + mode = JSmode; + } else if(valueType == napi_object) { + bool isArrayBuffer = false; + status = napi_is_arraybuffer(env, funcArg[NARG_POS::FIRST], &isArrayBuffer); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + if (!isArrayBuffer) { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succMode, JSmode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + mode = JSmode; + size_t arrayBufferLength; + void* arrayBufferData; + status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + napi_ref arrayBufferRef; + status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + pathStr = std::string(static_cast(arrayBufferData), arrayBufferLength); + status = napi_delete_reference(env, arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } else { + HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); + NError(EINVAL).ThrowErr(env); return nullptr; } - string pathStr(path.get()); + #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (pathStr.find("://") != string::npos) { auto [res, uriStr] = OpenFileByUri(pathStr, mode);