From f33553c544f799f41f3a24c0b75555d804861e79 Mon Sep 17 00:00:00 2001 From: fengjq Date: Mon, 6 Nov 2023 17:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87uri=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9A=84file=E5=AF=B9=E8=B1=A1=EF=BC=8C=E6=94=AF=E6=8C=81name/?= =?UTF-8?q?path=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengjq --- .../src/mod_fs/class_file/file_n_exporter.cpp | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) 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 9286110d3..4b7bf13ba 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 @@ -79,11 +79,7 @@ static tuple> Re 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) }; + return { ret, move(realpath_req) }; } static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) @@ -111,10 +107,16 @@ napi_value FileNExporter::GetPath(napi_env env, napi_callback_info info) auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); if (!fileEntity) { HILOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); return nullptr; } + if (fileEntity->uri_.length() != 0) { + AppFileService::ModuleFileUri::FileUri fileUri(fileEntity->uri_); + return NVal::CreateUTF8String(env, fileUri.GetPath()).val_; + } auto [realPathRes, realPath] = RealPathCore(fileEntity->path_); if (realPathRes != ERRNO_NOERR) { + HILOGE("Failed to get real path, ret: %{public}d", realPathRes); NError(realPathRes).ThrowErr(env); return nullptr; } @@ -132,14 +134,20 @@ napi_value FileNExporter::GetName(napi_env env, napi_callback_info info) auto fileEntity = GetFileEntity(env, funcArg.GetThisVar()); if (!fileEntity) { HILOGE("Failed to get file entity"); + NError(EINVAL).ThrowErr(env); return nullptr; } + if (fileEntity->uri_.length() != 0) { + AppFileService::ModuleFileUri::FileUri fileUri(fileEntity->uri_); + return NVal::CreateUTF8String(env, fileUri.GetName()).val_; + } auto [realPathRes, realPath] = RealPathCore(fileEntity->path_); if (realPathRes != ERRNO_NOERR) { + HILOGE("Failed to get real path, ret: %{public}d", realPathRes); NError(realPathRes).ThrowErr(env); return nullptr; } - string path = string(static_cast(realPath->ptr)); + string path(static_cast(realPath->ptr)); auto pos = path.find_last_of('/'); if (pos == string::npos) { HILOGE("Failed to split filename from path"); @@ -167,14 +175,16 @@ napi_value FileNExporter::GetParent(napi_env env, napi_callback_info info) string path(fileEntity->path_); if (fileEntity->uri_.length() != 0) { AppFileService::ModuleFileUri::FileUri fileUri(fileEntity->uri_); - path = fileUri.GetRealPath(); - } - auto [realPathRes, realPath] = RealPathCore(path); - if (realPathRes != ERRNO_NOERR) { - NError(realPathRes).ThrowErr(env); - return nullptr; + path = fileUri.GetPath(); + } else { + auto [realPathRes, realPath] = RealPathCore(path); + if (realPathRes) { + HILOGE("Failed to get real path, ret: %{public}d", realPathRes); + NError(realPathRes).ThrowErr(env); + return nullptr; + } + path = static_cast(realPath->ptr); } - path = string(static_cast(realPath->ptr)); auto pos = path.find_last_of('/'); if (pos == string::npos) { HILOGE("Failed to split filename from path"); -- Gitee