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 065495c9ade9c4c775ce494fa218d0c382077459..7a9557f5b34718e3ea77a0001b64cfc880018796 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 @@ -43,7 +43,7 @@ static ani_status BindFileMethods(ani_env *env) std::array methods = { ani_native_function { "getParent", nullptr, reinterpret_cast(FileAni::GetParent) }, - ani_native_function { "lock", nullptr, reinterpret_cast(FileAni::Lock) }, + ani_native_function { "lockSync", nullptr, reinterpret_cast(FileAni::LockSync) }, ani_native_function { "tryLock", nullptr, reinterpret_cast(FileAni::TryLock) }, ani_native_function { "unlock", nullptr, reinterpret_cast(FileAni::UnLock) }, }; 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 67eb95add30a0cd803f0542ae46c90f870189651..64a1c206771a066ddb8268e66f54c2df59983503 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 @@ -539,7 +539,9 @@ export interface File { name: String; getParent(): String; - lock(exclusive?: boolean): void; + lock(exclusive?: boolean): Promise; + lock(callback: AsyncCallback): void; + lock(exclusive: boolean, callback: AsyncCallback): void; tryLock(exclusive?: boolean): void; unlock(): void; } @@ -558,7 +560,58 @@ class FileInner implements File { } native getParent(): String; - native lock(exclusive?: boolean): void; + native lockSync(exclusive?: boolean): void; + + lock(exclusive?: boolean): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + if (exclusive === undefined) { + let promise = taskpool.execute((): undefined => { + return this.lockSync(); + }); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + } else { + let promise = taskpool.execute((exclusive: boolean): undefined => { + return this.lockSync(exclusive); + }, exclusive); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + } + }); + } + + lock(callback: AsyncCallback): void { + let promise = taskpool.execute((): undefined => { + return this.lockSync(); + }); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); + } + + lock(exclusive: boolean, callback: AsyncCallback): void { + let promise = taskpool.execute((exclusive: boolean): undefined => { + return this.lockSync(exclusive); + }, exclusive); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); + } + native tryLock(exclusive?: boolean): void; native unlock(): void; diff --git a/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.cpp b/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.cpp index 5dc1136590656cda875c8d1ee80b73597af47b27..43a6e5f42af39b7eb0fcb6915e71e47057f09c48 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.cpp @@ -65,7 +65,7 @@ ani_string FileAni::GetParent(ani_env *env, [[maybe_unused]] ani_object object) return parent; } -void FileAni::Lock(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive) +void FileAni::LockSync(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive) { ani_boolean isUndefined; bool exc = false; diff --git a/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.h b/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.h index cc7780f857e335746b46a6b49e8721caf9fefb51..1ab5eb61ebbfd577ffba77ef23bf2839e1d7800e 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.h +++ b/interfaces/kits/js/src/mod_fs/class_file/ani/file_ani.h @@ -26,7 +26,7 @@ namespace ANI { class FileAni final { public: static ani_string GetParent(ani_env *env, [[maybe_unused]] ani_object object); - static void Lock(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive); + static void LockSync(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive); static void TryLock(ani_env *env, [[maybe_unused]] ani_object object, ani_object exclusive); static void UnLock(ani_env *env, [[maybe_unused]] ani_object object); };