From a78608419b613fa711fcac2fa000b7e7abbd36cf Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Sun, 2 Jul 2023 16:03:15 +0800 Subject: [PATCH] fs_add_file.path&name Signed-off-by: 18721213663 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 67 +++++++++++++++++++ .../src/mod_fs/class_file/file_n_exporter.h | 4 +- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 28fb937cf..cd85970df 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -49,6 +50,22 @@ static FileEntity *GetFileEntity(napi_env env, napi_value raf_entity) return rafEntity; } +static tuple> RealPathCore(const string &srcPath) +{ + std::unique_ptr realpath_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!realpath_req) { + HILOGE("Failed to request heap memory."); + return { ENOMEM, move(realpath_req)}; + } + int ret = uv_fs_realpath(nullptr, realpath_req.get(), srcPath.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to realpath, ret: %{public}d", ret); + return { ret, move(realpath_req)}; + } + return { ERRNO_NOERR, move(realpath_req) }; +} + napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -65,6 +82,54 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) return NVal::CreateInt32(env, rafEntity->fd_.get()->GetFD()).val_; } +napi_value FileNExporter::GetPath(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); + if (!fileEntity) { + HILOGE("Failed to get file entity"); + return nullptr; + } + auto [realPathRes, realPath] = RealPathCore(fileEntity->path_); + if (realPathRes != ERRNO_NOERR) { + NError(realPathRes).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, string(static_cast(realPath->ptr))).val_; +} + +napi_value FileNExporter::GetName(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); + if (!fileEntity) { + HILOGE("Failed to get file entity"); + return nullptr; + } + auto [realPathRes, realPath] = RealPathCore(fileEntity->path_); + if (realPathRes != ERRNO_NOERR) { + NError(realPathRes).ThrowErr(env); + return nullptr; + } + string path = string(static_cast(realPath->ptr)); + auto pos = path.find_last_of('/'); + if (pos == string::npos) { + HILOGE("Failed to split filename from path, path: %{public}s", path.c_str()); + NError(ENOENT).ThrowErr(env); + return nullptr; + } + return NVal::CreateUTF8String(env, path.substr(pos + 1)).val_; +} #ifndef WIN_PLATFORM static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) { @@ -216,6 +281,8 @@ bool FileNExporter::Export() { vector props = { NVal::DeclareNapiGetter("fd", GetFD), + NVal::DeclareNapiGetter("name", GetName), + NVal::DeclareNapiGetter("path", GetPath), #ifndef WIN_PLATFORM NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h index 5ccc6b08b..0e39af4e5 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.h @@ -37,7 +37,9 @@ public: #endif static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value GetFD(napi_env env, napi_callback_info info); - + static napi_value GetPath(napi_env env, napi_callback_info info); + static napi_value GetName(napi_env env, napi_callback_info info); + FileNExporter(napi_env env, napi_value exports); ~FileNExporter() override; }; -- Gitee