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 adb93d29c91709718c78d506e1c41199b5344a16..ed91aea80299008bbb236f7607acbd43f2cc07b7 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -82,8 +82,14 @@ private: return ANI_NOT_FOUND; } + auto [succ, message] = TypeConverter::ToAniString(env, errMsg); + if (!succ) { + HILOGE("Convert errMsg to ani string failed"); + return ANI_ERROR; + } + ani_object obj; - if (ANI_OK != env->Object_New(cls, ctor, &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/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 16a06adc6f3c9948431690251b89cba902b717e3..489841a8dc6ece236cf7574bac38d76019e5c9e5 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,7 +70,7 @@ 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) }, @@ -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 7245f85d23f57002f9d34bc727cf7c562e11e96e..8738e3c4c1b794cd8c76f60f471ad917a3973ee6 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,243 +12,18 @@ * 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; -} - -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; - 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?: number; - length?: number; - encoding?: string; -} - -enum AccessModeType { - EXIST = 0, - WRITE = 2, - READ = 4, - READ_WRITE = 6, -} - -enum AccessFlagType { - LOCAL = 0, -} - -interface File { - fd: number; - path: String; - name: String; - - getParent(): String; - lock(exclusive?: boolean): void; - tryLock(exclusive?: boolean): void; - unlock(): void; -} - -class FileInner implements File { - fd: number = -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; - - 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; -} - -class fileIo { - - static { - loadLibrary("ani_fs_class"); - } - - 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 { +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(fileIo.accessSync1, path); - promise.then((ret: NullishType): void => { + 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; @@ -256,11 +31,13 @@ class fileIo { } }); } else { - let promise = taskpool.execute(fileIo.accessSync2, path, mode); - promise.then((ret: NullishType): void => { + 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; @@ -271,1185 +48,1395 @@ 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): void => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-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); + } + }); +} - static access(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.accessSync1, path); +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 => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1; - callback(err, undefined); + let err = new BusinessError(-1, ""); + + reject(err); } else { - err.code = 0; let result = ret as boolean; - callback(err, result); + resolve(result); } }); - } + }) +} - static native closeSync(file: number | File): void; +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); +} - 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); +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 => { - let e = new BusinessError(0, ""); - callback(e, undefined); + resolve(undefined); }).catch((e: BusinessError): void => { - callback(e, undefined); + reject(e); }); - } - - 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); - } - }); - }); - } +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); + }); +} - 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); - } - }); - } +function closeSync(file: number | File): void { + return FileIoImpl.closeSync(file) +} - 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); - } - }); - } +function mkdirSync(path: string): void { + return FileIoImpl.mkdirSync(path) +} - static native mkdirSync(path: string): void; +function mkdirSync(path: string, recursion: boolean): void { + return FileIoImpl.mkdirSync(path, recursion) +} - static native mkdirSync(path: string, recursion: boolean): void; +function mkdirSync1(path: string): undefined { + FileIoImpl.mkdirSync(path); + return undefined; +} - 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); - }); - }); - } +function mkdirSync2(path: string, recursion: boolean): undefined { + FileIoImpl.mkdirSync(path, recursion); + return undefined; +} - static mkdir(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): undefined => fileIo.mkdirSync(path), path); +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 => { - let e = new BusinessError(0, ""); - callback(e, undefined); + resolve(undefined); }).catch((e: BusinessError): void => { - callback(e, undefined); + reject(e as BusinessError); }); - } + }); +} - 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); - }); - }); - } +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); + }); +} - static mkdir(path: string, recursion: boolean, callback: AsyncCallback): void { +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 => - fileIo.mkdirSync(path, recursion), path, recursion); + 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); + resolve(undefined); + }).catch((e: NullishType): void => { + reject(e as BusinessError); }); - } - - static native moveFileSync(src: String, dest: String, mode?: number): void; + }); +} - static native openSync(path: String, mode?: number): File; +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); + }); +} - static openSync1(path: String, mode: number): File { - return fileIo.openSync(path, mode); - } +function moveFileSync(src: string, dest: string, mode?: number): void { + return FileIoImpl.moveFileSync(src, dest, mode); +} - static openSync2(path: String): File { - return fileIo.openSync(path); - } +function openSync(path: string, mode?: number): File { + return FileIoImpl.openSync(path, mode); +} - 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); - }); - }); - } +function openSync1(path: String, mode: number): File { + return FileIoImpl.openSync(path, mode); +} - 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); - }); - }); - } +function openSync2(path: String): File { + return FileIoImpl.openSync(path); +} - static open(path: String, mode: number, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.openSync1, path, mode); +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 e = new BusinessError(0, ""); let file = ret as File; - callback(e, file); + resolve(file); }).catch((e: BusinessError): void => { - let f: File = new FileInner(0); - callback(e, f); + reject(e); }); - } + }); +} - static open(path: String, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.openSync2, path); +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 e = new BusinessError(0, ""); let file = ret as File; - callback(e, file); + resolve(file); }).catch((e: BusinessError): void => { - let f: File = new FileInner(0); - callback(e, f); + reject(e); }); - } - - 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); - } +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); + }); +} - 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, ""); +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); + }); +} - reject(err); - } else { - let r = ret as number; - resolve(r); - } - }); - }); - } +function writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number { + return FileIoImpl.writeSync(fd, buffer, options); +} - 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, ""); +function writeSync1(fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number { + return FileIoImpl.writeSync(fd, buffer, options); +} - reject(err); - } else { - let r = ret as number; - resolve(r); - } - }); - }); - } +function writeSync2(fd: number, buffer: string | ArrayBuffer): number { + return FileIoImpl.writeSync(fd, buffer); +} - static read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync1, 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 err = new BusinessError(-1, ""); + let result = ret as number 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 number; - callback(err, r); + resolve(result); } }); - } + }); +} - static read(fd: number, buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readSync2, fd, buffer, options); +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 err = new BusinessError(-1, ""); + let result = ret as number 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 number; - callback(err, r); + resolve(result); } }); - } - - 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, ""); +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); + } + }); +} - reject(err); - } else { - let r = ret as string; - resolve(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); + } + }); +} - 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, ""); +function readSync1(fd: number, buffer: ArrayBuffer): number { + return FileIoImpl.readSync(fd, buffer) +} - reject(err); - } else { - let r = ret as string; - resolve(r); - } - }); - }); - } +function readSync2(fd: number, buffer: ArrayBuffer, options: ReadOptions): number { + return FileIoImpl.readSync(fd, buffer, options) +} - static readText(filePath: string, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync1, filePath); +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 => { - let err = new BusinessError(-1, ""); 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); + let r = ret as number; + resolve(r); } }); - } + }); +} - static readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(fileIo.readTextSync2, filePath, options); +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 => { - let err = new BusinessError(-1, ""); 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); + let r = ret as number; + resolve(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); - } - }); - } +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); + } + }); +} - static native truncateSync(file: string | number, len?: number): void; +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); + } + }); +} - static native unlinkSync(path: string): void; +function truncateSync(file: string | number, len?: number): void { + return FileIoImpl.truncateSync(file, len) +} - static unlink(path: string): Promise { - return new Promise((resolve: (result: undefined) => void, - reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute((path: string): undefined => fileIo.unlinkSync(path), path); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - }); - } +function unlinkSync(path: string): void { + return FileIoImpl.unlinkSync(path) +} - static unlink(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string): undefined => fileIo.unlinkSync(path), 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 => unlinkSync(path), path); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); - callback(e, undefined); + resolve(undefined); }).catch((e: BusinessError): void => { - callback(e, undefined); + reject(e); }); - } - - 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); - } +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, ""); + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} - 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); +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 => { - let result = ret as number if (ret === null || ret === undefined) { let err = new BusinessError(-1, ""); + reject(err); } else { - resolve(result); + let r = ret as string; + resolve(r); } }); - }); - } - - 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); + } else { + let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, 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); + let r = ret as string; + resolve(r); } }); - }); - } - - 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 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 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!"); +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 { - console.error("closeCallbackTest: close Error:", err); + err.code = 0; + let r = ret as string; + callback(err, r); } }); +} - console.println("closeCallbackTest end"); +function readTextSync(filePath: string, options?: ReadTextOptions): string { + return FileIoImpl.readTextSync(filePath, options); } -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); +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); + } + }); } - } 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); + }); +} + +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); } - } 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 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 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 listFileSync(path: string, options?: ListFileOptions): string[] { + return FileIoImpl.listFileSync(path, options); } -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 copyFileSync(src: string | number, dest: string | number, mode?: number): void { + return FileIoImpl.copyFileSync(src, dest, mode) } -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 statSync(file: string | number): Stat { + return FileIoImpl.statSync(file) } -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!"); - } +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); + } + }); }); - 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}`); +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 { - console.error("open failed!", err); + err.code = 0; + let r = ret as Stat; + callback(err, r); } }); } -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); +export class BusinessError { + name: string; + message: string; + code: number; + data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; } - 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, + + constructor(code: number, msg: string, data?: T) { + this.name = 'BusinessError'; + this.code = code; + this.message = msg; + if (data !== undefined) { + this.data = data; } - 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"); +export type AsyncCallback = (err: BusinessError, data?: T) => void; + +export interface Filter { + suffix?: Array; + displayName?: Array; + mimeType?: Array; + fileSizeOver?: number; + lastModifiedAfter?: number; + excludeMedia?: boolean; } -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 ListFileOptions { + recursion?: boolean; + listNum?: number; + filter?: Filter; } -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 ReadOptions { + offset?: number; + length?: number; } -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"); +export interface ReadTextOptions extends ReadOptions { + encoding?: string; } -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 WriteOptions { + offset?: number; + length?: number; + encoding?: string; } -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"); +enum AccessModeType { + EXIST = 0, + WRITE = 2, + READ = 4, + READ_WRITE = 6, } -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"); +enum AccessFlagType { + LOCAL = 0, } -function unlinkSyncTest() { - console.println("unlinkSyncTest begin"); - try { - fileIo.unlinkSync("/data/local/tmp/a1.txt"); - console.println(`unlink result`); +interface File { + fd: number; + path: String; + name: String; - } catch (error) { - console.error("unlinkSyncTest Error!", error); - } - console.println("unlinkSyncTest end"); + getParent(): String; + lock(exclusive?: boolean): void; + tryLock(exclusive?: boolean): void; + unlock(): void; } -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"); -} +class FileInner implements File { + fd: number = -1; + path: String = ""; + name: String = ""; -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); + private nativePtr: long = 0; + + constructor(ptr: long) { + if (this.nativePtr === 0) { + this.nativePtr = ptr; } - }); - 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"); + + native getParent(): String; + native lock(exclusive?: boolean): void; + native tryLock(exclusive?: boolean): void; + native unlock(): void; + } -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); +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; } - }); - 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); + + native isBlockDevice(): boolean; + native isCharacterDevice(): boolean; + native isDirectory(): boolean; + native isFIFO(): boolean; + native isFile(): boolean; + native isSocket(): boolean; + native isSymbolicLink(): boolean; +} + +class FileIoImpl { + + static { + loadLibrary("ani_fs_class"); } - console.println("errorHandlerTest3 end"); + + static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; + + static native closeSync(file: number | File): void; + + static native copyFileSync(src: string | number, dest: string | number, mode?: number): void; + + static native listFileSync(path: string, options?: ListFileOptions): string[]; + + static native mkdirSync(path: string): void; + + static native mkdirSync(path: string, recursion: boolean): void; + + static native moveFileSync(src: String, dest: String, mode?: number): void; + + static native openSync(path: String, mode?: number): File; + + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; + + static native readTextSync(filePath: string, options?: ReadTextOptions): string; + + static native rmdirSync(path: string): void; + + static native statSync(file: string | number): Stat; + + static native truncateSync(file: string | number, len?: number): void; + + static native unlinkSync(path: string): void; + + static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; + } -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); - } - }); +// 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.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}`); + fileIo.close(file, (err: BusinessError) => { + if (err.code === 0) { + console.log("closeCallbackTest: close success!"); } else { - console.error("open file Error!", err); + console.error("closeCallbackTest: close Error:", err); } }); - console.println("errorHandlerTest4 end"); -} + +// 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(); 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 fc2bd3c059cf7bbb240ffc6149cd549bb8064de4..52ee635891b5765649a72e907fe31b8f97fa41d2 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 468c3c4e4034b2641a95b011de788f98606d52c0..286d181561b3770c4043d3e5c8ee966087c10bd2 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 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 74c9466dceedceb4687788619b81540c23c6ee76..9dfe0fb3ff27cb1896ae8304e28ef3176e6baae0 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 962c25d0f54b1dba0f6b1e5374738ae4dc48e5cc..1458f300135b574026af0180313d70d9b9d8ae6e 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