From 84fb2b728c69c402f0b991836a1b992710f2711a Mon Sep 17 00:00:00 2001 From: WangKe Date: Sun, 27 Apr 2025 07:50:10 +0000 Subject: [PATCH 1/4] =?UTF-8?q?update=20interfaces/kits/js/src/mod=5Ffs/pr?= =?UTF-8?q?operties/ani/read=5Ftext=5Fani.cpp.=20update=20interfaces/kits/?= =?UTF-8?q?js/src/mod=5Ffs/properties/ani/read=5Ftext=5Fani.cpp.=20?= =?UTF-8?q?=E8=AA=BF=E6=95=B4=E9=A1=9E=E8=B7=AF=E5=BE=91=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=20stream=20=E9=80=82?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: WangKe Change-Id: I55f1e33e710d1f8453ba2bb547b7ba26d96e3ae0 --- interfaces/kits/js/BUILD.gn | 15 +- .../mod_environment/ani/environment_ani.cpp | 9 +- .../ani/ets/@ohos.file.environment.ets | 2 +- .../src/mod_environment/environment_core.cpp | 11 +- .../js/src/mod_environment/environment_core.h | 2 - .../js/src/mod_fs/ani/bind_function_class.cpp | 15 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 234 ++++++++++------- .../mod_fs/class_file/ani/file_wrapper.cpp | 2 +- .../ani/randomaccessfile_ani.cpp | 240 +++++++++++++++++- .../ani/randomaccessfile_ani.h | 8 +- .../ani/reader_iterator_ani.cpp | 2 +- .../ani/reader_iterator_result_ani.cpp | 2 +- .../mod_fs/class_stat/ani/stat_wrapper.cpp | 2 +- .../class_stream/ani/stream_wrapper.cpp | 2 +- .../src/mod_fs/properties/ani/close_ani.cpp | 2 +- .../js/src/mod_fs/properties/ani/copy_ani.cpp | 5 +- .../mod_fs/properties/ani/copy_dir_ani.cpp | 2 +- .../ani/create_randomaccessfile_ani.cpp | 4 +- .../src/mod_fs/properties/ani/movedir_ani.cpp | 2 +- .../mod_fs/properties/ani/read_text_ani.cpp | 4 +- .../src/mod_fs/properties/read_text_core.cpp | 3 + .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 2 +- .../ani/ets/@ohos.file.securityLabel.ets | 2 +- 23 files changed, 425 insertions(+), 147 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 80b2a3735..532923e60 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -624,10 +624,9 @@ group("build_kits_js") { group("ani_file_api") { deps = [ - ":ani_fs_class", - ":ani_hash_class", - ":ani_securitylabel_class", - ":ohos_file_fs_abc", + ":ani_file_fs", + ":ani_file_hash", + ":ani_file_securitylabel", ] } @@ -658,7 +657,7 @@ config("ani_config") { ] } -ohos_shared_library("ani_fs_class") { +ohos_shared_library("ani_file_fs") { public_configs = [ ":ani_config" ] include_dirs = [ "include/ipc", @@ -852,7 +851,7 @@ ohos_prebuilt_etc("ohos_file_fs_abc_etc") { deps = [ ":ohos_file_fs_abc" ] } -ohos_shared_library("ani_hash_class") { +ohos_shared_library("ani_file_hash") { public_configs = [ ":ani_config" ] include_dirs = [ "src/mod_hash", @@ -922,7 +921,7 @@ ohos_prebuilt_etc("ohos_file_hash_abc_etc") { deps = [ ":ohos_file_hash_abc" ] } -ohos_shared_library("ani_securitylabel_class") { +ohos_shared_library("ani_file_securitylabel") { public_configs = [ ":ani_config" ] include_dirs = [ "src/mod_securitylabel/ani", @@ -990,7 +989,7 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { deps = [ ":ohos_file_securityLabel_abc" ] } -ohos_shared_library("ani_environment_class") { +ohos_shared_library("ani_file_environment") { public_configs = [ ":ani_config" ] include_dirs = [ "src/mod_environment/ani", diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp index fd3f5c084..da95c9a2a 100644 --- a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -15,9 +15,9 @@ #include "environment_ani.h" +#include "environment_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" -#include "environment_core.h" #include "type_converter.h" namespace OHOS { @@ -46,7 +46,6 @@ ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -67,7 +66,6 @@ ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -88,7 +86,6 @@ ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -109,7 +106,6 @@ ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -130,7 +126,6 @@ ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -151,7 +146,6 @@ ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unuse ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } @@ -172,7 +166,6 @@ ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } - return result; } diff --git a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets index 5155ebd3a..9ccc27495 100644 --- a/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -90,7 +90,7 @@ export default Environment; class EnvironmentImpl { static { - loadLibrary("ani_environment_class"); + loadLibrary("ani_file_environment"); } static native getStorageDataDirSync(): string; diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index c46fe5066..0f86ede13 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -14,6 +14,10 @@ */ #include "environment_core.h" + +#include +#include + #include "accesstoken_kit.h" #include "account_error_no.h" #include "filemgmt_libhilog.h" @@ -21,8 +25,7 @@ #include "os_account_manager.h" #include "parameter.h" #include "tokenid_kit.h" -#include -#include + namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { @@ -125,7 +128,6 @@ FsResult DoGetUserDataDir() auto userDataPath = std::make_shared(); (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); return FsResult::Success(std::move(*userDataPath)); - } FsResult DoGetUserDownloadDir() @@ -141,7 +143,6 @@ FsResult DoGetUserDownloadDir() return FsResult::Error(E_UNKNOWN_ERROR); } return FsResult::Success(std::move(downloadPath)); - } FsResult DoGetUserDesktopDir() @@ -181,7 +182,6 @@ FsResult DoGetExternalStorageDir() return FsResult::Error(res); } return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); - } FsResult DoGetUserHomeDir() @@ -198,7 +198,6 @@ FsResult DoGetUserHomeDir() } std::string result = PC_STORAGE_PATH + userName; return FsResult::Success(std::move(result)); - } } // namespace ModuleEnvironment } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_environment/environment_core.h b/interfaces/kits/js/src/mod_environment/environment_core.h index 2ac81134f..0962c1f4a 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.h +++ b/interfaces/kits/js/src/mod_environment/environment_core.h @@ -23,7 +23,6 @@ namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { using namespace ModuleFileIO; - FsResult DoGetStorageDataDir(); FsResult DoGetUserDataDir(); FsResult DoGetUserDownloadDir(); @@ -31,7 +30,6 @@ FsResult DoGetUserDesktopDir(); FsResult DoGetUserDocumentDir(); FsResult DoGetExternalStorageDir(); FsResult DoGetUserHomeDir(); - } // namespace ModuleEnvironment } // namespace FileManagement } // namespace OHOS 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 6f1f37532..5086029c2 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 @@ -64,7 +64,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindRafFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/RandomAccessFileInner;"; + static const char *className = "L@ohos/file/fs/fileIo/RandomAccessFileInner;"; std::array methods = { ani_native_function { @@ -72,6 +72,9 @@ static ani_status BindRafFileMethods(ani_env *env) ani_native_function { "close", nullptr, reinterpret_cast(RandomAccessFileAni::Close) }, ani_native_function { "writeSync0", nullptr, reinterpret_cast(RandomAccessFileAni::WriteSync) }, ani_native_function { "readSync0", nullptr, reinterpret_cast(RandomAccessFileAni::ReadSync) }, + ani_native_function { "getReadStream", nullptr, reinterpret_cast(RandomAccessFileAni::GetReadStream) }, + ani_native_function { + "getWriteStream", nullptr, reinterpret_cast(RandomAccessFileAni::GetWriteStream) }, }; return BindClass(env, className, methods); @@ -91,7 +94,7 @@ static ani_status BindWatcherClassMethods(ani_env *env) static ani_status BindFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/FileInner;"; + static const char *className = "L@ohos/file/fs/fileIo/FileInner;"; std::array methods = { ani_native_function { "getParent", nullptr, reinterpret_cast(FileAni::GetParent) }, @@ -105,7 +108,7 @@ static ani_status BindFileMethods(ani_env *env) static ani_status BindReaderIteratorMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/ReaderIteratorInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorInner;"; std::array methods = { ani_native_function { "next", nullptr, reinterpret_cast(ReaderIteratorAni::Next) }, @@ -116,7 +119,7 @@ static ani_status BindReaderIteratorMethods(ani_env *env) static ani_status BindStatClassMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/StatInner;"; + static const char *className = "L@ohos/file/fs/fileIo/StatInner;"; std::array methods = { ani_native_function { "isBlockDevice", ":Z", reinterpret_cast(StatAni::IsBlockDevice) }, @@ -133,7 +136,7 @@ static ani_status BindStatClassMethods(ani_env *env) static ani_status BindStreamMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/StreamInner;"; + static const char *className = "L@ohos/file/fs/fileIo/StreamInner;"; std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(StreamAni::Close) }, @@ -148,7 +151,7 @@ static ani_status BindStreamMethods(ani_env *env) static ani_status BindTaskSignalClassMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/TaskSignal;"; + static const char *className = "L@ohos/file/fs/fileIo/TaskSignal;"; std::array methods = { ani_native_function { "cancel", ":V", reinterpret_cast(TaskSignalAni::Cancel) }, 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 c43cc008c..e8fa2c76b 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 @@ -28,7 +28,6 @@ namespace fileIo { export const NOFOLLOW = 0o400000; export const SYNC = 0o4010000; } -} function access(path: string, mode?: AccessModeType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { @@ -489,23 +488,13 @@ function openSync(path: string, mode?: number): File { function open(path: String, mode?: number): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute(FileIoImpl.openSync, path); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(FileIoImpl.openSync, path, mode); - promise.then((ret: NullishType): void => { - let file = ret as File; - resolve(file); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute(FileIoImpl.openSync, path, mode); + promise.then((ret: NullishType): void => { + let file = ret as File; + resolve(file); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } @@ -949,6 +938,19 @@ function fsync(fd: number): Promise { }); } +function fsync(fd: number, callback: AsyncCallback): void { + let promise = taskpool.execute((fd: number): undefined => { + return FileIoImpl.fsyncSync(fd); + }, fd); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + function symlinkSync(target: string, srcPath: string): void { return FileIoImpl.symlinkSync(target, srcPath); } @@ -982,6 +984,20 @@ function rename(oldPath: string, newPath: string): Promise { }); }); } + +function rename(oldPath: string, newPath: string, callback: AsyncCallback): void { + let promise = taskpool.execute((oldPath: string, newPath: string): undefined => { + return FileIoImpl.renameSync(oldPath, newPath); + }, oldPath, newPath); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); + }).catch((e: BusinessError): void => { + callback(e, undefined); + }); +} + function createRandomAccessFileSync(file: string | File, mode?: number, options?: RandomAccessFileOptions): RandomAccessFile { return FileIoImpl.createRandomAccessFileSync(file, mode, options); @@ -1309,35 +1325,6 @@ export interface Filter { 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 Options { - encoding?: string; -} - -export interface WriteOptions extends Options { - offset?: number; - length?: number; -} - -export interface RandomAccessFileOptions { - start?: number; - end?: number; -} - export interface Progress { processedSize: number; totalSize: number; @@ -1347,7 +1334,7 @@ export interface DfsListeners { onStatus(networkId: string, status: number): void; } -class ProgressInner implements Progress { +export class ProgressInner implements Progress { processedSize: number; totalSize: number; @@ -1382,14 +1369,14 @@ export interface CopyOptions { copySignal?: TaskSignal; } -enum AccessModeType { +export enum AccessModeType { EXIST = 0, WRITE = 2, READ = 4, READ_WRITE = 6, } -enum AccessFlagType { +export enum AccessFlagType { LOCAL = 0, } @@ -1407,9 +1394,11 @@ export interface RandomAccessFile { read(buffer: ArrayBuffer, callback: AsyncCallback): void; read(buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void; readSync(buffer: ArrayBuffer, options?: ReadOptions): number; + getReadStream(): ReadStream; + getWriteStream(): WriteStream; } -class RandomAccessFileInner implements RandomAccessFile { +export class RandomAccessFileInner implements RandomAccessFile { fd: number = -1; filePointer: number = -1; @@ -1537,6 +1526,9 @@ class RandomAccessFileInner implements RandomAccessFile { callback(e, 0); }); } + + native getReadStream(): ReadStream; + native getWriteStream(): WriteStream; } export interface File { @@ -1552,7 +1544,7 @@ export interface File { unlock(): void; } -class FileInner implements File { +export class FileInner implements File { fd: number = -1; path: String = ""; name: String = ""; @@ -1623,17 +1615,13 @@ class FileInner implements File { } -enum LocationType { +export enum LocationType { LOCAL = 1, CLOUD = 2 } -export interface ReaderIteratorResult { - done: boolean; - value: string; -} -class ReaderIteratorResultInner implements ReaderIteratorResult { +export class ReaderIteratorResultInner implements ReaderIteratorResult { private nativePtr: long = 0; constructor(ptr: long) { @@ -1650,7 +1638,7 @@ export interface ReaderIterator { next(): ReaderIteratorResult; } -class ReaderIteratorInner implements ReaderIterator { +export class ReaderIteratorInner implements ReaderIterator { private nativePtr: long = 0; constructor(ptr: long) { @@ -1685,7 +1673,7 @@ export interface Stat { isSymbolicLink(): boolean; } -class StatInner implements Stat { +export class StatInner implements Stat { ino: bigint = 0n; mode: number; uid: number; @@ -1734,7 +1722,7 @@ export interface Stream { seek(offset: number, whence?: number): number; } -class StreamInner implements Stream { +export class StreamInner implements Stream { private nativePtr: long = 0; constructor(ptr: long) { @@ -1889,16 +1877,6 @@ class StreamInner implements Stream { native seek(offset: number, whence?: number): number; } -export interface ReadStreamOptions { - start?: number; - end?: number; -} - -export interface WriteStreamOptions { - mode?: number; - start?: number; -} - export class ReadStream extends stream.Readable { private pathInner: string; private bytesReadInner: number; @@ -2058,12 +2036,15 @@ export class WriteStream extends stream.Writable { return modeStr; } } -enum WhenceType { + +export enum WhenceType { SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2 } +} + export interface Watcher { start(): void; stop(): void; @@ -2090,7 +2071,7 @@ export interface WatchEvent { } class WatchEventInner implements WatchEvent { - fileName: string; + fileName: string = ''; event: number; cookie: number; @@ -2104,17 +2085,86 @@ class WatchEventInner implements WatchEvent { export type WatchEventListener = (event: WatchEvent) => void; -class FileIoImpl { +export interface Options { + encoding?: string; +} + +export interface ReadOptions { + offset?: number; + length?: number; +} + +export interface ReadTextOptions extends ReadOptions { + encoding?: string; +} + +export interface WriteOptions extends Options { + offset?: number; + length?: number; +} + +export interface RandomAccessFileOptions { + start?: number; + end?: number; +} + +export interface ListFileOptions { + recursion?: boolean; + listNum?: number; + filter?: Filter; +} + +export interface WriteStreamOptions { + mode?: number; + start?: number; +} + +export interface ReadStreamOptions { + start?: number; + end?: number; +} + +export class ReadStreamOptionsInner implements ReadStreamOptions { + constructor() {} + start?: number; + end?: number; +} + +export class WriteStreamOptionsInner implements WriteStreamOptions { + constructor() {} + mode?: number; + start?: number; +} + +export interface ReaderIteratorResult { + done: boolean; + value: string; +} + +export interface Filter { + suffix?: Array; + displayName?: Array; + mimeType?: Array; + fileSizeOver?: number; + lastModifiedAfter?: number; + excludeMedia?: boolean; +} + +type TaskSignal = fileIo.TaskSignal; +type DfsListeners = fileIo.DfsListeners; + + +export class FileIoImpl { static { - loadLibrary("ani_fs_class"); + loadLibrary("ani_file_fs"); } - static native doAccessSync(path: string, mode?: AccessModeType, flag?: AccessFlagType): boolean; + static native doAccessSync(path: string, mode?: fileIo.AccessModeType, flag?: fileIo.AccessFlagType): boolean; - static native closeSync(file: number | File): void; + static native closeSync(file: number | fileIo.File): void; - static native copySync(srcUri: string, destUri: string, options?: CopyOptions): void; + static native copySync(srcUri: string, destUri: string, options?: fileIo.CopyOptions): void; static native connectDfs(networkId: string, listeners: DfsListeners): void; @@ -2128,19 +2178,19 @@ class FileIoImpl { static native getxattrSync(path: string, key: string): string; - static native createStreamSync(path: string, mode: string): Stream; + static native createStreamSync(path: string, mode: string): fileIo.Stream; static native createWatcherSync(path: string, events: number, listener: WatchEventListener): Watcher; - - static native fdopenStreamSync(fd: number, mode: string): Stream; - - static native dup(fd: number): File; + + static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; + + static native dup(fd: number): fileIo.File; static native listFileSync(path: string, options?: ListFileOptions): string[]; - static native lstatSync(path: string): Stat; - - static native lseekSync(fd: number, offset: number, whence?: WhenceType): number; + static native lstatSync(path: string): fileIo.Stat; + + static native lseekSync(fd: number, offset: number, whence?: fileIo.WhenceType): number; static native mkdirSync(path: string): void; @@ -2152,10 +2202,10 @@ class FileIoImpl { static native moveFileSync(src: String, dest: String, mode?: number): void; - static native openSync(path: String, mode?: number): File; - - static native readlinesSync(filePath: string, options?: Options): ReaderIterator; + static native openSync(path: String, mode?: number): fileIo.File; + static native readlinesSync(filePath: string, options?: Options): fileIo.ReaderIterator; + static native readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): number; static native readTextSync(filePath: string, options?: ReadTextOptions): string; @@ -2164,7 +2214,7 @@ class FileIoImpl { static native setxattrSync(path: string, key: string, value: string): void; - static native statSync(file: string | number): Stat; + static native statSync(file: string | number): fileIo.Stat; static native truncateSync(file: string | number, len?: number): void; @@ -2176,8 +2226,8 @@ class FileIoImpl { static native renameSync(oldPath: string, newPath: string): void; - static native createRandomAccessFileSync(file: string | File, mode?: number, - options?: RandomAccessFileOptions): RandomAccessFile; + static native createRandomAccessFileSync(file: string | fileIo.File, mode?: number, + options?: RandomAccessFileOptions): fileIo.RandomAccessFile; static native symlinkSync(target: string, srcPath: string): void; diff --git a/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp index 047f033a5..544d62f4c 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp @@ -42,7 +42,7 @@ FsFile *FileWrapper::Unwrap(ani_env *env, ani_object object) ani_object FileWrapper::Wrap(ani_env *env, const FsFile *file) { - static const char *className = "L@ohos/file/fs/FileInner;"; + static const char *className = "L@ohos/file/fs/fileIo/FileInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %s", className); diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp index 36f30dad8..fd474b29b 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp @@ -28,6 +28,10 @@ namespace ANI { using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +const int BUF_SIZE = 1024; +const string READ_STREAM_CLASS = "ReadStream"; +const string WRITE_STREAM_CLASS = "WriteStream"; + static FsRandomAccessFile *Unwrap(ani_env *env, ani_object object) { ani_long nativePtr; @@ -160,8 +164,8 @@ void RandomAccessFileAni::Close(ani_env *env, [[maybe_unused]] ani_object object } } -ani_double RandomAccessFileAni::WriteSync(ani_env *env, [[maybe_unused]] ani_object object, - ani_object buf, ani_object options) +ani_double RandomAccessFileAni::WriteSync( + ani_env *env, [[maybe_unused]] ani_object object, ani_object buf, ani_object options) { auto rafFile = Unwrap(env, object); if (rafFile == nullptr) { @@ -215,8 +219,8 @@ ani_double RandomAccessFileAni::WriteSync(ani_env *env, [[maybe_unused]] ani_obj return -1; } -ani_double RandomAccessFileAni::ReadSync(ani_env *env, [[maybe_unused]] ani_object object, - ani_arraybuffer buf, ani_object options) +ani_double RandomAccessFileAni::ReadSync( + ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buf, ani_object options) { auto rafFile = Unwrap(env, object); if (rafFile == nullptr) { @@ -239,7 +243,7 @@ ani_double RandomAccessFileAni::ReadSync(ani_env *env, [[maybe_unused]] ani_obje return -1; } - auto ret = rafFile-> ReadSync(arrayBuffer, op); + auto ret = rafFile->ReadSync(arrayBuffer, op); if (!ret.IsSuccess()) { HILOGE("Read file content failed!"); const auto &err = ret.GetError(); @@ -249,6 +253,232 @@ ani_double RandomAccessFileAni::ReadSync(ani_env *env, [[maybe_unused]] ani_obje return static_cast(ret.GetData().value()); } +static ani_string GetFilePath(ani_env *env, const int fd) +{ + auto dstFd = dup(fd); + if (dstFd < 0) { + HILOGE("Failed to get valid fd, fail reason: %{public}s, fd: %{public}d", strerror(errno), fd); + return nullptr; + } + + string path = "/proc/self/fd/" + to_string(dstFd); + auto buf = CreateUniquePtr(BUF_SIZE); + int readLinkRes = readlink(path.c_str(), buf.get(), BUF_SIZE); + if (readLinkRes < 0) { + close(dstFd); + return nullptr; + } + + close(dstFd); + auto [succ, filePath] = TypeConverter::ToAniString(env, string(buf.get())); + if (!succ) { + return nullptr; + } + return move(filePath); +} + +static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t end) +{ + static const char *className = "L@ohos/file/fs/ReadStreamOptionsInner;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { + // if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + ani_field startField = nullptr; + ani_field endField = nullptr; + if (ANI_OK != env->Class_FindField(cls, "start", &startField)) { + HILOGE("Cannot find start in class %s", className); + return nullptr; + } + if (ANI_OK != env->Class_FindField(cls, "end", &endField)) { + HILOGE("Cannot find end in class %s", className); + return nullptr; + } + + if (start >= 0) { + env->Object_SetField_Int(obj, startField, start); + } + if (end >= 0) { + env->Object_SetField_Int(obj, endField, end); + } + if (obj == nullptr) { + HILOGE("CreateReadStreamOptions is nullptr"); + } + + return move(obj); +} + +static ani_object CreateWriteStreamOptions(ani_env *env, int64_t start, int flags) +{ + static const char *className = "L@ohos/file/fs/WriteStreamOptionsInner;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + ani_field modeField = nullptr; + ani_field startField = nullptr; + if (ANI_OK != env->Class_FindField(cls, "mode", &modeField)) { + HILOGE("Cannot find mode in class %s", className); + return nullptr; + } + if (ANI_OK != env->Class_FindField(cls, "start", &startField)) { + HILOGE("Cannot find start in class %s", className); + return nullptr; + } + + env->Object_SetField_Int(obj, modeField, flags); + if (start >= 0) { + env->Object_SetField_Int(obj, startField, start); + } + + return move(obj); +} + +static ani_object CreateReadStream(ani_env *env, ani_string filePath, ani_object options) +{ + static const char *className = "L@ohos/file/fs/fileIo/ReadStream;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;L@ohos/file/fs/ReadStreamOptions;:V", &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath, options)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + return move(obj); +} + +static ani_object CreateWriteStream(ani_env *env, ani_string filePath, ani_object options) +{ + static const char *className = "L@ohos/file/fs/fileIo/WriteStream;"; + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + HILOGE("Cannot find class %s", className); + return nullptr; + } + ani_method ctor; + if (ANI_OK != + env->Class_FindMethod(cls, "", "Lstd/core/String;L@ohos/file/fs/WriteStreamOptions;:V", &ctor)) { + HILOGE("Cannot find constructor method for class %s", className); + return nullptr; + } + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, filePath, options)) { + HILOGE("New %s obj Failed", className); + return nullptr; + } + + return move(obj); +} + +static ani_object CreateStream(ani_env *env, const string &streamName, RandomAccessFileEntity *rafEntity, int flags) +{ + ani_string filePath = GetFilePath(env, rafEntity->fd.get()->GetFD()); + if (!filePath) { + HILOGE("Get file path failed, errno=%{public}d", errno); + ErrorHandler::Throw(env, errno); + return nullptr; + } + + if (streamName == READ_STREAM_CLASS) { + ani_object obj = CreateReadStreamOptions(env, rafEntity->start, rafEntity->end); + return CreateReadStream(env, filePath, obj); + } + if (streamName == WRITE_STREAM_CLASS) { + ani_object obj = CreateWriteStreamOptions(env, rafEntity->start, flags); + return CreateWriteStream(env, filePath, obj); + } + + return nullptr; +} + +ani_object RandomAccessFileAni::GetReadStream(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto rafFile = Unwrap(env, object); + if (rafFile == nullptr) { + HILOGE("Cannot unwrap rafFile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto entity = rafFile->GetRAFEntity(); + if (!entity) { + HILOGE("Get RandomAccessFileEntity failed!"); + ErrorHandler::Throw(env, EIO); + return nullptr; + } + + int flags = fcntl(entity->fd.get()->GetFD(), F_GETFL); + unsigned int uflags = static_cast(flags); + if (((uflags & O_ACCMODE) != O_RDONLY) && ((uflags & O_ACCMODE) != O_RDWR)) { + HILOGE("Failed to check Permission"); + ErrorHandler::Throw(env, EACCES); + return nullptr; + } + + return CreateStream(env, READ_STREAM_CLASS, entity, flags); +} + +ani_object RandomAccessFileAni::GetWriteStream(ani_env *env, [[maybe_unused]] ani_object object) +{ + auto rafFile = Unwrap(env, object); + if (rafFile == nullptr) { + HILOGE("Cannot unwrap rafFile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + auto entity = rafFile->GetRAFEntity(); + if (!entity) { + HILOGE("Get RandomAccessFileEntity failed!"); + ErrorHandler::Throw(env, EIO); + return nullptr; + } + + int flags = fcntl(entity->fd.get()->GetFD(), F_GETFL); + unsigned int uflags = static_cast(flags); + if (((uflags & O_ACCMODE) != O_RDONLY) && ((uflags & O_ACCMODE) != O_RDWR)) { + HILOGE("Failed to check Permission"); + ErrorHandler::Throw(env, EACCES); + return nullptr; + } + + return CreateStream(env, READ_STREAM_CLASS, entity, flags); +} + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h index 7fc783bd5..d1a29d8ec 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.h @@ -28,11 +28,13 @@ public: static void SetFilePointer(ani_env *env, [[maybe_unused]] ani_object object, ani_double fp); static void Close(ani_env *env, [[maybe_unused]] ani_object object); static ani_double WriteSync(ani_env *env, [[maybe_unused]] ani_object object, ani_object buf, ani_object options); - static ani_double ReadSync(ani_env *env, [[maybe_unused]] ani_object object, - ani_arraybuffer buf, ani_object options); + static ani_double ReadSync( + ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buf, ani_object options); + static ani_object GetReadStream(ani_env *env, [[maybe_unused]] ani_object object); + static ani_object GetWriteStream(ani_env *env, [[maybe_unused]] ani_object object); }; } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -#endif //INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_ANI_RANDOMACCESSFILE_ANI_H \ No newline at end of file +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_ANI_RANDOMACCESSFILE_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp index b5f8b55b9..f65189add 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp @@ -29,7 +29,7 @@ using namespace std; ani_object ReaderIteratorAni::Wrap(ani_env *env, const FsReaderIterator *it) { - static const char *className = "L@ohos/file/fs/ReaderIteratorInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp index 0ea8f121b..a94c62e22 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp @@ -28,7 +28,7 @@ using namespace std; ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResult *result) { - static const char *className = "L@ohos/file/fs/ReaderIteratorResultInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorResultInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp index 02a9bca9f..ed6c02eb3 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp @@ -147,7 +147,7 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & tuple StatWrapper::Wrap(ani_env *env, FsStat *fsStat) { ani_object statObject = {}; - static const char *className = "L@ohos/file/fs/StatInner;"; + static const char *className = "L@ohos/file/fs/fileIo/StatInner;"; ani_class cls; ani_status ret; diff --git a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp index 31ff9ec26..2f03fb78e 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp @@ -43,7 +43,7 @@ FsStream *StreamWrapper::Unwrap(ani_env *env, ani_object object) ani_object StreamWrapper::Wrap(ani_env *env, const FsStream *stream) { - static const char *className = "L@ohos/file/fs/StreamInner;"; + static const char *className = "L@ohos/file/fs/fileIo/StreamInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %s", className); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index f49ccf2f2..7de532c82 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -48,7 +48,7 @@ tuple ParseFd(ani_env *env, ani_object obj) } ani_class FileClass; - env->FindClass("L@ohos/file/fs/FileInner;", &FileClass); + env->FindClass("L@ohos/file/fs/fileIo/FileInner;", &FileClass); ani_boolean isFile; env->Object_InstanceOf(obj, FileClass, &isFile); if (isFile) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp index 1f9b713c3..003003d7d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp @@ -37,7 +37,7 @@ static void SetProgressListenerCb(ani_env *env, ani_ref &callback, CopyOptions & opts.listenerCb = [vm, &callback](uint64_t progressSize, uint64_t totalSize) -> void { ani_status ret; ani_object progress = {}; - static const char *className = "L@ohos/file/fs/ProgressInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ProgressInner;"; ani_class cls; auto env = AniHelper::GetThreadEnv(vm); @@ -47,7 +47,8 @@ static void SetProgressListenerCb(ani_env *env, ani_ref &callback, CopyOptions & } if (progressSize > MAX_VALUE || totalSize > MAX_VALUE) { - HILOGE("progressSize or totalSize exceed MAX_VALUE: %{private}llu %{private}llu", progressSize, totalSize); + HILOGE("progressSize or totalSize exceed MAX_VALUE: %{private}" PRIu64 " %{private}" PRIu64, progressSize, + totalSize); } if ((ret = env->FindClass(className, &cls)) != ANI_OK) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp index 200573ca1..cfb210a51 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp @@ -30,7 +30,7 @@ using namespace OHOS::FileManagement::ModuleFileIO; static tuple ToConflictFiles(ani_env *env, const ConflictFiles &files) { - static const char *className = "L@ohos/file/fs/ConflictFilesInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ConflictFilesInner;"; ani_class cls; ani_status ret; if ((ret = env->FindClass(className, &cls)) != ANI_OK) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp index 3b7226648..677fde168 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp @@ -31,7 +31,7 @@ using namespace OHOS::FileManagement::ModuleFileIO; static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) { - static const char *className = "L@ohos/file/fs/RandomAccessFileInner;"; + static const char *className = "L@ohos/file/fs/fileIo/RandomAccessFileInner;"; ani_class cls; if (ANI_OK != env->FindClass(className, &cls)) { HILOGE("Cannot find class %s", className); @@ -82,7 +82,7 @@ static tuple JudgeFile(ani_env *env, ani_object obj) } ani_class fileClass; - env->FindClass("L@ohos/file/fs/FileInner;", &fileClass); + env->FindClass("L@ohos/file/fs/fileIo/FileInner;", &fileClass); ani_boolean isFile = false; env->Object_InstanceOf(obj, fileClass, &isFile); if (isFile) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp index e1239cfca..beec317d3 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp @@ -28,7 +28,7 @@ namespace ANI { static tuple ToConflictFiles(ani_env *env, const ErrFiles &files) { - static const char *className = "L@ohos/file/fs/ConflictFilesInner;"; + static const char *className = "L@ohos/file/fs/fileIo/ConflictFilesInner;"; ani_class cls; ani_status ret; if ((ret = env->FindClass(className, &cls)) != ANI_OK) { 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 7e2fe63ff..05689d3d7 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 @@ -91,8 +91,8 @@ ani_string ReadTextAni::ReadTextSync( } const auto &resText = ret.GetData().value(); - string res = std::get<0>(resText); - size_t size = std::get<1>(resText); + string res = std::get<0>(resText); // get buffer + size_t size = std::get<1>(resText); // get buffer length auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp index 90b1b7109..d62cf1640 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp @@ -124,13 +124,16 @@ FsResult> ReadTextCore::DoReadText(const std::string &pat len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; string buffer(len, '\0'); + // HILOGE("TEST: before ReadFromFile res.length: %{public}d", buffer.length()); int readRet = ReadFromFile(sfd.GetFD(), offset, buffer); if (readRet < 0) { HILOGE("Failed to read file by fd: %{public}d", fd); return FsResult>::Error(readRet); } + // HILOGE("TEST: after ReadFromFile res.length: %{public}d", buffer.length()); return FsResult>::Success(make_tuple(move(buffer), readRet)); + // return FsResult>::Success(make_tuple(buffer.c_str(), readRet)); } } // namespace ModuleFileIO 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 3c4fd40dc..dfa16ab00 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 @@ -86,7 +86,7 @@ export default namespace hash { class HashImpl { static { - loadLibrary("ani_hash_class"); + loadLibrary("ani_file_hash"); } static native hashSync(path: string, algorithm: string): string; 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 b4268c611..06e659dc2 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 @@ -78,7 +78,7 @@ export default securityLabel; class SecurityLabelImpl { static { - loadLibrary("ani_securitylabel_class"); + loadLibrary("ani_file_securitylabel"); } static native setSecurityLabelSync(path: string, type: securityLabel.DataLevel): void; -- Gitee From a0b36be64f1393824529f14e50e367cc1ecf20dc Mon Sep 17 00:00:00 2001 From: WangKe Date: Mon, 28 Apr 2025 22:48:33 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A=20?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=B3=A8=E9=87=8A=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D=E9=A3=8E=E6=A0=BC=E4=B8=BA?= =?UTF-8?q?g=5F=E5=89=8D=E7=BC=80=E7=9A=84=E5=B0=8F=E9=A9=BC=E5=B3=B0=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D=20statvf?= =?UTF-8?q?s=E6=8E=A5=E5=8F=A3=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: WangKe Change-Id: I2fc7362a41e85054613e9beba5a1debfb7f050f2 --- interfaces/kits/js/BUILD.gn | 69 +++++++++++++++ .../src/mod_environment/environment_core.cpp | 4 +- .../ani/randomaccessfile_ani.cpp | 1 - .../src/mod_fs/properties/read_text_core.cpp | 3 - .../mod_statvfs/ani/bind_function_class.cpp | 60 +++++++++++++ .../ani/ets/@ohos.file.statvfs.ets | 86 +++++++++++++++++++ .../js/src/mod_statvfs/ani/statvfs_ani.cpp | 75 ++++++++++++++++ .../kits/js/src/mod_statvfs/ani/statvfs_ani.h | 37 ++++++++ .../kits/js/src/mod_statvfs/statvfs_core.cpp | 52 +++++++++++ .../kits/js/src/mod_statvfs/statvfs_core.h | 35 ++++++++ 10 files changed, 416 insertions(+), 6 deletions(-) create mode 100644 interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp create mode 100644 interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets create mode 100644 interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.cpp create mode 100644 interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.h create mode 100644 interfaces/kits/js/src/mod_statvfs/statvfs_core.cpp create mode 100644 interfaces/kits/js/src/mod_statvfs/statvfs_core.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 532923e60..88d8ff491 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -627,6 +627,7 @@ group("ani_file_api") { ":ani_file_fs", ":ani_file_hash", ":ani_file_securitylabel", + ":ani_file_statvfs", ] } @@ -1053,3 +1054,71 @@ ohos_prebuilt_etc("ohos_file_environment_abc_etc") { part_name = "file_api" deps = [ ":ohos_file_environment_abc" ] } + +ohos_shared_library("ani_file_statvfs") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_statvfs/ani", + "src/mod_statvfs", + ] + sources = [ + "src/common/ani_helper/error_handler.cpp", + "src/common/ani_helper/type_converter.cpp", + "src/common/file_helper/fd_guard.cpp", + "src/mod_fs/fs_utils.cpp", + "src/mod_statvfs/ani/bind_function_class.cpp", + "src/mod_statvfs/ani/statvfs_ani.cpp", + "src/mod_statvfs/statvfs_core.cpp", + ] + + deps = [ + ":ohos_file_statvfs_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "hilog:libhilog", + "libuv:uv", + "runtime_core:ani", + "runtime_core:libarkruntime", + ] + use_exceptions = true + cflags = [ + "-fvisibility=hidden", + "-fdata-sections", + "-ffunction-sections", + "-Oz", + ] + cflags_cc = [ + "-fvisibility-inlines-hidden", + "-Oz", + ] + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + output_extension = "so" + subsystem_name = "filemanagement" + part_name = "file_api" +} + +generate_static_abc("ohos_file_statvfs_abc") { + base_url = "./src/mod_statvfs/ani/ets" + files = [ "./src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_statvfs_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_statvfs_abc_etc") { + source = "$target_out_dir/ohos_file_statvfs_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_statvfs_abc" ] +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index 0f86ede13..f13e9b942 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -42,7 +42,7 @@ const std::string DOWNLOAD_PATH = "/Download"; const std::string DESKTOP_PATH = "/Desktop"; const std::string DOCUMENTS_PATH = "/Documents"; const std::string DEFAULT_USERNAME = "currentUser"; -const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +const char *FILE_MANAMER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; static bool IsSystemApp() { uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); @@ -79,7 +79,7 @@ static std::string GetPublicPath(const std::string &directoryName) static bool CheckFileManagerFullMountEnable() { char value[] = "false"; - int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + int retSystem = GetParameter(FILE_MANAMER_FULL_MOUNT_ENABLE_PARAMETER, "false", value, sizeof(value)); if (retSystem > 0 && !std::strcmp(value, "true")) { return true; } diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp index fd474b29b..51c130c07 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp @@ -287,7 +287,6 @@ static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t e } ani_method ctor; if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { - // if (ANI_OK != env->Class_FindMethod(cls, "", nullptr, &ctor)) { HILOGE("Cannot find constructor method for class %s", className); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp index d62cf1640..90b1b7109 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp @@ -124,16 +124,13 @@ FsResult> ReadTextCore::DoReadText(const std::string &pat len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; string buffer(len, '\0'); - // HILOGE("TEST: before ReadFromFile res.length: %{public}d", buffer.length()); int readRet = ReadFromFile(sfd.GetFD(), offset, buffer); if (readRet < 0) { HILOGE("Failed to read file by fd: %{public}d", fd); return FsResult>::Error(readRet); } - // HILOGE("TEST: after ReadFromFile res.length: %{public}d", buffer.length()); return FsResult>::Success(make_tuple(move(buffer), readRet)); - // return FsResult>::Success(make_tuple(buffer.c_str(), readRet)); } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp new file mode 100644 index 000000000..aed652923 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "bind_function.h" +#include "statvfs_ani.h" + +using namespace OHOS::FileManagement::ModuleFileIO::ANI; + +static ani_status BindStaticMethods(ani_env *env) +{ + static const char *className = "L@ohos/file/statvfs/StatvfsImpl;"; + + std::array methods = { + ani_native_function { "getFreeSizeSync", nullptr, reinterpret_cast(StatvfsAni::GetFreeSizeSync) }, + ani_native_function { "getTotalSizeSync", nullptr, reinterpret_cast(StatvfsAni::GetTotalSizeSync) }, + }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + if ((status = BindStaticMethods(env)) != ANI_OK) { + HILOGE("Cannot bind native static methods for statvfs!"); + return status; + }; + + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets new file mode 100644 index 000000000..831bc046d --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/ani/ets/@ohos.file.statvfs.ets @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BusinessError, AsyncCallback } from '@ohos.base'; + +namespace statfs { + export function getFreeSizeSync(path: string): number { + return StatvfsImpl.getFreeSizeSync(path); + } + + export function getFreeSize(path: string): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(StatvfsImpl.getFreeSizeSync, path); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function getFreeSize(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(StatvfsImpl.getFreeSizeSync, path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); + }); + } + + export function getTotalSizeSync(path: string): number { + return StatvfsImpl.getTotalSizeSync(path); + } + + export function getTotalSize(path: string): Promise { + return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function getTotalSize(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute(StatvfsImpl.getTotalSizeSync, path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as number; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, 0); + }); + } +} + +export default statfs; + +class StatvfsImpl { + + static { + loadLibrary("ani_file_statvfs"); + } + + static native getFreeSizeSync(path: string): number; + static native getTotalSizeSync(path: string): number; +} \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.cpp b/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.cpp new file mode 100644 index 000000000..337b78f84 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "statvfs_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "statvfs_core.h" +#include "type_converter.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleStatvfs; + +ani_double StatvfsAni::GetFreeSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +{ + auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return 0; + } + + auto ret = StatvfsCore::DoGetFreeSize(srcPath); + if (!ret.IsSuccess()) { + HILOGE("Get freesize failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return 0; + } + + return static_cast(ret.GetData().value()); +} + +ani_double StatvfsAni::GetTotalSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +{ + auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return 0; + } + + auto ret = StatvfsCore::DoGetTotalSize(srcPath); + if (!ret.IsSuccess()) { + HILOGE("Get totalsize failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return 0; + } + + return static_cast(ret.GetData().value()); +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.h b/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.h new file mode 100644 index 000000000..1eaf5081f --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/ani/statvfs_ani.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_STATVFS_ANI_STATVFS_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_STATVFS_ANI_STATVFS_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class StatvfsAni final { +public: + static ani_double GetFreeSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); + static ani_double GetTotalSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_STATVFS_ANI_STATVFS_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/statvfs_core.cpp b/interfaces/kits/js/src/mod_statvfs/statvfs_core.cpp new file mode 100644 index 000000000..7a9aa9db6 --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_core.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "statvfs_core.h" + +#include +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleStatvfs { +using namespace std; + +FsResult StatvfsCore::DoGetFreeSize(const string &path) +{ + struct statvfs diskInfo; + int ret = statvfs(path.c_str(), &diskInfo); + if (ret != 0) { + return FsResult::Error(errno); + } + unsigned long long freeSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_bfree); + return FsResult::Success(static_cast(freeSize)); +} + +FsResult StatvfsCore::DoGetTotalSize(const string &path) +{ + struct statvfs diskInfo; + int ret = statvfs(path.c_str(), &diskInfo); + if (ret != 0) { + return FsResult::Error(errno); + } + unsigned long long totalSize = static_cast(diskInfo.f_bsize) * + static_cast(diskInfo.f_blocks); + return FsResult::Success(static_cast(totalSize)); +} + +} // namespace ModuleStatfs +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_statvfs/statvfs_core.h b/interfaces/kits/js/src/mod_statvfs/statvfs_core.h new file mode 100644 index 000000000..24817f3eb --- /dev/null +++ b/interfaces/kits/js/src/mod_statvfs/statvfs_core.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_STATVFS_STATVFS_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_STATVFS_STATVFS_CORE_H + +#include "filemgmt_libfs.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleStatvfs { +using namespace ModuleFileIO; + +class StatvfsCore final { +public: + static FsResult DoGetFreeSize(const string &path); + static FsResult DoGetTotalSize(const string &path); +}; + +} // namespace ModuleStatvfs +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_STATVFS_STATVFS_CORE_H \ No newline at end of file -- Gitee From 360c43bb08311ab5cda2f48f711123e395f85c75 Mon Sep 17 00:00:00 2001 From: WangKe Date: Thu, 8 May 2025 15:18:29 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=20build.gn=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0environment=20update=20interfaces/kits/js/src/mod=5Ffs?= =?UTF-8?q?/class=5Fstream/fs=5Fstream.cpp.=20update=20interfaces/kits/js/?= =?UTF-8?q?BUILD.gn.=20close=E5=85=B3=E9=97=AD=E5=90=8E=E5=A2=9E=E5=8A=A0f?= =?UTF-8?q?ile=E5=A4=84=E7=90=86=20ets=E6=96=87=E4=BB=B6=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: WangKe Change-Id: I57f31649405c89398785d395da9c6f5b077f05a0 --- interfaces/kits/js/BUILD.gn | 1 + .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 621 ++++++++---------- .../kits/js/src/mod_fs/class_file/fs_file.cpp | 10 + .../kits/js/src/mod_fs/class_file/fs_file.h | 1 + .../js/src/mod_fs/class_stream/fs_stream.cpp | 2 +- .../src/mod_fs/properties/ani/close_ani.cpp | 44 +- .../js/src/mod_fs/properties/close_core.cpp | 18 + .../js/src/mod_fs/properties/close_core.h | 2 + 8 files changed, 315 insertions(+), 384 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 88d8ff491..d107b8d59 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -624,6 +624,7 @@ group("build_kits_js") { group("ani_file_api") { deps = [ + ":ani_file_environment", ":ani_file_fs", ":ani_file_hash", ":ani_file_securitylabel", 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 e8fa2c76b..297cc036e 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 @@ -31,27 +31,15 @@ namespace fileIo { function access(path: string, mode?: AccessModeType): Promise { return new Promise((resolve: (result: boolean) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute((path: string): boolean => { - return FileIoImpl.doAccessSync(path); - }, path); - promise.then((ret: NullishType) => { - let result = ret as boolean; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((path: string, mode: AccessModeType): boolean => { - return FileIoImpl.doAccessSync(path, mode); - }, path, mode); - promise.then((ret: NullishType) => { - let result = ret as boolean; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((path: string, mode?: AccessModeType): boolean => { + return FileIoImpl.doAccessSync(path, mode); + }, path, mode); + promise.then((ret: NullishType): void => { + let result = ret as boolean; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } @@ -71,7 +59,9 @@ function access(path: string, callback: AsyncCallback): void { 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); + let promise = taskpool.execute((path: string, mode: AccessModeType, flag: AccessFlagType): boolean => { + return FileIoImpl.doAccessSync(path, mode, flag); + }, path, mode, flag); promise.then((ret: NullishType): void => { let result = ret as boolean; resolve(result); @@ -144,7 +134,9 @@ function getxattrSync(path: string, key: string): string { function getxattr(path: string, key: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute(FileIoImpl.getxattrSync, path, key); + let promise = taskpool.execute((path: string, key: string): string => { + return FileIoImpl.getxattrSync(path, key); + }, path, key); promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); @@ -163,25 +155,15 @@ function copyDirSync(src: string, dest: string, mode?: number): void { } function copyDir(src: string, dest: string, mode?: number): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError>) => void): void => { - if (mode === undefined) { - let promise = taskpool.execute((src: string, dest: string): undefined => - FileIoImpl.copyDirSync(src, dest), src, dest); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((src: string, dest: string, mode: number): undefined => - FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); - }); - } - + return new Promise((resolve: (result: undefined) => void, + reject: (e: BusinessError>) => void): void => { + let promise = taskpool.execute((src: string, dest: string, mode?: number): undefined => + FileIoImpl.copyDirSync(src, dest, mode), src, dest, mode); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError>): void => { + reject(e); + }); }); } @@ -329,25 +311,14 @@ function moveDirSync(src: string, dest: string, mode?: number): void { function moveDir(src: string, dest: string, mode?: number): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError>) => void): void => { - if (mode === undefined) { - let promise = taskpool.execute((src: string, dest: string): undefined => { - return FileIoImpl.movedirSync(src, dest); - }, src, dest); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((src: string, dest: string, mode: number): undefined => { - return FileIoImpl.movedirSync(src, dest, mode); - }, src, dest, mode); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError>): void => { - reject(e); - }); - } + let promise = taskpool.execute((src: string, dest: string, mode?: number): undefined => { + return FileIoImpl.movedirSync(src, dest, mode); + }, src, dest, mode); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError>): void => { + reject(e); + }); }) } @@ -406,7 +377,9 @@ function mkdtempSync(prefix: string): string { function mkdtemp(prefix: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { - let promise = taskpool.execute(FileIoImpl.mkdtempSync, prefix); + let promise = taskpool.execute((prefix: string): string => { + return FileIoImpl.mkdtempSync(prefix); + }, prefix); promise.then((ret: NullishType): void => { let result = ret as string; resolve(result); @@ -417,7 +390,9 @@ function mkdtemp(prefix: string): Promise { } function mkdtemp(prefix: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.mkdtempSync, prefix); + let promise = taskpool.execute((prefix: string): string => { + return FileIoImpl.mkdtempSync(prefix); + }, prefix); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -434,25 +409,14 @@ function moveFileSync(src: string, dest: string, mode?: number): void { function moveFile(src: string, dest: string, mode?: number): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - if (mode === undefined) { - let promise = taskpool.execute((src: string, dest: string): undefined => { - return FileIoImpl.moveFileSync(src, dest); - }, src, dest); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((src: string, dest: string, mode: number): undefined => { - return FileIoImpl.moveFileSync(src, dest, mode); - }, src, dest, mode); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((src: string, dest: string, mode?: number): undefined => { + return FileIoImpl.moveFileSync(src, dest, mode); + }, src, dest, mode); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } @@ -488,7 +452,9 @@ function openSync(path: string, mode?: number): File { function open(path: String, mode?: number): Promise { return new Promise((resolve: (result: File) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.openSync, path, mode); + let promise = taskpool.execute((path: String, mode?: number): File => { + return FileIoImpl.openSync(path, mode); + },path, mode); promise.then((ret: NullishType): void => { let file = ret as File; resolve(file); @@ -499,7 +465,9 @@ function open(path: String, mode?: number): Promise { } function open(path: String, mode: number, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.openSync, path, mode); + let promise = taskpool.execute((path: String, mode: number): File => { + return FileIoImpl.openSync(path, mode); + }, path, mode); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -512,7 +480,9 @@ function open(path: String, mode: number, callback: AsyncCallback): } function open(path: String, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.openSync, path); + let promise = taskpool.execute((path: String): File => { + return FileIoImpl.openSync(path); + }, path); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -530,29 +500,23 @@ function writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOpti function write(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number => { + return FileIoImpl.writeSync(fd, buffer, options); + }, fd, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer, options); + let promise = taskpool.execute((fd: number, buffer: string | ArrayBuffer, options: WriteOptions): number => { + return FileIoImpl.writeSync(fd, buffer, options); + }, fd, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -564,7 +528,9 @@ function write(fd: number, buffer: string | ArrayBuffer, options: WriteOptions, } function write(fd: number, buffer: string | ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.writeSync, fd, buffer); + let promise = taskpool.execute((fd: number, buffer: string | ArrayBuffer): number => { + return FileIoImpl.writeSync(fd, buffer); + }, fd, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -581,32 +547,22 @@ function readSync(fd: number, buffer: ArrayBuffer, options?: ReadOptions): numbe function read(fd: number, buffer: ArrayBuffer, options?: ReadOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute((fd: number, buffer: ArrayBuffer): number => { - return FileIoImpl.readSync(fd, buffer); - }, fd, buffer); - promise.then((ret: NullishType) => { - let result = ret as number; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((fd: number, buffer: ArrayBuffer, options: ReadOptions): number => { - return FileIoImpl.readSync(fd, buffer, options) - }, fd, buffer, options); - promise.then((ret: NullishType) => { - let result = ret as number; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer, options?: ReadOptions): number => { + return FileIoImpl.readSync(fd, buffer, options) + }, fd, buffer, options); + promise.then((ret: NullishType) => { + let result = ret as number; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer); + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer): number => { + return FileIoImpl.readSync(fd, buffer); + }, fd, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -618,7 +574,9 @@ function read(fd: number, buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readSync, fd, buffer, options); + let promise = taskpool.execute((fd: number, buffer: ArrayBuffer, options: ReadOptions): number => { + return FileIoImpl.readSync(fd, buffer, options); + }, fd, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -634,29 +592,24 @@ function readLinesSync(filePath: string, options?: Options): ReaderIterator { } function readLines(filePath: string, options?: Options): Promise { - return new Promise((resolve: (result: ReaderIterator) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(FileIoImpl.readlinesSync, filePath); - promise.then((ret: NullishType): void => { - let it = ret as ReaderIterator; - resolve(it); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(FileIoImpl.readlinesSync, filePath, options); - promise.then((ret: NullishType): void => { - let it = ret as ReaderIterator; - resolve(it); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + return new Promise((resolve: (result: ReaderIterator) => void, + reject: (e: BusinessError) => void) => { + let promise = taskpool.execute((filePath: string, options?: Options): ReaderIterator => { + return FileIoImpl.readlinesSync(filePath, options); + }, filePath, options); + promise.then((ret: NullishType): void => { + let it = ret as ReaderIterator; + resolve(it); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function readLines(filePath: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readlinesSync, filePath); + let promise = taskpool.execute((filePath: string): ReaderIterator => { + return FileIoImpl.readlinesSync(filePath); + }, filePath); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -669,7 +622,9 @@ function readLines(filePath: string, callback: AsyncCallback): v } function readLines(filePath: string, options: Options, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readlinesSync, filePath, options); + let promise = taskpool.execute((filePath: string, options: Options): ReaderIterator => { + return FileIoImpl.readlinesSync(filePath, options); + }, filePath, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -714,25 +669,14 @@ function truncateSync(file: string | number, len?: number): void { function truncate(file: string | number, len?: number): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - if (len === undefined) { - let promise = taskpool.execute((file: string | number): undefined => { - return FileIoImpl.truncateSync(file); - }, file); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((file: string | number, len: number): undefined => { - return FileIoImpl.truncateSync(file, len); - }, file, len); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((file: string | number, len?: number): undefined => { + return FileIoImpl.truncateSync(file, len); + }, file, len); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); }) } @@ -791,28 +735,22 @@ function unlink(path: string, callback: AsyncCallback): void { function readText(filePath: string, options?: ReadTextOptions): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); - promise.then((ret: NullishType): void => { - let r = ret as string; - resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); - promise.then((ret: NullishType): void => { - let r = ret as string; - resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((filePath: string, options?: ReadTextOptions): string => { + return FileIoImpl.readTextSync(filePath, options); + }, filePath, options); + promise.then((ret: NullishType): void => { + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function readText(filePath: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readTextSync, filePath); + let promise = taskpool.execute((filePath: string): string => { + return FileIoImpl.readTextSync(filePath); + }, filePath); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -824,7 +762,9 @@ function readText(filePath: string, callback: AsyncCallback): void { } function readText(filePath: string, options: ReadTextOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.readTextSync, filePath, options); + let promise = taskpool.execute((filePath: string, options: ReadTextOptions): string => { + return FileIoImpl.readTextSync(filePath, options); + }, filePath, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -841,28 +781,22 @@ function readTextSync(filePath: string, options?: ReadTextOptions): string { function listFile(path: string, options?: ListFileOptions): Promise { return new Promise((resolve: (result: string[]) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(FileIoImpl.listFileSync, path); - promise.then((ret: NullishType): void => { - let r = ret as string[]; - resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); - promise.then((ret: NullishType): void => { - let r = ret as string[]; - resolve(r); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((path: string, options?: ListFileOptions): string[] => { + return FileIoImpl.listFileSync(path, options); + }, path, options); + promise.then((ret: NullishType): void => { + let r = ret as string[]; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function listFile(path: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.listFileSync, path); + let promise = taskpool.execute((path: string): string[] => { + return FileIoImpl.listFileSync(path); + }, path); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -874,7 +808,9 @@ function listFile(path: string, callback: AsyncCallback): void { } function listFile(path: string, options: ListFileOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.listFileSync, path, options); + let promise = taskpool.execute((path: string, options: ListFileOptions): string[] => { + return FileIoImpl.listFileSync(path, options); + }, path, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -899,7 +835,9 @@ function statSync(file: string | number): Stat { function stat(file: string | number): Promise { return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.statSync, file); + let promise = taskpool.execute((file: string | number): Stat => { + return FileIoImpl.statSync(file); + }, file); promise.then((ret: NullishType): void => { let r = ret as Stat; resolve(r); @@ -910,7 +848,9 @@ function stat(file: string | number): Promise { } function stat(file: string | number, callback: AsyncCallback): void { - let p = taskpool.execute(FileIoImpl.statSync, file); + let p = taskpool.execute((file: string | number): Stat => { + return FileIoImpl.statSync(file); + }, file); p.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1007,27 +947,10 @@ function createRandomAccessFile(file: string | File, mode?: number, options?: RandomAccessFileOptions): Promise { return new Promise((resolve: (result: RandomAccessFile) => void, reject: (e: BusinessError) => void) => { - if (mode === undefined) { - let promise = taskpool.execute(FileIoImpl.createRandomAccessFileSync, file); - promise.then((ret: NullishType): void => { - let raffile = ret as RandomAccessFileInner; - resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); - }); - return; - } - if (mode !== undefined && options === undefined) { - let promise = taskpool.execute(FileIoImpl.createRandomAccessFileSync, file, mode); - promise.then((ret: NullishType): void => { - let raffile = ret as RandomAccessFileInner; - resolve(raffile); - }).catch((e: BusinessError): void => { - reject(e); - }); - return; - } - let promise = taskpool.execute(FileIoImpl.createRandomAccessFileSync, file, mode, options); + let promise = taskpool.execute((file: string | File, mode?: number, + options?: RandomAccessFileOptions): RandomAccessFile => { + return FileIoImpl.createRandomAccessFileSync(file, mode, options); + }, file, mode, options); promise.then((ret: NullishType): void => { let raffile = ret as RandomAccessFileInner; resolve(raffile); @@ -1036,9 +959,12 @@ function createRandomAccessFile(file: string | File, mode?: number, }); }); } + function fdopenStream(fd: number, mode: string): Promise { return new Promise((resolve: (result: Stream) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.fdopenStreamSync, fd, mode); + let promise = taskpool.execute((fd: number, mode: string): Stream => { + return FileIoImpl.fdopenStreamSync(fd, mode); + }, fd, mode); promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); @@ -1065,7 +991,9 @@ function setxattr(path: string, key: string, value: string): Promise { } function createRandomAccessFile(file: string | File, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.createRandomAccessFileSync, file); + let promise = taskpool.execute((file: string | File): RandomAccessFile => { + return FileIoImpl.createRandomAccessFileSync(file); + }, file); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1079,7 +1007,9 @@ function createRandomAccessFile(file: string | File, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.createRandomAccessFileSync, file, mode); + let promise = taskpool.execute((file: string | File, mode: number): RandomAccessFile => { + return FileIoImpl.createRandomAccessFileSync(file, mode); + }, file, mode); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1092,7 +1022,9 @@ function createRandomAccessFile(file: string | File, mode: number, } function fdopenStream(fd: number, mode: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.fdopenStreamSync, fd, mode); + let promise = taskpool.execute((fd: number, mode: string): Stream => { + return FileIoImpl.fdopenStreamSync(fd, mode); + }, fd, mode); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1110,7 +1042,9 @@ function fdopenStreamSync(fd: number, mode: string): Stream { function createStream(path: string, mode: string): Promise { return new Promise((resolve: (result: Stream) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.createStreamSync, path, mode); + let promise = taskpool.execute((path: string, mode: string): Stream => { + return FileIoImpl.createStreamSync(path, mode); + }, path, mode); promise.then((ret: NullishType): void => { let stream = ret as Stream; resolve(stream); @@ -1121,7 +1055,9 @@ function createStream(path: string, mode: string): Promise { } function createStream(path: string, mode: string, callback: AsyncCallback): void { - let promise = taskpool.execute(FileIoImpl.createStreamSync, path, mode); + let promise = taskpool.execute((path: string, mode: string): Stream => { + return FileIoImpl.createStreamSync(path, mode); + }, path, mode); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1172,7 +1108,9 @@ function lstatSync(path: string): Stat { function lstat(path: string): Promise { return new Promise((resolve: (result: Stat) => void, reject: (e: BusinessError) => void) => { - let promise = taskpool.execute(FileIoImpl.lstatSync, path); + let promise = taskpool.execute((path: string): Stat => { + return FileIoImpl.lstatSync(path); + }, path); promise.then((ret: NullishType): void => { if (ret === null || ret === undefined) { let e = new BusinessError(); @@ -1189,7 +1127,9 @@ function lstat(path: string): Promise { } function lstat(path: string, callback: AsyncCallback): void { - let p = taskpool.execute(FileIoImpl.lstatSync, path); + let p = taskpool.execute((path: string): Stat => { + return FileIoImpl.lstatSync(path); + }, path); p.then((ret: NullishType): void => { let e = new BusinessError(); if (ret === null || ret === undefined) { @@ -1207,45 +1147,25 @@ function lstat(path: string, callback: AsyncCallback): void { } function copyFile(src: string | number, dest: string | number, mode?: number): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - if (mode === undefined) { - let promise = taskpool.execute((src: string | number, dest: string | number): undefined => - FileIoImpl.copyFileSync(src, dest), src, dest); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((src: string | number, dest: string | number, mode: number): undefined => - FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((src: string | number, dest: string | number, mode?: number): undefined => + FileIoImpl.copyFileSync(src, dest, mode), src, dest, mode); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } function copy(srcUri: string, destUri: string, options?: CopyOptions): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - if (options === undefined) { - let promise = taskpool.execute((srcUri: string, destUri: string): undefined => - FileIoImpl.copySync(srcUri, destUri), srcUri, destUri); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((srcUri: string, destUri: string, options: CopyOptions): undefined => - FileIoImpl.copySync(srcUri, destUri, options), srcUri, destUri, options); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((srcUri: string, destUri: string, options?: CopyOptions): undefined => + FileIoImpl.copySync(srcUri, destUri, options), srcUri, destUri, options); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } @@ -1429,28 +1349,22 @@ export class RandomAccessFileInner implements RandomAccessFile { write(buffer: ArrayBuffer | string, options?: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(this.writeSync, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(this.writeSync, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((buffer: ArrayBuffer | string, options?: WriteOptions): number => { + return this.writeSync(buffer, options); + }, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } write(buffer: ArrayBuffer | string, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(this.writeSync, buffer, options); + let promise = taskpool.execute((buffer: ArrayBuffer | string, options: WriteOptions): number => { + return this.writeSync(buffer, options); + }, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1462,7 +1376,9 @@ export class RandomAccessFileInner implements RandomAccessFile { } write(buffer: ArrayBuffer | string, callback: AsyncCallback): void { - let promise = taskpool.execute(this.writeSync, buffer); + let promise = taskpool.execute((buffer: ArrayBuffer | string): number => { + return this.writeSync(buffer); + }, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1483,28 +1399,22 @@ export class RandomAccessFileInner implements RandomAccessFile { read(buffer: ArrayBuffer, options?: ReadOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(this.readSync, buffer); - promise.then((ret: NullishType): void => { - let result = ret as number; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(this.readSync, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number; - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((buffer: ArrayBuffer, options?: ReadOptions): number => { + return this.readSync(buffer, options); + }, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } read(buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(this.readSync, buffer, options); + let promise = taskpool.execute((buffer: ArrayBuffer, options: ReadOptions): number => { + return this.readSync(buffer, options); + }, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1516,7 +1426,9 @@ export class RandomAccessFileInner implements RandomAccessFile { } read(buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(this.readSync, buffer); + let promise = taskpool.execute((buffer: ArrayBuffer): number => { + return this.readSync(buffer); + }, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1562,25 +1474,14 @@ export class FileInner implements File { lock(exclusive?: boolean): Promise { return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { - if (exclusive === undefined) { - let promise = taskpool.execute((): undefined => { - return this.lockSync(); - }); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute((exclusive: boolean): undefined => { - return this.lockSync(exclusive); - }, exclusive); - promise.then((ret: NullishType): void => { - resolve(undefined); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((exclusive?: boolean): undefined => { + return this.lockSync(exclusive); + }, exclusive); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } @@ -1781,28 +1682,22 @@ export class StreamInner implements Stream { write(buffer: ArrayBuffer | string, options?: WriteOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(this.writeSync, buffer); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(this.writeSync, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((buffer: ArrayBuffer | string, options?: WriteOptions): number => { + return this.writeSync(buffer, options); + }, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } write(buffer: ArrayBuffer | string, callback: AsyncCallback): void { - let promise = taskpool.execute(this.writeSync, buffer); + let promise = taskpool.execute((buffer: ArrayBuffer | string): number => { + return this.writeSync(buffer); + }, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1814,7 +1709,9 @@ export class StreamInner implements Stream { } write(buffer: ArrayBuffer | string, options: WriteOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(this.writeSync, buffer, options); + let promise = taskpool.execute((buffer: ArrayBuffer | string, options: WriteOptions): number => { + return this.writeSync(buffer, options); + }, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1829,28 +1726,22 @@ export class StreamInner implements Stream { read(buffer: ArrayBuffer, options?: ReadOptions): Promise { return new Promise((resolve: (result: number) => void, reject: (e: BusinessError) => void) => { - if (options === undefined) { - let promise = taskpool.execute(this.readSync, buffer); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } else { - let promise = taskpool.execute(this.readSync, buffer, options); - promise.then((ret: NullishType): void => { - let result = ret as number - resolve(result); - }).catch((e: BusinessError): void => { - reject(e); - }); - } + let promise = taskpool.execute((buffer: ArrayBuffer, options?: ReadOptions): number => { + return this.readSync(buffer, options); + }, buffer, options); + promise.then((ret: NullishType): void => { + let result = ret as number + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); }); } read(buffer: ArrayBuffer, callback: AsyncCallback): void { - let promise = taskpool.execute(this.readSync, buffer); + let promise = taskpool.execute((buffer: ArrayBuffer): number => { + return this.readSync(buffer); + }, buffer); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; @@ -1862,7 +1753,9 @@ export class StreamInner implements Stream { } read(buffer: ArrayBuffer, options: ReadOptions, callback: AsyncCallback): void { - let promise = taskpool.execute(this.readSync, buffer, options); + let promise = taskpool.execute((buffer: ArrayBuffer, options: ReadOptions): number => { + return this.readSync(buffer, options); + }, buffer, options); promise.then((ret: NullishType): void => { let e = new BusinessError(); e.code = 0; diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp index ae55f8504..35744bb89 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp @@ -40,6 +40,16 @@ static tuple> RealPa return { ret, move(realpath_req) }; } +bool FsFile::RemoveEntity() +{ + if (!fileEntity) { + HILOGE("Failed to get file entity"); + return false; + } + fileEntity = nullptr; + return true; +} + FsResult FsFile::GetFD() const { if (!fileEntity) { diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h index dbdd471bf..73c41d4d0 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.h +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.h @@ -62,6 +62,7 @@ public: #endif static FsResult Constructor(); FsResult GetFD() const; + bool RemoveEntity(); private: unique_ptr fileEntity; diff --git a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.cpp b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.cpp index 2a39e3506..00a493ed2 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/fs_stream.cpp @@ -29,7 +29,7 @@ using namespace std; std::shared_ptr FsStream::GetFilePtr() { std::lock_guard lock(mtx); - if (!streamEntity->fp) { + if (!streamEntity) { return nullptr; } return streamEntity->fp; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 7de532c82..f24a82871 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -20,6 +20,7 @@ #include "close_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" +#include "file_wrapper.h" #include "type_converter.h" namespace OHOS { @@ -30,7 +31,7 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; -tuple ParseFd(ani_env *env, ani_object obj) +tuple ParseFdOrFile(ani_env *env, ani_object obj) { int32_t result = -1; ani_class IntClass; @@ -41,45 +42,50 @@ tuple ParseFd(ani_env *env, ani_object obj) ani_int fd; if (ANI_OK != env->Object_CallMethodByName_Int(obj, "intValue", nullptr, &fd)) { HILOGE("Get fd value failed"); - return { false, result }; + return { false, result, nullptr }; } result = static_cast(fd); - return { true, result }; + return { true, result, nullptr }; } - ani_class FileClass; - env->FindClass("L@ohos/file/fs/fileIo/FileInner;", &FileClass); - ani_boolean isFile; - env->Object_InstanceOf(obj, FileClass, &isFile); - if (isFile) { - ani_double fd; - if (ANI_OK != env->Object_GetPropertyByName_Double(obj, "fd", &fd)) { - HILOGE("Get fd in class file failed"); - return { false, result }; - } - result = static_cast(fd); - return { true, result }; + FsFile * file = FileWrapper::Unwrap(env, obj); + if (file != nullptr) { + return { true, -1, file }; } - HILOGE("Invalid fd type"); - return { false, result }; + + HILOGE("Cannot unwrap fsfile!"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return { false, -1, nullptr }; } void CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_object obj) { - auto [succ, fd] = ParseFd(env, obj); + auto [succ, fd, file] = ParseFdOrFile(env, obj); if (!succ) { HILOGE("Parse fd argument failed"); ErrorHandler::Throw(env, EINVAL); return; } - auto ret = CloseCore::DoClose(fd); + if (file == nullptr) { + auto ret = CloseCore::DoClose(fd); + if (!ret.IsSuccess()) { + HILOGE("Close %d failed", fd); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } + return; + } + + auto ret = CloseCore::DoClose(file); if (!ret.IsSuccess()) { HILOGE("Close %d failed", fd); const auto &err = ret.GetError(); ErrorHandler::Throw(env, err); return; } + return; } } // namespace ANI 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 9ac7a7840..51f4b68ad 100644 --- a/interfaces/kits/js/src/mod_fs/properties/close_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/close_core.cpp @@ -60,4 +60,22 @@ FsResult CloseCore::DoClose(const int32_t &fd) } return FsResult::Success(); } + +FsResult CloseCore::DoClose(FsFile *file) +{ + auto ret = file->GetFD(); + if (!ret.IsSuccess()) { + return FsResult::Error(EINVAL); + } + auto err = CloseFd(ret.GetData().value()); + if (err) { + return FsResult::Error(err); + } + + bool succ = file->RemoveEntity(); + if (!succ) { + return FsResult::Error(EINVAL); + } + return FsResult::Success(); +} } // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/close_core.h b/interfaces/kits/js/src/mod_fs/properties/close_core.h index 1bbdd1c31..3b54ca4c5 100644 --- a/interfaces/kits/js/src/mod_fs/properties/close_core.h +++ b/interfaces/kits/js/src/mod_fs/properties/close_core.h @@ -17,6 +17,7 @@ #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CLOSE_CORE_H #include "filemgmt_libfs.h" +#include "fs_file.h" #include "fs_utils.h" #include "fs_result.h" @@ -25,6 +26,7 @@ namespace OHOS::FileManagement::ModuleFileIO { class CloseCore final { public: static FsResult DoClose(const int32_t &fd); + static FsResult DoClose(FsFile *file); }; const std::string PROCEDURE_CLOSE_NAME = "FileIOClose"; -- Gitee From 2ba9364b6843b79a502763c61f7d4cee8e6b4351 Mon Sep 17 00:00:00 2001 From: WangKe Date: Tue, 13 May 2025 03:36:22 +0000 Subject: [PATCH 4/4] update interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp. Signed-off-by: WangKe Change-Id: Id4b680f0bf749bf39538f79d7961a0355bbe59c4 --- interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index f24a82871..130a10329 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -48,7 +48,7 @@ tuple ParseFdOrFile(ani_env *env, ani_object obj) return { true, result, nullptr }; } - FsFile * file = FileWrapper::Unwrap(env, obj); + FsFile *file = FileWrapper::Unwrap(env, obj); if (file != nullptr) { return { true, -1, file }; } -- Gitee