From 7f2c8f580953821832170e539acf58d8dd33a3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 10 Mar 2025 16:05:12 +0800 Subject: [PATCH] =?UTF-8?q?ets=E9=85=8D=E7=BD=AE=E4=B8=8Ebug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 6 + .../kits/js/src/mod_fs/ani/file_fs_class.ets | 359 ++++++++++++++++++ .../mod_fs/properties/ani/read_text_ani.cpp | 2 +- .../js/src/mod_fs/properties/close_core.cpp | 2 +- 4 files changed, 367 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 9498f847..f45ec361 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 @@ -24,6 +24,9 @@ #include "stat_ani.h" #include "unlink_ani.h" #include "rmdir_ani.h" +#include "close_ani.h" +#include "listfile_ani.h" +#include "read_text_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; @@ -58,6 +61,9 @@ static ani_status BindFileFs(ani_vm *vm) ani_native_function { "copyFileSync", nullptr, reinterpret_cast(CopyFileAni::CopyFileSync) }, ani_native_function { "statSync", nullptr, reinterpret_cast(StatAni::StatSync) }, ani_native_function { "rmdirSync", "Lstd/core/String;:V", reinterpret_cast(RmdirAni::RmdirSync) }, + ani_native_function {"closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync)}, + ani_native_function {"listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync)}, + ani_native_function {"readTextSync", nullptr, reinterpret_cast(ReadTextAni::ReadTextSync)}, }; return BindClass(vm, className, functions); 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 ba52d311..44f80c60 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 @@ -40,6 +40,60 @@ enum AccessFlagType { LOCAL = 0, } +export interface ReadOptions { + offset?: number; + length?: number; +} + +export interface ReadTextOptions extends ReadOptions { + encoding?: string; +} + +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 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; + path: String = ""; + name: String = ""; + + private nativeFile:long = 0; + + constructor(file:long) { + if(this.nativeFile == 0){ + this.nativeFile = file; + } + } + native getParent(): String; + native lock(exclusive?: boolean): void; + native tryLock(exclusive?: boolean): void; + native unlock(): void; +} + +type DataType = number | File; class fileIo { @@ -49,6 +103,9 @@ class fileIo { static native mkdirSync(path: string, recursion: boolean): int; static native moveFileSync(src: String, dest: String, mode?: int): void; static native rmdirSync(path: string): void; + static native closeSync(file: DataType): int; + static native listFileSync(path: string, options?: ListFileOptions): string[]; + static native readTextSync(filePath: string, options?:ReadTextOptions): string; static accessSync(path: string, mode?: AccessModeType): boolean { return fileIo.doAccessSync(path, mode); @@ -239,6 +296,179 @@ class fileIo { }); } + static close(file: DataType): 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: DataType, 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 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(); + err.code = -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(); + err.code = -1; + reject(err); + } else { + let r = ret as string[]; + resolve(r); + } + }); + }); + } + + static listFile(path: string, callback: AsyncCallback): void { + let p1 = taskpool.execute(fileIo.listFileSync1, path); + 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); + } + }); + } + + 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(); + if (ret === null || ret === undefined) { + err.code = -1 + callback(err, undefined) + }else { + err.code = 0 + let r = ret as string[]; + callback(err, r); + } + }); + } + + 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 p1 = taskpool.execute(fileIo.readTextSync1, filePath); + 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); + } + }); + } + + 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(); + 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); + } + }); + } + static native copyFileSync(src: FileDataType, dest: FileDataType, mode?: int): void; static native statSync(file: FileDataType): Stat; } @@ -453,10 +683,139 @@ function stat_test(file: FileDataType) { console.info("stat, isSymbolicLink is " + stat.isSymbolicLink()); } +function closeTest() { + console.println("closeSync start"); + let ret = fileIo.closeSync(64) + console.println("closeSync end"); + console.println(ret); + + let srcFile :FileInner = new FileInner(64); + srcFile.fd = 64; // 此时给的参数与结果无关,文件描述符为进入接口后临时添加的open打开的文件 + ret = fileIo.closeSync(srcFile) + console.println("closeSync end"); + console.println(ret); + + try { + fileIo.close(srcFile).then((result: int) => { + console.println(result) + }); + } catch (error) { + console.error("Promise: close Error:", error); + } + console.println("close Promise async end"); + + let filePath = "/data/local/tmp"; + fileIo.close(srcFile, (err: BusinessError, data?: int) => { + if (err.code) { + console.error("Callback: close Error:", err); + } else { + console.log("Callback: close:", data); + } + }); + console.println("readText callback async end"); +} + +function listFileTest() { + console.println("listFileTest start") + console.println("dir: /data/local/tmp") + + let array = fileIo.listFileSync("/data/local/tmp") + console.println("listFileSync end") + for (let str of array) { + console.println(str) + } + + let tmp :Array = new Array(3); + tmp[0] = ".txt"; + tmp[1] = ".png"; + tmp[2] = ".data"; + let filter: Filter = { + suffix : tmp, + fileSizeOver: 1, + lastModifiedAfter: 0, + } + let options :ListFileOptions = { + recursion: true, + listNum: 5, + filter: filter, + } + + console.println("listFileSync start") + let err = fileIo.listFileSync("/data/local/tmp", options) + console.println("listFileSync end") + console.println(err) + + try { + fileIo.listFile("/data/local/tmp", options).then((result: string[]) => { + console.println(result) + }); + } catch (error) { + console.error("Promise: Error listFile:", error); + } + console.println("listFile Promise async end"); + + let filePath = "/data/local/tmp"; + fileIo.listFile(filePath, options, (err: BusinessError, data?: string[]) => { + if (err.code) { + console.error("Callback: Error listFile:", err); + } else { + console.log("Callback: listFile:", data); + } + }); + console.println("listFile callback async end"); +} + +function readTextTest() { + let ret = fileIo.readTextSync("/data/local/tmp/hello.txt") + console.println("readTextSync end") + console.println(ret) + + let readOptions1 :ReadTextOptions = { + offset: 0, + length: 5, + encoding: "utf-8", + } + + let readOptions2 :ReadTextOptions = { + offset: 0, + } + + console.println("set readOptions") + + let ret1 = fileIo.readTextSync("/data/local/tmp/hello.txt", readOptions1) + console.println("readTextSync options1 end") + console.println(ret1) + + let ret2 = fileIo.readTextSync("/data/local/tmp/hello.txt", readOptions2) + console.println("readTextSync options2 end") + + try { + fileIo.readText("/data/local/tmp/hello.txt").then((result: string) => { + console.println(result) + }); + } catch (error) { + console.error("Promise: Error getting temp dir:", error); + } + console.println("readText Promise async end"); + + let filePath = "/data/local/tmp/hello.txt"; + fileIo.readText(filePath, (err: BusinessError, data?: string) => { + if (err.code) { + console.error("Callback: Error readText:", err); + } else { + console.log("Callback: readText:", data); + } + }); + console.println("readText callback async end"); +} + function main(){ console.println("---------- hello ani --------------") accessSyncTest() moveSyncTest() rmdirSyncTest() + readTextTest() + listFileTest() + closeTest() console.println("---------- hello ani end ---------------") } \ No newline at end of file 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 4b2096af..2b59f6b6 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 @@ -57,7 +57,7 @@ static tuple> ParseEncoding(ani_env *env, ani_object obj) } env->Reference_IsUndefined(encoding_ref, &isUndefined); if (isUndefined) { - return { false, nullopt }; + return { true, nullopt }; } auto [succ, encoding] = TypeConverter::ToUTF8String(env, (ani_string)encoding_ref); if (!succ) { diff --git a/interfaces/kits/js/src/mod_fs/properties/close_core.cpp b/interfaces/kits/js/src/mod_fs/properties/close_core.cpp index e93ec88c..9ac7a784 100644 --- a/interfaces/kits/js/src/mod_fs/properties/close_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/close_core.cpp @@ -56,7 +56,7 @@ FsResult CloseCore::DoClose(const int32_t &fd) } auto err = CloseFd(fd); if (err) { - FsResult::Error(err); + return FsResult::Error(err); } return FsResult::Success(); } -- Gitee