From 4a43b3e3dd90e34d0cb122903e6111f4e848527f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=86=AC=E6=B4=8B?= Date: Wed, 6 Aug 2025 10:38:01 +0800 Subject: [PATCH 1/6] Modify build.gn for file_api construction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 顾冬洋 --- interfaces/kits/js/BUILD.gn | 5 ++ interfaces/test/unittest/js/BUILD.gn | 115 +++++++++++++++++++++++++-- 2 files changed, 114 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 25490bdae..645ed82ba 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -835,6 +835,7 @@ ohos_shared_library("ani_file_fs") { cfi_cross_dso = true debug = false } + shlib_type = "ani" output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" @@ -905,6 +906,7 @@ ohos_shared_library("ani_file_hash") { cfi_cross_dso = true debug = false } + shlib_type = "ani" output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" @@ -969,6 +971,7 @@ ohos_shared_library("ani_file_securitylabel") { cfi_cross_dso = true debug = false } + shlib_type = "ani" output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" @@ -1035,6 +1038,7 @@ ohos_shared_library("ani_file_environment") { cfi_cross_dso = true debug = false } + shlib_type = "ani" output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" @@ -1101,6 +1105,7 @@ ohos_shared_library("ani_file_statvfs") { cfi_cross_dso = true debug = false } + shlib_type = "ani" output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index d7b5876fc..2640a84b1 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -14,12 +14,20 @@ import("//build/test.gni") import("//foundation/filemanagement/file_api/file_api.gni") +ani_file_environment_core = [ + "${src_path}/common/file_helper/fd_guard.cpp", + "${src_path}/mod_environment/environment_core.cpp", + "${src_path}/mod_fs/fs_utils.cpp", +] ohos_unittest("ani_file_environment_test") { branch_protector_ret = "pac_ret" testonly = true module_out_path = "file_api/file_api" include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/common/file_helper", + "${file_api_path}/interfaces/kits/js/src/mod_fs", + "${file_api_path}/interfaces/kits/js/src/common", "${src_path}/mod_environment", "${src_path}/mod_environment/ani", "${file_api_path}/interfaces/test/unittest/js/mod_environment/mock", @@ -31,9 +39,9 @@ ohos_unittest("ani_file_environment_test") { "mod_environment/mock/ipc_skeleton_mock.cpp", "mod_environment/mock/parameter_mock.cpp", ] + sources += ani_file_environment_core deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_environment", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] @@ -56,6 +64,59 @@ ohos_unittest("ani_file_environment_test") { defines = [ "private=public" ] } +ani_file_fs_core = [ + "${src_path}/common/file_helper/fd_guard.cpp", + "${src_path}/mod_fs/class_atomicfile/fs_atomicfile.cpp", + "${src_path}/mod_fs/class_file/file_instantiator.cpp", + "${src_path}/mod_fs/class_file/fs_file.cpp", + "${src_path}/mod_fs/class_randomaccessfile/fs_randomaccessfile.cpp", + "${src_path}/mod_fs/class_readeriterator/fs_reader_iterator.cpp", + "${src_path}/mod_fs/class_stat/fs_stat.cpp", + "${src_path}/mod_fs/class_stat/stat_instantiator.cpp", + "${src_path}/mod_fs/class_stream/fs_stream.cpp", + "${src_path}/mod_fs/class_stream/stream_instantiator.cpp", + "${src_path}/mod_fs/class_tasksignal/fs_task_signal.cpp", + "${src_path}/mod_fs/class_watcher/fs_file_watcher.cpp", + "${src_path}/mod_fs/class_watcher/fs_watcher.cpp", + "${src_path}/mod_fs/class_watcher/watcher_data_cache.cpp", + "${src_path}/mod_fs/fs_utils.cpp", + "${src_path}/mod_fs/properties/access_core.cpp", + "${src_path}/mod_fs/properties/close_core.cpp", + "${src_path}/mod_fs/properties/connectdfs_core.cpp", + "${src_path}/mod_fs/properties/copy_core.cpp", + "${src_path}/mod_fs/properties/copy_dir_core.cpp", + "${src_path}/mod_fs/properties/copy_file_core.cpp", + "${src_path}/mod_fs/properties/copy_listener/trans_listener_core.cpp", + "${src_path}/mod_fs/properties/create_randomaccessfile_core.cpp", + "${src_path}/mod_fs/properties/create_stream_core.cpp", + "${src_path}/mod_fs/properties/dfs_listener/file_dfs_listener_stub.cpp", + "${src_path}/mod_fs/properties/disconnectdfs_core.cpp", + "${src_path}/mod_fs/properties/dup_core.cpp", + "${src_path}/mod_fs/properties/fdatasync_core.cpp", + "${src_path}/mod_fs/properties/fdopen_stream_core.cpp", + "${src_path}/mod_fs/properties/fsync_core.cpp", + "${src_path}/mod_fs/properties/listfile_core.cpp", + "${src_path}/mod_fs/properties/lseek_core.cpp", + "${src_path}/mod_fs/properties/lstat_core.cpp", + "${src_path}/mod_fs/properties/mkdir_core.cpp", + "${src_path}/mod_fs/properties/mkdtemp_core.cpp", + "${src_path}/mod_fs/properties/move_core.cpp", + "${src_path}/mod_fs/properties/movedir_core.cpp", + "${src_path}/mod_fs/properties/open_core.cpp", + "${src_path}/mod_fs/properties/read_core.cpp", + "${src_path}/mod_fs/properties/read_lines_core.cpp", + "${src_path}/mod_fs/properties/read_text_core.cpp", + "${src_path}/mod_fs/properties/rename_core.cpp", + "${src_path}/mod_fs/properties/rmdir_core.cpp", + "${src_path}/mod_fs/properties/stat_core.cpp", + "${src_path}/mod_fs/properties/symlink_core.cpp", + "${src_path}/mod_fs/properties/truncate_core.cpp", + "${src_path}/mod_fs/properties/unlink_core.cpp", + "${src_path}/mod_fs/properties/utimes_core.cpp", + "${src_path}/mod_fs/properties/watcher_core.cpp", + "${src_path}/mod_fs/properties/write_core.cpp", + "${src_path}/mod_fs/properties/xattr_core.cpp", +] ohos_unittest("ani_file_fs_mock_test") { branch_protector_ret = "pac_ret" testonly = true @@ -63,6 +124,10 @@ ohos_unittest("ani_file_fs_mock_test") { module_out_path = "file_api/file_api" include_dirs = [ + "./include", + "${file_api_path}/interfaces/kits/js/src/common/file_helper", + "${file_api_path}/interfaces/kits/js/src/mod_fs", + "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", @@ -119,9 +184,9 @@ ohos_unittest("ani_file_fs_mock_test") { "mod_fs/properties/write_core_mock_test.cpp", "mod_fs/properties/xattr_core_mock_test.cpp", ] + sources += ani_file_fs_core deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", @@ -140,6 +205,9 @@ ohos_unittest("ani_file_fs_mock_test") { "hilog:libhilog", "ipc:ipc_core", "libuv:uv", + "data_share:datashare_common", + "data_share:datashare_consumer", + "hisysevent:libhisysevent", ] libs = [ "dl" ] @@ -159,6 +227,11 @@ ohos_unittest("ani_file_fs_test") { resource_config_file = "../resource/ohos_test.xml" include_dirs = [ + "./include", + "${file_api_path}/interfaces/kits/js/src/common/file_helper", + "${file_api_path}/interfaces/kits/js/src/mod_fs", + "${file_api_path}/interfaces/kits/js/src/common", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", @@ -204,9 +277,9 @@ ohos_unittest("ani_file_fs_test") { "mod_fs/properties/write_core_test.cpp", "mod_fs/properties/xattr_core_test.cpp", ] + sources += ani_file_fs_core deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_fs", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/native:task_signal_native", "${file_api_path}/interfaces/kits/rust:rust_file", @@ -224,25 +297,40 @@ ohos_unittest("ani_file_fs_test") { "hilog:libhilog", "ipc:ipc_core", "libuv:uv", + "data_share:datashare_common", + "data_share:datashare_consumer", + "hisysevent:libhisysevent", ] defines = [ "private=public" ] } +ani_file_hash_core = [ + "${src_path}/common/file_helper/fd_guard.cpp", + "${src_path}/common/file_helper/hash_file.cpp", + "${src_path}/mod_fs/fs_utils.cpp", + "${src_path}/mod_hash/class_hashstream/hs_hashstream.cpp", + "${src_path}/mod_hash/hash_core.cpp", +] + ohos_unittest("ani_file_hash_test") { module_out_path = "file_api/file_api" resource_config_file = "../resource/ohos_test.xml" sources = [ "mod_hash/hash_core_test.cpp" ] + sources += ani_file_hash_core include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/common", + "./include", + "${file_api_path}/interfaces/kits/js/src/common/file_helper", + "${file_api_path}/interfaces/kits/js/src/mod_fs", "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_hash", ] deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_hash", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] @@ -253,23 +341,31 @@ ohos_unittest("ani_file_hash_test") { "googletest:gtest_main", "hilog:libhilog", "libuv:uv", + "openssl:libcrypto_shared", ] } +ani_file_securitylabel_core = [ + "${src_path}/common/file_helper/fd_guard.cpp", + "${src_path}/mod_fs/fs_utils.cpp", + "${src_path}/mod_securitylabel/securitylabel_core.cpp", +] ohos_unittest("ani_file_securitylabel_test") { module_out_path = "file_api/file_api" resource_config_file = "../resource/ohos_test.xml" sources = [ "mod_securitylabel/securitylabel_core_test.cpp" ] + sources += ani_file_securitylabel_core include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/common", + "${file_api_path}/interfaces/kits/js/src/common/file_helper", "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", ] deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_securitylabel", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] @@ -283,20 +379,27 @@ ohos_unittest("ani_file_securitylabel_test") { ] } +ani_file_statvfs_core = [ + "${src_path}/common/file_helper/fd_guard.cpp", + "${src_path}/mod_fs/fs_utils.cpp", + "${src_path}/mod_statvfs/statvfs_core.cpp", +] ohos_unittest("ani_file_statvfs_test") { module_out_path = "file_api/file_api" resource_config_file = "../resource/ohos_test.xml" sources = [ "mod_statvfs/statvfs_core_test.cpp" ] + sources += ani_file_statvfs_core include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/common", + "${file_api_path}/interfaces/kits/js/src/common/file_helper", "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_statvfs", ] deps = [ - "${file_api_path}/interfaces/kits/js:ani_file_statvfs", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", ] -- Gitee From ca59e81e29df384406f22e4dac969d2080492a08 Mon Sep 17 00:00:00 2001 From: yangbiao59 Date: Wed, 6 Aug 2025 03:20:48 +0000 Subject: [PATCH 2/6] =?UTF-8?q?dup=E6=8E=A5=E5=8F=A3=20=E9=87=8A=E6=94=BEd?= =?UTF-8?q?stFd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangbiao59 --- interfaces/kits/js/src/mod_fs/properties/dup.cpp | 2 ++ interfaces/kits/js/src/mod_fs/properties/dup_core.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/dup.cpp b/interfaces/kits/js/src/mod_fs/properties/dup.cpp index 77d71f738..de2de57d3 100644 --- a/interfaces/kits/js/src/mod_fs/properties/dup.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/dup.cpp @@ -51,6 +51,7 @@ napi_value Dup::Sync(napi_env env, napi_callback_info info) new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; if (!readlink_req) { HILOGE("Failed to request heap memory."); + close(dstFd); NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -58,6 +59,7 @@ napi_value Dup::Sync(napi_env env, napi_callback_info info) int ret = uv_fs_readlink(nullptr, readlink_req.get(), path.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to readlink fd, ret: %{public}d", ret); + close(dstFd); NError(ret).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/dup_core.cpp b/interfaces/kits/js/src/mod_fs/properties/dup_core.cpp index 716205149..17d099d13 100644 --- a/interfaces/kits/js/src/mod_fs/properties/dup_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/dup_core.cpp @@ -52,12 +52,14 @@ FsResult DupCore::DoDup(const int32_t &fd) new (std::nothrow) uv_fs_t, FsUtils::FsReqCleanup }; if (!readLinkReq) { HILOGE("Failed to request heap memory."); + close(dstFd); return FsResult::Error(ENOMEM); } string path = "/proc/self/fd/" + to_string(dstFd); int ret = uv_fs_readlink(nullptr, readLinkReq.get(), path.c_str(), nullptr); if (ret < 0) { HILOGE("Failed to readlink fd, ret: %{public}d", ret); + close(dstFd); return FsResult::Error(ret); } return FileInstantiator::InstantiateFile(dstFd, string(static_cast(readLinkReq->ptr)), false); -- Gitee From 790a5e6afdd6b3910f3ba552b1d4a706e4a58dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=86=AC=E6=B4=8B?= Date: Wed, 6 Aug 2025 17:38:21 +0800 Subject: [PATCH 3/6] Enforce alphabetical ordering in build.gn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 顾冬洋 --- interfaces/test/unittest/js/BUILD.gn | 29 +++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index 2640a84b1..069ac9f98 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -25,12 +25,12 @@ ohos_unittest("ani_file_environment_test") { module_out_path = "file_api/file_api" include_dirs = [ + "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/common/file_helper", "${file_api_path}/interfaces/kits/js/src/mod_fs", - "${file_api_path}/interfaces/kits/js/src/common", + "${file_api_path}/interfaces/test/unittest/js/mod_environment/mock", "${src_path}/mod_environment", "${src_path}/mod_environment/ani", - "${file_api_path}/interfaces/test/unittest/js/mod_environment/mock", ] sources = [ @@ -124,10 +124,9 @@ ohos_unittest("ani_file_fs_mock_test") { module_out_path = "file_api/file_api" include_dirs = [ - "./include", + "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/common/file_helper", "${file_api_path}/interfaces/kits/js/src/mod_fs", - "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_randomaccessfile", @@ -198,16 +197,16 @@ ohos_unittest("ani_file_fs_mock_test") { "ability_runtime:ability_manager", "app_file_service:fileuri_native", "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", "dfs_service:distributed_file_daemon_kit_inner", "dfs_service:libdistributedfileutils", "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "libuv:uv", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hisysevent:libhisysevent", ] libs = [ "dl" ] @@ -227,10 +226,9 @@ ohos_unittest("ani_file_fs_test") { resource_config_file = "../resource/ohos_test.xml" include_dirs = [ - "./include", + "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/common/file_helper", "${file_api_path}/interfaces/kits/js/src/mod_fs", - "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_atomicfile", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_file", @@ -291,15 +289,15 @@ ohos_unittest("ani_file_fs_test") { "ability_runtime:ability_manager", "app_file_service:fileuri_native", "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", "dfs_service:distributed_file_daemon_kit_inner", "dfs_service:libdistributedfileutils", "googletest:gtest_main", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "libuv:uv", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hisysevent:libhisysevent", ] defines = [ "private=public" ] @@ -322,11 +320,10 @@ ohos_unittest("ani_file_hash_test") { sources += ani_file_hash_core include_dirs = [ + "mock/libuv", "${file_api_path}/interfaces/kits/js/src/common", - "./include", "${file_api_path}/interfaces/kits/js/src/common/file_helper", "${file_api_path}/interfaces/kits/js/src/mod_fs", - "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_hash", ] @@ -359,9 +356,9 @@ ohos_unittest("ani_file_securitylabel_test") { sources += ani_file_securitylabel_core include_dirs = [ + "mock/libuv", "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/common/file_helper", - "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_securitylabel", ] @@ -393,9 +390,9 @@ ohos_unittest("ani_file_statvfs_test") { sources += ani_file_statvfs_core include_dirs = [ + "mock/libuv", "${file_api_path}/interfaces/kits/js/src/common", "${file_api_path}/interfaces/kits/js/src/common/file_helper", - "mock/libuv", "${file_api_path}/interfaces/kits/js/src/mod_statvfs", ] -- Gitee From 0d35e6695330624d308c8c9fb1c02be8b890923e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=86=AC=E6=B4=8B?= Date: Fri, 8 Aug 2025 08:57:03 +0800 Subject: [PATCH 4/6] handle listFile API failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 顾冬洋 --- interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index 79a94260d..84dd373c1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -129,8 +129,8 @@ tuple>> ParseArrayString(ani_env *env, ani_object return { true, nullopt }; } - ani_double length; - if (ANI_OK != env->Object_GetPropertyByName_Double( + ani_int length; + if (ANI_OK != env->Object_GetPropertyByName_Int( static_cast(resultRef), "length", &length) || length == 0) { return { false, nullopt }; } -- Gitee From f68fb462e6e118e61452688c41d703a6e703df41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=BE=E5=86=AC=E6=B4=8B?= Date: Tue, 12 Aug 2025 07:12:29 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=E6=9B=B4=E6=94=B9Array=5Fnew=E7=9A=84?= =?UTF-8?q?=E5=85=A5=E5=8F=82nullptr=E4=B8=BAundefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 顾冬洋 --- interfaces/kits/js/src/common/ani_helper/type_converter.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 662006f95..cbafc941e 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -295,7 +295,9 @@ std::tuple TypeConverter::ToAniStringList( } ani_array result = nullptr; - if (env->Array_New(length, nullptr, &result) != ANI_OK) { + ani_ref undefined; + env->GetUndefined(&undefined); + if (env->Array_New(length, undefined, &result) != ANI_OK) { return { false, result }; } for (uint32_t i = 0; i < length; i++) { -- Gitee From 537cce3a712afa6f93fe165943e9cc6ff8acbc95 Mon Sep 17 00:00:00 2001 From: liyuke Date: Fri, 8 Aug 2025 16:44:14 +0800 Subject: [PATCH 6/6] =?UTF-8?q?copy=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: Iac330d3708864c5d35210872e2e7fb83aa8ee34e --- .../src/common/ani_helper/ani_signature.cpp | 4 +- .../js/src/common/ani_helper/ani_signature.h | 2 + .../js/src/mod_fs/ani/bind_function_class.cpp | 3 ++ .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 2 +- .../class_tasksignal/ani/task_signal_ani.cpp | 25 ++++++++++++ .../class_tasksignal/ani/task_signal_ani.h | 1 + .../ani/task_signal_listener_ani.cpp | 38 +++++++++++++++++-- .../ani/task_signal_listener_ani.h | 8 ++-- .../ani/task_signal_wrapper.cpp | 10 ++--- .../ani/task_signal_wrapper.h | 2 +- .../class_tasksignal/fs_task_signal.cpp | 17 ++++++--- .../mod_fs/class_tasksignal/fs_task_signal.h | 6 +-- .../js/src/mod_fs/properties/ani/copy_ani.cpp | 28 ++------------ .../js/src/mod_fs/properties/copy_core.cpp | 2 +- .../kits/js/src/mod_fs/properties/copy_core.h | 6 +-- .../mod_fs/properties/copy_core_mock_test.cpp | 2 +- 16 files changed, 102 insertions(+), 54 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index a632f6f88..008031606 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -106,7 +106,9 @@ const string FS::StreamInner::ctorSig = Builder::BuildSignatureDescriptor({ Basi // FS::TaskSignal const Type FS::TaskSignal::classType = Builder::BuildClass("@ohos.file.fs.fileIo.TaskSignal"); const string FS::TaskSignal::classDesc = FS::TaskSignal::classType.Descriptor(); -const string FS::TaskSignal::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +const string FS::TaskSignal::ctorDesc = Builder::BuildConstructorName(); +const string FS::TaskSignal::ctorSig = Builder::BuildSignatureDescriptor({}); +const string FS::TaskSignal::nativeTaskSignal = "nativeTaskSignal"; // FS::WatcherInner const Type FS::WatcherInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WatcherInner"); const string FS::WatcherInner::classDesc = FS::WatcherInner::classType.Descriptor(); diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h index 3938f3fb8..e27e81c2c 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -162,7 +162,9 @@ struct StreamInner : public BaseType { struct TaskSignal : public BaseType { static const Type classType; static const string classDesc; + static const string ctorDesc; static const string ctorSig; + static const string nativeTaskSignal; }; struct WatcherInner : public BaseType { diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index cec91441f..d6221d30c 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -155,10 +155,13 @@ static ani_status BindStreamMethods(ani_env *env) static ani_status BindTaskSignalClassMethods(ani_env *env) { auto classDesc = FS::TaskSignal::classDesc.c_str(); + auto ctorDesc = FS::TaskSignal::ctorDesc.c_str(); + auto ctorSig = FS::TaskSignal::ctorSig.c_str(); std::array methods = { ani_native_function { "cancel", nullptr, reinterpret_cast(TaskSignalAni::Cancel) }, ani_native_function { "onCancelNative", nullptr, reinterpret_cast(TaskSignalAni::OnCancel) }, + ani_native_function { ctorDesc, ctorSig, reinterpret_cast(TaskSignalAni::Constructor) }, }; return BindClass(env, classDesc, methods); diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index ee6be66d5..5d71a03b1 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -1254,7 +1254,7 @@ export type ProgressListener = (progress: Progress) => void; export class TaskSignal { private nativeTaskSignal: long = 0; - + native constructor(); private native onCancelNative(): void; private onCancelResolve: (path: string) => void = (path: string): void => {}; diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp index 2b1229a69..da5b27fa8 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.cpp @@ -20,6 +20,7 @@ #include "error_handler.h" #include "filemgmt_libhilog.h" #include "fs_task_signal.h" +#include "task_signal_listener_ani.h" #include "task_signal_wrapper.h" #include "type_converter.h" @@ -30,6 +31,30 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +void TaskSignalAni::Constructor(ani_env *env, ani_object signalObj) +{ + auto taskSignal = CreateSharedPtr(); + if (taskSignal == nullptr) { + HILOGE("Failed to request heap memory."); + return; + } + + ani_vm *vm = nullptr; + env->GetVM(&vm); + auto listener = CreateUniquePtr(vm, signalObj, taskSignal); + if (listener == nullptr) { + HILOGE("Failed to request heap memory."); + return ; + } + + FsResult> result = FsTaskSignal::Constructor(move(taskSignal), move(listener)); + if (!result.IsSuccess()) { + HILOGE("Failed to FsTaskSignal::Constructor"); + return ; + } + TaskSignalWrapper::Wrap(env, signalObj, move(result.GetData().value())); +} + void TaskSignalAni::Cancel(ani_env *env, [[maybe_unused]] ani_object object) { FsTaskSignal *copySignal = TaskSignalWrapper::Unwrap(env, object); diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.h b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.h index d27e52538..c2d9a0901 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.h +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_ani.h @@ -25,6 +25,7 @@ namespace ANI { class TaskSignalAni final { public: + static void Constructor(ani_env *env, ani_object obj); static void Cancel(ani_env *env, [[maybe_unused]] ani_object object); static void OnCancel(ani_env *env, [[maybe_unused]] ani_object object); }; diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp index 8f533889c..f5cbf4a50 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.cpp @@ -26,8 +26,26 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; +bool TaskSignalListenerAni::CreateGlobalReference() +{ + if (signalRef) { + return true; + } + ani_env *env = AniHelper::GetThreadEnv(vm); + int ret = 0; + if ((ret = env->GlobalReference_Create(static_cast(signalObj), &signalRef)) != ANI_OK) { + HILOGE("TaskSignalListenerAni GlobalReference_Create failed: %{public}d", ret); + signalRef = nullptr; + return false; + } + return true; +} + void TaskSignalListenerAni::OnCancel() { + if (!CreateGlobalReference()) { + return; + } auto filepath = taskSignal->filePath_; auto task = [this, filepath]() { SendCancelEvent(filepath); }; AniHelper::SendEventToMainThread(task); @@ -39,8 +57,8 @@ void TaskSignalListenerAni::SendCancelEvent(const string &filepath) const HILOGE("Cannot send cancel event because the vm is null."); return; } - if (signalObj == nullptr) { - HILOGE("Cannot send cancel event because the signalObj is null."); + if (signalRef == nullptr) { + HILOGE("Cannot send cancel event because the signalRef is null."); return; } ani_env *env = AniHelper::GetThreadEnv(vm); @@ -53,11 +71,25 @@ void TaskSignalListenerAni::SendCancelEvent(const string &filepath) const HILOGE("Cannot convert filepath to ani string!"); return; } - auto ret = env->Object_CallMethodByName_Void(signalObj, "onCancelCallback", nullptr, aniPath); + + auto ret = env->Object_CallMethodByName_Void(static_cast(signalRef), "onCancelCallback", nullptr, + aniPath); if (ret != ANI_OK) { HILOGE("Call onCancelCallback failed, err: %{public}d", ret); return; } } +TaskSignalListenerAni::~TaskSignalListenerAni() +{ + if (signalRef == nullptr) { + return; + } + ani_env *env = AniHelper::GetThreadEnv(vm); + int ret = 0; + if ((ret = env->GlobalReference_Delete(signalRef)) != ANI_OK) { + HILOGE("TaskSignalListenerAni GlobalReference_Delete: %{public}d", ret); + } +} + } // namespace OHOS::FileManagement::ModuleFileIO::ANI diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.h b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.h index ae02a941e..ab84ded38 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.h +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_listener_ani.h @@ -25,20 +25,22 @@ using namespace DistributedFS::ModuleTaskSignal; class TaskSignalListenerAni : public TaskSignalListener { public: - TaskSignalListenerAni(ani_vm *vm, const ani_object &signalObj, std::shared_ptr taskSignal) - : vm(vm), signalObj(signalObj), taskSignal(taskSignal) {} + TaskSignalListenerAni(ani_vm *vm, const ani_object &signalObject, std::shared_ptr taskSignal) + : vm(vm), signalObj(signalObject), taskSignal(taskSignal) {} void OnCancel() override; public: TaskSignalListenerAni() = default; - ~TaskSignalListenerAni() = default; + ~TaskSignalListenerAni(); private: void SendCancelEvent(const std::string &filepath) const; + bool CreateGlobalReference(); private: ani_vm *vm; ani_object signalObj; + ani_ref signalRef = nullptr; std::shared_ptr taskSignal; }; diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp index 79be64231..4ae2b06f4 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp @@ -37,12 +37,12 @@ FsTaskSignal *TaskSignalWrapper::Unwrap(ani_env *env, ani_object object) HILOGE("Unwrap taskSignal obj failed! status: %{public}d", status); return nullptr; } - uintptr_t ptrValue = static_cast(nativePtr); - FsTaskSignal *copySignal = reinterpret_cast(ptrValue); + + FsTaskSignal *copySignal = reinterpret_cast(nativePtr); return copySignal; } -bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, const FsTaskSignal *signal) +bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, unique_ptr signal) { if (object == nullptr) { HILOGE("TaskSignal obj is null!"); @@ -54,14 +54,14 @@ bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, const FsTaskSignal return false; } - ani_long ptr = static_cast(reinterpret_cast(signal)); + ani_long ptr = reinterpret_cast(signal.get()); auto status = env->Object_SetFieldByName_Long(object, "nativeTaskSignal", ptr); if (status != ANI_OK) { HILOGE("Wrap taskSignal obj failed! status: %{public}d", status); return false; } - + signal.release(); return true; } diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.h b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.h index 4fe9fb143..7e3093994 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.h +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.h @@ -27,7 +27,7 @@ namespace ANI { class TaskSignalWrapper final { public: static FsTaskSignal *Unwrap(ani_env *env, ani_object object); - static bool Wrap(ani_env *env, ani_object object, const FsTaskSignal *signal); + static bool Wrap(ani_env *env, ani_object object, unique_ptr signal); }; } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.cpp index d9b4b6ffe..bc2b2b418 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.cpp @@ -24,25 +24,25 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; -FsResult> FsTaskSignal::Constructor( +FsResult> FsTaskSignal::Constructor( shared_ptr taskSignal, shared_ptr signalListener) { if (!taskSignal) { HILOGE("Invalid taskSignal"); - return FsResult>::Error(EINVAL); + return FsResult>::Error(EINVAL); } if (!signalListener) { HILOGE("Invalid signalListener"); - return FsResult>::Error(EINVAL); + return FsResult>::Error(EINVAL); } - auto copySignal = CreateSharedPtr(); + auto copySignal = CreateUniquePtr(); if (copySignal == nullptr) { HILOGE("Failed to request heap memory."); - return FsResult>::Error(ENOMEM); + return FsResult>::Error(ENOMEM); } copySignal->taskSignal_ = move(taskSignal); copySignal->signalListener_ = move(signalListener); - return FsResult>::Success(copySignal); + return FsResult>::Success(move(copySignal)); } FsResult FsTaskSignal::Cancel() @@ -74,6 +74,11 @@ shared_ptr FsTaskSignal::GetTaskSignal() const return taskSignal_; } +FsTaskSignal::FsTaskSignal() +{ + taskSignal_ = CreateSharedPtr(); +} + } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.h b/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.h index 33161108c..87b7fbadf 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.h +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/fs_task_signal.h @@ -27,14 +27,12 @@ using namespace DistributedFS::ModuleTaskSignal; class FsTaskSignal { public: - static FsResult> Constructor( + static FsResult> Constructor( shared_ptr taskSignal, shared_ptr signalListener); FsResult Cancel(); FsResult OnCancel(); shared_ptr GetTaskSignal() const; - -public: - FsTaskSignal() = default; + FsTaskSignal(); ~FsTaskSignal() = default; FsTaskSignal(const FsTaskSignal &other) = delete; FsTaskSignal &operator=(const FsTaskSignal &other) = delete; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp index 541cd7f6c..6bca55f03 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp @@ -81,33 +81,11 @@ static bool ParseCopySignalFromOptionArg(ani_env *env, const ani_object &options return true; } - auto taskSignal = CreateSharedPtr(); - if (taskSignal == nullptr) { - HILOGE("Failed to request heap memory."); - return false; - } - - auto signalObj = static_cast(prog); - ani_vm *vm = nullptr; - env->GetVM(&vm); - auto listener = CreateSharedPtr(vm, signalObj, taskSignal); - if (listener == nullptr) { - HILOGE("Failed to request heap memory."); - return false; - } - - auto result = FsTaskSignal::Constructor(taskSignal, listener); - if (!result.IsSuccess()) { - return false; - } - - auto copySignal = result.GetData().value(); - auto succ = TaskSignalWrapper::Wrap(env, signalObj, copySignal.get()); - if (!succ) { - return false; + FsTaskSignal *copySignal = TaskSignalWrapper::Unwrap(env, static_cast(prog)); + if (copySignal != nullptr) { + opts.copySignal = copySignal; } - opts.copySignal = move(copySignal); return true; } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp index 45403ca6d..6e6c2b732 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.cpp @@ -774,7 +774,7 @@ tuple> CopyCore::CreateFileInfos( } auto copySignal = options.value().copySignal; if (copySignal) { - infos->taskSignal = copySignal->GetTaskSignal(); + infos->taskSignal = copySignal->GetTaskSignal().get(); } } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_core.h b/interfaces/kits/js/src/mod_fs/properties/copy_core.h index 387792260..fb30c4048 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/copy_core.h @@ -36,8 +36,8 @@ using namespace OHOS::AppExecFwk; using namespace DistributedFS::ModuleTaskSignal; struct CopyOptions { - std::shared_ptr progressListener; - std::shared_ptr copySignal; + shared_ptr progressListener; + FsTaskSignal* copySignal = nullptr; }; struct ReceiveInfo { @@ -96,7 +96,7 @@ struct FsFileInfos { bool run = true; bool hasListener = false; std::shared_ptr listener = nullptr; - std::shared_ptr taskSignal = nullptr; + TaskSignal* taskSignal = nullptr; std::set filePaths; int exceptionCode = ERRNO_NOERR; // notify copy thread or listener thread has exceptions. bool operator==(const FsFileInfos &infos) const diff --git a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp index 6e832264a..104552554 100644 --- a/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp +++ b/interfaces/test/unittest/js/mod_fs/properties/copy_core_mock_test.cpp @@ -531,7 +531,7 @@ HWTEST_F(CopyCoreMockTest, CopyCoreMockTest_CreateFileInfos_001, testing::ext::T copySignal->taskSignal_ = std::make_shared(); auto options = std::make_optional(); options->progressListener = std::make_shared(); - options->copySignal = std::move(copySignal); + options->copySignal = copySignal.get(); auto [errCode, infos] = CopyCore::CreateFileInfos(srcFile, destFile, options); EXPECT_EQ(errCode, ERRNO_NOERR); -- Gitee