From ec10241d0c25914d040a9334f60de64390ea7d13 Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Mon, 7 Aug 2023 12:00:11 +0800 Subject: [PATCH] fs_add_file.path&name Signed-off-by: 18721213663 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 68 +++++++++++++++++++ .../src/mod_fs/class_file/file_n_exporter.h | 2 + 2 files changed, 70 insertions(+) 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 28fb937cfb..9e3c9ea0e5 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" @@ -66,6 +67,22 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) } #ifndef WIN_PLATFORM +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, path: %{public}s", ret, srcPath.c_str()); + return { ret, move(realpath_req)}; + } + return { ERRNO_NOERR, move(realpath_req) }; +} + static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) { if (funcArg.GetArgc() >= NARG_CNT::ONE) { @@ -80,6 +97,55 @@ static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) return true; } +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_; +} + napi_value FileNExporter::Lock(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -217,6 +283,8 @@ bool FileNExporter::Export() vector props = { NVal::DeclareNapiGetter("fd", GetFD), #ifndef WIN_PLATFORM + NVal::DeclareNapiGetter("path", GetPath), + NVal::DeclareNapiGetter("name", GetName), NVal::DeclareNapiFunction("lock", Lock), NVal::DeclareNapiFunction("tryLock", TryLock), NVal::DeclareNapiFunction("unlock", UnLock), 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 5ccc6b08be..43e5278a0b 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 @@ -31,6 +31,8 @@ public: std::string GetNExporterName() override; #else std::string GetClassName() override; + static napi_value GetPath(napi_env env, napi_callback_info info); + static napi_value GetName(napi_env env, napi_callback_info info); static napi_value Lock(napi_env env, napi_callback_info info); static napi_value TryLock(napi_env env, napi_callback_info info); static napi_value UnLock(napi_env env, napi_callback_info info); -- Gitee