From fcb26f60a7e68700390ea0ae417e30dc6c2f9678 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 10:25:41 +0800 Subject: [PATCH 1/5] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- .../js/src/mod_fs/properties/listfile.cpp | 62 ++++++++++++++- .../kits/js/src/mod_fs/properties/open.cpp | 79 +++++++++++++++++-- 2 files changed, 131 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 777297995..808627853 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -326,13 +326,67 @@ 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"); + // get path type:arraybuffer || string + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - if (!GetOptionArg(env, funcArg, g_optionArgs, string(path.get()))) { + std::string pathStr; + if(valueType==napi_string){//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){//arraybuffer + 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; + } + size_t arrayBufferLength; + void* arrayBufferData; + //get [arraybuffer pointer] and its [length] to transfer to string + status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + //create reference to assert arraybuffer alive while being used + + napi_ref arrayBufferRef; + status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + //get path + pathStr = std::string(static_cast(data), length); + + // delete referrence + 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; + } + + if (!GetOptionArg(env, funcArg, g_optionArgs, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 81efa8112..3d0d19d26 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -229,17 +229,84 @@ 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"); + // get path type:arraybuffer || string + napi_valuetype valueType; + napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); + + if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succMode, mode] = GetJsFlags(env, funcArg); - if (!succMode) { + + //init path + std::string pathStr; + if(valueType==napi_string){//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, mode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + pathStr = std::string(path.get()); + + } + else if(valueType==napi_object){//arraybuffer + 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, mode] = GetJsFlags(env, funcArg); + if (!succMode) { + return nullptr; + } + + //get [arraybuffer pointer] and its [length] to transfer to string + 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; + } + + //create reference to assert arraybuffer alive while being used + napi_ref arrayBufferRef; + + status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); + if (status != napi_ok) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //get path + pathStr = std::string(static_cast(data), length); + + // delete referrence + 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); -- Gitee From 9ac17b27d482c234702a0e2e8104a15db868895e Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 11:14:42 +0800 Subject: [PATCH 2/5] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- interfaces/kits/js/src/mod_fs/properties/listfile.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 808627853..e83a7c55c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -349,7 +349,7 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - if(!IsArrayBuffer) { + if(!isArrayBuffer) { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -371,7 +371,7 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) return nullptr; } //get path - pathStr = std::string(static_cast(data), length); + pathStr = std::string(static_cast(arrayBufferData), length); // delete referrence status = napi_delete_reference(env, arrayBufferRef); -- Gitee From abaa1f38bea6223fe261dbfbff7810db0a72a6e6 Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 14:09:17 +0800 Subject: [PATCH 3/5] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- .../js/src/mod_fs/properties/listfile.cpp | 34 +++++---------- .../kits/js/src/mod_fs/properties/open.cpp | 42 +++++-------------- 2 files changed, 21 insertions(+), 55 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index e83a7c55c..de6338ab6 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -329,11 +329,14 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) // get path type:arraybuffer || string 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){//string auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); if (!succPath) { @@ -354,31 +357,14 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - size_t arrayBufferLength; - void* arrayBufferData; - //get [arraybuffer pointer] and its [length] to transfer to string - status = napi_get_arraybuffer_info(env, funcArg[NARG_POS::FIRST], &arrayBufferData, &arrayBufferLength); - if (status != napi_ok) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - //create reference to assert arraybuffer alive while being used - - napi_ref arrayBufferRef; - status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); - if (status != napi_ok) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - //get path - pathStr = std::string(static_cast(arrayBufferData), length); - - // delete referrence - status = napi_delete_reference(env, arrayBufferRef); - if (status != napi_ok) { + auto [succPath, buf, bufLen] = NVal(env, funcArg[NARG_POS::FIRST]).ToArraybuffer(); + if (!succPath) { + HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; } + // 将 arraybuffer 转换为 string + pathStr = std::string(static_cast(buf), bufLen); } else { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); @@ -393,12 +379,12 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) } 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 3d0d19d26..e76ca5df9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -232,12 +232,11 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) // get path type:arraybuffer || string 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; } - //init path std::string pathStr; if(valueType==napi_string){//string @@ -247,12 +246,12 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succMode, mode] = GetJsFlags(env, funcArg); + auto [succMode, JSmode] = GetJsFlags(env, funcArg); if (!succMode) { return nullptr; } pathStr = std::string(path.get()); - + mode=JSmode; } else if(valueType==napi_object){//arraybuffer bool isArrayBuffer=false; @@ -261,45 +260,26 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - if(!IsArrayBuffer) { + if(!isArrayBuffer) { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); NError(EINVAL).ThrowErr(env); return nullptr; } - auto [succMode, mode] = GetJsFlags(env, funcArg); + auto [succMode, JSmode] = GetJsFlags(env, funcArg); if (!succMode) { return nullptr; } + mode=JSmode; - //get [arraybuffer pointer] and its [length] to transfer to string - 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; - } - - //create reference to assert arraybuffer alive while being used - napi_ref arrayBufferRef; - - status = napi_create_reference(env, funcArg[NARG_POS::FIRST], 1, &arrayBufferRef); - if (status != napi_ok) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - //get path - pathStr = std::string(static_cast(data), length); - - // delete referrence - status = napi_delete_reference(env, arrayBufferRef); - if (status != napi_ok) { + auto [succPath, buf, bufLen] = NVal(env, funcArg[NARG_POS::FIRST]).ToArraybuffer(); + if (!succPath) { + HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; } + // 将 arraybuffer 转换为 string + pathStr = std::string(static_cast(buf), bufLen); } else { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); -- Gitee From b7068a4cc9bef29d7b7a1e1f782b06612f9acd0c Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 15:51:31 +0800 Subject: [PATCH 4/5] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- .../js/src/mod_fs/properties/listfile.cpp | 14 ++----- .../kits/js/src/mod_fs/properties/open.cpp | 41 +++++++++++-------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index de6338ab6..365ea5de2 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -326,7 +326,6 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - // get path type:arraybuffer || string napi_valuetype valueType; napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); @@ -334,10 +333,8 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - std::string pathStr; - - if(valueType==napi_string){//string + if (valueType == napi_string) { auto [succPath, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); if (!succPath) { HILOGE("Invalid path"); @@ -345,14 +342,14 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) return nullptr; } pathStr = std::string(path.get()); - } else if(valueType==napi_object){//arraybuffer - bool isArrayBuffer=false; + } 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) { + if (!isArrayBuffer) { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -363,15 +360,12 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - // 将 arraybuffer 转换为 string 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, pathStr)) { HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index e76ca5df9..58de4cebd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -229,17 +229,15 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - // get path type:arraybuffer || string napi_valuetype valueType; napi_status status = napi_typeof(env, funcArg[NARG_POS::FIRST], &valueType); - unsigned int mode=0; + unsigned int mode = 0; if (status != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; } - //init path std::string pathStr; - if(valueType==napi_string){//string + if (valueType == napi_string) { auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8StringPath(); if (!succPath) { HILOGE("Invalid path"); @@ -251,36 +249,43 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) return nullptr; } pathStr = std::string(path.get()); - mode=JSmode; - } - else if(valueType==napi_object){//arraybuffer - bool isArrayBuffer=false; + 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) { + 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; - - auto [succPath, buf, bufLen] = NVal(env, funcArg[NARG_POS::FIRST]).ToArraybuffer(); - if (!succPath) { - HILOGE("Invalid path"); + 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; } - // 将 arraybuffer 转换为 string - pathStr = std::string(static_cast(buf), bufLen); - } else { HILOGE("Type of path unmathched:expect string|arraybuffer|uri"); NError(EINVAL).ThrowErr(env); -- Gitee From f29032a749adf4c53b96774fef1d4b055a7fbf7b Mon Sep 17 00:00:00 2001 From: hahahahahayesyeseys <1145018948@qq.com> Date: Thu, 18 Jul 2024 15:51:43 +0800 Subject: [PATCH 5/5] add arraybuffer to open and listfile Signed-off-by: hahahahahayesyeseys <1145018948@qq.com> --- .vscode/c_cpp_properties.json | 16 ++++++++++++++++ .vscode/settings.json | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..ba951e1ba --- /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 000000000..ce81378ac --- /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 -- Gitee