From aecf7901ba1a296193d4e3d4a23cc8353016b050 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 26 May 2023 18:13:03 +0800 Subject: [PATCH] =?UTF-8?q?fileAPI=20=E9=80=9A=E7=94=A8=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=AE=E5=A4=8D=20Signed-off-by:=20yangjin?= =?UTF-8?q?gbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic321a54731aa2a1e5a72dc322e102e9e1b896990 --- interfaces/kits/js/src/common/log.h | 2 +- .../mod_fs/class_stream/stream_n_exporter.cpp | 66 ++++++++------ .../kits/js/src/mod_fs/properties/open.cpp | 87 ++++++++++--------- .../js/src/mod_fs/properties/read_text.cpp | 49 ++++++----- .../src/n_async/n_async_work_callback.cpp | 18 ++-- 5 files changed, 123 insertions(+), 99 deletions(-) diff --git a/interfaces/kits/js/src/common/log.h b/interfaces/kits/js/src/common/log.h index 71aacaf4c..f79c1f97a 100644 --- a/interfaces/kits/js/src/common/log.h +++ b/interfaces/kits/js/src/common/log.h @@ -27,7 +27,7 @@ namespace OHOS { namespace DistributedFS { #ifndef FILE_SUBSYSTEM_DEBUG_LOCAL -static constexpr int FILEIO_DOMAIN_ID = 0xD004388; +constexpr int FILEIO_DOMAIN_ID = 0xD004388; static constexpr OHOS::HiviewDFX::HiLogLabel FILEIO_LABEL = { LOG_CORE, FILEIO_DOMAIN_ID, "file_api" }; #ifdef HILOGD diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 6ecdf56b2..0e7e3e125 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -144,6 +144,23 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) return NVal::CreateInt64(env, static_cast(writeLen)).val_; } +static NError WriteExec(shared_ptr arg, void* buf, size_t len, FILE* filp, int64_t offset) +{ + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + return NError(errno); + } + } + arg->actLen = fwrite(buf, 1, len, filp); + if ((arg->actLen == 0) && (arg->actLen != len)) { + HILOGE("Failed to fwrite stream"); + return NError(EIO); + } + return NError(ERRNO_NOERR); +} + napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -177,19 +194,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { - if (offset >= 0) { - int ret = fseek(filp, static_cast(offset), SEEK_SET); - if (ret < 0) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - return NError(errno); - } - } - arg->actLen = fwrite(buf, 1, len, filp); - if ((arg->actLen == 0) && (arg->actLen != len)) { - HILOGE("Failed to fwrite stream"); - return NError(EIO); - } - return NError(ERRNO_NOERR); + return WriteExec(arg, static_cast(buf), len, filp, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { @@ -210,6 +215,25 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) } } +static NError ReadExec(shared_ptr arg, void* buf, size_t len, FILE* filp, int64_t offset) +{ + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + return NError(errno); + } + } + size_t actLen = fread(buf, 1, len, filp); + if ((actLen != len && !feof(filp)) || ferror(filp)) { + HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); + return NError(EIO); + } else { + arg->lenRead = actLen; + return NError(ERRNO_NOERR); + } +} + napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -243,21 +267,7 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { - if (offset >= 0) { - int ret = fseek(filp, static_cast(offset), SEEK_SET); - if (ret < 0) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - return NError(errno); - } - } - size_t actLen = fread(buf, 1, len, filp); - if ((actLen != static_cast(len) && !feof(filp)) || ferror(filp)) { - HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); - return NError(EIO); - } else { - arg->lenRead = actLen; - return NError(ERRNO_NOERR); - } + return ReadExec(arg, static_cast(buf), len, filp, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index ea5f82bc1..566b7693a 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -252,6 +252,51 @@ struct AsyncOpenFileArg { string uri; }; +static NError openExec(shared_ptr arg, const std::string& path, const int32_t mode) +{ + string pathStr = path; + int fd = -1; + if (RemoteUri::IsMediaUri(path)) { + int ret = OpenFileByDatashare(path, mode); + if (ret >= 0) { + arg->fd = ret; + arg->path = ""; + arg->uri = path; + return NError(ERRNO_NOERR); + } + HILOGE("Failed to open file by Datashare"); + return NError(-ret); + } 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(E_INVAL); + } + std::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(), pathStr.c_str(), mode, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP, nullptr); + if (ret < 0) { + HILOGE("Failed to open file for libuv error %{public}d", ret); + return NError(errno); + } + arg->fd = ret; + arg->path = pathStr; + arg->uri = ""; + return NError(ERRNO_NOERR); +} + napi_value Open::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -278,47 +323,7 @@ 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)) { - int ret = OpenFileByDatashare(path, mode); - if (ret >= 0) { - arg->fd = ret; - arg->path = ""; - arg->uri = path; - return NError(ERRNO_NOERR); - } - HILOGE("Failed to open file by Datashare"); - return NError(-ret); - } 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(E_INVAL); - } - std::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(), pathStr.c_str(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP, nullptr); - if (ret < 0) { - HILOGE("Failed to open file for libuv error %{public}d", ret); - return NError(errno); - } - arg->fd = ret; - arg->path = pathStr; - arg->uri = ""; - return NError(ERRNO_NOERR); + return openExec(arg, path, mode); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 262f52da0..c680ae1c1 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -119,28 +119,8 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -149,7 +129,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_open(nullptr, open_req.get(), path.get(), O_RDONLY, + int ret = uv_fs_open(nullptr, open_req.get(), pathStr.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open file by ret: %{public}d", ret); @@ -190,6 +170,31 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, readbuf.base, ret).val_; } +napi_value ReadText::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + HILOGE("Invalid path"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [resGetReadTextArg, offset, hasLen, len, encoding] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + if (!resGetReadTextArg) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + return DoReadText(env, path.get(), offset, hasLen, len); +} + napi_value ReadText::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); diff --git a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp index 3df058b85..1fdd10dd4 100644 --- a/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp +++ b/utils/filemgmt_libn/src/n_async/n_async_work_callback.cpp @@ -18,6 +18,7 @@ #include #include +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "uv.h" @@ -45,9 +46,9 @@ NAsyncWorkCallback::~NAsyncWorkCallback() return; } - unique_ptr work(new (std::nothrow) uv_work_t); + auto work = CreateUniquePtr(); if (work == nullptr) { - HILOGE("Failed to new uv_work_t"); + HILOGE("Failed to request heap memory."); return; } work->data = static_cast(ctx_); @@ -195,17 +196,20 @@ void NAsyncWorkCallback::ThreadSafeSchedule(NContextCBComplete cbComplete) return; } - unique_ptr work(new (std::nothrow) uv_work_t); - if (!work) { - HILOGE("Failed to new uv_work_t"); + auto work = CreateUniquePtr(); + if (work == nullptr) { + HILOGE("Failed to request heap memory."); return; } - struct WorkArgs { NAsyncWorkCallback *ptr = nullptr; NContextCBComplete cb; }; - auto workArgs = make_unique(); + auto workArgs = CreateUniquePtr(); + if (workArgs == nullptr) { + HILOGE("Failed to request heap memory."); + return; + } workArgs->ptr = this; workArgs->cb = cbComplete; -- Gitee