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 28fb937cfb46a55ec334ed481c759785b3c3f949..cd85970df68ba3e93bc0beff5cd478f4fe3b84fc 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 5ccc6b08beb0f47254bf8b8e1cc31fa22b6d7a30..0e39af4e5d8f6aaa90b4b667ae967b38e027ffd8 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; };