diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 399d7339ab5354726e4e7c4a93523c666cc28ba4..e1ed33fe8c1b8a41a07b6eead6ead166fe5cef94 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -175,9 +175,7 @@ static int MoveFile(const string &src, const string &dest, int mode) uv_fs_req_cleanup(&access_req); return ret; } - uv_fs_req_cleanup(&access_req); if (mode == MODE_THROW_ERR) { - uv_fs_t access_req; ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); uv_fs_req_cleanup(&access_req); if (ret == 0) { @@ -188,6 +186,8 @@ static int MoveFile(const string &src, const string &dest, int mode) HILOGE("Failed to access destPath with MODE_THROW_ERR."); return ret; } + } else { + uv_fs_req_cleanup(&access_req); } return RenameFile(src, dest); } diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index cba863538b9e3be6e6c56aadebbd1ef9bf98491f..e6052ab34f74372c7082c36be2f7906da8c2423a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -137,6 +137,25 @@ static int OpenFileByDatashare(const string &path, unsigned int flags) } #endif +static void OpenByMediaUri(const string &path, unsigned int mode, int &ret) +{ +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) + Uri uri(path); + string bundleName = uri.GetAuthority(); + if (bundleName == MEDIA) { + ret = OpenFileByDatashare(uri.ToString(), mode); + if (ret < 0) { + HILOGE("Failed to open file by Datashare error %{public}d", ret); + ret = -ret; + } + } else { + HILOGE("Failed to open file for libuv error %{public}d", ret); + } +#else + HILOGE("Failed to open file for libuv error %{public}d", ret); +#endif +} + napi_value Open::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -164,8 +183,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) if (uriType == SCHEME_FILE) { AppFileService::ModuleFileUri::FileUri fileUri(pathStr); pathStr = fileUri.GetRealPath(); - if ((bundleName == MEDIA || bundleName == DOCS) && - access(pathStr.c_str(), F_OK) != 0) { + if ((bundleName == MEDIA || bundleName == DOCS) && access(pathStr.c_str(), F_OK) != 0) { int ret = OpenFileByDatashare(uri.ToString(), mode); if (ret >= 0) { auto file = InstantiateFile(env, ret, uri.ToString(), true).val_; @@ -189,8 +207,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) } } #endif - std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); @@ -199,24 +216,9 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) int ret = uv_fs_open(nullptr, open_req.get(), pathStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - if (bundleName == MEDIA) { - ret = OpenFileByDatashare(uri.ToString(), mode); - if (ret < 0) { - HILOGE("Failed to open file by Datashare error %{public}d", ret); - NError(-ret).ThrowErr(env); - return nullptr; - } - } else { - HILOGE("Failed to open file for libuv error %{public}d", ret); - NError(ret).ThrowErr(env); - return nullptr; - } -#else - HILOGE("Failed to open file for libuv error %{public}d", ret); + OpenByMediaUri(pathStr, mode, ret); NError(ret).ThrowErr(env); return nullptr; -#endif } auto file = InstantiateFile(env, ret, pathStr, false).val_; return file; @@ -239,8 +241,7 @@ static NError AsyncCbExec(shared_ptr arg, const string &path, if (uriType == SCHEME_FILE) { AppFileService::ModuleFileUri::FileUri fileUri(path); pStr = fileUri.GetRealPath(); - if ((bundleName == MEDIA || bundleName == DOCS) && - access(pStr.c_str(), F_OK) != 0) { + if ((bundleName == MEDIA || bundleName == DOCS) && access(pStr.c_str(), F_OK) != 0) { int ret = OpenFileByDatashare(path, mode); if (ret >= 0) { arg->fd = ret; @@ -266,29 +267,15 @@ static NError AsyncCbExec(shared_ptr arg, const string &path, } } #endif - std::unique_ptr open_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; + unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } int ret = uv_fs_open(nullptr, open_req.get(), pStr.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - if (bundleName == MEDIA) { - ret = OpenFileByDatashare(path, mode); - if (ret < 0) { - HILOGE("Failed to open file by Datashare error %{public}d", ret); - return NError(-ret); - } - } else { - HILOGE("Failed to open file for libuv error %{public}d", ret); - return NError(ret); - } -#else - HILOGE("Failed to open file for libuv error %{public}d", ret); + OpenByMediaUri(pStr, mode, ret); return NError(ret); -#endif } arg->fd = ret; arg->path = pStr;