From 29846c90a0e146dd50f059226f3c598106a9c1da Mon Sep 17 00:00:00 2001 From: zky Date: Fri, 24 Feb 2023 11:32:22 +0800 Subject: [PATCH] add open remoteUri processing logic, update open ausnahme handle logik. Signed-off-by: zky --- .../kits/js/src/mod_fs/properties/open.cpp | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 954534516..12014dc0b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -63,6 +63,7 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) if (!objFile) { HILOGE("Failed to instantiate class"); NError(EIO).ThrowErr(env); + close(fd); return NVal(); } @@ -70,6 +71,7 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) if (!fileEntity) { HILOGE("Failed to get fileEntity"); NError(EIO).ThrowErr(env); + close(fd); return NVal(); } auto fdg = make_unique(fd, false); @@ -90,7 +92,7 @@ static int OpenFileByDatashare(napi_env env, napi_value argv, string path, unsig int fd = -1; sptr remote = new (std::nothrow) IRemoteStub(); if (!remote) { - return ENOMEM; + return -ENOMEM; } dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); @@ -165,19 +167,28 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } + int fd = -1; string pathStr = string(path.get()); if (RemoteUri::IsMediaUri(pathStr)) { - auto fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], pathStr, mode); + fd = OpenFileByDatashare(env, funcArg[NARG_POS::FIRST], pathStr, mode); if (fd >= 0) { auto file = InstantiateFile(env, fd, pathStr, true).val_; return file; } HILOGE("Failed to open file by Datashare"); - NError(-1).ThrowErr(env); + NError(UNKROWN_ERR).ThrowErr(env); return nullptr; } else if (RemoteUri::IsFileUri(pathStr)) { RemoteUri remoteUri = RemoteUri(pathStr); pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); + } else if (RemoteUri::IsRemoteUri(pathStr, fd, mode)) { + if (fd >= 0) { + auto file = InstantiateFile(env, fd, pathStr, true).val_; + return file; + } + HILOGE("Failed to open file by RemoteUri"); + NError(UNKROWN_ERR).ThrowErr(env); + return nullptr; } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -226,8 +237,9 @@ napi_value Open::Async(napi_env env, napi_callback_info info) auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; + int fd = -1; if (RemoteUri::IsMediaUri(path)) { - auto fd = OpenFileByDatashare(env, argv, path, mode); + fd = OpenFileByDatashare(env, argv, path, mode); if (fd >= 0) { arg->fd = fd; arg->path = ""; @@ -235,10 +247,19 @@ napi_value Open::Async(napi_env env, napi_callback_info info) return NError(ERRNO_NOERR); } HILOGE("Failed to open file by Datashare"); - return NError(-1); + return NError(UNKROWN_ERR); } else if (RemoteUri::IsFileUri(path)) { RemoteUri remoteUri = RemoteUri(path); pathStr = GetPathFromFileUri(remoteUri.GetPath(), remoteUri.GetAuthority(), mode); + } else if (RemoteUri::IsRemoteUri(path, fd, mode)) { + if (fd >= 0) { + arg->fd = fd; + arg->path = ""; + arg->uri = path; + return NError(ERRNO_NOERR); + } + HILOGE("Failed to open file by RemoteUri"); + return NError(UNKROWN_ERR); } std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; -- Gitee