From ec8d4f646da46f18d1388c465702f430a2cfe419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 21:10:30 +0800 Subject: [PATCH 01/15] =?UTF-8?q?readtext=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../js/src/common/ani_helper/type_converter.cpp | 13 +++++++++++++ .../kits/js/src/common/ani_helper/type_converter.h | 1 + .../js/src/mod_fs/properties/ani/read_text_ani.cpp | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index fe336b31e..8ccc2d8f8 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,6 +108,19 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } +static std::tuple ToAniString(ani_env *env, std::string str, size_t size) +{ + if (env == nullptr) { + return { false, {} }; + } + + ani_string result; + if (ANI_OK != env->String_NewUTF8(str.c_str(), size, &result)) { + return { false, {} }; + } + return { true, std::move(result) }; +} + std::tuple TypeConverter::ToAniString(ani_env *env, const char *str) { if (env == nullptr) { diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.h b/interfaces/kits/js/src/common/ani_helper/type_converter.h index 24bdd736a..a6d062659 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.h +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.h @@ -33,6 +33,7 @@ public: static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &value); static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &value); static std::tuple ToAniString(ani_env *env, std::string str); + static std::tuple ToAniString(ani_env *env, std::string str, size_t size); static std::tuple ToAniString(ani_env *env, const char *str); static std::tuple> EnumToInt32(ani_env *env, const ani_enum_item &enumOp); static std::tuple ToFileInfo(ani_env *env, const ani_object &pathOrFd); 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 dc733c606..7e2fe63ff 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 @@ -92,7 +92,8 @@ ani_string ReadTextAni::ReadTextSync( const auto &resText = ret.GetData().value(); string res = std::get<0>(resText); - auto [succ, result] = TypeConverter::ToAniString(env, res); + size_t size = std::get<1>(resText); + auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); ErrorHandler::Throw(env, UNKNOWN_ERR); -- Gitee From 517226ccfe6f7f59d967bee9f3596791387abcd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 14:35:06 +0000 Subject: [PATCH 02/15] update interfaces/kits/js/src/common/ani_helper/type_converter.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/common/ani_helper/type_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 8ccc2d8f8..1206d5877 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,7 +108,7 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } -static std::tuple ToAniString(ani_env *env, std::string str, size_t size) +static std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) { if (env == nullptr) { return { false, {} }; -- Gitee From 74ccd252dfbd518f92628a03310e41cfca93f26b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 16:02:51 +0000 Subject: [PATCH 03/15] update interfaces/kits/js/src/common/ani_helper/type_converter.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/common/ani_helper/type_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 1206d5877..bfa635e0e 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,7 +108,7 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } -static std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) +std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) { if (env == nullptr) { return { false, {} }; -- Gitee From c380da8f31ba4f7fc37b27ba8e5f61dddf441705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Apr 2025 07:50:10 +0000 Subject: [PATCH 04/15] update interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../kits/js/src/mod_fs/properties/ani/read_text_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..125286c9f 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); // 取字符串 + size_t size = std::get<1>(resText); // 取读取字符数 auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); -- Gitee From 8752951b4204878701b2b9692fdb3658bf7cffea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Apr 2025 07:52:31 +0000 Subject: [PATCH 05/15] update interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../kits/js/src/mod_fs/properties/ani/read_text_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 125286c9f..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"); -- Gitee From eee3f085c6b8f322b9824474228a0b0926d991ec Mon Sep 17 00:00:00 2001 From: zhangxiaoliang25 Date: Mon, 7 Apr 2025 17:10:32 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=E8=AA=BF=E6=95=B4=E9=A1=9E=E8=B7=AF?= =?UTF-8?q?=E5=BE=91=20Signed-off-by:=20zhangxiaoliang25=20=20Change-Id:=20I8637e251aa895be8707d9c74a32a42ac?= =?UTF-8?q?28d35c22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic58e980db7dd71f94001d1b968199b51d4c73823 statvfs接口改造 Signed-off-by: tianp Change-Id: I4159588478b0969e1fedd6972198649c10f17541 statvfs接口改造 Signed-off-by: tianp Change-Id: Ic3fec8619148dae3cf8e8f754bef906ded95dafa --- interfaces/kits/js/BUILD.gn | 82 ++++++- .../js/src/mod_fs/ani/bind_function_class.cpp | 12 +- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 209 +++++++++++------- .../mod_fs/class_file/ani/file_wrapper.cpp | 2 +- .../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 +- .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 2 +- .../ani/ets/@ohos.file.securityLabel.ets | 2 +- .../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 +++ 21 files changed, 568 insertions(+), 109 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 f12c564b2..2ff5b455a 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -624,10 +624,10 @@ 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", + ":ani_file_statvfs", ] } @@ -658,7 +658,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 +852,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 +922,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", @@ -989,3 +989,71 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { part_name = "file_api" deps = [ ":ohos_file_securityLabel_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_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 6f1f37532..690640511 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 { @@ -91,7 +91,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 +105,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 +116,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 +133,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 +148,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..b48f2890c 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, } @@ -1409,7 +1396,7 @@ export interface RandomAccessFile { readSync(buffer: ArrayBuffer, options?: ReadOptions): number; } -class RandomAccessFileInner implements RandomAccessFile { +export class RandomAccessFileInner implements RandomAccessFile { fd: number = -1; filePointer: number = -1; @@ -1552,7 +1539,7 @@ export interface File { unlock(): void; } -class FileInner implements File { +export class FileInner implements File { fd: number = -1; path: String = ""; name: String = ""; @@ -1623,17 +1610,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 +1633,7 @@ export interface ReaderIterator { next(): ReaderIteratorResult; } -class ReaderIteratorInner implements ReaderIterator { +export class ReaderIteratorInner implements ReaderIterator { private nativePtr: long = 0; constructor(ptr: long) { @@ -1685,7 +1668,7 @@ export interface Stat { isSymbolicLink(): boolean; } -class StatInner implements Stat { +export class StatInner implements Stat { ino: bigint = 0n; mode: number; uid: number; @@ -1734,7 +1717,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) { @@ -2058,12 +2041,14 @@ 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 +2075,7 @@ export interface WatchEvent { } class WatchEventInner implements WatchEvent { - fileName: string; + fileName: string = ''; event: number; cookie: number; @@ -2104,17 +2089,77 @@ 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 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 +2173,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 +2197,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 +2209,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 +2221,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_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_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; 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..9ea38ff27 --- /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 a4602990552d7e4bb1eab505cbb155af7733f4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Apr 2025 14:39:23 +0800 Subject: [PATCH 07/15] =?UTF-8?q?stream=20=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 Change-Id: I3380c892230c77ac77252632b54a8747f70dd1e2 --- .../js/src/mod_fs/ani/bind_function_class.cpp | 3 + .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 31 ++- .../ani/randomaccessfile_ani.cpp | 240 +++++++++++++++++- .../ani/randomaccessfile_ani.h | 8 +- .../src/mod_fs/properties/read_text_core.cpp | 3 + 5 files changed, 264 insertions(+), 21 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 690640511..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 @@ -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); 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 b48f2890c..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 @@ -1394,6 +1394,8 @@ 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; } export class RandomAccessFileInner implements RandomAccessFile { @@ -1524,6 +1526,9 @@ export class RandomAccessFileInner implements RandomAccessFile { callback(e, 0); }); } + + native getReadStream(): ReadStream; + native getWriteStream(): WriteStream; } export interface File { @@ -1872,16 +1877,6 @@ export 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; @@ -2041,6 +2036,7 @@ export class WriteStream extends stream.Writable { return modeStr; } } + export enum WhenceType { SEEK_SET = 0, SEEK_CUR = 1, @@ -2118,9 +2114,6 @@ export interface ListFileOptions { filter?: Filter; } - - - export interface WriteStreamOptions { mode?: number; start?: number; @@ -2131,6 +2124,18 @@ export interface ReadStreamOptions { 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; 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/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 -- Gitee From 0b8e7d5ded857ee5e9f1f658ea26b4c2571e5ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Mon, 28 Apr 2025 22:48:33 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 Change-Id: Id852676ae959fefae34afd046736b7d3fa510fda --- interfaces/kits/js/src/mod_fs/properties/read_text_core.cpp | 3 --- 1 file changed, 3 deletions(-) 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 -- Gitee From aee38d1fde8c898546d18ed22c881303010e5654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Tue, 29 Apr 2025 09:01:26 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 Change-Id: I671c4382c8867945c2dbdd6c82b86e0d3d867319 --- .../mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp | 1 - 1 file changed, 1 deletion(-) 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; } -- Gitee From 00cf619fe13fd2c0c32a061b5ae4a484a70b32e5 Mon Sep 17 00:00:00 2001 From: liyuke Date: Sun, 27 Apr 2025 14:17:29 +0800 Subject: [PATCH 10/15] =?UTF-8?q?environment=20ani=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/BUILD.gn | 64 ++++++ .../ani/bind_function_class.cpp | 71 ++++++ .../mod_environment/ani/environment_ani.cpp | 182 ++++++++++++++++ .../src/mod_environment/ani/environment_ani.h | 42 ++++ .../ani/ets/@ohos.file.environment.ets | 103 +++++++++ .../src/mod_environment/environment_core.cpp | 205 ++++++++++++++++++ .../js/src/mod_environment/environment_core.h | 38 ++++ 7 files changed, 705 insertions(+) create mode 100644 interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp create mode 100644 interfaces/kits/js/src/mod_environment/ani/environment_ani.h create mode 100644 interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.cpp create mode 100644 interfaces/kits/js/src/mod_environment/environment_core.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 2ff5b455a..9a6403a8f 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -990,6 +990,70 @@ ohos_prebuilt_etc("ohos_file_securityLabel_abc_etc") { deps = [ ":ohos_file_securityLabel_abc" ] } +ohos_shared_library("ani_environment_class") { + public_configs = [ ":ani_config" ] + include_dirs = [ + "src/mod_environment/ani", + "src/mod_environment", + ] + 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_environment/ani/bind_function_class.cpp", + "src/mod_environment/ani/environment_ani.cpp", + "src/mod_environment/environment_core.cpp", + ] + + deps = [ + ":ohos_file_environment_abc_etc", + "${file_api_path}/interfaces/kits/rust:rust_file", + "${utils_path}/filemgmt_libfs:filemgmt_libfs", + "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "hilog:libhilog", + "libuv:uv", + "ipc:ipc_core", + "init:libbegetutil", + "openssl:libcrypto_shared", + "os_account:os_account_innerkits", + "runtime_core:ani", + "runtime_core:libarkruntime", + ] + use_exceptions = true + 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_environment_abc") { + base_url = "./src/mod_environment/ani/ets" + files = [ "./src/mod_environment/ani/ets/@ohos.file.environment.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ohos_file_environment_abc.abc" +} + +ohos_prebuilt_etc("ohos_file_environment_abc_etc") { + source = "$target_out_dir/ohos_file_environment_abc.abc" + module_install_dir = "framework" + subsystem_name = "filemanagement" + part_name = "file_api" + deps = [ ":ohos_file_environment_abc" ] +} + ohos_shared_library("ani_file_statvfs") { public_configs = [ ":ani_config" ] include_dirs = [ diff --git a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp new file mode 100644 index 000000000..86e43e7e1 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp @@ -0,0 +1,71 @@ +/* + * 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 "bind_function.h" +#include "environment_ani.h" + +using namespace OHOS::FileManagement::ModuleFileIO::ANI; + +static ani_status BindStaticMethods(ani_env *env) +{ + static const char *className = "L@ohos/file/environment/EnvironmentImpl;"; + std::array methods = { + ani_native_function { + "getStorageDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetStorageDataDirSync) }, + ani_native_function { + "getUserDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDataDirSync) }, + ani_native_function { + "getUserDownloadDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDownloadDirSync) }, + ani_native_function { + "getUserDesktopDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDesktopDirSync) }, + ani_native_function { + "getUserDocumentDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserDocumentDirSync) }, + ani_native_function { + "getExternalStorageDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetExternalStorageDirSync) }, + ani_native_function { + "getUserHomeDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserHomeDirSync) }, + }; + return BindClass(env, className, methods); +} + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + if (vm == nullptr) { + HILOGE("Invalid parameter vm"); + return ANI_INVALID_ARGS; + } + + if (result == nullptr) { + HILOGE("Invalid parameter result"); + return ANI_INVALID_ARGS; + } + + ani_env *env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + HILOGE("Invalid ani version!"); + return ANI_INVALID_VERSION; + } + + status = BindStaticMethods(env); + if (status != ANI_OK) { + HILOGE("Cannot bind native static methods for environment!"); + return status; + }; + + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp new file mode 100644 index 000000000..fd3f5c084 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -0,0 +1,182 @@ +/* + * 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 "environment_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "environment_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::ModuleEnvironment; + +ani_string EnvironmentAni::GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetStorageDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get storage data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDataDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user data dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDownloadDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user download dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDesktopDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user desktop dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserDocumentDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user document dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetExternalStorageDir(); + if (!ret.IsSuccess()) { + HILOGE("Get external storage dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +ani_string EnvironmentAni::GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz) +{ + auto ret = DoGetUserHomeDir(); + if (!ret.IsSuccess()) { + HILOGE("Get user home dir failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/ani/environment_ani.h b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h new file mode 100644 index 000000000..d14a78c69 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.h @@ -0,0 +1,42 @@ +/* + * 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_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class EnvironmentAni final { +public: + static ani_string GetStorageDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDataDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDownloadDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDesktopDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserDocumentDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetExternalStorageDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); + static ani_string GetUserHomeDirSync(ani_env *env, [[maybe_unused]] ani_class clazz); +}; + +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ANI_ENVIRONMENT_ANI_H \ No newline at end of file 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 new file mode 100644 index 000000000..5155ebd3a --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/ani/ets/@ohos.file.environment.ets @@ -0,0 +1,103 @@ +/* + * 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 Environment { + export function getStorageDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function getStorageDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getStorageDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); + } + + export function getUserDataDir(): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let result = ret as string; + resolve(result); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + export function getUserDataDir(callback: AsyncCallback): void { + let promise = taskpool.execute(EnvironmentImpl.getUserDataDirSync); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let result = ret as string; + callback(e, result); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); + } + + export function getUserDownloadDir(): string { + return EnvironmentImpl.getUserDownloadDirSync(); + } + + export function getUserDesktopDir(): string { + return EnvironmentImpl.getUserDesktopDirSync(); + } + + export function getUserDocumentDir(): string { + return EnvironmentImpl.getUserDocumentDirSync(); + } + + export function getExternalStorageDir(): string { + return EnvironmentImpl.getExternalStorageDirSync(); + } + + export function getUserHomeDir(): string { + return EnvironmentImpl.getUserHomeDirSync(); + } + +} + +export default Environment; + +class EnvironmentImpl { + + static { + loadLibrary("ani_environment_class"); + } + + static native getStorageDataDirSync(): string; + static native getUserDataDirSync(): string; + static native getUserDownloadDirSync(): string; + static native getUserDesktopDirSync(): string; + static native getUserDocumentDirSync(): string; + static native getExternalStorageDirSync(): string; + static native getUserHomeDirSync(): string; +} \ 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 new file mode 100644 index 000000000..c46fe5066 --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -0,0 +1,205 @@ +/* + * 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 "environment_core.h" +#include "accesstoken_kit.h" +#include "account_error_no.h" +#include "filemgmt_libhilog.h" +#include "ipc_skeleton.h" +#include "os_account_manager.h" +#include "parameter.h" +#include "tokenid_kit.h" +#include +#include +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +namespace { +const std::string STORAGE_DATA_PATH = "/data"; +const std::string PC_STORAGE_PATH = "/storage/Users/"; +const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; +const std::string USER_APP_DATA_PATH = "/appdata"; +const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string FILE_ACCESS_MANAGER_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; +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"; +static bool IsSystemApp() +{ + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); +} + +static bool CheckCallingPermission(const std::string &permission) +{ + Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); + if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("ModuleUserDirectory::CheckCallingPermission have no fileAccess permission"); + return false; + } + return true; +} + +static std::string GetUserName() +{ + std::string userName; + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK || userName.empty()) { + HILOGE("Get userName Failed"); + } + userName = DEFAULT_USERNAME; + return userName; +} + +static std::string GetPublicPath(const std::string &directoryName) +{ + return PC_STORAGE_PATH + GetUserName() + directoryName; +} + +static bool CheckFileManagerFullMountEnable() +{ + char value[] = "false"; + int retSystem = GetParameter(g_fileManagerFullMountEnableParameter, "false", value, sizeof(value)); + if (retSystem > 0 && !std::strcmp(value, "true")) { + return true; + } + HILOGE("Not supporting all mounts"); + return false; +} + +static int CheckInvalidAccess(const std::string &permission) +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return E_DEVICENOTSUPPORT; + } + if (permission == FILE_ACCESS_MANAGER_PERMISSION) { + if (!IsSystemApp()) { + return E_PERMISSION_SYS; + } + } + if (!CheckCallingPermission(permission)) { + HILOGE("No Permission"); + return E_PERMISSION; + } + return ERRNO_NOERR; +} +} + +FsResult DoGetStorageDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + return FsResult::Success(std::move(STORAGE_DATA_PATH)); +} + +int GetUserId() +{ + return 0; +} + +FsResult DoGetUserDataDir() +{ + if (!IsSystemApp()) { + return FsResult::Error(E_PERMISSION_SYS); + } + + auto userDataPath = std::make_shared(); + (*userDataPath).append("/storage/media/").append(std::to_string(GetUserId())).append("/local"); + return FsResult::Success(std::move(*userDataPath)); + +} + +FsResult DoGetUserDownloadDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string downloadPath = GetPublicPath(DOWNLOAD_PATH); + if (downloadPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(downloadPath)); + +} + +FsResult DoGetUserDesktopDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string desktopPath = GetPublicPath(DESKTOP_PATH); + if (desktopPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(desktopPath)); +} + +FsResult DoGetUserDocumentDir() +{ + if (!CheckFileManagerFullMountEnable()) { + HILOGE("Capability not supported"); + return FsResult::Error(E_DEVICENOTSUPPORT); + } + + static std::string documentsPath = GetPublicPath(DOCUMENTS_PATH); + if (documentsPath.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + return FsResult::Success(std::move(documentsPath)); +} + +FsResult DoGetExternalStorageDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); + +} + +FsResult DoGetUserHomeDir() +{ + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + if (res) { + return FsResult::Error(res); + } + + static std::string userName = GetUserName(); + if (userName.empty()) { + HILOGE("Unknown error"); + return FsResult::Error(E_UNKNOWN_ERROR); + } + std::string result = PC_STORAGE_PATH + userName; + return FsResult::Success(std::move(result)); + +} +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_environment/environment_core.h b/interfaces/kits/js/src/mod_environment/environment_core.h new file mode 100644 index 000000000..2ac81134f --- /dev/null +++ b/interfaces/kits/js/src/mod_environment/environment_core.h @@ -0,0 +1,38 @@ +/* + * 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_ENVIRONMENT_ENVIRONMENT_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H + +#include "filemgmt_libfs.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleEnvironment { +using namespace ModuleFileIO; + +FsResult DoGetStorageDataDir(); +FsResult DoGetUserDataDir(); +FsResult DoGetUserDownloadDir(); +FsResult DoGetUserDesktopDir(); +FsResult DoGetUserDocumentDir(); +FsResult DoGetExternalStorageDir(); +FsResult DoGetUserHomeDir(); + +} // namespace ModuleEnvironment +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_ENVIRONMENT_ENVIRONMENT_CORE_H \ No newline at end of file -- Gitee From 54ea0eb11e9c6583f59e93a4a914f2706f92e290 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 28 Apr 2025 21:40:24 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/BUILD.gn | 2 +- .../js/src/mod_environment/ani/environment_ani.cpp | 7 ------- .../kits/js/src/mod_environment/environment_core.cpp | 11 +++++------ .../kits/js/src/mod_environment/environment_core.h | 2 -- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9a6403a8f..7da81e330 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -990,7 +990,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..5a8d7c9bd 100644 --- a/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp +++ b/interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp @@ -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/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 -- Gitee From 80d9c88b28e4d5b1b4e7a68ca0cffb28f1b18b63 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 28 Apr 2025 21:43:03 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- interfaces/kits/js/src/mod_environment/ani/environment_ani.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5a8d7c9bd..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 { -- Gitee From 2264136f226c0a9f5bc9c1827b1d47f1495e0364 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 28 Apr 2025 21:46:42 +0800 Subject: [PATCH 13/15] =?UTF-8?q?=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../js/src/mod_environment/ani/ets/@ohos.file.environment.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; -- Gitee From 579329811ecc5057e73fc8e9d33e65c1de37ed02 Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 29 Apr 2025 11:35:12 +0800 Subject: [PATCH 14/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E9=A3=8E=E6=A0=BC=E4=B8=BAg=5F=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E7=9A=84=E5=B0=8F=E9=A9=BC=E5=B3=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../src/mod_environment/environment_core.cpp | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index 0f86ede13..f85406e68 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -30,18 +30,18 @@ namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { namespace { -const std::string STORAGE_DATA_PATH = "/data"; -const std::string PC_STORAGE_PATH = "/storage/Users/"; -const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; -const std::string USER_APP_DATA_PATH = "/appdata"; -const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; -const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; -const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; -const std::string FILE_ACCESS_MANAGER_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; -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 std::string g_storageDataPath = "/data"; +const std::string g_pcStoragePath = "/storage/Users/"; +const std::string g_externalStoragePath = "/storage/External"; +const std::string g_userAppDataPath = "/appdata"; +const std::string g_readWriteDownloadPermission = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string g_readWriteDesktopPermission = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string g_readWriteDocumentsPermission = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string g_fileAccessManagerPermission = "ohos.permission.FILE_ACCESS_MANAGER"; +const std::string g_downloadPath = "/Download"; +const std::string g_desktopPath = "/Desktop"; +const std::string g_documentsPath = "/Documents"; +const std::string g_defaultUserName = "currentUser"; const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; static bool IsSystemApp() { @@ -67,13 +67,13 @@ static std::string GetUserName() if (errCode != ERR_OK || userName.empty()) { HILOGE("Get userName Failed"); } - userName = DEFAULT_USERNAME; + userName = g_defaultUserName; return userName; } static std::string GetPublicPath(const std::string &directoryName) { - return PC_STORAGE_PATH + GetUserName() + directoryName; + return g_pcStoragePath + GetUserName() + directoryName; } static bool CheckFileManagerFullMountEnable() @@ -93,7 +93,7 @@ static int CheckInvalidAccess(const std::string &permission) HILOGE("Capability not supported"); return E_DEVICENOTSUPPORT; } - if (permission == FILE_ACCESS_MANAGER_PERMISSION) { + if (permission == g_fileAccessManagerPermission) { if (!IsSystemApp()) { return E_PERMISSION_SYS; } @@ -111,7 +111,7 @@ FsResult DoGetStorageDataDir() if (!IsSystemApp()) { return FsResult::Error(E_PERMISSION_SYS); } - return FsResult::Success(std::move(STORAGE_DATA_PATH)); + return FsResult::Success(std::move(g_storageDataPath)); } int GetUserId() @@ -137,7 +137,7 @@ FsResult DoGetUserDownloadDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string downloadPath = GetPublicPath(DOWNLOAD_PATH); + static std::string downloadPath = GetPublicPath(g_downloadPath); if (downloadPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -152,7 +152,7 @@ FsResult DoGetUserDesktopDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string desktopPath = GetPublicPath(DESKTOP_PATH); + static std::string desktopPath = GetPublicPath(g_desktopPath); if (desktopPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -167,7 +167,7 @@ FsResult DoGetUserDocumentDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string documentsPath = GetPublicPath(DOCUMENTS_PATH); + static std::string documentsPath = GetPublicPath(g_documentsPath); if (documentsPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -177,16 +177,16 @@ FsResult DoGetUserDocumentDir() FsResult DoGetExternalStorageDir() { - auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + auto res = CheckInvalidAccess(g_fileAccessManagerPermission); if (res) { return FsResult::Error(res); } - return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); + return FsResult::Success(std::move(g_externalStoragePath)); } FsResult DoGetUserHomeDir() { - auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); + auto res = CheckInvalidAccess(g_fileAccessManagerPermission); if (res) { return FsResult::Error(res); } @@ -196,7 +196,7 @@ FsResult DoGetUserHomeDir() HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); } - std::string result = PC_STORAGE_PATH + userName; + std::string result = g_pcStoragePath + userName; return FsResult::Success(std::move(result)); } } // namespace ModuleEnvironment -- Gitee From 7ad6018a08adc942a2bc8100b50fd715adacfd1f Mon Sep 17 00:00:00 2001 From: liyuke Date: Tue, 29 Apr 2025 14:15:52 +0800 Subject: [PATCH 15/15] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../src/mod_environment/environment_core.cpp | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/interfaces/kits/js/src/mod_environment/environment_core.cpp b/interfaces/kits/js/src/mod_environment/environment_core.cpp index f85406e68..f13e9b942 100644 --- a/interfaces/kits/js/src/mod_environment/environment_core.cpp +++ b/interfaces/kits/js/src/mod_environment/environment_core.cpp @@ -30,19 +30,19 @@ namespace OHOS { namespace FileManagement { namespace ModuleEnvironment { namespace { -const std::string g_storageDataPath = "/data"; -const std::string g_pcStoragePath = "/storage/Users/"; -const std::string g_externalStoragePath = "/storage/External"; -const std::string g_userAppDataPath = "/appdata"; -const std::string g_readWriteDownloadPermission = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; -const std::string g_readWriteDesktopPermission = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; -const std::string g_readWriteDocumentsPermission = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; -const std::string g_fileAccessManagerPermission = "ohos.permission.FILE_ACCESS_MANAGER"; -const std::string g_downloadPath = "/Download"; -const std::string g_desktopPath = "/Desktop"; -const std::string g_documentsPath = "/Documents"; -const std::string g_defaultUserName = "currentUser"; -const char *g_fileManagerFullMountEnableParameter = "const.filemanager.full_mount.enable"; +const std::string STORAGE_DATA_PATH = "/data"; +const std::string PC_STORAGE_PATH = "/storage/Users/"; +const std::string EXTERNAL_STORAGE_PATH = "/storage/External"; +const std::string USER_APP_DATA_PATH = "/appdata"; +const std::string READ_WRITE_DOWNLOAD_PERMISSION = "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY"; +const std::string READ_WRITE_DESKTOP_PERMISSION = "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY"; +const std::string READ_WRITE_DOCUMENTS_PERMISSION = "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY"; +const std::string FILE_ACCESS_MANAGER_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; +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 *FILE_MANAMER_FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable"; static bool IsSystemApp() { uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); @@ -67,19 +67,19 @@ static std::string GetUserName() if (errCode != ERR_OK || userName.empty()) { HILOGE("Get userName Failed"); } - userName = g_defaultUserName; + userName = DEFAULT_USERNAME; return userName; } static std::string GetPublicPath(const std::string &directoryName) { - return g_pcStoragePath + GetUserName() + directoryName; + return PC_STORAGE_PATH + GetUserName() + 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; } @@ -93,7 +93,7 @@ static int CheckInvalidAccess(const std::string &permission) HILOGE("Capability not supported"); return E_DEVICENOTSUPPORT; } - if (permission == g_fileAccessManagerPermission) { + if (permission == FILE_ACCESS_MANAGER_PERMISSION) { if (!IsSystemApp()) { return E_PERMISSION_SYS; } @@ -111,7 +111,7 @@ FsResult DoGetStorageDataDir() if (!IsSystemApp()) { return FsResult::Error(E_PERMISSION_SYS); } - return FsResult::Success(std::move(g_storageDataPath)); + return FsResult::Success(std::move(STORAGE_DATA_PATH)); } int GetUserId() @@ -137,7 +137,7 @@ FsResult DoGetUserDownloadDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string downloadPath = GetPublicPath(g_downloadPath); + static std::string downloadPath = GetPublicPath(DOWNLOAD_PATH); if (downloadPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -152,7 +152,7 @@ FsResult DoGetUserDesktopDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string desktopPath = GetPublicPath(g_desktopPath); + static std::string desktopPath = GetPublicPath(DESKTOP_PATH); if (desktopPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -167,7 +167,7 @@ FsResult DoGetUserDocumentDir() return FsResult::Error(E_DEVICENOTSUPPORT); } - static std::string documentsPath = GetPublicPath(g_documentsPath); + static std::string documentsPath = GetPublicPath(DOCUMENTS_PATH); if (documentsPath.empty()) { HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); @@ -177,16 +177,16 @@ FsResult DoGetUserDocumentDir() FsResult DoGetExternalStorageDir() { - auto res = CheckInvalidAccess(g_fileAccessManagerPermission); + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); if (res) { return FsResult::Error(res); } - return FsResult::Success(std::move(g_externalStoragePath)); + return FsResult::Success(std::move(EXTERNAL_STORAGE_PATH)); } FsResult DoGetUserHomeDir() { - auto res = CheckInvalidAccess(g_fileAccessManagerPermission); + auto res = CheckInvalidAccess(FILE_ACCESS_MANAGER_PERMISSION); if (res) { return FsResult::Error(res); } @@ -196,7 +196,7 @@ FsResult DoGetUserHomeDir() HILOGE("Unknown error"); return FsResult::Error(E_UNKNOWN_ERROR); } - std::string result = g_pcStoragePath + userName; + std::string result = PC_STORAGE_PATH + userName; return FsResult::Success(std::move(result)); } } // namespace ModuleEnvironment -- Gitee