From dbab3b4f0df7cfff7544221a0026ac7e561d4369 Mon Sep 17 00:00:00 2001 From: liyuke Date: Thu, 21 Aug 2025 11:14:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9C=A8ets=E8=BF=9B=E8=A1=8C=E5=9E=83?= =?UTF-8?q?=E5=9C=BE=E5=9B=9E=E6=94=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke Change-Id: I14f2a1bd593e13081f37a95385a3621092747fde --- interfaces/kits/js/BUILD.gn | 3 + .../src/common/ani_helper/ani_signature.cpp | 7 +- .../js/src/common/ani_helper/ani_signature.h | 11 +++ .../js/src/mod_fs/ani/bind_function_class.cpp | 17 ++++ .../js/src/mod_fs/ani/cleaner/cleaner_ani.cpp | 86 +++++++++++++++++ .../js/src/mod_fs/ani/cleaner/cleaner_ani.h | 35 +++++++ .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 96 +++++++++++++++++-- .../class_atomicfile/ani/atomicfile_ani.cpp | 12 +++ .../mod_fs/class_atomicfile/fs_atomicfile.h | 3 + .../kits/js/src/mod_fs/class_file/fs_file.h | 6 +- .../fs_randomaccessfile.h | 17 +--- .../class_readeriterator/fs_reader_iterator.h | 4 + .../kits/js/src/mod_fs/class_stat/fs_stat.h | 4 + .../js/src/mod_fs/class_stream/fs_stream.h | 18 ++-- .../js/src/mod_fs/class_watcher/fs_watcher.h | 5 +- .../js/src/mod_fs/common/obj/read_options.h | 32 +++++++ .../js/src/mod_fs/common/obj/write_options.h | 33 +++++++ .../kits/js/src/mod_fs/properties/read_core.h | 6 +- .../js/src/mod_fs/properties/write_core.h | 7 +- 19 files changed, 356 insertions(+), 46 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp create mode 100644 interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.h create mode 100644 interfaces/kits/js/src/mod_fs/common/obj/read_options.h create mode 100644 interfaces/kits/js/src/mod_fs/common/obj/write_options.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 645ed82ba..6655a41a5 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -666,6 +666,7 @@ ohos_shared_library("ani_file_fs") { include_dirs = [ "include/ipc", "src/mod_fs/ani", + "src/mod_fs/ani/cleaner", "src/mod_fs/class_atomicfile", "src/mod_fs/class_atomicfile/ani", "src/mod_fs/class_file", @@ -682,6 +683,7 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/class_tasksignal/ani", "src/mod_fs/class_watcher", "src/mod_fs/class_watcher/ani", + "src/mod_fs/common/obj", "src/mod_fs/properties", "src/mod_fs/properties/ani", "src/mod_fs/properties/copy_listener", @@ -693,6 +695,7 @@ ohos_shared_library("ani_file_fs") { "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", + "src/mod_fs/ani/cleaner/cleaner_ani.cpp", "src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp", "src/mod_fs/class_atomicfile/fs_atomicfile.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", 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 1f65c2d5a..abb600994 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -99,7 +99,7 @@ const string FS::ReaderIteratorInner::ctorSig = Builder::BuildSignatureDescripto const Type FS::ReaderIteratorResultInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ReaderIteratorResultInner"); const string FS::ReaderIteratorResultInner::classDesc = FS::ReaderIteratorResultInner::classType.Descriptor(); -const string FS::ReaderIteratorResultInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +const string FS::ReaderIteratorResultInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::booleanType, BuiltInTypes::stringType }); // FS::StatInner const Type FS::StatInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.StatInner"); const string FS::StatInner::classDesc = FS::StatInner::classType.Descriptor(); @@ -114,6 +114,7 @@ const string FS::TaskSignal::classDesc = FS::TaskSignal::classType.Descriptor(); const string FS::TaskSignal::ctorDesc = Builder::BuildConstructorName(); const string FS::TaskSignal::ctorSig = Builder::BuildSignatureDescriptor({}); const string FS::TaskSignal::nativeTaskSignal = "nativeTaskSignal"; +const string FS::TaskSignal::bindNativePtrSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); // FS::WatcherInner const Type FS::WatcherInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WatcherInner"); const string FS::WatcherInner::classDesc = FS::WatcherInner::classType.Descriptor(); @@ -137,6 +138,7 @@ const string FS::WriteStream::ctorSig = const Type FS::AtomicFile::classType = Builder::BuildClass("@ohos.file.fs.fileIo.AtomicFile"); const string FS::AtomicFile::classDesc = FS::AtomicFile::classType.Descriptor(); const string FS::AtomicFile::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +const string FS::AtomicFile::bindNativePtrSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); // FS::ReadStreamOptionsInner const Type FS::ReadStreamOptionsInner::classType = Builder::BuildClass("@ohos.file.fs.ReadStreamOptionsInner"); const string FS::ReadStreamOptionsInner::classDesc = FS::ReadStreamOptionsInner::classType.Descriptor(); @@ -165,5 +167,8 @@ const string Impl::StatvfsImpl::classDesc = Impl::StatvfsImpl::classType.Descrip const Type HASH::HashStreamImpl::classType = Builder::BuildClass("@ohos.file.hash.HashStreamImpl"); const string HASH::HashStreamImpl::classDesc = HASH::HashStreamImpl::classType.Descriptor(); const string HASH::HashStreamImpl::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +// CLEANER::CleanerImpl +const Type CLEANER::CleanerImpl::classType = Builder::BuildClass("@ohos.file.fs.Cleaner"); +const string CLEANER::CleanerImpl::classDesc = CLEANER::CleanerImpl::classType.Descriptor(); } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature \ No newline at end of file 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 71ed2a8f4..c4734bd6b 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -172,6 +172,7 @@ struct TaskSignal : public BaseType { static const string ctorDesc; static const string ctorSig; static const string nativeTaskSignal; + static const string bindNativePtrSig; }; struct WatcherInner : public BaseType { @@ -207,6 +208,7 @@ struct AtomicFile : public BaseType { static const Type classType; static const string classDesc; static const string ctorSig; + static const string bindNativePtrSig; }; struct ReadStreamOptionsInner : public BaseType { @@ -260,6 +262,15 @@ struct HashStreamImpl : public BaseType { } // namespace HASH +namespace CLEANER { + +struct CleanerImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +} // namespace CLEANER + } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature #endif // INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H \ No newline at end of file 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 d6221d30c..cfecebcd7 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 @@ -61,6 +61,7 @@ #include "watcher_ani.h" #include "write_ani.h" #include "xattr_ani.h" +#include "cleaner_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; @@ -239,6 +240,17 @@ static ani_status BindStaticMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindCleanerMethods(ani_env *env) +{ + auto classDesc = CLEANER::CleanerImpl::classDesc.c_str(); + + std::array methods = { + ani_native_function { "clean", nullptr, reinterpret_cast(CleanerAni::Clean) }, + }; + + return BindClass(env, classDesc, methods); +} + static ani_status DoBindMethods(ani_env *env) { ani_status status; @@ -287,6 +299,11 @@ static ani_status DoBindMethods(ani_env *env) return status; }; + if ((status = BindCleanerMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native methods for Cleaner Class!"); + return status; + }; + return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp new file mode 100644 index 000000000..0a448bd80 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cleaner_ani.h" + +#include "filemgmt_libhilog.h" +#include "fs_atomicfile.h" +#include "fs_file.h" +#include "fs_randomaccessfile.h" +#include "fs_reader_iterator.h" +#include "fs_stat.h" +#include "fs_stream.h" +#include "fs_watcher.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI; + +void CleanerAni::Clean(ani_env *env, ani_object object) +{ + if (env == nullptr) { + HILOGE("env is nullptr"); + return; + } + ani_long ptr; + ani_status status = env->Object_GetFieldByName_Long(object, "ptr", &ptr); + if (status != ANI_OK) { + HILOGE("Clean Object_GetFieldByName_Long status: %{public}d", status); + return; + } + + ani_ref nameObj = nullptr; + if ((status = env->Object_GetFieldByName_Ref(object, "name", &nameObj)) != ANI_OK) { + HILOGE("Clean Object_GetFieldByName_Ref status: %{public}d", status); + return; + } + auto [succ, clsName] = TypeConverter::ToUTF8String(env, static_cast(nameObj)); + if (!succ) { + HILOGE("Clean ParseString failed."); + return; + } + if (clsName == "File") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else if (clsName == "RandomAccessFile") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else if (clsName == "ReaderIterator") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else if (clsName == "Stat") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else if (clsName == "Stream") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else if (clsName == "AtomicFile") { + HILOGE("Clean className: %{public}s", clsName.c_str()); + delete reinterpret_cast(ptr); + } else { + HILOGE("Clean unsupport className: %{public}s", clsName.c_str()); + } + return; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.h b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.h new file mode 100644 index 000000000..9217ad092 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_ANI_CLEANER_CLEANER_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_ANI_CLEANER_CLEANER_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class CleanerAni final { +public: + static void Clean(ani_env *env, ani_object object); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_ANI_CLEANER_CLEANER_ANI_H \ No newline at end of file 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 85d4d9891..039649ad5 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 @@ -14,6 +14,7 @@ */ import { BusinessError, AsyncCallback } from '@ohos.base'; import stream from '@ohos.util.stream'; +import hilog from '@ohos.hilog' const UNKNOWN_ERR: int = 13900042 const UNKNOWN_MSG: string = "Unknown error" @@ -25,6 +26,23 @@ function createBusinessError(code: int, msg: string): BusinessError { return err; } +class Cleaner { + private ptr: long = 0 + private name: string = ""; + constructor(ptr: long, name: string) { + this.ptr = ptr + this.name = name + } + native clean(): void +} + +function CleanerCallback(cleaner: Cleaner): void { + cleaner.clean() +} + +let destroyRegister = new FinalizationRegistry(CleanerCallback) +let unregisterToken = new object() + namespace fileIo { export namespace OpenMode { export const READ_ONLY = 0o0; @@ -1253,6 +1271,9 @@ export class ProgressInner implements Progress { export type ProgressListener = (progress: Progress) => void; export class TaskSignal { + static { + loadLibrary("ani_file_fs"); + } private nativeTaskSignal: long = 0; native constructor(); private native onCancelNative(): void; @@ -1320,11 +1341,21 @@ export class RandomAccessFileInner implements RandomAccessFile { filePointer: long = -1; private nativePtr: long = 0; + private cleaner: Cleaner | null = null; constructor(ptr: long) { if (this.nativePtr === 0) { this.nativePtr = ptr; } + this.registerCleaner(this.nativePtr) + } + + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "RandomAccessFile"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); } setFilePointer(filePointer: long): void { @@ -1459,11 +1490,20 @@ export class FileInner implements File { name: String = ""; private nativePtr: long = 0; + private cleaner: Cleaner | null = null; constructor(ptr: long) { if (this.nativePtr === 0) { this.nativePtr = ptr; } + this.registerCleaner(this.nativePtr) + } + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "File"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); } native getParent(): String; @@ -1520,16 +1560,13 @@ export enum LocationType { export class ReaderIteratorResultInner implements ReaderIteratorResult { - private nativePtr: long = 0; - - constructor(ptr: long) { - if (this.nativePtr === 0) { - this.nativePtr = ptr; - } - } - done: boolean = false; value: string = ""; + + constructor(done: boolean, value: string) { + this.done = done; + this.value = value; + } } export interface ReaderIterator { @@ -1538,11 +1575,20 @@ export interface ReaderIterator { export class ReaderIteratorInner implements ReaderIterator { private nativePtr: long = 0; + private cleaner: Cleaner | null = null; constructor(ptr: long) { if (this.nativePtr === 0) { this.nativePtr = ptr; } + this.registerCleaner(this.nativePtr) + } + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "ReaderIterator"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); } native next(): ReaderIteratorResult; @@ -1586,11 +1632,20 @@ export class StatInner implements Stat { location: LocationType = LocationType.LOCAL; private nativeStat: long = 0; + private cleaner: Cleaner | null = null; constructor(stat: long) { if (this.nativeStat === 0) { this.nativeStat = stat; } + this.registerCleaner(this.nativeStat) + } + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "Stat"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); } native isBlockDevice(): boolean; @@ -1622,11 +1677,20 @@ export interface Stream { export class StreamInner implements Stream { private nativePtr: long = 0; + private cleaner: Cleaner | null = null; constructor(ptr: long) { if (this.nativePtr === 0) { this.nativePtr = ptr; } + this.registerCleaner(this.nativePtr) + } + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "Stream"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); } close(): Promise { @@ -1927,9 +1991,25 @@ export class AtomicFile { } private nativePtr: long = 0; + private cleaner: Cleaner | null = null; private native getPath(): string; private writeStream: WriteStream | null = null; + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "AtomicFile"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); + } + bindNativePtr(ptr: long): void { + if (this.nativePtr === 0) { + this.nativePtr = ptr; + } + this.registerCleaner(this.nativePtr) + + } + native constructor(path: string); native getBaseFile(): File; diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index 8756289da..b979574bc 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -37,6 +37,17 @@ const std::string READ_STREAM_CLASS = "ReadStream"; const std::string WRITE_STREAM_CLASS = "WriteStream"; const std::string TEMP_FILE_SUFFIX = "_XXXXXX"; +void CallBindNativePtr(ani_env *env, ani_object obj, FsAtomicFile *fsAtomicFile) +{ + auto bindNativePtrSig = FS::AtomicFile::bindNativePtrSig.c_str(); + ani_long longValue = reinterpret_cast(fsAtomicFile); + ani_status ret = env->Object_CallMethodByName_Void(obj, "bindNativePtr", bindNativePtrSig, longValue); + if (ret != ANI_OK) { + HILOGE("Object_CallMethodByName_Void failed. ret = %{public}d", static_cast(ret)); + return; + } +} + void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj) { auto [succ, filePath] = TypeConverter::ToUTF8String(env, pathObj); @@ -59,6 +70,7 @@ void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj ErrorHandler::Throw(env, EIO); return; } + CallBindNativePtr(env, obj, ret.GetData().value()); } static FsAtomicFile *Unwrap(ani_env *env, ani_object object) diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h index 80996b1c7..4571e62fa 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h @@ -46,6 +46,9 @@ public: FsResult FailWrite(); FsResult Delete(); static void FinalizeCallback(void *finalizeData, [[maybe_unused]] void *finalizeHint); + ~FsAtomicFile() { + HILOGE("~FsAtomicFile success."); + } private: unique_ptr entity; diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h index 48f511409..9e1cc0d3c 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h @@ -50,7 +50,11 @@ public: return *this; } - ~FsFile() = default; + // ~FsFile() = default; + ~FsFile() { + HILOGE("~FsFile success."); + } + #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) FsResult GetPath() const; diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h index bbbd51802..5a08ff37d 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h @@ -18,23 +18,14 @@ #include "filemgmt_libfs.h" #include "randomaccessfile_entity.h" +#include "write_options.h" +#include "read_options.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; -struct WriteOptions { - optional length = nullopt; - optional offset = nullopt; - optional encoding = nullopt; -}; - -struct ReadOptions { - optional offset = nullopt; - optional length = nullopt; -}; - class FsRandomAccessFile { public: inline static const string className_ = "RandomAccessFile"; @@ -61,7 +52,9 @@ public: return *this; } - ~FsRandomAccessFile() = default; + ~FsRandomAccessFile() { + HILOGE("~FsRandomAccessFile success."); + } FsResult SetFilePointerSync(const int64_t &fp) const; FsResult WriteSync(const string &buffer, const optional &options = nullopt) const; diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h index 9d5935225..4d3637246 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h @@ -37,6 +37,10 @@ public: FsResult Next(); + ~FsReaderIterator() { + HILOGE("~FsReaderIterator success"); + } + private: unique_ptr entity; explicit FsReaderIterator(unique_ptr entity) : entity(move(entity)) {}; diff --git a/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h b/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h index 802015087..662a08e82 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h +++ b/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h @@ -18,6 +18,7 @@ #include "filemgmt_libfs.h" #include "fs_stat_entity.h" +#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -60,6 +61,9 @@ public: int32_t GetLocation(); #endif + ~FsStat() { + HILOGE("~FsStat success."); + }; private: unique_ptr entity; bool CheckStatMode(mode_t mode); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h index a6b9661e0..bda1097e7 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h @@ -24,23 +24,15 @@ #include "filemgmt_libfs.h" #include "fs_utils.h" +#include "write_options.h" +#include "read_options.h" +#include "filemgmt_libhilog.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; -struct WriteOptions { - optional length = nullopt; - optional offset = nullopt; - optional encoding = nullopt; -}; - -struct ReadOptions { - optional length = nullopt; - optional offset = nullopt; -}; - class FsStream final { public: StreamEntity *GetStreamEntity() const @@ -59,7 +51,9 @@ public: FsResult Flush(); FsResult Seek(const int64_t &offset, const optional &typeOpt = nullopt); - ~FsStream() = default; + ~FsStream() { + HILOGE("~FsStream success."); + } static FsResult Constructor(); private: diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h index 46a1c5340..3bdb2d2d7 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h @@ -18,6 +18,7 @@ #include "filemgmt_libfs.h" #include "fs_watch_entity.h" +#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -38,7 +39,9 @@ public: FsWatcher(FsWatcher &&) noexcept = default; FsWatcher &operator=(FsWatcher &&) noexcept = default; - ~FsWatcher() = default; + ~FsWatcher() { + HILOGE("~FsWatcher success."); + } private: unique_ptr watchEntity; diff --git a/interfaces/kits/js/src/mod_fs/common/obj/read_options.h b/interfaces/kits/js/src/mod_fs/common/obj/read_options.h new file mode 100644 index 000000000..0303bc98a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/common/obj/read_options.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_READ_OPTIONS_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_READ_OPTIONS_H + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +struct ReadOptions { + optional offset = nullopt; + optional length = nullopt; +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_READ_OPTIONS_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/common/obj/write_options.h b/interfaces/kits/js/src/mod_fs/common/obj/write_options.h new file mode 100644 index 000000000..64964ba4b --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/common/obj/write_options.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_WRITE_OPTIONS_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_WRITE_OPTIONS_H + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +struct WriteOptions { + optional length = nullopt; + optional offset = nullopt; + optional encoding = nullopt; +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_OBJ_WRITE_OPTIONS_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/read_core.h b/interfaces/kits/js/src/mod_fs/properties/read_core.h index 5c9965673..69750786c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/read_core.h @@ -19,15 +19,11 @@ #include #include "filemgmt_libfs.h" #include "fs_utils.h" +#include "read_options.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; -struct ReadOptions final { - optional offset = nullopt; - optional length = nullopt; -}; - class ReadCore final { public: static FsResult DoRead( diff --git a/interfaces/kits/js/src/mod_fs/properties/write_core.h b/interfaces/kits/js/src/mod_fs/properties/write_core.h index f50ae27c8..119c354bf 100644 --- a/interfaces/kits/js/src/mod_fs/properties/write_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/write_core.h @@ -19,18 +19,13 @@ #include "filemgmt_libfs.h" #include "filemgmt_libhilog.h" #include "fs_utils.h" +#include "write_options.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; -struct WriteOptions { - optional length = nullopt; - optional offset = nullopt; - optional encoding = nullopt; -}; - class WriteCore final { public: static FsResult DoWrite( -- Gitee From 7b70a589e501321b870bdc88dfc313851d655bcf Mon Sep 17 00:00:00 2001 From: liyuke Date: Thu, 28 Aug 2025 16:26:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?hashstream=E5=8F=8A=E6=84=8F=E8=A7=81?= =?UTF-8?q?=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: Ia571a4604db38cdab7ca234c1ea0d13ccd3d3aa8 --- interfaces/kits/js/BUILD.gn | 2 + .../src/common/ani_helper/ani_signature.cpp | 15 ++-- .../js/src/common/ani_helper/ani_signature.h | 14 ++-- .../js/src/mod_fs/ani/bind_function_class.cpp | 6 +- .../js/src/mod_fs/ani/cleaner/cleaner_ani.cpp | 58 ++++++++-------- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 8 +-- .../class_atomicfile/ani/atomicfile_ani.cpp | 3 +- .../mod_fs/class_atomicfile/fs_atomicfile.h | 3 - .../kits/js/src/mod_fs/class_file/fs_file.h | 6 +- .../fs_randomaccessfile.h | 4 +- .../class_readeriterator/fs_reader_iterator.h | 4 -- .../kits/js/src/mod_fs/class_stat/fs_stat.h | 4 -- .../js/src/mod_fs/class_stream/fs_stream.h | 7 +- .../js/src/mod_fs/class_watcher/fs_watcher.h | 5 +- .../src/mod_hash/ani/bind_function_class.cpp | 18 +++++ .../src/mod_hash/ani/cleaner/cleaner_ani.cpp | 69 +++++++++++++++++++ .../js/src/mod_hash/ani/cleaner/cleaner_ani.h | 35 ++++++++++ .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 32 +++++++++ .../class_hashstream/ani/hashstream_ani.cpp | 13 ++++ interfaces/test/unittest/js/BUILD.gn | 4 +- 20 files changed, 232 insertions(+), 78 deletions(-) create mode 100644 interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.cpp create mode 100644 interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 6655a41a5..a5286db0a 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -864,6 +864,7 @@ ohos_shared_library("ani_file_hash") { include_dirs = [ "src/mod_hash", "src/mod_hash/ani", + "src/mod_hash/ani/cleaner", "src/mod_hash/class_hashstream", "src/mod_hash/class_hashstream/ani", ] @@ -875,6 +876,7 @@ ohos_shared_library("ani_file_hash") { "src/common/file_helper/hash_file.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", + "src/mod_hash/ani/cleaner/cleaner_ani.cpp", "src/mod_hash/ani/hash_ani.cpp", "src/mod_hash/class_hashstream/ani/hashstream_ani.cpp", "src/mod_hash/class_hashstream/hs_hashstream.cpp", 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 abb600994..5990f58f1 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -99,7 +99,8 @@ const string FS::ReaderIteratorInner::ctorSig = Builder::BuildSignatureDescripto const Type FS::ReaderIteratorResultInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ReaderIteratorResultInner"); const string FS::ReaderIteratorResultInner::classDesc = FS::ReaderIteratorResultInner::classType.Descriptor(); -const string FS::ReaderIteratorResultInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::booleanType, BuiltInTypes::stringType }); +const string FS::ReaderIteratorResultInner::ctorSig = + Builder::BuildSignatureDescriptor({ BasicTypes::booleanType, BuiltInTypes::stringType }); // FS::StatInner const Type FS::StatInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.StatInner"); const string FS::StatInner::classDesc = FS::StatInner::classType.Descriptor(); @@ -114,7 +115,6 @@ const string FS::TaskSignal::classDesc = FS::TaskSignal::classType.Descriptor(); const string FS::TaskSignal::ctorDesc = Builder::BuildConstructorName(); const string FS::TaskSignal::ctorSig = Builder::BuildSignatureDescriptor({}); const string FS::TaskSignal::nativeTaskSignal = "nativeTaskSignal"; -const string FS::TaskSignal::bindNativePtrSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); // FS::WatcherInner const Type FS::WatcherInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WatcherInner"); const string FS::WatcherInner::classDesc = FS::WatcherInner::classType.Descriptor(); @@ -138,6 +138,7 @@ const string FS::WriteStream::ctorSig = const Type FS::AtomicFile::classType = Builder::BuildClass("@ohos.file.fs.fileIo.AtomicFile"); const string FS::AtomicFile::classDesc = FS::AtomicFile::classType.Descriptor(); const string FS::AtomicFile::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +const string FS::AtomicFile::bindNativePtr = "bindNativePtr"; const string FS::AtomicFile::bindNativePtrSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); // FS::ReadStreamOptionsInner const Type FS::ReadStreamOptionsInner::classType = Builder::BuildClass("@ohos.file.fs.ReadStreamOptionsInner"); @@ -167,8 +168,12 @@ const string Impl::StatvfsImpl::classDesc = Impl::StatvfsImpl::classType.Descrip const Type HASH::HashStreamImpl::classType = Builder::BuildClass("@ohos.file.hash.HashStreamImpl"); const string HASH::HashStreamImpl::classDesc = HASH::HashStreamImpl::classType.Descriptor(); const string HASH::HashStreamImpl::ctorSig = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); -// CLEANER::CleanerImpl -const Type CLEANER::CleanerImpl::classType = Builder::BuildClass("@ohos.file.fs.Cleaner"); -const string CLEANER::CleanerImpl::classDesc = CLEANER::CleanerImpl::classType.Descriptor(); +const string HASH::HashStreamImpl::bindNativePtrSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::CleanerImpl +const Type FS::CleanerImpl::classType = Builder::BuildClass("@ohos.file.fs.Cleaner"); +const string FS::CleanerImpl::classDesc = FS::CleanerImpl::classType.Descriptor(); +// HASH::CleanerImpl +const Type HASH::CleanerImpl::classType = Builder::BuildClass("@ohos.file.hash.Cleaner"); +const string HASH::CleanerImpl::classDesc = HASH::CleanerImpl::classType.Descriptor(); } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature \ No newline at end of file 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 c4734bd6b..a40f932d4 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -172,7 +172,6 @@ struct TaskSignal : public BaseType { static const string ctorDesc; static const string ctorSig; static const string nativeTaskSignal; - static const string bindNativePtrSig; }; struct WatcherInner : public BaseType { @@ -208,6 +207,7 @@ struct AtomicFile : public BaseType { static const Type classType; static const string classDesc; static const string ctorSig; + static const string bindNativePtr; static const string bindNativePtrSig; }; @@ -221,6 +221,11 @@ struct WriteStreamOptionsInner : public BaseType { static const string classDesc; }; +struct CleanerImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + } // namespace FS namespace Impl { @@ -258,18 +263,15 @@ struct HashStreamImpl : public BaseType { static const Type classType; static const string classDesc; static const string ctorSig; + static const string bindNativePtrSig; }; -} // namespace HASH - -namespace CLEANER { - struct CleanerImpl : public BaseType { static const Type classType; static const string classDesc; }; -} // namespace CLEANER +} // namespace HASH } // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature 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 cfecebcd7..687f2c6af 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 @@ -21,6 +21,7 @@ #include "ani_signature.h" #include "atomicfile_ani.h" #include "bind_function.h" +#include "cleaner_ani.h" #include "close_ani.h" #include "connectdfs_ani.h" #include "copy_ani.h" @@ -34,8 +35,8 @@ #include "fdopen_stream_ani.h" #include "file_ani.h" #include "filemgmt_libhilog.h" -#include "fsync_ani.h" #include "fs_watcher_ani.h" +#include "fsync_ani.h" #include "listfile_ani.h" #include "lseek_ani.h" #include "lstat_ani.h" @@ -61,7 +62,6 @@ #include "watcher_ani.h" #include "write_ani.h" #include "xattr_ani.h" -#include "cleaner_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; @@ -242,7 +242,7 @@ static ani_status BindStaticMethods(ani_env *env) static ani_status BindCleanerMethods(ani_env *env) { - auto classDesc = CLEANER::CleanerImpl::classDesc.c_str(); + auto classDesc = FS::CleanerImpl::classDesc.c_str(); std::array methods = { ani_native_function { "clean", nullptr, reinterpret_cast(CleanerAni::Clean) }, diff --git a/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp index 0a448bd80..cbf9b4231 100644 --- a/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/cleaner/cleaner_ani.cpp @@ -33,6 +33,31 @@ using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; using namespace OHOS::FileManagement::ModuleFileIO::ANI; +static void DoCleanByClassType(ani_env *env, ani_long ptr, ani_ref classTypeObj) +{ + auto [succ, classType] = TypeConverter::ToUTF8String(env, static_cast(classTypeObj)); + if (!succ) { + HILOGE("Clean ParseString failed."); + return; + } + if (classType == "File") { + delete reinterpret_cast(ptr); + } else if (classType == "RandomAccessFile") { + delete reinterpret_cast(ptr); + } else if (classType == "ReaderIterator") { + delete reinterpret_cast(ptr); + } else if (classType == "Stat") { + delete reinterpret_cast(ptr); + } else if (classType == "Stream") { + delete reinterpret_cast(ptr); + } else if (classType == "AtomicFile") { + delete reinterpret_cast(ptr); + } else { + HILOGE("Clean unsupport className: %{public}s", classType.c_str()); + } + return; +} + void CleanerAni::Clean(ani_env *env, ani_object object) { if (env == nullptr) { @@ -46,38 +71,13 @@ void CleanerAni::Clean(ani_env *env, ani_object object) return; } - ani_ref nameObj = nullptr; - if ((status = env->Object_GetFieldByName_Ref(object, "name", &nameObj)) != ANI_OK) { + ani_ref classTypeObj = nullptr; + if ((status = env->Object_GetFieldByName_Ref(object, "classType", &classTypeObj)) != ANI_OK) { HILOGE("Clean Object_GetFieldByName_Ref status: %{public}d", status); return; } - auto [succ, clsName] = TypeConverter::ToUTF8String(env, static_cast(nameObj)); - if (!succ) { - HILOGE("Clean ParseString failed."); - return; - } - if (clsName == "File") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else if (clsName == "RandomAccessFile") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else if (clsName == "ReaderIterator") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else if (clsName == "Stat") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else if (clsName == "Stream") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else if (clsName == "AtomicFile") { - HILOGE("Clean className: %{public}s", clsName.c_str()); - delete reinterpret_cast(ptr); - } else { - HILOGE("Clean unsupport className: %{public}s", clsName.c_str()); - } - return; + + DoCleanByClassType(env, ptr, classTypeObj); } } // namespace ANI 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 039649ad5..4b01ef430 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 @@ -14,7 +14,6 @@ */ import { BusinessError, AsyncCallback } from '@ohos.base'; import stream from '@ohos.util.stream'; -import hilog from '@ohos.hilog' const UNKNOWN_ERR: int = 13900042 const UNKNOWN_MSG: string = "Unknown error" @@ -28,10 +27,10 @@ function createBusinessError(code: int, msg: string): BusinessError { class Cleaner { private ptr: long = 0 - private name: string = ""; - constructor(ptr: long, name: string) { + private classType: string = ""; + constructor(ptr: long, classType: string) { this.ptr = ptr - this.name = name + this.classType = classType } native clean(): void } @@ -41,7 +40,6 @@ function CleanerCallback(cleaner: Cleaner): void { } let destroyRegister = new FinalizationRegistry(CleanerCallback) -let unregisterToken = new object() namespace fileIo { export namespace OpenMode { diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index b979574bc..1d18c6e05 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -40,8 +40,9 @@ const std::string TEMP_FILE_SUFFIX = "_XXXXXX"; void CallBindNativePtr(ani_env *env, ani_object obj, FsAtomicFile *fsAtomicFile) { auto bindNativePtrSig = FS::AtomicFile::bindNativePtrSig.c_str(); + auto ptr = FS::AtomicFile::bindNativePtr.c_str(); ani_long longValue = reinterpret_cast(fsAtomicFile); - ani_status ret = env->Object_CallMethodByName_Void(obj, "bindNativePtr", bindNativePtrSig, longValue); + ani_status ret = env->Object_CallMethodByName_Void(obj, ptr, bindNativePtrSig, longValue); if (ret != ANI_OK) { HILOGE("Object_CallMethodByName_Void failed. ret = %{public}d", static_cast(ret)); return; diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h index 4571e62fa..80996b1c7 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/fs_atomicfile.h @@ -46,9 +46,6 @@ public: FsResult FailWrite(); FsResult Delete(); static void FinalizeCallback(void *finalizeData, [[maybe_unused]] void *finalizeHint); - ~FsAtomicFile() { - HILOGE("~FsAtomicFile success."); - } private: unique_ptr entity; diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h index 9e1cc0d3c..48f511409 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h @@ -50,11 +50,7 @@ public: return *this; } - // ~FsFile() = default; - ~FsFile() { - HILOGE("~FsFile success."); - } - + ~FsFile() = default; #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) FsResult GetPath() const; diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h index 5a08ff37d..72b87cbce 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/fs_randomaccessfile.h @@ -52,9 +52,7 @@ public: return *this; } - ~FsRandomAccessFile() { - HILOGE("~FsRandomAccessFile success."); - } + ~FsRandomAccessFile() = default; FsResult SetFilePointerSync(const int64_t &fp) const; FsResult WriteSync(const string &buffer, const optional &options = nullopt) const; diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h b/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h index 4d3637246..9d5935225 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/fs_reader_iterator.h @@ -37,10 +37,6 @@ public: FsResult Next(); - ~FsReaderIterator() { - HILOGE("~FsReaderIterator success"); - } - private: unique_ptr entity; explicit FsReaderIterator(unique_ptr entity) : entity(move(entity)) {}; diff --git a/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h b/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h index 662a08e82..802015087 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h +++ b/interfaces/kits/js/src/mod_fs/class_stat/fs_stat.h @@ -18,7 +18,6 @@ #include "filemgmt_libfs.h" #include "fs_stat_entity.h" -#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; @@ -61,9 +60,6 @@ public: int32_t GetLocation(); #endif - ~FsStat() { - HILOGE("~FsStat success."); - }; private: unique_ptr entity; bool CheckStatMode(mode_t mode); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h index bda1097e7..66cf6b9c6 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.h @@ -24,9 +24,8 @@ #include "filemgmt_libfs.h" #include "fs_utils.h" -#include "write_options.h" #include "read_options.h" -#include "filemgmt_libhilog.h" +#include "write_options.h" namespace OHOS { namespace FileManagement { @@ -51,9 +50,7 @@ public: FsResult Flush(); FsResult Seek(const int64_t &offset, const optional &typeOpt = nullopt); - ~FsStream() { - HILOGE("~FsStream success."); - } + ~FsStream() = default; static FsResult Constructor(); private: diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h index 3bdb2d2d7..46a1c5340 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/fs_watcher.h @@ -18,7 +18,6 @@ #include "filemgmt_libfs.h" #include "fs_watch_entity.h" -#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -39,9 +38,7 @@ public: FsWatcher(FsWatcher &&) noexcept = default; FsWatcher &operator=(FsWatcher &&) noexcept = default; - ~FsWatcher() { - HILOGE("~FsWatcher success."); - } + ~FsWatcher() = default; private: unique_ptr watchEntity; diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 9f8f7b08f..e87151d19 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -16,6 +16,7 @@ #include #include "ani_signature.h" #include "bind_function.h" +#include "cleaner_ani.h" #include "hash_ani.h" #include "hashstream_ani.h" @@ -44,6 +45,17 @@ static ani_status BindHashStreamMethods(ani_env *env) return BindClass(env, classDesc, methods); } +static ani_status BindCleanerMethods(ani_env *env) +{ + auto classDesc = HASH::CleanerImpl::classDesc.c_str(); + + std::array methods = { + ani_native_function { "clean", nullptr, reinterpret_cast(CleanerAni::Clean) }, + }; + + return BindClass(env, classDesc, methods); +} + ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { if (vm == nullptr) { @@ -75,6 +87,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; + status = BindCleanerMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for cleaner!"); + return status; + }; + *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.cpp b/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.cpp new file mode 100644 index 000000000..56fef88ac --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cleaner_ani.h" + +#include "filemgmt_libhilog.h" +#include "hs_hashstream.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI; + +static void DoCleanByClassType(ani_env *env, ani_long ptr, ani_ref classTypeObj) +{ + auto [succ, classType] = TypeConverter::ToUTF8String(env, static_cast(classTypeObj)); + if (!succ) { + HILOGE("Clean ParseString failed."); + return; + } + if (classType == "HashStream") { + delete reinterpret_cast(ptr); + } else { + HILOGE("Clean unsupport className: %{public}s", classType.c_str()); + } + return; +} +void CleanerAni::Clean(ani_env *env, ani_object object) +{ + if (env == nullptr) { + HILOGE("env is nullptr"); + return; + } + ani_long ptr; + ani_status status = env->Object_GetFieldByName_Long(object, "ptr", &ptr); + if (status != ANI_OK) { + HILOGE("Clean Object_GetFieldByName_Long status: %{public}d", status); + return; + } + + ani_ref classTypeObj = nullptr; + if ((status = env->Object_GetFieldByName_Ref(object, "classType", &classTypeObj)) != ANI_OK) { + HILOGE("Clean Object_GetFieldByName_Ref status: %{public}d", status); + return; + } + + DoCleanByClassType(env, ptr, classTypeObj); +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.h b/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.h new file mode 100644 index 000000000..a8ca237a2 --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/ani/cleaner/cleaner_ani.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_HASH_ANI_CLEANER_CLEANER_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_HASH_ANI_CLEANER_CLEANER_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class CleanerAni final { +public: + static void Clean(ani_env *env, ani_object object); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_HASH_ANI_CLEANER_CLEANER_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index ce7030387..8dbf484cb 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -16,6 +16,22 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; import stream from '@ohos.util.stream'; +class Cleaner { + private ptr: long = 0 + private classType: string = ""; + constructor(ptr: long, classType: string) { + this.ptr = ptr + this.classType = classType + } + native clean(): void +} + +function CleanerCallback(cleaner: Cleaner): void { + cleaner.clean() +} + +let destroyRegister = new FinalizationRegistry(CleanerCallback) + export default namespace hash { export function hash(path: string, algorithm: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { @@ -89,6 +105,22 @@ export class HashStreamImpl extends stream.Transform { loadLibrary("ani_file_hash"); } private nativePtr: long = 0; + private cleaner: Cleaner | null = null; + + registerCleaner(ptr: long): void { + this.cleaner = new Cleaner(ptr, "HashStream"); + destroyRegister.register(this, this.cleaner!, this); + } + unregisterCleaner(): void { + destroyRegister.unregister(this); + } + bindNativePtr(ptr: long): void { + if (this.nativePtr === 0) { + this.nativePtr = ptr; + } + this.registerCleaner(this.nativePtr) + + } native constructor(alg: string); native digest(): string; diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp index 440f5c6a5..484eb1232 100644 --- a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp @@ -93,6 +93,17 @@ ani_string HashStreamAni::Digest(ani_env *env, [[maybe_unused]] ani_object objec return result; } +void CallBindNativePtr(ani_env *env, ani_object obj, HsHashStream *hsHashStream) +{ + auto bindNativePtrSig = HASH::HashStreamImpl::bindNativePtrSig.c_str(); + ani_long longValue = reinterpret_cast(hsHashStream); + ani_status ret = env->Object_CallMethodByName_Void(obj, "bindNativePtr", bindNativePtrSig, longValue); + if (ret != ANI_OK) { + HILOGE("Object_CallMethodByName_Void failed. ret = %{public}d", static_cast(ret)); + return; + } +} + void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) { auto [succ, algorithm] = TypeConverter::ToUTF8String(env, alg); @@ -115,6 +126,8 @@ void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) ErrorHandler::Throw(env, EIO); return; } + + CallBindNativePtr(env, obj, ret.GetData().value()); } } // namespace ANI diff --git a/interfaces/test/unittest/js/BUILD.gn b/interfaces/test/unittest/js/BUILD.gn index ca5d5eabe..9b7da2ad9 100644 --- a/interfaces/test/unittest/js/BUILD.gn +++ b/interfaces/test/unittest/js/BUILD.gn @@ -135,6 +135,7 @@ ohos_unittest("ani_file_fs_mock_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", + "${file_api_path}/interfaces/kits/js/src/mod_fs/common/obj", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", "${file_api_path}/interfaces/test/unittest/js/mod_fs/class_stream/mock", @@ -229,7 +230,6 @@ ohos_unittest("ani_file_fs_test") { "${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/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", @@ -237,6 +237,8 @@ ohos_unittest("ani_file_fs_test") { "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stat", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_stream", "${file_api_path}/interfaces/kits/js/src/mod_fs/class_tasksignal", + "${file_api_path}/interfaces/kits/js/src/mod_fs/class_watcher", + "${file_api_path}/interfaces/kits/js/src/mod_fs/common/obj", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties", "${file_api_path}/interfaces/kits/js/src/mod_fs/properties/copy_listener", ] -- Gitee