From 6e4a4090e6d50ff577628c10381e05bf5e00c1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 12 Mar 2025 20:38:03 +0800 Subject: [PATCH 01/76] =?UTF-8?q?=E5=A2=9E=E5=8A=A0close=E5=92=8Cstat?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E6=AD=A5=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=9A=E4=BD=99=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98=20Signed-off-by:?= =?UTF-8?q?=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I165b115af25c5d4253c042a1d7b7456b103031e0 --- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 143 ++++++++++++------ .../src/mod_fs/properties/ani/close_ani.cpp | 15 +- 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index dde54350..55dfa7c7 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -188,27 +188,27 @@ class fileIo { static access(path: string, mode?: AccessModeType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { if (mode === undefined) { - let promise = taskpool.execute(fileIo.accessSync1, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); } else { - let promise = taskpool.execute(fileIo.accessSync2, path, mode); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync2, path, mode); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); } @@ -217,44 +217,76 @@ class fileIo { static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); }) } static access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.accessSync1, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { let err = new BusinessError(); - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); } else { err.code = 0; let result = ret as boolean; - callback(err, result); // 正常结果 + callback(err, result); } }); } static native closeSync(file: FdOrFile): int; - static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; + static close(file: FdOrFile): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.closeSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as int; + resolve(r); + } + }); + }); + } + + static close(file: FdOrFile, callback: AsyncCallback): void { + let p1 = taskpool.execute(fileIo.closeSync, file); + p1.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as int; + callback(err, r); + } + }); + } + + static native copyFileSync(src: PathOrFd, dest: PathOrFdx, mode?: int): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; static listFileSync1(path: string): string[] { return fileIo.listFileSync(path); } + static listFileSync2(path: string, options: ListFileOptions): string[] { return fileIo.listFileSync(path, options); } @@ -328,15 +360,15 @@ class fileIo { static mkdir(path: string): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); // 这里调用同步方法 + let promise = taskpool.execute(mkdirSyncWrapper, path); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -348,11 +380,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -363,17 +393,15 @@ class fileIo { static mkdir(path: string, recursion: boolean): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { const mkdirSyncWrapper = (path: string, recursion: boolean) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); // 这里调用同步方法 + let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 - console.println("-------- err code = -1 -------------"); + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - console.println("-------- err code = 0 -------------"); - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -385,11 +413,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -404,6 +430,7 @@ class fileIo { static openSync1(path: String, mode: int): File { return fileIo.openSync(path, mode); } + static openSync2(path: String): File { return fileIo.openSync(path); } @@ -445,11 +472,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as File; callback(err, r); @@ -462,11 +487,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as File; callback(err, r); @@ -479,6 +502,7 @@ class fileIo { static readSync1(fd: int, buffer: ArrayBuffer): long { return fileIo.readSync(fd, buffer); } + static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { return fileIo.readSync(fd, buffer, options); } @@ -520,11 +544,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as long; callback(err, r); @@ -537,11 +559,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as long; callback(err, r); @@ -554,6 +574,7 @@ class fileIo { static readTextSync1(filePath: string): string { return fileIo.readTextSync(filePath); } + static readTextSync2(filePath: string, options: ReadTextOptions): string { return fileIo.readTextSync(filePath, options); } @@ -562,13 +583,13 @@ class fileIo { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync1, filePath); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let r = ret as string; - resolve(r); // 正常结果 + resolve(r); } }); }); @@ -578,13 +599,13 @@ class fileIo { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let r = ret as string; - resolve(r); // 正常结果 + resolve(r); } }); }); @@ -595,11 +616,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as string; callback(err, r); @@ -612,11 +631,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as string; callback(err, r); @@ -628,21 +645,52 @@ class fileIo { static native statSync(file: PathOrFd): Stat; + static stat(file: PathOrFd): Promise { + return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.statSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as Stat; + resolve(r); + } + }); + }); + } + + static stat(file: PathOrFd, callback: AsyncCallback): void { + let p = taskpool.execute(fileIo.statSync, file); + p.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as Stat; + callback(err, r); + } + }); + } + static native truncateSync(file: PathOrFd, len?: long): void; static native unlinkSync(path: string): int; static unlink(path: string): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.unlinkSync, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.unlinkSync, path); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -653,11 +701,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -670,6 +716,7 @@ class fileIo { static writeSync1(fd: int, buffer: BufferType, options: WriteOptions): long { return fileIo.writeSync(fd, buffer, options); } + static writeSync2(fd: int, buffer: BufferType): long { return fileIo.writeSync(fd, buffer); } @@ -711,11 +758,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as long; callback(err, r); @@ -728,11 +773,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as long; callback(err, r); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index e4d1fe4e..313a5c14 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -29,11 +29,11 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; -tuple AnalyzerFdUnion(ani_env *env, ani_object obj) +tuple ParseFd(ani_env *env, ani_object obj) { - int result = -1; + int32_t result = -1; ani_class IntClass; - env->FindClass("Lstd/core/Double;", &IntClass); + env->FindClass("Lstd/core/Int;", &IntClass); ani_boolean isInt; env->Object_InstanceOf(obj, IntClass, &isInt); if (isInt) { @@ -42,7 +42,7 @@ tuple AnalyzerFdUnion(ani_env *env, ani_object obj) HILOGE("Get fd value failed"); return { false, result }; } - result = static_cast(fd); + result = static_cast(fd); return { true, result }; } @@ -56,17 +56,18 @@ tuple AnalyzerFdUnion(ani_env *env, ani_object obj) HILOGE("Get fd in class file failed"); return { false, result }; } - result = static_cast(fd); + result = static_cast(fd); return { true, result }; } + HILOGE("Invalid fd type"); return { false, result }; } ani_int CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj) { - auto [succ, fd] = AnalyzerFdUnion(env, obj); + auto [succ, fd] = ParseFd(env, obj); if (!succ) { - HILOGE("Invalid arguments"); + HILOGE("Parse fd argument failed"); return -1; } -- Gitee From 88c7ed903bc2ebb0384c69eb1ae0e749cf266050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 12 Mar 2025 20:38:03 +0800 Subject: [PATCH 02/76] =?UTF-8?q?=E5=A2=9E=E5=8A=A0close=E5=92=8Cstat?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E6=AD=A5=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=9A=E4=BD=99=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98=20Signed-off-by:?= =?UTF-8?q?=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I165b115af25c5d4253c042a1d7b7456b103031e0 --- interfaces/kits/js/BUILD.gn | 3 - .../src/common/ani_helper/bind_function.cpp | 78 ------------------- .../js/src/common/ani_helper/bind_function.h | 34 ++++---- .../js/src/mod_fs/ani/bind_function_class.cpp | 22 +++--- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 2 +- .../src/mod_hash/ani/bind_function_class.cpp | 33 +++++++- .../ani/bind_function_class.cpp | 36 +++++++-- 7 files changed, 95 insertions(+), 113 deletions(-) delete mode 100644 interfaces/kits/js/src/common/ani_helper/bind_function.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d4f31e1b..10829853 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -669,7 +669,6 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/copy_listener", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", @@ -779,7 +778,6 @@ ohos_shared_library("ani_hash_class") { "src/mod_hash/ani", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/common/file_helper/hash_file.cpp", @@ -849,7 +847,6 @@ ohos_shared_library("ani_securitylabel_class") { "src/mod_securitylabel", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", diff --git a/interfaces/kits/js/src/common/ani_helper/bind_function.cpp b/interfaces/kits/js/src/common/ani_helper/bind_function.cpp deleted file mode 100644 index 1f1b2904..00000000 --- a/interfaces/kits/js/src/common/ani_helper/bind_function.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 "bind_function.h" -#include "filemgmt_libhilog.h" - -namespace OHOS { -namespace FileManagement { -namespace ModuleFileIO { -namespace ANI { -ANI_EXPORT ani_status BindClass(ani_vm *vm, const char *className, const std::vector &methods) -{ - if (vm == nullptr) { - HILOGE("ani_vm is null!"); - return ANI_ERROR; - } - - ani_env *env; - if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - HILOGE("Unsupported ANI_VERSION_1!"); - return ANI_OUT_OF_REF; - } - - ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Not found '%{private}s'", className); - return ANI_INVALID_ARGS; - } - - if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { - HILOGE("Cannot bind native methods to '%{private}s'", className); - return ANI_INVALID_TYPE; - }; - return ANI_OK; -} - -ANI_EXPORT ani_status BindNamespace( - ani_vm *vm, const char *namespaceStr, const std::vector &functions) -{ - if (vm == nullptr) { - HILOGE("ani_vm is null!"); - return ANI_ERROR; - } - - ani_env *env; - if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - HILOGE("Unsupported ANI_VERSION_1 Fail!!!"); - return ANI_OUT_OF_REF; - } - - ani_namespace ns; - if (ANI_OK != env->FindNamespace(namespaceStr, &ns)) { - HILOGE("Not found '%{private}s'", namespaceStr); - return ANI_INVALID_ARGS; - } - - if (ANI_OK != env->Namespace_BindNativeFunctions(ns, functions.data(), functions.size())) { - HILOGE("Cannot bind native methods to '%{private}s'", namespaceStr); - return ANI_INVALID_TYPE; - }; - return ANI_OK; -} -} // namespace ANI -} // namespace ModuleFileIO -} // namespace FileManagement -} // namespace OHOS diff --git a/interfaces/kits/js/src/common/ani_helper/bind_function.h b/interfaces/kits/js/src/common/ani_helper/bind_function.h index 7d41c655..3c951b25 100644 --- a/interfaces/kits/js/src/common/ani_helper/bind_function.h +++ b/interfaces/kits/js/src/common/ani_helper/bind_function.h @@ -25,27 +25,28 @@ namespace FileManagement { namespace ModuleFileIO { namespace ANI { -ANI_EXPORT ani_status BindClass(ani_vm *vm, const char *className, const std::vector &methods); -ANI_EXPORT ani_status BindNamespace( - ani_vm *vm, const char *namespaceStr, const std::vector &functions); - template ANI_EXPORT ani_status BindClass(ani_env *env, const char *className, const std::array &methods) { if (env == nullptr) { - HILOGE("ani_env is null!"); - return ANI_ERROR; + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; } - + + if (className == nullptr) { + HILOGE("Invalid parameter className"); + return ANI_INVALID_ARGS; + } + ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class '%{private}s'", className); - return ANI_INVALID_ARGS; + return ANI_NOT_FOUND; } if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { HILOGE("Cannot bind native methods to '%{private}s'", className); - return ANI_INVALID_TYPE; + return ANI_ERROR; }; return ANI_OK; } @@ -55,19 +56,24 @@ ANI_EXPORT ani_status BindNamespace( ani_env *env, const char *namespaceStr, const std::array &methods) { if (env == nullptr) { - HILOGE("ani_env is null!"); - return ANI_ERROR; + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; + } + + if (namespaceStr == nullptr) { + HILOGE("Invalid parameter namespaceStr"); + return ANI_INVALID_ARGS; } ani_namespace ns; if (ANI_OK != env->FindNamespace(namespaceStr, &ns)) { - HILOGE("Not found '%{private}s'", namespaceStr); - return ANI_INVALID_ARGS; + HILOGE("Cannot find namespace '%{private}s'", namespaceStr); + return ANI_NOT_FOUND; } if (ANI_OK != env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size())) { HILOGE("Cannot bind native methods to '%{private}s'", namespaceStr); - return ANI_INVALID_TYPE; + return ANI_ERROR; }; return ANI_OK; } 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 779bd453..b0354e5c 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 @@ -94,12 +94,21 @@ static ani_status BindStaticMethods(ani_env *env) ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + ani_env *env; - ani_status status = ANI_ERROR; - status = vm->GetEnv(ANI_VERSION_1, &env); + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); if (status != ANI_OK) { - HILOGE("Unsupported ANI_VERSION_1"); - return status; + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; } status = BindStaticMethods(env); @@ -120,11 +129,6 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; - if (result == nullptr) { - HILOGE("result is null!"); - return ANI_ERROR; - } - *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index 55dfa7c7..c4e3d3ae 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -279,7 +279,7 @@ class fileIo { }); } - static native copyFileSync(src: PathOrFd, dest: PathOrFdx, mode?: int): void; + static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; 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 8b51760d..e0c81d4e 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 @@ -19,14 +19,41 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +static ani_status BindStaticMethods(ani_env *env) { static const char *className = "Lfile_hash_class/hash;"; - std::vector functions = { + std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for hash!"); + return status; + }; *result = ANI_VERSION_1; - return BindClass(vm, className, functions); + return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 19e735c5..157773f9 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -19,15 +19,41 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +static ani_status BindStaticMethods(ani_env *env) { - *result = ANI_VERSION_1; - static const char *className = "Lfile_securitylabel_class/securitylabel;"; - std::vector functions = { + std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:I", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for securitylabel!"); + return status; + }; - return BindClass(vm, className, functions); + *result = ANI_VERSION_1; + return ANI_OK; } -- Gitee From 4d02099060d56b5f48b27333d5e451c27c8883dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 13 Mar 2025 21:08:04 +0800 Subject: [PATCH 03/76] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD=20Signed-off-by:=20?= =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic1bae335556eca1771b53e4e9e857ee28c1c1b9f --- interfaces/kits/js/BUILD.gn | 1 + .../js/src/common/ani_helper/error_handler.h | 100 +++++++++ .../src/common/ani_helper/type_converter.cpp | 31 +++ .../kits/js/src/mod_fs/ani/error_handler.cpp | 26 +++ .../kits/js/src/mod_fs/ani/file_fs_class.ets | 193 +++++++++--------- .../js/src/mod_fs/properties/ani/open_ani.cpp | 37 ++-- 6 files changed, 281 insertions(+), 107 deletions(-) create mode 100644 interfaces/kits/js/src/common/ani_helper/error_handler.h create mode 100644 interfaces/kits/js/src/mod_fs/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 10829853..c03bb58f 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -672,6 +672,7 @@ ohos_shared_library("ani_fs_class") { "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/error_handler.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", "src/mod_fs/class_file/fs_file.cpp", "src/mod_fs/class_stat/ani/stat_ani.cpp", diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h new file mode 100644 index 00000000..a2b86173 --- /dev/null +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -0,0 +1,100 @@ +/* + * 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 FILEMANAGEMENT_ANI_ERROR_HANDLER_H +#define FILEMANAGEMENT_ANI_ERROR_HANDLER_H + +#include +#include +#include +#include "filemgmt_libhilog.h" +#include "fs_error.h" +#include "type_converter.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI { + +class ErrorHandler { +public: + static ani_status Throw(ani_env *env, int32_t code, const std::string &errMsg); + + static ani_status Throw(ani_env *env, int32_t code) + { + if (env == nullptr) { + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; + } + FsError err(code); + return Throw(env, std::move(err)); + } + + static ani_status Throw(ani_env *env, const FsError &err) + { + if (env == nullptr) { + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; + } + auto code = err.GetErrNo(); + const auto &errMsg = err.GetErrMsg(); + return Throw(env, code, errMsg); + } + +private: + static ani_status Throw(ani_env *env, const char *className, int32_t code, const std::string &errMsg) + { + if (env == nullptr) { + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; + } + + if (className == nullptr) { + HILOGE("Invalid parameter className"); + return ANI_INVALID_ARGS; + } + + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class '%{private}s'", className); + return ANI_NOT_FOUND; + } + + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { + HILOGE("Cannot find constructor for class '%{private}s'", className); + return ANI_NOT_FOUND; + } + + auto [succ, msg] = TypeConverter::ToAniString(env, errMsg); + if (!succ) { + HILOGE("Cannot convert errMsg to ani string"); + return ANI_ERROR; + } + + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, static_cast(code), msg)) { + HILOGE("Cannot create ani error object"); + return ANI_ERROR; + } + auto status = env->ThrowError(static_cast(obj)); + if (status != ANI_OK) { + HILOGE("Throw ani error object failed!"); + return status; + } + return ANI_OK; + } +}; + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI + +#endif // FILEMANAGEMENT_ANI_ERROR_HANDLER_H \ No newline at end of file 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 2f23db6e..e0948fa1 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -27,6 +27,9 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { std::tuple TypeConverter::ToUTF8String(ani_env *env, const ani_string &path) { + if (env == nullptr) { + return { false, EMPTY_STRING }; + } ani_size sz {}; std::string result; auto status = env->String_GetUTF8Size(path, &sz); @@ -44,6 +47,9 @@ std::tuple TypeConverter::ToUTF8String(ani_env *env, const an std::tuple> TypeConverter::ToOptionalInt32(ani_env *env, const ani_object &intOp) { + if (env == nullptr) { + return { false, {} }; + } ani_boolean isUndefined; env->Reference_IsUndefined(intOp, &isUndefined); if (isUndefined) { @@ -60,6 +66,10 @@ std::tuple> TypeConverter::ToOptionalInt32(ani_env std::tuple> TypeConverter::ToOptionalInt64(ani_env *env, const ani_object &longOp) { + if (env == nullptr) { + return { false, {} }; + } + ani_boolean isUndefined; env->Reference_IsUndefined(longOp, &isUndefined); if (isUndefined) { @@ -76,6 +86,10 @@ std::tuple> TypeConverter::ToOptionalInt64(ani_env std::tuple TypeConverter::ToAniString(ani_env *env, std::string str) { + if (env == nullptr) { + return { false, {} }; + } + ani_string result; if (ANI_OK != env->String_NewUTF8(str.c_str(), str.size(), &result)) { return { false, {} }; @@ -85,6 +99,10 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin std::tuple TypeConverter::ToAniString(ani_env *env, const char *str) { + if (env == nullptr) { + return { false, {} }; + } + size_t length = std::strlen(str); ani_string result; if (ANI_OK != env->String_NewUTF8(str, length, &result)) { @@ -111,6 +129,11 @@ std::tuple> TypeConverter::EnumToInt32(ani_env *env std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_object &pathOrFd) { + if (env == nullptr) { + HILOGE("Invalid parameter env"); + return { false, FileInfo { false, {}, {} } }; + } + ani_class stringClass; env->FindClass("Lstd/core/String;", &stringClass); @@ -155,6 +178,10 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj std::tuple TypeConverter::ToArrayBuffer(ani_env *env, ani_arraybuffer &buffer) { + if (env == nullptr) { + return { false, ArrayBuffer { nullptr, 0 } }; + } + void *buf = nullptr; ani_size length = 0; @@ -167,6 +194,10 @@ std::tuple TypeConverter::ToArrayBuffer(ani_env *env, ani_arr std::tuple TypeConverter::ToAniStringList( ani_env *env, const std::string strList[], const uint32_t length) { + if (env == nullptr) { + return { false, nullptr }; + } + ani_array_ref result = nullptr; ani_class itemCls = nullptr; if (env->FindClass("Lstd/core/String;", &itemCls) != ANI_OK) { diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp new file mode 100644 index 00000000..34fc9249 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp @@ -0,0 +1,26 @@ +/* + * 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 "error_handler.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI { + +ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) +{ + const char *className = "Lfile_fs_class/BusinessError;"; + return Throw(env, className, code, errMsg); +} + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index c4e3d3ae..c7570eba 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -16,8 +16,18 @@ export default fileIo; export class BusinessError { - code: number = 0; + name: string; + message: string; + code: number; data?: T; + constructor(code: number, msg: string, data?: T) { + this.name = 'BusinessError'; + this.code = code; + this.message = msg; + if (data !== undefined) { + this.data = data; + } + } } export type AsyncCallback = (err: BusinessError, data?: T) => void; @@ -191,8 +201,8 @@ class fileIo { let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let result = ret as boolean; @@ -203,8 +213,8 @@ class fileIo { let promise = taskpool.execute(fileIo.accessSync2, path, mode); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let result = ret as boolean; @@ -220,8 +230,8 @@ class fileIo { let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let result = ret as boolean; @@ -234,7 +244,8 @@ class fileIo { static access(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { - let err = new BusinessError(); + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -253,8 +264,8 @@ class fileIo { let promise = taskpool.execute(fileIo.closeSync, file); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as int; @@ -265,9 +276,10 @@ class fileIo { } static close(file: FdOrFile, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.closeSync, file); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.closeSync, file); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) @@ -296,8 +308,8 @@ class fileIo { let promise = taskpool.execute(fileIo.listFileSync1, path); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as string[]; @@ -312,8 +324,8 @@ class fileIo { let promise = taskpool.execute(fileIo.listFileSync2, path, options); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as string[]; @@ -324,9 +336,9 @@ class fileIo { } static listFile(path: string, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.listFileSync1, path); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.listFileSync1, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -339,9 +351,9 @@ class fileIo { } static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.listFileSync2, path, options); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.listFileSync2, path, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -364,8 +376,8 @@ class fileIo { promise.then((ret: NullishType) => { let result = ret as int if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { resolve(result); @@ -376,9 +388,9 @@ class fileIo { static mkdir(path: string, callback: AsyncCallback): void { const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let p1 = taskpool.execute(mkdirSyncWrapper, path); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(mkdirSyncWrapper, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) @@ -397,8 +409,8 @@ class fileIo { promise.then((ret: NullishType) => { let result = ret as int if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { resolve(result); @@ -409,9 +421,9 @@ class fileIo { static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { const mkdirSyncWrapper = (path: string, recursion: boolean,) => fileIo.mkdirSync(path, recursion); - let p1 = taskpool.execute(mkdirSyncWrapper, path, recursion); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) @@ -439,14 +451,10 @@ class fileIo { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync1, path, mode); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as File; - resolve(r); - } + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -456,7 +464,7 @@ class fileIo { let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); + let err = new BusinessError(-1, ""); err.code = -1; reject(err); } else { @@ -468,24 +476,21 @@ class fileIo { } static open(path: String, mode: int, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.openSync1, path, mode); - p1.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as File; - callback(err, r); - } + let promise = taskpool.execute(fileIo.openSync1, path, mode); + promise.then((ret: NullishType) => { + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); }); } static open(path: String, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.openSync2, path); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.openSync2, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -512,8 +517,8 @@ class fileIo { let promise = taskpool.execute(fileIo.readSync1, fd, buffer); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as long; @@ -528,8 +533,8 @@ class fileIo { let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as long; @@ -540,9 +545,9 @@ class fileIo { } static read(fd: int, buffer: ArrayBuffer, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.readSync1, fd, buffer); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -555,9 +560,9 @@ class fileIo { } static read(fd: int, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.readSync2, fd, buffer, options); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -584,8 +589,8 @@ class fileIo { let promise = taskpool.execute(fileIo.readTextSync1, filePath); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as string; @@ -600,8 +605,8 @@ class fileIo { let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as string; @@ -612,9 +617,9 @@ class fileIo { } static readText(filePath: string, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.readTextSync1, filePath); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.readTextSync1, filePath); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -627,9 +632,9 @@ class fileIo { } static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.readTextSync2, filePath, options); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -650,8 +655,8 @@ class fileIo { let promise = taskpool.execute(fileIo.statSync, file); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { let r = ret as Stat; @@ -664,7 +669,7 @@ class fileIo { static stat(file: PathOrFd, callback: AsyncCallback): void { let p = taskpool.execute(fileIo.statSync, file); p.then((ret: NullishType) => { - let err = new BusinessError(); + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); @@ -686,8 +691,8 @@ class fileIo { promise.then((ret: NullishType) => { let result = ret as int if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { resolve(result); @@ -697,9 +702,9 @@ class fileIo { } static unlink(path: string, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.unlinkSync, path); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.unlinkSync, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) @@ -727,8 +732,8 @@ class fileIo { promise.then((ret: NullishType) => { let result = ret as long if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { resolve(result); @@ -743,8 +748,8 @@ class fileIo { promise.then((ret: NullishType) => { let result = ret as long if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; + let err = new BusinessError(-1, ""); + reject(err); } else { resolve(result); @@ -754,9 +759,9 @@ class fileIo { } static write(fd: int, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) @@ -769,9 +774,9 @@ class fileIo { } static write(fd: int, buffer: BufferType, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.writeSync2, fd, buffer); - p1.then((ret: NullishType) => { - let err = new BusinessError(); + let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 callback(err, undefined) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 82eaf615..dc8bf986 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -16,9 +16,11 @@ #include "open_ani.h" #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "open_core.h" #include "type_converter.h" +#include namespace OHOS { namespace FileManagement { @@ -32,54 +34,54 @@ static ani_object Wrap(ani_env *env, const FsFile *file) ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %s", className); - return {}; + return nullptr; } ani_method ctor; if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { HILOGE("Cannot find constructor method for class %s", className); - return {}; + return nullptr; } ani_long ptr = static_cast(reinterpret_cast(file)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { HILOGE("New %s obj Failed!", className); - return {}; + return nullptr; } const auto &fdRet = file->GetFD(); if (!fdRet.IsSuccess()) { HILOGE("GetFD Failed!"); - return {}; + return nullptr; } const auto &fd = fdRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", fd)) { HILOGE("Set fd field value failed!"); - return {}; + return nullptr; } const auto &pathRet = file->GetPath(); if (!pathRet.IsSuccess()) { HILOGE("GetPath Failed!"); - return {}; + return nullptr; } const auto &path = pathRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "path", path)) { HILOGE("Set path field value failed!"); - return {}; + return nullptr; } const auto &nameRet = file->GetName(); if (!pathRet.IsSuccess()) { HILOGE("GetPath Failed!"); - return {}; + return nullptr; } const auto &name = nameRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "name", name)) { HILOGE("Set name field value failed!"); - return {}; + return nullptr; } return obj; } @@ -89,21 +91,30 @@ ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); - return {}; + ErrorHandler::Throw(env, EINVAL); + return nullptr; } auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); - return {}; + ErrorHandler::Throw(env, EINVAL); + return nullptr; } FsResult ret = OpenCore::DoOpen(filePath, modeOp); if (!ret.IsSuccess()) { HILOGE("Open failed"); - return {}; + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; } const FsFile *file = ret.GetData().value(); - return Wrap(env, move(file)); + auto result = Wrap(env, move(file)); + if (result == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; } } // namespace ANI } // namespace ModuleFileIO -- Gitee From baf8b81ab0e0ea0dcedd79e6dd580fa80dac85ea Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Thu, 13 Mar 2025 19:18:52 +0800 Subject: [PATCH 04/76] =?UTF-8?q?=E6=A1=86=E6=9E=B6=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangxiaoliang25 Change-Id: Idddf680c22e82683628a15d37d06ef9b20cf259d --- interfaces/kits/js/BUILD.gn | 21 +- .../kits/js/src/mod_fs/ani/arktsconfig.json | 19 - .../js/src/mod_fs/ani/bind_function_class.cpp | 36 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 1045 +++++++++++++++++ 4 files changed, 1075 insertions(+), 46 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_fs/ani/arktsconfig.json create mode 100644 interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index c03bb58f..815ea13e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -623,6 +623,7 @@ group("build_kits_js") { group("ani_file_api") { deps = [ ":ani_fs_class", + ":ohos_file_fs_abc", ":ani_hash_class", ":ani_securitylabel_class", ] @@ -707,7 +708,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/write_core.cpp", ] deps = [ - ":ani_fs_abc_etc", + ":ohos_file_fs_abc_etc", "${file_api_path}/interfaces/kits/native:remote_uri_native", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", @@ -752,24 +753,26 @@ ohos_shared_library("ani_fs_class") { cfi_cross_dso = true debug = false } + output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" } -generate_static_abc("ani_fs_abc") { - arktsconfig = "src/mod_fs/ani/arktsconfig.json" - dst_file = "$target_out_dir/ani_fs.abc" - out_puts = [ "$target_out_dir/ani_fs.abc" ] +generate_static_abc("ohos_file_fs_abc") { + base_url = "./src/mod_fs/ani/ets" + files = [ + "./src/mod_fs/ani/ets/@ohos.file.fs.ets" + ] is_boot_abc = "True" - device_dst_file = "/system/framework/ani_fs.abc" + device_dst_file = "/system/framework/ohos_file_fs_abc.abc" } -ohos_prebuilt_etc("ani_fs_abc_etc") { - source = "$target_out_dir/ani_fs.abc" +ohos_prebuilt_etc("ohos_file_fs_abc_etc") { + source = "$target_out_dir/ohos_file_fs_abc.abc" module_install_dir = "framework" subsystem_name = "filemanagement" part_name = "file_api" - deps = [ ":ani_fs_abc" ] + deps = [ ":ohos_file_fs_abc" ] } ohos_shared_library("ani_hash_class") { diff --git a/interfaces/kits/js/src/mod_fs/ani/arktsconfig.json b/interfaces/kits/js/src/mod_fs/ani/arktsconfig.json deleted file mode 100644 index 748c9057..00000000 --- a/interfaces/kits/js/src/mod_fs/ani/arktsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ - -{ - "compilerOptions": { - "package": "", - "baseUrl": ".", - "outDir": "./dist", - "paths": { - "std": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/std" - ], - "escompat": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/escompat" - ] - } - }, - "include": [ - "*.ets" - ] -} 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 b0354e5c..cf0c66c4 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 @@ -39,7 +39,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindFileMethods(ani_env *env) { - static const char *className = "Lfile_fs_class/FileInner;"; + static const char *className = "L@ohos/file/fs/FileInner;"; std::array methods = { ani_native_function { "getParent", nullptr, reinterpret_cast(FileAni::GetParent) }, @@ -53,7 +53,7 @@ static ani_status BindFileMethods(ani_env *env) static ani_status BindStatClassMethods(ani_env *env) { - static const char *className = "Lfile_fs_class/StatInner;"; + static const char *className = "L@ohos/file/fs/StatInner;"; std::array methods = { ani_native_function { "isBlockDevice", ":Z", reinterpret_cast(StatAni::IsBlockDevice) }, @@ -70,24 +70,24 @@ static ani_status BindStatClassMethods(ani_env *env) static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "Lfile_fs_class/fileIo;"; + static const char *className = "L@ohos/file/fs/ETSGLOBAL;"; std::array methods = { - ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + // ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, + // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, - ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, - ani_native_function { "mkdirSync", "Lstd/core/String;:I", reinterpret_cast(MkdirkAni::MkdirSync0) }, - ani_native_function { "mkdirSync", "Lstd/core/String;Z:I", reinterpret_cast(MkdirkAni::MkdirSync1) }, - ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, - ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, - ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, - ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, - ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, - ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, - ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, - ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, - ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, + // ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + // ani_native_function { "mkdirSync", "Lstd/core/String;:I", reinterpret_cast(MkdirkAni::MkdirSync0) }, + // ani_native_function { "mkdirSync", "Lstd/core/String;Z:I", reinterpret_cast(MkdirkAni::MkdirSync1) }, + // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, + // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + // ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, + // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + // ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, + // ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, className, methods); } @@ -113,7 +113,7 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) status = BindStaticMethods(env); if (status != ANI_OK) { - HILOGE("Cannot bind native static methods for fileio!"); + HILOGE("Cannot bind native static methods for BindStaticMethods!"); return status; }; 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 new file mode 100644 index 00000000..a3ca81fb --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -0,0 +1,1045 @@ + + +import { AsyncCallback, BusinessError } from '@ohos.base'; +import stream from '@ohos.util.stream'; +export default fileIo; + +declare namespace fileIo { + loadLibrary("ani_fs_class"); + export { access }; + export { accessSync }; + export { close }; + export { closeSync }; + export { copy }; + export { copyDir }; + export { copyDirSync }; + export { copyFile }; + export { copyFileSync }; + export { createRandomAccessFile }; + export { createRandomAccessFileSync }; + export { createStream }; + export { createStreamSync }; + export { createReadStream }; + export { createWriteStream }; + export { createWatcher }; + export { dup }; + export { fdatasync }; + export { fdatasyncSync }; + export { fdopenStream }; + export { fdopenStreamSync }; + export { fsync }; + export { fsyncSync }; + export { getxattr }; + export { getxattrSync }; + export { listFile }; + export { listFileSync }; + export { lseek }; + export { lstat }; + export { lstatSync }; + export { mkdir }; + export { mkdirSync }; + export { mkdtemp }; + export { mkdtempSync }; + export { moveDir }; + export { moveDirSync }; + export { moveFile }; + export { moveFileSync }; + export { open }; + export { openSync }; + export { read }; + export { readSync }; + export { readLines }; + export { readLinesSync }; + export { readText }; + export { readTextSync }; + export { rename }; + export { renameSync }; + export { rmdir }; + export { rmdirSync }; + export { setxattr }; + export { setxattrSync }; + export { stat }; + export { statSync }; + export { symlink }; + export { symlinkSync }; + export { truncate }; + export { truncateSync }; + export { unlink }; + export { unlinkSync }; + export { utimes }; + export { write }; + export { writeSync }; + export { AccessModeType }; + export { AccessFlagType }; + export { File }; + export { OpenMode }; + export { RandomAccessFile }; + export { ReaderIterator }; + export { Stat }; + export { Stream }; + export { ReadStream }; + export { WriteStream }; + export { WhenceType }; + export { connectDfs }; + export { disconnectDfs }; + export type { Progress }; + export type { CopyOptions }; + export type { ProgressListener }; + + namespace OpenMode { + const READ_ONLY = 0o0; + const WRITE_ONLY = 0o1; + const READ_WRITE = 0o2; + const CREATE = 0o100; + const TRUNC = 0o1000; + const APPEND = 0o2000; + const NONBLOCK = 0o4000; + const DIR = 0o200000; + const NOFOLLOW = 0o400000; + const SYNC = 0o4010000; + } +} + +loadLibrary("ani_fs_class"); +function access(path: string, mode?: AccessModeType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + if (mode === undefined) { + let promise = taskpool.execute((path: string): boolean => { + return doAccessSync(path) + }, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } else { + let promise = taskpool.execute((path: string, mode: AccessModeType): boolean => { + return doAccessSync(path, mode) + }, path, mode); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } + }); +} + + +function access(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): boolean => { + return doAccessSync(path) + }, path); + promise.then((ret: NullishType) => { + let err = new BusinessErrorInner(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, false); + } else { + err.code = 0; + let result = ret as boolean; + callback(err, result); + } + }); +} + +function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute((path: string, mode: AccessModeType, flag: AccessFlagType): boolean => { + return doAccessSync(path, mode, flag) + }, path, mode, flag); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + }) +} + +function accessSync(path: string, mode?: AccessModeType): boolean { + return doAccessSync(path, mode) +} + +function accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { + return doAccessSync(path, mode, flag) +} + +native function doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; + +native function close(file: number | File): Promise; + +native function close(file: number | File, callback: AsyncCallback): void; + +native function closeSync(file: number | File): void; + +native function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise; + +native function copy(srcUri: string, destUri: string, callback: AsyncCallback): void; + +native function copy(srcUri: string, destUri: string, options: CopyOptions, callback: AsyncCallback): void; + +native function copyDir(src: string, dest: string, mode?: number): Promise; + +native function copyDir(src: string, dest: string, callback: AsyncCallback): void; + +native function copyDir(src: string, dest: string, mode: number, callback: AsyncCallback): void; +native function copyDirSync(src: string, dest: string, mode?: number): void; + +native function copyFile(src: string | number, dest: string | number, mode?: number): Promise; + +native function copyFile(src: string | number, dest: string | number, callback: AsyncCallback): void; + +native function copyFile( + src: string | number, + dest: string | number, + mode: number, + callback: AsyncCallback +): void; + +native function copyFileSync(src: string | number, dest: string | number, mode?: number): void; + +native function createStream(path: string, mode: string): Promise; + +native function createStream(path: string, mode: string, callback: AsyncCallback): void; + +native function createStreamSync(path: string, mode: string): Stream; + + +native function createRandomAccessFile(file: string | File, mode?: number, + options?: RandomAccessFileOptions): Promise; + +native function createRandomAccessFile(file: string | File, callback: AsyncCallback): void; + +native function createRandomAccessFile(file: string | File, mode: number, callback: AsyncCallback): void; + +native function createRandomAccessFileSync(file: string | File, mode?: number, + options?: RandomAccessFileOptions): RandomAccessFile; + +native function createReadStream(path: string, options?: ReadStreamOptions): ReadStream; + +native function createWriteStream(path: string, options?: WriteStreamOptions): WriteStream; + +native function createWatcher(path: string, events: number, listener: WatchEventListener): Watcher; + +native function dup(fd: number): File; + +native function fdatasync(fd: number): Promise; + +native function fdatasync(fd: number, callback: AsyncCallback): void; + +native function fdatasyncSync(fd: number): void; + +native function fdopenStream(fd: number, mode: string): Promise; + +native function fdopenStream(fd: number, mode: string, callback: AsyncCallback): void; + +native function fdopenStreamSync(fd: number, mode: string): Stream; + +native function fsync(fd: number): Promise; + +native function fsync(fd: number, callback: AsyncCallback): void; + +native function fsyncSync(fd: number): void; + +function listFile( + path: string, + options?: ListFileOptions +): Promise { + return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute((path: string, options?: ListFileOptions): string[] => { + return listFileSync(path, options); + }, path, options); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + }); +} + +function listFile(path: string, callback: AsyncCallback): void { + let p1 = taskpool.execute((path: string): string[] => { + return listFileSync(path); + }, path); + p1.then((ret: NullishType) => { + let err = new BusinessErrorInner(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, []); + } else { + err.code = 0; + let r = ret as string[]; + callback(err, r); + } + }); +} + +function listFile( + path: string, + options: ListFileOptions, + callback: AsyncCallback +): void { + let p1 = taskpool.execute((path: string, options: ListFileOptions): string[] => { + return listFileSync(path, options); + }, path, options); + p1.then((ret: NullishType) => { + let err = new BusinessErrorInner(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, []); + } else { + err.code = 0; + let r = ret as string[]; + callback(err, r); + } + }); +} + +native function listFileSync( + path: string, + options?: ListFileOptions +): string[]; + +native function lseek(fd: number, offset: number, whence?: WhenceType): number; + +native function lstat(path: string): Promise; + +native function lstat(path: string, callback: AsyncCallback): void; + +native function lstatSync(path: string): Stat; + +function mkdir(path: string): Promise { + let pvoid: Promise = new Promise((resolve: (value: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): void => mkdirSync(path), path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + resolve(undefined); + } + }); + }) + return pvoid +} + +function mkdir(path: string, recursion: boolean): Promise { + let pvoid: Promise = new Promise((resolve: (value: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, recursion: boolean): void => mkdirSync(path, recursion), + path, recursion); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + resolve(undefined); + } + }); + }) + return pvoid +} + +function mkdir(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): void => mkdirSync(path), path); + promise.then((ret: NullishType) => { + let err = new BusinessErrorInner(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + callback(err, undefined); + } + }); +} + +function mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string, recursion: boolean): void => mkdirSync(path, recursion), + path, recursion); + promise.then((ret: NullishType) => { + let err = new BusinessErrorInner(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + callback(err, undefined); + } + }); +} + +native function mkdirSync(path: string): void; + +native function mkdirSync(path: string, recursion: boolean): void; + +native function mkdtemp(prefix: string): Promise; + +native function mkdtemp(prefix: string, callback: AsyncCallback): void; + +native function mkdtempSync(prefix: string): string; + +native function moveDir(src: string, dest: string, mode?: number): Promise; + +native function moveDir(src: string, dest: string, callback: AsyncCallback): void; + +native function moveDir(src: string, dest: string, mode: number, callback: AsyncCallback): void; + +native function moveDirSync(src: string, dest: string, mode?: number): void; + +native function moveFile(src: string, dest: string, mode?: number): Promise; + +native function moveFile(src: string, dest: string, callback: AsyncCallback): void; + +native function moveFile(src: string, dest: string, mode: number, callback: AsyncCallback): void; + +native function moveFileSync(src: string, dest: string, mode?: number): void; + +function open(path: string, mode?: number): Promise { + return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(openSync, path, mode); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessErrorInner(); + err.code = -1; + reject(err); + } else { + let r = ret as File; + resolve(r); + } + }); + }); +} + +native function open(path: string, callback: AsyncCallback): void; + +native function open(path: string, mode: number, callback: AsyncCallback): void; + +native function openSync(path: string, mode?: number): File; + +native function read( + fd: number, + buffer: ArrayBuffer, + options?: ReadOptions +): Promise; + +native function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void; + +native function read( + fd: number, + buffer: ArrayBuffer, + options: ReadOptions, + callback: AsyncCallback +): void; + +native function readSync( + fd: number, + buffer: ArrayBuffer, + options?: ReadOptions +): number; + +native function readLines(filePath: string, options?: Options): Promise; + +native function readLines(filePath: string, callback: AsyncCallback): void; + +native function readLines(filePath: string, options: Options, callback: AsyncCallback): void; + +native function readLinesSync(filePath: string, options?: Options): ReaderIterator; + +native function readText( + filePath: string, + options?: ReadTextOptions +): Promise; + +native function readText(filePath: string, callback: AsyncCallback): void; + +native function readText( + filePath: string, + options: ReadTextOptions, + callback: AsyncCallback +): void; + +native function readTextSync( + filePath: string, + options?: ReadTextOptions +): string; + +native function rename(oldPath: string, newPath: string): Promise; + +native function rename(oldPath: string, newPath: string, callback: AsyncCallback): void; + +native function renameSync(oldPath: string, newPath: string): void; + +native function rmdir(path: string): Promise; + +native function rmdir(path: string, callback: AsyncCallback): void; + +native function rmdirSync(path: string): void; + +native function stat(file: string | number): Promise; + +native function stat(file: string | number, callback: AsyncCallback): void; + +native function statSync(file: string | number): Stat; + +native function symlink(target: string, srcPath: string): Promise; + +native function symlink(target: string, srcPath: string, callback: AsyncCallback): void; + +native function symlinkSync(target: string, srcPath: string): void; + +native function truncate(file: string | number, len?: number): Promise; + +native function truncate(file: string | number, callback: AsyncCallback): void; + +native function truncate(file: string | number, len: number, callback: AsyncCallback): void; + +native function truncateSync(file: string | number, len?: number): void; + +native function unlink(path: string): Promise; + +native function unlink(path: string, callback: AsyncCallback): void; + +native function unlinkSync(path: string): void; + +native function utimes(path: string, mtime: number): void; + +native function write( + fd: number, + buffer: ArrayBuffer | string, + options?: WriteOptions +): Promise; + +native function write(fd: number, buffer: ArrayBuffer | string, callback: AsyncCallback): void; + +native function write( + fd: number, + buffer: ArrayBuffer | string, + options: WriteOptions, + callback: AsyncCallback +): void; + +native function writeSync( + fd: number, + buffer: ArrayBuffer | string, + options?: WriteOptions +): number; + +native function connectDfs(networkId: string, listeners: DfsListeners): Promise; + +native function disconnectDfs(networkId: string): Promise; + +native function setxattr(path: string, key: string, value: string): Promise; + + +native function setxattrSync(path: string, key: string, value: string): void; + +native function getxattr(path: string, key: string): Promise; + +native function getxattrSync(path: string, key: string): string; + +interface Progress { + readonly processedSize: number; + + readonly totalSize: number; +} + +export class TaskSignal { + native cancel(): void; + + onCancel(): Promise { + return Promise.resolve("直接返回已解决的 Promise") + } +} + +interface CopyOptions { + progressListener?: ProgressListener; + copySignal?: TaskSignal; +} + +type ProgressListener = (progress: Progress) => void; + +interface File { + readonly fd: number; + + readonly path: string; + + readonly name: string; + + getParent(): string; + + lock(exclusive?: boolean): Promise; + + lock(callback: AsyncCallback): void; + + lock(exclusive: boolean, callback: AsyncCallback): void; + + tryLock(exclusive?: boolean): void; + + unlock(): void; +} + +class FileInner implements File { + readonly fd: number; + + readonly path: string = ""; + + readonly name: string = ""; + + private nativePtr: long = 0; + + constructor(ptr: long) { + if (this.nativePtr == 0) { + this.nativePtr = ptr; + } + } + + native getParent(): string; + + native lock(exclusive?: boolean): Promise; + + native lock(callback: AsyncCallback): void; + + native lock(exclusive: boolean, callback: AsyncCallback): void; + + native tryLock(exclusive?: boolean): void; + + native unlock(): void; +} + + +interface RandomAccessFile { + + readonly fd: number; + + readonly filePointer: number; + + setFilePointer(filePointer: number): void; + + close(): void; + + write( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): Promise; + + write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; + + write( + buffer: ArrayBuffer | string, + options: WriteOptions, + callback: AsyncCallback + ): void; + + writeSync( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): number; + + read( + buffer: ArrayBuffer, + options?: ReadOptions + ): Promise; + + read(buffer: ArrayBuffer, callback: AsyncCallback): void; + + read( + buffer: ArrayBuffer, + options: ReadOptions, + callback: AsyncCallback + ): void; + + readSync( + buffer: ArrayBuffer, + options?: ReadOptions + ): number; + + getReadStream(): ReadStream; + + getWriteStream(): WriteStream; +} + +class RandomAccessFileInner implements RandomAccessFile { + readonly fd: number; + + readonly filePointer: number; + + native setFilePointer(filePointer: number): void; + + native close(): void; + + native write( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): Promise; + + native write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; + + native write( + buffer: ArrayBuffer | string, + options: WriteOptions, + callback: AsyncCallback + ): void; + + native writeSync( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): number; + + native read( + buffer: ArrayBuffer, + options?: ReadOptions + ): Promise; + + native read(buffer: ArrayBuffer, callback: AsyncCallback): void; + + native read( + buffer: ArrayBuffer, + options: ReadOptions, + callback: AsyncCallback + ): void; + + native readSync( + buffer: ArrayBuffer, + options?: ReadOptions + ): number; + + native getReadStream(): ReadStream; + + native getWriteStream(): WriteStream; +} + +class ReadStream extends stream.Readable { + native constructor(); + + readonly bytesRead: number; + + readonly path: string; + + native seek(offset: number, whence?: WhenceType): number; + + native close(): void; +} + +class WriteStream extends stream.Writable { + native constructor(); + + readonly bytesWritten: number; + + readonly path: string; + + native seek(offset: number, whence?: WhenceType): number; + + native close(): void; +} + +interface Stat { + readonly ino: bigint; + readonly mode: number; + readonly uid: number; + readonly gid: number; + readonly size: number; + readonly atime: number; + readonly mtime: number; + readonly ctime: number; + readonly location: LocationType; + + isBlockDevice(): boolean; + isCharacterDevice(): boolean; + isDirectory(): boolean; + isFIFO(): boolean; + isFile(): boolean; + isSocket(): boolean; + isSymbolicLink(): boolean; +} + +class StatInner implements Stat { + readonly ino: bigint = 0n; + readonly mode: number; + readonly uid: number; + readonly gid: number; + readonly size: number; + readonly atime: number; + readonly mtime: number; + readonly ctime: number; + readonly location: LocationType; + + private nativeStat: long = 0; + + constructor(stat: long) { + if (this.nativeStat == 0) { + this.nativeStat = stat; + } + } + + native isBlockDevice(): boolean; + native isCharacterDevice(): boolean; + native isDirectory(): boolean; + native isFIFO(): boolean; + native isFile(): boolean; + native isSocket(): boolean; + native isSymbolicLink(): boolean; +} + + +interface Stream { + close(): Promise; + + close(callback: AsyncCallback): void; + + closeSync(): void; + + flush(): Promise; + + flush(callback: AsyncCallback): void; + + flushSync(): void; + + write( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): Promise; + + write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; + + write( + buffer: ArrayBuffer | string, + options: WriteOptions, + callback: AsyncCallback + ): void; + + writeSync( + buffer: ArrayBuffer | string, + options?: WriteOptions + ): number; + + read( + buffer: ArrayBuffer, + options?: ReadOptions + ): Promise; + + read(buffer: ArrayBuffer, callback: AsyncCallback): void; + + read( + buffer: ArrayBuffer, + options: ReadOptions, + callback: AsyncCallback + ): void; + + readSync( + buffer: ArrayBuffer, + options?: ReadOptions + ): number; +} + +class StreamInner implements Stream { + native close(): Promise; + + native close(callback: AsyncCallback): void; + + native closeSync(): void; + + native flush(): Promise; + + native flush(callback: AsyncCallback): void; + + native flushSync(): void; + + native write( + buffer: ArrayBuffer | string, + options?: WriteOptions +): Promise; + + native write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; + + native write( + buffer: ArrayBuffer | string, + options: WriteOptions, + callback: AsyncCallback +): void; + + native writeSync( + buffer: ArrayBuffer | string, + options?: WriteOptions +): number; + + native read( + buffer: ArrayBuffer, + options?: ReadOptions +): Promise; + + native read(buffer: ArrayBuffer, callback: AsyncCallback): void; + + native read( + buffer: ArrayBuffer, + options: ReadOptions, + callback: AsyncCallback +): void; + + native readSync( + buffer: ArrayBuffer, + options?: ReadOptions +): number; +} + +export interface WatchEventListener { + foo(event: WatchEvent): void; +} + + +export interface WatchEvent { + readonly fileName: string; + + readonly event: number; + + readonly cookie: number; +} + +export interface Watcher { + start(): void; + + stop(): void; +} + +export interface ReaderIteratorResult { + done: boolean; + + value: string; +} + +declare interface ReaderIterator { + next(): ReaderIteratorResult; +} + +class ReaderIteratorInner implements ReaderIterator { + native next(): ReaderIteratorResult; +} + +export interface Filter { + suffix?: Array; + displayName?: Array; + mimeType?: Array; + fileSizeOver?: number; + lastModifiedAfter?: number; + excludeMedia?: boolean; +} + +export interface ConflictFiles { + srcFile: string; + + destFile: string; +} + +export interface Options { + encoding?: string; +} + +export interface ReadOptions { + offset?: number; + length?: number; +} + +export interface ReadTextOptions extends ReadOptions { + encoding?: string; +} + +export interface WriteOptions extends Options { + offset?: number; + length?: number; +} + +export interface ListFileOptions { + recursion?: boolean; + + listNum?: number; + + filter?: Filter; +} + +export interface RandomAccessFileOptions { + start?: number; + + end?: number; +} + +export interface ReadStreamOptions { + start?: number; + + end?: number; +} + +export interface WriteStreamOptions { + mode?: number; + start?: number; +} + +export interface DfsListeners { + onStatus(networkId: string, status: number): void; +} + +declare enum WhenceType { + SEEK_SET = 0, + + SEEK_CUR = 1, + + SEEK_END = 2, +} + +declare enum LocationType { + LOCAL = 1 << 0, + + CLOUD = 1 << 1, +} + +declare enum AccessModeType { + EXIST = 0, + + WRITE = 2, + + READ = 4, + + READ_WRITE = 6, +} + +declare enum AccessFlagType { + LOCAL = 0, +} + + +class BusinessErrorInner implements BusinessError { + code: number; + data?: T; + name: string = ""; + message: string = ""; + stack?: string = ""; +} + +function main():void { + loadLibrary("ani_fs_class"); + console.info("hello world") + let result = accessSync("/data") + console.info(`access: ${result}`) +} \ No newline at end of file -- Gitee From 3f7b7b0757a79eb769d2564bcf38de7b1ab40690 Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Fri, 14 Mar 2025 10:45:05 +0800 Subject: [PATCH 05/76] hash and securityLabel Signed-off-by: zhangxiaoliang25 Change-Id: I6116bda5e21c0c124587fa9aa09d57f8a168c7f9 --- interfaces/kits/js/BUILD.gn | 38 ++++++++++--------- .../kits/js/src/mod_hash/ani/arktsconfig.json | 18 --------- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 19 ++++++++++ .../mod_securitylabel/ani/arktsconfig.json | 18 --------- .../ani/ets/@ohos.file.securityLabel.ets | 19 ++++++++++ 5 files changed, 58 insertions(+), 54 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_hash/ani/arktsconfig.json create mode 100644 interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets delete mode 100644 interfaces/kits/js/src/mod_securitylabel/ani/arktsconfig.json create mode 100644 interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 815ea13e..5680632c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -792,7 +792,7 @@ ohos_shared_library("ani_hash_class") { ] deps = [ - ":ani_hash_abc_etc", + ":ohos_file_hash_abc_etc", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", @@ -828,20 +828,21 @@ ohos_shared_library("ani_hash_class") { part_name = "file_api" } -generate_static_abc("ani_hash_abc") { - arktsconfig = "src/mod_hash/ani/arktsconfig.json" - dst_file = "$target_out_dir/ani_hash.abc" - out_puts = [ "$target_out_dir/ani_hash.abc" ] +generate_static_abc("ohos_file_hash_abc") { + base_url = "./src/mod_hash/ani/ets" + files = [ + "./src/mod_hash/ani/ets/@ohos.file.hash.ets" + ] is_boot_abc = "True" - device_dst_file = "/system/framework/ani_hash.abc" + device_dst_file = "/system/framework/ohos_file_hash_abc.abc" } -ohos_prebuilt_etc("ani_hash_abc_etc") { - source = "$target_out_dir/ani_hash.abc" +ohos_prebuilt_etc("ohos_file_hash_abc_etc") { + source = "$target_out_dir/ohos_file_hash_abc.abc" module_install_dir = "framework" subsystem_name = "filemanagement" part_name = "file_api" - deps = [ ":ani_hash_abc" ] + deps = [ ":ohos_file_hash_abc" ] } ohos_shared_library("ani_securitylabel_class") { @@ -860,7 +861,7 @@ ohos_shared_library("ani_securitylabel_class") { ] deps = [ - ":ani_securitylabel_abc_etc", + ":ohos_file_securityLabel_abc_etc", "${file_api_path}/interfaces/kits/rust:rust_file", "${utils_path}/filemgmt_libfs:filemgmt_libfs", "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", @@ -895,18 +896,19 @@ ohos_shared_library("ani_securitylabel_class") { part_name = "file_api" } -generate_static_abc("ani_securitylabel_abc") { - arktsconfig = "src/mod_securitylabel/ani/arktsconfig.json" - dst_file = "$target_out_dir/ani_securitylabel.abc" - out_puts = [ "$target_out_dir/ani_securitylabel.abc" ] +generate_static_abc("ohos_file_securityLabel_abc") { + base_url = "./src/mod_securitylabel/ani/ets" + files = [ + "./src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets" + ] is_boot_abc = "True" - device_dst_file = "/system/framework/ani_securitylabel.abc" + device_dst_file = "/system/framework/ohos_file_securityLabel_abc.abc" } -ohos_prebuilt_etc("ani_securitylabel_abc_etc") { - source = "$target_out_dir/ani_securitylabel.abc" +ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { + source = "$target_out_dir/ohos_file_securityLabel_abc.abc" module_install_dir = "framework" subsystem_name = "filemanagement" part_name = "file_api" - deps = [ ":ani_securitylabel_abc" ] + deps = [ ":ohos_file_securityLabel_abc" ] } diff --git a/interfaces/kits/js/src/mod_hash/ani/arktsconfig.json b/interfaces/kits/js/src/mod_hash/ani/arktsconfig.json deleted file mode 100644 index ae12d05b..00000000 --- a/interfaces/kits/js/src/mod_hash/ani/arktsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compilerOptions": { - "package": "", - "baseUrl": ".", - "outDir": "./dist", - "paths": { - "std": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/std" - ], - "escompat": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/escompat" - ] - } - }, - "include": [ - "*.ets" - ] -} 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 new file mode 100644 index 00000000..d291adce --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -0,0 +1,19 @@ + +import type { AsyncCallback } from './@ohos.base'; +import stream from './@ohos.util.stream'; + +declare namespace hash { + native function hash(path: string, algorithm: string): Promise; + + native function hash(path: string, algorithm: string, callback: AsyncCallback): void; + + class HashStream extends stream.Transform { + native digest(): string; + + native update(data: ArrayBuffer): void; + } + + native function createHash(algorithm: string): HashStream; +} + +export default hash; \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/arktsconfig.json b/interfaces/kits/js/src/mod_securitylabel/ani/arktsconfig.json deleted file mode 100644 index ae12d05b..00000000 --- a/interfaces/kits/js/src/mod_securitylabel/ani/arktsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compilerOptions": { - "package": "", - "baseUrl": ".", - "outDir": "./dist", - "paths": { - "std": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/std" - ], - "escompat": [ - "../../../../../../../../../arkcompiler/runtime_core/static_core/plugins/ets/stdlib/escompat" - ] - } - }, - "include": [ - "*.ets" - ] -} diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets new file mode 100644 index 00000000..47841fcd --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -0,0 +1,19 @@ +import type { AsyncCallback } from './@ohos.base'; + +declare namespace securityLabel { + type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; + + native function setSecurityLabel(path: string, type: DataLevel): Promise; + + native function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void; + + native function setSecurityLabelSync(path: string, type: DataLevel): void; + + native function getSecurityLabel(path: string): Promise; + + native function getSecurityLabel(path: string, callback: AsyncCallback): void; + + native function getSecurityLabelSync(path: string): string; +} + +export default securityLabel; \ No newline at end of file -- Gitee From ed92e6995cc6a83cf1cf40ca2e07187500b39d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Fri, 14 Mar 2025 15:32:02 +0800 Subject: [PATCH 06/76] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I8cbecd3bd845ef4f62842fe478fbe93d61ae6a08 --- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 537 ++++++++++++++++++ 1 file changed, 537 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index c7570eba..38d801d4 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -788,3 +788,540 @@ class fileIo { }); } } + +function accessSyncTest1() { + console.println("accessSyncTest1 begin"); + try { + let ret = fileIo.accessSync("/data/local/tmp/a.txt"); + console.println(`accessSync result : ${ret}`); + } catch (error) { + console.error("accessSyncTest1 Error!", error); + } + console.println("accessSyncTest1 end"); +} + +function closeSyncTest1() { + console.println("closeSyncTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let ret = fileIo.closeSync(file.fd); + console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest1 Error!", error); + } + console.println("closeSyncTest1 end"); +} + +function closeSyncTest2() { + console.println("closeSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let ret = fileIo.closeSync(file); + console.println(`close file by file, fd=${file.fd}, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest2 Error!", error); + } + console.println("closeSyncTest2 end"); +} + +function closeSyncTest3() { + console.println("closeSyncTest3 begin"); + try { + let ret = fileIo.closeSync(-1); + console.println(`close file by invalid fd: -1, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest3 Error!", error); + } + console.println("closeSyncTest3 end"); +} + +function closePromiseTest() { + console.println("closePromiseTest begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.close(file.fd).then((result: int) => { + console.println(`closePromiseTest close result: ${result}`); + }); + } catch (error) { + console.error("closePromiseTest Error:", error); + } + console.println("closePromiseTest end"); +} + +function closeCallbackTest() { + console.println("closeCallbackTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.close(file, (err: BusinessError, data?: int) => { + if (err.code) { + console.error("closeCallbackTest: close Error:", err); + } else { + console.log("closeCallbackTest: close result:", data); + } + }); + console.println("closeCallbackTest end"); +} + +function copyFileTest() { + console.println("copyFileTest begin"); + try { + fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); + } catch (error) { + console.error("copyFileTest Error!", error); + } + console.println("copyFileTest end"); +} + +function fileTest() { + console.println("fileTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt"); + let fd = file.fd; + console.println(`open file, fd=${fd}`); + let parent = file.getParent(); + console.println(`file parent=${parent}`); + file.lock(); + console.println(`file lock completed`); + file.unlock(); + console.println(`file unlock completed`); + file.tryLock(true); + console.println(`file tryLock completed`); + file.unlock(); + console.println(`file unlock completed`); + console.println("fileTest end"); +} + +function listFileTest() { + console.println("listFileTest begin"); + try { + let files = fileIo.listFileSync("/data/local/tmp/"); + for (const file of files) { + console.println(file); + } + } catch (error) { + console.error("listFileTest Error!", error); + } + console.println("listFileTest end"); +} + +function mkdirTest1() { + console.println("mkdirTest1 begin") + try { + const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); + console.println(`mkdir result: ${ret}`); + } catch (error) { + console.error("mkdirTest1 Error!", error); + } + console.println("mkdirTest1 end"); +} + +function moveSyncTest() { + console.println("moveSyncTest begin") + fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) + console.println("moveSyncTest end") +} + +function openSyncTest() { + console.println("openSyncTest begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let mode = 2; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openSyncTest Error!", error); + } + console.println("openSyncTest end"); +} + +function readSyncTest1() { + console.println("readSyncTest1 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let length = fileIo.readSync(fd, buffer); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest1 Error!", error); + } + console.println("readSyncTest1 end") +} + +function readSyncTest2() { + console.println("readSyncTest2 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest2 Error!", error); + } + console.println("readSyncTest2 end") +} + +function readSyncTest3() { + console.println("readSyncTest3 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + offset: 3, + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest3 Error!", error); + } + console.println("readSyncTest3 end") +} + +function readTextSyncTest1() { + console.println("readTextSyncTest1 begin") + try { + let content = fileIo.readTextSync("/data/local/tmp/a.txt"); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readTextSyncTest1 Error!", error); + } + console.println("readTextSyncTest1 end") +} + +function readTextSyncTest2() { + console.println("readTextSyncTest2 begin"); + try { + let options: ReadTextOptions = { + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest2 Error!", error); + } + console.println("readTextSyncTest2 end"); +} + +function readTextSyncTest3() { + console.println("readTextSyncTest3 begin") + try { + let options: ReadTextOptions = { + offset: 3, + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest3 Error!", error); + } + console.println("readTextSyncTest3 end"); +} + +function rmdirSyncTest() { + console.println("rmdirSyncTest begin"); + try { + fileIo.rmdirSync("/data/local/tmp/dir01"); + } catch (error) { + console.error("rmdirSyncTest Error!", error); + } + console.println("rmdirSyncTest end"); +} + +function printStatInfo(stat?: Stat) { + if (stat === undefined) { + console.error("stat is null or undefined!"); + return; + } + console.info("stat, ino is " + stat.ino); + console.info("stat, mode is " + stat.mode); + console.info("stat, uid is " + stat.uid); + console.info("stat, gid is " + stat.gid); + console.info("stat, size is " + stat.size); + console.info("stat, atime is " + stat.atime); + console.info("stat, mtime is " + stat.mtime); + console.info("stat, ctime is " + stat.ctime); + console.info("stat, atimeNs is " + stat.atimeNs); + console.info("stat, mtimeNs is " + stat.mtimeNs); + console.info("stat, ctimeNs is " + stat.ctimeNs); + console.info("stat, location is " + stat.location); + console.info("stat, isBlockDevice is " + stat.isBlockDevice()); + console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); + console.info("stat, isDirectory is " + stat.isDirectory()); + console.info("stat, isFIFO is " + stat.isFIFO()); + console.info("stat, isFile is " + stat.isFile()); + console.info("stat, isSocket is " + stat.isSocket()); + console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); +} + +function statSyncTest1() { + console.println("statSyncTest1 begin"); + try { + let stat = fileIo.statSync("/data/local/tmp/a.txt"); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest1 Error!", error); + } + console.println("statSyncTest1 end"); +} + +function statSyncTest2() { + console.println("statSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file fd: ${file.fd}`); + let stat = fileIo.statSync(file.fd); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest2 Error!", error); + } + console.println("statSyncTest2 end"); +} + +function statPromiseTest() { + console.println("statPromiseTest start"); + try { + let path = "/data/local/tmp/a.txt" + let file = fileIo.openSync(path); + console.println(`open file fd: ${file.fd}`); + fileIo.stat(path).then((result: Stat) => { + console.log("statPromiseTest success"); + printStatInfo(result); + }); + } catch (error) { + console.error("statPromiseTest Error!", error); + } + console.println("statPromiseTest end"); +} + +function statCallbackTest() { + console.println("statCallbackTest start"); + let path = "/data/local/tmp/a.txt" + fileIo.stat(path, (err: BusinessError, data?: Stat) => { + if (data === undefined || data === null) { + console.error("Callback: Error stat:", err); + } else { + console.log("Callback stat success"); + printStatInfo(data); + } + }); + console.println("statCallbackTest end"); +} + +function truncateSyncTest1() { + console.println("truncateSyncTest1 begin"); + try { + fileIo.truncateSync(-1, 4); + } catch (error) { + console.error("truncateSyncTest1 Error!", error); + } + console.println("truncateSyncTest1 end"); +} + +function truncateSyncTest2() { + console.println("truncateSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file, fd=${file.fd}`); + fileIo.truncateSync(file.fd, 4); + } catch (error) { + console.error("truncateSyncTest2 Error!", error); + } + console.println("truncateSyncTest2 end"); +} + +function truncateSyncTest3() { + console.println("truncateSyncTest3 begin"); + try { + fileIo.truncateSync("/data/local/tmp/b.txt", 4); + } catch (error) { + console.error("truncateSyncTest3 Error!", error); + } + console.println("truncateSyncTest3 end"); +} + +function unlinkSyncTest() { + console.println("unlinkSyncTest begin"); + try { + const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); + console.println(`unlink result: ${ret}`); + + } catch (error) { + console.error("unlinkSyncTest Error!", error); + } + console.println("unlinkSyncTest end"); +} + +function writeSyncTest1() { + console.println("writeSyncTest1 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + encoding: "utf-8", + }; + let length = fileIo.writeSync(fd, "hello,world!", options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest1 Error!", error); + } + console.println("writeSyncTest1 end"); +} + +function writeSyncTest2() { + console.println("writeSyncTest2 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + offset: 100, + }; + let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest2 Error!", error); + } + console.println("writeSyncTest2 end"); +} + +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + console.println("\n"); + console.println("try open file notexist.txt ..."); + let mode = 2; + file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); + console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest1 Error!", error); + } + console.println("errorHandlerTest1 end"); +} + +function errorHandlerTest2() { + console.println("errorHandlerTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println("try open file a.txt ..."); + let mode = -1; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest2 Error!", error); + } + console.println("errorHandlerTest2 end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let file = await fileIo.open("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + console.println("-----------"); + console.println("try open file a.txt ..."); + let mode = -1; + file = await fileIo.open("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + + fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { + console.println("2)-----------"); + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + // accessSyncTest1(); + // openSyncTest(); + // closeSyncTest1(); + // closeSyncTest2(); + // closeSyncTest3(); + // closePromiseTest(); + // closeCallbackTest(); + // copyFileTest(); + // fileTest(); + // listFileTest(); + // mkdirTest1(); + // moveSyncTest(); + // readSyncTest1(); + // readSyncTest2(); + // readSyncTest3(); + // readTextSyncTest1(); + // readTextSyncTest2(); + // readTextSyncTest3(); + // rmdirSyncTest(); + // statSyncTest1(); + // statSyncTest2(); + // statPromiseTest(); + // statCallbackTest(); + // truncateSyncTest1(); + // truncateSyncTest2(); + // truncateSyncTest3(); + // unlinkSyncTest(); + // writeSyncTest1(); + // writeSyncTest2(); + // errorHandlerTest1(); // 打开文件失败,文件不存在 + errorHandlerTest2(); // 参数校验失败 + // errorHandlerTest3(); // 异步Promise + // errorHandlerTest4(); // 异步callback + console.println("---------- hello ani end ---------------"); +} \ No newline at end of file -- Gitee From f2dd22246d12fae2741a45eba872589a66fd103f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Fri, 14 Mar 2025 17:07:04 +0800 Subject: [PATCH 07/76] =?UTF-8?q?=E9=80=82=E9=85=8Dnumber=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I452cddd750f8bdd46fffbaa794531bc99e323332 --- .../src/common/ani_helper/type_converter.cpp | 60 ++++++++++++++----- .../js/src/common/ani_helper/type_converter.h | 4 +- 2 files changed, 47 insertions(+), 17 deletions(-) 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 e0948fa1..267b9006 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -15,6 +15,7 @@ #include "type_converter.h" +#include #include #include @@ -45,43 +46,53 @@ std::tuple TypeConverter::ToUTF8String(ani_env *env, const an return { true, std::move(result) }; } -std::tuple> TypeConverter::ToOptionalInt32(ani_env *env, const ani_object &intOp) +std::tuple> TypeConverter::ToOptionalInt32(ani_env *env, const ani_object &value) { if (env == nullptr) { return { false, {} }; } ani_boolean isUndefined; - env->Reference_IsUndefined(intOp, &isUndefined); + env->Reference_IsUndefined(value, &isUndefined); if (isUndefined) { return { true, std::nullopt }; } - ani_int result; - if (ANI_OK != env->Object_CallMethodByName_Int(intOp, "intValue", nullptr, &result)) { - return { false, {} }; + ani_double doubleValue; + if (ANI_OK == env->Object_CallMethodByName_Double(value, "doubleValue", nullptr, &doubleValue)) { + return { true, std::make_optional(static_cast(doubleValue)) }; } - return { true, std::make_optional(result) }; + ani_int intValue; + if (ANI_OK == env->Object_CallMethodByName_Int(value, "intValue", nullptr, &intValue)) { + return { true, std::make_optional(intValue) }; + } + + return { false, {} }; } -std::tuple> TypeConverter::ToOptionalInt64(ani_env *env, const ani_object &longOp) +std::tuple> TypeConverter::ToOptionalInt64(ani_env *env, const ani_object &value) { if (env == nullptr) { return { false, {} }; } ani_boolean isUndefined; - env->Reference_IsUndefined(longOp, &isUndefined); + env->Reference_IsUndefined(value, &isUndefined); if (isUndefined) { return { true, std::nullopt }; } - ani_long result; - if (ANI_OK != env->Object_CallMethodByName_Long(longOp, "longValue", nullptr, &result)) { - return { false, {} }; + ani_double doubleValue; + if (ANI_OK == env->Object_CallMethodByName_Double(value, "doubleValue", nullptr, &doubleValue)) { + return { true, std::make_optional(static_cast(doubleValue)) }; } - return { true, std::make_optional(result) }; + ani_long longValue; + if (ANI_OK == env->Object_CallMethodByName_Long(value, "longValue", nullptr, &longValue)) { + return { true, std::make_optional(longValue) }; + } + + return { false, {} }; } std::tuple TypeConverter::ToAniString(ani_env *env, std::string str) @@ -137,9 +148,6 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj ani_class stringClass; env->FindClass("Lstd/core/String;", &stringClass); - ani_class IntClass; - env->FindClass("Lstd/core/Int;", &IntClass); - ani_boolean isPath = false; env->Object_InstanceOf(pathOrFd, stringClass, &isPath); if (isPath) { @@ -159,6 +167,27 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj } ani_boolean isFd = false; + + ani_class DoubleClass; + env->FindClass("Lstd/core/Double;", &DoubleClass); + env->Object_InstanceOf(pathOrFd, DoubleClass, &isFd); + if (isFd) { + ani_double doubleValue; + if (ANI_OK != env->Object_CallMethodByName_Double(pathOrFd, "doubleValue", nullptr, &doubleValue)) { + HILOGE("Parse file path failed"); + return { false, FileInfo { false, {}, {} } }; + } + int32_t fd = static_cast(doubleValue); + auto fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); + return { false, FileInfo { false, {}, {} } }; + } + return { true, FileInfo { false, {}, move(fdg) } }; + } + + ani_class IntClass; + env->FindClass("Lstd/core/Int;", &IntClass); env->Object_InstanceOf(pathOrFd, IntClass, &isFd); if (isFd) { ani_int fd; @@ -173,6 +202,7 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj } return { true, FileInfo { false, {}, move(fdg) } }; } + return { false, FileInfo { false, {}, {} } }; } diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.h b/interfaces/kits/js/src/common/ani_helper/type_converter.h index ff8463a7..24bdd736 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.h +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.h @@ -30,8 +30,8 @@ inline const std::string EMPTY_STRING = ""; class TypeConverter { public: static std::tuple ToUTF8String(ani_env *env, const ani_string &path); - static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &intOp); - static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &longOp); + static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &value); + static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &value); static std::tuple ToAniString(ani_env *env, std::string str); static std::tuple ToAniString(ani_env *env, const char *str); static std::tuple> EnumToInt32(ani_env *env, const ani_enum_item &enumOp); -- Gitee From 1f6e7589d3ba58facd7bffab52264350b29e0cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Fri, 14 Mar 2025 19:29:37 +0800 Subject: [PATCH 08/76] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E4=BF=AE=E5=A4=8DBusin?= =?UTF-8?q?essError=E9=97=AE=E9=A2=98=20Signed-off-by:=20=E5=A7=9C?= =?UTF-8?q?=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I24bfea6f3812303b407d76215aee24404ad3c479 --- .../js/src/common/ani_helper/error_handler.h | 18 ++++-- .../kits/js/src/mod_fs/ani/error_handler.cpp | 2 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 56 ++++++++++++++----- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index a2b86173..248df5fe 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -19,6 +19,7 @@ #include #include #include +#include "ani_helper.h" #include "filemgmt_libhilog.h" #include "fs_error.h" #include "type_converter.h" @@ -75,17 +76,22 @@ private: return ANI_NOT_FOUND; } - auto [succ, msg] = TypeConverter::ToAniString(env, errMsg); - if (!succ) { - HILOGE("Cannot convert errMsg to ani string"); + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj)) { + HILOGE("Cannot create ani error object"); return ANI_ERROR; } - ani_object obj; - if (ANI_OK != env->Object_New(cls, ctor, &obj, static_cast(code), msg)) { - HILOGE("Cannot create ani error object"); + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "code", static_cast(code))) { + HILOGE("Set code field value failed!"); return ANI_ERROR; } + + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "message", errMsg)) { + HILOGE("Set message field value failed!"); + return ANI_ERROR; + } + auto status = env->ThrowError(static_cast(obj)); if (status != ANI_OK) { HILOGE("Throw ani error object failed!"); diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp index 34fc9249..a05d70f2 100644 --- a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp @@ -19,7 +19,7 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) { - const char *className = "Lfile_fs_class/BusinessError;"; + const char *className = "L@ohos/file/fs/BusinessErrorInner;"; return Throw(env, className, code, errMsg); } 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 a3ca81fb..d63670f2 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 @@ -1,9 +1,11 @@ -import { AsyncCallback, BusinessError } from '@ohos.base'; +// import { AsyncCallback, BusinessError } from '@ohos.base'; import stream from '@ohos.util.stream'; export default fileIo; +export type AsyncCallback = (err: BusinessError, data: T) => void; + declare namespace fileIo { loadLibrary("ani_fs_class"); export { access }; @@ -100,7 +102,6 @@ declare namespace fileIo { } } -loadLibrary("ani_fs_class"); function access(path: string, mode?: AccessModeType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { if (mode === undefined) { @@ -168,7 +169,7 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi resolve(result); } }); - }) + }); } function accessSync(path: string, mode?: AccessModeType): boolean { @@ -338,10 +339,10 @@ function mkdir(path: string): Promise { err.code = -1; reject(err); } else { - resolve(undefined); + resolve(undefined); } }); - }) + }); return pvoid } @@ -359,7 +360,7 @@ function mkdir(path: string, recursion: boolean): Promise { resolve(undefined); } }); - }) + }); return pvoid } @@ -1000,6 +1001,20 @@ export interface DfsListeners { onStatus(networkId: string, status: number): void; } +export interface BusinessError { + message: string; + code: number; + data?: T; +} + +class BusinessErrorInner implements BusinessError { + name: string = "BusinessError"; + message: string = ""; + code: number = 0; + data?: T; + stack?: string; +} + declare enum WhenceType { SEEK_SET = 0, @@ -1028,18 +1043,31 @@ declare enum AccessFlagType { LOCAL = 0, } - -class BusinessErrorInner implements BusinessError { - code: number; - data?: T; - name: string = ""; - message: string = ""; - stack?: string = ""; +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + let file = openSync("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + console.println("\n"); + console.println("try open file notexist.txt ..."); + let mode = 2; + file = openSync("/data/local/tmp/notexist.txt", mode); + console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest1 Error!", error); + } + console.println("errorHandlerTest1 end"); } -function main():void { +function main(): void { + console.println("---------- hello ani --------------"); loadLibrary("ani_fs_class"); console.info("hello world") let result = accessSync("/data") console.info(`access: ${result}`) + + // errorHandlerTest1(); + + console.println("---------- hello ani end ---------------"); } \ No newline at end of file -- Gitee From b281e1451a292591f83add9fe31150ee374f11b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Fri, 14 Mar 2025 20:36:53 +0800 Subject: [PATCH 09/76] =?UTF-8?q?ets=E6=A0=B7=E6=9C=AC=20Signed-off-by:=20?= =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic6c950536fdcbd1eaefb3a54223ed27f0036fe52 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 1960 ++++++++++------- 1 file changed, 1107 insertions(+), 853 deletions(-) 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 d63670f2..543c62ba 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 @@ -1,1058 +1,1235 @@ +/* + * 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. + */ - -// import { AsyncCallback, BusinessError } from '@ohos.base'; -import stream from '@ohos.util.stream'; export default fileIo; -export type AsyncCallback = (err: BusinessError, data: T) => void; - -declare namespace fileIo { - loadLibrary("ani_fs_class"); - export { access }; - export { accessSync }; - export { close }; - export { closeSync }; - export { copy }; - export { copyDir }; - export { copyDirSync }; - export { copyFile }; - export { copyFileSync }; - export { createRandomAccessFile }; - export { createRandomAccessFileSync }; - export { createStream }; - export { createStreamSync }; - export { createReadStream }; - export { createWriteStream }; - export { createWatcher }; - export { dup }; - export { fdatasync }; - export { fdatasyncSync }; - export { fdopenStream }; - export { fdopenStreamSync }; - export { fsync }; - export { fsyncSync }; - export { getxattr }; - export { getxattrSync }; - export { listFile }; - export { listFileSync }; - export { lseek }; - export { lstat }; - export { lstatSync }; - export { mkdir }; - export { mkdirSync }; - export { mkdtemp }; - export { mkdtempSync }; - export { moveDir }; - export { moveDirSync }; - export { moveFile }; - export { moveFileSync }; - export { open }; - export { openSync }; - export { read }; - export { readSync }; - export { readLines }; - export { readLinesSync }; - export { readText }; - export { readTextSync }; - export { rename }; - export { renameSync }; - export { rmdir }; - export { rmdirSync }; - export { setxattr }; - export { setxattrSync }; - export { stat }; - export { statSync }; - export { symlink }; - export { symlinkSync }; - export { truncate }; - export { truncateSync }; - export { unlink }; - export { unlinkSync }; - export { utimes }; - export { write }; - export { writeSync }; - export { AccessModeType }; - export { AccessFlagType }; - export { File }; - export { OpenMode }; - export { RandomAccessFile }; - export { ReaderIterator }; - export { Stat }; - export { Stream }; - export { ReadStream }; - export { WriteStream }; - export { WhenceType }; - export { connectDfs }; - export { disconnectDfs }; - export type { Progress }; - export type { CopyOptions }; - export type { ProgressListener }; - - namespace OpenMode { - const READ_ONLY = 0o0; - const WRITE_ONLY = 0o1; - const READ_WRITE = 0o2; - const CREATE = 0o100; - const TRUNC = 0o1000; - const APPEND = 0o2000; - const NONBLOCK = 0o4000; - const DIR = 0o200000; - const NOFOLLOW = 0o400000; - const SYNC = 0o4010000; - } -} - -function access(path: string, mode?: AccessModeType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute((path: string): boolean => { - return doAccessSync(path) - }, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } else { - let promise = taskpool.execute((path: string, mode: AccessModeType): boolean => { - return doAccessSync(path, mode) - }, path, mode); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); +export class BusinessError { + name: string; + message: string; + code: number; + data?: T; + constructor(code: number, msg: string, data?: T) { + this.name = 'BusinessError'; + this.code = code; + this.message = msg; + if (data !== undefined) { + this.data = data; } - }); + } } +export type AsyncCallback = (err: BusinessError, data?: T) => void; -function access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): boolean => { - return doAccessSync(path) - }, path); - promise.then((ret: NullishType) => { - let err = new BusinessErrorInner(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, false); - } else { - err.code = 0; - let result = ret as boolean; - callback(err, result); - } - }); +export interface Filter { + suffix?: Array; + displayName?: Array; + mimeType?: Array; + fileSizeOver?: number; + lastModifiedAfter?: number; + excludeMedia?: boolean; } -function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute((path: string, mode: AccessModeType, flag: AccessFlagType): boolean => { - return doAccessSync(path, mode, flag) - }, path, mode, flag); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - }); +export interface ListFileOptions { + recursion?: boolean; + listNum?: number; + filter?: Filter; } -function accessSync(path: string, mode?: AccessModeType): boolean { - return doAccessSync(path, mode) +export interface ReadOptions { + offset?: number; + length?: number; } -function accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { - return doAccessSync(path, mode, flag) +export interface ReadTextOptions extends ReadOptions { + encoding?: string; } -native function doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; - -native function close(file: number | File): Promise; - -native function close(file: number | File, callback: AsyncCallback): void; +export interface WriteOptions { + offset?: long; + length?: long; + encoding?: string; +} -native function closeSync(file: number | File): void; +enum AccessModeType { + EXIST = 0, + WRITE = 2, + READ = 4, + READ_WRITE = 6, +} -native function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise; +enum AccessFlagType { + LOCAL = 0, +} -native function copy(srcUri: string, destUri: string, callback: AsyncCallback): void; +interface File { + fd: int; + path: String; + name: String; -native function copy(srcUri: string, destUri: string, options: CopyOptions, callback: AsyncCallback): void; + getParent(): String; + lock(exclusive?: boolean): void; + tryLock(exclusive?: boolean): void; + unlock(): void; +} -native function copyDir(src: string, dest: string, mode?: number): Promise; +class FileInner implements File { + fd: int = -1; + path: String = ""; + name: String = ""; -native function copyDir(src: string, dest: string, callback: AsyncCallback): void; + private nativePtr: long = 0; -native function copyDir(src: string, dest: string, mode: number, callback: AsyncCallback): void; -native function copyDirSync(src: string, dest: string, mode?: number): void; + constructor(ptr: long) { + if (this.nativePtr == 0) { + this.nativePtr = ptr; + } + } -native function copyFile(src: string | number, dest: string | number, mode?: number): Promise; + native getParent(): String; + native lock(exclusive?: boolean): void; + native tryLock(exclusive?: boolean): void; + native unlock(): void; -native function copyFile(src: string | number, dest: string | number, callback: AsyncCallback): void; +} -native function copyFile( - src: string | number, - dest: string | number, - mode: number, - callback: AsyncCallback -): void; +enum LocationType { + LOCAl = 1, + CLOUD = 2 +} -native function copyFileSync(src: string | number, dest: string | number, mode?: number): void; +interface Stat { + ino: bigint; + mode: number; + uid: number; + gid: number; + size: number; + atime: number; + mtime: number; + ctime: number; + atimeNs: bigint; + mtimeNs: bigint; + ctimeNs: bigint; + location: LocationType; -native function createStream(path: string, mode: string): Promise; + isBlockDevice(): boolean; + isCharacterDevice(): boolean; + isDirectory(): boolean; + isFIFO(): boolean; + isFile(): boolean; + isSocket(): boolean; + isSymbolicLink(): boolean; +} -native function createStream(path: string, mode: string, callback: AsyncCallback): void; +class StatInner implements Stat { + ino: bigint = 0n; + mode: number; + uid: number; + gid: number; + size: number; + atime: number; + mtime: number; + ctime: number; + atimeNs: bigint = 0n; + mtimeNs: bigint = 0n; + ctimeNs: bigint = 0n; + location: LocationType; -native function createStreamSync(path: string, mode: string): Stream; + private nativeStat: long = 0; + constructor(stat: long) { + if (this.nativeStat == 0) { + this.nativeStat = stat; + } + } + + native isBlockDevice(): boolean; + native isCharacterDevice(): boolean; + native isDirectory(): boolean; + native isFIFO(): boolean; + native isFile(): boolean; + native isSocket(): boolean; + native isSymbolicLink(): boolean; +} -native function createRandomAccessFile(file: string | File, mode?: number, - options?: RandomAccessFileOptions): Promise; +type FdOrFile = int | File; +type PathOrFd = string | int; +type BufferType = string | ArrayBuffer; -native function createRandomAccessFile(file: string | File, callback: AsyncCallback): void; +class fileIo { -native function createRandomAccessFile(file: string | File, mode: number, callback: AsyncCallback): void; + static { + loadLibrary("ani_fs_class.z"); + } -native function createRandomAccessFileSync(file: string | File, mode?: number, - options?: RandomAccessFileOptions): RandomAccessFile; + static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; -native function createReadStream(path: string, options?: ReadStreamOptions): ReadStream; + static accessSync(path: string, mode?: AccessModeType): boolean { + return fileIo.doAccessSync(path, mode); + } -native function createWriteStream(path: string, options?: WriteStreamOptions): WriteStream; + static accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { + return fileIo.doAccessSync(path, mode, flag); + } -native function createWatcher(path: string, events: number, listener: WatchEventListener): Watcher; + static accessSync1(path: string): boolean { + return fileIo.accessSync(path); + } -native function dup(fd: number): File; + static accessSync2(path: string, mode: AccessModeType): boolean { + return fileIo.accessSync(path, mode); + } -native function fdatasync(fd: number): Promise; + static accessSync3(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { + return fileIo.accessSync(path, mode, flag); + } -native function fdatasync(fd: number, callback: AsyncCallback): void; + static access(path: string, mode?: AccessModeType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + if (mode === undefined) { + let promise = taskpool.execute(fileIo.accessSync1, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } else { + let promise = taskpool.execute(fileIo.accessSync2, path, mode); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } + }); + } -native function fdatasyncSync(fd: number): void; + static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function fdopenStream(fd: number, mode: string): Promise; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + }) + } -native function fdopenStream(fd: number, mode: string, callback: AsyncCallback): void; + static access(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.accessSync1, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); -native function fdopenStreamSync(fd: number, mode: string): Stream; + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let result = ret as boolean; + callback(err, result); + } + }); + } -native function fsync(fd: number): Promise; + static native closeSync(file: FdOrFile): int; -native function fsync(fd: number, callback: AsyncCallback): void; + static close(file: FdOrFile): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.closeSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function fsyncSync(fd: number): void; + reject(err); + } else { + let r = ret as int; + resolve(r); + } + }); + }); + } -function listFile( - path: string, - options?: ListFileOptions -): Promise { - return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute((path: string, options?: ListFileOptions): string[] => { - return listFileSync(path, options); - }, path, options); + static close(file: FdOrFile, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.closeSync, file); promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); - err.code = -1; - reject(err); + err.code = -1 + callback(err, undefined) } else { - let r = ret as string[]; - resolve(r); + err.code = 0 + let r = ret as int; + callback(err, r); } }); - }); -} + } -function listFile(path: string, callback: AsyncCallback): void { - let p1 = taskpool.execute((path: string): string[] => { - return listFileSync(path); - }, path); - p1.then((ret: NullishType) => { - let err = new BusinessErrorInner(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, []); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); -} + static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; -function listFile( - path: string, - options: ListFileOptions, - callback: AsyncCallback -): void { - let p1 = taskpool.execute((path: string, options: ListFileOptions): string[] => { - return listFileSync(path, options); - }, path, options); - p1.then((ret: NullishType) => { - let err = new BusinessErrorInner(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, []); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); -} + static native listFileSync(path: string, options?: ListFileOptions): string[]; + + static listFileSync1(path: string): string[] { + return fileIo.listFileSync(path); + } -native function listFileSync( - path: string, - options?: ListFileOptions -): string[]; + static listFileSync2(path: string, options: ListFileOptions): string[] { + return fileIo.listFileSync(path, options); + } -native function lseek(fd: number, offset: number, whence?: WhenceType): number; + static listFile(path: string): Promise { + return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.listFileSync1, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function lstat(path: string): Promise; + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + }); + } -native function lstat(path: string, callback: AsyncCallback): void; + static listFile(path: string, options: ListFileOptions): Promise { + return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.listFileSync2, path, options); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function lstatSync(path: string): Stat; + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + }); + } -function mkdir(path: string): Promise { - let pvoid: Promise = new Promise((resolve: (value: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): void => mkdirSync(path), path); + static listFile(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.listFileSync1, path); promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); err.code = -1; - reject(err); + callback(err, undefined); } else { - resolve(undefined); + err.code = 0; + let r = ret as string[]; + callback(err, r); } }); - }); - return pvoid -} + } -function mkdir(path: string, recursion: boolean): Promise { - let pvoid: Promise = new Promise((resolve: (value: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string, recursion: boolean): void => mkdirSync(path, recursion), - path, recursion); + static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.listFileSync2, path, options); promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); err.code = -1; - reject(err); + callback(err, undefined); } else { - resolve(undefined); + err.code = 0; + let r = ret as string[]; + callback(err, r); } }); - }); - return pvoid -} + } -function mkdir(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): void => mkdirSync(path), path); - promise.then((ret: NullishType) => { - let err = new BusinessErrorInner(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - callback(err, undefined); - } - }); -} + static native mkdirSync(path: string): int; -function mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, recursion: boolean): void => mkdirSync(path, recursion), - path, recursion); - promise.then((ret: NullishType) => { - let err = new BusinessErrorInner(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - callback(err, undefined); - } - }); -} + static native mkdirSync(path: string, recursion: boolean): int; -native function mkdirSync(path: string): void; + static mkdir(path: string): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); + let promise = taskpool.execute(mkdirSyncWrapper, path); + promise.then((ret: NullishType) => { + let result = ret as int + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function mkdirSync(path: string, recursion: boolean): void; + reject(err); + } else { + resolve(result); + } + }); + }); + } -native function mkdtemp(prefix: string): Promise; + static mkdir(path: string, callback: AsyncCallback): void { + const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); + let promise = taskpool.execute(mkdirSyncWrapper, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as int; + callback(err, r); + } + }); + } -native function mkdtemp(prefix: string, callback: AsyncCallback): void; + static mkdir(path: string, recursion: boolean): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + const mkdirSyncWrapper = (path: string, recursion: boolean) => fileIo.mkdirSync(path, recursion); + let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); + promise.then((ret: NullishType) => { + let result = ret as int + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function mkdtempSync(prefix: string): string; + reject(err); + } else { + resolve(result); + } + }); + }); + } -native function moveDir(src: string, dest: string, mode?: number): Promise; + static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { + const mkdirSyncWrapper = (path: string, recursion: boolean,) => fileIo.mkdirSync(path, recursion); + let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as int; + callback(err, r); + } + }); + } -native function moveDir(src: string, dest: string, callback: AsyncCallback): void; + static native moveFileSync(src: String, dest: String, mode?: int): void; -native function moveDir(src: string, dest: string, mode: number, callback: AsyncCallback): void; + static native openSync(path: String, mode?: int): File; -native function moveDirSync(src: string, dest: string, mode?: number): void; + static openSync1(path: String, mode: int): File { + return fileIo.openSync(path, mode); + } -native function moveFile(src: string, dest: string, mode?: number): Promise; + static openSync2(path: String): File { + return fileIo.openSync(path); + } -native function moveFile(src: string, dest: string, callback: AsyncCallback): void; + static open(path: String, mode: int): Promise { + return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.openSync1, path, mode); + promise.then((ret: NullishType) => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } -native function moveFile(src: string, dest: string, mode: number, callback: AsyncCallback): void; + static open(path: String): Promise { + return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.openSync2, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + err.code = -1; + reject(err); + } else { + let r = ret as File; + resolve(r); + } + }); + }); + } -native function moveFileSync(src: string, dest: string, mode?: number): void; + static open(path: String, mode: int, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.openSync1, path, mode); + promise.then((ret: NullishType) => { + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); + }); + } -function open(path: string, mode?: number): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(openSync, path, mode); + static open(path: String, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { - let err = new BusinessErrorInner(); err.code = -1; - reject(err); + callback(err, undefined); } else { + err.code = 0; let r = ret as File; - resolve(r); + callback(err, r); } }); - }); -} - -native function open(path: string, callback: AsyncCallback): void; - -native function open(path: string, mode: number, callback: AsyncCallback): void; - -native function openSync(path: string, mode?: number): File; - -native function read( - fd: number, - buffer: ArrayBuffer, - options?: ReadOptions -): Promise; + } -native function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void; + static native readSync(fd: int, buffer: ArrayBuffer, options?: ReadOptions): long; -native function read( - fd: number, - buffer: ArrayBuffer, - options: ReadOptions, - callback: AsyncCallback -): void; + static readSync1(fd: int, buffer: ArrayBuffer): long { + return fileIo.readSync(fd, buffer); + } -native function readSync( - fd: number, - buffer: ArrayBuffer, - options?: ReadOptions -): number; + static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { + return fileIo.readSync(fd, buffer, options); + } -native function readLines(filePath: string, options?: Options): Promise; + static read(fd: int, buffer: ArrayBuffer): Promise { + return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function readLines(filePath: string, callback: AsyncCallback): void; + reject(err); + } else { + let r = ret as long; + resolve(r); + } + }); + }); + } -native function readLines(filePath: string, options: Options, callback: AsyncCallback): void; + static read(fd: int, buffer: ArrayBuffer, options: ReadOptions): Promise { + return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function readLinesSync(filePath: string, options?: Options): ReaderIterator; - -native function readText( - filePath: string, - options?: ReadTextOptions -): Promise; - -native function readText(filePath: string, callback: AsyncCallback): void; - -native function readText( - filePath: string, - options: ReadTextOptions, - callback: AsyncCallback -): void; - -native function readTextSync( - filePath: string, - options?: ReadTextOptions -): string; - -native function rename(oldPath: string, newPath: string): Promise; - -native function rename(oldPath: string, newPath: string, callback: AsyncCallback): void; - -native function renameSync(oldPath: string, newPath: string): void; - -native function rmdir(path: string): Promise; - -native function rmdir(path: string, callback: AsyncCallback): void; + reject(err); + } else { + let r = ret as long; + resolve(r); + } + }); + }); + } -native function rmdirSync(path: string): void; + static read(fd: int, buffer: ArrayBuffer, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as long; + callback(err, r); + } + }); + } -native function stat(file: string | number): Promise; + static read(fd: int, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as long; + callback(err, r); + } + }); + } -native function stat(file: string | number, callback: AsyncCallback): void; + static native readTextSync(filePath: string, options?: ReadTextOptions): string; -native function statSync(file: string | number): Stat; + static readTextSync1(filePath: string): string { + return fileIo.readTextSync(filePath); + } -native function symlink(target: string, srcPath: string): Promise; + static readTextSync2(filePath: string, options: ReadTextOptions): string { + return fileIo.readTextSync(filePath, options); + } -native function symlink(target: string, srcPath: string, callback: AsyncCallback): void; + static readText(filePath: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.readTextSync1, filePath); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function symlinkSync(target: string, srcPath: string): void; + reject(err); + } else { + let r = ret as string; + resolve(r); + } + }); + }); + } -native function truncate(file: string | number, len?: number): Promise; + static readText(filePath: string, options: ReadTextOptions): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function truncate(file: string | number, callback: AsyncCallback): void; + reject(err); + } else { + let r = ret as string; + resolve(r); + } + }); + }); + } -native function truncate(file: string | number, len: number, callback: AsyncCallback): void; + static readText(filePath: string, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.readTextSync1, filePath); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string; + callback(err, r); + } + }); + } -native function truncateSync(file: string | number, len?: number): void; + static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string; + callback(err, r); + } + }); + } -native function unlink(path: string): Promise; + static native rmdirSync(path: string): void; -native function unlink(path: string, callback: AsyncCallback): void; + static native statSync(file: PathOrFd): Stat; -native function unlinkSync(path: string): void; + static stat(file: PathOrFd): Promise { + return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.statSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function utimes(path: string, mtime: number): void; + reject(err); + } else { + let r = ret as Stat; + resolve(r); + } + }); + }); + } -native function write( - fd: number, - buffer: ArrayBuffer | string, - options?: WriteOptions -): Promise; + static stat(file: PathOrFd, callback: AsyncCallback): void { + let p = taskpool.execute(fileIo.statSync, file); + p.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as Stat; + callback(err, r); + } + }); + } -native function write(fd: number, buffer: ArrayBuffer | string, callback: AsyncCallback): void; + static native truncateSync(file: PathOrFd, len?: long): void; -native function write( - fd: number, - buffer: ArrayBuffer | string, - options: WriteOptions, - callback: AsyncCallback -): void; + static native unlinkSync(path: string): int; -native function writeSync( - fd: number, - buffer: ArrayBuffer | string, - options?: WriteOptions -): number; + static unlink(path: string): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.unlinkSync, path); + promise.then((ret: NullishType) => { + let result = ret as int + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function connectDfs(networkId: string, listeners: DfsListeners): Promise; + reject(err); + } else { + resolve(result); + } + }); + }); + } -native function disconnectDfs(networkId: string): Promise; + static unlink(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.unlinkSync, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as int; + callback(err, r); + } + }); + } -native function setxattr(path: string, key: string, value: string): Promise; + static native writeSync(fd: int, buffer: BufferType, options?: WriteOptions): long; + static writeSync1(fd: int, buffer: BufferType, options: WriteOptions): long { + return fileIo.writeSync(fd, buffer, options); + } -native function setxattrSync(path: string, key: string, value: string): void; + static writeSync2(fd: int, buffer: BufferType): long { + return fileIo.writeSync(fd, buffer); + } -native function getxattr(path: string, key: string): Promise; + static write(fd: int, buffer: BufferType, options: WriteOptions): Promise { + return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); + promise.then((ret: NullishType) => { + let result = ret as long + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); -native function getxattrSync(path: string, key: string): string; + reject(err); + } else { + resolve(result); + } + }); + }); + } -interface Progress { - readonly processedSize: number; + static write(fd: int, buffer: BufferType): Promise { + return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); + promise.then((ret: NullishType) => { + let result = ret as long + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); - readonly totalSize: number; -} + reject(err); + } else { + resolve(result); + } + }); + }); + } -export class TaskSignal { - native cancel(): void; + static write(fd: int, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as long; + callback(err, r); + } + }); + } - onCancel(): Promise { - return Promise.resolve("直接返回已解决的 Promise") + static write(fd: int, buffer: BufferType, callback: AsyncCallback): void { + let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as long; + callback(err, r); + } + }); } } -interface CopyOptions { - progressListener?: ProgressListener; - copySignal?: TaskSignal; +function accessSyncTest1() { + console.println("accessSyncTest1 begin"); + try { + let ret = fileIo.accessSync("/data/local/tmp/a.txt"); + console.println(`accessSync result : ${ret}`); + } catch (error) { + console.error("accessSyncTest1 Error!", error); + } + console.println("accessSyncTest1 end"); } -type ProgressListener = (progress: Progress) => void; - -interface File { - readonly fd: number; - - readonly path: string; - - readonly name: string; - - getParent(): string; - - lock(exclusive?: boolean): Promise; - - lock(callback: AsyncCallback): void; - - lock(exclusive: boolean, callback: AsyncCallback): void; - - tryLock(exclusive?: boolean): void; - - unlock(): void; +function closeSyncTest1() { + console.println("closeSyncTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let ret = fileIo.closeSync(file.fd); + console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest1 Error!", error); + } + console.println("closeSyncTest1 end"); } -class FileInner implements File { - readonly fd: number; - - readonly path: string = ""; - - readonly name: string = ""; - - private nativePtr: long = 0; - - constructor(ptr: long) { - if (this.nativePtr == 0) { - this.nativePtr = ptr; - } +function closeSyncTest2() { + console.println("closeSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let ret = fileIo.closeSync(file); + console.println(`close file by file, fd=${file.fd}, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest2 Error!", error); } - - native getParent(): string; - - native lock(exclusive?: boolean): Promise; - - native lock(callback: AsyncCallback): void; - - native lock(exclusive: boolean, callback: AsyncCallback): void; - - native tryLock(exclusive?: boolean): void; - - native unlock(): void; + console.println("closeSyncTest2 end"); } - -interface RandomAccessFile { - - readonly fd: number; - - readonly filePointer: number; - - setFilePointer(filePointer: number): void; - - close(): void; - - write( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): Promise; - - write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; - - write( - buffer: ArrayBuffer | string, - options: WriteOptions, - callback: AsyncCallback - ): void; - - writeSync( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): number; - - read( - buffer: ArrayBuffer, - options?: ReadOptions - ): Promise; - - read(buffer: ArrayBuffer, callback: AsyncCallback): void; - - read( - buffer: ArrayBuffer, - options: ReadOptions, - callback: AsyncCallback - ): void; - - readSync( - buffer: ArrayBuffer, - options?: ReadOptions - ): number; - - getReadStream(): ReadStream; - - getWriteStream(): WriteStream; +function closeSyncTest3() { + console.println("closeSyncTest3 begin"); + try { + let ret = fileIo.closeSync(-1); + console.println(`close file by invalid fd: -1, ret=${ret}`); + } catch (error) { + console.error("closeSyncTest3 Error!", error); + } + console.println("closeSyncTest3 end"); } -class RandomAccessFileInner implements RandomAccessFile { - readonly fd: number; - - readonly filePointer: number; - - native setFilePointer(filePointer: number): void; - - native close(): void; - - native write( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): Promise; - - native write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; - - native write( - buffer: ArrayBuffer | string, - options: WriteOptions, - callback: AsyncCallback - ): void; - - native writeSync( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): number; - - native read( - buffer: ArrayBuffer, - options?: ReadOptions - ): Promise; - - native read(buffer: ArrayBuffer, callback: AsyncCallback): void; - - native read( - buffer: ArrayBuffer, - options: ReadOptions, - callback: AsyncCallback - ): void; - - native readSync( - buffer: ArrayBuffer, - options?: ReadOptions - ): number; - - native getReadStream(): ReadStream; - - native getWriteStream(): WriteStream; +function closePromiseTest() { + console.println("closePromiseTest begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.close(file.fd).then((result: int) => { + console.println(`closePromiseTest close result: ${result}`); + }); + } catch (error) { + console.error("closePromiseTest Error:", error); + } + console.println("closePromiseTest end"); } -class ReadStream extends stream.Readable { - native constructor(); - - readonly bytesRead: number; - - readonly path: string; - - native seek(offset: number, whence?: WhenceType): number; - - native close(): void; +function closeCallbackTest() { + console.println("closeCallbackTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.close(file, (err: BusinessError, data?: int) => { + if (err.code) { + console.error("closeCallbackTest: close Error:", err); + } else { + console.log("closeCallbackTest: close result:", data); + } + }); + console.println("closeCallbackTest end"); } -class WriteStream extends stream.Writable { - native constructor(); - - readonly bytesWritten: number; - - readonly path: string; - - native seek(offset: number, whence?: WhenceType): number; - - native close(): void; +function copyFileTest() { + console.println("copyFileTest begin"); + try { + fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); + } catch (error) { + console.error("copyFileTest Error!", error); + } + console.println("copyFileTest end"); } -interface Stat { - readonly ino: bigint; - readonly mode: number; - readonly uid: number; - readonly gid: number; - readonly size: number; - readonly atime: number; - readonly mtime: number; - readonly ctime: number; - readonly location: LocationType; - - isBlockDevice(): boolean; - isCharacterDevice(): boolean; - isDirectory(): boolean; - isFIFO(): boolean; - isFile(): boolean; - isSocket(): boolean; - isSymbolicLink(): boolean; +function fileTest() { + console.println("fileTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt"); + let fd = file.fd; + console.println(`open file, fd=${fd}`); + let parent = file.getParent(); + console.println(`file parent=${parent}`); + file.lock(); + console.println(`file lock completed`); + file.unlock(); + console.println(`file unlock completed`); + file.tryLock(true); + console.println(`file tryLock completed`); + file.unlock(); + console.println(`file unlock completed`); + console.println("fileTest end"); } -class StatInner implements Stat { - readonly ino: bigint = 0n; - readonly mode: number; - readonly uid: number; - readonly gid: number; - readonly size: number; - readonly atime: number; - readonly mtime: number; - readonly ctime: number; - readonly location: LocationType; - - private nativeStat: long = 0; - - constructor(stat: long) { - if (this.nativeStat == 0) { - this.nativeStat = stat; +function listFileTest() { + console.println("listFileTest begin"); + try { + let files = fileIo.listFileSync("/data/local/tmp/"); + for (const file of files) { + console.println(file); } + } catch (error) { + console.error("listFileTest Error!", error); } - - native isBlockDevice(): boolean; - native isCharacterDevice(): boolean; - native isDirectory(): boolean; - native isFIFO(): boolean; - native isFile(): boolean; - native isSocket(): boolean; - native isSymbolicLink(): boolean; -} - - -interface Stream { - close(): Promise; - - close(callback: AsyncCallback): void; - - closeSync(): void; - - flush(): Promise; - - flush(callback: AsyncCallback): void; - - flushSync(): void; - - write( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): Promise; - - write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; - - write( - buffer: ArrayBuffer | string, - options: WriteOptions, - callback: AsyncCallback - ): void; - - writeSync( - buffer: ArrayBuffer | string, - options?: WriteOptions - ): number; - - read( - buffer: ArrayBuffer, - options?: ReadOptions - ): Promise; - - read(buffer: ArrayBuffer, callback: AsyncCallback): void; - - read( - buffer: ArrayBuffer, - options: ReadOptions, - callback: AsyncCallback - ): void; - - readSync( - buffer: ArrayBuffer, - options?: ReadOptions - ): number; + console.println("listFileTest end"); } -class StreamInner implements Stream { - native close(): Promise; - - native close(callback: AsyncCallback): void; - - native closeSync(): void; - - native flush(): Promise; - - native flush(callback: AsyncCallback): void; - - native flushSync(): void; - - native write( - buffer: ArrayBuffer | string, - options?: WriteOptions -): Promise; - - native write(buffer: ArrayBuffer | string, callback: AsyncCallback): void; - - native write( - buffer: ArrayBuffer | string, - options: WriteOptions, - callback: AsyncCallback -): void; - - native writeSync( - buffer: ArrayBuffer | string, - options?: WriteOptions -): number; - - native read( - buffer: ArrayBuffer, - options?: ReadOptions -): Promise; - - native read(buffer: ArrayBuffer, callback: AsyncCallback): void; - - native read( - buffer: ArrayBuffer, - options: ReadOptions, - callback: AsyncCallback -): void; - - native readSync( - buffer: ArrayBuffer, - options?: ReadOptions -): number; -} - -export interface WatchEventListener { - foo(event: WatchEvent): void; -} - - -export interface WatchEvent { - readonly fileName: string; - - readonly event: number; - - readonly cookie: number; -} - -export interface Watcher { - start(): void; - - stop(): void; +function mkdirTest1() { + console.println("mkdirTest1 begin") + try { + const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); + console.println(`mkdir result: ${ret}`); + } catch (error) { + console.error("mkdirTest1 Error!", error); + } + console.println("mkdirTest1 end"); } -export interface ReaderIteratorResult { - done: boolean; - - value: string; +function moveSyncTest() { + console.println("moveSyncTest begin") + fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) + console.println("moveSyncTest end") } -declare interface ReaderIterator { - next(): ReaderIteratorResult; +function openSyncTest() { + console.println("openSyncTest begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + let mode = 2; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openSyncTest Error!", error); + } + console.println("openSyncTest end"); } -class ReaderIteratorInner implements ReaderIterator { - native next(): ReaderIteratorResult; +function readSyncTest1() { + console.println("readSyncTest1 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let length = fileIo.readSync(fd, buffer); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest1 Error!", error); + } + console.println("readSyncTest1 end") } -export interface Filter { - suffix?: Array; - displayName?: Array; - mimeType?: Array; - fileSizeOver?: number; - lastModifiedAfter?: number; - excludeMedia?: boolean; +function readSyncTest2() { + console.println("readSyncTest2 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest2 Error!", error); + } + console.println("readSyncTest2 end") } -export interface ConflictFiles { - srcFile: string; - - destFile: string; +function readSyncTest3() { + console.println("readSyncTest3 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + offset: 3, + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest3 Error!", error); + } + console.println("readSyncTest3 end") } -export interface Options { - encoding?: string; +function readTextSyncTest1() { + console.println("readTextSyncTest1 begin") + try { + let content = fileIo.readTextSync("/data/local/tmp/a.txt"); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readTextSyncTest1 Error!", error); + } + console.println("readTextSyncTest1 end") } -export interface ReadOptions { - offset?: number; - length?: number; +function readTextSyncTest2() { + console.println("readTextSyncTest2 begin"); + try { + let options: ReadTextOptions = { + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest2 Error!", error); + } + console.println("readTextSyncTest2 end"); } -export interface ReadTextOptions extends ReadOptions { - encoding?: string; +function readTextSyncTest3() { + console.println("readTextSyncTest3 begin") + try { + let options: ReadTextOptions = { + offset: 3, + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest3 Error!", error); + } + console.println("readTextSyncTest3 end"); } -export interface WriteOptions extends Options { - offset?: number; - length?: number; +function rmdirSyncTest() { + console.println("rmdirSyncTest begin"); + try { + fileIo.rmdirSync("/data/local/tmp/dir01"); + } catch (error) { + console.error("rmdirSyncTest Error!", error); + } + console.println("rmdirSyncTest end"); } -export interface ListFileOptions { - recursion?: boolean; - - listNum?: number; - - filter?: Filter; +function printStatInfo(stat?: Stat) { + if (stat === undefined) { + console.error("stat is null or undefined!"); + return; + } + console.info("stat, ino is " + stat.ino); + console.info("stat, mode is " + stat.mode); + console.info("stat, uid is " + stat.uid); + console.info("stat, gid is " + stat.gid); + console.info("stat, size is " + stat.size); + console.info("stat, atime is " + stat.atime); + console.info("stat, mtime is " + stat.mtime); + console.info("stat, ctime is " + stat.ctime); + console.info("stat, atimeNs is " + stat.atimeNs); + console.info("stat, mtimeNs is " + stat.mtimeNs); + console.info("stat, ctimeNs is " + stat.ctimeNs); + console.info("stat, location is " + stat.location); + console.info("stat, isBlockDevice is " + stat.isBlockDevice()); + console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); + console.info("stat, isDirectory is " + stat.isDirectory()); + console.info("stat, isFIFO is " + stat.isFIFO()); + console.info("stat, isFile is " + stat.isFile()); + console.info("stat, isSocket is " + stat.isSocket()); + console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); } -export interface RandomAccessFileOptions { - start?: number; - - end?: number; +function statSyncTest1() { + console.println("statSyncTest1 begin"); + try { + let stat = fileIo.statSync("/data/local/tmp/a.txt"); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest1 Error!", error); + } + console.println("statSyncTest1 end"); } -export interface ReadStreamOptions { - start?: number; - - end?: number; +function statSyncTest2() { + console.println("statSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file fd: ${file.fd}`); + let stat = fileIo.statSync(file.fd); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest2 Error!", error); + } + console.println("statSyncTest2 end"); } -export interface WriteStreamOptions { - mode?: number; - start?: number; +function statPromiseTest() { + console.println("statPromiseTest start"); + try { + let path = "/data/local/tmp/a.txt" + let file = fileIo.openSync(path); + console.println(`open file fd: ${file.fd}`); + fileIo.stat(path).then((result: Stat) => { + console.log("statPromiseTest success"); + printStatInfo(result); + }); + } catch (error) { + console.error("statPromiseTest Error!", error); + } + console.println("statPromiseTest end"); } -export interface DfsListeners { - onStatus(networkId: string, status: number): void; +function statCallbackTest() { + console.println("statCallbackTest start"); + let path = "/data/local/tmp/a.txt" + fileIo.stat(path, (err: BusinessError, data?: Stat) => { + if (data === undefined || data === null) { + console.error("Callback: Error stat:", err); + } else { + console.log("Callback stat success"); + printStatInfo(data); + } + }); + console.println("statCallbackTest end"); } -export interface BusinessError { - message: string; - code: number; - data?: T; +function truncateSyncTest1() { + console.println("truncateSyncTest1 begin"); + try { + fileIo.truncateSync(-1, 4); + } catch (error) { + console.error("truncateSyncTest1 Error!", error); + } + console.println("truncateSyncTest1 end"); } -class BusinessErrorInner implements BusinessError { - name: string = "BusinessError"; - message: string = ""; - code: number = 0; - data?: T; - stack?: string; +function truncateSyncTest2() { + console.println("truncateSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file, fd=${file.fd}`); + fileIo.truncateSync(file.fd, 4); + } catch (error) { + console.error("truncateSyncTest2 Error!", error); + } + console.println("truncateSyncTest2 end"); } -declare enum WhenceType { - SEEK_SET = 0, - - SEEK_CUR = 1, - - SEEK_END = 2, +function truncateSyncTest3() { + console.println("truncateSyncTest3 begin"); + try { + fileIo.truncateSync("/data/local/tmp/b.txt", 4); + } catch (error) { + console.error("truncateSyncTest3 Error!", error); + } + console.println("truncateSyncTest3 end"); } -declare enum LocationType { - LOCAL = 1 << 0, +function unlinkSyncTest() { + console.println("unlinkSyncTest begin"); + try { + const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); + console.println(`unlink result: ${ret}`); - CLOUD = 1 << 1, + } catch (error) { + console.error("unlinkSyncTest Error!", error); + } + console.println("unlinkSyncTest end"); } -declare enum AccessModeType { - EXIST = 0, - - WRITE = 2, - - READ = 4, - - READ_WRITE = 6, +function writeSyncTest1() { + console.println("writeSyncTest1 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + encoding: "utf-8", + }; + let length = fileIo.writeSync(fd, "hello,world!", options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest1 Error!", error); + } + console.println("writeSyncTest1 end"); } -declare enum AccessFlagType { - LOCAL = 0, +function writeSyncTest2() { + console.println("writeSyncTest2 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + offset: 100, + }; + let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest2 Error!", error); + } + console.println("writeSyncTest2 end"); } function errorHandlerTest1() { console.println("errorHandlerTest1 begin"); try { - let file = openSync("/data/local/tmp/a.txt"); + let file = fileIo.openSync("/data/local/tmp/a.txt"); console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); console.println("\n"); console.println("try open file notexist.txt ..."); let mode = 2; - file = openSync("/data/local/tmp/notexist.txt", mode); + file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); } catch (error) { console.error("errorHandlerTest1 Error!", error); @@ -1060,14 +1237,91 @@ function errorHandlerTest1() { console.println("errorHandlerTest1 end"); } -function main(): void { - console.println("---------- hello ani --------------"); - loadLibrary("ani_fs_class"); - console.info("hello world") - let result = accessSync("/data") - console.info(`access: ${result}`) +function errorHandlerTest2() { + console.println("errorHandlerTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println("try open file a.txt ..."); + let mode = -1; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest2 Error!", error); + } + console.println("errorHandlerTest2 end"); +} - // errorHandlerTest1(); +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let file = await fileIo.open("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + console.println("-----------"); + console.println("try open file a.txt ..."); + let mode = -1; + file = await fileIo.open("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { + console.println("2)-----------"); + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + // accessSyncTest1(); + // openSyncTest(); + // closeSyncTest1(); + // closeSyncTest2(); + // closeSyncTest3(); + // closePromiseTest(); + // closeCallbackTest(); + // copyFileTest(); + // fileTest(); + // listFileTest(); + // mkdirTest1(); + // moveSyncTest(); + // readSyncTest1(); + // readSyncTest2(); + // readSyncTest3(); + // readTextSyncTest1(); + // readTextSyncTest2(); + // readTextSyncTest3(); + // rmdirSyncTest(); + // statSyncTest1(); + // statSyncTest2(); + // statPromiseTest(); + // statCallbackTest(); + // truncateSyncTest1(); + // truncateSyncTest2(); + // truncateSyncTest3(); + // unlinkSyncTest(); + // writeSyncTest1(); + // writeSyncTest2(); + // errorHandlerTest1(); // 打开文件失败,文件不存在 + errorHandlerTest2(); // 参数校验失败 + // errorHandlerTest3(); // 异步Promise + // errorHandlerTest4(); // 异步callback console.println("---------- hello ani end ---------------"); } \ No newline at end of file -- Gitee From 9fe619696c1465f43751850d7e001edda8bd3ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 02:49:53 +0800 Subject: [PATCH 10/76] =?UTF-8?q?framework=E5=8C=96=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I6955447eef6cf7bdfac652333f183824aef17fed --- .../js/src/mod_fs/ani/bind_function_class.cpp | 30 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 982 +++++++++--------- .../js/src/mod_fs/class_stat/ani/stat_ani.cpp | 2 +- .../js/src/mod_fs/properties/ani/open_ani.cpp | 14 +- 4 files changed, 509 insertions(+), 519 deletions(-) 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 cf0c66c4..f05e7a52 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 @@ -70,24 +70,24 @@ static ani_status BindStatClassMethods(ani_env *env) static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/ETSGLOBAL;"; + static const char *className = "L@ohos/file/fs/fileIo;"; std::array methods = { - // ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, + ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, - // ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, - // ani_native_function { "mkdirSync", "Lstd/core/String;:I", reinterpret_cast(MkdirkAni::MkdirSync0) }, - // ani_native_function { "mkdirSync", "Lstd/core/String;Z:I", reinterpret_cast(MkdirkAni::MkdirSync1) }, - // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, - // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, - // ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, - // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, - // ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, - // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, - // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, - // ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, - // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, + ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + ani_native_function { "mkdirSync", "Lstd/core/String;:I", reinterpret_cast(MkdirkAni::MkdirSync0) }, + ani_native_function { "mkdirSync", "Lstd/core/String;Z:I", reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, + ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, + ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, + ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, className, 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 543c62ba..c2a69abc 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 @@ -105,7 +105,7 @@ class FileInner implements File { } enum LocationType { - LOCAl = 1, + LOCAL = 1, CLOUD = 2 } @@ -170,7 +170,7 @@ type BufferType = string | ArrayBuffer; class fileIo { static { - loadLibrary("ani_fs_class.z"); + loadLibrary("ani_fs_class"); } static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; @@ -800,497 +800,497 @@ function accessSyncTest1() { console.println("accessSyncTest1 end"); } -function closeSyncTest1() { - console.println("closeSyncTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let ret = fileIo.closeSync(file.fd); - console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest1 Error!", error); - } - console.println("closeSyncTest1 end"); -} - -function closeSyncTest2() { - console.println("closeSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let ret = fileIo.closeSync(file); - console.println(`close file by file, fd=${file.fd}, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest2 Error!", error); - } - console.println("closeSyncTest2 end"); -} - -function closeSyncTest3() { - console.println("closeSyncTest3 begin"); - try { - let ret = fileIo.closeSync(-1); - console.println(`close file by invalid fd: -1, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest3 Error!", error); - } - console.println("closeSyncTest3 end"); -} - -function closePromiseTest() { - console.println("closePromiseTest begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.close(file.fd).then((result: int) => { - console.println(`closePromiseTest close result: ${result}`); - }); - } catch (error) { - console.error("closePromiseTest Error:", error); - } - console.println("closePromiseTest end"); -} - -function closeCallbackTest() { - console.println("closeCallbackTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.close(file, (err: BusinessError, data?: int) => { - if (err.code) { - console.error("closeCallbackTest: close Error:", err); - } else { - console.log("closeCallbackTest: close result:", data); - } - }); - console.println("closeCallbackTest end"); -} - -function copyFileTest() { - console.println("copyFileTest begin"); - try { - fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); - } catch (error) { - console.error("copyFileTest Error!", error); - } - console.println("copyFileTest end"); -} - -function fileTest() { - console.println("fileTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt"); - let fd = file.fd; - console.println(`open file, fd=${fd}`); - let parent = file.getParent(); - console.println(`file parent=${parent}`); - file.lock(); - console.println(`file lock completed`); - file.unlock(); - console.println(`file unlock completed`); - file.tryLock(true); - console.println(`file tryLock completed`); - file.unlock(); - console.println(`file unlock completed`); - console.println("fileTest end"); -} - -function listFileTest() { - console.println("listFileTest begin"); - try { - let files = fileIo.listFileSync("/data/local/tmp/"); - for (const file of files) { - console.println(file); - } - } catch (error) { - console.error("listFileTest Error!", error); - } - console.println("listFileTest end"); -} - -function mkdirTest1() { - console.println("mkdirTest1 begin") - try { - const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); - console.println(`mkdir result: ${ret}`); - } catch (error) { - console.error("mkdirTest1 Error!", error); - } - console.println("mkdirTest1 end"); -} - -function moveSyncTest() { - console.println("moveSyncTest begin") - fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) - console.println("moveSyncTest end") -} - -function openSyncTest() { - console.println("openSyncTest begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let mode = 2; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openSyncTest Error!", error); - } - console.println("openSyncTest end"); -} - -function readSyncTest1() { - console.println("readSyncTest1 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let length = fileIo.readSync(fd, buffer); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest1 Error!", error); - } - console.println("readSyncTest1 end") -} - -function readSyncTest2() { - console.println("readSyncTest2 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest2 Error!", error); - } - console.println("readSyncTest2 end") -} - -function readSyncTest3() { - console.println("readSyncTest3 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - offset: 3, - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest3 Error!", error); - } - console.println("readSyncTest3 end") -} - -function readTextSyncTest1() { - console.println("readTextSyncTest1 begin") - try { - let content = fileIo.readTextSync("/data/local/tmp/a.txt"); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readTextSyncTest1 Error!", error); - } - console.println("readTextSyncTest1 end") -} - -function readTextSyncTest2() { - console.println("readTextSyncTest2 begin"); - try { - let options: ReadTextOptions = { - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest2 Error!", error); - } - console.println("readTextSyncTest2 end"); -} - -function readTextSyncTest3() { - console.println("readTextSyncTest3 begin") - try { - let options: ReadTextOptions = { - offset: 3, - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest3 Error!", error); - } - console.println("readTextSyncTest3 end"); -} - -function rmdirSyncTest() { - console.println("rmdirSyncTest begin"); - try { - fileIo.rmdirSync("/data/local/tmp/dir01"); - } catch (error) { - console.error("rmdirSyncTest Error!", error); - } - console.println("rmdirSyncTest end"); -} - -function printStatInfo(stat?: Stat) { - if (stat === undefined) { - console.error("stat is null or undefined!"); - return; - } - console.info("stat, ino is " + stat.ino); - console.info("stat, mode is " + stat.mode); - console.info("stat, uid is " + stat.uid); - console.info("stat, gid is " + stat.gid); - console.info("stat, size is " + stat.size); - console.info("stat, atime is " + stat.atime); - console.info("stat, mtime is " + stat.mtime); - console.info("stat, ctime is " + stat.ctime); - console.info("stat, atimeNs is " + stat.atimeNs); - console.info("stat, mtimeNs is " + stat.mtimeNs); - console.info("stat, ctimeNs is " + stat.ctimeNs); - console.info("stat, location is " + stat.location); - console.info("stat, isBlockDevice is " + stat.isBlockDevice()); - console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); - console.info("stat, isDirectory is " + stat.isDirectory()); - console.info("stat, isFIFO is " + stat.isFIFO()); - console.info("stat, isFile is " + stat.isFile()); - console.info("stat, isSocket is " + stat.isSocket()); - console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); -} - -function statSyncTest1() { - console.println("statSyncTest1 begin"); - try { - let stat = fileIo.statSync("/data/local/tmp/a.txt"); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest1 Error!", error); - } - console.println("statSyncTest1 end"); -} - -function statSyncTest2() { - console.println("statSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file fd: ${file.fd}`); - let stat = fileIo.statSync(file.fd); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest2 Error!", error); - } - console.println("statSyncTest2 end"); -} - -function statPromiseTest() { - console.println("statPromiseTest start"); - try { - let path = "/data/local/tmp/a.txt" - let file = fileIo.openSync(path); - console.println(`open file fd: ${file.fd}`); - fileIo.stat(path).then((result: Stat) => { - console.log("statPromiseTest success"); - printStatInfo(result); - }); - } catch (error) { - console.error("statPromiseTest Error!", error); - } - console.println("statPromiseTest end"); -} - -function statCallbackTest() { - console.println("statCallbackTest start"); - let path = "/data/local/tmp/a.txt" - fileIo.stat(path, (err: BusinessError, data?: Stat) => { - if (data === undefined || data === null) { - console.error("Callback: Error stat:", err); - } else { - console.log("Callback stat success"); - printStatInfo(data); - } - }); - console.println("statCallbackTest end"); -} - -function truncateSyncTest1() { - console.println("truncateSyncTest1 begin"); - try { - fileIo.truncateSync(-1, 4); - } catch (error) { - console.error("truncateSyncTest1 Error!", error); - } - console.println("truncateSyncTest1 end"); -} - -function truncateSyncTest2() { - console.println("truncateSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file, fd=${file.fd}`); - fileIo.truncateSync(file.fd, 4); - } catch (error) { - console.error("truncateSyncTest2 Error!", error); - } - console.println("truncateSyncTest2 end"); -} - -function truncateSyncTest3() { - console.println("truncateSyncTest3 begin"); - try { - fileIo.truncateSync("/data/local/tmp/b.txt", 4); - } catch (error) { - console.error("truncateSyncTest3 Error!", error); - } - console.println("truncateSyncTest3 end"); -} - -function unlinkSyncTest() { - console.println("unlinkSyncTest begin"); - try { - const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); - console.println(`unlink result: ${ret}`); - - } catch (error) { - console.error("unlinkSyncTest Error!", error); - } - console.println("unlinkSyncTest end"); -} - -function writeSyncTest1() { - console.println("writeSyncTest1 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - encoding: "utf-8", - }; - let length = fileIo.writeSync(fd, "hello,world!", options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest1 Error!", error); - } - console.println("writeSyncTest1 end"); -} - -function writeSyncTest2() { - console.println("writeSyncTest2 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - offset: 100, - }; - let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest2 Error!", error); - } - console.println("writeSyncTest2 end"); -} - -function errorHandlerTest1() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - console.println("\n"); - console.println("try open file notexist.txt ..."); - let mode = 2; - file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); - console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest1 Error!", error); - } - console.println("errorHandlerTest1 end"); -} - -function errorHandlerTest2() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println("try open file a.txt ..."); - let mode = -1; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest2 Error!", error); - } - console.println("errorHandlerTest2 end"); -} - -function errorHandlerTest3() { - console.println("errorHandlerTest3 begin"); - try { - let file = await fileIo.open("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - console.println("-----------"); - console.println("try open file a.txt ..."); - let mode = -1; - file = await fileIo.open("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest3 Error!", error); - } - console.println("errorHandlerTest3 end"); -} - -function errorHandlerTest4() { - console.println("errorHandlerTest4 begin"); - fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - - fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { - console.println("2)-----------"); - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - console.println("errorHandlerTest4 end"); -} +// function closeSyncTest1() { +// console.println("closeSyncTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// let ret = fileIo.closeSync(file.fd); +// console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); +// } catch (error) { +// console.error("closeSyncTest1 Error!", error); +// } +// console.println("closeSyncTest1 end"); +// } + +// function closeSyncTest2() { +// console.println("closeSyncTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// let ret = fileIo.closeSync(file); +// console.println(`close file by file, fd=${file.fd}, ret=${ret}`); +// } catch (error) { +// console.error("closeSyncTest2 Error!", error); +// } +// console.println("closeSyncTest2 end"); +// } + +// function closeSyncTest3() { +// console.println("closeSyncTest3 begin"); +// try { +// let ret = fileIo.closeSync(-1); +// console.println(`close file by invalid fd: -1, ret=${ret}`); +// } catch (error) { +// console.error("closeSyncTest3 Error!", error); +// } +// console.println("closeSyncTest3 end"); +// } + +// function closePromiseTest() { +// console.println("closePromiseTest begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// fileIo.close(file.fd).then((result: int) => { +// console.println(`closePromiseTest close result: ${result}`); +// }); +// } catch (error) { +// console.error("closePromiseTest Error:", error); +// } +// console.println("closePromiseTest end"); +// } + +// function closeCallbackTest() { +// console.println("closeCallbackTest begin"); +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// fileIo.close(file, (err: BusinessError, data?: int) => { +// if (err.code) { +// console.error("closeCallbackTest: close Error:", err); +// } else { +// console.log("closeCallbackTest: close result:", data); +// } +// }); +// console.println("closeCallbackTest end"); +// } + +// function copyFileTest() { +// console.println("copyFileTest begin"); +// try { +// fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); +// } catch (error) { +// console.error("copyFileTest Error!", error); +// } +// console.println("copyFileTest end"); +// } + +// function fileTest() { +// console.println("fileTest begin"); +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let fd = file.fd; +// console.println(`open file, fd=${fd}`); +// let parent = file.getParent(); +// console.println(`file parent=${parent}`); +// file.lock(); +// console.println(`file lock completed`); +// file.unlock(); +// console.println(`file unlock completed`); +// file.tryLock(true); +// console.println(`file tryLock completed`); +// file.unlock(); +// console.println(`file unlock completed`); +// console.println("fileTest end"); +// } + +// function listFileTest() { +// console.println("listFileTest begin"); +// try { +// let files = fileIo.listFileSync("/data/local/tmp/"); +// for (const file of files) { +// console.println(file); +// } +// } catch (error) { +// console.error("listFileTest Error!", error); +// } +// console.println("listFileTest end"); +// } + +// function mkdirTest1() { +// console.println("mkdirTest1 begin") +// try { +// const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); +// console.println(`mkdir result: ${ret}`); +// } catch (error) { +// console.error("mkdirTest1 Error!", error); +// } +// console.println("mkdirTest1 end"); +// } + +// function moveSyncTest() { +// console.println("moveSyncTest begin") +// fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) +// console.println("moveSyncTest end") +// } + +// function openSyncTest() { +// console.println("openSyncTest begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// let mode = 2; +// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("openSyncTest Error!", error); +// } +// console.println("openSyncTest end"); +// } + +// function readSyncTest1() { +// console.println("readSyncTest1 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let length = fileIo.readSync(fd, buffer); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest1 Error!", error); +// } +// console.println("readSyncTest1 end") +// } + +// function readSyncTest2() { +// console.println("readSyncTest2 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let options: ReadOptions = { +// length: 5, +// } +// let length = fileIo.readSync(fd, buffer, options); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest2 Error!", error); +// } +// console.println("readSyncTest2 end") +// } + +// function readSyncTest3() { +// console.println("readSyncTest3 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let options: ReadOptions = { +// offset: 3, +// length: 5, +// } +// let length = fileIo.readSync(fd, buffer, options); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest3 Error!", error); +// } +// console.println("readSyncTest3 end") +// } + +// function readTextSyncTest1() { +// console.println("readTextSyncTest1 begin") +// try { +// let content = fileIo.readTextSync("/data/local/tmp/a.txt"); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readTextSyncTest1 Error!", error); +// } +// console.println("readTextSyncTest1 end") +// } + +// function readTextSyncTest2() { +// console.println("readTextSyncTest2 begin"); +// try { +// let options: ReadTextOptions = { +// length: 5, +// } +// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// console.println("-----------------------------------------------"); +// console.println(content); +// console.println("-----------------------------------------------"); +// } catch (error) { +// console.error("readTextSyncTest2 Error!", error); +// } +// console.println("readTextSyncTest2 end"); +// } + +// function readTextSyncTest3() { +// console.println("readTextSyncTest3 begin") +// try { +// let options: ReadTextOptions = { +// offset: 3, +// length: 5, +// } +// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// console.println("-----------------------------------------------"); +// console.println(content); +// console.println("-----------------------------------------------"); +// } catch (error) { +// console.error("readTextSyncTest3 Error!", error); +// } +// console.println("readTextSyncTest3 end"); +// } + +// function rmdirSyncTest() { +// console.println("rmdirSyncTest begin"); +// try { +// fileIo.rmdirSync("/data/local/tmp/dir01"); +// } catch (error) { +// console.error("rmdirSyncTest Error!", error); +// } +// console.println("rmdirSyncTest end"); +// } + +// function printStatInfo(stat?: Stat) { +// if (stat === undefined) { +// console.error("stat is null or undefined!"); +// return; +// } +// console.info("stat, ino is " + stat.ino); +// console.info("stat, mode is " + stat.mode); +// console.info("stat, uid is " + stat.uid); +// console.info("stat, gid is " + stat.gid); +// console.info("stat, size is " + stat.size); +// console.info("stat, atime is " + stat.atime); +// console.info("stat, mtime is " + stat.mtime); +// console.info("stat, ctime is " + stat.ctime); +// console.info("stat, atimeNs is " + stat.atimeNs); +// console.info("stat, mtimeNs is " + stat.mtimeNs); +// console.info("stat, ctimeNs is " + stat.ctimeNs); +// console.info("stat, location is " + stat.location); +// console.info("stat, isBlockDevice is " + stat.isBlockDevice()); +// console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); +// console.info("stat, isDirectory is " + stat.isDirectory()); +// console.info("stat, isFIFO is " + stat.isFIFO()); +// console.info("stat, isFile is " + stat.isFile()); +// console.info("stat, isSocket is " + stat.isSocket()); +// console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); +// } + +// function statSyncTest1() { +// console.println("statSyncTest1 begin"); +// try { +// let stat = fileIo.statSync("/data/local/tmp/a.txt"); +// printStatInfo(stat); +// } catch (error) { +// console.error("statSyncTest1 Error!", error); +// } +// console.println("statSyncTest1 end"); +// } + +// function statSyncTest2() { +// console.println("statSyncTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file fd: ${file.fd}`); +// let stat = fileIo.statSync(file.fd); +// printStatInfo(stat); +// } catch (error) { +// console.error("statSyncTest2 Error!", error); +// } +// console.println("statSyncTest2 end"); +// } + +// function statPromiseTest() { +// console.println("statPromiseTest start"); +// try { +// let path = "/data/local/tmp/a.txt" +// let file = fileIo.openSync(path); +// console.println(`open file fd: ${file.fd}`); +// fileIo.stat(path).then((result: Stat) => { +// console.log("statPromiseTest success"); +// printStatInfo(result); +// }); +// } catch (error) { +// console.error("statPromiseTest Error!", error); +// } +// console.println("statPromiseTest end"); +// } + +// function statCallbackTest() { +// console.println("statCallbackTest start"); +// let path = "/data/local/tmp/a.txt" +// fileIo.stat(path, (err: BusinessError, data?: Stat) => { +// if (data === undefined || data === null) { +// console.error("Callback: Error stat:", err); +// } else { +// console.log("Callback stat success"); +// printStatInfo(data); +// } +// }); +// console.println("statCallbackTest end"); +// } + +// function truncateSyncTest1() { +// console.println("truncateSyncTest1 begin"); +// try { +// fileIo.truncateSync(-1, 4); +// } catch (error) { +// console.error("truncateSyncTest1 Error!", error); +// } +// console.println("truncateSyncTest1 end"); +// } + +// function truncateSyncTest2() { +// console.println("truncateSyncTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file, fd=${file.fd}`); +// fileIo.truncateSync(file.fd, 4); +// } catch (error) { +// console.error("truncateSyncTest2 Error!", error); +// } +// console.println("truncateSyncTest2 end"); +// } + +// function truncateSyncTest3() { +// console.println("truncateSyncTest3 begin"); +// try { +// fileIo.truncateSync("/data/local/tmp/b.txt", 4); +// } catch (error) { +// console.error("truncateSyncTest3 Error!", error); +// } +// console.println("truncateSyncTest3 end"); +// } + +// function unlinkSyncTest() { +// console.println("unlinkSyncTest begin"); +// try { +// const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); +// console.println(`unlink result: ${ret}`); + +// } catch (error) { +// console.error("unlinkSyncTest Error!", error); +// } +// console.println("unlinkSyncTest end"); +// } + +// function writeSyncTest1() { +// console.println("writeSyncTest1 begin"); +// try { +// let mode = 2; +// let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); +// let fd = file.fd; +// console.println(`file open fd=${fd}`); +// const options: WriteOptions = { +// encoding: "utf-8", +// }; +// let length = fileIo.writeSync(fd, "hello,world!", options); +// console.println(`write file length=${length}`); +// } catch (error) { +// console.error("writeSyncTest1 Error!", error); +// } +// console.println("writeSyncTest1 end"); +// } + +// function writeSyncTest2() { +// console.println("writeSyncTest2 begin"); +// try { +// let mode = 2; +// let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); +// let fd = file.fd; +// console.println(`file open fd=${fd}`); +// const options: WriteOptions = { +// offset: 100, +// }; +// let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); +// console.println(`write file length=${length}`); +// } catch (error) { +// console.error("writeSyncTest2 Error!", error); +// } +// console.println("writeSyncTest2 end"); +// } + +// function errorHandlerTest1() { +// console.println("errorHandlerTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + +// console.println("\n"); +// console.println("try open file notexist.txt ..."); +// let mode = 2; +// file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); +// console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest1 Error!", error); +// } +// console.println("errorHandlerTest1 end"); +// } + +// function errorHandlerTest2() { +// console.println("errorHandlerTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println("try open file a.txt ..."); +// let mode = -1; +// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest2 Error!", error); +// } +// console.println("errorHandlerTest2 end"); +// } + +// function errorHandlerTest3() { +// console.println("errorHandlerTest3 begin"); +// try { +// let file = await fileIo.open("/data/local/tmp/a.txt"); +// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// console.println("-----------"); +// console.println("try open file a.txt ..."); +// let mode = -1; +// file = await fileIo.open("/data/local/tmp/a.txt", mode); +// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest3 Error!", error); +// } +// console.println("errorHandlerTest3 end"); +// } + +// function errorHandlerTest4() { +// console.println("errorHandlerTest4 begin"); +// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { +// if (err.code == 0 && data !== undefined) { +// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open file Error!", err); +// } +// }); + +// fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { +// console.println("2)-----------"); +// if (err.code == 0 && data !== undefined) { +// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open file Error!", err); +// } +// }); +// console.println("errorHandlerTest4 end"); +// } function main() { console.println("---------- hello ani --------------"); - // accessSyncTest1(); + accessSyncTest1(); // openSyncTest(); // closeSyncTest1(); // closeSyncTest2(); @@ -1320,7 +1320,7 @@ function main() { // writeSyncTest1(); // writeSyncTest2(); // errorHandlerTest1(); // 打开文件失败,文件不存在 - errorHandlerTest2(); // 参数校验失败 + // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise // errorHandlerTest4(); // 异步callback console.println("---------- hello ani end ---------------"); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index dd4f6bdc..31dd5e61 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -143,7 +143,7 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & static tuple Wrap(ani_env *env, FsStat *fsStat) { ani_object statObject = {}; - static const char *className = "Lfile_fs_class/StatInner;"; + static const char *className = "L@ohos/file/fs/StatInner;"; ani_class cls; ani_status ret; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index dc8bf986..9db22cae 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -16,7 +16,6 @@ #include "open_ani.h" #include "ani_helper.h" -#include "error_handler.h" #include "filemgmt_libhilog.h" #include "open_core.h" #include "type_converter.h" @@ -30,7 +29,7 @@ using namespace OHOS::FileManagement::ModuleFileIO; static ani_object Wrap(ani_env *env, const FsFile *file) { - static const char *className = "Lfile_fs_class/FileInner;"; + static const char *className = "L@ohos/file/fs/FileInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %s", className); @@ -91,30 +90,21 @@ ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); - ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); - ErrorHandler::Throw(env, EINVAL); return nullptr; } FsResult ret = OpenCore::DoOpen(filePath, modeOp); if (!ret.IsSuccess()) { HILOGE("Open failed"); - const auto &err = ret.GetError(); - ErrorHandler::Throw(env, err); return nullptr; } const FsFile *file = ret.GetData().value(); - auto result = Wrap(env, move(file)); - if (result == nullptr) { - ErrorHandler::Throw(env, UNKNOWN_ERR); - return nullptr; - } - return result; + return Wrap(env, move(file)); } } // namespace ANI } // namespace ModuleFileIO -- Gitee From 6d570cf9686a7a4cdc868cf0c2c4b0e29539d329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 12 Mar 2025 20:38:03 +0800 Subject: [PATCH 11/76] =?UTF-8?q?=E5=A2=9E=E5=8A=A0close=E5=92=8Cstat?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E6=AD=A5=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=9A=E4=BD=99=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98=20Signed-off-by:?= =?UTF-8?q?=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I165b115af25c5d4253c042a1d7b7456b103031e0 --- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 143 ++++++++++++------ .../src/mod_fs/properties/ani/close_ani.cpp | 15 +- 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index dde54350..55dfa7c7 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -188,27 +188,27 @@ class fileIo { static access(path: string, mode?: AccessModeType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { if (mode === undefined) { - let promise = taskpool.execute(fileIo.accessSync1, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); } else { - let promise = taskpool.execute(fileIo.accessSync2, path, mode); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync2, path, mode); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); } @@ -217,44 +217,76 @@ class fileIo { static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let result = ret as boolean; - resolve(result); // 正常结果 + resolve(result); } }); }) } static access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.accessSync1, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.accessSync1, path); promise.then((ret: NullishType) => { let err = new BusinessError(); - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); } else { err.code = 0; let result = ret as boolean; - callback(err, result); // 正常结果 + callback(err, result); } }); } static native closeSync(file: FdOrFile): int; - static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; + static close(file: FdOrFile): Promise { + return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.closeSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as int; + resolve(r); + } + }); + }); + } + + static close(file: FdOrFile, callback: AsyncCallback): void { + let p1 = taskpool.execute(fileIo.closeSync, file); + p1.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as int; + callback(err, r); + } + }); + } + + static native copyFileSync(src: PathOrFd, dest: PathOrFdx, mode?: int): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; static listFileSync1(path: string): string[] { return fileIo.listFileSync(path); } + static listFileSync2(path: string, options: ListFileOptions): string[] { return fileIo.listFileSync(path, options); } @@ -328,15 +360,15 @@ class fileIo { static mkdir(path: string): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); // 这里调用同步方法 + let promise = taskpool.execute(mkdirSyncWrapper, path); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -348,11 +380,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -363,17 +393,15 @@ class fileIo { static mkdir(path: string, recursion: boolean): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { const mkdirSyncWrapper = (path: string, recursion: boolean) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); // 这里调用同步方法 + let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 - console.println("-------- err code = -1 -------------"); + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - console.println("-------- err code = 0 -------------"); - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -385,11 +413,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -404,6 +430,7 @@ class fileIo { static openSync1(path: String, mode: int): File { return fileIo.openSync(path, mode); } + static openSync2(path: String): File { return fileIo.openSync(path); } @@ -445,11 +472,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as File; callback(err, r); @@ -462,11 +487,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as File; callback(err, r); @@ -479,6 +502,7 @@ class fileIo { static readSync1(fd: int, buffer: ArrayBuffer): long { return fileIo.readSync(fd, buffer); } + static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { return fileIo.readSync(fd, buffer, options); } @@ -520,11 +544,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as long; callback(err, r); @@ -537,11 +559,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as long; callback(err, r); @@ -554,6 +574,7 @@ class fileIo { static readTextSync1(filePath: string): string { return fileIo.readTextSync(filePath); } + static readTextSync2(filePath: string, options: ReadTextOptions): string { return fileIo.readTextSync(filePath, options); } @@ -562,13 +583,13 @@ class fileIo { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync1, filePath); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let r = ret as string; - resolve(r); // 正常结果 + resolve(r); } }); }); @@ -578,13 +599,13 @@ class fileIo { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { let r = ret as string; - resolve(r); // 正常结果 + resolve(r); } }); }); @@ -595,11 +616,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as string; callback(err, r); @@ -612,11 +631,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1; callback(err, undefined); } else { - console.println("-------- err code = 0 -------------"); err.code = 0; let r = ret as string; callback(err, r); @@ -628,21 +645,52 @@ class fileIo { static native statSync(file: PathOrFd): Stat; + static stat(file: PathOrFd): Promise { + return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(fileIo.statSync, file); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as Stat; + resolve(r); + } + }); + }); + } + + static stat(file: PathOrFd, callback: AsyncCallback): void { + let p = taskpool.execute(fileIo.statSync, file); + p.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as Stat; + callback(err, r); + } + }); + } + static native truncateSync(file: PathOrFd, len?: long): void; static native unlinkSync(path: string): int; static unlink(path: string): Promise { return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.unlinkSync, path); // 这里调用同步方法 + let promise = taskpool.execute(fileIo.unlinkSync, path); promise.then((ret: NullishType) => { let result = ret as int - if (ret === null || ret === undefined) { // 异常处理 + if (ret === null || ret === undefined) { let err = new BusinessError(); err.code = -1; reject(err); } else { - resolve(result); // 正常结果 + resolve(result); } }); }); @@ -653,11 +701,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as int; callback(err, r); @@ -670,6 +716,7 @@ class fileIo { static writeSync1(fd: int, buffer: BufferType, options: WriteOptions): long { return fileIo.writeSync(fd, buffer, options); } + static writeSync2(fd: int, buffer: BufferType): long { return fileIo.writeSync(fd, buffer); } @@ -711,11 +758,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as long; callback(err, r); @@ -728,11 +773,9 @@ class fileIo { p1.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { - console.println("-------- err code = -1 -------------"); err.code = -1 callback(err, undefined) } else { - console.println("-------- err code = 0 -------------"); err.code = 0 let r = ret as long; callback(err, r); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index e4d1fe4e..313a5c14 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -29,11 +29,11 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; -tuple AnalyzerFdUnion(ani_env *env, ani_object obj) +tuple ParseFd(ani_env *env, ani_object obj) { - int result = -1; + int32_t result = -1; ani_class IntClass; - env->FindClass("Lstd/core/Double;", &IntClass); + env->FindClass("Lstd/core/Int;", &IntClass); ani_boolean isInt; env->Object_InstanceOf(obj, IntClass, &isInt); if (isInt) { @@ -42,7 +42,7 @@ tuple AnalyzerFdUnion(ani_env *env, ani_object obj) HILOGE("Get fd value failed"); return { false, result }; } - result = static_cast(fd); + result = static_cast(fd); return { true, result }; } @@ -56,17 +56,18 @@ tuple AnalyzerFdUnion(ani_env *env, ani_object obj) HILOGE("Get fd in class file failed"); return { false, result }; } - result = static_cast(fd); + result = static_cast(fd); return { true, result }; } + HILOGE("Invalid fd type"); return { false, result }; } ani_int CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj) { - auto [succ, fd] = AnalyzerFdUnion(env, obj); + auto [succ, fd] = ParseFd(env, obj); if (!succ) { - HILOGE("Invalid arguments"); + HILOGE("Parse fd argument failed"); return -1; } -- Gitee From 22341068d72c387133d63d8d42a1a8d5b4235f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 12 Mar 2025 20:38:03 +0800 Subject: [PATCH 12/76] =?UTF-8?q?=E5=A2=9E=E5=8A=A0close=E5=92=8Cstat?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E6=AD=A5=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=9A=E4=BD=99=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98=20Signed-off-by:?= =?UTF-8?q?=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I165b115af25c5d4253c042a1d7b7456b103031e0 --- interfaces/kits/js/BUILD.gn | 3 - .../src/common/ani_helper/bind_function.cpp | 78 ------------------- .../js/src/common/ani_helper/bind_function.h | 34 ++++---- .../js/src/mod_fs/ani/bind_function_class.cpp | 22 +++--- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 2 +- .../src/mod_hash/ani/bind_function_class.cpp | 33 +++++++- .../ani/bind_function_class.cpp | 36 +++++++-- 7 files changed, 95 insertions(+), 113 deletions(-) delete mode 100644 interfaces/kits/js/src/common/ani_helper/bind_function.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d4f31e1b..10829853 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -669,7 +669,6 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/copy_listener", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/ani/bind_function_class.cpp", @@ -779,7 +778,6 @@ ohos_shared_library("ani_hash_class") { "src/mod_hash/ani", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/common/file_helper/hash_file.cpp", @@ -849,7 +847,6 @@ ohos_shared_library("ani_securitylabel_class") { "src/mod_securitylabel", ] sources = [ - "src/common/ani_helper/bind_function.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", diff --git a/interfaces/kits/js/src/common/ani_helper/bind_function.cpp b/interfaces/kits/js/src/common/ani_helper/bind_function.cpp deleted file mode 100644 index 1f1b2904..00000000 --- a/interfaces/kits/js/src/common/ani_helper/bind_function.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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 "bind_function.h" -#include "filemgmt_libhilog.h" - -namespace OHOS { -namespace FileManagement { -namespace ModuleFileIO { -namespace ANI { -ANI_EXPORT ani_status BindClass(ani_vm *vm, const char *className, const std::vector &methods) -{ - if (vm == nullptr) { - HILOGE("ani_vm is null!"); - return ANI_ERROR; - } - - ani_env *env; - if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - HILOGE("Unsupported ANI_VERSION_1!"); - return ANI_OUT_OF_REF; - } - - ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Not found '%{private}s'", className); - return ANI_INVALID_ARGS; - } - - if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { - HILOGE("Cannot bind native methods to '%{private}s'", className); - return ANI_INVALID_TYPE; - }; - return ANI_OK; -} - -ANI_EXPORT ani_status BindNamespace( - ani_vm *vm, const char *namespaceStr, const std::vector &functions) -{ - if (vm == nullptr) { - HILOGE("ani_vm is null!"); - return ANI_ERROR; - } - - ani_env *env; - if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - HILOGE("Unsupported ANI_VERSION_1 Fail!!!"); - return ANI_OUT_OF_REF; - } - - ani_namespace ns; - if (ANI_OK != env->FindNamespace(namespaceStr, &ns)) { - HILOGE("Not found '%{private}s'", namespaceStr); - return ANI_INVALID_ARGS; - } - - if (ANI_OK != env->Namespace_BindNativeFunctions(ns, functions.data(), functions.size())) { - HILOGE("Cannot bind native methods to '%{private}s'", namespaceStr); - return ANI_INVALID_TYPE; - }; - return ANI_OK; -} -} // namespace ANI -} // namespace ModuleFileIO -} // namespace FileManagement -} // namespace OHOS diff --git a/interfaces/kits/js/src/common/ani_helper/bind_function.h b/interfaces/kits/js/src/common/ani_helper/bind_function.h index 7d41c655..3c951b25 100644 --- a/interfaces/kits/js/src/common/ani_helper/bind_function.h +++ b/interfaces/kits/js/src/common/ani_helper/bind_function.h @@ -25,27 +25,28 @@ namespace FileManagement { namespace ModuleFileIO { namespace ANI { -ANI_EXPORT ani_status BindClass(ani_vm *vm, const char *className, const std::vector &methods); -ANI_EXPORT ani_status BindNamespace( - ani_vm *vm, const char *namespaceStr, const std::vector &functions); - template ANI_EXPORT ani_status BindClass(ani_env *env, const char *className, const std::array &methods) { if (env == nullptr) { - HILOGE("ani_env is null!"); - return ANI_ERROR; + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; } - + + if (className == nullptr) { + HILOGE("Invalid parameter className"); + return ANI_INVALID_ARGS; + } + ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class '%{private}s'", className); - return ANI_INVALID_ARGS; + return ANI_NOT_FOUND; } if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) { HILOGE("Cannot bind native methods to '%{private}s'", className); - return ANI_INVALID_TYPE; + return ANI_ERROR; }; return ANI_OK; } @@ -55,19 +56,24 @@ ANI_EXPORT ani_status BindNamespace( ani_env *env, const char *namespaceStr, const std::array &methods) { if (env == nullptr) { - HILOGE("ani_env is null!"); - return ANI_ERROR; + HILOGE("Invalid parameter env"); + return ANI_INVALID_ARGS; + } + + if (namespaceStr == nullptr) { + HILOGE("Invalid parameter namespaceStr"); + return ANI_INVALID_ARGS; } ani_namespace ns; if (ANI_OK != env->FindNamespace(namespaceStr, &ns)) { - HILOGE("Not found '%{private}s'", namespaceStr); - return ANI_INVALID_ARGS; + HILOGE("Cannot find namespace '%{private}s'", namespaceStr); + return ANI_NOT_FOUND; } if (ANI_OK != env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size())) { HILOGE("Cannot bind native methods to '%{private}s'", namespaceStr); - return ANI_INVALID_TYPE; + return ANI_ERROR; }; return ANI_OK; } 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 779bd453..b0354e5c 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 @@ -94,12 +94,21 @@ static ani_status BindStaticMethods(ani_env *env) ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + ani_env *env; - ani_status status = ANI_ERROR; - status = vm->GetEnv(ANI_VERSION_1, &env); + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); if (status != ANI_OK) { - HILOGE("Unsupported ANI_VERSION_1"); - return status; + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; } status = BindStaticMethods(env); @@ -120,11 +129,6 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) return status; }; - if (result == nullptr) { - HILOGE("result is null!"); - return ANI_ERROR; - } - *result = ANI_VERSION_1; return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets index 55dfa7c7..c4e3d3ae 100644 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets @@ -279,7 +279,7 @@ class fileIo { }); } - static native copyFileSync(src: PathOrFd, dest: PathOrFdx, mode?: int): void; + static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; 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 8b51760d..e0c81d4e 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 @@ -19,14 +19,41 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +static ani_status BindStaticMethods(ani_env *env) { static const char *className = "Lfile_hash_class/hash;"; - std::vector functions = { + std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for hash!"); + return status; + }; *result = ANI_VERSION_1; - return BindClass(vm, className, functions); + return ANI_OK; } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 19e735c5..157773f9 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -19,15 +19,41 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; -ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +static ani_status BindStaticMethods(ani_env *env) { - *result = ANI_VERSION_1; - static const char *className = "Lfile_securitylabel_class/securitylabel;"; - std::vector functions = { + std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:I", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for securitylabel!"); + return status; + }; - return BindClass(vm, className, functions); + *result = ANI_VERSION_1; + return ANI_OK; } -- Gitee From fb1dcabf36d0ffe01db485f3fc632531eb5d22d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 03:36:01 +0800 Subject: [PATCH 13/76] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20Si?= =?UTF-8?q?gned-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I249d975aaeb552985b567ecff0050aab5c30fce4 --- interfaces/kits/js/BUILD.gn | 14 ++++---------- .../kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp | 6 ++++-- .../kits/js/src/mod_fs/properties/ani/open_ani.cpp | 1 - 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 5680632c..fb173ccf 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -623,9 +623,9 @@ group("build_kits_js") { group("ani_file_api") { deps = [ ":ani_fs_class", - ":ohos_file_fs_abc", ":ani_hash_class", ":ani_securitylabel_class", + ":ohos_file_fs_abc", ] } @@ -760,9 +760,7 @@ ohos_shared_library("ani_fs_class") { generate_static_abc("ohos_file_fs_abc") { base_url = "./src/mod_fs/ani/ets" - files = [ - "./src/mod_fs/ani/ets/@ohos.file.fs.ets" - ] + files = [ "./src/mod_fs/ani/ets/@ohos.file.fs.ets" ] is_boot_abc = "True" device_dst_file = "/system/framework/ohos_file_fs_abc.abc" } @@ -830,9 +828,7 @@ ohos_shared_library("ani_hash_class") { generate_static_abc("ohos_file_hash_abc") { base_url = "./src/mod_hash/ani/ets" - files = [ - "./src/mod_hash/ani/ets/@ohos.file.hash.ets" - ] + files = [ "./src/mod_hash/ani/ets/@ohos.file.hash.ets" ] is_boot_abc = "True" device_dst_file = "/system/framework/ohos_file_hash_abc.abc" } @@ -898,9 +894,7 @@ ohos_shared_library("ani_securitylabel_class") { generate_static_abc("ohos_file_securityLabel_abc") { base_url = "./src/mod_securitylabel/ani/ets" - files = [ - "./src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets" - ] + files = [ "./src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets" ] is_boot_abc = "True" device_dst_file = "/system/framework/ohos_file_securityLabel_abc.abc" } diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index 31dd5e61..3c9161cb 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -109,7 +109,8 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & { "size", ani_double(static_cast(fsStat->GetSize())) }, { "atime", ani_double(static_cast(fsStat->GetAtime())) }, { "mtime", ani_double(static_cast(fsStat->GetMtime())) }, - { "ctime", ani_double(static_cast(fsStat->GetCtime())) } }; + { "ctime", ani_double(static_cast(fsStat->GetCtime())) }, + }; for (auto iter : numProperties) { ret = SetNumProperty(env, cls, statObject, iter.first, iter.second); if (ret != ANI_OK) { @@ -122,7 +123,8 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & { "ino", ani_double(static_cast(fsStat->GetIno())) }, { "atimeNs", ani_double(static_cast(fsStat->GetAtimeNs())) }, { "mtimeNs", ani_double(static_cast(fsStat->GetMtimeNs())) }, - { "ctimeNs", ani_double(static_cast(fsStat->GetCtimeNs())) } }; + { "ctimeNs", ani_double(static_cast(fsStat->GetCtimeNs())) }, + }; for (auto iter : bigIntProperties) { ret = SetBigIntProperty(env, cls, statObject, iter.first, iter.second); if (ret != ANI_OK) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 9db22cae..3e15ada6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -19,7 +19,6 @@ #include "filemgmt_libhilog.h" #include "open_core.h" #include "type_converter.h" -#include namespace OHOS { namespace FileManagement { -- Gitee From c7203a1781529e885f0ee05fbb9f7eea46f414f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 04:09:24 +0800 Subject: [PATCH 14/76] =?UTF-8?q?hash=E6=A8=A1=E5=9D=97=20Signed-off-by:?= =?UTF-8?q?=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ibc35f4ab2ea99bab3b12f6f7eb0812c8041729e3 --- interfaces/kits/js/BUILD.gn | 2 + .../src/mod_hash/ani/bind_function_class.cpp | 2 +- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 74 ++++++++++++++++--- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index fb173ccf..f992afa2 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -822,6 +822,7 @@ ohos_shared_library("ani_hash_class") { cfi_cross_dso = true debug = false } + output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" } @@ -888,6 +889,7 @@ ohos_shared_library("ani_securitylabel_class") { cfi_cross_dso = true debug = false } + output_extension = "so" subsystem_name = "filemanagement" part_name = "file_api" } 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 e0c81d4e..fc2bd3c0 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 @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "Lfile_hash_class/hash;"; + static const char *className = "L@ohos/file/hash/hash;"; std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, 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 d291adce..468c3c4e 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 @@ -1,19 +1,73 @@ +/* + * 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. + */ -import type { AsyncCallback } from './@ohos.base'; -import stream from './@ohos.util.stream'; +export default hash; -declare namespace hash { - native function hash(path: string, algorithm: string): Promise; +export class BusinessError { + code: number = 0; + data?: T; +} + +export type AsyncCallback = (err: BusinessError, data?: T) => void; - native function hash(path: string, algorithm: string, callback: AsyncCallback): void; +class hash { + + static { + loadLibrary("ani_hash_class"); + } - class HashStream extends stream.Transform { - native digest(): string; + static native hashSync(path: string, algorithm: string): string; - native update(data: ArrayBuffer): void; + static hash(path: string, algorithm: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(hash.hashSync, path, algorithm); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let r = ret as string; + resolve(r); + } + }); + }); } - native function createHash(algorithm: string): HashStream; + static hash(path: string, algorithm: string, callback: AsyncCallback): void { + let promise = taskpool.execute(hash.hashSync, path, algorithm); + promise.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as string; + callback(err, r); + } + }); + } } -export default hash; \ No newline at end of file +function main() { + console.println("---------- hello ani --------------"); + + let ret = hash.hashSync("/data/local/tmp/a.txt", "md5"); + console.println(`hash: ${ret}`); + + + console.println("---------- hello ani end ---------------"); +} \ No newline at end of file -- Gitee From ed2de94ded4fbf4006b5c949799c06bc4d8ee662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 04:32:15 +0800 Subject: [PATCH 15/76] =?UTF-8?q?securityLabel=E6=A8=A1=E5=9D=97=20Signed-?= =?UTF-8?q?off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iddbd2b18ebf39dfdfa4deb62d9a6b58ae9467cd2 --- .../ani/bind_function_class.cpp | 4 +- .../ani/ets/@ohos.file.securityLabel.ets | 74 ++++++++++++++++--- .../ani/securitylabel_ani.cpp | 10 +-- .../mod_securitylabel/ani/securitylabel_ani.h | 3 +- 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 157773f9..74c9466d 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -21,9 +21,9 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "Lfile_securitylabel_class/securitylabel;"; + static const char *className = "L@ohos/file/securityLabel/securitylabel;"; std::array methods = { - ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:I", + ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, }; return BindClass(env, className, methods); diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 47841fcd..c04cad5e 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -1,19 +1,73 @@ -import type { AsyncCallback } from './@ohos.base'; +/* + * 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. + */ -declare namespace securityLabel { - type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; +export default securitylabel; - native function setSecurityLabel(path: string, type: DataLevel): Promise; +export class BusinessError { + code: number = 0; + data?: T; +} + +export type AsyncCallback = (err: BusinessError, data?: T) => void; + +type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; - native function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void; +class securitylabel { - native function setSecurityLabelSync(path: string, type: DataLevel): void; + static { + loadLibrary("ani_securitylabel_class"); + } - native function getSecurityLabel(path: string): Promise; + static native setSecurityLabelSync(path: string, type: DataLevel): void; - native function getSecurityLabel(path: string, callback: AsyncCallback): void; + static setSecurityLabel(path: string, type: DataLevel): Promise { + let pvoid: Promise = new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + resolve(undefined); + } + }); + }); + return pvoid; + } - native function getSecurityLabelSync(path: string): string; + static setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { + let p1 = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); + p1.then((ret: NullishType) => { + let err = new BusinessError(); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + callback(err, undefined); + } + }); + } } -export default securityLabel; \ No newline at end of file +function main() { + console.println("---------- hello ani --------------"); + + securitylabel.setSecurityLabelSync("/data/local/tmp/a.txt", "s2"); + + console.println("---------- hello ani end ---------------"); +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index 35f28cc7..7c78437d 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -28,28 +28,26 @@ using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; using namespace OHOS::FileManagement::ModuleSecurityLabel; -ani_int SecurityLabelAni::SetSecurityLabelSync( +void SecurityLabelAni::SetSecurityLabelSync( ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level) { auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); - return -1; + return; } auto [succLevel, dataLevel] = TypeConverter::ToUTF8String(env, level); if (!succLevel) { HILOGE("Invalid dataLevel"); - return -1; + return; } auto ret = DoSetSecurityLabel(srcPath, dataLevel); if (!ret.IsSuccess()) { HILOGE("Set securitylabel failed"); - return -1; + return; } - - return 0; } } // namespace ANI diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h index b99fa8bc..025d6530 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h @@ -25,8 +25,7 @@ namespace ANI { class SecurityLabelAni final { public: - static ani_int SetSecurityLabelSync( - ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level); + static void SetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level); }; } // namespace ANI -- Gitee From 5f0da2db69f74fb5611f7fdaf6663de7d26042c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 05:38:00 +0800 Subject: [PATCH 16/76] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=20Si?= =?UTF-8?q?gned-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I2629071db828f0ca10dc1d079a37b278278f83e5 --- .../src/common/ani_helper/type_converter.cpp | 61 ++++++++++--------- .../ani/ets/@ohos.file.securityLabel.ets | 8 +-- 2 files changed, 36 insertions(+), 33 deletions(-) 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 267b9006..e6186996 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -138,6 +138,38 @@ std::tuple> TypeConverter::EnumToInt32(ani_env *env return { true, std::make_optional(result) }; } +static std::tuple ParseFd(ani_env *env, const ani_object &pathOrFd) +{ + ani_boolean isFd = false; + + ani_class DoubleClass; + env->FindClass("Lstd/core/Double;", &DoubleClass); + env->Object_InstanceOf(pathOrFd, DoubleClass, &isFd); + if (isFd) { + ani_double doubleValue; + if (ANI_OK != env->Object_CallMethodByName_Double(pathOrFd, "doubleValue", nullptr, &doubleValue)) { + HILOGE("Parse file path failed"); + return { false, 0 }; + } + int32_t fd = static_cast(doubleValue); + return { true, fd }; + } + + ani_class IntClass; + env->FindClass("Lstd/core/Int;", &IntClass); + env->Object_InstanceOf(pathOrFd, IntClass, &isFd); + if (isFd) { + ani_int fd; + if (ANI_OK != env->Object_CallMethodByName_Int(pathOrFd, "intValue", nullptr, &fd)) { + HILOGE("Parse file path failed"); + return { false, 0 }; + } + return { true, fd }; + } + + return { false, 0 }; +} + std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_object &pathOrFd) { if (env == nullptr) { @@ -166,35 +198,8 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj return { true, FileInfo { true, move(chars), {} } }; } - ani_boolean isFd = false; - - ani_class DoubleClass; - env->FindClass("Lstd/core/Double;", &DoubleClass); - env->Object_InstanceOf(pathOrFd, DoubleClass, &isFd); - if (isFd) { - ani_double doubleValue; - if (ANI_OK != env->Object_CallMethodByName_Double(pathOrFd, "doubleValue", nullptr, &doubleValue)) { - HILOGE("Parse file path failed"); - return { false, FileInfo { false, {}, {} } }; - } - int32_t fd = static_cast(doubleValue); - auto fdg = CreateUniquePtr(fd, false); - if (fdg == nullptr) { - HILOGE("Failed to request heap memory."); - return { false, FileInfo { false, {}, {} } }; - } - return { true, FileInfo { false, {}, move(fdg) } }; - } - - ani_class IntClass; - env->FindClass("Lstd/core/Int;", &IntClass); - env->Object_InstanceOf(pathOrFd, IntClass, &isFd); + auto [isFd, fd] = ParseFd(env, pathOrFd); if (isFd) { - ani_int fd; - if (ANI_OK != env->Object_CallMethodByName_Int(pathOrFd, "intValue", nullptr, &fd)) { - HILOGE("Parse file path failed"); - return { false, FileInfo { false, {}, {} } }; - } auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { HILOGE("Failed to request heap memory."); diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index c04cad5e..962c25d0 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -33,8 +33,7 @@ class securitylabel { static native setSecurityLabelSync(path: string, type: DataLevel): void; static setSecurityLabel(path: string, type: DataLevel): Promise { - let pvoid: Promise = new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { @@ -46,12 +45,11 @@ class securitylabel { } }); }); - return pvoid; } static setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let p1 = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); - p1.then((ret: NullishType) => { + let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType) => { let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1 -- Gitee From 585c8b17e9e0fbf854984071e75fb8e59ffe8aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 15:08:21 +0800 Subject: [PATCH 17/76] =?UTF-8?q?open=20=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I02a9a34974010544f07b31677806f0efb9b77a0a --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 124 +++++++++++++++--- .../js/src/mod_fs/properties/ani/open_ani.cpp | 2 +- 2 files changed, 105 insertions(+), 21 deletions(-) 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 c2a69abc..35f00c94 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 @@ -15,6 +15,49 @@ export default fileIo; +export namespace OpenMode { + /** + * Read only Permission. + */ + export const READ_ONLY = 0o0; + /** + * Write only Permission. + */ + export const WRITE_ONLY = 0o1; + /** + * Write and Read Permission. + */ + export const READ_WRITE = 0o2; + /** + * If not exist, create file. + */ + export const CREATE = 0o100; + /** + * File truncate len 0. + */ + export const TRUNC = 0o1000; + /** + * File append write. + */ + export const APPEND = 0o2000; + /** + * File open in nonblocking mode. + */ + export const NONBLOCK = 0o4000; + /** + * File is Dir. + */ + export const DIR = 0o200000; + /** + * File is not symbolic link. + */ + export const NOFOLLOW = 0o400000; + /** + * SYNC IO. + */ + export const SYNC = 0o4010000; +} + export class BusinessError { name: string; message: string; @@ -74,7 +117,7 @@ enum AccessFlagType { } interface File { - fd: int; + fd: number; path: String; name: String; @@ -85,7 +128,7 @@ interface File { } class FileInner implements File { - fd: int = -1; + fd: number = -1; path: String = ""; name: String = ""; @@ -437,9 +480,9 @@ class fileIo { static native moveFileSync(src: String, dest: String, mode?: int): void; - static native openSync(path: String, mode?: int): File; + static native openSync(path: String, mode?: number): File; - static openSync1(path: String, mode: int): File { + static openSync1(path: String, mode: number): File { return fileIo.openSync(path, mode); } @@ -447,7 +490,7 @@ class fileIo { return fileIo.openSync(path); } - static open(path: String, mode: int): Promise { + static open(path: String, mode: number): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync1, path, mode); promise.then((ret: NullishType) => { @@ -475,7 +518,7 @@ class fileIo { }); } - static open(path: String, mode: int, callback: AsyncCallback): void { + static open(path: String, mode: number, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.openSync1, path, mode); promise.then((ret: NullishType) => { let e = new BusinessError(0, ""); @@ -923,19 +966,58 @@ function accessSyncTest1() { // console.println("moveSyncTest end") // } -// function openSyncTest() { -// console.println("openSyncTest begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// let mode = 2; -// file = fileIo.openSync("/data/local/tmp/a.txt", mode); -// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("openSyncTest Error!", error); -// } -// console.println("openSyncTest end"); -// } +function openSyncTest() { + console.println("openSyncTest begin"); + try { + // 不指定mode + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + // 以可读可写方式打开 + let mode = OpenMode.READ_WRITE; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openSyncTest Error!", error); + } + console.println("openSyncTest end"); +} + +function openPromiseTest() { + console.println("openPromiseTest begin"); + try { + // 不指定mode + let file = await fileIo.open("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + // 以可读可写方式打开 + let mode = OpenMode.READ_WRITE; + file = await fileIo.open("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openPromiseTest Error!", error); + } + console.println("openPromiseTest end"); +} + +function openCallbackTest() { + console.println("openCallbackTest begin"); + // 不指定mode + fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { + if (data !== undefined) { + console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open failed!", err); + } + }); + // 以可读可写方式打开 + let mode = OpenMode.READ_WRITE; + fileIo.open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { + if (data !== undefined) { + console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open failed!", err); + } + }); +} // function readSyncTest1() { // console.println("readSyncTest1 begin") @@ -1291,7 +1373,9 @@ function accessSyncTest1() { function main() { console.println("---------- hello ani --------------"); accessSyncTest1(); - // openSyncTest(); + openPromiseTest(); + openCallbackTest(); + openSyncTest(); // closeSyncTest1(); // closeSyncTest2(); // closeSyncTest3(); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 3e15ada6..22177da7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -53,7 +53,7 @@ static ani_object Wrap(ani_env *env, const FsFile *file) } const auto &fd = fdRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", fd)) { + if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast(fd))) { HILOGE("Set fd field value failed!"); return nullptr; } -- Gitee From edacf56db144d2f98cae9936ef60f105385ab0ea Mon Sep 17 00:00:00 2001 From: tianp Date: Sat, 15 Mar 2025 16:35:34 +0800 Subject: [PATCH 18/76] =?UTF-8?q?write=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 61 ++++++++++++++----- .../src/mod_fs/properties/ani/write_ani.cpp | 12 ++-- .../js/src/mod_fs/properties/ani/write_ani.h | 4 +- 3 files changed, 53 insertions(+), 24 deletions(-) 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 35f00c94..c2ec3ebc 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 @@ -478,7 +478,7 @@ class fileIo { }); } - static native moveFileSync(src: String, dest: String, mode?: int): void; + static native moveFileSync(src: String, dest: String, mode?: number): void; static native openSync(path: String, mode?: number): File; @@ -759,24 +759,23 @@ class fileIo { }); } - static native writeSync(fd: int, buffer: BufferType, options?: WriteOptions): long; + static native writeSync(fd: number, buffer: BufferType, options?: WriteOptions): number; - static writeSync1(fd: int, buffer: BufferType, options: WriteOptions): long { + static writeSync1(fd: number, buffer: BufferType, options: WriteOptions): number { return fileIo.writeSync(fd, buffer, options); } - static writeSync2(fd: int, buffer: BufferType): long { + static writeSync2(fd: number, buffer: BufferType): number { return fileIo.writeSync(fd, buffer); } - static write(fd: int, buffer: BufferType, options: WriteOptions): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + static write(fd: number, buffer: BufferType, options: WriteOptions): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); promise.then((ret: NullishType) => { - let result = ret as long + let result = ret as number if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); - reject(err); } else { resolve(result); @@ -785,14 +784,13 @@ class fileIo { }); } - static write(fd: int, buffer: BufferType): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + static write(fd: number, buffer: BufferType): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); promise.then((ret: NullishType) => { - let result = ret as long + let result = ret as number if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); - reject(err); } else { resolve(result); @@ -801,7 +799,7 @@ class fileIo { }); } - static write(fd: int, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { + static write(fd: number, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); @@ -810,13 +808,13 @@ class fileIo { callback(err, undefined) } else { err.code = 0 - let r = ret as long; + let r = ret as number; callback(err, r); } }); } - static write(fd: int, buffer: BufferType, callback: AsyncCallback): void { + static write(fd: number, buffer: BufferType, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); @@ -825,7 +823,7 @@ class fileIo { callback(err, undefined) } else { err.code = 0 - let r = ret as long; + let r = ret as number; callback(err, r); } }); @@ -1302,6 +1300,35 @@ function openCallbackTest() { // console.println("writeSyncTest2 end"); // } +// function writePromiseTest() { +// console.println("writePromiseTest begin"); +// try { +// let mode = OpenMode.READ_WRITE; +// let file = fileIo.openSync("/data/local/tmp/c.txt", mode); +// let fd = file.fd; +// let length = await fileIo.write(fd, "writePromiseTest"); +// console.println(`writePromiseTest length=${length}`); +// } catch (error) { +// console.error("writePromiseTest Error!", error); +// } +// console.println("writePromiseTest end"); +// } + +// function writeCallbackTest() { +// console.println("writeCallbackTest begin"); +// let mode = OpenMode.READ_WRITE; +// let file = fileIo.openSync("/data/local/tmp/d.txt", mode); +// let fd = file.fd; +// fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { +// if (data !== undefined) { +// console.println(`writeCallbackTest length=${data}`); +// } else { +// console.error("writeCallbackTest Error!", err); +// } +// }); +// console.println("writeCallbackTest end"); +// } + // function errorHandlerTest1() { // console.println("errorHandlerTest1 begin"); // try { @@ -1403,6 +1430,8 @@ function main() { // unlinkSyncTest(); // writeSyncTest1(); // writeSyncTest2(); + // writePromiseTest(); + // writeCallbackTest(); // errorHandlerTest1(); // 打开文件失败,文件不存在 // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp index 0f6248f6..249f6447 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp @@ -88,8 +88,8 @@ static std::tuple ParseArrayBuffer(ani_env *env, const an return { true, std::move(result) }; } -ani_long WriteAni::WriteSync( - ani_env *env, [[maybe_unused]] ani_class clazz, ani_int fd, ani_object buf, ani_object options) +ani_double WriteAni::WriteSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_object buf, ani_object options) { auto [succOp, op] = ToWriteOptions(env, options); if (!succOp) { @@ -104,12 +104,12 @@ ani_long WriteAni::WriteSync( HILOGE("Failed to resolve stringBuffer!"); return -1; } - auto ret = WriteCore::DoWrite(fd, buffer, op); + auto ret = WriteCore::DoWrite(static_cast(fd), buffer, op); if (!ret.IsSuccess()) { HILOGE("write buffer failed!"); return -1; } - return ret.GetData().value(); + return static_cast(ret.GetData().value()); } auto [isArrayBuffer, arrayBuffer] = ParseArrayBuffer(env, buf); @@ -119,12 +119,12 @@ ani_long WriteAni::WriteSync( HILOGE("Failed to resolve arrayBuffer!"); return -1; } - auto ret = WriteCore::DoWrite(fd, buffer, op); + auto ret = WriteCore::DoWrite(static_cast(fd), buffer, op); if (!ret.IsSuccess()) { HILOGE("write buffer failed!"); return -1; } - return ret.GetData().value(); + return static_cast(ret.GetData().value()); } HILOGE("Unsupported buffer type!"); return -1; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.h index 24d6be84..aeb6e2c4 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.h @@ -25,8 +25,8 @@ namespace ANI { class WriteAni final { public: - static ani_long WriteSync( - ani_env *env, [[maybe_unused]] ani_class clazz, ani_int fd, ani_object buf, ani_object options); + static ani_double WriteSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_object buf, ani_object options); }; } // namespace ANI } // namespace ModuleFileIO -- Gitee From af99b2737a7cd79e254063eb9d3f04b066cf1b5f Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 15 Mar 2025 18:49:27 +0800 Subject: [PATCH 19/76] =?UTF-8?q?1=E3=80=81unlink=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E4=BF=AE=E6=94=B9=E4=B8=BAvoid=EF=BC=9B2=E3=80=81ets?= =?UTF-8?q?=E4=B8=ADtruncateSync=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?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 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 41 ++++++++----------- .../src/mod_fs/properties/ani/unlink_ani.cpp | 7 ++-- .../js/src/mod_fs/properties/ani/unlink_ani.h | 2 +- 3 files changed, 20 insertions(+), 30 deletions(-) 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 c2ec3ebc..1dfa89cc 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 @@ -207,7 +207,7 @@ class StatInner implements Stat { } type FdOrFile = int | File; -type PathOrFd = string | int; +type PathOrFd = string | number; type BufferType = string | ArrayBuffer; class fileIo { @@ -724,38 +724,29 @@ class fileIo { }); } - static native truncateSync(file: PathOrFd, len?: long): void; + static native truncateSync(file: PathOrFd, len?: number): void; - static native unlinkSync(path: string): int; + static native unlinkSync(path: string): void; - static unlink(path: string): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.unlinkSync, path); + static unlink(path: string): Promise { + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - resolve(result); - } + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); }); }); } - static unlink(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.unlinkSync, path); + static unlink(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp index 7c8e7a8f..978ea3dd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp @@ -24,19 +24,18 @@ namespace FileManagement { namespace ModuleFileIO { namespace ANI { -ani_int UnlinkAni::UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +void UnlinkAni::UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) { auto [succ, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); - return -1; + return; } auto ret = UnlinkCore::DoUnlink(pathStr); if (!ret.IsSuccess()) { HILOGE("Unlink failed"); - return -1; + return; } - return 0; } } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h index 83bb1722..817b2580 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.h @@ -25,7 +25,7 @@ namespace ANI { class UnlinkAni final { public: - static ani_int UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); + static void UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); }; } // namespace ANI -- Gitee From 2c29654967da688694d58195a41c86635217763b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 18:52:15 +0800 Subject: [PATCH 20/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id2c8ce3a4343c22bd487d289725951a078055730 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 c2ec3ebc..d8292890 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 @@ -1286,7 +1286,7 @@ function openCallbackTest() { // console.println("writeSyncTest2 begin"); // try { // let mode = 2; -// let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); +// let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); // let fd = file.fd; // console.println(`file open fd=${fd}`); // const options: WriteOptions = { @@ -1304,7 +1304,7 @@ function openCallbackTest() { // console.println("writePromiseTest begin"); // try { // let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/c.txt", mode); +// let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); // let fd = file.fd; // let length = await fileIo.write(fd, "writePromiseTest"); // console.println(`writePromiseTest length=${length}`); @@ -1317,7 +1317,7 @@ function openCallbackTest() { // function writeCallbackTest() { // console.println("writeCallbackTest begin"); // let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/d.txt", mode); +// let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); // let fd = file.fd; // fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { // if (data !== undefined) { -- Gitee From 3e7e203736cacebc501ed0720775f51f008073bd Mon Sep 17 00:00:00 2001 From: liyuke Date: Sat, 15 Mar 2025 18:58:01 +0800 Subject: [PATCH 21/76] =?UTF-8?q?mkdir=E8=BF=94=E5=9B=9E=E5=80=BC=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BAvoid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 80 +++++++------------ .../src/mod_fs/properties/ani/mkdir_ani.cpp | 14 ++-- .../js/src/mod_fs/properties/ani/mkdir_ani.h | 4 +- 3 files changed, 38 insertions(+), 60 deletions(-) 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 d8292890..d4c0577b 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 @@ -408,73 +408,53 @@ class fileIo { }); } - static native mkdirSync(path: string): int; + static native mkdirSync(path: string): void; - static native mkdirSync(path: string, recursion: boolean): int; + static native mkdirSync(path: string, recursion: boolean): void; - static mkdir(path: string): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); + static mkdir(path: string): Promise { + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): void => fileIo.mkdirSync(path), path); promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - resolve(result); - } + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); }); }); } - static mkdir(path: string, callback: AsyncCallback): void { - const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); + static mkdir(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): void => fileIo.mkdirSync(path), path); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } - static mkdir(path: string, recursion: boolean): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - const mkdirSyncWrapper = (path: string, recursion: boolean) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); + static mkdir(path: string, recursion: boolean): Promise { + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, recursion: boolean): void => + fileIo.mkdirSync(path, recursion), path, recursion); promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - resolve(result); - } + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); }); }); } - static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - const mkdirSyncWrapper = (path: string, recursion: boolean,) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); + static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string, recursion: boolean): void => + fileIo.mkdirSync(path, recursion), path, recursion); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp index 12251930..1cade280 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp @@ -24,34 +24,32 @@ namespace FileManagement { namespace ModuleFileIO { namespace ANI { -ani_int MkdirkAni::MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +void MkdirkAni::MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) { auto [succ, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); - return -1; + return; } auto ret = MkdirCore::DoMkdir(pathStr); if (!ret.IsSuccess()) { HILOGE("Mkdir failed"); - return -1; + return; } - return 0; } -ani_int MkdirkAni::MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_boolean recursion) +void MkdirkAni::MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_boolean recursion) { auto [succ, pathStr] = ANI::TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); - return -1; + return; } auto ret = MkdirCore::DoMkdir(pathStr, recursion); if (!ret.IsSuccess()) { HILOGE("DoMkdir failed"); - return -1; + return; } - return 0; } } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h index fa342d95..8ed780e9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.h @@ -25,8 +25,8 @@ namespace ANI { class MkdirkAni final { public: - static ani_int MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); - static ani_int MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_boolean recursion); + static void MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); + static void MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_boolean recursion); }; } // namespace ANI -- Gitee From 38f09b3e8296281d72bdd18a77f96ce8df81e40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 19:11:34 +0800 Subject: [PATCH 22/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5840176857ac015f2d858e921d08cecd9364f0f9 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 35979b17..ab117e32 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 @@ -1246,8 +1246,8 @@ function openCallbackTest() { // function unlinkSyncTest() { // console.println("unlinkSyncTest begin"); // try { -// const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); -// console.println(`unlink result: ${ret}`); +// fileIo.unlinkSync("/data/local/tmp/a4.txt"); +// console.println(`unlink result`); // } catch (error) { // console.error("unlinkSyncTest Error!", error); -- Gitee From caf1db1082be1349af64cf348f1f3f7d35a9cc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 15 Mar 2025 11:12:40 +0000 Subject: [PATCH 23/76] =?UTF-8?q?!23=20close=20*=20Merge=20branch=20'ani?= =?UTF-8?q?=5Ferror=5Fhandler'=20of=20gitee.com:provii/OpenHarmony=5Ffilem?= =?UTF-8?q?=E2=80=A6=20*=20=E6=84=8F=E8=A7=81=E4=BF=AE=E5=A4=8D=20*=20clos?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 117 +++++++++++++----- .../src/mod_fs/properties/ani/close_ani.cpp | 13 +- .../js/src/mod_fs/properties/ani/close_ani.h | 2 +- 3 files changed, 90 insertions(+), 42 deletions(-) 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 ab117e32..a0daf35a 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 @@ -206,7 +206,7 @@ class StatInner implements Stat { native isSymbolicLink(): boolean; } -type FdOrFile = int | File; +type FdOrFile = number | File; type PathOrFd = string | number; type BufferType = string | ArrayBuffer; @@ -302,35 +302,25 @@ class fileIo { static native closeSync(file: FdOrFile): int; - static close(file: FdOrFile): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.closeSync, file); + static close(file: FdOrFile): Promise { + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((file: FdOrFile): void => fileIo.closeSync(file), file); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as int; - resolve(r); - } + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); }); }); } - static close(file: FdOrFile, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.closeSync, file); + static close(file: FdOrFile, callback: AsyncCallback): void { + let promise = taskpool.execute((file: FdOrFile): void => fileIo.closeSync(file), file); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } @@ -821,15 +811,72 @@ class fileIo { } } -function accessSyncTest1() { - console.println("accessSyncTest1 begin"); +// function accessSyncTest1() { +// console.println("accessSyncTest1 begin"); +// try { +// let ret = fileIo.accessSync("/data/local/tmp/a.txt"); +// console.println(`accessSync result : ${ret}`); +// } catch (error) { +// console.error("accessSyncTest1 Error!", error); +// } +// console.println("accessSyncTest1 end"); +// } + +// function closeSyncTest() { +// console.println("closeSyncTest begin"); +// let file = fileIo.openSync("/data/local/tmp/hello.txt"); +// fileIo.closeSync(file); +// fileIo.closeSync(64); +// console.println("closeSyncTest end"); +// } + +function closeTest() { + console.println("closeTest begin"); + let file = fileIo.openSync("/data/local/tmp/hello.txt"); try { - let ret = fileIo.accessSync("/data/local/tmp/a.txt"); - console.println(`accessSync result : ${ret}`); + await fileIo.close(file).then((result: undefined): void => { + console.println(`closePromiseTest close result: ${result}`); + }).catch((e: Object): void => { + console.error("async closePromiseTest Error!!!", e); + }); } catch (error) { - console.error("accessSyncTest1 Error!", error); + console.error("Promise: Error getting temp file:", error); } - console.println("accessSyncTest1 end"); + console.println("closeTest end"); + + try { + await fileIo.close(64).then((result: undefined): void => { + console.println(`closePromiseTest close result: ${result}`); + }).catch((e: Object): void => { + console.error("async closePromiseTest Error!!!", e); + }); + } catch (error) { + console.error("Promise number : Error getting temp file:", error); + } + console.println("closeTest end"); +} + +function closeTest1() { + console.println("closeCallbackTest begin"); + let file = fileIo.openSync("/data/local/tmp/hello.txt"); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + fileIo.close(file, (err: BusinessError) => { + if (err.code == 0) { + console.log("closeCallbackTest: close result!"); + } else { + console.error("closeCallbackTest: close Error:", err); + } + }); + + fileIo.close(64, (err: BusinessError) => { + if (err.code == 0) { + console.log("closeCallbackTest: close result!"); + } else { + console.error("closeCallbackTest: close Error:", err); + } + }); + console.println("closeCallbackTest end"); } // function closeSyncTest1() { @@ -1390,10 +1437,12 @@ function openCallbackTest() { function main() { console.println("---------- hello ani --------------"); - accessSyncTest1(); - openPromiseTest(); - openCallbackTest(); - openSyncTest(); + // accessSyncTest1(); + // openPromiseTest(); + // openCallbackTest(); + // openSyncTest(); + closeTest(); + closeTest1(); // closeSyncTest1(); // closeSyncTest2(); // closeSyncTest3(); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 313a5c14..773fb346 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -33,7 +33,7 @@ tuple ParseFd(ani_env *env, ani_object obj) { int32_t result = -1; ani_class IntClass; - env->FindClass("Lstd/core/Int;", &IntClass); + env->FindClass("Lstd/core/Double;", &IntClass); ani_boolean isInt; env->Object_InstanceOf(obj, IntClass, &isInt); if (isInt) { @@ -51,8 +51,8 @@ tuple ParseFd(ani_env *env, ani_object obj) ani_boolean isFile; env->Object_InstanceOf(obj, FileClass, &isFile); if (isFile) { - ani_int fd; - if (ANI_OK != env->Object_GetPropertyByName_Int(obj, "fd", &fd)) { + ani_double fd; + if (ANI_OK != env->Object_GetPropertyByName_Double(obj, "fd", &fd)) { HILOGE("Get fd in class file failed"); return { false, result }; } @@ -63,20 +63,19 @@ tuple ParseFd(ani_env *env, ani_object obj) return { false, result }; } -ani_int CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj) +void CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj) { auto [succ, fd] = ParseFd(env, obj); if (!succ) { HILOGE("Parse fd argument failed"); - return -1; + return; } auto ret = CloseCore::DoClose(fd); if (!ret.IsSuccess()) { HILOGE("Close %d failed", fd); - return -1; + return; } - return 0; } } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.h index 7c6202f6..cd2715eb 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.h @@ -25,7 +25,7 @@ namespace ANI { class CloseAni final { public: - static ani_int CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj); + static void CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj); }; } // namespace ANI -- Gitee From fdbb8c8fbcacadeb1667d74b599154488a5e4837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sat, 15 Mar 2025 11:47:55 +0000 Subject: [PATCH 24/76] =?UTF-8?q?!26=20=E7=B1=BB=E5=9E=8B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20*=20file=E7=B1=BB=E5=9E=8B=20*=20=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 2 +- interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 a0daf35a..5f338ad0 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 @@ -300,7 +300,7 @@ class fileIo { }); } - static native closeSync(file: FdOrFile): int; + static native closeSync(file: FdOrFile): void; static close(file: FdOrFile): Promise { return new Promise((resolve: (result: undefined) => void, diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 773fb346..750ec0cb 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -47,7 +47,7 @@ tuple ParseFd(ani_env *env, ani_object obj) } ani_class FileClass; - env->FindClass("Lfile_fs_class/FileInner;", &FileClass); + env->FindClass("L@ohos/file/fs/FileInner;", &FileClass); ani_boolean isFile; env->Object_InstanceOf(obj, FileClass, &isFile); if (isFile) { -- Gitee From 9acdb6b5988b2652ada5319d515d1ab5e2dd8c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 19:56:15 +0800 Subject: [PATCH 25/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I27868e8849889b7753f317e4dacafa7fe1822636 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 106 +++++------------- 1 file changed, 26 insertions(+), 80 deletions(-) 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 5f338ad0..d181c542 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 @@ -811,72 +811,15 @@ class fileIo { } } -// function accessSyncTest1() { -// console.println("accessSyncTest1 begin"); -// try { -// let ret = fileIo.accessSync("/data/local/tmp/a.txt"); -// console.println(`accessSync result : ${ret}`); -// } catch (error) { -// console.error("accessSyncTest1 Error!", error); -// } -// console.println("accessSyncTest1 end"); -// } - -// function closeSyncTest() { -// console.println("closeSyncTest begin"); -// let file = fileIo.openSync("/data/local/tmp/hello.txt"); -// fileIo.closeSync(file); -// fileIo.closeSync(64); -// console.println("closeSyncTest end"); -// } - -function closeTest() { - console.println("closeTest begin"); - let file = fileIo.openSync("/data/local/tmp/hello.txt"); +function accessSyncTest1() { + console.println("accessSyncTest1 begin"); try { - await fileIo.close(file).then((result: undefined): void => { - console.println(`closePromiseTest close result: ${result}`); - }).catch((e: Object): void => { - console.error("async closePromiseTest Error!!!", e); - }); + let ret = fileIo.accessSync("/data/local/tmp/a.txt"); + console.println(`accessSync result : ${ret}`); } catch (error) { - console.error("Promise: Error getting temp file:", error); + console.error("accessSyncTest1 Error!", error); } - console.println("closeTest end"); - - try { - await fileIo.close(64).then((result: undefined): void => { - console.println(`closePromiseTest close result: ${result}`); - }).catch((e: Object): void => { - console.error("async closePromiseTest Error!!!", e); - }); - } catch (error) { - console.error("Promise number : Error getting temp file:", error); - } - console.println("closeTest end"); -} - -function closeTest1() { - console.println("closeCallbackTest begin"); - let file = fileIo.openSync("/data/local/tmp/hello.txt"); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - fileIo.close(file, (err: BusinessError) => { - if (err.code == 0) { - console.log("closeCallbackTest: close result!"); - } else { - console.error("closeCallbackTest: close Error:", err); - } - }); - - fileIo.close(64, (err: BusinessError) => { - if (err.code == 0) { - console.log("closeCallbackTest: close result!"); - } else { - console.error("closeCallbackTest: close Error:", err); - } - }); - console.println("closeCallbackTest end"); + console.println("accessSyncTest1 end"); } // function closeSyncTest1() { @@ -884,8 +827,8 @@ function closeTest1() { // try { // let file = fileIo.openSync("/data/local/tmp/a.txt"); // console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// let ret = fileIo.closeSync(file.fd); -// console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); +// fileIo.closeSync(file.fd); +// console.println(`close file by fd, fd=${file.fd}`); // } catch (error) { // console.error("closeSyncTest1 Error!", error); // } @@ -897,8 +840,8 @@ function closeTest1() { // try { // let file = fileIo.openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// let ret = fileIo.closeSync(file); -// console.println(`close file by file, fd=${file.fd}, ret=${ret}`); +// fileIo.closeSync(file); +// console.println(`close file by file, fd=${file.fd}`); // } catch (error) { // console.error("closeSyncTest2 Error!", error); // } @@ -908,8 +851,8 @@ function closeTest1() { // function closeSyncTest3() { // console.println("closeSyncTest3 begin"); // try { -// let ret = fileIo.closeSync(-1); -// console.println(`close file by invalid fd: -1, ret=${ret}`); +// fileIo.closeSync(-1); +// console.println(`close file by invalid fd: -1`); // } catch (error) { // console.error("closeSyncTest3 Error!", error); // } @@ -921,8 +864,11 @@ function closeTest1() { // try { // let file = fileIo.openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.close(file.fd).then((result: int) => { +// let fd = file.fd as int; +// fileIo.close(fd).then((result: undefined): void => { // console.println(`closePromiseTest close result: ${result}`); +// }).catch((e: Object): void => { +// console.error("async closePromiseTest Error!!!", e); // }); // } catch (error) { // console.error("closePromiseTest Error:", error); @@ -934,13 +880,15 @@ function closeTest1() { // console.println("closeCallbackTest begin"); // let file = fileIo.openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.close(file, (err: BusinessError, data?: int) => { -// if (err.code) { -// console.error("closeCallbackTest: close Error:", err); + +// fileIo.close(file, (err: BusinessError) => { +// if (err.code == 0) { +// console.log("closeCallbackTest: close result!"); // } else { -// console.log("closeCallbackTest: close result:", data); +// console.error("closeCallbackTest: close Error:", err); // } // }); + // console.println("closeCallbackTest end"); // } @@ -1437,12 +1385,10 @@ function openCallbackTest() { function main() { console.println("---------- hello ani --------------"); - // accessSyncTest1(); - // openPromiseTest(); - // openCallbackTest(); - // openSyncTest(); - closeTest(); - closeTest1(); + accessSyncTest1(); + openPromiseTest(); + openCallbackTest(); + openSyncTest(); // closeSyncTest1(); // closeSyncTest2(); // closeSyncTest3(); -- Gitee From 39adae7b52ffb6a0119c84491891682d6d921046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Sat, 15 Mar 2025 20:30:36 +0800 Subject: [PATCH 26/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id91e2b66aa27242b46a6d154c0fa873a5c7387f6 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 4 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 63 ++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) 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 f05e7a52..16a06adc 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 @@ -77,8 +77,8 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, - ani_native_function { "mkdirSync", "Lstd/core/String;:I", reinterpret_cast(MkdirkAni::MkdirSync0) }, - ani_native_function { "mkdirSync", "Lstd/core/String;Z:I", reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, + ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, 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 0e4d9a81..2eb1c8b7 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 @@ -913,15 +913,60 @@ function accessSyncTest1() { // console.println("listFileTest end"); // } -// function mkdirTest1() { -// console.println("mkdirTest1 begin") +// function mkdirSyncTest1() { +// console.println("mkdirSyncTest1 begin") // try { -// const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); -// console.println(`mkdir result: ${ret}`); +// fileIo.mkdirSync("/data/local/tmp/dir01"); +// console.println(`mkdir result`); // } catch (error) { -// console.error("mkdirTest1 Error!", error); +// console.error("mkdirSyncTest1 Error!", error); // } -// console.println("mkdirTest1 end"); +// console.println("mkdirSyncTest1 end"); +// } + +// function mkdirSyncTest2() { +// console.println("mkdirSyncTest2 begin") +// try { +// fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); +// console.println(`mkdir result`); +// } catch (error) { +// console.error("mkdirSyncTest2 Error!", error); +// } +// console.println("mkdirSyncTest2 end"); +// } + +// function mkdirSyncTest3() { +// console.println("mkdirSyncTest3 begin") +// try { +// fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); +// console.println(`mkdir result`); +// } catch (error) { +// console.error("mkdirSyncTest3 Error!", error); +// } +// console.println("mkdirSyncTest3 end"); +// } + +// function mkdirPromiseTest() { +// console.println("mkdirPromiseTest begin") +// try { +// await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); +// console.println(`mkdir result`); +// } catch (error) { +// console.error("mkdirPromiseTest Error!", error); +// } +// console.println("mkdirPromiseTest end"); +// } + +// function mkdirCallbackTest() { +// console.println("mkdirCallbackTest begin") +// fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { +// if (err.code == 0) { +// console.println("mkdirCallbackTest success"); +// } else { +// console.println("mkdirCallbackTest failed!"); +// } +// }); +// console.println("mkdirCallbackTest end"); // } // function moveSyncTest() { @@ -1377,7 +1422,11 @@ function main() { // copyFileTest(); // fileTest(); // listFileTest(); - // mkdirTest1(); + // mkdirSyncTest1(); + // mkdirSyncTest2(); + // mkdirSyncTest3(); + // mkdirPromiseTest(); + // mkdirCallbackTest(); // moveSyncTest(); // readSyncTest1(); // readSyncTest2(); -- Gitee From 1ffbbbb599af659e36ad0837918dc4a581660cb7 Mon Sep 17 00:00:00 2001 From: tianp Date: Mon, 17 Mar 2025 03:13:38 +0000 Subject: [PATCH 27/76] =?UTF-8?q?!28=20BufferType=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=95=B4=E6=94=B9=20*=20BufferType=E7=B1=BB=E5=9E=8B=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 2eb1c8b7..2a9f2e5b 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 @@ -208,7 +208,6 @@ class StatInner implements Stat { type FdOrFile = number | File; type PathOrFd = string | number; -type BufferType = string | ArrayBuffer; class fileIo { @@ -720,17 +719,17 @@ class fileIo { }); } - static native writeSync(fd: number, buffer: BufferType, options?: WriteOptions): number; + static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; - static writeSync1(fd: number, buffer: BufferType, options: WriteOptions): number { + static writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { return fileIo.writeSync(fd, buffer, options); } - static writeSync2(fd: number, buffer: BufferType): number { + static writeSync2(fd: number, buffer: string | ArrayBuffer): number { return fileIo.writeSync(fd, buffer); } - static write(fd: number, buffer: BufferType, options: WriteOptions): Promise { + static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); promise.then((ret: NullishType) => { @@ -745,7 +744,7 @@ class fileIo { }); } - static write(fd: number, buffer: BufferType): Promise { + static write(fd: number, buffer: string | ArrayBuffer): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); promise.then((ret: NullishType) => { @@ -760,7 +759,7 @@ class fileIo { }); } - static write(fd: number, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { + static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); @@ -775,7 +774,7 @@ class fileIo { }); } - static write(fd: number, buffer: BufferType, callback: AsyncCallback): void { + static write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); -- Gitee From 0380f6ddfd03f52c882ef99e3aca889f503e63b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 17 Mar 2025 03:13:48 +0000 Subject: [PATCH 28/76] =?UTF-8?q?!27=20union=E5=8F=98=E6=9B=B4=20*=20union?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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 2a9f2e5b..21602e36 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 @@ -206,7 +206,6 @@ class StatInner implements Stat { native isSymbolicLink(): boolean; } -type FdOrFile = number | File; type PathOrFd = string | number; class fileIo { @@ -299,12 +298,12 @@ class fileIo { }); } - static native closeSync(file: FdOrFile): void; + static native closeSync(file: number | File): void; - static close(file: FdOrFile): Promise { + static close(file: number | File): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((file: FdOrFile): void => fileIo.closeSync(file), file); + let promise = taskpool.execute((file: number | File): void => fileIo.closeSync(file), file); promise.then((ret: NullishType) => { resolve(undefined); }).catch((e: BusinessError): void => { @@ -313,8 +312,8 @@ class fileIo { }); } - static close(file: FdOrFile, callback: AsyncCallback): void { - let promise = taskpool.execute((file: FdOrFile): void => fileIo.closeSync(file), file); + static close(file: number | File, callback: AsyncCallback): void { + let promise = taskpool.execute((file: number | File): void => fileIo.closeSync(file), file); promise.then((ret: NullishType) => { let e = new BusinessError(0, ""); callback(e, undefined); -- Gitee From b478d9d253c38d95b853fcc9246be39316e3575e Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 17 Mar 2025 11:25:00 +0800 Subject: [PATCH 29/76] =?UTF-8?q?truncateSync=20union=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 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 21602e36..5fd44bef 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 @@ -692,7 +692,7 @@ class fileIo { }); } - static native truncateSync(file: PathOrFd, len?: number): void; + static native truncateSync(file: string | number, len?: number): void; static native unlinkSync(path: string): void; -- Gitee From 9541afc5338ed2d498eaec4804ca7f5a2470e610 Mon Sep 17 00:00:00 2001 From: nieben Date: Mon, 17 Mar 2025 10:40:39 +0800 Subject: [PATCH 30/76] copyFileSync mode number Signed-off-by: nieben Change-Id: I5247a61013cd39c16b37081c6c65c148de391ae2 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 8 ++++---- interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) 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 2eb1c8b7..8d8157b3 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 @@ -324,7 +324,7 @@ class fileIo { }); } - static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; + static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; @@ -661,9 +661,9 @@ class fileIo { static native rmdirSync(path: string): void; - static native statSync(file: PathOrFd): Stat; + static native statSync(file: string | number): Stat; - static stat(file: PathOrFd): Promise { + static stat(file: string | number): Promise { return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.statSync, file); promise.then((ret: NullishType) => { @@ -679,7 +679,7 @@ class fileIo { }); } - static stat(file: PathOrFd, callback: AsyncCallback): void { + static stat(file: string | number, callback: AsyncCallback): void { let p = taskpool.execute(fileIo.statSync, file); p.then((ret: NullishType) => { let err = new BusinessError(-1, ""); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index 3c9161cb..23ec937f 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -133,11 +133,13 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & } } +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if ((ret = SetEnumLocation(env, cls, statObject, "location", static_cast(fsStat->GetLocation()))) != ANI_OK) { HILOGE("Object_CallMethod_Void Fail location, err: %{private}d", ret); return ret; } +#endif return ANI_OK; } -- Gitee From c96ae094193b7ee3978ff53cd4be66140e8099c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Mon, 17 Mar 2025 11:35:04 +0800 Subject: [PATCH 31/76] =?UTF-8?q?read=20fd=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 兔叽和猪扒 --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 21602e36..6822e817 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 @@ -513,17 +513,17 @@ class fileIo { }); } - static native readSync(fd: int, buffer: ArrayBuffer, options?: ReadOptions): long; + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): long; - static readSync1(fd: int, buffer: ArrayBuffer): long { + static readSync1(fd: number, buffer: ArrayBuffer): long { return fileIo.readSync(fd, buffer); } - static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { + static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): long { return fileIo.readSync(fd, buffer, options); } - static read(fd: int, buffer: ArrayBuffer): Promise { + static read(fd: number, buffer: ArrayBuffer): Promise { return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readSync1, fd, buffer); promise.then((ret: NullishType) => { @@ -539,7 +539,7 @@ class fileIo { }); } - static read(fd: int, buffer: ArrayBuffer, options: ReadOptions): Promise { + static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); promise.then((ret: NullishType) => { @@ -555,7 +555,7 @@ class fileIo { }); } - static read(fd: int, buffer: ArrayBuffer, callback: AsyncCallback): void { + static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readSync1, fd, buffer); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); @@ -570,7 +570,7 @@ class fileIo { }); } - static read(fd: int, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); promise.then((ret: NullishType) => { let err = new BusinessError(-1, ""); -- Gitee From f1f6236e374eecb5fc4e807b3a6046e7dc913fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Mon, 17 Mar 2025 11:39:47 +0800 Subject: [PATCH 32/76] =?UTF-8?q?read=20ani=E4=BF=AE=E6=94=B9?= 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/read_ani.cpp | 8 ++++---- interfaces/kits/js/src/mod_fs/properties/ani/read_ani.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp index 330d75c3..016c0960 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp @@ -51,8 +51,8 @@ static tuple> ToReadOptions(ani_env *env, ani_object return { true, make_optional(move(options)) }; } -ani_long ReadAni::ReadSync( - ani_env *env, [[maybe_unused]] ani_class clazz, ani_int fd, ani_arraybuffer buffer, ani_object options) +ani_double ReadAni::ReadSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_arraybuffer buffer, ani_object options) { auto [succBuf, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer); if (!succBuf) { @@ -66,11 +66,11 @@ ani_long ReadAni::ReadSync( return -1; } - auto ret = ReadCore::DoRead(fd, arrayBuffer, op); + auto ret = ReadCore::DoRead(static_cast(fd), arrayBuffer, op); if (!ret.IsSuccess()) { HILOGE("Read file content failed!"); return -1; } - return ret.GetData().value(); + return static_cast(ret.GetData().value()); } } // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.h index 027fd540..ceec384b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.h +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.h @@ -22,8 +22,8 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { class ReadAni final { public: - static ani_long ReadSync( - ani_env *env, [[maybe_unused]] ani_class clazz, ani_int fd, ani_arraybuffer buffer, ani_object options); + static ani_double ReadSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_arraybuffer buffer, ani_object options); }; } // namespace OHOS::FileManagement::ModuleFileIO::ANI #endif // INTERFACES_KITS_JS_SRC_MOD_FS_READ_ANI_H \ No newline at end of file -- Gitee From 28e8e9ed691fca6571c68256d71e0eb1a07f1d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 17 Mar 2025 05:49:51 +0000 Subject: [PATCH 33/76] =?UTF-8?q?!32=20=E5=8F=82=E6=95=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D=20*=20=E5=8F=82=E6=95=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 514d8d69..2361312f 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 @@ -35,7 +35,7 @@ tuple ParseBooleanParam(ani_env *env, ani_object obj, string tag) return { false, false }; } env->Reference_IsUndefined(bool_ref, &isUndefined); - if (!isUndefined) { + if (isUndefined) { return { true, false }; } ani_boolean bool_ref_res; -- Gitee From 6fde1738457ddc67024df37f7e255f2ec86aaee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 17 Mar 2025 14:07:38 +0800 Subject: [PATCH 34/76] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=B2=E7=9F=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ia007915e263b6a2d71314fa860ea30f602c914a2 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 1273 +++++++++-------- .../mod_fs/properties/ani/listfile_ani.cpp | 14 +- 2 files changed, 664 insertions(+), 623 deletions(-) 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 cf343924..c2294a03 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 @@ -100,8 +100,8 @@ export interface ReadTextOptions extends ReadOptions { } export interface WriteOptions { - offset?: long; - length?: long; + offset?: number; + length?: number; encoding?: string; } @@ -206,8 +206,6 @@ class StatInner implements Stat { native isSymbolicLink(): boolean; } -type PathOrFd = string | number; - class fileIo { static { @@ -240,7 +238,7 @@ class fileIo { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { if (mode === undefined) { let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -252,7 +250,7 @@ class fileIo { }); } else { let promise = taskpool.execute(fileIo.accessSync2, path, mode); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -269,7 +267,7 @@ class fileIo { static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -284,7 +282,7 @@ class fileIo { static access(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { @@ -301,10 +299,9 @@ class fileIo { static native closeSync(file: number | File): void; static close(file: number | File): Promise { - return new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((file: number | File): void => fileIo.closeSync(file), file); - promise.then((ret: NullishType) => { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((file: number | File): undefined => fileIo.closeSync(file), file); + promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { reject(e); @@ -313,8 +310,8 @@ class fileIo { } static close(file: number | File, callback: AsyncCallback): void { - let promise = taskpool.execute((file: number | File): void => fileIo.closeSync(file), file); - promise.then((ret: NullishType) => { + let promise = taskpool.execute((file: number | File): undefined => fileIo.closeSync(file), file); + promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); callback(e, undefined); }).catch((e: BusinessError): void => { @@ -337,7 +334,7 @@ class fileIo { static listFile(path: string): Promise { return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -353,7 +350,7 @@ class fileIo { static listFile(path: string, options: ListFileOptions): Promise { return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -368,7 +365,7 @@ class fileIo { static listFile(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -383,7 +380,7 @@ class fileIo { static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -401,10 +398,9 @@ class fileIo { static native mkdirSync(path: string, recursion: boolean): void; static mkdir(path: string): Promise { - return new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): void => fileIo.mkdirSync(path), path); - promise.then((ret: NullishType) => { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); + promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { reject(e); @@ -413,8 +409,8 @@ class fileIo { } static mkdir(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): void => fileIo.mkdirSync(path), path); - promise.then((ret: NullishType) => { + let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); + promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); callback(e, undefined); }).catch((e: BusinessError): void => { @@ -423,11 +419,10 @@ class fileIo { } static mkdir(path: string, recursion: boolean): Promise { - return new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string, recursion: boolean): void => + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, recursion: boolean): undefined => fileIo.mkdirSync(path, recursion), path, recursion); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { reject(e); @@ -436,9 +431,9 @@ class fileIo { } static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, recursion: boolean): void => + let promise = taskpool.execute((path: string, recursion: boolean): undefined => fileIo.mkdirSync(path, recursion), path, recursion); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); callback(e, undefined); }).catch((e: BusinessError): void => { @@ -461,7 +456,7 @@ class fileIo { static open(path: String, mode: number): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync1, path, mode); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); }).catch((e: BusinessError): void => { @@ -473,7 +468,7 @@ class fileIo { static open(path: String): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); err.code = -1; @@ -488,7 +483,7 @@ class fileIo { static open(path: String, mode: number, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.openSync1, path, mode); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); let file = ret as File; callback(e, file); @@ -500,7 +495,7 @@ class fileIo { static open(path: String, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -513,73 +508,73 @@ class fileIo { }); } - static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): long; + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; - static readSync1(fd: number, buffer: ArrayBuffer): long { + static readSync1(fd: number, buffer: ArrayBuffer): number { return fileIo.readSync(fd, buffer); } - static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): long { + static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { return fileIo.readSync(fd, buffer, options); } - static read(fd: number, buffer: ArrayBuffer): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + static read(fd: number, buffer: ArrayBuffer): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); reject(err); } else { - let r = ret as long; + let r = ret as number; resolve(r); } }); }); } - static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { + static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); reject(err); } else { - let r = ret as long; + let r = ret as number; resolve(r); } }); }); } - static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { + static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); } else { err.code = 0; - let r = ret as long; + let r = ret as number; callback(err, r); } }); } - static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; callback(err, undefined); } else { err.code = 0; - let r = ret as long; + let r = ret as number; callback(err, r); } }); @@ -598,7 +593,7 @@ class fileIo { static readText(filePath: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -614,7 +609,7 @@ class fileIo { static readText(filePath: string, options: ReadTextOptions): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -629,7 +624,7 @@ class fileIo { static readText(filePath: string, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -644,7 +639,7 @@ class fileIo { static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -664,7 +659,7 @@ class fileIo { static stat(file: string | number): Promise { return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.statSync, file); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -679,7 +674,7 @@ class fileIo { static stat(file: string | number, callback: AsyncCallback): void { let p = taskpool.execute(fileIo.statSync, file); - p.then((ret: NullishType) => { + p.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1; @@ -700,7 +695,7 @@ class fileIo { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { reject(e); @@ -710,7 +705,7 @@ class fileIo { static unlink(path: string, callback: AsyncCallback): void { let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); callback(e, undefined); }).catch((e: BusinessError): void => { @@ -731,7 +726,7 @@ class fileIo { static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let result = ret as number if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -746,7 +741,7 @@ class fileIo { static write(fd: number, buffer: string | ArrayBuffer): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let result = ret as number if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); @@ -760,7 +755,7 @@ class fileIo { static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 @@ -775,7 +770,7 @@ class fileIo { static write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType) => { + promise.then((ret: NullishType): void => { let err = new BusinessError(-1, ""); if (ret === null || ret === undefined) { err.code = -1 @@ -800,178 +795,206 @@ function accessSyncTest1() { console.println("accessSyncTest1 end"); } -// function closeSyncTest1() { -// console.println("closeSyncTest1 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.closeSync(file.fd); -// console.println(`close file by fd, fd=${file.fd}`); -// } catch (error) { -// console.error("closeSyncTest1 Error!", error); -// } -// console.println("closeSyncTest1 end"); -// } - -// function closeSyncTest2() { -// console.println("closeSyncTest2 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.closeSync(file); -// console.println(`close file by file, fd=${file.fd}`); -// } catch (error) { -// console.error("closeSyncTest2 Error!", error); -// } -// console.println("closeSyncTest2 end"); -// } - -// function closeSyncTest3() { -// console.println("closeSyncTest3 begin"); -// try { -// fileIo.closeSync(-1); -// console.println(`close file by invalid fd: -1`); -// } catch (error) { -// console.error("closeSyncTest3 Error!", error); -// } -// console.println("closeSyncTest3 end"); -// } - -// function closePromiseTest() { -// console.println("closePromiseTest begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// let fd = file.fd as int; -// fileIo.close(fd).then((result: undefined): void => { -// console.println(`closePromiseTest close result: ${result}`); -// }).catch((e: Object): void => { -// console.error("async closePromiseTest Error!!!", e); -// }); -// } catch (error) { -// console.error("closePromiseTest Error:", error); -// } -// console.println("closePromiseTest end"); -// } - -// function closeCallbackTest() { -// console.println("closeCallbackTest begin"); -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - -// fileIo.close(file, (err: BusinessError) => { -// if (err.code == 0) { -// console.log("closeCallbackTest: close result!"); -// } else { -// console.error("closeCallbackTest: close Error:", err); -// } -// }); - -// console.println("closeCallbackTest end"); -// } - -// function copyFileTest() { -// console.println("copyFileTest begin"); -// try { -// fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); -// } catch (error) { -// console.error("copyFileTest Error!", error); -// } -// console.println("copyFileTest end"); -// } - -// function fileTest() { -// console.println("fileTest begin"); -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// let fd = file.fd; -// console.println(`open file, fd=${fd}`); -// let parent = file.getParent(); -// console.println(`file parent=${parent}`); -// file.lock(); -// console.println(`file lock completed`); -// file.unlock(); -// console.println(`file unlock completed`); -// file.tryLock(true); -// console.println(`file tryLock completed`); -// file.unlock(); -// console.println(`file unlock completed`); -// console.println("fileTest end"); -// } - -// function listFileTest() { -// console.println("listFileTest begin"); -// try { -// let files = fileIo.listFileSync("/data/local/tmp/"); -// for (const file of files) { -// console.println(file); -// } -// } catch (error) { -// console.error("listFileTest Error!", error); -// } -// console.println("listFileTest end"); -// } - -// function mkdirSyncTest1() { -// console.println("mkdirSyncTest1 begin") -// try { -// fileIo.mkdirSync("/data/local/tmp/dir01"); -// console.println(`mkdir result`); -// } catch (error) { -// console.error("mkdirSyncTest1 Error!", error); -// } -// console.println("mkdirSyncTest1 end"); -// } - -// function mkdirSyncTest2() { -// console.println("mkdirSyncTest2 begin") -// try { -// fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); -// console.println(`mkdir result`); -// } catch (error) { -// console.error("mkdirSyncTest2 Error!", error); -// } -// console.println("mkdirSyncTest2 end"); -// } - -// function mkdirSyncTest3() { -// console.println("mkdirSyncTest3 begin") -// try { -// fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); -// console.println(`mkdir result`); -// } catch (error) { -// console.error("mkdirSyncTest3 Error!", error); -// } -// console.println("mkdirSyncTest3 end"); -// } - -// function mkdirPromiseTest() { -// console.println("mkdirPromiseTest begin") -// try { -// await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); -// console.println(`mkdir result`); -// } catch (error) { -// console.error("mkdirPromiseTest Error!", error); -// } -// console.println("mkdirPromiseTest end"); -// } - -// function mkdirCallbackTest() { -// console.println("mkdirCallbackTest begin") -// fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { -// if (err.code == 0) { -// console.println("mkdirCallbackTest success"); -// } else { -// console.println("mkdirCallbackTest failed!"); -// } -// }); -// console.println("mkdirCallbackTest end"); -// } - -// function moveSyncTest() { -// console.println("moveSyncTest begin") -// fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) -// console.println("moveSyncTest end") -// } +function closeSyncTest1() { + console.println("closeSyncTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.closeSync(file.fd); + console.println(`close file by fd, fd=${file.fd}`); + } catch (error) { + console.error("closeSyncTest1 Error!", error); + } + console.println("closeSyncTest1 end"); +} + +function closeSyncTest2() { + console.println("closeSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.closeSync(file); + console.println(`close file by file, fd=${file.fd}`); + } catch (error) { + console.error("closeSyncTest2 Error!", error); + } + console.println("closeSyncTest2 end"); +} + +function closeSyncTest3() { + console.println("closeSyncTest3 begin"); + try { + fileIo.closeSync(-1); + console.println(`close file by invalid fd: -1`); + } catch (error) { + console.error("closeSyncTest3 Error!", error); + } + console.println("closeSyncTest3 end"); +} + +function closePromiseTest() { + console.println("closePromiseTest begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + fileIo.close(file.fd).then((result: undefined): void => { + console.println(`closePromiseTest close success`); + }).catch((e: Object): void => { + console.error("async closePromiseTest Error!!!", e); + }); + } catch (error) { + console.error("closePromiseTest Error:", error); + } + console.println("closePromiseTest end"); +} + +function closePromiseTest2() { + console.println("closePromiseTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + await fileIo.close(file.fd); + console.println(`closePromiseTest2 close success`); + } catch (error) { + console.error("closePromiseTest2 Error:", error); + } + console.println("closePromiseTest2 end"); +} + +function closeCallbackTest() { + console.println("closeCallbackTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + fileIo.close(file, (err: BusinessError) => { + if (err.code == 0) { + console.log("closeCallbackTest: close success!"); + } else { + console.error("closeCallbackTest: close Error:", err); + } + }); + + console.println("closeCallbackTest end"); +} + +function copyFileTest() { + console.println("copyFileTest begin"); + try { + fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); + } catch (error) { + console.error("copyFileTest Error!", error); + } + console.println("copyFileTest end"); +} + +function fileTest() { + console.println("fileTest begin"); + let file = fileIo.openSync("/data/local/tmp/a.txt"); + let fd = file.fd; + console.println(`open file, fd=${fd}`); + let parent = file.getParent(); + console.println(`file parent=${parent}`); + file.lock(); + console.println(`file lock completed`); + file.unlock(); + console.println(`file unlock completed`); + file.tryLock(true); + console.println(`file tryLock completed`); + file.unlock(); + console.println(`file unlock completed`); + console.println("fileTest end"); +} + +function listFileSyncTest1() { + console.println("listFileSyncTest1 begin"); + try { + let files = fileIo.listFileSync("/data/local/tmp/"); + for (const file of files) { + console.println(file); + } + } catch (error) { + console.error("listFileSyncTest1 Error!", error); + } + console.println("listFileSyncTest1 end"); +} + +function listFileSyncTest2() { + console.println("listFileSyncTest2 begin"); + try { + let options: ListFileOptions = { + recursion: true + }; + let files = fileIo.listFileSync("/data/", options); + for (const file of files) { + console.println(file); + } + } catch (error) { + console.error("listFileSyncTest2 Error!", error); + } + console.println("listFileSyncTest2 end"); +} + +function mkdirSyncTest1() { + console.println("mkdirSyncTest1 begin") + try { + fileIo.mkdirSync("/data/local/tmp/dir01"); + console.println(`mkdirSyncTest1 success`); + } catch (error) { + console.error("mkdirSyncTest1 Error!", error); + } + console.println("mkdirSyncTest1 end"); +} + +function mkdirSyncTest2() { + console.println("mkdirSyncTest2 begin") + try { + fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); + console.println(`mkdirSyncTest2 success`); + } catch (error) { + console.error("mkdirSyncTest2 Error!", error); + } + console.println("mkdirSyncTest2 end"); +} + +function mkdirSyncTest3() { + console.println("mkdirSyncTest3 begin") + try { + fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); + console.println(`mkdirSyncTest3 success`); + } catch (error) { + console.error("mkdirSyncTest3 Error!", error); + } + console.println("mkdirSyncTest3 end"); +} + +function mkdirPromiseTest() { + console.println("mkdirPromiseTest begin") + try { + await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); + console.println(`mkdirPromiseTest success`); + } catch (error) { + console.error("mkdirPromiseTest Error!", error); + } + console.println("mkdirPromiseTest end"); +} + +function mkdirCallbackTest() { + console.println("mkdirCallbackTest begin") + fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { + if (err.code == 0) { + console.println("mkdirCallbackTest success"); + } else { + console.println("mkdirCallbackTest failed!"); + } + }); + console.println("mkdirCallbackTest end"); +} + +function moveSyncTest() { + console.println("moveSyncTest begin") + fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) + console.println("moveSyncTest end") +} function openSyncTest() { console.println("openSyncTest begin"); @@ -1026,385 +1049,400 @@ function openCallbackTest() { }); } -// function readSyncTest1() { -// console.println("readSyncTest1 begin") -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let length = fileIo.readSync(fd, buffer); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest1 Error!", error); -// } -// console.println("readSyncTest1 end") -// } - -// function readSyncTest2() { -// console.println("readSyncTest2 begin") -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let options: ReadOptions = { -// length: 5, -// } -// let length = fileIo.readSync(fd, buffer, options); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest2 Error!", error); -// } -// console.println("readSyncTest2 end") -// } - -// function readSyncTest3() { -// console.println("readSyncTest3 begin") -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let options: ReadOptions = { -// offset: 3, -// length: 5, -// } -// let length = fileIo.readSync(fd, buffer, options); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest3 Error!", error); -// } -// console.println("readSyncTest3 end") -// } - -// function readTextSyncTest1() { -// console.println("readTextSyncTest1 begin") -// try { -// let content = fileIo.readTextSync("/data/local/tmp/a.txt"); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readTextSyncTest1 Error!", error); -// } -// console.println("readTextSyncTest1 end") -// } - -// function readTextSyncTest2() { -// console.println("readTextSyncTest2 begin"); -// try { -// let options: ReadTextOptions = { -// length: 5, -// } -// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); -// console.println("-----------------------------------------------"); -// console.println(content); -// console.println("-----------------------------------------------"); -// } catch (error) { -// console.error("readTextSyncTest2 Error!", error); -// } -// console.println("readTextSyncTest2 end"); -// } - -// function readTextSyncTest3() { -// console.println("readTextSyncTest3 begin") -// try { -// let options: ReadTextOptions = { -// offset: 3, -// length: 5, -// } -// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); -// console.println("-----------------------------------------------"); -// console.println(content); -// console.println("-----------------------------------------------"); -// } catch (error) { -// console.error("readTextSyncTest3 Error!", error); -// } -// console.println("readTextSyncTest3 end"); -// } - -// function rmdirSyncTest() { -// console.println("rmdirSyncTest begin"); -// try { -// fileIo.rmdirSync("/data/local/tmp/dir01"); -// } catch (error) { -// console.error("rmdirSyncTest Error!", error); -// } -// console.println("rmdirSyncTest end"); -// } - -// function printStatInfo(stat?: Stat) { -// if (stat === undefined) { -// console.error("stat is null or undefined!"); -// return; -// } -// console.info("stat, ino is " + stat.ino); -// console.info("stat, mode is " + stat.mode); -// console.info("stat, uid is " + stat.uid); -// console.info("stat, gid is " + stat.gid); -// console.info("stat, size is " + stat.size); -// console.info("stat, atime is " + stat.atime); -// console.info("stat, mtime is " + stat.mtime); -// console.info("stat, ctime is " + stat.ctime); -// console.info("stat, atimeNs is " + stat.atimeNs); -// console.info("stat, mtimeNs is " + stat.mtimeNs); -// console.info("stat, ctimeNs is " + stat.ctimeNs); -// console.info("stat, location is " + stat.location); -// console.info("stat, isBlockDevice is " + stat.isBlockDevice()); -// console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); -// console.info("stat, isDirectory is " + stat.isDirectory()); -// console.info("stat, isFIFO is " + stat.isFIFO()); -// console.info("stat, isFile is " + stat.isFile()); -// console.info("stat, isSocket is " + stat.isSocket()); -// console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); -// } - -// function statSyncTest1() { -// console.println("statSyncTest1 begin"); -// try { -// let stat = fileIo.statSync("/data/local/tmp/a.txt"); -// printStatInfo(stat); -// } catch (error) { -// console.error("statSyncTest1 Error!", error); -// } -// console.println("statSyncTest1 end"); -// } - -// function statSyncTest2() { -// console.println("statSyncTest2 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println(`open file fd: ${file.fd}`); -// let stat = fileIo.statSync(file.fd); -// printStatInfo(stat); -// } catch (error) { -// console.error("statSyncTest2 Error!", error); -// } -// console.println("statSyncTest2 end"); -// } - -// function statPromiseTest() { -// console.println("statPromiseTest start"); -// try { -// let path = "/data/local/tmp/a.txt" -// let file = fileIo.openSync(path); -// console.println(`open file fd: ${file.fd}`); -// fileIo.stat(path).then((result: Stat) => { -// console.log("statPromiseTest success"); -// printStatInfo(result); -// }); -// } catch (error) { -// console.error("statPromiseTest Error!", error); -// } -// console.println("statPromiseTest end"); -// } - -// function statCallbackTest() { -// console.println("statCallbackTest start"); -// let path = "/data/local/tmp/a.txt" -// fileIo.stat(path, (err: BusinessError, data?: Stat) => { -// if (data === undefined || data === null) { -// console.error("Callback: Error stat:", err); -// } else { -// console.log("Callback stat success"); -// printStatInfo(data); -// } -// }); -// console.println("statCallbackTest end"); -// } - -// function truncateSyncTest1() { -// console.println("truncateSyncTest1 begin"); -// try { -// fileIo.truncateSync(-1, 4); -// } catch (error) { -// console.error("truncateSyncTest1 Error!", error); -// } -// console.println("truncateSyncTest1 end"); -// } - -// function truncateSyncTest2() { -// console.println("truncateSyncTest2 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file, fd=${file.fd}`); -// fileIo.truncateSync(file.fd, 4); -// } catch (error) { -// console.error("truncateSyncTest2 Error!", error); -// } -// console.println("truncateSyncTest2 end"); -// } - -// function truncateSyncTest3() { -// console.println("truncateSyncTest3 begin"); -// try { -// fileIo.truncateSync("/data/local/tmp/b.txt", 4); -// } catch (error) { -// console.error("truncateSyncTest3 Error!", error); -// } -// console.println("truncateSyncTest3 end"); -// } - -// function unlinkSyncTest() { -// console.println("unlinkSyncTest begin"); -// try { -// fileIo.unlinkSync("/data/local/tmp/a4.txt"); -// console.println(`unlink result`); - -// } catch (error) { -// console.error("unlinkSyncTest Error!", error); -// } -// console.println("unlinkSyncTest end"); -// } - -// function writeSyncTest1() { -// console.println("writeSyncTest1 begin"); -// try { -// let mode = 2; -// let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); -// let fd = file.fd; -// console.println(`file open fd=${fd}`); -// const options: WriteOptions = { -// encoding: "utf-8", -// }; -// let length = fileIo.writeSync(fd, "hello,world!", options); -// console.println(`write file length=${length}`); -// } catch (error) { -// console.error("writeSyncTest1 Error!", error); -// } -// console.println("writeSyncTest1 end"); -// } - -// function writeSyncTest2() { -// console.println("writeSyncTest2 begin"); -// try { -// let mode = 2; -// let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); -// let fd = file.fd; -// console.println(`file open fd=${fd}`); -// const options: WriteOptions = { -// offset: 100, -// }; -// let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); -// console.println(`write file length=${length}`); -// } catch (error) { -// console.error("writeSyncTest2 Error!", error); -// } -// console.println("writeSyncTest2 end"); -// } - -// function writePromiseTest() { -// console.println("writePromiseTest begin"); -// try { -// let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); -// let fd = file.fd; -// let length = await fileIo.write(fd, "writePromiseTest"); -// console.println(`writePromiseTest length=${length}`); -// } catch (error) { -// console.error("writePromiseTest Error!", error); -// } -// console.println("writePromiseTest end"); -// } - -// function writeCallbackTest() { -// console.println("writeCallbackTest begin"); -// let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); -// let fd = file.fd; -// fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { -// if (data !== undefined) { -// console.println(`writeCallbackTest length=${data}`); -// } else { -// console.error("writeCallbackTest Error!", err); -// } -// }); -// console.println("writeCallbackTest end"); -// } - -// function errorHandlerTest1() { -// console.println("errorHandlerTest1 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - -// console.println("\n"); -// console.println("try open file notexist.txt ..."); -// let mode = 2; -// file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); -// console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest1 Error!", error); -// } -// console.println("errorHandlerTest1 end"); -// } - -// function errorHandlerTest2() { -// console.println("errorHandlerTest1 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println("try open file a.txt ..."); -// let mode = -1; -// file = fileIo.openSync("/data/local/tmp/a.txt", mode); -// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest2 Error!", error); -// } -// console.println("errorHandlerTest2 end"); -// } - -// function errorHandlerTest3() { -// console.println("errorHandlerTest3 begin"); -// try { -// let file = await fileIo.open("/data/local/tmp/a.txt"); -// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// console.println("-----------"); -// console.println("try open file a.txt ..."); -// let mode = -1; -// file = await fileIo.open("/data/local/tmp/a.txt", mode); -// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest3 Error!", error); -// } -// console.println("errorHandlerTest3 end"); -// } - -// function errorHandlerTest4() { -// console.println("errorHandlerTest4 begin"); -// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { -// if (err.code == 0 && data !== undefined) { -// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open file Error!", err); -// } -// }); - -// fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { -// console.println("2)-----------"); -// if (err.code == 0 && data !== undefined) { -// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open file Error!", err); -// } -// }); -// console.println("errorHandlerTest4 end"); -// } +function readSyncTest1() { + console.println("readSyncTest1 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let length = fileIo.readSync(fd, buffer); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest1 Error!", error); + } + console.println("readSyncTest1 end") +} + +function readSyncTest2() { + console.println("readSyncTest2 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest2 Error!", error); + } + console.println("readSyncTest2 end") +} + +function readSyncTest3() { + console.println("readSyncTest3 begin") + try { + let file = fileIo.openSync("/data/local/tmp/a.txt") + let fd = file.fd; + console.println(`open file, fd=${fd}`) + let buffer = new ArrayBuffer(100); + let options: ReadOptions = { + offset: 3, + length: 5, + } + let length = fileIo.readSync(fd, buffer, options); + console.println(`read length: ${length}`) + let len = length as int; + let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readSyncTest3 Error!", error); + } + console.println("readSyncTest3 end") +} + +function readTextSyncTest1() { + console.println("readTextSyncTest1 begin") + try { + let content = fileIo.readTextSync("/data/local/tmp/a.txt"); + console.println("-----------------------------------------------") + console.println(content) + console.println("-----------------------------------------------") + } catch (error) { + console.error("readTextSyncTest1 Error!", error); + } + console.println("readTextSyncTest1 end") +} + +function readTextSyncTest2() { + console.println("readTextSyncTest2 begin"); + try { + let options: ReadTextOptions = { + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest2 Error!", error); + } + console.println("readTextSyncTest2 end"); +} + +function readTextSyncTest3() { + console.println("readTextSyncTest3 begin") + try { + let options: ReadTextOptions = { + offset: 3, + length: 5, + } + let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); + console.println("-----------------------------------------------"); + console.println(content); + console.println("-----------------------------------------------"); + } catch (error) { + console.error("readTextSyncTest3 Error!", error); + } + console.println("readTextSyncTest3 end"); +} + +function rmdirSyncTest() { + console.println("rmdirSyncTest begin"); + try { + fileIo.rmdirSync("/data/local/tmp/dir01"); + } catch (error) { + console.error("rmdirSyncTest Error!", error); + } + console.println("rmdirSyncTest end"); +} + +function printStatInfo(stat?: Stat) { + if (stat === undefined) { + console.error("stat is null or undefined!"); + return; + } + console.info("stat, ino is " + stat.ino); + console.info("stat, mode is " + stat.mode); + console.info("stat, uid is " + stat.uid); + console.info("stat, gid is " + stat.gid); + console.info("stat, size is " + stat.size); + console.info("stat, atime is " + stat.atime); + console.info("stat, mtime is " + stat.mtime); + console.info("stat, ctime is " + stat.ctime); + console.info("stat, atimeNs is " + stat.atimeNs); + console.info("stat, mtimeNs is " + stat.mtimeNs); + console.info("stat, ctimeNs is " + stat.ctimeNs); + if (stat.location !== undefined) { + console.info("stat, location is " + stat.location); + } + console.info("stat, isBlockDevice is " + stat.isBlockDevice()); + console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); + console.info("stat, isDirectory is " + stat.isDirectory()); + console.info("stat, isFIFO is " + stat.isFIFO()); + console.info("stat, isFile is " + stat.isFile()); + console.info("stat, isSocket is " + stat.isSocket()); + console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); +} + +function statSyncTest1() { + console.println("statSyncTest1 begin"); + try { + let stat = fileIo.statSync("/data/local/tmp/a.txt"); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest1 Error!", error); + } + console.println("statSyncTest1 end"); +} + +function statSyncTest2() { + console.println("statSyncTest2 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file fd: ${file.fd}`); + let stat = fileIo.statSync(file.fd); + printStatInfo(stat); + } catch (error) { + console.error("statSyncTest2 Error!", error); + } + console.println("statSyncTest2 end"); +} + +function statPromiseTest() { + console.println("statPromiseTest start"); + try { + let path = "/data/local/tmp/a.txt" + let file = fileIo.openSync(path); + console.println(`open file fd: ${file.fd}`); + fileIo.stat(path).then((result: Stat): void => { + console.log("statPromiseTest success"); + printStatInfo(result); + }); + } catch (error) { + console.error("statPromiseTest Error!", error); + } + console.println("statPromiseTest end"); +} + +function statCallbackTest() { + console.println("statCallbackTest start"); + let path = "/data/local/tmp/a.txt" + fileIo.stat(path, (err: BusinessError, data?: Stat) => { + if (data === undefined || data === null) { + console.error("Callback: Error stat:", err); + } else { + console.log("Callback stat success"); + printStatInfo(data); + } + }); + console.println("statCallbackTest end"); +} + +function truncateSyncTest1() { + console.println("truncateSyncTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt", 2); + console.println(`open file, fd=${file.fd}`); + fileIo.truncateSync(file.fd, 4); + } catch (error) { + console.error("truncateSyncTest1 Error!", error); + } + console.println("truncateSyncTest1 end"); +} + +function truncateSyncTest2() { + console.println("truncateSyncTest2 begin"); + try { + fileIo.truncateSync("/data/local/tmp/b.txt", 4); + } catch (error) { + console.error("truncateSyncTest2 Error!", error); + } + console.println("truncateSyncTest2 end"); +} + +function unlinkSyncTest() { + console.println("unlinkSyncTest begin"); + try { + fileIo.unlinkSync("/data/local/tmp/a1.txt"); + console.println(`unlink result`); + + } catch (error) { + console.error("unlinkSyncTest Error!", error); + } + console.println("unlinkSyncTest end"); +} + +function unlinkPromiseTest() { + console.println("unlinkPromiseTest begin"); + try { + await fileIo.unlink("/data/local/tmp/a2.txt"); + console.println(`unlinkPromiseTest success`); + } catch (error) { + console.error("unlinkPromiseTest Error!", error); + } + console.println("unlinkPromiseTest end"); +} + +function unlinkCallbackTest() { + console.println("unlinkCallbackTest begin"); + fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { + if (err.code == 0) { + console.error("unlinkCallbackTest success"); + } else { + console.error("unlinkCallbackTest Error!", err); + } + }); + console.println("unlinkCallbackTest end"); +} + +function writeSyncTest1() { + console.println("writeSyncTest1 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + encoding: "utf-8", + }; + let length = fileIo.writeSync(fd, "hello,world!", options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest1 Error!", error); + } + console.println("writeSyncTest1 end"); +} + +function writeSyncTest2() { + console.println("writeSyncTest2 begin"); + try { + let mode = 2; + let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + offset: 100, + }; + let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest2 Error!", error); + } + console.println("writeSyncTest2 end"); +} + +function writePromiseTest() { + console.println("writePromiseTest begin"); + try { + let mode = OpenMode.READ_WRITE; + let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); + let fd = file.fd; + let length = await fileIo.write(fd, "writePromiseTest"); + console.println(`writePromiseTest length=${length}`); + } catch (error) { + console.error("writePromiseTest Error!", error); + } + console.println("writePromiseTest end"); +} + +function writeCallbackTest() { + console.println("writeCallbackTest begin"); + let mode = OpenMode.READ_WRITE; + let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); + let fd = file.fd; + fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { + if (data !== undefined) { + console.println(`writeCallbackTest length=${data}`); + } else { + console.error("writeCallbackTest Error!", err); + } + }); + console.println("writeCallbackTest end"); +} + +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + console.println("\n"); + console.println("try open file notexist.txt ..."); + let mode = 2; + file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); + console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest1 Error!", error); + } + console.println("errorHandlerTest1 end"); +} + +function errorHandlerTest2() { + console.println("errorHandlerTest1 begin"); + try { + let file = fileIo.openSync("/data/local/tmp/a.txt"); + console.println("try open file a.txt ..."); + let mode = -1; + file = fileIo.openSync("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest2 Error!", error); + } + console.println("errorHandlerTest2 end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let file = await fileIo.open("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + console.println("-----------"); + console.println("try open file a.txt ..."); + let mode = -1; + file = await fileIo.open("/data/local/tmp/a.txt", mode); + console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + + fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { + console.println("2)-----------"); + if (err.code == 0 && data !== undefined) { + console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open file Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} function main() { console.println("---------- hello ani --------------"); @@ -1416,10 +1454,12 @@ function main() { // closeSyncTest2(); // closeSyncTest3(); // closePromiseTest(); + // closePromiseTest2(); // closeCallbackTest(); // copyFileTest(); // fileTest(); - // listFileTest(); + // listFileSyncTest1(); + // listFileSyncTest2(); // mkdirSyncTest1(); // mkdirSyncTest2(); // mkdirSyncTest3(); @@ -1439,8 +1479,9 @@ function main() { // statCallbackTest(); // truncateSyncTest1(); // truncateSyncTest2(); - // truncateSyncTest3(); // unlinkSyncTest(); + // unlinkPromiseTest(); + // unlinkCallbackTest(); // writeSyncTest1(); // writeSyncTest2(); // writePromiseTest(); 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 2361312f..07bf046e 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 @@ -89,7 +89,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str return { true, make_optional(result) }; } -tuple>> AnalyzerArrayString(ani_env *env, ani_object obj, string tag) +tuple>> ParseArrayString(ani_env *env, ani_object obj, string tag) { ani_boolean isUndefined; ani_ref result_ref; @@ -122,7 +122,7 @@ tuple>> AnalyzerArrayString(ani_env *env, ani_obje return { true, make_optional>(move(strings)) }; } -tuple> AnalyzeFilter(ani_env *env, ani_object obj) +tuple> ParseFilter(ani_env *env, ani_object obj) { FsFileFilter filter; @@ -140,14 +140,14 @@ tuple> AnalyzeFilter(ani_env *env, ani_object obj) } filter.SetFileSizeOver(lastModifiedAfter); - auto [succSuffix, suffix] = AnalyzerArrayString(env, obj, "suffix"); + auto [succSuffix, suffix] = ParseArrayString(env, obj, "suffix"); if (!succSuffix) { HILOGE("Illegal option.suffix parameter"); return { false, move(filter) }; } filter.SetSuffix(move(suffix)); - auto [succDisplayName, displayName] = AnalyzerArrayString(env, obj, "displayName"); + auto [succDisplayName, displayName] = ParseArrayString(env, obj, "displayName"); if (!succDisplayName) { HILOGE("Illegal option.displayName parameter"); return { false, move(filter) }; @@ -157,7 +157,7 @@ tuple> AnalyzeFilter(ani_env *env, ani_object obj) return { true, move(filter) }; } -tuple> AnalyzeArgs(ani_env *env, ani_object obj) +tuple> ParseArgs(ani_env *env, ani_object obj) { FsListFileOptions result; ani_boolean isUndefined; @@ -189,7 +189,7 @@ tuple> AnalyzeArgs(ani_env *env, ani_object ob if (isUndefined) { return { true, make_optional(result) }; } - auto [succFilter, filterFilterClass] = AnalyzeFilter(env, static_cast(filter_ref)); + auto [succFilter, filterFilterClass] = ParseFilter(env, static_cast(filter_ref)); if (!succFilter) { HILOGE("Invalid filter"); return { false, nullopt }; @@ -207,7 +207,7 @@ ani_array_ref ListFileAni::ListFileSync(ani_env *env, [[maybe_unused]] ani_class return nullptr; } - auto [succOpt, opt] = AnalyzeArgs(env, obj); + auto [succOpt, opt] = ParseArgs(env, obj); if (!succOpt) { HILOGE("Invalid options Arguments"); return nullptr; -- Gitee From 110c335f446c655af05a227c0913822eec2b300c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 17 Mar 2025 17:53:11 +0800 Subject: [PATCH 35/76] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1e681e0eae9f4b35261cb441e368b1173f173989 --- .../js/src/common/ani_helper/error_handler.h | 16 +++++------ .../kits/js/src/mod_fs/ani/error_handler.cpp | 2 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 27 +++++++------------ .../js/src/mod_fs/properties/ani/open_ani.cpp | 12 ++++++++- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index 248df5fe..dff2581f 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -76,19 +76,15 @@ private: return ANI_NOT_FOUND; } - ani_object obj; - if (ANI_OK != env->Object_New(cls, ctor, &obj)) { - HILOGE("Cannot create ani error object"); + auto [succ, message] = TypeConverter::ToAniString(env, errMsg); + if (!succ) { + HILOGE("Convert errMsg to ani string failed"); return ANI_ERROR; } - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "code", static_cast(code))) { - HILOGE("Set code field value failed!"); - return ANI_ERROR; - } - - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "message", errMsg)) { - HILOGE("Set message field value failed!"); + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, static_cast(code), message)) { + HILOGE("Cannot create ani error object"); return ANI_ERROR; } diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp index a05d70f2..6fbacb66 100644 --- a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp @@ -19,7 +19,7 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) { - const char *className = "L@ohos/file/fs/BusinessErrorInner;"; + const char *className = "L@ohos/file/fs/BusinessError;"; return Throw(env, className, code, errMsg); } 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 c2294a03..13cd2093 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 @@ -469,14 +469,10 @@ class fileIo { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - err.code = -1; - reject(err); - } else { - let r = ret as File; - resolve(r); - } + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -496,15 +492,12 @@ class fileIo { static open(path: String, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as File; - callback(err, r); - } + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 22177da7..bb76bd08 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -16,6 +16,7 @@ #include "open_ani.h" #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "open_core.h" #include "type_converter.h" @@ -89,21 +90,30 @@ ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } FsResult ret = OpenCore::DoOpen(filePath, modeOp); if (!ret.IsSuccess()) { HILOGE("Open failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } const FsFile *file = ret.GetData().value(); - return Wrap(env, move(file)); + auto result = Wrap(env, move(file)); + if (result == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; } } // namespace ANI } // namespace ModuleFileIO -- Gitee From 1e3250fe911d124ee0cc3beb828e72d4dbc8a8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 17 Mar 2025 17:53:11 +0800 Subject: [PATCH 36/76] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1e681e0eae9f4b35261cb441e368b1173f173989 --- .../js/src/common/ani_helper/error_handler.h | 33 +++++++++++++----- .../kits/js/src/mod_fs/ani/error_handler.cpp | 5 +-- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 34 +++++++++---------- .../js/src/mod_fs/properties/ani/open_ani.cpp | 12 ++++++- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index 248df5fe..adb93d29 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -52,7 +52,8 @@ public: } private: - static ani_status Throw(ani_env *env, const char *className, int32_t code, const std::string &errMsg) + static ani_status Throw( + ani_env *env, const char *className, const char *name, int32_t code, const std::string &errMsg) { if (env == nullptr) { HILOGE("Invalid parameter env"); @@ -64,6 +65,11 @@ private: return ANI_INVALID_ARGS; } + if (name == nullptr) { + HILOGE("Invalid parameter name"); + return ANI_INVALID_ARGS; + } + ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class '%{private}s'", className); @@ -71,7 +77,7 @@ private: } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { HILOGE("Cannot find constructor for class '%{private}s'", className); return ANI_NOT_FOUND; } @@ -82,17 +88,26 @@ private: return ANI_ERROR; } - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "code", static_cast(code))) { - HILOGE("Set code field value failed!"); - return ANI_ERROR; + ani_status status = ANI_ERROR; + status = AniHelper::SetFieldValue(env, cls, obj, "name", name); + if (status != ANI_OK) { + HILOGE("Set field 'name' value failed"); + return status; } - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "message", errMsg)) { - HILOGE("Set message field value failed!"); - return ANI_ERROR; + status = AniHelper::SetFieldValue(env, cls, obj, "message", errMsg); + if (status != ANI_OK) { + HILOGE("Set field 'message' value failed"); + return status; + } + + status = AniHelper::SetFieldValue(env, cls, obj, "code", static_cast(code)); + if (status != ANI_OK) { + HILOGE("Set field 'code' value failed"); + return status; } - auto status = env->ThrowError(static_cast(obj)); + status = env->ThrowError(static_cast(obj)); if (status != ANI_OK) { HILOGE("Throw ani error object failed!"); return status; diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp index a05d70f2..0434519c 100644 --- a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp @@ -19,8 +19,9 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) { - const char *className = "L@ohos/file/fs/BusinessErrorInner;"; - return Throw(env, className, code, errMsg); + const char *className = "L@ohos/file/fs/BusinessError;"; + const char *name = "BusinessError"; + return Throw(env, className, name, code, errMsg); } } // namespace OHOS::FileManagement::ModuleFileIO::ANI \ 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 c2294a03..ce428ee7 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 @@ -63,6 +63,13 @@ export class BusinessError { message: string; code: number; data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; + } + constructor(code: number, msg: string, data?: T) { this.name = 'BusinessError'; this.code = code; @@ -469,14 +476,10 @@ class fileIo { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - err.code = -1; - reject(err); - } else { - let r = ret as File; - resolve(r); - } + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -496,15 +499,12 @@ class fileIo { static open(path: String, callback: AsyncCallback): void { let promise = taskpool.execute(fileIo.openSync2, path); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as File; - callback(err, r); - } + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 22177da7..bb76bd08 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -16,6 +16,7 @@ #include "open_ani.h" #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "open_core.h" #include "type_converter.h" @@ -89,21 +90,30 @@ ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, filePath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } FsResult ret = OpenCore::DoOpen(filePath, modeOp); if (!ret.IsSuccess()) { HILOGE("Open failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } const FsFile *file = ret.GetData().value(); - return Wrap(env, move(file)); + auto result = Wrap(env, move(file)); + if (result == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + return result; } } // namespace ANI } // namespace ModuleFileIO -- Gitee From 32d87384e8cbe89cdc0a885e97de098f759a2eb4 Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Tue, 18 Mar 2025 10:59:12 +0800 Subject: [PATCH 37/76] =?UTF-8?q?=E5=88=87=E6=8D=A2namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangxiaoliang25 Change-Id: I1251f02c1a298e40dee913eb6ae761ca87c90ed0 Signed-off-by: zhangxiaoliang25 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 28 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 2490 ++++++++--------- 2 files changed, 1237 insertions(+), 1281 deletions(-) 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 16a06adc..5c70efaf 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 @@ -70,24 +70,24 @@ static ani_status BindStatClassMethods(ani_env *env) static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo;"; + static const char *className = "L@ohos/file/fs/FileIoImpl;"; std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, - ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, - ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, - ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, - ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, - ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, - ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, - ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, - ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, - ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, - ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, - ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, - ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, + // ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + // ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, + // ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, + // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, + // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + // ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, + // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + // ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, + // ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, className, 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 13cd2093..2e0a8771 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 @@ -12,52 +12,111 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +// import type { AsyncCallback } from '@ohos.base'; -export default fileIo; - -export namespace OpenMode { - /** - * Read only Permission. - */ - export const READ_ONLY = 0o0; - /** - * Write only Permission. - */ - export const WRITE_ONLY = 0o1; - /** - * Write and Read Permission. - */ - export const READ_WRITE = 0o2; - /** - * If not exist, create file. - */ - export const CREATE = 0o100; - /** - * File truncate len 0. - */ - export const TRUNC = 0o1000; - /** - * File append write. - */ - export const APPEND = 0o2000; - /** - * File open in nonblocking mode. - */ - export const NONBLOCK = 0o4000; - /** - * File is Dir. - */ - export const DIR = 0o200000; - /** - * File is not symbolic link. - */ - export const NOFOLLOW = 0o400000; - /** - * SYNC IO. - */ - export const SYNC = 0o4010000; +function access(path: string, mode?: AccessModeType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + if (mode === undefined) { + let promise = taskpool.execute((path: string):boolean=> { + return FileIoImpl.doAccessSync(path); + }, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } else { + let promise = taskpool.execute((path: string, mode: AccessModeType):boolean=> { + return FileIoImpl.doAccessSync(path, mode); + }, path, mode); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } + }); + } + +function access(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path:string):boolean=> { + return FileIoImpl.doAccessSync(path); + } , path); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let result = ret as boolean; + callback(err, result); + } + }); } +function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(FileIoImpl.doAccessSync, path, mode, flag); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + }) +} + +function accessSync(path: string, mode?: AccessModeType): boolean { + return FileIoImpl.doAccessSync(path, mode); +} + +function accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { + return FileIoImpl.doAccessSync(path, mode, flag); +} + + +function close(file: number | File): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function close(file: number | File, callback: AsyncCallback): void { + let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + + +function closeSync(file: number | File): void { + return FileIoImpl.closeSync(file) +} + + export class BusinessError { name: string; message: string; @@ -187,7 +246,7 @@ class StatInner implements Stat { atimeNs: bigint = 0n; mtimeNs: bigint = 0n; ctimeNs: bigint = 0n; - location: LocationType; + location: LocationType = LocationType.LOCAL; private nativeStat: long = 0; @@ -206,7 +265,7 @@ class StatInner implements Stat { native isSymbolicLink(): boolean; } -class fileIo { +class FileIoImpl { static { loadLibrary("ani_fs_class"); @@ -214,1235 +273,1132 @@ class fileIo { static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; - static accessSync(path: string, mode?: AccessModeType): boolean { - return fileIo.doAccessSync(path, mode); - } - - static accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { - return fileIo.doAccessSync(path, mode, flag); - } - - static accessSync1(path: string): boolean { - return fileIo.accessSync(path); - } - - static accessSync2(path: string, mode: AccessModeType): boolean { - return fileIo.accessSync(path, mode); - } - - static accessSync3(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { - return fileIo.accessSync(path, mode, flag); - } - - static access(path: string, mode?: AccessModeType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } else { - let promise = taskpool.execute(fileIo.accessSync2, path, mode); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } - }); - } - - static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - }) - } - - static access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let result = ret as boolean; - callback(err, result); - } - }); - } - static native closeSync(file: number | File): void; - static close(file: number | File): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((file: number | File): undefined => fileIo.closeSync(file), file); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static close(file: number | File, callback: AsyncCallback): void { - let promise = taskpool.execute((file: number | File): undefined => fileIo.closeSync(file), file); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); - } - - static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; - - static native listFileSync(path: string, options?: ListFileOptions): string[]; - - static listFileSync1(path: string): string[] { - return fileIo.listFileSync(path); - } - - static listFileSync2(path: string, options: ListFileOptions): string[] { - return fileIo.listFileSync(path, options); - } - - static listFile(path: string): Promise { - return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } - }); - }); - } - - static listFile(path: string, options: ListFileOptions): Promise { - return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } - }); - }); - } - - static listFile(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); - } - - static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); - } - - static native mkdirSync(path: string): void; - - static native mkdirSync(path: string, recursion: boolean): void; - - static mkdir(path: string): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static mkdir(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); - } - - static mkdir(path: string, recursion: boolean): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string, recursion: boolean): undefined => - fileIo.mkdirSync(path, recursion), path, recursion); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, recursion: boolean): undefined => - fileIo.mkdirSync(path, recursion), path, recursion); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); - } - - static native moveFileSync(src: String, dest: String, mode?: number): void; - - static native openSync(path: String, mode?: number): File; - - static openSync1(path: String, mode: number): File { - return fileIo.openSync(path, mode); - } - - static openSync2(path: String): File { - return fileIo.openSync(path); - } - - static open(path: String, mode: number): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.openSync1, path, mode); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static open(path: String): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static open(path: String, mode: number, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.openSync1, path, mode); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - let file = ret as File; - callback(e, file); - }).catch((e: BusinessError): void => { - let f: File = new FileInner(0); - callback(e, f); - }); - } - - static open(path: String, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - let file = ret as File; - callback(e, file); - }).catch((e: BusinessError): void => { - let f: File = new FileInner(0); - callback(e, f); - }); - } - - static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; - - static readSync1(fd: number, buffer: ArrayBuffer): number { - return fileIo.readSync(fd, buffer); - } - - static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { - return fileIo.readSync(fd, buffer, options); - } - - static read(fd: number, buffer: ArrayBuffer): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as number; - resolve(r); - } - }); - }); - } - - static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as number; - resolve(r); - } - }); - }); - } - - static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as number; - callback(err, r); - } - }); - } - - static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as number; - callback(err, r); - } - }); - } - - static native readTextSync(filePath: string, options?: ReadTextOptions): string; - - static readTextSync1(filePath: string): string { - return fileIo.readTextSync(filePath); - } - - static readTextSync2(filePath: string, options: ReadTextOptions): string { - return fileIo.readTextSync(filePath, options); - } - - static readText(filePath: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static readText(filePath: string, options: ReadTextOptions): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static readText(filePath: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } - }); - } - - static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } - }); - } - - static native rmdirSync(path: string): void; - - static native statSync(file: string | number): Stat; - - static stat(file: string | number): Promise { - return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.statSync, file); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as Stat; - resolve(r); - } - }); - }); - } - - static stat(file: string | number, callback: AsyncCallback): void { - let p = taskpool.execute(fileIo.statSync, file); - p.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as Stat; - callback(err, r); - } - }); - } - - static native truncateSync(file: string | number, len?: number): void; - - static native unlinkSync(path: string): void; - - static unlink(path: string): Promise { - return new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static unlink(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); - } - - static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; - - static writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { - return fileIo.writeSync(fd, buffer, options); - } - - static writeSync2(fd: number, buffer: string | ArrayBuffer): number { - return fileIo.writeSync(fd, buffer); - } - - static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static write(fd: number, buffer: string | ArrayBuffer): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType): void => { - let result = ret as number - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as number; - callback(err, r); - } - }); - } - - static write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as number; - callback(err, r); - } - }); - } -} - -function accessSyncTest1() { - console.println("accessSyncTest1 begin"); - try { - let ret = fileIo.accessSync("/data/local/tmp/a.txt"); - console.println(`accessSync result : ${ret}`); - } catch (error) { - console.error("accessSyncTest1 Error!", error); - } - console.println("accessSyncTest1 end"); -} - -function closeSyncTest1() { - console.println("closeSyncTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.closeSync(file.fd); - console.println(`close file by fd, fd=${file.fd}`); - } catch (error) { - console.error("closeSyncTest1 Error!", error); - } - console.println("closeSyncTest1 end"); -} - -function closeSyncTest2() { - console.println("closeSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.closeSync(file); - console.println(`close file by file, fd=${file.fd}`); - } catch (error) { - console.error("closeSyncTest2 Error!", error); - } - console.println("closeSyncTest2 end"); -} - -function closeSyncTest3() { - console.println("closeSyncTest3 begin"); - try { - fileIo.closeSync(-1); - console.println(`close file by invalid fd: -1`); - } catch (error) { - console.error("closeSyncTest3 Error!", error); - } - console.println("closeSyncTest3 end"); -} - -function closePromiseTest() { - console.println("closePromiseTest begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.close(file.fd).then((result: undefined): void => { - console.println(`closePromiseTest close success`); - }).catch((e: Object): void => { - console.error("async closePromiseTest Error!!!", e); - }); - } catch (error) { - console.error("closePromiseTest Error:", error); - } - console.println("closePromiseTest end"); -} - -function closePromiseTest2() { - console.println("closePromiseTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - await fileIo.close(file.fd); - console.println(`closePromiseTest2 close success`); - } catch (error) { - console.error("closePromiseTest2 Error:", error); - } - console.println("closePromiseTest2 end"); -} - -function closeCallbackTest() { - console.println("closeCallbackTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - fileIo.close(file, (err: BusinessError) => { - if (err.code == 0) { - console.log("closeCallbackTest: close success!"); - } else { - console.error("closeCallbackTest: close Error:", err); - } - }); - - console.println("closeCallbackTest end"); -} - -function copyFileTest() { - console.println("copyFileTest begin"); - try { - fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); - } catch (error) { - console.error("copyFileTest Error!", error); - } - console.println("copyFileTest end"); + // static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; + + // static native listFileSync(path: string, options?: ListFileOptions): string[]; + + // static listFileSync1(path: string): string[] { + // return fileIo.listFileSync(path); + // } + + // static listFileSync2(path: string, options: ListFileOptions): string[] { + // return fileIo.listFileSync(path, options); + // } + + // static listFile(path: string): Promise { + // return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.listFileSync1, path); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as string[]; + // resolve(r); + // } + // }); + // }); + // } + + // static listFile(path: string, options: ListFileOptions): Promise { + // return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.listFileSync2, path, options); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as string[]; + // resolve(r); + // } + // }); + // }); + // } + + // static listFile(path: string, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.listFileSync1, path); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as string[]; + // callback(err, r); + // } + // }); + // } + + // static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.listFileSync2, path, options); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as string[]; + // callback(err, r); + // } + // }); + // } + + // static native mkdirSync(path: string): void; + + // static native mkdirSync(path: string, recursion: boolean): void; + + // static mkdir(path: string): Promise { + // return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + // let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); + // promise.then((ret: NullishType): void => { + // resolve(undefined); + // }).catch((e: BusinessError): void => { + // reject(e); + // }); + // }); + // } + + // static mkdir(path: string, callback: AsyncCallback): void { + // let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); + // promise.then((ret: NullishType): void => { + // let e = new BusinessError(0, ""); + // callback(e, undefined); + // }).catch((e: BusinessError): void => { + // callback(e, undefined); + // }); + // } + + // static mkdir(path: string, recursion: boolean): Promise { + // return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + // let promise = taskpool.execute((path: string, recursion: boolean): undefined => + // fileIo.mkdirSync(path, recursion), path, recursion); + // promise.then((ret: NullishType): void => { + // resolve(undefined); + // }).catch((e: BusinessError): void => { + // reject(e); + // }); + // }); + // } + + // static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { + // let promise = taskpool.execute((path: string, recursion: boolean): undefined => + // fileIo.mkdirSync(path, recursion), path, recursion); + // promise.then((ret: NullishType): void => { + // let e = new BusinessError(0, ""); + // callback(e, undefined); + // }).catch((e: BusinessError): void => { + // callback(e, undefined); + // }); + // } + + // static native moveFileSync(src: String, dest: String, mode?: number): void; + + // static native openSync(path: String, mode?: number): File; + + // static openSync1(path: String, mode: number): File { + // return fileIo.openSync(path, mode); + // } + + // static openSync2(path: String): File { + // return fileIo.openSync(path); + // } + + // static open(path: String, mode: number): Promise { + // return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.openSync1, path, mode); + // promise.then((ret: NullishType): void => { + // let file = ret as File; + // resolve(file); + // }).catch((e: BusinessError): void => { + // reject(e); + // }); + // }); + // } + + // static open(path: String): Promise { + // return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.openSync2, path); + // promise.then((ret: NullishType): void => { + // let file = ret as File; + // resolve(file); + // }).catch((e: BusinessError): void => { + // reject(e); + // }); + // }); + // } + + // static open(path: String, mode: number, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.openSync1, path, mode); + // promise.then((ret: NullishType): void => { + // let e = new BusinessError(0, ""); + // let file = ret as File; + // callback(e, file); + // }).catch((e: BusinessError): void => { + // let f: File = new FileInner(0); + // callback(e, f); + // }); + // } + + // static open(path: String, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.openSync2, path); + // promise.then((ret: NullishType): void => { + // let e = new BusinessError(0, ""); + // let file = ret as File; + // callback(e, file); + // }).catch((e: BusinessError): void => { + // let f: File = new FileInner(0); + // callback(e, f); + // }); + // } + + // static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; + + // static readSync1(fd: number, buffer: ArrayBuffer): number { + // return fileIo.readSync(fd, buffer); + // } + + // static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { + // return fileIo.readSync(fd, buffer, options); + // } + + // static read(fd: number, buffer: ArrayBuffer): Promise { + // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as number; + // resolve(r); + // } + // }); + // }); + // } + + // static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { + // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as number; + // resolve(r); + // } + // }); + // }); + // } + + // static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.readSync1, fd, buffer); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as number; + // callback(err, r); + // } + // }); + // } + + // static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as number; + // callback(err, r); + // } + // }); + // } + + // static native readTextSync(filePath: string, options?: ReadTextOptions): string; + + // static readTextSync1(filePath: string): string { + // return fileIo.readTextSync(filePath); + // } + + // static readTextSync2(filePath: string, options: ReadTextOptions): string { + // return fileIo.readTextSync(filePath, options); + // } + + // static readText(filePath: string): Promise { + // return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.readTextSync1, filePath); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as string; + // resolve(r); + // } + // }); + // }); + // } + + // static readText(filePath: string, options: ReadTextOptions): Promise { + // return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as string; + // resolve(r); + // } + // }); + // }); + // } + + // static readText(filePath: string, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.readTextSync1, filePath); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as string; + // callback(err, r); + // } + // }); + // } + + // static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as string; + // callback(err, r); + // } + // }); + // } + + // static native rmdirSync(path: string): void; + + // static native statSync(file: string | number): Stat; + + // static stat(file: string | number): Promise { + // return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.statSync, file); + // promise.then((ret: NullishType): void => { + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + + // reject(err); + // } else { + // let r = ret as Stat; + // resolve(r); + // } + // }); + // }); + // } + + // static stat(file: string | number, callback: AsyncCallback): void { + // let p = taskpool.execute(fileIo.statSync, file); + // p.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1; + // callback(err, undefined); + // } else { + // err.code = 0; + // let r = ret as Stat; + // callback(err, r); + // } + // }); + // } + + // static native truncateSync(file: string | number, len?: number): void; + + // static native unlinkSync(path: string): void; + + // static unlink(path: string): Promise { + // return new Promise((resolve: (result: undefined) => void, + // reject: (e: BusinessError) => void): void => { + // let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); + // promise.then((ret: NullishType): void => { + // resolve(undefined); + // }).catch((e: BusinessError): void => { + // reject(e); + // }); + // }); + // } + + // static unlink(path: string, callback: AsyncCallback): void { + // let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); + // promise.then((ret: NullishType): void => { + // let e = new BusinessError(0, ""); + // callback(e, undefined); + // }).catch((e: BusinessError): void => { + // callback(e, undefined); + // }); + // } + + // static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; + + // static writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { + // return fileIo.writeSync(fd, buffer, options); + // } + + // static writeSync2(fd: number, buffer: string | ArrayBuffer): number { + // return fileIo.writeSync(fd, buffer); + // } + + // static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { + // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); + // promise.then((ret: NullishType): void => { + // let result = ret as number + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + // reject(err); + // } else { + // resolve(result); + // } + // }); + // }); + // } + + // static write(fd: number, buffer: string | ArrayBuffer): Promise { + // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + // let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); + // promise.then((ret: NullishType): void => { + // let result = ret as number + // if (ret === null || ret === undefined) { + // let err = new BusinessError(-1, ""); + // reject(err); + // } else { + // resolve(result); + // } + // }); + // }); + // } + + // static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1 + // callback(err, undefined) + // } else { + // err.code = 0 + // let r = ret as number; + // callback(err, r); + // } + // }); + // } + + // static write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { + // let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); + // promise.then((ret: NullishType): void => { + // let err = new BusinessError(-1, ""); + // if (ret === null || ret === undefined) { + // err.code = -1 + // callback(err, undefined) + // } else { + // err.code = 0 + // let r = ret as number; + // callback(err, r); + // } + // }); + // } } -function fileTest() { - console.println("fileTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt"); - let fd = file.fd; - console.println(`open file, fd=${fd}`); - let parent = file.getParent(); - console.println(`file parent=${parent}`); - file.lock(); - console.println(`file lock completed`); - file.unlock(); - console.println(`file unlock completed`); - file.tryLock(true); - console.println(`file tryLock completed`); - file.unlock(); - console.println(`file unlock completed`); - console.println("fileTest end"); -} - -function listFileSyncTest1() { - console.println("listFileSyncTest1 begin"); - try { - let files = fileIo.listFileSync("/data/local/tmp/"); - for (const file of files) { - console.println(file); - } - } catch (error) { - console.error("listFileSyncTest1 Error!", error); - } - console.println("listFileSyncTest1 end"); -} - -function listFileSyncTest2() { - console.println("listFileSyncTest2 begin"); - try { - let options: ListFileOptions = { - recursion: true - }; - let files = fileIo.listFileSync("/data/", options); - for (const file of files) { - console.println(file); - } - } catch (error) { - console.error("listFileSyncTest2 Error!", error); - } - console.println("listFileSyncTest2 end"); -} - -function mkdirSyncTest1() { - console.println("mkdirSyncTest1 begin") - try { - fileIo.mkdirSync("/data/local/tmp/dir01"); - console.println(`mkdirSyncTest1 success`); - } catch (error) { - console.error("mkdirSyncTest1 Error!", error); - } - console.println("mkdirSyncTest1 end"); -} - -function mkdirSyncTest2() { - console.println("mkdirSyncTest2 begin") - try { - fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); - console.println(`mkdirSyncTest2 success`); - } catch (error) { - console.error("mkdirSyncTest2 Error!", error); - } - console.println("mkdirSyncTest2 end"); -} - -function mkdirSyncTest3() { - console.println("mkdirSyncTest3 begin") - try { - fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); - console.println(`mkdirSyncTest3 success`); - } catch (error) { - console.error("mkdirSyncTest3 Error!", error); - } - console.println("mkdirSyncTest3 end"); -} - -function mkdirPromiseTest() { - console.println("mkdirPromiseTest begin") - try { - await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); - console.println(`mkdirPromiseTest success`); - } catch (error) { - console.error("mkdirPromiseTest Error!", error); - } - console.println("mkdirPromiseTest end"); -} - -function mkdirCallbackTest() { - console.println("mkdirCallbackTest begin") - fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { - if (err.code == 0) { - console.println("mkdirCallbackTest success"); - } else { - console.println("mkdirCallbackTest failed!"); - } - }); - console.println("mkdirCallbackTest end"); -} - -function moveSyncTest() { - console.println("moveSyncTest begin") - fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) - console.println("moveSyncTest end") -} - -function openSyncTest() { - console.println("openSyncTest begin"); - try { - // 不指定mode - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - // 以可读可写方式打开 - let mode = OpenMode.READ_WRITE; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openSyncTest Error!", error); - } - console.println("openSyncTest end"); -} - -function openPromiseTest() { - console.println("openPromiseTest begin"); - try { - // 不指定mode - let file = await fileIo.open("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - // 以可读可写方式打开 - let mode = OpenMode.READ_WRITE; - file = await fileIo.open("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openPromiseTest Error!", error); - } - console.println("openPromiseTest end"); -} - -function openCallbackTest() { - console.println("openCallbackTest begin"); - // 不指定mode - fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (data !== undefined) { - console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open failed!", err); - } - }); - // 以可读可写方式打开 - let mode = OpenMode.READ_WRITE; - fileIo.open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { - if (data !== undefined) { - console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open failed!", err); - } - }); -} - -function readSyncTest1() { - console.println("readSyncTest1 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let length = fileIo.readSync(fd, buffer); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest1 Error!", error); - } - console.println("readSyncTest1 end") -} - -function readSyncTest2() { - console.println("readSyncTest2 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest2 Error!", error); - } - console.println("readSyncTest2 end") -} - -function readSyncTest3() { - console.println("readSyncTest3 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - offset: 3, - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest3 Error!", error); - } - console.println("readSyncTest3 end") -} - -function readTextSyncTest1() { - console.println("readTextSyncTest1 begin") - try { - let content = fileIo.readTextSync("/data/local/tmp/a.txt"); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readTextSyncTest1 Error!", error); - } - console.println("readTextSyncTest1 end") -} - -function readTextSyncTest2() { - console.println("readTextSyncTest2 begin"); - try { - let options: ReadTextOptions = { - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest2 Error!", error); - } - console.println("readTextSyncTest2 end"); -} - -function readTextSyncTest3() { - console.println("readTextSyncTest3 begin") - try { - let options: ReadTextOptions = { - offset: 3, - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest3 Error!", error); - } - console.println("readTextSyncTest3 end"); -} - -function rmdirSyncTest() { - console.println("rmdirSyncTest begin"); - try { - fileIo.rmdirSync("/data/local/tmp/dir01"); - } catch (error) { - console.error("rmdirSyncTest Error!", error); - } - console.println("rmdirSyncTest end"); -} - -function printStatInfo(stat?: Stat) { - if (stat === undefined) { - console.error("stat is null or undefined!"); - return; - } - console.info("stat, ino is " + stat.ino); - console.info("stat, mode is " + stat.mode); - console.info("stat, uid is " + stat.uid); - console.info("stat, gid is " + stat.gid); - console.info("stat, size is " + stat.size); - console.info("stat, atime is " + stat.atime); - console.info("stat, mtime is " + stat.mtime); - console.info("stat, ctime is " + stat.ctime); - console.info("stat, atimeNs is " + stat.atimeNs); - console.info("stat, mtimeNs is " + stat.mtimeNs); - console.info("stat, ctimeNs is " + stat.ctimeNs); - if (stat.location !== undefined) { - console.info("stat, location is " + stat.location); - } - console.info("stat, isBlockDevice is " + stat.isBlockDevice()); - console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); - console.info("stat, isDirectory is " + stat.isDirectory()); - console.info("stat, isFIFO is " + stat.isFIFO()); - console.info("stat, isFile is " + stat.isFile()); - console.info("stat, isSocket is " + stat.isSocket()); - console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); -} - -function statSyncTest1() { - console.println("statSyncTest1 begin"); - try { - let stat = fileIo.statSync("/data/local/tmp/a.txt"); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest1 Error!", error); - } - console.println("statSyncTest1 end"); -} - -function statSyncTest2() { - console.println("statSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file fd: ${file.fd}`); - let stat = fileIo.statSync(file.fd); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest2 Error!", error); - } - console.println("statSyncTest2 end"); -} - -function statPromiseTest() { - console.println("statPromiseTest start"); - try { - let path = "/data/local/tmp/a.txt" - let file = fileIo.openSync(path); - console.println(`open file fd: ${file.fd}`); - fileIo.stat(path).then((result: Stat): void => { - console.log("statPromiseTest success"); - printStatInfo(result); - }); - } catch (error) { - console.error("statPromiseTest Error!", error); - } - console.println("statPromiseTest end"); -} - -function statCallbackTest() { - console.println("statCallbackTest start"); - let path = "/data/local/tmp/a.txt" - fileIo.stat(path, (err: BusinessError, data?: Stat) => { - if (data === undefined || data === null) { - console.error("Callback: Error stat:", err); - } else { - console.log("Callback stat success"); - printStatInfo(data); - } - }); - console.println("statCallbackTest end"); -} - -function truncateSyncTest1() { - console.println("truncateSyncTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file, fd=${file.fd}`); - fileIo.truncateSync(file.fd, 4); - } catch (error) { - console.error("truncateSyncTest1 Error!", error); - } - console.println("truncateSyncTest1 end"); -} - -function truncateSyncTest2() { - console.println("truncateSyncTest2 begin"); - try { - fileIo.truncateSync("/data/local/tmp/b.txt", 4); - } catch (error) { - console.error("truncateSyncTest2 Error!", error); - } - console.println("truncateSyncTest2 end"); -} - -function unlinkSyncTest() { - console.println("unlinkSyncTest begin"); - try { - fileIo.unlinkSync("/data/local/tmp/a1.txt"); - console.println(`unlink result`); - - } catch (error) { - console.error("unlinkSyncTest Error!", error); - } - console.println("unlinkSyncTest end"); -} - -function unlinkPromiseTest() { - console.println("unlinkPromiseTest begin"); - try { - await fileIo.unlink("/data/local/tmp/a2.txt"); - console.println(`unlinkPromiseTest success`); - } catch (error) { - console.error("unlinkPromiseTest Error!", error); - } - console.println("unlinkPromiseTest end"); -} - -function unlinkCallbackTest() { - console.println("unlinkCallbackTest begin"); - fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { - if (err.code == 0) { - console.error("unlinkCallbackTest success"); - } else { - console.error("unlinkCallbackTest Error!", err); - } - }); - console.println("unlinkCallbackTest end"); -} - -function writeSyncTest1() { - console.println("writeSyncTest1 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - encoding: "utf-8", - }; - let length = fileIo.writeSync(fd, "hello,world!", options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest1 Error!", error); - } - console.println("writeSyncTest1 end"); -} - -function writeSyncTest2() { - console.println("writeSyncTest2 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - offset: 100, - }; - let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest2 Error!", error); - } - console.println("writeSyncTest2 end"); -} - -function writePromiseTest() { - console.println("writePromiseTest begin"); - try { - let mode = OpenMode.READ_WRITE; - let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); - let fd = file.fd; - let length = await fileIo.write(fd, "writePromiseTest"); - console.println(`writePromiseTest length=${length}`); - } catch (error) { - console.error("writePromiseTest Error!", error); - } - console.println("writePromiseTest end"); -} - -function writeCallbackTest() { - console.println("writeCallbackTest begin"); - let mode = OpenMode.READ_WRITE; - let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); - let fd = file.fd; - fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { - if (data !== undefined) { - console.println(`writeCallbackTest length=${data}`); - } else { - console.error("writeCallbackTest Error!", err); - } - }); - console.println("writeCallbackTest end"); -} - -function errorHandlerTest1() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - console.println("\n"); - console.println("try open file notexist.txt ..."); - let mode = 2; - file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); - console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest1 Error!", error); - } - console.println("errorHandlerTest1 end"); -} - -function errorHandlerTest2() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println("try open file a.txt ..."); - let mode = -1; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest2 Error!", error); - } - console.println("errorHandlerTest2 end"); -} - -function errorHandlerTest3() { - console.println("errorHandlerTest3 begin"); - try { - let file = await fileIo.open("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - console.println("-----------"); - console.println("try open file a.txt ..."); - let mode = -1; - file = await fileIo.open("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest3 Error!", error); - } - console.println("errorHandlerTest3 end"); -} - -function errorHandlerTest4() { - console.println("errorHandlerTest4 begin"); - fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - - fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { - console.println("2)-----------"); - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - console.println("errorHandlerTest4 end"); -} +// function accessSyncTest1() { +// console.println("accessSyncTest1 begin"); +// try { +// let ret = fileIo.accessSync("/data/local/tmp/a.txt"); +// console.println(`accessSync result : ${ret}`); +// } catch (error) { +// console.error("accessSyncTest1 Error!", error); +// } +// console.println("accessSyncTest1 end"); +// } + +// function closeSyncTest1() { +// console.println("closeSyncTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// fileIo.closeSync(file.fd); +// console.println(`close file by fd, fd=${file.fd}`); +// } catch (error) { +// console.error("closeSyncTest1 Error!", error); +// } +// console.println("closeSyncTest1 end"); +// } + +// function closeSyncTest2() { +// console.println("closeSyncTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// fileIo.closeSync(file); +// console.println(`close file by file, fd=${file.fd}`); +// } catch (error) { +// console.error("closeSyncTest2 Error!", error); +// } +// console.println("closeSyncTest2 end"); +// } + +// function closeSyncTest3() { +// console.println("closeSyncTest3 begin"); +// try { +// fileIo.closeSync(-1); +// console.println(`close file by invalid fd: -1`); +// } catch (error) { +// console.error("closeSyncTest3 Error!", error); +// } +// console.println("closeSyncTest3 end"); +// } + +// function closePromiseTest() { +// console.println("closePromiseTest begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// fileIo.close(file.fd).then((result: undefined): void => { +// console.println(`closePromiseTest close success`); +// }).catch((e: Object): void => { +// console.error("async closePromiseTest Error!!!", e); +// }); +// } catch (error) { +// console.error("closePromiseTest Error:", error); +// } +// console.println("closePromiseTest end"); +// } + +// function closePromiseTest2() { +// console.println("closePromiseTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// await fileIo.close(file.fd); +// console.println(`closePromiseTest2 close success`); +// } catch (error) { +// console.error("closePromiseTest2 Error:", error); +// } +// console.println("closePromiseTest2 end"); +// } + +// function closeCallbackTest() { +// console.println("closeCallbackTest begin"); +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + +// fileIo.close(file, (err: BusinessError) => { +// if (err.code == 0) { +// console.log("closeCallbackTest: close success!"); +// } else { +// console.error("closeCallbackTest: close Error:", err); +// } +// }); + +// console.println("closeCallbackTest end"); +// } + +// function copyFileTest() { +// console.println("copyFileTest begin"); +// try { +// fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); +// } catch (error) { +// console.error("copyFileTest Error!", error); +// } +// console.println("copyFileTest end"); +// } + +// function fileTest() { +// console.println("fileTest begin"); +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let fd = file.fd; +// console.println(`open file, fd=${fd}`); +// let parent = file.getParent(); +// console.println(`file parent=${parent}`); +// file.lock(); +// console.println(`file lock completed`); +// file.unlock(); +// console.println(`file unlock completed`); +// file.tryLock(true); +// console.println(`file tryLock completed`); +// file.unlock(); +// console.println(`file unlock completed`); +// console.println("fileTest end"); +// } + +// function listFileSyncTest1() { +// console.println("listFileSyncTest1 begin"); +// try { +// let files = fileIo.listFileSync("/data/local/tmp/"); +// for (const file of files) { +// console.println(file); +// } +// } catch (error) { +// console.error("listFileSyncTest1 Error!", error); +// } +// console.println("listFileSyncTest1 end"); +// } + +// function listFileSyncTest2() { +// console.println("listFileSyncTest2 begin"); +// try { +// let options: ListFileOptions = { +// recursion: true +// }; +// let files = fileIo.listFileSync("/data/", options); +// for (const file of files) { +// console.println(file); +// } +// } catch (error) { +// console.error("listFileSyncTest2 Error!", error); +// } +// console.println("listFileSyncTest2 end"); +// } + +// function mkdirSyncTest1() { +// console.println("mkdirSyncTest1 begin") +// try { +// fileIo.mkdirSync("/data/local/tmp/dir01"); +// console.println(`mkdirSyncTest1 success`); +// } catch (error) { +// console.error("mkdirSyncTest1 Error!", error); +// } +// console.println("mkdirSyncTest1 end"); +// } + +// function mkdirSyncTest2() { +// console.println("mkdirSyncTest2 begin") +// try { +// fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); +// console.println(`mkdirSyncTest2 success`); +// } catch (error) { +// console.error("mkdirSyncTest2 Error!", error); +// } +// console.println("mkdirSyncTest2 end"); +// } + +// function mkdirSyncTest3() { +// console.println("mkdirSyncTest3 begin") +// try { +// fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); +// console.println(`mkdirSyncTest3 success`); +// } catch (error) { +// console.error("mkdirSyncTest3 Error!", error); +// } +// console.println("mkdirSyncTest3 end"); +// } + +// function mkdirPromiseTest() { +// console.println("mkdirPromiseTest begin") +// try { +// await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); +// console.println(`mkdirPromiseTest success`); +// } catch (error) { +// console.error("mkdirPromiseTest Error!", error); +// } +// console.println("mkdirPromiseTest end"); +// } + +// function mkdirCallbackTest() { +// console.println("mkdirCallbackTest begin") +// fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { +// if (err.code == 0) { +// console.println("mkdirCallbackTest success"); +// } else { +// console.println("mkdirCallbackTest failed!"); +// } +// }); +// console.println("mkdirCallbackTest end"); +// } + +// function moveSyncTest() { +// console.println("moveSyncTest begin") +// fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) +// console.println("moveSyncTest end") +// } + +// function openSyncTest() { +// console.println("openSyncTest begin"); +// try { +// // 不指定mode +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// // 以可读可写方式打开 +// let mode = OpenMode.READ_WRITE; +// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("openSyncTest Error!", error); +// } +// console.println("openSyncTest end"); +// } + +// function openPromiseTest() { +// console.println("openPromiseTest begin"); +// try { +// // 不指定mode +// let file = await fileIo.open("/data/local/tmp/a.txt"); +// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// // 以可读可写方式打开 +// let mode = OpenMode.READ_WRITE; +// file = await fileIo.open("/data/local/tmp/a.txt", mode); +// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("openPromiseTest Error!", error); +// } +// console.println("openPromiseTest end"); +// } + +// function openCallbackTest() { +// console.println("openCallbackTest begin"); +// // 不指定mode +// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { +// if (data !== undefined) { +// console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open failed!", err); +// } +// }); +// // 以可读可写方式打开 +// let mode = OpenMode.READ_WRITE; +// fileIo.open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { +// if (data !== undefined) { +// console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open failed!", err); +// } +// }); +// } + +// function readSyncTest1() { +// console.println("readSyncTest1 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let length = fileIo.readSync(fd, buffer); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest1 Error!", error); +// } +// console.println("readSyncTest1 end") +// } + +// function readSyncTest2() { +// console.println("readSyncTest2 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let options: ReadOptions = { +// length: 5, +// } +// let length = fileIo.readSync(fd, buffer, options); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest2 Error!", error); +// } +// console.println("readSyncTest2 end") +// } + +// function readSyncTest3() { +// console.println("readSyncTest3 begin") +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let fd = file.fd; +// console.println(`open file, fd=${fd}`) +// let buffer = new ArrayBuffer(100); +// let options: ReadOptions = { +// offset: 3, +// length: 5, +// } +// let length = fileIo.readSync(fd, buffer, options); +// console.println(`read length: ${length}`) +// let len = length as int; +// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readSyncTest3 Error!", error); +// } +// console.println("readSyncTest3 end") +// } + +// function readTextSyncTest1() { +// console.println("readTextSyncTest1 begin") +// try { +// let content = fileIo.readTextSync("/data/local/tmp/a.txt"); +// console.println("-----------------------------------------------") +// console.println(content) +// console.println("-----------------------------------------------") +// } catch (error) { +// console.error("readTextSyncTest1 Error!", error); +// } +// console.println("readTextSyncTest1 end") +// } + +// function readTextSyncTest2() { +// console.println("readTextSyncTest2 begin"); +// try { +// let options: ReadTextOptions = { +// length: 5, +// } +// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// console.println("-----------------------------------------------"); +// console.println(content); +// console.println("-----------------------------------------------"); +// } catch (error) { +// console.error("readTextSyncTest2 Error!", error); +// } +// console.println("readTextSyncTest2 end"); +// } + +// function readTextSyncTest3() { +// console.println("readTextSyncTest3 begin") +// try { +// let options: ReadTextOptions = { +// offset: 3, +// length: 5, +// } +// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// console.println("-----------------------------------------------"); +// console.println(content); +// console.println("-----------------------------------------------"); +// } catch (error) { +// console.error("readTextSyncTest3 Error!", error); +// } +// console.println("readTextSyncTest3 end"); +// } + +// function rmdirSyncTest() { +// console.println("rmdirSyncTest begin"); +// try { +// fileIo.rmdirSync("/data/local/tmp/dir01"); +// } catch (error) { +// console.error("rmdirSyncTest Error!", error); +// } +// console.println("rmdirSyncTest end"); +// } + +// function printStatInfo(stat?: Stat) { +// if (stat === undefined) { +// console.error("stat is null or undefined!"); +// return; +// } +// console.info("stat, ino is " + stat.ino); +// console.info("stat, mode is " + stat.mode); +// console.info("stat, uid is " + stat.uid); +// console.info("stat, gid is " + stat.gid); +// console.info("stat, size is " + stat.size); +// console.info("stat, atime is " + stat.atime); +// console.info("stat, mtime is " + stat.mtime); +// console.info("stat, ctime is " + stat.ctime); +// console.info("stat, atimeNs is " + stat.atimeNs); +// console.info("stat, mtimeNs is " + stat.mtimeNs); +// console.info("stat, ctimeNs is " + stat.ctimeNs); +// if (stat.location !== undefined) { +// console.info("stat, location is " + stat.location); +// } +// console.info("stat, isBlockDevice is " + stat.isBlockDevice()); +// console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); +// console.info("stat, isDirectory is " + stat.isDirectory()); +// console.info("stat, isFIFO is " + stat.isFIFO()); +// console.info("stat, isFile is " + stat.isFile()); +// console.info("stat, isSocket is " + stat.isSocket()); +// console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); +// } + +// function statSyncTest1() { +// console.println("statSyncTest1 begin"); +// try { +// let stat = fileIo.statSync("/data/local/tmp/a.txt"); +// printStatInfo(stat); +// } catch (error) { +// console.error("statSyncTest1 Error!", error); +// } +// console.println("statSyncTest1 end"); +// } + +// function statSyncTest2() { +// console.println("statSyncTest2 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file fd: ${file.fd}`); +// let stat = fileIo.statSync(file.fd); +// printStatInfo(stat); +// } catch (error) { +// console.error("statSyncTest2 Error!", error); +// } +// console.println("statSyncTest2 end"); +// } + +// function statPromiseTest() { +// console.println("statPromiseTest start"); +// try { +// let path = "/data/local/tmp/a.txt" +// let file = fileIo.openSync(path); +// console.println(`open file fd: ${file.fd}`); +// fileIo.stat(path).then((result: Stat): void => { +// console.log("statPromiseTest success"); +// printStatInfo(result); +// }); +// } catch (error) { +// console.error("statPromiseTest Error!", error); +// } +// console.println("statPromiseTest end"); +// } + +// function statCallbackTest() { +// console.println("statCallbackTest start"); +// let path = "/data/local/tmp/a.txt" +// fileIo.stat(path, (err: BusinessError, data?: Stat) => { +// if (data === undefined || data === null) { +// console.error("Callback: Error stat:", err); +// } else { +// console.log("Callback stat success"); +// printStatInfo(data); +// } +// }); +// console.println("statCallbackTest end"); +// } + +// function truncateSyncTest1() { +// console.println("truncateSyncTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// console.println(`open file, fd=${file.fd}`); +// fileIo.truncateSync(file.fd, 4); +// } catch (error) { +// console.error("truncateSyncTest1 Error!", error); +// } +// console.println("truncateSyncTest1 end"); +// } + +// function truncateSyncTest2() { +// console.println("truncateSyncTest2 begin"); +// try { +// fileIo.truncateSync("/data/local/tmp/b.txt", 4); +// } catch (error) { +// console.error("truncateSyncTest2 Error!", error); +// } +// console.println("truncateSyncTest2 end"); +// } + +// function unlinkSyncTest() { +// console.println("unlinkSyncTest begin"); +// try { +// fileIo.unlinkSync("/data/local/tmp/a1.txt"); +// console.println(`unlink result`); + +// } catch (error) { +// console.error("unlinkSyncTest Error!", error); +// } +// console.println("unlinkSyncTest end"); +// } + +// function unlinkPromiseTest() { +// console.println("unlinkPromiseTest begin"); +// try { +// await fileIo.unlink("/data/local/tmp/a2.txt"); +// console.println(`unlinkPromiseTest success`); +// } catch (error) { +// console.error("unlinkPromiseTest Error!", error); +// } +// console.println("unlinkPromiseTest end"); +// } + +// function unlinkCallbackTest() { +// console.println("unlinkCallbackTest begin"); +// fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { +// if (err.code == 0) { +// console.error("unlinkCallbackTest success"); +// } else { +// console.error("unlinkCallbackTest Error!", err); +// } +// }); +// console.println("unlinkCallbackTest end"); +// } + +// function writeSyncTest1() { +// console.println("writeSyncTest1 begin"); +// try { +// let mode = 2; +// let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); +// let fd = file.fd; +// console.println(`file open fd=${fd}`); +// const options: WriteOptions = { +// encoding: "utf-8", +// }; +// let length = fileIo.writeSync(fd, "hello,world!", options); +// console.println(`write file length=${length}`); +// } catch (error) { +// console.error("writeSyncTest1 Error!", error); +// } +// console.println("writeSyncTest1 end"); +// } + +// function writeSyncTest2() { +// console.println("writeSyncTest2 begin"); +// try { +// let mode = 2; +// let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); +// let fd = file.fd; +// console.println(`file open fd=${fd}`); +// const options: WriteOptions = { +// offset: 100, +// }; +// let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); +// console.println(`write file length=${length}`); +// } catch (error) { +// console.error("writeSyncTest2 Error!", error); +// } +// console.println("writeSyncTest2 end"); +// } + +// function writePromiseTest() { +// console.println("writePromiseTest begin"); +// try { +// let mode = OpenMode.READ_WRITE; +// let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); +// let fd = file.fd; +// let length = await fileIo.write(fd, "writePromiseTest"); +// console.println(`writePromiseTest length=${length}`); +// } catch (error) { +// console.error("writePromiseTest Error!", error); +// } +// console.println("writePromiseTest end"); +// } + +// function writeCallbackTest() { +// console.println("writeCallbackTest begin"); +// let mode = OpenMode.READ_WRITE; +// let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); +// let fd = file.fd; +// fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { +// if (data !== undefined) { +// console.println(`writeCallbackTest length=${data}`); +// } else { +// console.error("writeCallbackTest Error!", err); +// } +// }); +// console.println("writeCallbackTest end"); +// } + +// function errorHandlerTest1() { +// console.println("errorHandlerTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + +// console.println("\n"); +// console.println("try open file notexist.txt ..."); +// let mode = 2; +// file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); +// console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest1 Error!", error); +// } +// console.println("errorHandlerTest1 end"); +// } + +// function errorHandlerTest2() { +// console.println("errorHandlerTest1 begin"); +// try { +// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// console.println("try open file a.txt ..."); +// let mode = -1; +// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest2 Error!", error); +// } +// console.println("errorHandlerTest2 end"); +// } + +// function errorHandlerTest3() { +// console.println("errorHandlerTest3 begin"); +// try { +// let file = await fileIo.open("/data/local/tmp/a.txt"); +// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// console.println("-----------"); +// console.println("try open file a.txt ..."); +// let mode = -1; +// file = await fileIo.open("/data/local/tmp/a.txt", mode); +// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); +// } catch (error) { +// console.error("errorHandlerTest3 Error!", error); +// } +// console.println("errorHandlerTest3 end"); +// } + +// function errorHandlerTest4() { +// console.println("errorHandlerTest4 begin"); +// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { +// if (err.code == 0 && data !== undefined) { +// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open file Error!", err); +// } +// }); + +// fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { +// console.println("2)-----------"); +// if (err.code == 0 && data !== undefined) { +// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); +// } else { +// console.error("open file Error!", err); +// } +// }); +// console.println("errorHandlerTest4 end"); +// } function main() { console.println("---------- hello ani --------------"); - accessSyncTest1(); - openPromiseTest(); - openCallbackTest(); - openSyncTest(); + // accessSyncTest1(); + // openPromiseTest(); + // openCallbackTest(); + // openSyncTest(); // closeSyncTest1(); // closeSyncTest2(); // closeSyncTest3(); -- Gitee From 19ef3ddbe731f1ce955ca407289d8c648dc69762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Tue, 18 Mar 2025 11:55:36 +0000 Subject: [PATCH 38/76] =?UTF-8?q?!63=20namespace=20=E6=95=B4=E6=94=B9=20*?= =?UTF-8?q?=20namespace=20=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/ani/bind_function_class.cpp | 4 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 141 +++++++++--------- 2 files changed, 71 insertions(+), 74 deletions(-) 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 5c70efaf..1b46d898 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 @@ -81,9 +81,9 @@ static ani_status BindStaticMethods(ani_env *env) // ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, - // ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, + ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, - // ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, // ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, 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 2e0a8771..a0717d6d 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 @@ -116,6 +116,73 @@ function closeSync(file: number | File): void { return FileIoImpl.closeSync(file) } +function readSync1(fd: number, buffer: ArrayBuffer): number { + return FileIoImpl.readSync(fd, buffer) +} + +function readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { + return FileIoImpl.readSync(fd, buffer, options) +} + +function read(fd: number, buffer: ArrayBuffer): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + reject(err); + } else { + let r = ret as number; + resolve(r); + } + }); + }); +} + +function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + reject(err); + } else { + let r = ret as number; + resolve(r); + } + }); + }); +} + +function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as number; + callback(err, r); + } + }); +} + +function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as number; + callback(err, r); + } + }); +} export class BusinessError { name: string; @@ -457,77 +524,7 @@ class FileIoImpl { // }); // } - // static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; - - // static readSync1(fd: number, buffer: ArrayBuffer): number { - // return fileIo.readSync(fd, buffer); - // } - - // static readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { - // return fileIo.readSync(fd, buffer, options); - // } - - // static read(fd: number, buffer: ArrayBuffer): Promise { - // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as number; - // resolve(r); - // } - // }); - // }); - // } - - // static read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { - // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as number; - // resolve(r); - // } - // }); - // }); - // } - - // static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as number; - // callback(err, r); - // } - // }); - // } - - // static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as number; - // callback(err, r); - // } - // }); - // } + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; // static native readTextSync(filePath: string, options?: ReadTextOptions): string; @@ -601,7 +598,7 @@ class FileIoImpl { // }); // } - // static native rmdirSync(path: string): void; + static native rmdirSync(path: string): void; // static native statSync(file: string | number): Stat; -- Gitee From 9d181e1bf58ed7c9db91154a1500614f63f6a75b Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 18 Mar 2025 20:03:14 +0800 Subject: [PATCH 39/76] =?UTF-8?q?unlink=20namespace=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 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 2 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 50 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) 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 1b46d898..3bf9dc98 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 @@ -86,7 +86,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, - // ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, + ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, className, 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 a0717d6d..e548d56f 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 @@ -184,6 +184,32 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A }); } +function unlinkSync(path: string): void { + return FileIoImpl.unlinkSync(path) +} + +function unlink(path: string): Promise { + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): undefined => FileIoImpl.unlinkSync(path), path); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function unlink(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): undefined => FileIoImpl.unlinkSync(path), path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + export class BusinessError { name: string; message: string; @@ -635,29 +661,7 @@ class FileIoImpl { // static native truncateSync(file: string | number, len?: number): void; - // static native unlinkSync(path: string): void; - - // static unlink(path: string): Promise { - // return new Promise((resolve: (result: undefined) => void, - // reject: (e: BusinessError) => void): void => { - // let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - // promise.then((ret: NullishType): void => { - // resolve(undefined); - // }).catch((e: BusinessError): void => { - // reject(e); - // }); - // }); - // } - - // static unlink(path: string, callback: AsyncCallback): void { - // let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); - // promise.then((ret: NullishType): void => { - // let e = new BusinessError(0, ""); - // callback(e, undefined); - // }).catch((e: BusinessError): void => { - // callback(e, undefined); - // }); - // } + static native unlinkSync(path: string): void; // static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; -- Gitee From 3d059695ed9555fbea86155069997736c979171a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Tue, 18 Mar 2025 20:09:31 +0800 Subject: [PATCH 40/76] =?UTF-8?q?=E5=B7=B2=E7=9F=A5BUG=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib14f08f9f4a4e504d313773165edd8f18a0a3594 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ce428ee7..4d8cf172 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 @@ -694,7 +694,7 @@ class fileIo { static unlink(path: string): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); + let promise = taskpool.execute((path: string): undefined => fileIo.unlinkSync(path), path); promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { @@ -704,7 +704,7 @@ class fileIo { } static unlink(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): void => fileIo.unlinkSync(path), path); + let promise = taskpool.execute((path: string): undefined => fileIo.unlinkSync(path), path); promise.then((ret: NullishType): void => { let e = new BusinessError(0, ""); callback(e, undefined); -- Gitee From ab7e35b5c4f3f3aa92109b376a3af588c4c21dd7 Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 18 Mar 2025 20:43:01 +0800 Subject: [PATCH 41/76] =?UTF-8?q?mkdir=20namespace=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 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 4 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 110 ++++++++++-------- 2 files changed, 66 insertions(+), 48 deletions(-) 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 1b46d898..ca16fabc 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 @@ -77,8 +77,8 @@ static ani_status BindStaticMethods(ani_env *env) // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, // ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, - // ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, - // ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, + ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, 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 a0717d6d..145f544e 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 @@ -116,6 +116,68 @@ function closeSync(file: number | File): void { return FileIoImpl.closeSync(file) } +function mkdirSync(path: string): void { + return FileIoImpl.mkdirSync(path) +} + +function mkdirSync(path: string, recursion: boolean): void { + return FileIoImpl.mkdirSync(path, recursion) +} + +function mkdirSync1(path: string): undefined { + FileIoImpl.mkdirSync(path); + return undefined; +} + +function mkdirSync2(path: string, recursion: boolean): undefined { + FileIoImpl.mkdirSync(path, recursion); + return undefined; +} + +function mkdir(path: string): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e as BusinessError); + }); + }); +} + +function mkdir(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + +function mkdir(path: string, recursion: boolean): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, recursion: boolean): undefined => + mkdirSync2(path, recursion), path, recursion); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: NullishType): void => { + reject(e as BusinessError); + }); + }); +} + +function mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string, recursion: boolean): undefined => + mkdirSync2(path, recursion), path, recursion); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + function readSync1(fd: number, buffer: ArrayBuffer): number { return FileIoImpl.readSync(fd, buffer) } @@ -416,53 +478,9 @@ class FileIoImpl { // }); // } - // static native mkdirSync(path: string): void; - - // static native mkdirSync(path: string, recursion: boolean): void; - - // static mkdir(path: string): Promise { - // return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - // let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); - // promise.then((ret: NullishType): void => { - // resolve(undefined); - // }).catch((e: BusinessError): void => { - // reject(e); - // }); - // }); - // } + static native mkdirSync(path: string): void; - // static mkdir(path: string, callback: AsyncCallback): void { - // let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); - // promise.then((ret: NullishType): void => { - // let e = new BusinessError(0, ""); - // callback(e, undefined); - // }).catch((e: BusinessError): void => { - // callback(e, undefined); - // }); - // } - - // static mkdir(path: string, recursion: boolean): Promise { - // return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - // let promise = taskpool.execute((path: string, recursion: boolean): undefined => - // fileIo.mkdirSync(path, recursion), path, recursion); - // promise.then((ret: NullishType): void => { - // resolve(undefined); - // }).catch((e: BusinessError): void => { - // reject(e); - // }); - // }); - // } - - // static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - // let promise = taskpool.execute((path: string, recursion: boolean): undefined => - // fileIo.mkdirSync(path, recursion), path, recursion); - // promise.then((ret: NullishType): void => { - // let e = new BusinessError(0, ""); - // callback(e, undefined); - // }).catch((e: BusinessError): void => { - // callback(e, undefined); - // }); - // } + static native mkdirSync(path: string, recursion: boolean): void; // static native moveFileSync(src: String, dest: String, mode?: number): void; -- Gitee From d16bab7c630a0574b5179f2a8651fa3a2a143dc0 Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 18 Mar 2025 21:02:46 +0800 Subject: [PATCH 42/76] =?UTF-8?q?truncateSync=20namespace=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp | 2 +- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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 6ea811e5..a7e6b824 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 @@ -85,7 +85,7 @@ static ani_status BindStaticMethods(ani_env *env) // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, - // ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, + ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; 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 0bb5b69d..3f15b5cd 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 @@ -246,6 +246,10 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A }); } +function truncateSync(file: string | number, len?: number): void { + return FileIoImpl.truncateSync(file, len) +} + function unlinkSync(path: string): void { return FileIoImpl.unlinkSync(path) } @@ -677,7 +681,7 @@ class FileIoImpl { // }); // } - // static native truncateSync(file: string | number, len?: number): void; + static native truncateSync(file: string | number, len?: number): void; static native unlinkSync(path: string): void; -- Gitee From 7ba42e757c6a316ee1e90621d92613feb9c441c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 18 Mar 2025 13:43:48 +0000 Subject: [PATCH 43/76] !71 readtext * readtext --- .../js/src/mod_fs/ani/bind_function_class.cpp | 2 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 66 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) 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 a7e6b824..a472adb8 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 @@ -82,7 +82,7 @@ static ani_status BindStaticMethods(ani_env *env) // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, - // ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, + ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, 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 3f15b5cd..0b0ff62d 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 @@ -276,6 +276,70 @@ function unlink(path: string, callback: AsyncCallback): void { }); } +function readText(filePath: string, options?: ReadTextOptions): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + if (options == undefined) { + let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let r = ret as string; + resolve(r); + } + }); + } else { + let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let r = ret as string; + resolve(r); + } + }); + } + }); +} + +function readText(filePath: string, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string; + callback(err, r); + } + }); +} + +function readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string; + callback(err, r); + } + }); +} + +function readTextSync(filePath: string, options?: ReadTextOptions): string { + return FileIoImpl.readTextSync(filePath, options); +} + export class BusinessError { name: string; message: string; @@ -574,7 +638,7 @@ class FileIoImpl { static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; - // static native readTextSync(filePath: string, options?: ReadTextOptions): string; + static native readTextSync(filePath: string, options?: ReadTextOptions): string; // static readTextSync1(filePath: string): string { // return fileIo.readTextSync(filePath); -- Gitee From 71bbbee0d9396591f7a5140c58855dfedf260068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 18 Mar 2025 13:44:40 +0000 Subject: [PATCH 44/76] !69 hash * hash --- .../src/mod_hash/ani/bind_function_class.cpp | 2 +- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 77 +++++++++---------- 2 files changed, 38 insertions(+), 41 deletions(-) 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 fc2bd3c0..52ee6358 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 @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/hash/hash;"; + static const char *className = "L@ohos/file/hash/hashImpl;"; std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, 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 468c3c4e..286d1815 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 @@ -13,61 +13,58 @@ * limitations under the License. */ -export default hash; - export class BusinessError { - code: number = 0; + name: string; + message: string; + code: number; data?: T; + constructor(code: number, msg: string, data?: T) { + this.name = 'BusinessError'; + this.code = code; + this.message = msg; + if (data !== undefined) { + this.data = data; + } + } } export type AsyncCallback = (err: BusinessError, data?: T) => void; -class hash { - - static { - loadLibrary("ani_hash_class"); - } - - static native hashSync(path: string, algorithm: string): string; - - static hash(path: string, algorithm: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(hash.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static hash(path: string, algorithm: string, callback: AsyncCallback): void { - let promise = taskpool.execute(hash.hashSync, path, algorithm); +function hash(path: string, algorithm: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); promise.then((ret: NullishType) => { - let err = new BusinessError(); if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) + let err = new BusinessError(-1, ""); + reject(err); } else { - err.code = 0 let r = ret as string; - callback(err, r); + resolve(r); } }); - } + }); } -function main() { - console.println("---------- hello ani --------------"); +function hash(path: string, algorithm: string, callback: AsyncCallback): void { + let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as string; + callback(err, r); + } + }); +} - let ret = hash.hashSync("/data/local/tmp/a.txt", "md5"); - console.println(`hash: ${ret}`); +class hashImpl { + static { + loadLibrary("ani_hash_class"); + } - console.println("---------- hello ani end ---------------"); + static native hashSync(path: string, algorithm: string): string; } \ No newline at end of file -- Gitee From 3fe1e01610879e9a81ad8aa9b77dc9db5c916c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 18 Mar 2025 13:45:15 +0000 Subject: [PATCH 45/76] !68 securitylabel * securitylabel --- .../ani/bind_function_class.cpp | 2 +- .../ani/ets/@ohos.file.securityLabel.ets | 78 +++++++++---------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 74c9466d..9dfe0fb3 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/securityLabel/securitylabel;"; + static const char *className = "L@ohos/file/securityLabel/securitylabelImpl;"; std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 962c25d0..1458f300 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -13,59 +13,55 @@ * limitations under the License. */ -export default securitylabel; - export class BusinessError { - code: number = 0; + name: string; + message: string; + code: number; data?: T; + constructor(code: number, msg: string, data?: T) { + this.name = 'BusinessError'; + this.code = code; + this.message = msg; + if (data !== undefined) { + this.data = data; + } + } } export type AsyncCallback = (err: BusinessError, data?: T) => void; type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; -class securitylabel { - - static { - loadLibrary("ani_securitylabel_class"); - } - - static native setSecurityLabelSync(path: string, type: DataLevel): void; - - static setSecurityLabel(path: string, type: DataLevel): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - resolve(undefined); - } - }); +function setSecurityLabel(path: string, type: DataLevel): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); }); - } + }); +} - static setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabel.setSecurityLabelSync(path, type), path, type); - promise.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - callback(err, undefined); - } - }); - } +function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); } -function main() { - console.println("---------- hello ani --------------"); +function setSecurityLabelSync(path: string, type: DataLevel): void { + return securitylabelImpl.setSecurityLabelSync(path, type); +} - securitylabel.setSecurityLabelSync("/data/local/tmp/a.txt", "s2"); +class securitylabelImpl { - console.println("---------- hello ani end ---------------"); + static { + loadLibrary("ani_securitylabel_class"); + } + + static native setSecurityLabelSync(path: string, type: DataLevel): void; } \ No newline at end of file -- Gitee From 68a6625d8f98a48d0df55980891ef17a117bbc4f Mon Sep 17 00:00:00 2001 From: tianp Date: Tue, 18 Mar 2025 14:03:32 +0000 Subject: [PATCH 46/76] !72 open/write/move * open/write/move --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 265 +++++++++--------- 1 file changed, 139 insertions(+), 126 deletions(-) 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 0b0ff62d..b91ddaa5 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 @@ -178,6 +178,142 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): }); } +function moveFileSync(src: string, dest: string, mode?: number): void { + return FileIoImpl.moveFileSync(src, dest, mode); +} + +function openSync(path: string, mode?: number): File { + return FileIoImpl.openSync(path, mode); +} + +function openSync1(path: String, mode: number): File { + return FileIoImpl.openSync(path, mode); +} + +function openSync2(path: String): File { + return FileIoImpl.openSync(path); +} + +function open(path: String, mode: number): Promise { + return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(openSync1, path, mode); + promise.then((ret: NullishType): void => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function open(path: String): Promise { + return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(openSync2, path); + promise.then((ret: NullishType): void => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); +} + +function open(path: String, mode: number, callback: AsyncCallback): void { + let promise = taskpool.execute(openSync1, path, mode); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); + }); +} + +function open(path: String, callback: AsyncCallback): void { + let promise = taskpool.execute(openSync2, path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + let file = ret as File; + callback(e, file); + }).catch((e: BusinessError): void => { + let f: File = new FileInner(0); + callback(e, f); + }); +} + +function writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number { + return FileIoImpl.writeSync(fd, buffer, options); +} + +function writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { + return FileIoImpl.writeSync(fd, buffer, options); +} + +function writeSync2(fd: number, buffer: string | ArrayBuffer): number { + return FileIoImpl.writeSync(fd, buffer); +} + +function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(writeSync1, fd, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + reject(err); + } else { + resolve(result); + } + }); + }); +} + +function write(fd: number, buffer: string | ArrayBuffer): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(writeSync2, fd, buffer); + promise.then((ret: NullishType): void => { + let result = ret as number + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + reject(err); + } else { + resolve(result); + } + }); + }); +} + +function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(writeSync1, fd, buffer, options); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as number; + callback(err, r); + } + }); +} + +function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { + let promise = taskpool.execute(writeSync2, fd, buffer); + promise.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + } else { + err.code = 0 + let r = ret as number; + callback(err, r); + } + }); +} + function readSync1(fd: number, buffer: ArrayBuffer): number { return FileIoImpl.readSync(fd, buffer) } @@ -576,65 +712,9 @@ class FileIoImpl { static native mkdirSync(path: string, recursion: boolean): void; - // static native moveFileSync(src: String, dest: String, mode?: number): void; + static native moveFileSync(src: String, dest: String, mode?: number): void; - // static native openSync(path: String, mode?: number): File; - - // static openSync1(path: String, mode: number): File { - // return fileIo.openSync(path, mode); - // } - - // static openSync2(path: String): File { - // return fileIo.openSync(path); - // } - - // static open(path: String, mode: number): Promise { - // return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.openSync1, path, mode); - // promise.then((ret: NullishType): void => { - // let file = ret as File; - // resolve(file); - // }).catch((e: BusinessError): void => { - // reject(e); - // }); - // }); - // } - - // static open(path: String): Promise { - // return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.openSync2, path); - // promise.then((ret: NullishType): void => { - // let file = ret as File; - // resolve(file); - // }).catch((e: BusinessError): void => { - // reject(e); - // }); - // }); - // } - - // static open(path: String, mode: number, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.openSync1, path, mode); - // promise.then((ret: NullishType): void => { - // let e = new BusinessError(0, ""); - // let file = ret as File; - // callback(e, file); - // }).catch((e: BusinessError): void => { - // let f: File = new FileInner(0); - // callback(e, f); - // }); - // } - - // static open(path: String, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.openSync2, path); - // promise.then((ret: NullishType): void => { - // let e = new BusinessError(0, ""); - // let file = ret as File; - // callback(e, file); - // }).catch((e: BusinessError): void => { - // let f: File = new FileInner(0); - // callback(e, f); - // }); - // } + static native openSync(path: String, mode?: number): File; static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; @@ -749,75 +829,8 @@ class FileIoImpl { static native unlinkSync(path: string): void; - // static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; + static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; - // static writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { - // return fileIo.writeSync(fd, buffer, options); - // } - - // static writeSync2(fd: number, buffer: string | ArrayBuffer): number { - // return fileIo.writeSync(fd, buffer); - // } - - // static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { - // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - // promise.then((ret: NullishType): void => { - // let result = ret as number - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - // reject(err); - // } else { - // resolve(result); - // } - // }); - // }); - // } - - // static write(fd: number, buffer: string | ArrayBuffer): Promise { - // return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - // promise.then((ret: NullishType): void => { - // let result = ret as number - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - // reject(err); - // } else { - // resolve(result); - // } - // }); - // }); - // } - - // static write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1 - // callback(err, undefined) - // } else { - // err.code = 0 - // let r = ret as number; - // callback(err, r); - // } - // }); - // } - - // static write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1 - // callback(err, undefined) - // } else { - // err.code = 0 - // let r = ret as number; - // callback(err, r); - // } - // }); - // } } // function accessSyncTest1() { -- Gitee From c87bc269dc3398c4abaea9ece24597e1c6aec246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 18 Mar 2025 14:08:52 +0000 Subject: [PATCH 47/76] !73 listfile * listfile --- .../js/src/mod_fs/ani/bind_function_class.cpp | 2 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 65 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) 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 a472adb8..f9ae5bde 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 @@ -76,7 +76,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, - // ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, + ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, 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 b91ddaa5..25c78f19 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 @@ -476,6 +476,69 @@ function readTextSync(filePath: string, options?: ReadTextOptions): string { return FileIoImpl.readTextSync(filePath, options); } +function listFile(path: string, options?: ListFileOptions): Promise { + return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { + if (options == undefined) { + let promise = taskpool.execute(FileIoImpl.listFileSync, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + } else { + let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + } + }); +} + +function listFile(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.listFileSync, path); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string[]; + callback(err, r); + } + }); +} + +function listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); + promise.then((ret: NullishType) => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as string[]; + callback(err, r); + } + }); +} + +function listFileSync(path: string, options?: ListFileOptions): string[] { + return FileIoImpl.listFileSync(path, options); +} + export class BusinessError { name: string; message: string; @@ -636,7 +699,7 @@ class FileIoImpl { // static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; - // static native listFileSync(path: string, options?: ListFileOptions): string[]; + static native listFileSync(path: string, options?: ListFileOptions): string[]; // static listFileSync1(path: string): string[] { // return fileIo.listFileSync(path); -- Gitee From 7e3e2e4ad227d3dc940c397dd8b0258c3843f2e6 Mon Sep 17 00:00:00 2001 From: nieben Date: Tue, 18 Mar 2025 22:34:46 +0800 Subject: [PATCH 48/76] copy & stat Signed-off-by: nieben Change-Id: Ic0a150738c85e86f91b9d7739478865c0f813657 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 4 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 74 ++++++++++--------- 2 files changed, 43 insertions(+), 35 deletions(-) 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 f9ae5bde..489841a8 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 @@ -74,7 +74,7 @@ static ani_status BindStaticMethods(ani_env *env) std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, - // ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, + ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "doAccessSync", nullptr, reinterpret_cast(AccessAni::AccessSync3) }, ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, @@ -84,7 +84,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, - // ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, + ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, 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 25c78f19..cf9b1b17 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,6 +539,45 @@ function listFileSync(path: string, options?: ListFileOptions): string[] { return FileIoImpl.listFileSync(path, options); } +function copyFileSync(src: string | number, dest: string | number, mode?: number): void { + return FileIoImpl.copyFileSync(src, dest, mode) +} + +function statSync(file: string | number): Stat { + return FileIoImpl.statSync(file) +} + +function stat(file: string | number): Promise { + return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(FileIoImpl.statSync, file); + promise.then((ret: NullishType): void => { + if (ret === null || ret === undefined) { + let err = new BusinessError(-1, ""); + + reject(err); + } else { + let r = ret as Stat; + resolve(r); + } + }); + }); +} + +function stat(file: string | number, callback: AsyncCallback): void { + let p = taskpool.execute(FileIoImpl.statSync, file); + p.then((ret: NullishType): void => { + let err = new BusinessError(-1, ""); + if (ret === null || ret === undefined) { + err.code = -1; + callback(err, undefined); + } else { + err.code = 0; + let r = ret as Stat; + callback(err, r); + } + }); +} + export class BusinessError { name: string; message: string; @@ -697,7 +736,7 @@ class FileIoImpl { static native closeSync(file: number | File): void; - // static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; + static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; static native listFileSync(path: string, options?: ListFileOptions): string[]; @@ -855,38 +894,7 @@ class FileIoImpl { static native rmdirSync(path: string): void; - // static native statSync(file: string | number): Stat; - - // static stat(file: string | number): Promise { - // return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.statSync, file); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as Stat; - // resolve(r); - // } - // }); - // }); - // } - - // static stat(file: string | number, callback: AsyncCallback): void { - // let p = taskpool.execute(fileIo.statSync, file); - // p.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as Stat; - // callback(err, r); - // } - // }); - // } + static native statSync(file: string | number): Stat; static native truncateSync(file: string | number, len?: number): void; -- Gitee From 545c400cc7e520b7639c78a087ef9acf640c13d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 09:45:28 +0800 Subject: [PATCH 49/76] =?UTF-8?q?=E7=AD=89=E5=80=BC=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E7=BA=A0=E6=AD=A3=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ibcf6ffcd757eb0db3e14a43b31633dad2f6f6955 --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 4d8cf172..7245f85d 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 @@ -142,7 +142,7 @@ class FileInner implements File { private nativePtr: long = 0; constructor(ptr: long) { - if (this.nativePtr == 0) { + if (this.nativePtr === 0) { this.nativePtr = ptr; } } @@ -199,7 +199,7 @@ class StatInner implements Stat { private nativeStat: long = 0; constructor(stat: long) { - if (this.nativeStat == 0) { + if (this.nativeStat === 0) { this.nativeStat = stat; } } @@ -867,7 +867,7 @@ function closeCallbackTest() { console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); fileIo.close(file, (err: BusinessError) => { - if (err.code == 0) { + if (err.code === 0) { console.log("closeCallbackTest: close success!"); } else { console.error("closeCallbackTest: close Error:", err); @@ -981,7 +981,7 @@ function mkdirPromiseTest() { function mkdirCallbackTest() { console.println("mkdirCallbackTest begin") fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { - if (err.code == 0) { + if (err.code === 0) { console.println("mkdirCallbackTest success"); } else { console.println("mkdirCallbackTest failed!"); @@ -1302,7 +1302,7 @@ function unlinkPromiseTest() { function unlinkCallbackTest() { console.println("unlinkCallbackTest begin"); fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { - if (err.code == 0) { + if (err.code === 0) { console.error("unlinkCallbackTest success"); } else { console.error("unlinkCallbackTest Error!", err); @@ -1426,7 +1426,7 @@ function errorHandlerTest3() { function errorHandlerTest4() { console.println("errorHandlerTest4 begin"); fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (err.code == 0 && data !== undefined) { + if (err.code === 0 && data !== undefined) { console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); } else { console.error("open file Error!", err); @@ -1435,7 +1435,7 @@ function errorHandlerTest4() { fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { console.println("2)-----------"); - if (err.code == 0 && data !== undefined) { + if (err.code === 0 && data !== undefined) { console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); } else { console.error("open file Error!", err); -- Gitee From ad949262b5a47c682d99f99ea279cb1c7539b894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 09:45:28 +0800 Subject: [PATCH 50/76] =?UTF-8?q?=E7=AD=89=E5=80=BC=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E7=BA=A0=E6=AD=A3=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ibcf6ffcd757eb0db3e14a43b31633dad2f6f6955 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 166 ++---------------- 1 file changed, 13 insertions(+), 153 deletions(-) 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 cf9b1b17..ba81e481 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 @@ -655,7 +655,7 @@ class FileInner implements File { private nativePtr: long = 0; constructor(ptr: long) { - if (this.nativePtr == 0) { + if (this.nativePtr === 0) { this.nativePtr = ptr; } } @@ -712,7 +712,7 @@ class StatInner implements Stat { private nativeStat: long = 0; constructor(stat: long) { - if (this.nativeStat == 0) { + if (this.nativeStat === 0) { this.nativeStat = stat; } } @@ -740,76 +740,6 @@ class FileIoImpl { static native listFileSync(path: string, options?: ListFileOptions): string[]; - // static listFileSync1(path: string): string[] { - // return fileIo.listFileSync(path); - // } - - // static listFileSync2(path: string, options: ListFileOptions): string[] { - // return fileIo.listFileSync(path, options); - // } - - // static listFile(path: string): Promise { - // return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.listFileSync1, path); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as string[]; - // resolve(r); - // } - // }); - // }); - // } - - // static listFile(path: string, options: ListFileOptions): Promise { - // return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.listFileSync2, path, options); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as string[]; - // resolve(r); - // } - // }); - // }); - // } - - // static listFile(path: string, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.listFileSync1, path); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as string[]; - // callback(err, r); - // } - // }); - // } - - // static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.listFileSync2, path, options); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as string[]; - // callback(err, r); - // } - // }); - // } - static native mkdirSync(path: string): void; static native mkdirSync(path: string, recursion: boolean): void; @@ -822,76 +752,6 @@ class FileIoImpl { static native readTextSync(filePath: string, options?: ReadTextOptions): string; - // static readTextSync1(filePath: string): string { - // return fileIo.readTextSync(filePath); - // } - - // static readTextSync2(filePath: string, options: ReadTextOptions): string { - // return fileIo.readTextSync(filePath, options); - // } - - // static readText(filePath: string): Promise { - // return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.readTextSync1, filePath); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as string; - // resolve(r); - // } - // }); - // }); - // } - - // static readText(filePath: string, options: ReadTextOptions): Promise { - // return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - // let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - // promise.then((ret: NullishType): void => { - // if (ret === null || ret === undefined) { - // let err = new BusinessError(-1, ""); - - // reject(err); - // } else { - // let r = ret as string; - // resolve(r); - // } - // }); - // }); - // } - - // static readText(filePath: string, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.readTextSync1, filePath); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as string; - // callback(err, r); - // } - // }); - // } - - // static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - // let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - // promise.then((ret: NullishType): void => { - // let err = new BusinessError(-1, ""); - // if (ret === null || ret === undefined) { - // err.code = -1; - // callback(err, undefined); - // } else { - // err.code = 0; - // let r = ret as string; - // callback(err, r); - // } - // }); - // } - static native rmdirSync(path: string): void; static native statSync(file: string | number): Stat; @@ -986,13 +846,13 @@ class FileIoImpl { // let file = fileIo.openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.close(file, (err: BusinessError) => { -// if (err.code == 0) { -// console.log("closeCallbackTest: close success!"); -// } else { -// console.error("closeCallbackTest: close Error:", err); -// } -// }); + fileIo.close(file, (err: BusinessError) => { + if (err.code === 0) { + console.log("closeCallbackTest: close success!"); + } else { + console.error("closeCallbackTest: close Error:", err); + } + }); // console.println("closeCallbackTest end"); // } @@ -1101,7 +961,7 @@ class FileIoImpl { // function mkdirCallbackTest() { // console.println("mkdirCallbackTest begin") // fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { -// if (err.code == 0) { +// if (err.code === 0) { // console.println("mkdirCallbackTest success"); // } else { // console.println("mkdirCallbackTest failed!"); @@ -1422,7 +1282,7 @@ class FileIoImpl { // function unlinkCallbackTest() { // console.println("unlinkCallbackTest begin"); // fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { -// if (err.code == 0) { +// if (err.code === 0) { // console.error("unlinkCallbackTest success"); // } else { // console.error("unlinkCallbackTest Error!", err); @@ -1546,7 +1406,7 @@ class FileIoImpl { // function errorHandlerTest4() { // console.println("errorHandlerTest4 begin"); // fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { -// if (err.code == 0 && data !== undefined) { +// if (err.code === 0 && data !== undefined) { // console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); // } else { // console.error("open file Error!", err); @@ -1555,7 +1415,7 @@ class FileIoImpl { // fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { // console.println("2)-----------"); -// if (err.code == 0 && data !== undefined) { +// if (err.code === 0 && data !== undefined) { // console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); // } else { // console.error("open file Error!", err); -- Gitee From 2b58e2d35e89ef6544784754e9d30b982b923d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Mon, 17 Mar 2025 17:53:11 +0800 Subject: [PATCH 51/76] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1e681e0eae9f4b35261cb441e368b1173f173989 --- .../js/src/common/ani_helper/error_handler.h | 31 +++++++++++++++++-- .../kits/js/src/mod_fs/ani/error_handler.cpp | 3 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 7 +++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index dff2581f..ed91aea8 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -52,7 +52,8 @@ public: } private: - static ani_status Throw(ani_env *env, const char *className, int32_t code, const std::string &errMsg) + static ani_status Throw( + ani_env *env, const char *className, const char *name, int32_t code, const std::string &errMsg) { if (env == nullptr) { HILOGE("Invalid parameter env"); @@ -64,6 +65,11 @@ private: return ANI_INVALID_ARGS; } + if (name == nullptr) { + HILOGE("Invalid parameter name"); + return ANI_INVALID_ARGS; + } + ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class '%{private}s'", className); @@ -71,7 +77,7 @@ private: } ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { HILOGE("Cannot find constructor for class '%{private}s'", className); return ANI_NOT_FOUND; } @@ -88,7 +94,26 @@ private: return ANI_ERROR; } - auto status = env->ThrowError(static_cast(obj)); + ani_status status = ANI_ERROR; + status = AniHelper::SetFieldValue(env, cls, obj, "name", name); + if (status != ANI_OK) { + HILOGE("Set field 'name' value failed"); + return status; + } + + status = AniHelper::SetFieldValue(env, cls, obj, "message", errMsg); + if (status != ANI_OK) { + HILOGE("Set field 'message' value failed"); + return status; + } + + status = AniHelper::SetFieldValue(env, cls, obj, "code", static_cast(code)); + if (status != ANI_OK) { + HILOGE("Set field 'code' value failed"); + return status; + } + + status = env->ThrowError(static_cast(obj)); if (status != ANI_OK) { HILOGE("Throw ani error object failed!"); return status; diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp index 6fbacb66..0434519c 100644 --- a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp @@ -20,7 +20,8 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) { const char *className = "L@ohos/file/fs/BusinessError;"; - return Throw(env, className, code, errMsg); + const char *name = "BusinessError"; + return Throw(env, className, name, code, errMsg); } } // namespace OHOS::FileManagement::ModuleFileIO::ANI \ 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 ba81e481..f3c5c46f 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 @@ -583,6 +583,13 @@ export class BusinessError { message: string; code: number; data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; + } + constructor(code: number, msg: string, data?: T) { this.name = 'BusinessError'; this.code = code; -- Gitee From 3bbd1db61ffd2d28cd1c3fdf1c7b4d4b13faf94a Mon Sep 17 00:00:00 2001 From: liyuke Date: Wed, 19 Mar 2025 17:01:20 +0800 Subject: [PATCH 52/76] mkdir Signed-off-by: liyuke --- interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp index 1cade280..a5cd8837 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/mkdir_ani.cpp @@ -15,6 +15,7 @@ #include "mkdir_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "mkdir_core.h" #include "type_converter.h" @@ -29,11 +30,14 @@ void MkdirkAni::MkdirSync0(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s auto [succ, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = MkdirCore::DoMkdir(pathStr); if (!ret.IsSuccess()) { HILOGE("Mkdir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } @@ -43,11 +47,14 @@ void MkdirkAni::MkdirSync1(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s auto [succ, pathStr] = ANI::TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = MkdirCore::DoMkdir(pathStr, recursion); if (!ret.IsSuccess()) { HILOGE("DoMkdir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From 33faf321b0fad0504aeb3394015a1b566fc29319 Mon Sep 17 00:00:00 2001 From: liyuke Date: Wed, 19 Mar 2025 17:05:22 +0800 Subject: [PATCH 53/76] truncateSync Signed-off-by: liyuke --- .../kits/js/src/mod_fs/properties/ani/truncate_ani.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/truncate_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/truncate_ani.cpp index f2231a37..6176d45e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/truncate_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/truncate_ani.cpp @@ -15,6 +15,7 @@ #include "truncate_ani.h" +#include "error_handler.h" #include "file_utils.h" #include "filemgmt_libhilog.h" #include "truncate_core.h" @@ -30,18 +31,22 @@ void TruncateAni::TruncateSync(ani_env *env, [[maybe_unused]] ani_class clazz, a auto [succ, fileinfo] = TypeConverter::ToFileInfo(env, file); if (!succ) { HILOGE("Invalid fd/path"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succLen, len] = TypeConverter::ToOptionalInt64(env, length); if (!succLen) { HILOGE("Invalid truncate length"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = TruncateCore::DoTruncate(fileinfo, len); if (!ret.IsSuccess()) { HILOGE("Truncate failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From 06e7ecbad509a4a1d0e6d59244d43d37d936f168 Mon Sep 17 00:00:00 2001 From: liyuke Date: Wed, 19 Mar 2025 17:09:52 +0800 Subject: [PATCH 54/76] unlink Signed-off-by: liyuke --- interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp index 978ea3dd..91a929d1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/unlink_ani.cpp @@ -15,6 +15,7 @@ #include "unlink_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" #include "unlink_core.h" @@ -29,11 +30,14 @@ void UnlinkAni::UnlinkSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_s auto [succ, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succ) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = UnlinkCore::DoUnlink(pathStr); if (!ret.IsSuccess()) { HILOGE("Unlink failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From d640e5751738498a25c023ec4cae8e6e5ea37334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 17:12:41 +0800 Subject: [PATCH 55/76] =?UTF-8?q?=E9=87=87=E7=94=A8=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84BusinessError=E5=92=8CAsyncCallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 姜小林 Change-Id: I302c7b9ba9dbb6ff2ab67c13bf9234a2e435a513 --- interfaces/kits/js/BUILD.gn | 4 +- .../ani_helper}/error_handler.cpp | 2 +- .../js/src/mod_fs/ani/bind_function_class.cpp | 6 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 370 +++-- .../kits/js/src/mod_fs/ani/file_fs_class.ets | 1328 ----------------- 5 files changed, 185 insertions(+), 1525 deletions(-) rename interfaces/kits/js/src/{mod_fs/ani => common/ani_helper}/error_handler.cpp (93%) delete mode 100644 interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f992afa2..9334220b 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -670,10 +670,10 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/copy_listener", ] sources = [ + "src/common/ani_helper/error_handler.cpp", "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/error_handler.cpp", "src/mod_fs/class_file/ani/file_ani.cpp", "src/mod_fs/class_file/fs_file.cpp", "src/mod_fs/class_stat/ani/stat_ani.cpp", @@ -780,6 +780,7 @@ ohos_shared_library("ani_hash_class") { "src/mod_hash/ani", ] sources = [ + "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/common/file_helper/hash_file.cpp", @@ -849,6 +850,7 @@ ohos_shared_library("ani_securitylabel_class") { "src/mod_securitylabel", ] sources = [ + "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", diff --git a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp b/interfaces/kits/js/src/common/ani_helper/error_handler.cpp similarity index 93% rename from interfaces/kits/js/src/mod_fs/ani/error_handler.cpp rename to interfaces/kits/js/src/common/ani_helper/error_handler.cpp index 0434519c..a9e87cb8 100644 --- a/interfaces/kits/js/src/mod_fs/ani/error_handler.cpp +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.cpp @@ -19,7 +19,7 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) { - const char *className = "L@ohos/file/fs/BusinessError;"; + const char *className = "L@ohos/base/BusinessError;"; const char *name = "BusinessError"; return Throw(env, className, name, code, errMsg); } 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 489841a8..065495c9 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 @@ -79,15 +79,15 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, - // ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, - // ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, + ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, + ani_native_function { "openSync", nullptr, reinterpret_cast(OpenAni::OpenSync) }, ani_native_function { "readSync", nullptr, reinterpret_cast(ReadAni::ReadSync) }, ani_native_function { "readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync) }, ani_native_function { "rmdirSync", nullptr, reinterpret_cast(RmdirAni::RmdirSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, - // ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, + ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; return BindClass(env, className, 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 8738e3c4..8c855301 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 @@ -12,51 +12,51 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// import type { AsyncCallback } from '@ohos.base'; +import { BusinessError, AsyncCallback } from '@ohos.base'; function access(path: string, mode?: AccessModeType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute((path: string):boolean=> { - return FileIoImpl.doAccessSync(path); - }, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } else { - let promise = taskpool.execute((path: string, mode: AccessModeType):boolean=> { - return FileIoImpl.doAccessSync(path, mode); - }, path, mode); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } - }); - } + return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { + if (mode === undefined) { + let promise = taskpool.execute((path: string): boolean => { + return FileIoImpl.doAccessSync(path); + }, path); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } else { + let promise = taskpool.execute((path: string, mode: AccessModeType): boolean => { + return FileIoImpl.doAccessSync(path, mode); + }, path, mode); + promise.then((ret: NullishType) => { + if (ret === null || ret === undefined) { + let err = new BusinessError(); + err.code = -1; + reject(err); + } else { + let result = ret as boolean; + resolve(result); + } + }); + } + }); +} function access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path:string):boolean=> { - return FileIoImpl.doAccessSync(path); - } , path); + let promise = taskpool.execute((path: string): boolean => { + return FileIoImpl.doAccessSync(path); + }, path); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, false); } else { err.code = 0; let result = ret as boolean; @@ -70,8 +70,8 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi let promise = taskpool.execute(FileIoImpl.doAccessSync, path, mode, flag); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - + let err = new BusinessError(); + err.code = -1; reject(err); } else { let result = ret as boolean; @@ -104,7 +104,8 @@ function close(file: number | File): Promise { function close(file: number | File, callback: AsyncCallback): void { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; callback(e, undefined); }).catch((e: BusinessError): void => { callback(e, undefined); @@ -148,7 +149,8 @@ function mkdir(path: string): Promise { function mkdir(path: string, callback: AsyncCallback): void { let promise = taskpool.execute((path: string): undefined => mkdirSync1(path), path); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; callback(e, undefined); }).catch((e: BusinessError): void => { callback(e, undefined); @@ -171,7 +173,8 @@ function mkdir(path: string, recursion: boolean, callback: AsyncCallback): let promise = taskpool.execute((path: string, recursion: boolean): undefined => mkdirSync2(path, recursion), path, recursion); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; callback(e, undefined); }).catch((e: BusinessError): void => { callback(e, undefined); @@ -221,7 +224,8 @@ function open(path: String): Promise { function open(path: String, mode: number, callback: AsyncCallback): void { let promise = taskpool.execute(openSync1, path, mode); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let file = ret as File; callback(e, file); }).catch((e: BusinessError): void => { @@ -233,7 +237,8 @@ function open(path: String, mode: number, callback: AsyncCallback): function open(path: String, callback: AsyncCallback): void { let promise = taskpool.execute(openSync2, path); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let file = ret as File; callback(e, file); }).catch((e: BusinessError): void => { @@ -260,7 +265,8 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): promise.then((ret: NullishType): void => { let result = ret as number if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); + err.code = -1; reject(err); } else { resolve(result); @@ -275,7 +281,8 @@ function write(fd: number, buffer: string | ArrayBuffer): Promise { promise.then((ret: NullishType): void => { let result = ret as number if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); + err.code = -1; reject(err); } else { resolve(result); @@ -287,10 +294,10 @@ function write(fd: number, buffer: string | ArrayBuffer): Promise { function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { let promise = taskpool.execute(writeSync1, fd, buffer, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1 - callback(err, undefined) + callback(err, 0) } else { err.code = 0 let r = ret as number; @@ -302,10 +309,10 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(writeSync2, fd, buffer); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1 - callback(err, undefined) + callback(err, 0) } else { err.code = 0 let r = ret as number; @@ -327,7 +334,8 @@ function read(fd: number, buffer: ArrayBuffer): Promise { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as number; @@ -342,7 +350,8 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as number; @@ -355,10 +364,10 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise): void { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, 0); } else { err.code = 0; let r = ret as number; @@ -370,10 +379,10 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, 0); } else { err.code = 0; let r = ret as number; @@ -405,7 +414,8 @@ function unlink(path: string): Promise { function unlink(path: string, callback: AsyncCallback): void { let promise = taskpool.execute((path: string): undefined => unlinkSync(path), path); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; callback(e, undefined); }).catch((e: BusinessError): void => { callback(e, undefined); @@ -414,12 +424,12 @@ function unlink(path: string, callback: AsyncCallback): void { function readText(filePath: string, options?: ReadTextOptions): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - if (options == undefined) { + if (options === undefined) { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as string; @@ -430,8 +440,8 @@ function readText(filePath: string, options?: ReadTextOptions): Promise let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as string; @@ -445,10 +455,10 @@ function readText(filePath: string, options?: ReadTextOptions): Promise function readText(filePath: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, ''); } else { err.code = 0; let r = ret as string; @@ -460,10 +470,10 @@ function readText(filePath: string, callback: AsyncCallback): void { function readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, ''); } else { err.code = 0; let r = ret as string; @@ -478,11 +488,12 @@ function readTextSync(filePath: string, options?: ReadTextOptions): string { function listFile(path: string, options?: ListFileOptions): Promise { return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - if (options == undefined) { + if (options === undefined) { let promise = taskpool.execute(FileIoImpl.listFileSync, path); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as string[]; @@ -493,8 +504,8 @@ function listFile(path: string, options?: ListFileOptions): Promise { let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as string[]; @@ -508,10 +519,10 @@ function listFile(path: string, options?: ListFileOptions): Promise { function listFile(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, []); } else { err.code = 0; let r = ret as string[]; @@ -523,10 +534,10 @@ function listFile(path: string, callback: AsyncCallback): void { function listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + callback(err, []); } else { err.code = 0; let r = ret as string[]; @@ -552,8 +563,8 @@ function stat(file: string | number): Promise { let promise = taskpool.execute(FileIoImpl.statSync, file); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - + let err = new BusinessError(); + err.code = -1; reject(err); } else { let r = ret as Stat; @@ -566,10 +577,11 @@ function stat(file: string | number): Promise { function stat(file: string | number, callback: AsyncCallback): void { let p = taskpool.execute(FileIoImpl.statSync, file); p.then((ret: NullishType): void => { - let err = new BusinessError(-1, ""); + let err = new BusinessError(); if (ret === null || ret === undefined) { err.code = -1; - callback(err, undefined); + let stat: Stat = new StatInner(0); + callback(err, stat); } else { err.code = 0; let r = ret as Stat; @@ -578,30 +590,6 @@ function stat(file: string | number, callback: AsyncCallback): void }); } -export class BusinessError { - name: string; - message: string; - code: number; - data?: T; - - constructor() { - this.name = 'BusinessError'; - this.message = ''; - this.code = 0; - } - - constructor(code: number, msg: string, data?: T) { - this.name = 'BusinessError'; - this.code = code; - this.message = msg; - if (data !== undefined) { - this.data = data; - } - } -} - -export type AsyncCallback = (err: BusinessError, data?: T) => void; - export interface Filter { suffix?: Array; displayName?: Array; @@ -774,7 +762,7 @@ class FileIoImpl { // function accessSyncTest1() { // console.println("accessSyncTest1 begin"); // try { -// let ret = fileIo.accessSync("/data/local/tmp/a.txt"); +// let ret = accessSync("/data/local/tmp/a.txt"); // console.println(`accessSync result : ${ret}`); // } catch (error) { // console.error("accessSyncTest1 Error!", error); @@ -785,9 +773,9 @@ class FileIoImpl { // function closeSyncTest1() { // console.println("closeSyncTest1 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let file = openSync("/data/local/tmp/a.txt"); // console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.closeSync(file.fd); +// closeSync(file.fd); // console.println(`close file by fd, fd=${file.fd}`); // } catch (error) { // console.error("closeSyncTest1 Error!", error); @@ -798,9 +786,9 @@ class FileIoImpl { // function closeSyncTest2() { // console.println("closeSyncTest2 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// let file = openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.closeSync(file); +// closeSync(file); // console.println(`close file by file, fd=${file.fd}`); // } catch (error) { // console.error("closeSyncTest2 Error!", error); @@ -811,7 +799,7 @@ class FileIoImpl { // function closeSyncTest3() { // console.println("closeSyncTest3 begin"); // try { -// fileIo.closeSync(-1); +// closeSync(-1); // console.println(`close file by invalid fd: -1`); // } catch (error) { // console.error("closeSyncTest3 Error!", error); @@ -822,9 +810,9 @@ class FileIoImpl { // function closePromiseTest() { // console.println("closePromiseTest begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// let file = openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// fileIo.close(file.fd).then((result: undefined): void => { +// close(file.fd).then((result: undefined): void => { // console.println(`closePromiseTest close success`); // }).catch((e: Object): void => { // console.error("async closePromiseTest Error!!!", e); @@ -838,9 +826,9 @@ class FileIoImpl { // function closePromiseTest2() { // console.println("closePromiseTest2 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// let file = openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// await fileIo.close(file.fd); +// await close(file.fd); // console.println(`closePromiseTest2 close success`); // } catch (error) { // console.error("closePromiseTest2 Error:", error); @@ -850,24 +838,22 @@ class FileIoImpl { // function closeCallbackTest() { // console.println("closeCallbackTest begin"); -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// let file = openSync("/data/local/tmp/a.txt", 2); // console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - fileIo.close(file, (err: BusinessError) => { - if (err.code === 0) { - console.log("closeCallbackTest: close success!"); - } else { - console.error("closeCallbackTest: close Error:", err); - } - }); - +// close(file, (err: BusinessError) => { +// if (err.code === 0) { +// console.log("closeCallbackTest: close success!"); +// } else { +// console.error("closeCallbackTest: close Error:", err); +// } +// }); // console.println("closeCallbackTest end"); // } // function copyFileTest() { // console.println("copyFileTest begin"); // try { -// fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); +// copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); // } catch (error) { // console.error("copyFileTest Error!", error); // } @@ -876,7 +862,7 @@ class FileIoImpl { // function fileTest() { // console.println("fileTest begin"); -// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let file = openSync("/data/local/tmp/a.txt"); // let fd = file.fd; // console.println(`open file, fd=${fd}`); // let parent = file.getParent(); @@ -895,7 +881,7 @@ class FileIoImpl { // function listFileSyncTest1() { // console.println("listFileSyncTest1 begin"); // try { -// let files = fileIo.listFileSync("/data/local/tmp/"); +// let files = listFileSync("/data/local/tmp/"); // for (const file of files) { // console.println(file); // } @@ -911,7 +897,7 @@ class FileIoImpl { // let options: ListFileOptions = { // recursion: true // }; -// let files = fileIo.listFileSync("/data/", options); +// let files = listFileSync("/data/", options); // for (const file of files) { // console.println(file); // } @@ -924,7 +910,7 @@ class FileIoImpl { // function mkdirSyncTest1() { // console.println("mkdirSyncTest1 begin") // try { -// fileIo.mkdirSync("/data/local/tmp/dir01"); +// mkdirSync("/data/local/tmp/dir01"); // console.println(`mkdirSyncTest1 success`); // } catch (error) { // console.error("mkdirSyncTest1 Error!", error); @@ -935,7 +921,7 @@ class FileIoImpl { // function mkdirSyncTest2() { // console.println("mkdirSyncTest2 begin") // try { -// fileIo.mkdirSync("/data/local/tmp/dir02/sub01", true); +// mkdirSync("/data/local/tmp/dir02/sub01", true); // console.println(`mkdirSyncTest2 success`); // } catch (error) { // console.error("mkdirSyncTest2 Error!", error); @@ -946,7 +932,7 @@ class FileIoImpl { // function mkdirSyncTest3() { // console.println("mkdirSyncTest3 begin") // try { -// fileIo.mkdirSync("/data/local/tmp/dir03/sub01", false); +// mkdirSync("/data/local/tmp/dir03/sub01", false); // console.println(`mkdirSyncTest3 success`); // } catch (error) { // console.error("mkdirSyncTest3 Error!", error); @@ -957,7 +943,7 @@ class FileIoImpl { // function mkdirPromiseTest() { // console.println("mkdirPromiseTest begin") // try { -// await fileIo.mkdir("/data/local/tmp/dir04/sub01", true); +// await mkdir("/data/local/tmp/dir04/sub01", true); // console.println(`mkdirPromiseTest success`); // } catch (error) { // console.error("mkdirPromiseTest Error!", error); @@ -967,7 +953,7 @@ class FileIoImpl { // function mkdirCallbackTest() { // console.println("mkdirCallbackTest begin") -// fileIo.mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { +// mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { // if (err.code === 0) { // console.println("mkdirCallbackTest success"); // } else { @@ -979,7 +965,7 @@ class FileIoImpl { // function moveSyncTest() { // console.println("moveSyncTest begin") -// fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) +// moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) // console.println("moveSyncTest end") // } @@ -987,11 +973,11 @@ class FileIoImpl { // console.println("openSyncTest begin"); // try { // // 不指定mode -// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let file = openSync("/data/local/tmp/a.txt"); // console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); // // 以可读可写方式打开 // let mode = OpenMode.READ_WRITE; -// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// file = openSync("/data/local/tmp/a.txt", mode); // console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); // } catch (error) { // console.error("openSyncTest Error!", error); @@ -1003,11 +989,11 @@ class FileIoImpl { // console.println("openPromiseTest begin"); // try { // // 不指定mode -// let file = await fileIo.open("/data/local/tmp/a.txt"); +// let file = await open("/data/local/tmp/a.txt"); // console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); // // 以可读可写方式打开 // let mode = OpenMode.READ_WRITE; -// file = await fileIo.open("/data/local/tmp/a.txt", mode); +// file = await open("/data/local/tmp/a.txt", mode); // console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); // } catch (error) { // console.error("openPromiseTest Error!", error); @@ -1018,7 +1004,7 @@ class FileIoImpl { // function openCallbackTest() { // console.println("openCallbackTest begin"); // // 不指定mode -// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { +// open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { // if (data !== undefined) { // console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); // } else { @@ -1027,7 +1013,7 @@ class FileIoImpl { // }); // // 以可读可写方式打开 // let mode = OpenMode.READ_WRITE; -// fileIo.open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { +// open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { // if (data !== undefined) { // console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); // } else { @@ -1039,11 +1025,11 @@ class FileIoImpl { // function readSyncTest1() { // console.println("readSyncTest1 begin") // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let file = openSync("/data/local/tmp/a.txt") // let fd = file.fd; // console.println(`open file, fd=${fd}`) // let buffer = new ArrayBuffer(100); -// let length = fileIo.readSync(fd, buffer); +// let length = readSync(fd, buffer); // console.println(`read length: ${length}`) // let len = length as int; // let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); @@ -1059,14 +1045,14 @@ class FileIoImpl { // function readSyncTest2() { // console.println("readSyncTest2 begin") // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let file = openSync("/data/local/tmp/a.txt") // let fd = file.fd; // console.println(`open file, fd=${fd}`) // let buffer = new ArrayBuffer(100); // let options: ReadOptions = { // length: 5, // } -// let length = fileIo.readSync(fd, buffer, options); +// let length = readSync(fd, buffer, options); // console.println(`read length: ${length}`) // let len = length as int; // let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); @@ -1082,7 +1068,7 @@ class FileIoImpl { // function readSyncTest3() { // console.println("readSyncTest3 begin") // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt") +// let file = openSync("/data/local/tmp/a.txt") // let fd = file.fd; // console.println(`open file, fd=${fd}`) // let buffer = new ArrayBuffer(100); @@ -1090,7 +1076,7 @@ class FileIoImpl { // offset: 3, // length: 5, // } -// let length = fileIo.readSync(fd, buffer, options); +// let length = readSync(fd, buffer, options); // console.println(`read length: ${length}`) // let len = length as int; // let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); @@ -1106,7 +1092,7 @@ class FileIoImpl { // function readTextSyncTest1() { // console.println("readTextSyncTest1 begin") // try { -// let content = fileIo.readTextSync("/data/local/tmp/a.txt"); +// let content = readTextSync("/data/local/tmp/a.txt"); // console.println("-----------------------------------------------") // console.println(content) // console.println("-----------------------------------------------") @@ -1122,7 +1108,7 @@ class FileIoImpl { // let options: ReadTextOptions = { // length: 5, // } -// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// let content = readTextSync("/data/local/tmp/a.txt", options); // console.println("-----------------------------------------------"); // console.println(content); // console.println("-----------------------------------------------"); @@ -1139,7 +1125,7 @@ class FileIoImpl { // offset: 3, // length: 5, // } -// let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); +// let content = readTextSync("/data/local/tmp/a.txt", options); // console.println("-----------------------------------------------"); // console.println(content); // console.println("-----------------------------------------------"); @@ -1152,7 +1138,7 @@ class FileIoImpl { // function rmdirSyncTest() { // console.println("rmdirSyncTest begin"); // try { -// fileIo.rmdirSync("/data/local/tmp/dir01"); +// rmdirSync("/data/local/tmp/dir01"); // } catch (error) { // console.error("rmdirSyncTest Error!", error); // } @@ -1190,7 +1176,7 @@ class FileIoImpl { // function statSyncTest1() { // console.println("statSyncTest1 begin"); // try { -// let stat = fileIo.statSync("/data/local/tmp/a.txt"); +// let stat = statSync("/data/local/tmp/a.txt"); // printStatInfo(stat); // } catch (error) { // console.error("statSyncTest1 Error!", error); @@ -1201,9 +1187,9 @@ class FileIoImpl { // function statSyncTest2() { // console.println("statSyncTest2 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let file = openSync("/data/local/tmp/a.txt"); // console.println(`open file fd: ${file.fd}`); -// let stat = fileIo.statSync(file.fd); +// let stat = statSync(file.fd); // printStatInfo(stat); // } catch (error) { // console.error("statSyncTest2 Error!", error); @@ -1215,9 +1201,9 @@ class FileIoImpl { // console.println("statPromiseTest start"); // try { // let path = "/data/local/tmp/a.txt" -// let file = fileIo.openSync(path); +// let file = openSync(path); // console.println(`open file fd: ${file.fd}`); -// fileIo.stat(path).then((result: Stat): void => { +// stat(path).then((result: Stat): void => { // console.log("statPromiseTest success"); // printStatInfo(result); // }); @@ -1230,7 +1216,7 @@ class FileIoImpl { // function statCallbackTest() { // console.println("statCallbackTest start"); // let path = "/data/local/tmp/a.txt" -// fileIo.stat(path, (err: BusinessError, data?: Stat) => { +// stat(path, (err: BusinessError, data?: Stat) => { // if (data === undefined || data === null) { // console.error("Callback: Error stat:", err); // } else { @@ -1244,9 +1230,9 @@ class FileIoImpl { // function truncateSyncTest1() { // console.println("truncateSyncTest1 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt", 2); +// let file = openSync("/data/local/tmp/a.txt", 2); // console.println(`open file, fd=${file.fd}`); -// fileIo.truncateSync(file.fd, 4); +// truncateSync(file.fd, 4); // } catch (error) { // console.error("truncateSyncTest1 Error!", error); // } @@ -1256,7 +1242,7 @@ class FileIoImpl { // function truncateSyncTest2() { // console.println("truncateSyncTest2 begin"); // try { -// fileIo.truncateSync("/data/local/tmp/b.txt", 4); +// truncateSync("/data/local/tmp/b.txt", 4); // } catch (error) { // console.error("truncateSyncTest2 Error!", error); // } @@ -1266,7 +1252,7 @@ class FileIoImpl { // function unlinkSyncTest() { // console.println("unlinkSyncTest begin"); // try { -// fileIo.unlinkSync("/data/local/tmp/a1.txt"); +// unlinkSync("/data/local/tmp/a1.txt"); // console.println(`unlink result`); // } catch (error) { @@ -1278,7 +1264,7 @@ class FileIoImpl { // function unlinkPromiseTest() { // console.println("unlinkPromiseTest begin"); // try { -// await fileIo.unlink("/data/local/tmp/a2.txt"); +// await unlink("/data/local/tmp/a2.txt"); // console.println(`unlinkPromiseTest success`); // } catch (error) { // console.error("unlinkPromiseTest Error!", error); @@ -1288,7 +1274,7 @@ class FileIoImpl { // function unlinkCallbackTest() { // console.println("unlinkCallbackTest begin"); -// fileIo.unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { +// unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { // if (err.code === 0) { // console.error("unlinkCallbackTest success"); // } else { @@ -1302,13 +1288,13 @@ class FileIoImpl { // console.println("writeSyncTest1 begin"); // try { // let mode = 2; -// let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); +// let file = openSync("/data/local/tmp/a1.txt", mode); // let fd = file.fd; // console.println(`file open fd=${fd}`); // const options: WriteOptions = { // encoding: "utf-8", // }; -// let length = fileIo.writeSync(fd, "hello,world!", options); +// let length = writeSync(fd, "hello,world!", options); // console.println(`write file length=${length}`); // } catch (error) { // console.error("writeSyncTest1 Error!", error); @@ -1320,13 +1306,13 @@ class FileIoImpl { // console.println("writeSyncTest2 begin"); // try { // let mode = 2; -// let file = fileIo.openSync("/data/local/tmp/a2.txt", mode); +// let file = openSync("/data/local/tmp/a2.txt", mode); // let fd = file.fd; // console.println(`file open fd=${fd}`); // const options: WriteOptions = { // offset: 100, // }; -// let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); +// let length = writeSync(fd, new ArrayBuffer(1024), options); // console.println(`write file length=${length}`); // } catch (error) { // console.error("writeSyncTest2 Error!", error); @@ -1338,9 +1324,9 @@ class FileIoImpl { // console.println("writePromiseTest begin"); // try { // let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/a3.txt", mode); +// let file = openSync("/data/local/tmp/a3.txt", mode); // let fd = file.fd; -// let length = await fileIo.write(fd, "writePromiseTest"); +// let length = await write(fd, "writePromiseTest"); // console.println(`writePromiseTest length=${length}`); // } catch (error) { // console.error("writePromiseTest Error!", error); @@ -1351,9 +1337,9 @@ class FileIoImpl { // function writeCallbackTest() { // console.println("writeCallbackTest begin"); // let mode = OpenMode.READ_WRITE; -// let file = fileIo.openSync("/data/local/tmp/a4.txt", mode); +// let file = openSync("/data/local/tmp/a4.txt", mode); // let fd = file.fd; -// fileIo.write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { +// write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { // if (data !== undefined) { // console.println(`writeCallbackTest length=${data}`); // } else { @@ -1363,30 +1349,30 @@ class FileIoImpl { // console.println("writeCallbackTest end"); // } -// function errorHandlerTest1() { -// console.println("errorHandlerTest1 begin"); -// try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); -// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - -// console.println("\n"); -// console.println("try open file notexist.txt ..."); -// let mode = 2; -// file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); -// console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest1 Error!", error); -// } -// console.println("errorHandlerTest1 end"); -// } +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + let file = openSync("/data/local/tmp/a.txt"); + console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); + + console.println("\n"); + console.println("try open file notexist.txt ..."); + let mode = 2; + file = openSync("/data/local/tmp/notexist.txt", mode); + console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error: BusinessError) { + console.error(`errorHandlerTest1 Error! code=${error.code}, message=${error.message}`); + } + console.println("errorHandlerTest1 end"); +} // function errorHandlerTest2() { // console.println("errorHandlerTest1 begin"); // try { -// let file = fileIo.openSync("/data/local/tmp/a.txt"); +// let file = openSync("/data/local/tmp/a.txt"); // console.println("try open file a.txt ..."); // let mode = -1; -// file = fileIo.openSync("/data/local/tmp/a.txt", mode); +// file = openSync("/data/local/tmp/a.txt", mode); // console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); // } catch (error) { // console.error("errorHandlerTest2 Error!", error); @@ -1397,12 +1383,12 @@ class FileIoImpl { // function errorHandlerTest3() { // console.println("errorHandlerTest3 begin"); // try { -// let file = await fileIo.open("/data/local/tmp/a.txt"); +// let file = await open("/data/local/tmp/a.txt"); // console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); // console.println("-----------"); // console.println("try open file a.txt ..."); // let mode = -1; -// file = await fileIo.open("/data/local/tmp/a.txt", mode); +// file = await open("/data/local/tmp/a.txt", mode); // console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); // } catch (error) { // console.error("errorHandlerTest3 Error!", error); @@ -1412,7 +1398,7 @@ class FileIoImpl { // function errorHandlerTest4() { // console.println("errorHandlerTest4 begin"); -// fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { +// open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { // if (err.code === 0 && data !== undefined) { // console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); // } else { @@ -1420,7 +1406,7 @@ class FileIoImpl { // } // }); -// fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { +// open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { // console.println("2)-----------"); // if (err.code === 0 && data !== undefined) { // console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); @@ -1473,7 +1459,7 @@ function main() { // writeSyncTest2(); // writePromiseTest(); // writeCallbackTest(); - // errorHandlerTest1(); // 打开文件失败,文件不存在 + errorHandlerTest1(); // 打开文件失败,文件不存在 // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise // errorHandlerTest4(); // 异步callback diff --git a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets b/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets deleted file mode 100644 index c4ab4d4e..00000000 --- a/interfaces/kits/js/src/mod_fs/ani/file_fs_class.ets +++ /dev/null @@ -1,1328 +0,0 @@ -/* - * 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. - */ - -export default fileIo; - -export class BusinessError { - name: string; - message: string; - code: number; - data?: T; - constructor(code: number, msg: string, data?: T) { - this.name = 'BusinessError'; - this.code = code; - this.message = msg; - if (data !== undefined) { - this.data = data; - } - } -} - -export type AsyncCallback = (err: BusinessError, data?: T) => void; - -export interface Filter { - suffix?: Array; - displayName?: Array; - mimeType?: Array; - fileSizeOver?: number; - lastModifiedAfter?: number; - excludeMedia?: boolean; -} - -export interface ListFileOptions { - recursion?: boolean; - listNum?: number; - filter?: Filter; -} - -export interface ReadOptions { - offset?: number; - length?: number; -} - -export interface ReadTextOptions extends ReadOptions { - encoding?: string; -} - -export interface WriteOptions { - offset?: long; - length?: long; - encoding?: string; -} - -enum AccessModeType { - EXIST = 0, - WRITE = 2, - READ = 4, - READ_WRITE = 6, -} - -enum AccessFlagType { - LOCAL = 0, -} - -interface File { - fd: int; - path: String; - name: String; - - getParent(): String; - lock(exclusive?: boolean): void; - tryLock(exclusive?: boolean): void; - unlock(): void; -} - -class FileInner implements File { - fd: int = -1; - path: String = ""; - name: String = ""; - - private nativePtr: long = 0; - - constructor(ptr: long) { - if (this.nativePtr == 0) { - this.nativePtr = ptr; - } - } - - native getParent(): String; - native lock(exclusive?: boolean): void; - native tryLock(exclusive?: boolean): void; - native unlock(): void; - -} - -enum LocationType { - LOCAL = 1, - CLOUD = 2 -} - -interface Stat { - ino: bigint; - mode: number; - uid: number; - gid: number; - size: number; - atime: number; - mtime: number; - ctime: number; - atimeNs: bigint; - mtimeNs: bigint; - ctimeNs: bigint; - location: LocationType; - - isBlockDevice(): boolean; - isCharacterDevice(): boolean; - isDirectory(): boolean; - isFIFO(): boolean; - isFile(): boolean; - isSocket(): boolean; - isSymbolicLink(): boolean; -} - -class StatInner implements Stat { - ino: bigint = 0n; - mode: number; - uid: number; - gid: number; - size: number; - atime: number; - mtime: number; - ctime: number; - atimeNs: bigint = 0n; - mtimeNs: bigint = 0n; - ctimeNs: bigint = 0n; - location: LocationType = LocationType.LOCAL; - - private nativeStat: long = 0; - - constructor(stat: long) { - if (this.nativeStat == 0) { - this.nativeStat = stat; - } - } - - native isBlockDevice(): boolean; - native isCharacterDevice(): boolean; - native isDirectory(): boolean; - native isFIFO(): boolean; - native isFile(): boolean; - native isSocket(): boolean; - native isSymbolicLink(): boolean; -} - -type FdOrFile = int | File; -type PathOrFd = string | int; -type BufferType = string | ArrayBuffer; - -class fileIo { - - static { - loadLibrary("ani_fs_class.z"); - } - - static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; - - static accessSync(path: string, mode?: AccessModeType): boolean { - return fileIo.doAccessSync(path, mode); - } - - static accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { - return fileIo.doAccessSync(path, mode, flag); - } - - static accessSync1(path: string): boolean { - return fileIo.accessSync(path); - } - - static accessSync2(path: string, mode: AccessModeType): boolean { - return fileIo.accessSync(path, mode); - } - - static accessSync3(path: string, mode: AccessModeType, flag: AccessFlagType): boolean { - return fileIo.accessSync(path, mode, flag); - } - - static access(path: string, mode?: AccessModeType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } else { - let promise = taskpool.execute(fileIo.accessSync2, path, mode); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - } - }); - } - - static access(path: string, mode: AccessModeType, flag: AccessFlagType): Promise { - return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.accessSync3, path, mode, flag); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let result = ret as boolean; - resolve(result); - } - }); - }) - } - - static access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.accessSync1, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let result = ret as boolean; - callback(err, result); - } - }); - } - - static native closeSync(file: FdOrFile): int; - - static close(file: FdOrFile): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.closeSync, file); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as int; - resolve(r); - } - }); - }); - } - - static close(file: FdOrFile, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.closeSync, file); - p1.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } - }); - } - - static native copyFileSync(src: PathOrFd, dest: PathOrFd, mode?: int): void; - - static native listFileSync(path: string, options?: ListFileOptions): string[]; - - static listFileSync1(path: string): string[] { - return fileIo.listFileSync(path); - } - - static listFileSync2(path: string, options: ListFileOptions): string[] { - return fileIo.listFileSync(path, options); - } - - static listFile(path: string): Promise { - return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } - }); - }); - } - - static listFile(path: string, options: ListFileOptions): Promise { - return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } - }); - }); - } - - static listFile(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.listFileSync1, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); - } - - static listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.listFileSync2, path, options); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } - }); - } - - static native mkdirSync(path: string): int; - - static native mkdirSync(path: string, recursion: boolean): int; - - static mkdir(path: string): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); - promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static mkdir(path: string, callback: AsyncCallback): void { - const mkdirSyncWrapper = (path: string) => fileIo.mkdirSync(path); - let promise = taskpool.execute(mkdirSyncWrapper, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } - }); - } - - static mkdir(path: string, recursion: boolean): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - const mkdirSyncWrapper = (path: string, recursion: boolean) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); - promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { - const mkdirSyncWrapper = (path: string, recursion: boolean,) => fileIo.mkdirSync(path, recursion); - let promise = taskpool.execute(mkdirSyncWrapper, path, recursion); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } - }); - } - - static native moveFileSync(src: String, dest: String, mode?: int): void; - - static native openSync(path: String, mode?: int): File; - - static openSync1(path: String, mode: int): File { - return fileIo.openSync(path, mode); - } - - static openSync2(path: String): File { - return fileIo.openSync(path); - } - - static open(path: String, mode: int): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.openSync1, path, mode); - promise.then((ret: NullishType) => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } - - static open(path: String): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - err.code = -1; - reject(err); - } else { - let r = ret as File; - resolve(r); - } - }); - }); - } - - static open(path: String, mode: int, callback: AsyncCallback): void { - let p1 = taskpool.execute(fileIo.openSync1, path, mode); - p1.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as File; - callback(err, r); - } - }); - } - - static open(path: String, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.openSync2, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as File; - callback(err, r); - } - }); - } - - static native readSync(fd: int, buffer: ArrayBuffer, options?: ReadOptions): long; - - static readSync1(fd: int, buffer: ArrayBuffer): long { - return fileIo.readSync(fd, buffer); - } - - static readSync2(fd: int, buffer: ArrayBuffer, options: ReadOptions): long { - return fileIo.readSync(fd, buffer, options); - } - - static read(fd: int, buffer: ArrayBuffer): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as long; - resolve(r); - } - }); - }); - } - - static read(fd: int, buffer: ArrayBuffer, options: ReadOptions): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - let r = ret as long; - resolve(r); - } - }); - }); - } - - static read(fd: int, buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync1, fd, buffer); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as long; - callback(err, r); - } - }); - } - - static read(fd: int, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as long; - callback(err, r); - } - }); - } - - static native readTextSync(filePath: string, options?: ReadTextOptions): string; - - static readTextSync1(filePath: string): string { - return fileIo.readTextSync(filePath); - } - - static readTextSync2(filePath: string, options: ReadTextOptions): string { - return fileIo.readTextSync(filePath, options); - } - - static readText(filePath: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static readText(filePath: string, options: ReadTextOptions): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static readText(filePath: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync1, filePath); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } - }); - } - - static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } - }); - } - - static native rmdirSync(path: string): void; - - static native statSync(file: PathOrFd): Stat; - - static stat(file: PathOrFd): Promise { - return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.statSync, file); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as Stat; - resolve(r); - } - }); - }); - } - - static stat(file: PathOrFd, callback: AsyncCallback): void { - let p = taskpool.execute(fileIo.statSync, file); - p.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); - } else { - err.code = 0; - let r = ret as Stat; - callback(err, r); - } - }); - } - - static native truncateSync(file: PathOrFd, len?: long): void; - - static native unlinkSync(path: string): int; - - static unlink(path: string): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.unlinkSync, path); - promise.then((ret: NullishType) => { - let result = ret as int - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static unlink(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.unlinkSync, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } - }); - } - - static native writeSync(fd: int, buffer: BufferType, options?: WriteOptions): long; - - static writeSync1(fd: int, buffer: BufferType, options: WriteOptions): long { - return fileIo.writeSync(fd, buffer, options); - } - - static writeSync2(fd: int, buffer: BufferType): long { - return fileIo.writeSync(fd, buffer); - } - - static write(fd: int, buffer: BufferType, options: WriteOptions): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType) => { - let result = ret as long - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static write(fd: int, buffer: BufferType): Promise { - return new Promise((resolve: (result: long) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType) => { - let result = ret as long - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - - reject(err); - } else { - resolve(result); - } - }); - }); - } - - static write(fd: int, buffer: BufferType, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.writeSync1, fd, buffer, options); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as long; - callback(err, r); - } - }); - } - - static write(fd: int, buffer: BufferType, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.writeSync2, fd, buffer); - promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as long; - callback(err, r); - } - }); - } -} - -function accessSyncTest1() { - console.println("accessSyncTest1 begin"); - try { - let ret = fileIo.accessSync("/data/local/tmp/a.txt"); - console.println(`accessSync result : ${ret}`); - } catch (error) { - console.error("accessSyncTest1 Error!", error); - } - console.println("accessSyncTest1 end"); -} - -function closeSyncTest1() { - console.println("closeSyncTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let ret = fileIo.closeSync(file.fd); - console.println(`close file by fd, fd=${file.fd}, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest1 Error!", error); - } - console.println("closeSyncTest1 end"); -} - -function closeSyncTest2() { - console.println("closeSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let ret = fileIo.closeSync(file); - console.println(`close file by file, fd=${file.fd}, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest2 Error!", error); - } - console.println("closeSyncTest2 end"); -} - -function closeSyncTest3() { - console.println("closeSyncTest3 begin"); - try { - let ret = fileIo.closeSync(-1); - console.println(`close file by invalid fd: -1, ret=${ret}`); - } catch (error) { - console.error("closeSyncTest3 Error!", error); - } - console.println("closeSyncTest3 end"); -} - -function closePromiseTest() { - console.println("closePromiseTest begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.close(file.fd).then((result: int) => { - console.println(`closePromiseTest close result: ${result}`); - }); - } catch (error) { - console.error("closePromiseTest Error:", error); - } - console.println("closePromiseTest end"); -} - -function closeCallbackTest() { - console.println("closeCallbackTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - fileIo.close(file, (err: BusinessError, data?: int) => { - if (err.code) { - console.error("closeCallbackTest: close Error:", err); - } else { - console.log("closeCallbackTest: close result:", data); - } - }); - console.println("closeCallbackTest end"); -} - -function copyFileTest() { - console.println("copyFileTest begin"); - try { - fileIo.copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); - } catch (error) { - console.error("copyFileTest Error!", error); - } - console.println("copyFileTest end"); -} - -function fileTest() { - console.println("fileTest begin"); - let file = fileIo.openSync("/data/local/tmp/a.txt"); - let fd = file.fd; - console.println(`open file, fd=${fd}`); - let parent = file.getParent(); - console.println(`file parent=${parent}`); - file.lock(); - console.println(`file lock completed`); - file.unlock(); - console.println(`file unlock completed`); - file.tryLock(true); - console.println(`file tryLock completed`); - file.unlock(); - console.println(`file unlock completed`); - console.println("fileTest end"); -} - -function listFileTest() { - console.println("listFileTest begin"); - try { - let files = fileIo.listFileSync("/data/local/tmp/"); - for (const file of files) { - console.println(file); - } - } catch (error) { - console.error("listFileTest Error!", error); - } - console.println("listFileTest end"); -} - -function mkdirTest1() { - console.println("mkdirTest1 begin") - try { - const ret = fileIo.mkdirSync("/data/local/tmp/dir01"); - console.println(`mkdir result: ${ret}`); - } catch (error) { - console.error("mkdirTest1 Error!", error); - } - console.println("mkdirTest1 end"); -} - -function moveSyncTest() { - console.println("moveSyncTest begin") - fileIo.moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) - console.println("moveSyncTest end") -} - -function openSyncTest() { - console.println("openSyncTest begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - let mode = 2; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openSyncTest Error!", error); - } - console.println("openSyncTest end"); -} - -function readSyncTest1() { - console.println("readSyncTest1 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let length = fileIo.readSync(fd, buffer); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest1 Error!", error); - } - console.println("readSyncTest1 end") -} - -function readSyncTest2() { - console.println("readSyncTest2 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest2 Error!", error); - } - console.println("readSyncTest2 end") -} - -function readSyncTest3() { - console.println("readSyncTest3 begin") - try { - let file = fileIo.openSync("/data/local/tmp/a.txt") - let fd = file.fd; - console.println(`open file, fd=${fd}`) - let buffer = new ArrayBuffer(100); - let options: ReadOptions = { - offset: 3, - length: 5, - } - let length = fileIo.readSync(fd, buffer, options); - console.println(`read length: ${length}`) - let len = length as int; - let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readSyncTest3 Error!", error); - } - console.println("readSyncTest3 end") -} - -function readTextSyncTest1() { - console.println("readTextSyncTest1 begin") - try { - let content = fileIo.readTextSync("/data/local/tmp/a.txt"); - console.println("-----------------------------------------------") - console.println(content) - console.println("-----------------------------------------------") - } catch (error) { - console.error("readTextSyncTest1 Error!", error); - } - console.println("readTextSyncTest1 end") -} - -function readTextSyncTest2() { - console.println("readTextSyncTest2 begin"); - try { - let options: ReadTextOptions = { - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest2 Error!", error); - } - console.println("readTextSyncTest2 end"); -} - -function readTextSyncTest3() { - console.println("readTextSyncTest3 begin") - try { - let options: ReadTextOptions = { - offset: 3, - length: 5, - } - let content = fileIo.readTextSync("/data/local/tmp/a.txt", options); - console.println("-----------------------------------------------"); - console.println(content); - console.println("-----------------------------------------------"); - } catch (error) { - console.error("readTextSyncTest3 Error!", error); - } - console.println("readTextSyncTest3 end"); -} - -function rmdirSyncTest() { - console.println("rmdirSyncTest begin"); - try { - fileIo.rmdirSync("/data/local/tmp/dir01"); - } catch (error) { - console.error("rmdirSyncTest Error!", error); - } - console.println("rmdirSyncTest end"); -} - -function printStatInfo(stat?: Stat) { - if (stat === undefined) { - console.error("stat is null or undefined!"); - return; - } - console.info("stat, ino is " + stat.ino); - console.info("stat, mode is " + stat.mode); - console.info("stat, uid is " + stat.uid); - console.info("stat, gid is " + stat.gid); - console.info("stat, size is " + stat.size); - console.info("stat, atime is " + stat.atime); - console.info("stat, mtime is " + stat.mtime); - console.info("stat, ctime is " + stat.ctime); - console.info("stat, atimeNs is " + stat.atimeNs); - console.info("stat, mtimeNs is " + stat.mtimeNs); - console.info("stat, ctimeNs is " + stat.ctimeNs); - console.info("stat, location is " + stat.location); - console.info("stat, isBlockDevice is " + stat.isBlockDevice()); - console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); - console.info("stat, isDirectory is " + stat.isDirectory()); - console.info("stat, isFIFO is " + stat.isFIFO()); - console.info("stat, isFile is " + stat.isFile()); - console.info("stat, isSocket is " + stat.isSocket()); - console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); -} - -function statSyncTest1() { - console.println("statSyncTest1 begin"); - try { - let stat = fileIo.statSync("/data/local/tmp/a.txt"); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest1 Error!", error); - } - console.println("statSyncTest1 end"); -} - -function statSyncTest2() { - console.println("statSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file fd: ${file.fd}`); - let stat = fileIo.statSync(file.fd); - printStatInfo(stat); - } catch (error) { - console.error("statSyncTest2 Error!", error); - } - console.println("statSyncTest2 end"); -} - -function statPromiseTest() { - console.println("statPromiseTest start"); - try { - let path = "/data/local/tmp/a.txt" - let file = fileIo.openSync(path); - console.println(`open file fd: ${file.fd}`); - fileIo.stat(path).then((result: Stat) => { - console.log("statPromiseTest success"); - printStatInfo(result); - }); - } catch (error) { - console.error("statPromiseTest Error!", error); - } - console.println("statPromiseTest end"); -} - -function statCallbackTest() { - console.println("statCallbackTest start"); - let path = "/data/local/tmp/a.txt" - fileIo.stat(path, (err: BusinessError, data?: Stat) => { - if (data === undefined || data === null) { - console.error("Callback: Error stat:", err); - } else { - console.log("Callback stat success"); - printStatInfo(data); - } - }); - console.println("statCallbackTest end"); -} - -function truncateSyncTest1() { - console.println("truncateSyncTest1 begin"); - try { - fileIo.truncateSync(-1, 4); - } catch (error) { - console.error("truncateSyncTest1 Error!", error); - } - console.println("truncateSyncTest1 end"); -} - -function truncateSyncTest2() { - console.println("truncateSyncTest2 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt", 2); - console.println(`open file, fd=${file.fd}`); - fileIo.truncateSync(file.fd, 4); - } catch (error) { - console.error("truncateSyncTest2 Error!", error); - } - console.println("truncateSyncTest2 end"); -} - -function truncateSyncTest3() { - console.println("truncateSyncTest3 begin"); - try { - fileIo.truncateSync("/data/local/tmp/b.txt", 4); - } catch (error) { - console.error("truncateSyncTest3 Error!", error); - } - console.println("truncateSyncTest3 end"); -} - -function unlinkSyncTest() { - console.println("unlinkSyncTest begin"); - try { - const ret = fileIo.unlinkSync("/data/local/tmp/b.txt"); - console.println(`unlink result: ${ret}`); - - } catch (error) { - console.error("unlinkSyncTest Error!", error); - } - console.println("unlinkSyncTest end"); -} - -function writeSyncTest1() { - console.println("writeSyncTest1 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/a1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - encoding: "utf-8", - }; - let length = fileIo.writeSync(fd, "hello,world!", options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest1 Error!", error); - } - console.println("writeSyncTest1 end"); -} - -function writeSyncTest2() { - console.println("writeSyncTest2 begin"); - try { - let mode = 2; - let file = fileIo.openSync("/data/local/tmp/b1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - offset: 100, - }; - let length = fileIo.writeSync(fd, new ArrayBuffer(1024), options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest2 Error!", error); - } - console.println("writeSyncTest2 end"); -} - -function errorHandlerTest1() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - console.println("\n"); - console.println("try open file notexist.txt ..."); - let mode = 2; - file = fileIo.openSync("/data/local/tmp/notexist.txt", mode); - console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest1 Error!", error); - } - console.println("errorHandlerTest1 end"); -} - -function errorHandlerTest2() { - console.println("errorHandlerTest1 begin"); - try { - let file = fileIo.openSync("/data/local/tmp/a.txt"); - console.println("try open file a.txt ..."); - let mode = -1; - file = fileIo.openSync("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest2 Error!", error); - } - console.println("errorHandlerTest2 end"); -} - -function errorHandlerTest3() { - console.println("errorHandlerTest3 begin"); - try { - let file = await fileIo.open("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - console.println("-----------"); - console.println("try open file a.txt ..."); - let mode = -1; - file = await fileIo.open("/data/local/tmp/a.txt", mode); - console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("errorHandlerTest3 Error!", error); - } - console.println("errorHandlerTest3 end"); -} - -function errorHandlerTest4() { - console.println("errorHandlerTest4 begin"); - fileIo.open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - - fileIo.open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { - console.println("2)-----------"); - if (err.code == 0 && data !== undefined) { - console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open file Error!", err); - } - }); - console.println("errorHandlerTest4 end"); -} - -function main() { - console.println("---------- hello ani --------------"); - // accessSyncTest1(); - // openSyncTest(); - // closeSyncTest1(); - // closeSyncTest2(); - // closeSyncTest3(); - // closePromiseTest(); - // closeCallbackTest(); - // copyFileTest(); - // fileTest(); - // listFileTest(); - // mkdirTest1(); - // moveSyncTest(); - // readSyncTest1(); - // readSyncTest2(); - // readSyncTest3(); - // readTextSyncTest1(); - // readTextSyncTest2(); - // readTextSyncTest3(); - // rmdirSyncTest(); - // statSyncTest1(); - // statSyncTest2(); - // statPromiseTest(); - // statCallbackTest(); - // truncateSyncTest1(); - // truncateSyncTest2(); - // truncateSyncTest3(); - // unlinkSyncTest(); - // writeSyncTest1(); - // writeSyncTest2(); - // errorHandlerTest1(); // 打开文件失败,文件不存在 - errorHandlerTest2(); // 参数校验失败 - // errorHandlerTest3(); // 异步Promise - // errorHandlerTest4(); // 异步callback - console.println("---------- hello ani end ---------------"); -} \ No newline at end of file -- Gitee From 9948f7315244e28e2860867e464743a486ea2d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 17:59:06 +0800 Subject: [PATCH 56/76] =?UTF-8?q?file=E3=80=81open=E3=80=81write=E3=80=81m?= =?UTF-8?q?ove=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I6bee37d362849db954bd6d20056f670edf7e2c2a --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 425 ++++++++---------- .../js/src/mod_fs/class_file/ani/file_ani.cpp | 15 +- .../js/src/mod_fs/properties/ani/move_ani.cpp | 6 + .../src/mod_fs/properties/ani/write_ani.cpp | 8 + 4 files changed, 222 insertions(+), 232 deletions(-) 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 8c855301..465ad015 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 @@ -189,40 +189,30 @@ function openSync(path: string, mode?: number): File { return FileIoImpl.openSync(path, mode); } -function openSync1(path: String, mode: number): File { - return FileIoImpl.openSync(path, mode); -} - -function openSync2(path: String): File { - return FileIoImpl.openSync(path); -} - -function open(path: String, mode: number): Promise { - return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(openSync1, path, mode); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); -} - -function open(path: String): Promise { +function open(path: String, mode?: number): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(openSync2, path); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); + if (mode === undefined) { + let promise = taskpool.execute(FileIoImpl.openSync, path); + promise.then((ret: NullishType): void => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); + } else { + let promise = taskpool.execute(FileIoImpl.openSync, path, mode); + promise.then((ret: NullishType): void => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); + } }); } function open(path: String, mode: number, callback: AsyncCallback): void { - let promise = taskpool.execute(openSync1, path, mode); + let promise = taskpool.execute(FileIoImpl.openSync, path, mode); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -235,7 +225,7 @@ function open(path: String, mode: number, callback: AsyncCallback): } function open(path: String, callback: AsyncCallback): void { - let promise = taskpool.execute(openSync2, path); + let promise = taskpool.execute(FileIoImpl.openSync, path); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -251,84 +241,53 @@ function writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOpti return FileIoImpl.writeSync(fd, buffer, options); } -function writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { - return FileIoImpl.writeSync(fd, buffer, options); -} - -function writeSync2(fd: number, buffer: string | ArrayBuffer): number { - return FileIoImpl.writeSync(fd, buffer); -} - -function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): Promise { +function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(writeSync1, fd, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { + if (options === undefined) { + let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number resolve(result); - } - }); - }); -} - -function write(fd: number, buffer: string | ArrayBuffer): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(writeSync2, fd, buffer); - promise.then((ret: NullishType): void => { - let result = ret as number - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { + }).catch((e: BusinessError): void => { + reject(e); + }); + } else { + let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number resolve(result); - } - }); + }).catch((e: BusinessError): void => { + reject(e); + }); + } }); } -function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(writeSync1, fd, buffer, options); +function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, + callback: AsyncCallback): void { + let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, 0) - } else { - err.code = 0 - let r = ret as number; - callback(err, r); - } + let e = new BusinessError(); + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); }); } function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(writeSync2, fd, buffer); + let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer); promise.then((ret: NullishType): void => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, 0) - } else { - err.code = 0 - let r = ret as number; - callback(err, r); - } + let e = new BusinessError(); + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); }); } -function readSync1(fd: number, buffer: ArrayBuffer): number { - return FileIoImpl.readSync(fd, buffer) -} - -function readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { - return FileIoImpl.readSync(fd, buffer, options) -} - function read(fd: number, buffer: ArrayBuffer): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); @@ -860,23 +819,23 @@ class FileIoImpl { // console.println("copyFileTest end"); // } -// function fileTest() { -// console.println("fileTest begin"); -// let file = openSync("/data/local/tmp/a.txt"); -// let fd = file.fd; -// console.println(`open file, fd=${fd}`); -// let parent = file.getParent(); -// console.println(`file parent=${parent}`); -// file.lock(); -// console.println(`file lock completed`); -// file.unlock(); -// console.println(`file unlock completed`); -// file.tryLock(true); -// console.println(`file tryLock completed`); -// file.unlock(); -// console.println(`file unlock completed`); -// console.println("fileTest end"); -// } +function fileTest() { + console.println("fileTest begin"); + let file = openSync("/data/local/tmp/a.txt"); + let fd = file.fd; + console.println(`open file, fd=${fd}`); + let parent = file.getParent(); + console.println(`file parent=${parent}`); + file.lock(); + console.println(`file lock completed`); + file.unlock(); + console.println(`file unlock completed`); + file.tryLock(true); + console.println(`file tryLock completed`); + file.unlock(); + console.println(`file unlock completed`); + console.println("fileTest end"); +} // function listFileSyncTest1() { // console.println("listFileSyncTest1 begin"); @@ -963,64 +922,68 @@ class FileIoImpl { // console.println("mkdirCallbackTest end"); // } -// function moveSyncTest() { -// console.println("moveSyncTest begin") -// moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) -// console.println("moveSyncTest end") -// } +function moveSyncTest() { + console.println("moveSyncTest begin") + try { + moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) + } catch (error) { + console.error("moveSyncTest Error!", error); + } + console.println("moveSyncTest end") +} -// function openSyncTest() { -// console.println("openSyncTest begin"); -// try { -// // 不指定mode -// let file = openSync("/data/local/tmp/a.txt"); -// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// // 以可读可写方式打开 -// let mode = OpenMode.READ_WRITE; -// file = openSync("/data/local/tmp/a.txt", mode); -// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("openSyncTest Error!", error); -// } -// console.println("openSyncTest end"); -// } +function openSyncTest() { + console.println("openSyncTest begin"); + try { + // 不指定mode + let file = openSync("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + // 以可读可写方式打开 + let mode = 2; + file = openSync("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openSyncTest Error!", error); + } + console.println("openSyncTest end"); +} -// function openPromiseTest() { -// console.println("openPromiseTest begin"); -// try { -// // 不指定mode -// let file = await open("/data/local/tmp/a.txt"); -// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// // 以可读可写方式打开 -// let mode = OpenMode.READ_WRITE; -// file = await open("/data/local/tmp/a.txt", mode); -// console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("openPromiseTest Error!", error); -// } -// console.println("openPromiseTest end"); -// } +function openPromiseTest() { + console.println("openPromiseTest begin"); + try { + // 不指定mode + let file = await open("/data/local/tmp/a.txt"); + console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); + // 以可读可写方式打开 + let mode = 2; + file = await open("/data/local/tmp/a.txt", mode); + console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); + } catch (error) { + console.error("openPromiseTest Error!", error); + } + console.println("openPromiseTest end"); +} -// function openCallbackTest() { -// console.println("openCallbackTest begin"); -// // 不指定mode -// open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { -// if (data !== undefined) { -// console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open failed!", err); -// } -// }); -// // 以可读可写方式打开 -// let mode = OpenMode.READ_WRITE; -// open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { -// if (data !== undefined) { -// console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open failed!", err); -// } -// }); -// } +function openCallbackTest() { + console.println("openCallbackTest begin"); + // 不指定mode + open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { + if (data !== undefined) { + console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open failed!", err); + } + }); + // 以可读可写方式打开 + let mode = 2; + open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { + if (data !== undefined) { + console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); + } else { + console.error("open failed!", err); + } + }); +} // function readSyncTest1() { // console.println("readSyncTest1 begin") @@ -1284,70 +1247,70 @@ class FileIoImpl { // console.println("unlinkCallbackTest end"); // } -// function writeSyncTest1() { -// console.println("writeSyncTest1 begin"); -// try { -// let mode = 2; -// let file = openSync("/data/local/tmp/a1.txt", mode); -// let fd = file.fd; -// console.println(`file open fd=${fd}`); -// const options: WriteOptions = { -// encoding: "utf-8", -// }; -// let length = writeSync(fd, "hello,world!", options); -// console.println(`write file length=${length}`); -// } catch (error) { -// console.error("writeSyncTest1 Error!", error); -// } -// console.println("writeSyncTest1 end"); -// } +function writeSyncTest1() { + console.println("writeSyncTest1 begin"); + try { + let mode = 2; + let file = openSync("/data/local/tmp/a1.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + encoding: "utf-8", + }; + let length = writeSync(fd, "hello,world!", options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest1 Error!", error); + } + console.println("writeSyncTest1 end"); +} -// function writeSyncTest2() { -// console.println("writeSyncTest2 begin"); -// try { -// let mode = 2; -// let file = openSync("/data/local/tmp/a2.txt", mode); -// let fd = file.fd; -// console.println(`file open fd=${fd}`); -// const options: WriteOptions = { -// offset: 100, -// }; -// let length = writeSync(fd, new ArrayBuffer(1024), options); -// console.println(`write file length=${length}`); -// } catch (error) { -// console.error("writeSyncTest2 Error!", error); -// } -// console.println("writeSyncTest2 end"); -// } +function writeSyncTest2() { + console.println("writeSyncTest2 begin"); + try { + let mode = 2; + let file = openSync("/data/local/tmp/a2.txt", mode); + let fd = file.fd; + console.println(`file open fd=${fd}`); + const options: WriteOptions = { + offset: 100, + }; + let length = writeSync(fd, new ArrayBuffer(1024), options); + console.println(`write file length=${length}`); + } catch (error) { + console.error("writeSyncTest2 Error!", error); + } + console.println("writeSyncTest2 end"); +} -// function writePromiseTest() { -// console.println("writePromiseTest begin"); -// try { -// let mode = OpenMode.READ_WRITE; -// let file = openSync("/data/local/tmp/a3.txt", mode); -// let fd = file.fd; -// let length = await write(fd, "writePromiseTest"); -// console.println(`writePromiseTest length=${length}`); -// } catch (error) { -// console.error("writePromiseTest Error!", error); -// } -// console.println("writePromiseTest end"); -// } +function writePromiseTest() { + console.println("writePromiseTest begin"); + try { + let mode = 2; + let file = openSync("/data/local/tmp/a3.txt", mode); + let fd = file.fd; + let length = await write(fd, "writePromiseTest"); + console.println(`writePromiseTest length=${length}`); + } catch (error) { + console.error("writePromiseTest Error!", error); + } + console.println("writePromiseTest end"); +} -// function writeCallbackTest() { -// console.println("writeCallbackTest begin"); -// let mode = OpenMode.READ_WRITE; -// let file = openSync("/data/local/tmp/a4.txt", mode); -// let fd = file.fd; -// write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { -// if (data !== undefined) { -// console.println(`writeCallbackTest length=${data}`); -// } else { -// console.error("writeCallbackTest Error!", err); -// } -// }); -// console.println("writeCallbackTest end"); -// } +function writeCallbackTest() { + console.println("writeCallbackTest begin"); + let mode = 2; + let file = openSync("/data/local/tmp/a4.txt", mode); + let fd = file.fd; + write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { + if (data !== undefined) { + console.println(`writeCallbackTest length=${data}`); + } else { + console.error("writeCallbackTest Error!", err); + } + }); + console.println("writeCallbackTest end"); +} function errorHandlerTest1() { console.println("errorHandlerTest1 begin"); @@ -1455,11 +1418,11 @@ function main() { // unlinkSyncTest(); // unlinkPromiseTest(); // unlinkCallbackTest(); - // writeSyncTest1(); - // writeSyncTest2(); - // writePromiseTest(); - // writeCallbackTest(); - errorHandlerTest1(); // 打开文件失败,文件不存在 + writeSyncTest1(); + writeSyncTest2(); + writePromiseTest(); + writeCallbackTest(); + // errorHandlerTest1(); // 打开文件失败,文件不存在 // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise // errorHandlerTest4(); // 异步callback 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 c404e0a6..5dc11365 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 @@ -15,7 +15,7 @@ #include "file_ani.h" -#include +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "fs_file.h" #include "type_converter.h" @@ -45,17 +45,21 @@ ani_string FileAni::GetParent(ani_env *env, [[maybe_unused]] ani_object object) auto fsFile = Unwrap(env, object); if (fsFile == nullptr) { HILOGE("Cannot unwrap fsfile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return {}; } auto ret = fsFile->GetParent(); if (!ret.IsSuccess()) { HILOGE("Cannot get file parent!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return {}; } auto value = ret.GetData().value(); auto [succ, parent] = TypeConverter::ToAniString(env, value); if (!succ) { HILOGE("Cannot convert file parent to ani string!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return {}; } return parent; @@ -72,11 +76,14 @@ void FileAni::Lock(ani_env *env, [[maybe_unused]] ani_object object, ani_object auto fsFile = Unwrap(env, object); if (fsFile == nullptr) { HILOGE("Cannot unwrap fsfile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return; } auto ret = fsFile->Lock(exc); if (!ret.IsSuccess()) { HILOGE("Lock file failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } @@ -92,11 +99,14 @@ void FileAni::TryLock(ani_env *env, [[maybe_unused]] ani_object object, ani_obje auto fsFile = Unwrap(env, object); if (fsFile == nullptr) { HILOGE("Cannot unwrap fsfile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return; } auto ret = fsFile->TryLock(exc); if (!ret.IsSuccess()) { HILOGE("TryLock file failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } @@ -106,11 +116,14 @@ void FileAni::UnLock(ani_env *env, [[maybe_unused]] ani_object object) auto fsFile = Unwrap(env, object); if (fsFile == nullptr) { HILOGE("Cannot unwrap fsfile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return; } auto ret = fsFile->UnLock(); if (!ret.IsSuccess()) { HILOGE("UnLock file failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/move_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/move_ani.cpp index 0b5c597a..3243554f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/move_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/move_ani.cpp @@ -15,6 +15,7 @@ #include "move_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "move_core.h" #include "type_converter.h" @@ -30,21 +31,26 @@ void MoveAni::MoveFileSync( auto [succSrc, srcPath] = TypeConverter::ToUTF8String(env, src); if (!succSrc) { HILOGE("Invalid src"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succDest, destPath] = TypeConverter::ToUTF8String(env, dest); if (!succDest) { HILOGE("Invalid dest"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succMode, modeOp] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = MoveCore::DoMove(srcPath, destPath, modeOp); if (!ret.IsSuccess()) { HILOGE("Move failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp index 249f6447..9744c32d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp @@ -17,6 +17,7 @@ #include #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" #include "write_core.h" @@ -94,6 +95,7 @@ ani_double WriteAni::WriteSync( auto [succOp, op] = ToWriteOptions(env, options); if (!succOp) { HILOGE("Failed to resolve options!"); + ErrorHandler::Throw(env, EINVAL); return -1; } @@ -102,11 +104,14 @@ ani_double WriteAni::WriteSync( auto [succBuf, buffer] = TypeConverter::ToUTF8String(env, stringBuffer); if (!succBuf) { HILOGE("Failed to resolve stringBuffer!"); + ErrorHandler::Throw(env, EINVAL); return -1; } auto ret = WriteCore::DoWrite(static_cast(fd), buffer, op); if (!ret.IsSuccess()) { HILOGE("write buffer failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return -1; } return static_cast(ret.GetData().value()); @@ -117,11 +122,14 @@ ani_double WriteAni::WriteSync( auto [succBuf, buffer] = TypeConverter::ToArrayBuffer(env, arrayBuffer); if (!succBuf) { HILOGE("Failed to resolve arrayBuffer!"); + ErrorHandler::Throw(env, EINVAL); return -1; } auto ret = WriteCore::DoWrite(static_cast(fd), buffer, op); if (!ret.IsSuccess()) { HILOGE("write buffer failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return -1; } return static_cast(ret.GetData().value()); -- Gitee From 7a7f508e456d21f925b1e65446a3b4fc35858397 Mon Sep 17 00:00:00 2001 From: nieben Date: Wed, 19 Mar 2025 09:53:15 +0800 Subject: [PATCH 57/76] throw err Signed-off-by: nieben Change-Id: I4c63ab5024d9a5e882d99388e022730ce8e30c95 --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 6 +++++- .../kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp | 13 +++++++++++++ .../js/src/mod_fs/properties/ani/copy_file_ani.cpp | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) 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 465ad015..9ed1bd89 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 @@ -529,6 +529,8 @@ function stat(file: string | number): Promise { let r = ret as Stat; resolve(r); } + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -546,6 +548,8 @@ function stat(file: string | number, callback: AsyncCallback): void let r = ret as Stat; callback(err, r); } + }).catch((e: BusinessError): void => { + callback(e, new StatInner(0)); }); } @@ -626,7 +630,7 @@ enum LocationType { CLOUD = 2 } -interface Stat { +export interface Stat { ino: bigint; mode: number; uid: number; diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index 23ec937f..ac78c5ed 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -18,6 +18,7 @@ #include #include +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "stat_core.h" #include "type_converter.h" @@ -191,12 +192,15 @@ ani_object StatAni::StatSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, fileInfo] = TypeConverter::ToFileInfo(env, file); if (!succPath) { HILOGE("The first argument requires filepath/fd"); + ErrorHandler::Throw(env, EINVAL); return {}; } auto ret = StatCore::DoStat(fileInfo); if (!ret.IsSuccess()) { HILOGE("DoStat failed!"); + const FsError &err = ret.GetError(); + ErrorHandler::Throw(env, err); return {}; } @@ -206,8 +210,10 @@ ani_object StatAni::StatSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani delete fsStat; fsStat = nullptr; HILOGE("Wrap stat object failed!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return {}; } + return statObject; } @@ -215,6 +221,7 @@ ani_boolean StatAni::IsBlockDevice(ani_env *env, [[maybe_unused]] ani_object obj { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -226,6 +233,7 @@ ani_boolean StatAni::IsCharacterDevice(ani_env *env, [[maybe_unused]] ani_object { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -237,6 +245,7 @@ ani_boolean StatAni::IsDirectory(ani_env *env, [[maybe_unused]] ani_object objec { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -248,6 +257,7 @@ ani_boolean StatAni::IsFIFO(ani_env *env, [[maybe_unused]] ani_object object) { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -259,6 +269,7 @@ ani_boolean StatAni::IsFile(ani_env *env, [[maybe_unused]] ani_object object) { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -270,6 +281,7 @@ ani_boolean StatAni::IsSocket(ani_env *env, [[maybe_unused]] ani_object object) { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } @@ -281,6 +293,7 @@ ani_boolean StatAni::IsSymbolicLink(ani_env *env, [[maybe_unused]] ani_object ob { auto fsStat = Unwrap(env, object); if (fsStat == nullptr) { + ErrorHandler::Throw(env, UNKNOWN_ERR); return ANI_FALSE; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_file_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_file_ani.cpp index 161378a5..51e30f4d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_file_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_file_ani.cpp @@ -16,6 +16,7 @@ #include "copy_file_ani.h" #include "copy_file_core.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -33,18 +34,22 @@ void CopyFileAni::CopyFileSync( auto [succDest, destFile] = TypeConverter::ToFileInfo(env, dest); if (!succSrc || !succDest) { HILOGE("The first/second argument requires filepath/fd"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succMode, optMode] = TypeConverter::ToOptionalInt32(env, mode); if (!succMode) { HILOGE("Failed to convert mode to int32"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = CopyFileCore::DoCopyFile(srcFile, destFile, optMode); if (!ret.IsSuccess()) { HILOGE("DoCopyFile failed!"); + const FsError &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From ba5f5f68909c9b4fe8223b7335049a2291ad146f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 13:26:28 +0000 Subject: [PATCH 58/76] !37 close * test * close --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 158 ++++++++++++++++++ .../src/mod_fs/properties/ani/close_ani.cpp | 4 + 2 files changed, 162 insertions(+) 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 465ad015..754b296b 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 @@ -1380,6 +1380,154 @@ function errorHandlerTest1() { // console.println("errorHandlerTest4 end"); // } +function readTextSyncErr1() { + console.println("readTextSyncErr1 begin"); + try { + let ret = readTextSync("/data/local/tmp/afafes.txt.txt"); + console.println(`readTextSyncErr1 read text a.txt, result = ${ret}`); + + } catch (error) { + console.error("readTextSyncErr1 Error!", error); + } + console.println("readTextSyncErr1 end"); +} +function readTextSyncErr2() { + console.println("readTextSyncErr2 begin"); + let options: ReadTextOptions = { + offset: -1, + } + try { + let ret = readTextSync("/data/local/tmp/a.txt", options); + console.println(`readTextSyncErr2 read text a.txt, result = ${ret}`); + + } catch (error) { + console.error("readTextSyncErr2 Error!", error); + } + console.println("readTextSyncErr2 end"); +} +function readTextErr1() { + console.println("readTextErr1 begin"); + let options: ReadTextOptions = { + offset: -1, + } + try { + let ret = await readText("/data/local/tmp/a.txt", options); + console.println(`readTextErr1 read text a.txt, result = ${ret}`); + } catch (error) { + console.error("readTextErr1 Error!", error); + } + console.println("readTextErr1 end"); +} +function readTextErr2() { + console.println("readTextErr2 begin"); + let options: ReadTextOptions = { + offset: -1, + } + readText("/data/local/tmp/a.txt", options, (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`readTextErr2 read text a.txt, result = ${data}`); + } else { + console.error("readTextErr2 Error!", err); + } + }); + console.println("readTextErr2 end"); +} + + +function listFileSyncErr1() { + console.println("listFileSyncErr1 begin"); + try { + let ret = listFileSync("/data/local/tmp/zx/"); + console.println(`listFileSyncErr1 /data/local/tmp/zx/, result = ${ret}`); + } catch (error) { + console.error("listFileSyncErr1 Error!", error); + } + console.println("listFileSyncErr1 end"); +} +function listFileSyncErr2() { + console.println("listFileSyncErr2 begin"); + let options: ListFileOptions = { + listNum: -1, + } + try { + let ret = listFileSync("/data/local/tmp", options); + console.println(`listFileSyncErr2 /data/local/tmp, result = ${ret}`); + + } catch (error) { + console.error("listFileSyncErr2 Error!", error); + } + console.println("listFileSyncErr2 end"); +} +function listFileErr1() { + console.println("listFileErr1 begin"); + let options: ListFileOptions = { + listNum: -1, + } + try { + let ret = await listFile("/data/local/tmp/", options); + console.println(`listFileErr1 /data/local/tmp, result = ${ret}`); + } catch (error) { + console.error("listFileErr1 Error!", error); + } + console.println("listFileErr1 end"); +} +function listFileErr2() { + console.println("listFileErr2 begin"); + let options: ListFileOptions = { + listNum: -1, + } + listFile("/data/local/tmp", options, (err: BusinessError, data?: string[]) => { + if (err.code == 0 && data !== undefined) { + console.println(`listFileErr2 /data/local/tmp, result = ${data}`); + } else { + console.error("listFileErr2 Error!", err); + } + }); + console.println("listFileErr2 end"); +} + +function closeSyncErr1() { + console.println("closeSyncErr1 begin"); + try { + closeSync(-176); + console.println(`closeSyncErr1 -176`); + } catch (error) { + console.error("closeSyncErr1 Error!", error); + } + console.println("closeSyncErr1 end"); +} +function closeSyncErr2() { + console.println("closeSyncErr2 begin"); + try { + closeSync(65536); + console.println(`closeSyncErr2 65536`); + } catch (error) { + console.error("closeSyncErr2 Error!", error); + } + console.println("closeSyncErr2 end"); +} +function closeErr1() { + console.println("listFileErr1 begin"); + try { + let ret = await close(-1); + console.println(`closeErr1 -1, result = ${ret}`); + } catch (error) { + console.error("closeErr1 Error!", error); + } + console.println("closeErr1 end"); +} +function closeErr2() { + console.println("closeErr2 begin"); + close(-1, (err: BusinessError, data?: undefined) => { + if (err.code == 0) { + console.println(`closeErr2 Succ`); + } else { + console.error("closeErr2 Error!", err); + } + }); + console.println("closeErr2 end"); +} + function main() { console.println("---------- hello ani --------------"); // accessSyncTest1(); @@ -1426,5 +1574,15 @@ function main() { // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise // errorHandlerTest4(); // 异步callback + readTextErr1(); + readTextErr2(); + listFileSyncErr1() + listFileSyncErr2(); + listFileErr1(); + listFileErr1(); + closeSyncErr1(); + closeSyncErr2() + closeErr1(); + closeErr2(); console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 750ec0cb..f49ccf2f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -18,6 +18,7 @@ #include #include "close_core.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -68,12 +69,15 @@ void CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_obj auto [succ, fd] = ParseFd(env, obj); if (!succ) { HILOGE("Parse fd argument failed"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = CloseCore::DoClose(fd); if (!ret.IsSuccess()) { HILOGE("Close %d failed", fd); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From 836bfa057f6bab46cad9f2a9ea149f093bccf29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 13:27:35 +0000 Subject: [PATCH 59/76] =?UTF-8?q?!38=20readtext=20*=20Merge=20branch=20'an?= =?UTF-8?q?i=5Ferror=5Fhandler'=20of=20gitee.com:provii/OpenHarmony=5Ffile?= =?UTF-8?q?m=E2=80=A6=20*=20readtext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 52 +++++++------------ .../mod_fs/properties/ani/read_text_ani.cpp | 6 +++ 2 files changed, 24 insertions(+), 34 deletions(-) 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 754b296b..09341110 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 @@ -386,26 +386,18 @@ function readText(filePath: string, options?: ReadTextOptions): Promise if (options === undefined) { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); }); } else { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); }); } }); @@ -414,30 +406,22 @@ function readText(filePath: string, options?: ReadTextOptions): Promise function readText(filePath: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); promise.then((ret: NullishType): void => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, ''); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } + let e = new BusinessError(0, ""); + let r = ret as string; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, ""); }); } function readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, ''); - } else { - err.code = 0; - let r = ret as string; - callback(err, r); - } + let e = new BusinessError(0, ""); + let r = ret as string; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, ""); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp index 2c1c64d8..bd7dbd55 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp @@ -17,6 +17,7 @@ #include #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "read_text_core.h" #include "type_converter.h" @@ -70,18 +71,22 @@ ani_string ReadTextAni::ReadTextSync( auto [succOpt, options] = ToReadTextOptions(env, obj); if (!succOpt) { HILOGE("Ivalid options"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succPath, path] = TypeConverter::ToUTF8String(env, filePath); if (!succPath) { HILOGE("Invalid Path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto ret = ReadTextCore::DoReadText(path, options); if (!ret.IsSuccess()) { HILOGE("DoReadText failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } @@ -90,6 +95,7 @@ ani_string ReadTextAni::ReadTextSync( auto [succ, result] = TypeConverter::ToAniString(env, res); if (!succ) { HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } return result; -- Gitee From ff3a5f1f21e4879da69e2416225cce821e9914d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 13:28:34 +0000 Subject: [PATCH 60/76] =?UTF-8?q?!39=20listfile=20*=20Merge=20branch=20'an?= =?UTF-8?q?i=5Ferror=5Fhandler'=20of=20gitee.com:provii/OpenHarmony=5Ffile?= =?UTF-8?q?m=E2=80=A6=20*=20listfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 60 +++++++------------ .../mod_fs/properties/ani/listfile_ani.cpp | 6 ++ 2 files changed, 28 insertions(+), 38 deletions(-) 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 09341110..b06c2bc8 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 @@ -433,27 +433,19 @@ function listFile(path: string, options?: ListFileOptions): Promise { return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { if (options === undefined) { let promise = taskpool.execute(FileIoImpl.listFileSync, path); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } + promise.then((ret: NullishType): void => { + let r = ret as string[]; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); }); } else { let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string[]; - resolve(r); - } + promise.then((ret: NullishType): void => { + let r = ret as string[]; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); }); } }); @@ -461,31 +453,23 @@ function listFile(path: string, options?: ListFileOptions): Promise { function listFile(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path); - promise.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, []); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + let r = ret as string[]; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } function listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); - promise.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, []); - } else { - err.code = 0; - let r = ret as string[]; - callback(err, r); - } + promise.then((ret: NullishType): void => { + let e = new BusinessError(0, ""); + let r = ret as string[]; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, undefined); }); } 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 07bf046e..d19f25ae 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 @@ -15,6 +15,7 @@ #include "listfile_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "listfile_core.h" #include "type_converter.h" @@ -204,18 +205,22 @@ ani_array_ref ListFileAni::ListFileSync(ani_env *env, [[maybe_unused]] ani_class auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succOpt, opt] = ParseArgs(env, obj); if (!succOpt) { HILOGE("Invalid options Arguments"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto ret = ListFileCore::DoListFile(srcPath, opt); if (!ret.IsSuccess()) { HILOGE("DoListFile failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } @@ -224,6 +229,7 @@ ani_array_ref ListFileAni::ListFileSync(ani_env *env, [[maybe_unused]] ani_class auto [succ, result] = TypeConverter::ToAniStringList(env, strArray, fileList.size()); if (!succ) { HILOGE("list file result value to ani_string list failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } return result; -- Gitee From 7b1931253b3c381b22e496a00a6d0c7eaa134fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 13:32:48 +0000 Subject: [PATCH 61/76] !40 hash * hash --- interfaces/kits/js/BUILD.gn | 1 + .../js/src/mod_hash/ani/error_handler.cpp | 28 +++++++++++++++ .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 34 +++++++++---------- .../kits/js/src/mod_hash/ani/hash_ani.cpp | 6 ++++ 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 interfaces/kits/js/src/mod_hash/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220b..ca4a12ec 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -786,6 +786,7 @@ ohos_shared_library("ani_hash_class") { "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/error_handler.cpp", "src/mod_hash/ani/hash_ani.cpp", "src/mod_hash/hash_core.cpp", ] diff --git a/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp b/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp new file mode 100644 index 00000000..0d14637f --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp @@ -0,0 +1,28 @@ +/* + * 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 "error_handler.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI { + +ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) +{ + const char *className = "L@ohos/file/hash/BusinessError;"; + const char *name = "BusinessError"; + return Throw(env, className, name, code, errMsg); +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ 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 286d1815..f93f258d 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 @@ -18,6 +18,13 @@ export class BusinessError { message: string; code: number; data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; + } + constructor(code: number, msg: string, data?: T) { this.name = 'BusinessError'; this.code = code; @@ -33,14 +40,11 @@ export type AsyncCallback = (err: BusinessError, data?: T) => vo function hash(path: string, algorithm: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - reject(err); - } else { - let r = ret as string; - resolve(r); - } + promise.then((ret: NullishType): void => { + let res = ret as string; + resolve(res); + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -48,15 +52,11 @@ function hash(path: string, algorithm: string): Promise { function hash(path: string, algorithm: string, callback: AsyncCallback): void { let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as string; - callback(err, r); - } + let e = new BusinessError(0, ""); + let res = ret as string; + callback(e, res); + }).catch((e: BusinessError): void => { + callback(e, ""); }); } diff --git a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp index c8bffc53..0728869a 100644 --- a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp @@ -15,6 +15,7 @@ #include "hash_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "hash_core.h" #include "type_converter.h" @@ -32,18 +33,22 @@ ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succAlg, algType] = TypeConverter::ToUTF8String(env, algorithm); if (!succAlg) { HILOGE("Invalid algorithm"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto ret = HashCore::DoHash(srcPath, algType); if (!ret.IsSuccess()) { HILOGE("DoHash failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } @@ -51,6 +56,7 @@ ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succ, result] = TypeConverter::ToAniString(env, res); if (!succ) { HILOGE("Convert hash value to ani_string failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } return result; -- Gitee From 1c7f550c62343164ee96459e0c40fb34fd13116b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 21:33:51 +0800 Subject: [PATCH 62/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If289d3fda1822ae594379334ce4eef9870509efe --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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 b06c2bc8..dc2f89bb 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 @@ -267,7 +267,7 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); promise.then((ret: NullishType): void => { - let e = new BusinessError(); + let e = new BusinessError(); e.code = 0; let result = ret as number; callback(e, result); @@ -279,7 +279,7 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer); promise.then((ret: NullishType): void => { - let e = new BusinessError(); + let e = new BusinessError(); e.code = 0; let result = ret as number; callback(e, result); @@ -406,7 +406,8 @@ function readText(filePath: string, options?: ReadTextOptions): Promise function readText(filePath: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let r = ret as string; callback(e, r); }).catch((e: BusinessError): void => { @@ -417,7 +418,8 @@ function readText(filePath: string, callback: AsyncCallback): void { function readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let r = ret as string; callback(e, r); }).catch((e: BusinessError): void => { @@ -454,22 +456,24 @@ function listFile(path: string, options?: ListFileOptions): Promise { function listFile(path: string, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let r = ret as string[]; callback(e, r); }).catch((e: BusinessError): void => { - callback(e, undefined); + callback(e, []); }); } function listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let r = ret as string[]; callback(e, r); }).catch((e: BusinessError): void => { - callback(e, undefined); + callback(e, []); }); } -- Gitee From 1fda2db0dabd07555d10f6585aac951fa28d0c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 13:42:58 +0000 Subject: [PATCH 63/76] !41 securitylabel * test * securitylabel --- interfaces/kits/js/BUILD.gn | 1 + .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 52 ++++++++++++++ .../mod_securitylabel/ani/error_handler.cpp | 28 ++++++++ .../ani/ets/@ohos.file.securityLabel.ets | 70 ++++++++++++++++++- .../ani/securitylabel_ani.cpp | 5 ++ 5 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index ca4a12ec..46c90d3c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -856,6 +856,7 @@ ohos_shared_library("ani_securitylabel_class") { "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_securitylabel/ani/bind_function_class.cpp", + "src/mod_securitylabel/ani/error_handler.cpp", "src/mod_securitylabel/ani/securitylabel_ani.cpp", "src/mod_securitylabel/securitylabel_core.cpp", ] 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 f93f258d..cf2ad05b 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 @@ -67,4 +67,56 @@ class hashImpl { } static native hashSync(path: string, algorithm: string): string; +} + +function errorHandlerTestSucc() { + console.println("errorHandlerTestSucc begin"); + try { + let ret = await hash("/data/local/tmp/a.txt", "sha256"); + console.println(`errorHandlerTestSucc hash: ${ret}`); + } catch (error) { + console.error("errorHandlerTestSucc Error!", error); + } + + hash("/data/local/tmp/a.txt", "sha256", (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`errorHandlerTestSucc hash success: ${data}`); + } else { + console.error("errorHandlerTestSucc hash Error!", err); + } + }); + console.println("errorHandlerTestSucc end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let ret = await hash("/data/local/tmp/a.txt", "sha128"); + console.println(`errorHandlerTest3 hash: ${ret}`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + hash("/data/local/tmp/a.txt", "sha128", (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`errorHandlerTest4 hash success: ${data}`); + } else { + console.error("errorHandlerTest4 hash Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + + errorHandlerTestSucc(); + errorHandlerTest3(); + errorHandlerTest4(); + + console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp new file mode 100644 index 00000000..c302fc92 --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp @@ -0,0 +1,28 @@ +/* + * 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 "error_handler.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI { + +ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) +{ + const char *className = "L@ohos/file/securityLabel/BusinessError;"; + const char *name = "BusinessError"; + return Throw(env, className, name, code, errMsg); +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 1458f300..1550fb4b 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -18,6 +18,13 @@ export class BusinessError { message: string; code: number; data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; + } + constructor(code: number, msg: string, data?: T) { this.name = 'BusinessError'; this.code = code; @@ -37,7 +44,7 @@ function setSecurityLabel(path: string, type: DataLevel): Promise { let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: BusinessError): void => { reject(e); }); }); @@ -46,9 +53,9 @@ function setSecurityLabel(path: string, type: DataLevel): Promise { function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(0, ""); callback(e, undefined); - }).catch((e: BusinessError): void => { + }).catch((e: BusinessError): void => { callback(e, undefined); }); } @@ -64,4 +71,61 @@ class securitylabelImpl { } static native setSecurityLabelSync(path: string, type: DataLevel): void; +} + +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + setSecurityLabelSync("/data/local/tmp/uihuihuisu.txt", "s2"); + console.println(`errorHandlerTest1 setSecurityLabelSync to s1`); + } catch (error) { + console.error("errorHandlerTest1 Error!", error); + } + console.println("errorHandlerTest1 end"); +} + +function errorHandlerTest2() { + console.println("errorHandlerTest2 begin"); + try { + setSecurityLabelSync("/data/local/tmp/a.txt", "s0"); + console.println(`errorHandlerTest2 setSecurityLabelSync to s0`); + } catch (error) { + console.error("errorHandlerTest2 Error!", error); + } + console.println("errorHandlerTest2 end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let ret = await setSecurityLabel("/data/local/tmp/a.txt", "s0"); + console.println(`errorHandlerTest3 setSecurityLabelSync to s2`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + setSecurityLabel("/data/local/tmp/a.txt", "s0", (err: BusinessError, data?: undefined) => { + if (err.code == 0) { + console.println(`errorHandlerTest4 setSecurityLabel success`); + } else { + console.error("errorHandlerTest4 setSecurityLabel Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + + errorHandlerTest1(); // 打开文件失败,文件不存在 + setSecurityLabelSync("/data/local/tmp/a.txt", "s4"); + errorHandlerTest2(); // 参数校验失败 + errorHandlerTest3(); // 异步Promise + errorHandlerTest4(); // 异步callback + + console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index 7c78437d..6c44deb6 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -15,6 +15,7 @@ #include "securitylabel_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "securitylabel_core.h" #include "type_converter.h" @@ -34,18 +35,22 @@ void SecurityLabelAni::SetSecurityLabelSync( auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succLevel, dataLevel] = TypeConverter::ToUTF8String(env, level); if (!succLevel) { HILOGE("Invalid dataLevel"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = DoSetSecurityLabel(srcPath, dataLevel); if (!ret.IsSuccess()) { HILOGE("Set securitylabel failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From 480b7ae73b705a20caa97b2d49f0e0f2a8608f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 21:43:23 +0800 Subject: [PATCH 64/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id963d149370be7b1c8b130734f4c274bd49bf4ce --- interfaces/kits/js/BUILD.gn | 1 - .../js/src/mod_hash/ani/error_handler.cpp | 28 ---------------- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 33 +++++-------------- 3 files changed, 8 insertions(+), 54 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_hash/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 46c90d3c..2d506209 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -786,7 +786,6 @@ ohos_shared_library("ani_hash_class") { "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/error_handler.cpp", "src/mod_hash/ani/hash_ani.cpp", "src/mod_hash/hash_core.cpp", ] diff --git a/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp b/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp deleted file mode 100644 index 0d14637f..00000000 --- a/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 "error_handler.h" - -namespace OHOS::FileManagement::ModuleFileIO::ANI { - -ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) -{ - const char *className = "L@ohos/file/hash/BusinessError;"; - const char *name = "BusinessError"; - return Throw(env, className, name, code, errMsg); -} - - -} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ 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 cf2ad05b..5ab72429 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 @@ -13,33 +13,15 @@ * limitations under the License. */ -export class BusinessError { - name: string; - message: string; - code: number; - data?: T; +import { BusinessError, AsyncCallback } from '@ohos.base'; - constructor() { - this.name = 'BusinessError'; - this.message = ''; - this.code = 0; - } - - constructor(code: number, msg: string, data?: T) { - this.name = 'BusinessError'; - this.code = code; - this.message = msg; - if (data !== undefined) { - this.data = data; - } - } +function hashSync(path: string, algorithm: string): string { + return HashImpl.hashSync(path, algorithm); } -export type AsyncCallback = (err: BusinessError, data?: T) => void; - function hash(path: string, algorithm: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); + let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); promise.then((ret: NullishType): void => { let res = ret as string; resolve(res); @@ -50,9 +32,10 @@ function hash(path: string, algorithm: string): Promise { } function hash(path: string, algorithm: string, callback: AsyncCallback): void { - let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); + let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); promise.then((ret: NullishType) => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; let res = ret as string; callback(e, res); }).catch((e: BusinessError): void => { @@ -60,7 +43,7 @@ function hash(path: string, algorithm: string, callback: AsyncCallback Date: Wed, 19 Mar 2025 21:51:12 +0800 Subject: [PATCH 65/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1a500f11d79a679d7c200c7f748e6aa125b197ff --- interfaces/kits/js/BUILD.gn | 1 - .../mod_securitylabel/ani/error_handler.cpp | 28 ------------------- .../ani/file_securitylabel_class.ets | 9 ++---- 3 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 2d506209..9334220b 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -855,7 +855,6 @@ ohos_shared_library("ani_securitylabel_class") { "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_securitylabel/ani/bind_function_class.cpp", - "src/mod_securitylabel/ani/error_handler.cpp", "src/mod_securitylabel/ani/securitylabel_ani.cpp", "src/mod_securitylabel/securitylabel_core.cpp", ] diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp deleted file mode 100644 index c302fc92..00000000 --- a/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 "error_handler.h" - -namespace OHOS::FileManagement::ModuleFileIO::ANI { - -ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) -{ - const char *className = "L@ohos/file/securityLabel/BusinessError;"; - const char *name = "BusinessError"; - return Throw(env, className, name, code, errMsg); -} - - -} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets b/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets index 3e851ba6..43f04463 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets @@ -13,14 +13,9 @@ * limitations under the License. */ -export default securitylabel; - -export class BusinessError { - code: number = 0; - data?: T; -} +import { BusinessError, AsyncCallback } from '@ohos.base'; -export type AsyncCallback = (err: BusinessError, data?: T) => void; +export default securitylabel; type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; -- Gitee From b611b1a3bb66c5f44c92ead8f5a7bebadefa0f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Wed, 19 Mar 2025 22:15:35 +0800 Subject: [PATCH 66/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic370e73db667339803f66bb285c57aec1cf6e15f --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 947 +----------------- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 52 - .../js/src/mod_hash/ani/file_hash_class.ets | 63 -- .../ani/ets/@ohos.file.securityLabel.ets | 92 +- .../ani/file_securitylabel_class.ets | 60 -- 5 files changed, 45 insertions(+), 1169 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_hash/ani/file_hash_class.ets delete mode 100644 interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets 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 4324d009..35df4b3d 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 @@ -22,9 +22,9 @@ function access(path: string, mode?: AccessModeType): Promise { }, path); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let result = ret as boolean; resolve(result); @@ -36,9 +36,9 @@ function access(path: string, mode?: AccessModeType): Promise { }, path, mode); promise.then((ret: NullishType) => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let result = ret as boolean; resolve(result); @@ -53,14 +53,14 @@ function access(path: string, callback: AsyncCallback): void { return FileIoImpl.doAccessSync(path); }, path); promise.then((ret: NullishType): void => { - let err = new BusinessError(); + let e = new BusinessError(); if (ret === null || ret === undefined) { - err.code = -1; - callback(err, false); + e.code = -1; + callback(e, false); } else { - err.code = 0; + e.code = 0; let result = ret as boolean; - callback(err, result); + callback(e, result); } }); } @@ -70,9 +70,9 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi let promise = taskpool.execute(FileIoImpl.doAccessSync, path, mode, flag); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let result = ret as boolean; resolve(result); @@ -89,7 +89,6 @@ function accessSync(path: string, mode: AccessModeType, flag: AccessFlagType): b return FileIoImpl.doAccessSync(path, mode, flag); } - function close(file: number | File): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { let promise = taskpool.execute((file: number | File): undefined => FileIoImpl.closeSync(file), file); @@ -112,7 +111,6 @@ function close(file: number | File, callback: AsyncCallback): void { }); } - function closeSync(file: number | File): void { return FileIoImpl.closeSync(file) } @@ -293,9 +291,9 @@ function read(fd: number, buffer: ArrayBuffer): Promise { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let r = ret as number; resolve(r); @@ -309,9 +307,9 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let r = ret as number; resolve(r); @@ -323,14 +321,14 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise): void { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); promise.then((ret: NullishType): void => { - let err = new BusinessError(); + let e = new BusinessError(); if (ret === null || ret === undefined) { - err.code = -1; - callback(err, 0); + e.code = -1; + callback(e, 0); } else { - err.code = 0; + e.code = 0; let r = ret as number; - callback(err, r); + callback(e, r); } }); } @@ -338,14 +336,14 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); promise.then((ret: NullishType): void => { - let err = new BusinessError(); + let e = new BusinessError(); if (ret === null || ret === undefined) { - err.code = -1; - callback(err, 0); + e.code = -1; + callback(e, 0); } else { - err.code = 0; + e.code = 0; let r = ret as number; - callback(err, r); + callback(e, r); } }); } @@ -494,9 +492,9 @@ function stat(file: string | number): Promise { let promise = taskpool.execute(FileIoImpl.statSync, file); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); + let e = new BusinessError(); + e.code = -1; + reject(e); } else { let r = ret as Stat; resolve(r); @@ -510,15 +508,15 @@ function stat(file: string | number): Promise { function stat(file: string | number, callback: AsyncCallback): void { let p = taskpool.execute(FileIoImpl.statSync, file); p.then((ret: NullishType): void => { - let err = new BusinessError(); + let e = new BusinessError(); if (ret === null || ret === undefined) { - err.code = -1; + e.code = -1; let stat: Stat = new StatInner(0); - callback(err, stat); + callback(e, stat); } else { - err.code = 0; + e.code = 0; let r = ret as Stat; - callback(err, r); + callback(e, r); } }).catch((e: BusinessError): void => { callback(e, new StatInner(0)); @@ -693,872 +691,3 @@ class FileIoImpl { static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; } - -// function accessSyncTest1() { -// console.println("accessSyncTest1 begin"); -// try { -// let ret = accessSync("/data/local/tmp/a.txt"); -// console.println(`accessSync result : ${ret}`); -// } catch (error) { -// console.error("accessSyncTest1 Error!", error); -// } -// console.println("accessSyncTest1 end"); -// } - -// function closeSyncTest1() { -// console.println("closeSyncTest1 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt"); -// console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// closeSync(file.fd); -// console.println(`close file by fd, fd=${file.fd}`); -// } catch (error) { -// console.error("closeSyncTest1 Error!", error); -// } -// console.println("closeSyncTest1 end"); -// } - -// function closeSyncTest2() { -// console.println("closeSyncTest2 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// closeSync(file); -// console.println(`close file by file, fd=${file.fd}`); -// } catch (error) { -// console.error("closeSyncTest2 Error!", error); -// } -// console.println("closeSyncTest2 end"); -// } - -// function closeSyncTest3() { -// console.println("closeSyncTest3 begin"); -// try { -// closeSync(-1); -// console.println(`close file by invalid fd: -1`); -// } catch (error) { -// console.error("closeSyncTest3 Error!", error); -// } -// console.println("closeSyncTest3 end"); -// } - -// function closePromiseTest() { -// console.println("closePromiseTest begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// close(file.fd).then((result: undefined): void => { -// console.println(`closePromiseTest close success`); -// }).catch((e: Object): void => { -// console.error("async closePromiseTest Error!!!", e); -// }); -// } catch (error) { -// console.error("closePromiseTest Error:", error); -// } -// console.println("closePromiseTest end"); -// } - -// function closePromiseTest2() { -// console.println("closePromiseTest2 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// await close(file.fd); -// console.println(`closePromiseTest2 close success`); -// } catch (error) { -// console.error("closePromiseTest2 Error:", error); -// } -// console.println("closePromiseTest2 end"); -// } - -// function closeCallbackTest() { -// console.println("closeCallbackTest begin"); -// let file = openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file with mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// close(file, (err: BusinessError) => { -// if (err.code === 0) { -// console.log("closeCallbackTest: close success!"); -// } else { -// console.error("closeCallbackTest: close Error:", err); -// } -// }); -// console.println("closeCallbackTest end"); -// } - -// function copyFileTest() { -// console.println("copyFileTest begin"); -// try { -// copyFileSync("/data/local/tmp/a.txt", "/data/local/tmp/b.txt"); -// } catch (error) { -// console.error("copyFileTest Error!", error); -// } -// console.println("copyFileTest end"); -// } - -function fileTest() { - console.println("fileTest begin"); - let file = openSync("/data/local/tmp/a.txt"); - let fd = file.fd; - console.println(`open file, fd=${fd}`); - let parent = file.getParent(); - console.println(`file parent=${parent}`); - file.lock(); - console.println(`file lock completed`); - file.unlock(); - console.println(`file unlock completed`); - file.tryLock(true); - console.println(`file tryLock completed`); - file.unlock(); - console.println(`file unlock completed`); - console.println("fileTest end"); -} - -// function listFileSyncTest1() { -// console.println("listFileSyncTest1 begin"); -// try { -// let files = listFileSync("/data/local/tmp/"); -// for (const file of files) { -// console.println(file); -// } -// } catch (error) { -// console.error("listFileSyncTest1 Error!", error); -// } -// console.println("listFileSyncTest1 end"); -// } - -// function listFileSyncTest2() { -// console.println("listFileSyncTest2 begin"); -// try { -// let options: ListFileOptions = { -// recursion: true -// }; -// let files = listFileSync("/data/", options); -// for (const file of files) { -// console.println(file); -// } -// } catch (error) { -// console.error("listFileSyncTest2 Error!", error); -// } -// console.println("listFileSyncTest2 end"); -// } - -// function mkdirSyncTest1() { -// console.println("mkdirSyncTest1 begin") -// try { -// mkdirSync("/data/local/tmp/dir01"); -// console.println(`mkdirSyncTest1 success`); -// } catch (error) { -// console.error("mkdirSyncTest1 Error!", error); -// } -// console.println("mkdirSyncTest1 end"); -// } - -// function mkdirSyncTest2() { -// console.println("mkdirSyncTest2 begin") -// try { -// mkdirSync("/data/local/tmp/dir02/sub01", true); -// console.println(`mkdirSyncTest2 success`); -// } catch (error) { -// console.error("mkdirSyncTest2 Error!", error); -// } -// console.println("mkdirSyncTest2 end"); -// } - -// function mkdirSyncTest3() { -// console.println("mkdirSyncTest3 begin") -// try { -// mkdirSync("/data/local/tmp/dir03/sub01", false); -// console.println(`mkdirSyncTest3 success`); -// } catch (error) { -// console.error("mkdirSyncTest3 Error!", error); -// } -// console.println("mkdirSyncTest3 end"); -// } - -// function mkdirPromiseTest() { -// console.println("mkdirPromiseTest begin") -// try { -// await mkdir("/data/local/tmp/dir04/sub01", true); -// console.println(`mkdirPromiseTest success`); -// } catch (error) { -// console.error("mkdirPromiseTest Error!", error); -// } -// console.println("mkdirPromiseTest end"); -// } - -// function mkdirCallbackTest() { -// console.println("mkdirCallbackTest begin") -// mkdir("/data/local/tmp/dir05/sub01", true, (err: BusinessError) => { -// if (err.code === 0) { -// console.println("mkdirCallbackTest success"); -// } else { -// console.println("mkdirCallbackTest failed!"); -// } -// }); -// console.println("mkdirCallbackTest end"); -// } - -function moveSyncTest() { - console.println("moveSyncTest begin") - try { - moveFileSync("/data/local/tmp/a.txt", "/data/local/tmp/a1.txt", 1) - } catch (error) { - console.error("moveSyncTest Error!", error); - } - console.println("moveSyncTest end") -} - -function openSyncTest() { - console.println("openSyncTest begin"); - try { - // 不指定mode - let file = openSync("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - // 以可读可写方式打开 - let mode = 2; - file = openSync("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openSyncTest Error!", error); - } - console.println("openSyncTest end"); -} - -function openPromiseTest() { - console.println("openPromiseTest begin"); - try { - // 不指定mode - let file = await open("/data/local/tmp/a.txt"); - console.println(`open file without mode, fd=${file.fd}, path=${file.path}, name=${file.name}`); - // 以可读可写方式打开 - let mode = 2; - file = await open("/data/local/tmp/a.txt", mode); - console.println(`open file with mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error) { - console.error("openPromiseTest Error!", error); - } - console.println("openPromiseTest end"); -} - -function openCallbackTest() { - console.println("openCallbackTest begin"); - // 不指定mode - open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { - if (data !== undefined) { - console.println(`open file without mode, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open failed!", err); - } - }); - // 以可读可写方式打开 - let mode = 2; - open("/data/local/tmp/a.txt", mode, (err: BusinessError, data?: File) => { - if (data !== undefined) { - console.println(`open file with mode=${mode}, fd=${data.fd}, path=${data.path}, name=${data.name}`); - } else { - console.error("open failed!", err); - } - }); -} - -// function readSyncTest1() { -// console.println("readSyncTest1 begin") -// try { -// let file = openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let length = readSync(fd, buffer); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest1 Error!", error); -// } -// console.println("readSyncTest1 end") -// } - -// function readSyncTest2() { -// console.println("readSyncTest2 begin") -// try { -// let file = openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let options: ReadOptions = { -// length: 5, -// } -// let length = readSync(fd, buffer, options); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest2 Error!", error); -// } -// console.println("readSyncTest2 end") -// } - -// function readSyncTest3() { -// console.println("readSyncTest3 begin") -// try { -// let file = openSync("/data/local/tmp/a.txt") -// let fd = file.fd; -// console.println(`open file, fd=${fd}`) -// let buffer = new ArrayBuffer(100); -// let options: ReadOptions = { -// offset: 3, -// length: 5, -// } -// let length = readSync(fd, buffer, options); -// console.println(`read length: ${length}`) -// let len = length as int; -// let content = ArrayBuffer.stringify(buffer, "utf-8", 0, len); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readSyncTest3 Error!", error); -// } -// console.println("readSyncTest3 end") -// } - -// function readTextSyncTest1() { -// console.println("readTextSyncTest1 begin") -// try { -// let content = readTextSync("/data/local/tmp/a.txt"); -// console.println("-----------------------------------------------") -// console.println(content) -// console.println("-----------------------------------------------") -// } catch (error) { -// console.error("readTextSyncTest1 Error!", error); -// } -// console.println("readTextSyncTest1 end") -// } - -// function readTextSyncTest2() { -// console.println("readTextSyncTest2 begin"); -// try { -// let options: ReadTextOptions = { -// length: 5, -// } -// let content = readTextSync("/data/local/tmp/a.txt", options); -// console.println("-----------------------------------------------"); -// console.println(content); -// console.println("-----------------------------------------------"); -// } catch (error) { -// console.error("readTextSyncTest2 Error!", error); -// } -// console.println("readTextSyncTest2 end"); -// } - -// function readTextSyncTest3() { -// console.println("readTextSyncTest3 begin") -// try { -// let options: ReadTextOptions = { -// offset: 3, -// length: 5, -// } -// let content = readTextSync("/data/local/tmp/a.txt", options); -// console.println("-----------------------------------------------"); -// console.println(content); -// console.println("-----------------------------------------------"); -// } catch (error) { -// console.error("readTextSyncTest3 Error!", error); -// } -// console.println("readTextSyncTest3 end"); -// } - -// function rmdirSyncTest() { -// console.println("rmdirSyncTest begin"); -// try { -// rmdirSync("/data/local/tmp/dir01"); -// } catch (error) { -// console.error("rmdirSyncTest Error!", error); -// } -// console.println("rmdirSyncTest end"); -// } - -// function printStatInfo(stat?: Stat) { -// if (stat === undefined) { -// console.error("stat is null or undefined!"); -// return; -// } -// console.info("stat, ino is " + stat.ino); -// console.info("stat, mode is " + stat.mode); -// console.info("stat, uid is " + stat.uid); -// console.info("stat, gid is " + stat.gid); -// console.info("stat, size is " + stat.size); -// console.info("stat, atime is " + stat.atime); -// console.info("stat, mtime is " + stat.mtime); -// console.info("stat, ctime is " + stat.ctime); -// console.info("stat, atimeNs is " + stat.atimeNs); -// console.info("stat, mtimeNs is " + stat.mtimeNs); -// console.info("stat, ctimeNs is " + stat.ctimeNs); -// if (stat.location !== undefined) { -// console.info("stat, location is " + stat.location); -// } -// console.info("stat, isBlockDevice is " + stat.isBlockDevice()); -// console.info("stat, isCharacterDevice is " + stat.isCharacterDevice()); -// console.info("stat, isDirectory is " + stat.isDirectory()); -// console.info("stat, isFIFO is " + stat.isFIFO()); -// console.info("stat, isFile is " + stat.isFile()); -// console.info("stat, isSocket is " + stat.isSocket()); -// console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); -// } - -// function statSyncTest1() { -// console.println("statSyncTest1 begin"); -// try { -// let stat = statSync("/data/local/tmp/a.txt"); -// printStatInfo(stat); -// } catch (error) { -// console.error("statSyncTest1 Error!", error); -// } -// console.println("statSyncTest1 end"); -// } - -// function statSyncTest2() { -// console.println("statSyncTest2 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt"); -// console.println(`open file fd: ${file.fd}`); -// let stat = statSync(file.fd); -// printStatInfo(stat); -// } catch (error) { -// console.error("statSyncTest2 Error!", error); -// } -// console.println("statSyncTest2 end"); -// } - -// function statPromiseTest() { -// console.println("statPromiseTest start"); -// try { -// let path = "/data/local/tmp/a.txt" -// let file = openSync(path); -// console.println(`open file fd: ${file.fd}`); -// stat(path).then((result: Stat): void => { -// console.log("statPromiseTest success"); -// printStatInfo(result); -// }); -// } catch (error) { -// console.error("statPromiseTest Error!", error); -// } -// console.println("statPromiseTest end"); -// } - -// function statCallbackTest() { -// console.println("statCallbackTest start"); -// let path = "/data/local/tmp/a.txt" -// stat(path, (err: BusinessError, data?: Stat) => { -// if (data === undefined || data === null) { -// console.error("Callback: Error stat:", err); -// } else { -// console.log("Callback stat success"); -// printStatInfo(data); -// } -// }); -// console.println("statCallbackTest end"); -// } - -// function truncateSyncTest1() { -// console.println("truncateSyncTest1 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt", 2); -// console.println(`open file, fd=${file.fd}`); -// truncateSync(file.fd, 4); -// } catch (error) { -// console.error("truncateSyncTest1 Error!", error); -// } -// console.println("truncateSyncTest1 end"); -// } - -// function truncateSyncTest2() { -// console.println("truncateSyncTest2 begin"); -// try { -// truncateSync("/data/local/tmp/b.txt", 4); -// } catch (error) { -// console.error("truncateSyncTest2 Error!", error); -// } -// console.println("truncateSyncTest2 end"); -// } - -// function unlinkSyncTest() { -// console.println("unlinkSyncTest begin"); -// try { -// unlinkSync("/data/local/tmp/a1.txt"); -// console.println(`unlink result`); - -// } catch (error) { -// console.error("unlinkSyncTest Error!", error); -// } -// console.println("unlinkSyncTest end"); -// } - -// function unlinkPromiseTest() { -// console.println("unlinkPromiseTest begin"); -// try { -// await unlink("/data/local/tmp/a2.txt"); -// console.println(`unlinkPromiseTest success`); -// } catch (error) { -// console.error("unlinkPromiseTest Error!", error); -// } -// console.println("unlinkPromiseTest end"); -// } - -// function unlinkCallbackTest() { -// console.println("unlinkCallbackTest begin"); -// unlink("/data/local/tmp/a3.txt", (err: BusinessError) => { -// if (err.code === 0) { -// console.error("unlinkCallbackTest success"); -// } else { -// console.error("unlinkCallbackTest Error!", err); -// } -// }); -// console.println("unlinkCallbackTest end"); -// } - -function writeSyncTest1() { - console.println("writeSyncTest1 begin"); - try { - let mode = 2; - let file = openSync("/data/local/tmp/a1.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - encoding: "utf-8", - }; - let length = writeSync(fd, "hello,world!", options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest1 Error!", error); - } - console.println("writeSyncTest1 end"); -} - -function writeSyncTest2() { - console.println("writeSyncTest2 begin"); - try { - let mode = 2; - let file = openSync("/data/local/tmp/a2.txt", mode); - let fd = file.fd; - console.println(`file open fd=${fd}`); - const options: WriteOptions = { - offset: 100, - }; - let length = writeSync(fd, new ArrayBuffer(1024), options); - console.println(`write file length=${length}`); - } catch (error) { - console.error("writeSyncTest2 Error!", error); - } - console.println("writeSyncTest2 end"); -} - -function writePromiseTest() { - console.println("writePromiseTest begin"); - try { - let mode = 2; - let file = openSync("/data/local/tmp/a3.txt", mode); - let fd = file.fd; - let length = await write(fd, "writePromiseTest"); - console.println(`writePromiseTest length=${length}`); - } catch (error) { - console.error("writePromiseTest Error!", error); - } - console.println("writePromiseTest end"); -} - -function writeCallbackTest() { - console.println("writeCallbackTest begin"); - let mode = 2; - let file = openSync("/data/local/tmp/a4.txt", mode); - let fd = file.fd; - write(fd, "writeCallbackTest", (err: BusinessError, data?: number) => { - if (data !== undefined) { - console.println(`writeCallbackTest length=${data}`); - } else { - console.error("writeCallbackTest Error!", err); - } - }); - console.println("writeCallbackTest end"); -} - -function errorHandlerTest1() { - console.println("errorHandlerTest1 begin"); - try { - let file = openSync("/data/local/tmp/a.txt"); - console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); - - console.println("\n"); - console.println("try open file notexist.txt ..."); - let mode = 2; - file = openSync("/data/local/tmp/notexist.txt", mode); - console.println(`open file notexist.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); - } catch (error: BusinessError) { - console.error(`errorHandlerTest1 Error! code=${error.code}, message=${error.message}`); - } - console.println("errorHandlerTest1 end"); -} - -// function errorHandlerTest2() { -// console.println("errorHandlerTest1 begin"); -// try { -// let file = openSync("/data/local/tmp/a.txt"); -// console.println("try open file a.txt ..."); -// let mode = -1; -// file = openSync("/data/local/tmp/a.txt", mode); -// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest2 Error!", error); -// } -// console.println("errorHandlerTest2 end"); -// } - -// function errorHandlerTest3() { -// console.println("errorHandlerTest3 begin"); -// try { -// let file = await open("/data/local/tmp/a.txt"); -// console.println(`open file a.txt, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// console.println("-----------"); -// console.println("try open file a.txt ..."); -// let mode = -1; -// file = await open("/data/local/tmp/a.txt", mode); -// console.println(`open file a.txt, mode=${mode}, fd=${file.fd}, path=${file.path}, name=${file.name}`); -// } catch (error) { -// console.error("errorHandlerTest3 Error!", error); -// } -// console.println("errorHandlerTest3 end"); -// } - -// function errorHandlerTest4() { -// console.println("errorHandlerTest4 begin"); -// open("/data/local/tmp/a.txt", (err: BusinessError, data?: File) => { -// if (err.code === 0 && data !== undefined) { -// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open file Error!", err); -// } -// }); - -// open("/data/local/tmp/a.txt", -1, (err: BusinessError, data?: File) => { -// console.println("2)-----------"); -// if (err.code === 0 && data !== undefined) { -// console.println(`open file a.txt, fd=${data.fd}, path=${data.path}, name=${data.name}`); -// } else { -// console.error("open file Error!", err); -// } -// }); -// console.println("errorHandlerTest4 end"); -// } - -function readTextSyncErr1() { - console.println("readTextSyncErr1 begin"); - try { - let ret = readTextSync("/data/local/tmp/afafes.txt.txt"); - console.println(`readTextSyncErr1 read text a.txt, result = ${ret}`); - - } catch (error) { - console.error("readTextSyncErr1 Error!", error); - } - console.println("readTextSyncErr1 end"); -} -function readTextSyncErr2() { - console.println("readTextSyncErr2 begin"); - let options: ReadTextOptions = { - offset: -1, - } - try { - let ret = readTextSync("/data/local/tmp/a.txt", options); - console.println(`readTextSyncErr2 read text a.txt, result = ${ret}`); - - } catch (error) { - console.error("readTextSyncErr2 Error!", error); - } - console.println("readTextSyncErr2 end"); -} -function readTextErr1() { - console.println("readTextErr1 begin"); - let options: ReadTextOptions = { - offset: -1, - } - try { - let ret = await readText("/data/local/tmp/a.txt", options); - console.println(`readTextErr1 read text a.txt, result = ${ret}`); - } catch (error) { - console.error("readTextErr1 Error!", error); - } - console.println("readTextErr1 end"); -} -function readTextErr2() { - console.println("readTextErr2 begin"); - let options: ReadTextOptions = { - offset: -1, - } - readText("/data/local/tmp/a.txt", options, (err: BusinessError, data?: string) => { - if (err.code == 0 && data !== undefined) { - console.println(`readTextErr2 read text a.txt, result = ${data}`); - } else { - console.error("readTextErr2 Error!", err); - } - }); - console.println("readTextErr2 end"); -} - - -function listFileSyncErr1() { - console.println("listFileSyncErr1 begin"); - try { - let ret = listFileSync("/data/local/tmp/zx/"); - console.println(`listFileSyncErr1 /data/local/tmp/zx/, result = ${ret}`); - } catch (error) { - console.error("listFileSyncErr1 Error!", error); - } - console.println("listFileSyncErr1 end"); -} -function listFileSyncErr2() { - console.println("listFileSyncErr2 begin"); - let options: ListFileOptions = { - listNum: -1, - } - try { - let ret = listFileSync("/data/local/tmp", options); - console.println(`listFileSyncErr2 /data/local/tmp, result = ${ret}`); - - } catch (error) { - console.error("listFileSyncErr2 Error!", error); - } - console.println("listFileSyncErr2 end"); -} -function listFileErr1() { - console.println("listFileErr1 begin"); - let options: ListFileOptions = { - listNum: -1, - } - try { - let ret = await listFile("/data/local/tmp/", options); - console.println(`listFileErr1 /data/local/tmp, result = ${ret}`); - } catch (error) { - console.error("listFileErr1 Error!", error); - } - console.println("listFileErr1 end"); -} -function listFileErr2() { - console.println("listFileErr2 begin"); - let options: ListFileOptions = { - listNum: -1, - } - listFile("/data/local/tmp", options, (err: BusinessError, data?: string[]) => { - if (err.code == 0 && data !== undefined) { - console.println(`listFileErr2 /data/local/tmp, result = ${data}`); - } else { - console.error("listFileErr2 Error!", err); - } - }); - console.println("listFileErr2 end"); -} - -function closeSyncErr1() { - console.println("closeSyncErr1 begin"); - try { - closeSync(-176); - console.println(`closeSyncErr1 -176`); - } catch (error) { - console.error("closeSyncErr1 Error!", error); - } - console.println("closeSyncErr1 end"); -} -function closeSyncErr2() { - console.println("closeSyncErr2 begin"); - try { - closeSync(65536); - console.println(`closeSyncErr2 65536`); - } catch (error) { - console.error("closeSyncErr2 Error!", error); - } - console.println("closeSyncErr2 end"); -} -function closeErr1() { - console.println("listFileErr1 begin"); - try { - let ret = await close(-1); - console.println(`closeErr1 -1, result = ${ret}`); - } catch (error) { - console.error("closeErr1 Error!", error); - } - console.println("closeErr1 end"); -} -function closeErr2() { - console.println("closeErr2 begin"); - close(-1, (err: BusinessError, data?: undefined) => { - if (err.code == 0) { - console.println(`closeErr2 Succ`); - } else { - console.error("closeErr2 Error!", err); - } - }); - console.println("closeErr2 end"); -} - -function main() { - console.println("---------- hello ani --------------"); - // accessSyncTest1(); - // openPromiseTest(); - // openCallbackTest(); - // openSyncTest(); - // closeSyncTest1(); - // closeSyncTest2(); - // closeSyncTest3(); - // closePromiseTest(); - // closePromiseTest2(); - // closeCallbackTest(); - // copyFileTest(); - // fileTest(); - // listFileSyncTest1(); - // listFileSyncTest2(); - // mkdirSyncTest1(); - // mkdirSyncTest2(); - // mkdirSyncTest3(); - // mkdirPromiseTest(); - // mkdirCallbackTest(); - // moveSyncTest(); - // readSyncTest1(); - // readSyncTest2(); - // readSyncTest3(); - // readTextSyncTest1(); - // readTextSyncTest2(); - // readTextSyncTest3(); - // rmdirSyncTest(); - // statSyncTest1(); - // statSyncTest2(); - // statPromiseTest(); - // statCallbackTest(); - // truncateSyncTest1(); - // truncateSyncTest2(); - // unlinkSyncTest(); - // unlinkPromiseTest(); - // unlinkCallbackTest(); - writeSyncTest1(); - writeSyncTest2(); - writePromiseTest(); - writeCallbackTest(); - // errorHandlerTest1(); // 打开文件失败,文件不存在 - // errorHandlerTest2(); // 参数校验失败 - // errorHandlerTest3(); // 异步Promise - // errorHandlerTest4(); // 异步callback - readTextErr1(); - readTextErr2(); - listFileSyncErr1() - listFileSyncErr2(); - listFileErr1(); - listFileErr1(); - closeSyncErr1(); - closeSyncErr2() - closeErr1(); - closeErr2(); - console.println("---------- hello ani end ---------------"); -} \ 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 5ab72429..1e68269a 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 @@ -51,55 +51,3 @@ class HashImpl { static native hashSync(path: string, algorithm: string): string; } - -function errorHandlerTestSucc() { - console.println("errorHandlerTestSucc begin"); - try { - let ret = await hash("/data/local/tmp/a.txt", "sha256"); - console.println(`errorHandlerTestSucc hash: ${ret}`); - } catch (error) { - console.error("errorHandlerTestSucc Error!", error); - } - - hash("/data/local/tmp/a.txt", "sha256", (err: BusinessError, data?: string) => { - if (err.code == 0 && data !== undefined) { - console.println(`errorHandlerTestSucc hash success: ${data}`); - } else { - console.error("errorHandlerTestSucc hash Error!", err); - } - }); - console.println("errorHandlerTestSucc end"); -} - -function errorHandlerTest3() { - console.println("errorHandlerTest3 begin"); - try { - let ret = await hash("/data/local/tmp/a.txt", "sha128"); - console.println(`errorHandlerTest3 hash: ${ret}`); - } catch (error) { - console.error("errorHandlerTest3 Error!", error); - } - console.println("errorHandlerTest3 end"); -} - -function errorHandlerTest4() { - console.println("errorHandlerTest4 begin"); - hash("/data/local/tmp/a.txt", "sha128", (err: BusinessError, data?: string) => { - if (err.code == 0 && data !== undefined) { - console.println(`errorHandlerTest4 hash success: ${data}`); - } else { - console.error("errorHandlerTest4 hash Error!", err); - } - }); - console.println("errorHandlerTest4 end"); -} - -function main() { - console.println("---------- hello ani --------------"); - - errorHandlerTestSucc(); - errorHandlerTest3(); - errorHandlerTest4(); - - console.println("---------- hello ani end ---------------"); -} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/ani/file_hash_class.ets b/interfaces/kits/js/src/mod_hash/ani/file_hash_class.ets deleted file mode 100644 index c8808e36..00000000 --- a/interfaces/kits/js/src/mod_hash/ani/file_hash_class.ets +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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. - */ - -export default hash; - -export class BusinessError { - code: number = 0; - data?: T; -} - -export type AsyncCallback = (err: BusinessError, data?: T) => void; - -class hash { - - static { - loadLibrary("ani_hash_class.z"); - } - - static native hashSync(path: string, algorithm: string): string; - - static hash(path: string, algorithm: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(hash.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } - - static hash(path: string, algorithm: string, callback: AsyncCallback): void { - let p1 = taskpool.execute(hash.hashSync, path, algorithm); - p1.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as string; - callback(err, r); - } - }); - } -} diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 1550fb4b..28c002c7 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -13,35 +13,13 @@ * limitations under the License. */ -export class BusinessError { - name: string; - message: string; - code: number; - data?: T; - - constructor() { - this.name = 'BusinessError'; - this.message = ''; - this.code = 0; - } - - constructor(code: number, msg: string, data?: T) { - this.name = 'BusinessError'; - this.code = code; - this.message = msg; - if (data !== undefined) { - this.data = data; - } - } -} - -export type AsyncCallback = (err: BusinessError, data?: T) => void; +import { BusinessError, AsyncCallback } from '@ohos.base'; type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; function setSecurityLabel(path: string, type: DataLevel): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); + let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); }).catch((e: BusinessError): void => { @@ -51,9 +29,10 @@ function setSecurityLabel(path: string, type: DataLevel): Promise { } function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); + let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(); + e.code = 0; callback(e, undefined); }).catch((e: BusinessError): void => { callback(e, undefined); @@ -61,10 +40,10 @@ function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback } function setSecurityLabelSync(path: string, type: DataLevel): void { - return securitylabelImpl.setSecurityLabelSync(path, type); + return SecurityLabelImpl.setSecurityLabelSync(path, type); } -class securitylabelImpl { +class SecurityLabelImpl { static { loadLibrary("ani_securitylabel_class"); @@ -72,60 +51,3 @@ class securitylabelImpl { static native setSecurityLabelSync(path: string, type: DataLevel): void; } - -function errorHandlerTest1() { - console.println("errorHandlerTest1 begin"); - try { - setSecurityLabelSync("/data/local/tmp/uihuihuisu.txt", "s2"); - console.println(`errorHandlerTest1 setSecurityLabelSync to s1`); - } catch (error) { - console.error("errorHandlerTest1 Error!", error); - } - console.println("errorHandlerTest1 end"); -} - -function errorHandlerTest2() { - console.println("errorHandlerTest2 begin"); - try { - setSecurityLabelSync("/data/local/tmp/a.txt", "s0"); - console.println(`errorHandlerTest2 setSecurityLabelSync to s0`); - } catch (error) { - console.error("errorHandlerTest2 Error!", error); - } - console.println("errorHandlerTest2 end"); -} - -function errorHandlerTest3() { - console.println("errorHandlerTest3 begin"); - try { - let ret = await setSecurityLabel("/data/local/tmp/a.txt", "s0"); - console.println(`errorHandlerTest3 setSecurityLabelSync to s2`); - } catch (error) { - console.error("errorHandlerTest3 Error!", error); - } - console.println("errorHandlerTest3 end"); -} - -function errorHandlerTest4() { - console.println("errorHandlerTest4 begin"); - setSecurityLabel("/data/local/tmp/a.txt", "s0", (err: BusinessError, data?: undefined) => { - if (err.code == 0) { - console.println(`errorHandlerTest4 setSecurityLabel success`); - } else { - console.error("errorHandlerTest4 setSecurityLabel Error!", err); - } - }); - console.println("errorHandlerTest4 end"); -} - -function main() { - console.println("---------- hello ani --------------"); - - errorHandlerTest1(); // 打开文件失败,文件不存在 - setSecurityLabelSync("/data/local/tmp/a.txt", "s4"); - errorHandlerTest2(); // 参数校验失败 - errorHandlerTest3(); // 异步Promise - errorHandlerTest4(); // 异步callback - - console.println("---------- hello ani end ---------------"); -} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets b/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets deleted file mode 100644 index 43f04463..00000000 --- a/interfaces/kits/js/src/mod_securitylabel/ani/file_securitylabel_class.ets +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -import { BusinessError, AsyncCallback } from '@ohos.base'; - -export default securitylabel; - -type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; - -class securitylabel { - - static { - loadLibrary("ani_securitylabel_class.z"); - } - - static native setSecurityLabelSync(path: string, type: DataLevel): int; - - static setSecurityLabel(path: string, type: DataLevel): Promise { - return new Promise((resolve: (result: int) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(securitylabel.setSecurityLabelSync, path, type); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(); - err.code = -1; - reject(err); - } else { - let r = ret as int; - resolve(r); - } - }); - }); - } - - static setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let p1 = taskpool.execute(securitylabel.setSecurityLabelSync, path, type); - p1.then((ret: NullishType) => { - let err = new BusinessError(); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as int; - callback(err, r); - } - }); - } -} -- Gitee From f5ebc6bb0dcc2297730283764f24538595a89840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Thu, 20 Mar 2025 09:34:40 +0800 Subject: [PATCH 67/76] =?UTF-8?q?rmdir=20=E5=A2=9E=E5=8A=A0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 兔叽和猪扒 Change-Id: I747127bc618f81bf9de3e48143cd51deb485fb59 --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 4 ++++ interfaces/kits/js/src/mod_fs/properties/ani/rmdir_ani.cpp | 4 ++++ 2 files changed, 8 insertions(+) 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 35df4b3d..dd140a35 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 @@ -348,6 +348,10 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A }); } +function rmdirSync(path: string): void { + return FileIoImpl.rmdirSync(path) +} + function truncateSync(file: string | number, len?: number): void { return FileIoImpl.truncateSync(file, len) } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/rmdir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/rmdir_ani.cpp index 42e4c5cf..04a4cfa6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/rmdir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/rmdir_ani.cpp @@ -15,6 +15,7 @@ #include "rmdir_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "rmdir_core.h" #include "type_converter.h" @@ -30,11 +31,14 @@ void RmdirAni::RmdirSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_str auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = RmdirentCore::DoRmdirent(pathStr); if (!ret.IsSuccess()) { HILOGE("DoRmdirent failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } } -- Gitee From 0a741c8d39155dacccd112867c1e0d7a4ee12e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Thu, 20 Mar 2025 09:42:39 +0800 Subject: [PATCH 68/76] =?UTF-8?q?read=20=E5=A2=9E=E5=8A=A0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 兔叽和猪扒 Change-Id: Idd943a5102887178e803d79bdf7e0cd5ca4307d5 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 74 +++++++++---------- .../js/src/mod_fs/properties/ani/read_ani.cpp | 5 ++ 2 files changed, 38 insertions(+), 41 deletions(-) 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 35df4b3d..1e68d900 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 @@ -286,35 +286,33 @@ function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback }); } -function read(fd: number, buffer: ArrayBuffer): Promise { - return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; - reject(e); - } else { - let r = ret as number; - resolve(r); - } - }); - }); +function readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number { + return FileIoImpl.readSync(fd, buffer, options) } -function read(fd: number, buffer: ArrayBuffer, options: ReadOptions): Promise { +function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); - promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; + if (options === undefined) { + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer):number => { + return FileIoImpl.readSync(fd, buffer); + }, fd, buffer); + promise.then((ret: NullishType) => { + let result = ret as number; + resolve(result); + }).catch((e: BusinessError): void => { reject(e); - } else { - let r = ret as number; - resolve(r); - } - }); + }); + } else { + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer, options: ReadOptions):number=> { + return FileIoImpl.readSync(fd, buffer, options) + }, fd, buffer, options); + promise.then((ret: NullishType) => { + let result = ret as number; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + } }); } @@ -322,14 +320,11 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback { let e = new BusinessError(); - if (ret === null || ret === undefined) { - e.code = -1; - callback(e, 0); - } else { - e.code = 0; - let r = ret as number; - callback(e, r); - } + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); }); } @@ -337,14 +332,11 @@ function read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: A let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); - if (ret === null || ret === undefined) { - e.code = -1; - callback(e, 0); - } else { - e.code = 0; - let r = ret as number; - callback(e, r); - } + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); }); } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp index 016c0960..fa5ce233 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_ani.cpp @@ -17,6 +17,7 @@ #include #include "ani_helper.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "read_core.h" #include "type_converter.h" @@ -57,18 +58,22 @@ ani_double ReadAni::ReadSync( auto [succBuf, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer); if (!succBuf) { HILOGE("Failed to resolve arrayBuffer!"); + ErrorHandler::Throw(env, EINVAL); return -1; } auto [succOp, op] = ToReadOptions(env, options); if (!succOp) { HILOGE("Failed to resolve options!"); + ErrorHandler::Throw(env, EINVAL); return -1; } auto ret = ReadCore::DoRead(static_cast(fd), arrayBuffer, op); if (!ret.IsSuccess()) { HILOGE("Read file content failed!"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return -1; } return static_cast(ret.GetData().value()); -- Gitee From 3811c177c08554c27d7cb7f9d17b2126e80ad4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=8F=BD=E5=92=8C=E7=8C=AA=E6=89=92?= Date: Thu, 20 Mar 2025 09:54:01 +0800 Subject: [PATCH 69/76] =?UTF-8?q?access=20=E5=A2=9E=E5=8A=A0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 兔叽和猪扒 Change-Id: I4688d984573f5f75fd26e2e9e1b2fb4a00b7bda1 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 45 +++++++------------ .../src/mod_fs/properties/ani/access_ani.cpp | 6 +++ 2 files changed, 21 insertions(+), 30 deletions(-) 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 35df4b3d..bb666915 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 @@ -21,28 +21,20 @@ function access(path: string, mode?: AccessModeType): Promise { return FileIoImpl.doAccessSync(path); }, path); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; - reject(e); - } else { - let result = ret as boolean; - resolve(result); - } + let result = ret as boolean; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); }); } else { let promise = taskpool.execute((path: string, mode: AccessModeType): boolean => { return FileIoImpl.doAccessSync(path, mode); }, path, mode); promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; - reject(e); - } else { - let result = ret as boolean; - resolve(result); - } + let result = ret as boolean; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); }); } }); @@ -54,14 +46,11 @@ function access(path: string, callback: AsyncCallback): void { }, path); promise.then((ret: NullishType): void => { let e = new BusinessError(); - if (ret === null || ret === undefined) { - e.code = -1; - callback(e, false); - } else { - e.code = 0; - let result = ret as boolean; - callback(e, result); - } + e.code = 0; + let result = ret as boolean; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, false); }); } @@ -69,14 +58,10 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(FileIoImpl.doAccessSync, path, mode, flag); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; - reject(e); - } else { let result = ret as boolean; resolve(result); - } + }).catch((e: BusinessError): void => { + reject(e); }); }) } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/access_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/access_ani.cpp index e98af682..329e1162 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/access_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/access_ani.cpp @@ -16,6 +16,7 @@ #include "access_ani.h" #include "access_core.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -80,12 +81,14 @@ ani_boolean AccessAni::AccessSync3( auto [succPath, pathStr] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return ret; } auto [succMode, modeOp] = TypeConverter::EnumToInt32(env, mode); if (!succMode) { HILOGE("Invalid mode"); + ErrorHandler::Throw(env, EINVAL); return ret; } auto modeType = OptToAccessModeType(modeOp); @@ -93,6 +96,7 @@ ani_boolean AccessAni::AccessSync3( auto [succFlag, flagOpt] = TypeConverter::EnumToInt32(env, flag); if (!succFlag) { HILOGE("Invalid flag"); + ErrorHandler::Throw(env, EINVAL); return ret; } auto flagType = OptToAccessFlagType(flagOpt); @@ -106,6 +110,8 @@ ani_boolean AccessAni::AccessSync3( if (!fsRet.IsSuccess()) { HILOGE("DoAccess failed"); + const auto &err = fsRet.GetError(); + ErrorHandler::Throw(env, err); return false; } return fsRet.GetData().value(); -- Gitee From 4c81e4ea6c36908176a9636fa17e1dcad509abc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 10:15:15 +0800 Subject: [PATCH 70/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If74a3b988222f7417ee286b6fbc7d7ae1ec80c9d --- .../kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 10 +++++----- .../js/src/mod_fs/properties/ani/read_text_ani.cpp | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) 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 7e34bea5..c2d4c4f5 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 @@ -58,10 +58,10 @@ function access(path: string, mode: AccessModeType, flag: AccessFlagType): Promi return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(FileIoImpl.doAccessSync, path, mode, flag); promise.then((ret: NullishType): void => { - let result = ret as boolean; - resolve(result); + let result = ret as boolean; + resolve(result); }).catch((e: BusinessError): void => { - reject(e); + reject(e); }); }) } @@ -278,7 +278,7 @@ function readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): numbe function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { if (options === undefined) { - let promise = taskpool.execute((fd: number, buffer: ArrayBuffer):number => { + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer): number => { return FileIoImpl.readSync(fd, buffer); }, fd, buffer); promise.then((ret: NullishType) => { @@ -288,7 +288,7 @@ function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer, options: ReadOptions): number => { return FileIoImpl.readSync(fd, buffer, options) }, fd, buffer, options); promise.then((ret: NullishType) => { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp index bd7dbd55..dc733c60 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp @@ -70,7 +70,7 @@ ani_string ReadTextAni::ReadTextSync( { auto [succOpt, options] = ToReadTextOptions(env, obj); if (!succOpt) { - HILOGE("Ivalid options"); + HILOGE("Invalid options"); ErrorHandler::Throw(env, EINVAL); return nullptr; } @@ -94,7 +94,7 @@ ani_string ReadTextAni::ReadTextSync( string res = std::get<0>(resText); auto [succ, result] = TypeConverter::ToAniString(env, res); if (!succ) { - HILOGE("Create ani_string error"); + HILOGE("Convert result to ani string failed"); ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } -- Gitee From 3fcffdcecdd94d4cd01c60d9d93758bb5aa8f90d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 10:17:20 +0800 Subject: [PATCH 71/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I626b920367ed5032a8ebbeded739a8aae58f024f --- .../kits/js/src/mod_fs/properties/ani/listfile_ani.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 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 d19f25ae..915a590b 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 @@ -83,7 +83,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double result_ref_res; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(result_ref), "doubleValue", nullptr, &result_ref_res)) { + static_cast(result_ref), "doubleValue", nullptr, &result_ref_res)) { return { false, nullopt }; } double result = static_cast(result_ref_res); @@ -111,7 +111,7 @@ tuple>> ParseArrayString(ani_env *env, ani_object for (int i = 0; i < int(length); i++) { ani_ref stringEntryRef; if (ANI_OK != env->Object_CallMethodByName_Ref(static_cast(result_ref), "$_get", - "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i)) { + "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i)) { return { false, nullopt }; } auto [succ, tmp] = TypeConverter::ToUTF8String(env, static_cast(stringEntryRef)); @@ -228,7 +228,7 @@ ani_array_ref ListFileAni::ListFileSync(ani_env *env, [[maybe_unused]] ani_class const std::string *strArray = fileList.data(); auto [succ, result] = TypeConverter::ToAniStringList(env, strArray, fileList.size()); if (!succ) { - HILOGE("list file result value to ani_string list failed"); + HILOGE("Convert list file result to ani string array failed"); ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } -- Gitee From dd9d323c3dd0ae41958e0fa40b59753cea1994a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 10:19:16 +0800 Subject: [PATCH 72/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I6d4af2625879f2c7b6f74cbd843a1726bf4bf3d1 --- interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp index 0728869a..6acb4389 100644 --- a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp @@ -55,7 +55,7 @@ ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani const auto &res = ret.GetData().value(); auto [succ, result] = TypeConverter::ToAniString(env, res); if (!succ) { - HILOGE("Convert hash value to ani_string failed"); + HILOGE("Convert hash value to ani string failed"); ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } -- Gitee From 28a4addc01441ae7ebe82acf0d1d729884752ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 10:21:39 +0800 Subject: [PATCH 73/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I88d514cd729205684cb29d21398bf297da026011 --- interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp index 9744c32d..a8725410 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp @@ -135,6 +135,7 @@ ani_double WriteAni::WriteSync( return static_cast(ret.GetData().value()); } HILOGE("Unsupported buffer type!"); + ErrorHandler::Throw(env, EINVAL); return -1; } } // namespace ANI -- Gitee From acce5ebf5020a8d350f4b2121b7f36462af1cb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 10:23:27 +0800 Subject: [PATCH 74/76] =?UTF-8?q?Signed-off-by:=20=E5=A7=9C=E5=B0=8F?= =?UTF-8?q?=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I0f3a15e60ac82ca11c79374e66ebc8a6282d2abf --- interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c2d4c4f5..a2323294 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 @@ -545,7 +545,7 @@ enum AccessFlagType { LOCAL = 0, } -interface File { +export interface File { fd: number; path: String; name: String; -- Gitee From a96df119b3df8227ef456677fdeb0c7e45e8794f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 11:58:01 +0800 Subject: [PATCH 75/76] =?UTF-8?q?stat=20=E4=BB=A3=E7=A0=81=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20Signed-off-by:=20=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ieda6b49d115304a4f4b6e2875aa9bb88aefcdd34 --- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 22 +++++-------------- .../js/src/mod_fs/class_stat/ani/stat_ani.cpp | 3 --- 2 files changed, 5 insertions(+), 20 deletions(-) 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 a2323294..67eb95ad 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 @@ -472,14 +472,8 @@ function stat(file: string | number): Promise { return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(FileIoImpl.statSync, file); promise.then((ret: NullishType): void => { - if (ret === null || ret === undefined) { - let e = new BusinessError(); - e.code = -1; - reject(e); - } else { - let r = ret as Stat; - resolve(r); - } + let r = ret as Stat; + resolve(r); }).catch((e: BusinessError): void => { reject(e); }); @@ -490,15 +484,9 @@ function stat(file: string | number, callback: AsyncCallback): void let p = taskpool.execute(FileIoImpl.statSync, file); p.then((ret: NullishType): void => { let e = new BusinessError(); - if (ret === null || ret === undefined) { - e.code = -1; - let stat: Stat = new StatInner(0); - callback(e, stat); - } else { - e.code = 0; - let r = ret as Stat; - callback(e, r); - } + e.code = 0; + let r = ret as Stat; + callback(e, r); }).catch((e: BusinessError): void => { callback(e, new StatInner(0)); }); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index ac78c5ed..beb411ed 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -15,9 +15,6 @@ #include "stat_ani.h" -#include -#include - #include "error_handler.h" #include "filemgmt_libhilog.h" #include "stat_core.h" -- Gitee From 6b1a8d4a1ec10b1ff59ee1a839f102b72f6a2235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 20 Mar 2025 13:04:29 +0800 Subject: [PATCH 76/76] =?UTF-8?q?=E6=BB=A1=E8=B6=B3=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E8=A6=81=E6=B1=82=20Signed-off-by:=20?= =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I862de497782dcc2855c5a487232f2e4b5fe93d43 --- .../js/src/common/ani_helper/error_handler.h | 41 ++++++++++++------- .../mod_fs/properties/ani/listfile_ani.cpp | 18 ++++---- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index ed91aea8..59c41c11 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -70,55 +70,68 @@ private: return ANI_INVALID_ARGS; } + auto [status, err] = CreateErrorObj(env, className, name, code, errMsg); + + if (status != ANI_OK) { + HILOGE("Create error object failed!"); + return status; + } + + status = env->ThrowError(err); + if (status != ANI_OK) { + HILOGE("Throw ani error object failed!"); + return status; + } + return ANI_OK; + } + + static std::tuple CreateErrorObj( + ani_env *env, const char *className, const char *name, int32_t code, const std::string &errMsg) + { ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class '%{private}s'", className); - return ANI_NOT_FOUND; + return { ANI_NOT_FOUND, nullptr }; } ani_method ctor; if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { HILOGE("Cannot find constructor for class '%{private}s'", className); - return ANI_NOT_FOUND; + return { ANI_NOT_FOUND, nullptr }; } auto [succ, message] = TypeConverter::ToAniString(env, errMsg); if (!succ) { HILOGE("Convert errMsg to ani string failed"); - return ANI_ERROR; + return { ANI_ERROR, nullptr }; } ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, static_cast(code), message)) { HILOGE("Cannot create ani error object"); - return ANI_ERROR; + return { ANI_ERROR, nullptr }; } ani_status status = ANI_ERROR; status = AniHelper::SetFieldValue(env, cls, obj, "name", name); if (status != ANI_OK) { HILOGE("Set field 'name' value failed"); - return status; + return { status, nullptr }; } status = AniHelper::SetFieldValue(env, cls, obj, "message", errMsg); if (status != ANI_OK) { HILOGE("Set field 'message' value failed"); - return status; + return { status, nullptr }; } status = AniHelper::SetFieldValue(env, cls, obj, "code", static_cast(code)); if (status != ANI_OK) { HILOGE("Set field 'code' value failed"); - return status; + return { status, nullptr }; } - - status = env->ThrowError(static_cast(obj)); - if (status != ANI_OK) { - HILOGE("Throw ani error object failed!"); - return status; - } - return ANI_OK; + ani_error err = static_cast(obj); + return { ANI_OK, std::move(err) }; } }; 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 915a590b..8540848a 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 @@ -40,8 +40,8 @@ tuple ParseBooleanParam(ani_env *env, ani_object obj, string tag) return { true, false }; } ani_boolean bool_ref_res; - if (ANI_OK != - env->Object_CallMethodByName_Boolean(static_cast(bool_ref), "unboxed", ":Z", &bool_ref_res)) { + if (ANI_OK != env->Object_CallMethodByName_Boolean( + static_cast(bool_ref), "unboxed", ":Z", &bool_ref_res)) { return { false, false }; } return { true, static_cast(bool_ref_res) }; @@ -60,8 +60,8 @@ tuple ParseIntParam(ani_env *env, ani_object obj, string tag) return { true, result }; } ani_int result_ref_res; - if (ANI_OK != - env->Object_CallMethodByName_Int(static_cast(result_ref), "intValue", nullptr, &result_ref_res)) { + if (ANI_OK != env->Object_CallMethodByName_Int( + static_cast(result_ref), "intValue", nullptr, &result_ref_res)) { result = -1; return { false, result }; } @@ -83,7 +83,7 @@ tuple> ParseDoubleParam(ani_env *env, ani_object obj, str ani_double result_ref_res; if (ANI_OK != env->Object_CallMethodByName_Double( - static_cast(result_ref), "doubleValue", nullptr, &result_ref_res)) { + static_cast(result_ref), "doubleValue", nullptr, &result_ref_res)) { return { false, nullopt }; } double result = static_cast(result_ref_res); @@ -104,14 +104,14 @@ tuple>> ParseArrayString(ani_env *env, ani_object } ani_double length; - if (ANI_OK != env->Object_GetPropertyByName_Double(static_cast(result_ref), "length", &length) || - length == 0) { + if (ANI_OK != env->Object_GetPropertyByName_Double( + static_cast(result_ref), "length", &length) || length == 0) { return { false, nullopt }; } for (int i = 0; i < int(length); i++) { ani_ref stringEntryRef; - if (ANI_OK != env->Object_CallMethodByName_Ref(static_cast(result_ref), "$_get", - "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i)) { + if (ANI_OK != env->Object_CallMethodByName_Ref( + static_cast(result_ref), "$_get", "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i)) { return { false, nullopt }; } auto [succ, tmp] = TypeConverter::ToUTF8String(env, static_cast(stringEntryRef)); -- Gitee