From a1676b91d841d2349bdf443af9ef8bc09a1a51ea Mon Sep 17 00:00:00 2001 From: y30045862 Date: Wed, 17 May 2023 12:06:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?file=5Fapi=20randomAccess=C2=A7=C2=BB?= =?UTF-8?q?=E7=A7=BB=E6=A4=8D=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I6c5a463cc80afc7b78a013ec3d7276b4665e3049 --- interfaces/kits/js/BUILD.gn | 4 +- interfaces/kits/js/src/mod_fileio/module.cpp | 2 - .../mod_fileio/properties/prop_n_exporter.cpp | 3 - .../randomaccessfile_entity.h | 10 +- .../randomaccessfile_n_exporter.cpp | 215 +++++++++++------- .../randomaccessfile_n_exporter.h | 13 +- interfaces/kits/js/src/mod_fs/module.cpp | 2 + .../properties/create_randomaccessfile.cpp | 103 +++++---- .../properties/create_randomaccessfile.h | 10 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 3 + 10 files changed, 219 insertions(+), 146 deletions(-) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_randomaccessfile/randomaccessfile_entity.h (73%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_randomaccessfile/randomaccessfile_n_exporter.cpp (56%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_randomaccessfile/randomaccessfile_n_exporter.h (84%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/properties/create_randomaccessfile.cpp (63%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/properties/create_randomaccessfile.h (75%) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e1b4ec79a..63eb31772 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -48,7 +48,6 @@ ohos_shared_library("fileio") { "src/mod_fileio/class_constants/constants.cpp", "src/mod_fileio/class_dir/dir_n_exporter.cpp", "src/mod_fileio/class_dirent/dirent_n_exporter.cpp", - "src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp", "src/mod_fileio/class_stat/stat_n_exporter.cpp", "src/mod_fileio/class_stream/flush.cpp", "src/mod_fileio/class_stream/stream_n_exporter.cpp", @@ -59,7 +58,6 @@ ohos_shared_library("fileio") { "src/mod_fileio/properties/chown.cpp", "src/mod_fileio/properties/close.cpp", "src/mod_fileio/properties/copy_file.cpp", - "src/mod_fileio/properties/create_randomaccessfile.cpp", "src/mod_fileio/properties/create_stream.cpp", "src/mod_fileio/properties/fchmod.cpp", "src/mod_fileio/properties/fchown.cpp", @@ -116,6 +114,7 @@ ohos_shared_library("fs") { sources = [ "src/common/file_helper/fd_guard.cpp", "src/mod_fs/class_file/file_n_exporter.cpp", + "src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp", "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", @@ -126,6 +125,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/close.cpp", "src/mod_fs/properties/copy_file.cpp", "src/mod_fs/properties/copydir.cpp", + "src/mod_fs/properties/create_randomaccessfile.cpp", "src/mod_fs/properties/create_stream.cpp", "src/mod_fs/properties/fdatasync.cpp", "src/mod_fs/properties/fdopen_stream.cpp", diff --git a/interfaces/kits/js/src/mod_fileio/module.cpp b/interfaces/kits/js/src/mod_fileio/module.cpp index e0abeac25..811cf4aca 100644 --- a/interfaces/kits/js/src/mod_fileio/module.cpp +++ b/interfaces/kits/js/src/mod_fileio/module.cpp @@ -20,7 +20,6 @@ #include "class_constants/constants.h" #include "class_dir/dir_n_exporter.h" #include "class_dirent/dirent_n_exporter.h" -#include "class_randomaccessfile/randomaccessfile_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" #include "class_watcher/watcher_n_exporter.h" @@ -37,7 +36,6 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); - products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp index a7a1acbac..a5e2db5a1 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp @@ -26,7 +26,6 @@ #include "chown.h" #include "close.h" #include "copy_file.h" -#include "create_randomaccessfile.h" #include "create_stream.h" #include "fchmod.h" #include "fchown.h" @@ -683,8 +682,6 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("closeSync", Close::Sync), NVal::DeclareNapiFunction("copyFile", CopyFile::Async), NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), - NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), - NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), NVal::DeclareNapiFunction("createStream", CreateStream::Async), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), diff --git a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_entity.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h similarity index 73% rename from interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_entity.h rename to interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h index 2081bffab..a3101c933 100644 --- a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_entity.h @@ -13,18 +13,16 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H -#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_ENTITY_H #include -#include "../../common/file_helper/fd_guard.h" - namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleFileIO { struct RandomAccessFileEntity { - std::unique_ptr fd_; + std::unique_ptr fd_ = { nullptr }; size_t fpointer = 0; }; } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp similarity index 56% rename from interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp rename to interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp index 2078dea70..71daa85eb 100644 --- a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -22,29 +22,26 @@ #include #include -#include "../../common/log.h" -#include "../../common/napi/n_async/n_async_work_callback.h" -#include "../../common/napi/n_async/n_async_work_promise.h" -#include "../../common/napi/n_class.h" -#include "../../common/napi/n_func_arg.h" -#include "../../common/uni_error.h" #include "../common_func.h" #include "randomaccessfile_entity.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleFileIO { using namespace std; +using namespace OHOS::FileManagement::LibN; static RandomAccessFileEntity *GetRAFEntity(napi_env env, napi_value raf_entity) { auto rafEntity = NClass::GetEntityOf(env, raf_entity); if (!rafEntity) { - UniError(EINVAL).ThrowErr(env, "Cannot get entity of RandomAccessFile"); + HILOGE("Cannot get entity of RandomAccessFile"); + NError(EINVAL).ThrowErr(env); return nullptr; } if (rafEntity->fd_ == 0) { - UniError(EINVAL).ThrowErr(env, "RandomAccessFile has been closed yet"); + HILOGE("RandomAccessFile has been closed yet"); + NError(EINVAL).ThrowErr(env); return nullptr; } return rafEntity; @@ -56,55 +53,52 @@ static tuple ParseJsFP(napi_env env, napi_value FPFromJs) return { succ, fp }; } -static tuple GetRAFReadArg(napi_env env, +static tuple GetRAFReadArg(napi_env env, napi_value ReadBuf, napi_value option) { bool succ = false; - bool hasPos = false; void *buf = nullptr; size_t len = 0; - size_t pos = 0; - size_t offset = 0; - tie(succ, buf, len, hasPos, pos, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); + int64_t offset = -1; + tie(succ, buf, len, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); if (!succ) { - return { false, nullptr, 0, false, 0, 0 }; + return { false, nullptr, 0, 0 }; } - return { true, buf, len, hasPos, pos, offset }; + return { true, buf, len, offset }; } -static tuple GetRAFWriteArg(napi_env env, +static tuple GetRAFWriteArg(napi_env env, napi_value WriteBuf, napi_value option) { bool succ = false; - bool hasPos = false; void *buf = nullptr; size_t len = 0; - size_t pos = 0; - tie(succ, ignore, buf, len, hasPos, pos) = CommonFunc::GetWriteArg(env, WriteBuf, option); + int64_t offset = -1; + tie(succ, ignore, buf, len, offset) = CommonFunc::GetWriteArg(env, WriteBuf, option); if (!succ) { - return { false, nullptr, 0, false, 0 }; + return { false, nullptr, 0, 0 }; } - return { true, buf, len, hasPos, pos }; + return { true, buf, len, offset }; } -static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, size_t pos) +static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, int64_t offset) { uv_loop_s *loop = nullptr; uv_fs_t read_req; napi_get_uv_event_loop(env, &loop); uv_buf_t iov = uv_buf_init(static_cast(buf), len); - int ret = uv_fs_read(loop, &read_req, fd, &iov, 1, pos, NULL); + int ret = uv_fs_read(loop, &read_req, fd, &iov, 1, offset, NULL); uv_fs_req_cleanup(&read_req); return ret; } -static size_t DoWriteRAF(napi_env env, void* buf, size_t len, int fd, size_t pos) +static size_t DoWriteRAF(napi_env env, void* buf, size_t len, int fd, int64_t offset) { uv_loop_s *loop = nullptr; uv_fs_t write_req; napi_get_uv_event_loop(env, &loop); uv_buf_t iov = uv_buf_init(static_cast(buf), len); - size_t ret = uv_fs_write(loop, &write_req, fd, &iov, 1, pos, NULL); + size_t ret = uv_fs_write(loop, &write_req, fd, &iov, 1, offset, NULL); uv_fs_req_cleanup(&write_req); return ret; } @@ -113,7 +107,8 @@ napi_value RandomAccessFileNExporter::GetFD(napi_env env, napi_callback_info inf { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); @@ -127,7 +122,8 @@ napi_value RandomAccessFileNExporter::GetFPointer(napi_env env, napi_callback_in { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); @@ -141,7 +137,8 @@ napi_value RandomAccessFileNExporter::SetFilePointerSync(napi_env env, napi_call { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); @@ -150,7 +147,8 @@ napi_value RandomAccessFileNExporter::SetFilePointerSync(napi_env env, napi_call } auto [succ, fp] = ParseJsFP(env, funcArg[NARG_POS::FIRST]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid fpointer"); + HILOGE("Invalid fpointer"); + NError(EINVAL).ThrowErr(env); return nullptr; } rafEntity->fpointer = fp; @@ -161,22 +159,34 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); if (!rafEntity) { return nullptr; } - auto [succ, buf, len, hasPos, pos, ignore] = + auto [succ, buf, len, offset] = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); + HILOGE("Invalid buffer/options"); + NError(EINVAL).ThrowErr(env); return nullptr; } - int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + pos); + + if (offset < 0) { + offset = 0; + } + + HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF offset = %{public}d ", (int)offset); + HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF len = %{public}d ", (int)len); + HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); + HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); + int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + offset); + HILOGE("RandomAccessFileNExporter::ReadSync: after DoReadRAF actLen = %{public}d ", actLen); if (actLen < 0) { - UniError(errno).ThrowErr(env); + NError(errno).ThrowErr(env); return nullptr; } rafEntity->fpointer += actLen; @@ -185,7 +195,7 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info struct AsyncIOReadArg { size_t lenRead { 0 }; - int offset { 0 }; + int offset { -1 }; NRef refReadBuf; explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} @@ -195,36 +205,53 @@ struct AsyncIOReadArg { static napi_value ReadExec(napi_env env, NFuncArg &funcArg) { auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); + HILOGE("RandomAccessFileNExporter::ReadExec after GetRAFEntity"); if (!rafEntity) { return nullptr; } bool succ = false; - bool hasPos = false; size_t len = 0; - size_t pos = 0; - size_t offset = 0; + int64_t offset = -1; void* buf = nullptr; - tie(succ, buf, len, hasPos, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + + tie(succ, buf, len, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + HILOGE("RandomAccessFileNExporter::ReadExec after GetRAFReadArg"); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); + HILOGE("Invalid buffer/options"); + NError(EINVAL).ThrowErr(env); return nullptr; } + if (offset < 0) { + offset = 0; + } + + HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF offset = %{public}d ", (int)offset); + HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF len = %{public}d ", (int)len); + HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - auto cbExec = [arg, buf, len, hasPos, pos, offset, rafEntity](napi_env env) -> UniError { - int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + pos); + auto cbExec = [env, arg, buf, len, offset, rafEntity]() -> NError { + HILOGE("RandomAccessFileNExporter::ReadExec cbExec len = %{public}d ", (int)len); + HILOGE("RandomAccessFileNExporter::ReadExec cbExec rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); + int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + offset); + HILOGE("RandomAccessFileNExporter::ReadExec cbExec actLen = %{public}d ", (int)(actLen)); if (actLen < 0) { - return UniError(errno); + return NError(errno); } arg->lenRead = actLen; arg->offset = offset; rafEntity->fpointer += actLen; - return UniError(ERRNO_NOERR); + HILOGE("RandomAccessFileNExporter::ReadExec cbExec arg->offset = %{public}d ", (int)(arg->offset)); + HILOGE("RandomAccessFileNExporter::ReadExec cbExec 2 arg->lenRead = %{public}d ", int(arg->lenRead)); + return NError(ERRNO_NOERR); }; - auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { + HILOGE("RandomAccessFileNExporter::ReadExec cbCompl Error "); return { env, err.GetNapiErr(env) }; } + HILOGE("RandomAccessFileNExporter::ReadExec cbCompl bytesRead = %{public}d ", int(arg->lenRead)); + HILOGE("RandomAccessFileNExporter::ReadExec cbCompl offset = %{public}d ", int( arg->offset)); NVal obj = NVal::CreateObject(env); obj.AddProp( { NVal::DeclareNapiProperty("bytesRead", NVal::CreateInt64(env, arg->lenRead).val_), @@ -237,10 +264,12 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + HILOGE("RandomAccessFileNExporter:NAsyncWorkPromise "); return NAsyncWorkPromise(env, thisVar).Schedule(readProcedureName, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); + HILOGE("RandomAccessFileNExporter::NAsyncWorkCallback "); return NAsyncWorkCallback(env, thisVar, cb).Schedule(readProcedureName, cbExec, cbCompl).val_; } } @@ -249,47 +278,61 @@ napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } - + HILOGE("RandomAccessFileNExporter::Read after InitArgs"); return ReadExec(env, funcArg); } napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info info) { + HILOGE("RandomAccessFileNExporter::WriteSync start sync"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } + HILOGE("RandomAccessFileNExporter::WriteSync After InitArgs"); auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); if (!rafEntity) { return nullptr; } + HILOGE("RandomAccessFileNExporter::WriteSync After GetRAFEntity"); bool succ = false; - bool hasPos = false; void *buf = nullptr; size_t len = 0; - size_t pos = 0; - tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + int64_t offset = -1; + tie(succ, buf, len, offset) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + HILOGE("RandomAccessFileNExporter: After GetRAFWriteArg"); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); + HILOGE("Invalid buffer/options"); + NError(EINVAL).ThrowErr(env); return nullptr; } - if (hasPos) { - pos = rafEntity->fpointer + pos; + if (offset > 0) { + HILOGE("RandomAccessFileNExporter::WriteSync check offset > 0"); + offset = rafEntity->fpointer + offset; } else { - pos = rafEntity->fpointer; - } - ssize_t writeLen = DoWriteRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), pos); + HILOGE("RandomAccessFileNExporter::WriteSync check offset < 0"); + offset = rafEntity->fpointer; + } + HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF offset = %{public}d ", (int)offset); + HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF len = %{public}d ", (int)len); + HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); + HILOGE("RandomAccessFileNExporter::WriteSync: before DoWriteRAF rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); + ssize_t writeLen = DoWriteRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), offset); + HILOGE("RandomAccessFileNExporter::WriteSync after DoWriteRAF writeLen = %{public}d ", writeLen); if (writeLen < 0) { - UniError(errno).ThrowErr(env); + NError(errno).ThrowErr(env); return nullptr; } rafEntity->fpointer += writeLen; + HILOGE("RandomAccessFileNExporter::WriteSync before return"); return NVal::CreateInt64(env, writeLen).val_; } @@ -304,7 +347,8 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -313,36 +357,46 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf return nullptr; } bool succ = false; - bool hasPos = false; void *buf = nullptr; size_t len = 0; - size_t pos = 0; - tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + int64_t offset = -1; + tie(succ, buf, len, offset) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); + HILOGE("Invalid buffer/options"); + NError(EINVAL).ThrowErr(env); return nullptr; } - if (hasPos) { - pos = rafEntity->fpointer + pos; + if (offset > 0) { + HILOGE("RandomAccessFileNExporter::Write check offset > 0"); + offset = rafEntity->fpointer + offset; } else { - pos = rafEntity->fpointer; + offset = rafEntity->fpointer; } + HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF offset = %{public}d ", (int)offset); + HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF len = %{public}d ", (int)len); + HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - auto cbExec = [arg, buf, len, fd = rafEntity->fd_.get()->GetFD(), pos, rafEntity](napi_env env) -> UniError { - size_t writeLen = DoWriteRAF(env, buf, len, fd, pos); + auto cbExec = [env, arg, buf, len, fd = rafEntity->fd_.get()->GetFD(), offset, rafEntity]() -> NError { + size_t writeLen = DoWriteRAF(env, buf, len, fd, offset); + HILOGE("RandomAccessFileNExporter::Write cbExec writeLen = %{public}d ", (int)writeLen); + HILOGE("RandomAccessFileNExporter::Write cbExec rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); + HILOGE("RandomAccessFileNExporter::Write cbExec arg->actLen = %{public}d ", (int)(arg->actLen)); if (writeLen < 0) { - return UniError(errno); + return NError(errno); } arg->actLen = writeLen; rafEntity->fpointer += writeLen; - return UniError(ERRNO_NOERR); + HILOGE("RandomAccessFileNExporter::Write cbExec 2 arg->actLen = %{public}d ", int(arg->actLen)); + return NError(ERRNO_NOERR); }; - auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { + HILOGE("RandomAccessFileNExporter::Write cbCompl Error "); return { env, err.GetNapiErr(env) }; } else { + HILOGE("RandomAccessFileNExporter::Write cbCompl arg->actLen = %{public}d ", int(arg->actLen)); return { NVal::CreateInt64(env, arg->actLen) }; } }; @@ -350,10 +404,12 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + HILOGE("RandomAccessFileNExporter::Write NAsyncWorkPromise "); return NAsyncWorkPromise(env, thisVar).Schedule(writeProcedureName, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); + HILOGE("RandomAccessFileNExporter::Write NAsyncWorkCallback "); return NAsyncWorkCallback(env, thisVar, cb).Schedule(writeProcedureName, cbExec, cbCompl).val_; } } @@ -362,7 +418,8 @@ napi_value RandomAccessFileNExporter::CloseSync(napi_env env, napi_callback_info { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); @@ -377,13 +434,15 @@ napi_value RandomAccessFileNExporter::Constructor(napi_env env, napi_callback_in { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto rafEntity = make_unique(); if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { - UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj RandomAccessFile"); + HILOGE("INNER BUG. Failed to wrap entity for obj RandomAccessFile"); + NError(EIO).ThrowErr(env); return nullptr; } return funcArg.GetThisVar(); @@ -408,12 +467,14 @@ bool RandomAccessFileNExporter::Export() tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, RandomAccessFileNExporter::Constructor, move(props)); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class"); + HILOGE("INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); return false; } succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class"); + HILOGE("INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); return false; } diff --git a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h similarity index 84% rename from interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.h rename to interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h index bd89a1b9c..55f84c6ac 100644 --- a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.h @@ -13,14 +13,17 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H -#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_RANDOMACCESSFILE_N_EXPORTER_H -#include "../../common/napi/n_exporter.h" +#include "filemgmt_libhilog.h" +#include "filemgmt_libn.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleFileIO { +using namespace OHOS::FileManagement::LibN; + class RandomAccessFileNExporter final : public NExporter { public: inline static const std::string className_ = "RandomAccessFile"; @@ -47,6 +50,6 @@ public: const std::string readProcedureName = "FileIORandomAccessFileRead"; const std::string writeProcedureName = "FileIORandomAccessFileWrite"; } // namespace ModuleFileIO -} // namespace DistributedFS +} // namespace FileManagement } // namespace OHOS #endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 8f925287d..41b8aa1de 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -19,6 +19,7 @@ #include #include "class_file/file_n_exporter.h" +#include "class_randomaccessfile/randomaccessfile_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" #include "class_watcher/watcher_n_exporter.h" @@ -36,6 +37,7 @@ static napi_value Export(napi_env env, napi_value exports) std::vector> products; products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); diff --git a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp similarity index 63% rename from interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp rename to interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp index 461390164..cb0290e28 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp @@ -21,27 +21,16 @@ #include #include -#include "../../common/log.h" -#include "../../common/napi/n_async/n_async_work_callback.h" -#include "../../common/napi/n_async/n_async_work_promise.h" -#include "../../common/napi/n_class.h" -#include "../../common/napi/n_func_arg.h" -#include "../../common/uni_error.h" -#include "../common_func.h" +#include "common_func.h" #include "../class_randomaccessfile/randomaccessfile_entity.h" #include "../class_randomaccessfile/randomaccessfile_n_exporter.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleFileIO { using namespace std; - -struct FileInfo { - bool isPath = false; - unique_ptr path; - FDGuard fdg; -}; +using namespace OHOS::FileManagement::LibN; static bool CheckFilePath(napi_env env, string path) { @@ -49,11 +38,13 @@ static bool CheckFilePath(napi_env env, string path) struct stat fileStat; int ret = stat(path.c_str(), &fileStat); if (ret != 0) { - UniError(errno).ThrowErr(env, "Cannot stat filepath"); + HILOGE("Cannot stat filepath"); + NError(errno).ThrowErr(env); return false; } if ((fileStat.st_mode & S_IFMT) != S_IFREG) { - UniError(EINVAL).ThrowErr(env, "Invalid filepath"); + HILOGE("Invalid filepath"); + NError(EINVAL).ThrowErr(env); return false; } } @@ -67,33 +58,44 @@ static tuple ParseJsFileAndFP(napi_env env, napi_value p auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String(); if (isPath) { if (CheckFilePath(env, string(path.get()))) { - return { true, FileInfo { true, move(path), {} }, fp }; + HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 1"); + OHOS::DistributedFS::FDGuard sfd; + auto fdg = make_unique(sfd, false); + return { true, FileInfo { true, move(path), move(fdg) }, fp }; } - return { false, FileInfo { false, {}, {} }, -1 }; + HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 2"); + return { false, FileInfo { false, nullptr, nullptr }, -1 }; } auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32(); if (isFd) { if (fd < 0) { - UniError(EINVAL).ThrowErr(env, "Invalid fd"); - return { false, FileInfo { false, {}, {} }, -1 }; + HILOGE("Invalid fd"); + NError(EINVAL).ThrowErr(env); + return { false, FileInfo { false, nullptr, nullptr }, -1 }; } - return { true, FileInfo { false, {}, { fd, false } }, fp }; + auto fdg = make_unique(dup(fd), false); + HILOGE("CreateRandomAccessFile:ParseJsFileAndFP 3 fd = %{public}d ", (int)fd); + HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 3"); + return { true, FileInfo { false, nullptr, move(fdg) }, fp }; } - UniError(EINVAL).ThrowErr(env, "The first argument requires filepath/fd"); + HILOGE("The first argument requires filepath/fd"); + NError(EINVAL).ThrowErr(env); } - UniError(EINVAL).ThrowErr(env, "Invalid fp"); - return { false, FileInfo { false, {}, {} }, -1 }; + HILOGE("Invalid fp"); + NError(EINVAL).ThrowErr(env); + return { false, FileInfo { false, nullptr, nullptr }, -1 }; }; -static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo) +static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo) { - int flags = O_RDONLY; + unsigned int flags = O_RDONLY; bool succ = false; if (fileInfo.isPath) { if (funcArg.GetArgc() >= NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { tie(succ, flags) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid flags"); + HILOGE("Invalid flags"); + NError(EINVAL).ThrowErr(env); return { false, flags }; } (void)CommonFunc::ConvertJsFlags(flags); @@ -106,16 +108,17 @@ static NVal InstantiateRandomAccessFile(napi_env env, int fd, size_t fp) { napi_value objRAF = NClass::InstantiateClass(env, RandomAccessFileNExporter::className_, {}); if (!objRAF) { - UniError(EIO).ThrowErr(env, "Cannot instantiate randomaccessfile"); + HILOGE("Cannot instantiate randomaccessfile"); + NError(EIO).ThrowErr(env); return NVal(); } - auto rafEntity = NClass::GetEntityOf(env, objRAF); if (!rafEntity) { - UniError(EIO).ThrowErr(env, "Cannot instantiate randomaccessfile because of void entity"); + HILOGE("Cannot instantiate randomaccessfile because of void entity"); + NError(EIO).ThrowErr(env); return NVal(); } - auto fdg = make_unique(fd); + auto fdg = make_unique(fd, false); rafEntity->fd_.swap(fdg); rafEntity->fpointer = fp; return { env, objRAF }; @@ -125,14 +128,14 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto [succ, fileInfo, fp] = ParseJsFileAndFP(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { return nullptr; } - if (fileInfo.isPath) { auto [succFlags, flags] = GetJsFlags(env, funcArg, fileInfo); if (!succFlags) { @@ -144,13 +147,14 @@ napi_value CreateRandomAccessFile::Sync(napi_env env, napi_callback_info info) int ret = uv_fs_open(loop, &openReq, fileInfo.path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); if (ret < 0) { - UniError(errno).ThrowErr(env); + NError(errno).ThrowErr(env); return nullptr; } - fileInfo.fdg.SetFD(openReq.result, false); + + fileInfo.fdg->SetFD(openReq.result, false); uv_fs_req_cleanup(&openReq); } - return InstantiateRandomAccessFile(env, fileInfo.fdg.GetFD(), fp).val_; + return InstantiateRandomAccessFile(env, fileInfo.fdg->GetFD(), fp).val_; } struct AsyncCreateRandomAccessFileArg { @@ -162,7 +166,8 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto [succ, fileInfo, fp] = ParseJsFileAndFP(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); @@ -174,8 +179,13 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) return nullptr; } auto arg = make_shared(); - auto cbExec = [arg, fileInfo = make_shared(move(fileInfo)), fp = fp, flags = - flags](napi_env env) -> UniError { + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [env, arg, fileInfo = make_shared(move(fileInfo)), fp = fp, flags = + flags]() -> NError { if (fileInfo->isPath) { uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); @@ -183,16 +193,17 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) int ret = uv_fs_open(loop, &openReq, fileInfo->path.get(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL); if (ret < 0) { - return UniError(errno); + return NError(errno); } - fileInfo->fdg.SetFD(openReq.result, false); + fileInfo->fdg->SetFD(openReq.result, false); uv_fs_req_cleanup(&openReq); } - arg->fd = fileInfo->fdg.GetFD(); + arg->fd = fileInfo->fdg->GetFD(); arg->fp = fp; - return UniError(ERRNO_NOERR); + return NError(ERRNO_NOERR); }; - auto cbCompl = [arg](napi_env env, UniError err) -> NVal { + + auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; } @@ -201,11 +212,11 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) { - return NAsyncWorkPromise(env, thisVar).Schedule(createRAFProcedureName, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_CREATERAT_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::FOUR) ? NARG_POS::FOURTH : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(createRAFProcedureName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_CREATERAT_NAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.h b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.h similarity index 75% rename from interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.h rename to interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.h index f4a9b36ba..c7abbcf1b 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.h +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.h @@ -13,20 +13,20 @@ * limitations under the License. */ -#ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CREATE_RANDOMACCESSFILE_H -#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_CREATE_RANDOMACCESSFILE_H +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_RANDOMACCESSFILE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_RANDOMACCESSFILE_H -#include "../../common/napi/uni_header.h" +#include "filemgmt_libn.h" namespace OHOS { -namespace DistributedFS { +namespace FileManagement { namespace ModuleFileIO { class CreateRandomAccessFile final { public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; -const std::string createRAFProcedureName = "FileIOCreateRandomAccessFile"; +const std::string PROCEDURE_CREATERAT_NAME = "FileIOCreateRandomAccessFile"; } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index f9c145ac9..3223a1439 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -28,6 +28,7 @@ #include "close.h" #include "copy_file.h" #include "copydir.h" +#include "create_randomaccessfile.h" #include "create_stream.h" #include "fdatasync.h" #include "fdopen_stream.h" @@ -563,6 +564,8 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), NVal::DeclareNapiFunction("copyFile", CopyFile::Async), NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), + NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), + NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), NVal::DeclareNapiFunction("createStream", CreateStream::Async), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), NVal::DeclareNapiFunction("fdatasync", Fdatasync::Async), -- Gitee From 9c3726324306a22c1ffd8c4a2a5090a82db9a477 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Wed, 24 May 2023 14:10:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9merge=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I5aa63f0ed94fa06fb35a5555d0715f26d9538d14 --- .../randomaccessfile_n_exporter.cpp | 92 +++++-------------- .../properties/create_randomaccessfile.cpp | 43 +++++---- 2 files changed, 47 insertions(+), 88 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp index 36dbb4211..401636fa1 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -53,19 +53,18 @@ static tuple ParseJsFP(napi_env env, napi_value FPFromJs) return { succ, fp }; } -static tuple GetRAFReadArg(napi_env env, +static tuple GetRAFReadArg(napi_env env, napi_value ReadBuf, napi_value option) { bool succ = false; void *buf = nullptr; size_t len = 0; - int64_t pos = -1; - int64_t offset = 0; - tie(succ, buf, len, pos, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); + int64_t offset = -1; + tie(succ, buf, len, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); if (!succ) { - return { false, nullptr, 0, 0, 0 }; + return { false, nullptr, 0, 0 }; } - return { true, buf, len, pos, offset }; + return { true, buf, len, offset }; } static tuple GetRAFWriteArg(napi_env env, @@ -74,15 +73,15 @@ static tuple GetRAFWriteArg(napi_env env, bool succ = false; void *buf = nullptr; size_t len = 0; - int64_t pos = -1; - tie(succ, ignore, buf, len, pos) = CommonFunc::GetWriteArg(env, WriteBuf, option); + int64_t offset = -1; + tie(succ, ignore, buf, len, offset) = CommonFunc::GetWriteArg(env, WriteBuf, option); if (!succ) { return { false, nullptr, 0, 0 }; } - return { true, buf, len, pos }; + return { true, buf, len, offset }; } -static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, int64_t pos) +static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, int64_t offset) { uv_loop_s *loop = nullptr; uv_fs_t read_req; @@ -93,7 +92,7 @@ static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, int64_t pos) return ret; } -static size_t DoWriteRAF(napi_env env, void* buf, size_t len, int fd, int64_t pos) +static size_t DoWriteRAF(napi_env env, void* buf, size_t len, int fd, int64_t offset) { uv_loop_s *loop = nullptr; uv_fs_t write_req; @@ -168,7 +167,7 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info if (!rafEntity) { return nullptr; } - auto [succ, buf, len, pos, ignore] = + auto [succ, buf, len, offset] = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Invalid buffer/options"); @@ -180,12 +179,7 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info offset = 0; } - HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF offset = %{public}d ", (int)offset); - HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF len = %{public}d ", (int)len); - HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); - HILOGE("RandomAccessFileNExporter::ReadSync: before DoReadRAF rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + offset); - HILOGE("RandomAccessFileNExporter::ReadSync: after DoReadRAF actLen = %{public}d ", actLen); if (actLen < 0) { NError(errno).ThrowErr(env); return nullptr; @@ -206,49 +200,37 @@ struct AsyncIOReadArg { static napi_value ReadExec(napi_env env, NFuncArg &funcArg) { auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); - HILOGE("RandomAccessFileNExporter::ReadExec after GetRAFEntity"); if (!rafEntity) { return nullptr; } bool succ = false; size_t len = 0; - int64_t pos = -1; - int64_t offset = 0; + int64_t offset = -1; void* buf = nullptr; - tie(succ, buf, len, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + tie(succ, buf, len, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Invalid buffer/options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (offset < 0) { - offset = 0; - } + offset = (offset < 0) ? 0 : offset; - HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF offset = %{public}d ", (int)offset); - HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF len = %{public}d ", (int)len); - HILOGE("RandomAccessFileNExporter::ReadExec before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - auto cbExec = [arg, buf, len, pos, offset, rafEntity](napi_env env) -> UniError { - int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + pos); + auto cbExec = [env, arg, buf, len, offset, rafEntity]() -> NError { + int actLen = DoReadRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), rafEntity->fpointer + offset); if (actLen < 0) { return NError(errno); } arg->lenRead = actLen; arg->offset = offset; rafEntity->fpointer += actLen; - HILOGE("RandomAccessFileNExporter::ReadExec cbExec arg->offset = %{public}d ", (int)(arg->offset)); - HILOGE("RandomAccessFileNExporter::ReadExec cbExec 2 arg->lenRead = %{public}d ", int(arg->lenRead)); return NError(ERRNO_NOERR); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { - HILOGE("RandomAccessFileNExporter::ReadExec cbCompl Error "); return { env, err.GetNapiErr(env) }; } - HILOGE("RandomAccessFileNExporter::ReadExec cbCompl bytesRead = %{public}d ", int(arg->lenRead)); - HILOGE("RandomAccessFileNExporter::ReadExec cbCompl offset = %{public}d ", int( arg->offset)); NVal obj = NVal::CreateObject(env); obj.AddProp( { NVal::DeclareNapiProperty("bytesRead", NVal::CreateInt64(env, arg->lenRead).val_), @@ -261,12 +243,10 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { - HILOGE("RandomAccessFileNExporter:NAsyncWorkPromise "); return NAsyncWorkPromise(env, thisVar).Schedule(readProcedureName, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); - HILOGE("RandomAccessFileNExporter::NAsyncWorkCallback "); return NAsyncWorkCallback(env, thisVar, cb).Schedule(readProcedureName, cbExec, cbCompl).val_; } } @@ -279,13 +259,11 @@ napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info NError(EINVAL).ThrowErr(env); return nullptr; } - HILOGE("RandomAccessFileNExporter::Read after InitArgs"); return ReadExec(env, funcArg); } napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info info) { - HILOGE("RandomAccessFileNExporter::WriteSync start sync"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); @@ -293,41 +271,33 @@ napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info return nullptr; } - HILOGE("RandomAccessFileNExporter::WriteSync After InitArgs"); auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); if (!rafEntity) { return nullptr; } - HILOGE("RandomAccessFileNExporter::WriteSync After GetRAFEntity"); bool succ = false; void *buf = nullptr; size_t len = 0; - int64_t pos = -1; - tie(succ, buf, len, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + int64_t offset = -1; + tie(succ, buf, len, offset) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Invalid buffer/options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (pos >= 0) { - pos = rafEntity->fpointer + pos; + if (offset >= 0) { + offset = rafEntity->fpointer + offset; } else { HILOGE("RandomAccessFileNExporter::WriteSync check offset < 0"); offset = rafEntity->fpointer; } - HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF offset = %{public}d ", (int)offset); - HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF len = %{public}d ", (int)len); - HILOGE("RandomAccessFileNExporter::WriteSync before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); - HILOGE("RandomAccessFileNExporter::WriteSync: before DoWriteRAF rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); ssize_t writeLen = DoWriteRAF(env, buf, len, rafEntity->fd_.get()->GetFD(), offset); - HILOGE("RandomAccessFileNExporter::WriteSync after DoWriteRAF writeLen = %{public}d ", writeLen); if (writeLen < 0) { NError(errno).ThrowErr(env); return nullptr; } rafEntity->fpointer += writeLen; - HILOGE("RandomAccessFileNExporter::WriteSync before return"); return NVal::CreateInt64(env, writeLen).val_; } @@ -354,43 +324,31 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf bool succ = false; void *buf = nullptr; size_t len = 0; - int64_t pos = -1; - tie(succ, buf, len, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + int64_t offset = -1; + tie(succ, buf, len, offset) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Invalid buffer/options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (pos >= 0) { - pos = rafEntity->fpointer + pos; - } else { - offset = rafEntity->fpointer; - } - HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF offset = %{public}d ", (int)offset); - HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF len = %{public}d ", (int)len); - HILOGE("RandomAccessFileNExporter::Write before DoWriteRAF rafEntity->fd_.get()->GetFD() = %{public}d ", rafEntity->fd_.get()->GetFD()); + offset = (offset >= 0) ? rafEntity->fpointer + offset : rafEntity->fpointer; + auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); auto cbExec = [env, arg, buf, len, fd = rafEntity->fd_.get()->GetFD(), offset, rafEntity]() -> NError { size_t writeLen = DoWriteRAF(env, buf, len, fd, offset); - HILOGE("RandomAccessFileNExporter::Write cbExec writeLen = %{public}d ", (int)writeLen); - HILOGE("RandomAccessFileNExporter::Write cbExec rafEntity->fpointer = %{public}d ", (int)(rafEntity->fpointer)); - HILOGE("RandomAccessFileNExporter::Write cbExec arg->actLen = %{public}d ", (int)(arg->actLen)); if (writeLen < 0) { return NError(errno); } arg->actLen = writeLen; rafEntity->fpointer += writeLen; - HILOGE("RandomAccessFileNExporter::Write cbExec 2 arg->actLen = %{public}d ", int(arg->actLen)); return NError(ERRNO_NOERR); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { - HILOGE("RandomAccessFileNExporter::Write cbCompl Error "); return { env, err.GetNapiErr(env) }; } else { - HILOGE("RandomAccessFileNExporter::Write cbCompl arg->actLen = %{public}d ", int(arg->actLen)); return { NVal::CreateInt64(env, arg->actLen) }; } }; @@ -398,12 +356,10 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { - HILOGE("RandomAccessFileNExporter::Write NAsyncWorkPromise "); return NAsyncWorkPromise(env, thisVar).Schedule(writeProcedureName, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); - HILOGE("RandomAccessFileNExporter::Write NAsyncWorkCallback "); return NAsyncWorkCallback(env, thisVar, cb).Schedule(writeProcedureName, cbExec, cbCompl).val_; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp index 3209c1479..592717aab 100644 --- a/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_randomaccessfile.cpp @@ -58,12 +58,10 @@ static tuple ParseJsFileAndFP(napi_env env, napi_value p auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String(); if (isPath) { if (CheckFilePath(env, string(path.get()))) { - HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 1"); OHOS::DistributedFS::FDGuard sfd; auto fdg = make_unique(sfd, false); return { true, FileInfo { true, move(path), move(fdg) }, fp }; } - HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 2"); return { false, FileInfo { false, nullptr, nullptr }, -1 }; } auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32(); @@ -74,8 +72,6 @@ static tuple ParseJsFileAndFP(napi_env env, napi_value p return { false, FileInfo { false, nullptr, nullptr }, -1 }; } auto fdg = make_unique(dup(fd), false); - HILOGE("CreateRandomAccessFile:ParseJsFileAndFP 3 fd = %{public}d ", (int)fd); - HILOGE("CreateRandomAccessFile: ParseJsFileAndFP 3"); return { true, FileInfo { false, nullptr, move(fdg) }, fp }; } HILOGE("The first argument requires filepath/fd"); @@ -93,7 +89,8 @@ static tuple GetJsFlags(napi_env env, const NFuncArg &funcAr if (funcArg.GetArgc() >= NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { auto [succ, mode] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); if (!succ || mode < 0) { - UniError(EINVAL).ThrowErr(env, "Invalid flags"); + HILOGE("Invalid flags"); + NError(EINVAL).ThrowErr(env); return { false, flags }; } flags = static_cast(mode); @@ -161,6 +158,26 @@ struct AsyncCreateRandomAccessFileArg { size_t fp; }; +NError AsyncExec(napi_env env, shared_ptr arg, + shared_ptr fileInfo, size_t fp, unsigned int flags) +{ + if (fileInfo->isPath) { + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env, &loop); + uv_fs_t openReq; + int ret = uv_fs_open(loop, &openReq, fileInfo->path.get(), flags, S_IRUSR | + S_IWUSR | S_IRGRP | S_IWGRP, NULL); + if (ret < 0) { + return NError(errno); + } + fileInfo->fdg->SetFD(openReq.result, false); + uv_fs_req_cleanup(&openReq); + } + arg->fd = fileInfo->fdg->GetFD(); + arg->fp = fp; + return NError(ERRNO_NOERR); +} + napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -185,21 +202,7 @@ napi_value CreateRandomAccessFile::Async(napi_env env, napi_callback_info info) } auto cbExec = [env, arg, fileInfo = make_shared(move(fileInfo)), fp = fp, flags = flags]() -> NError { - if (fileInfo->isPath) { - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env, &loop); - uv_fs_t openReq; - int ret = uv_fs_open(loop, &openReq, fileInfo->path.get(), flags, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP, NULL); - if (ret < 0) { - return NError(errno); - } - fileInfo->fdg->SetFD(openReq.result, false); - uv_fs_req_cleanup(&openReq); - } - arg->fd = fileInfo->fdg->GetFD(); - arg->fp = fp; - return NError(ERRNO_NOERR); + return AsyncExec(env, arg, fileInfo, fp, flags); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { -- Gitee