From 056666ffe7f0ba0643ed38ba74c62b01c6177d66 Mon Sep 17 00:00:00 2001 From: fengjq Date: Tue, 18 Apr 2023 11:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81option=E4=BC=A0=E5=85=A5undef?= =?UTF-8?q?ined=EF=BC=8C=E8=BF=94=E5=9B=9E=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengjq --- interfaces/kits/js/src/common/napi/n_val.cpp | 34 +++ interfaces/kits/js/src/common/napi/n_val.h | 6 + .../randomaccessfile_n_exporter.cpp | 55 ++-- .../class_stream/stream_n_exporter.cpp | 85 +++--- .../kits/js/src/mod_fileio/common_func.cpp | 158 +++++----- .../kits/js/src/mod_fileio/common_func.h | 14 +- .../js/src/mod_fileio/properties/close.cpp | 4 +- .../src/mod_fileio/properties/copy_file.cpp | 64 ++--- .../properties/create_randomaccessfile.cpp | 8 +- .../mod_fileio/properties/fdopen_stream.cpp | 2 +- .../js/src/mod_fileio/properties/fstat.cpp | 4 +- .../js/src/mod_fileio/properties/fsync.cpp | 4 +- .../src/mod_fileio/properties/ftruncate.cpp | 36 ++- .../js/src/mod_fileio/properties/open.cpp | 78 ++--- .../mod_fileio/properties/prop_n_exporter.cpp | 271 ++++++------------ .../mod_fileio/properties/prop_n_exporter.h | 3 +- .../src/mod_fileio/properties/read_text.cpp | 84 +++--- .../js/src/mod_fileio/properties/read_text.h | 6 +- .../js/src/mod_fileio/properties/truncate.cpp | 32 +-- 19 files changed, 400 insertions(+), 548 deletions(-) diff --git a/interfaces/kits/js/src/common/napi/n_val.cpp b/interfaces/kits/js/src/common/napi/n_val.cpp index d05ff5221..5e068e708 100644 --- a/interfaces/kits/js/src/common/napi/n_val.cpp +++ b/interfaces/kits/js/src/common/napi/n_val.cpp @@ -72,6 +72,16 @@ tuple, size_t> NVal::ToUTF8String() const return make_tuple(status == napi_ok, move(str), strLen); } +tuple, size_t> NVal::ToUTF8String(string_view defaultValue) const +{ + if (TypeIs(napi_undefined) || TypeIs(napi_function)) { + auto str = make_unique(defaultValue.size() + 1); + copy(defaultValue.begin(), defaultValue.end(), str.get()); + return make_tuple(true, move(str), defaultValue.size()); + } + return ToUTF8String(); +} + tuple, size_t> NVal::ToUTF16String() const { #ifdef FILE_SUBSYSTEM_DEBUG_LOCAL @@ -110,6 +120,14 @@ tuple NVal::ToBool() const return make_tuple(status == napi_ok, flag); } +tuple NVal::ToBool(bool defaultValue) const +{ + if (TypeIs(napi_undefined) || TypeIs(napi_function)) { + return make_tuple(true, defaultValue); + } + return ToBool(); +} + tuple NVal::ToInt32() const { int32_t res = 0; @@ -117,6 +135,14 @@ tuple NVal::ToInt32() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToInt32(int32_t defaultValue) const +{ + if (TypeIs(napi_undefined) || TypeIs(napi_function)) { + return make_tuple(true, defaultValue); + } + return ToInt32(); +} + tuple NVal::ToInt64() const { int64_t res = 0; @@ -124,6 +150,14 @@ tuple NVal::ToInt64() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToInt64(int64_t defaultValue) const +{ + if (TypeIs(napi_undefined) || TypeIs(napi_function)) { + return make_tuple(true, defaultValue); + } + return ToInt64(); +} + tuple NVal::ToDouble() const { double res = 0; diff --git a/interfaces/kits/js/src/common/napi/n_val.h b/interfaces/kits/js/src/common/napi/n_val.h index 8fd57dc37..89d6a4ceb 100644 --- a/interfaces/kits/js/src/common/napi/n_val.h +++ b/interfaces/kits/js/src/common/napi/n_val.h @@ -16,6 +16,8 @@ #ifndef N_VAL_H #define N_VAL_H +#include + #include "sys/types.h" #include "uni_header.h" @@ -39,11 +41,15 @@ public: /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ std::tuple, size_t> ToUTF8String() const; + std::tuple, size_t> ToUTF8String(std::string_view defaultValue) const; std::tuple, size_t> ToUTF16String() const; std::tuple ToPointer() const; std::tuple ToBool() const; + std::tuple ToBool(bool defaultValue) const; std::tuple ToInt32() const; + std::tuple ToInt32(int32_t defaultValue) const; std::tuple ToInt64() const; + std::tuple ToInt64(int64_t defaultValue) const; std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); diff --git a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp index 2078dea70..e5ff8f32b 100644 --- a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -56,38 +56,36 @@ 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 pos = -1; + int64_t offset = 0; + tie(succ, buf, len, pos, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); if (!succ) { - return { false, nullptr, 0, false, 0, 0 }; + return { false, nullptr, 0, 0, 0 }; } - return { true, buf, len, hasPos, pos, offset }; + return { true, buf, len, pos, 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 pos = -1; + tie(succ, ignore, buf, len, pos) = 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, pos }; } -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 pos) { uv_loop_s *loop = nullptr; uv_fs_t read_req; @@ -98,7 +96,7 @@ static int DoReadRAF(napi_env env, void* buf, size_t len, int fd, size_t pos) 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 pos) { uv_loop_s *loop = nullptr; uv_fs_t write_req; @@ -168,7 +166,7 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info if (!rafEntity) { return nullptr; } - auto [succ, buf, len, hasPos, pos, ignore] = + auto [succ, buf, len, pos, ignore] = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); @@ -185,7 +183,7 @@ napi_value RandomAccessFileNExporter::ReadSync(napi_env env, napi_callback_info struct AsyncIOReadArg { size_t lenRead { 0 }; - int offset { 0 }; + int64_t offset { 0 }; NRef refReadBuf; explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} @@ -199,19 +197,18 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg) return nullptr; } bool succ = false; - bool hasPos = false; size_t len = 0; - size_t pos = 0; - size_t offset = 0; + int64_t pos = -1; + int64_t offset = 0; void* buf = nullptr; - tie(succ, buf, len, hasPos, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); + tie(succ, buf, len, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); return nullptr; } auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - auto cbExec = [arg, buf, len, hasPos, pos, offset, rafEntity](napi_env env) -> UniError { + 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); if (actLen < 0) { return UniError(errno); @@ -270,16 +267,15 @@ napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info } 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 pos = -1; + tie(succ, buf, len, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); return nullptr; } - if (hasPos) { + if (pos >= 0) { pos = rafEntity->fpointer + pos; } else { pos = rafEntity->fpointer; @@ -313,16 +309,15 @@ 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 pos = -1; + tie(succ, buf, len, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); return nullptr; } - if (hasPos) { + if (pos >= 0) { pos = rafEntity->fpointer + pos; } else { pos = rafEntity->fpointer; diff --git a/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp index f9d6960b8..7371eedcf 100644 --- a/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp @@ -41,7 +41,6 @@ using namespace std; napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; @@ -59,22 +58,21 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info info) } void *buf = nullptr; - int64_t len; - bool hasPos = false; - int64_t pos; - tie(succ, buf, len, hasPos, pos, ignore) = + size_t len = 0; + int64_t pos = -1; + tie(succ, buf, len, pos, ignore) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { return nullptr; } - if (hasPos && (fseek(filp, pos, SEEK_SET) == -1)) { + if (pos >= 0 && (fseek(filp, static_cast(pos), SEEK_SET) == -1)) { UniError(errno).ThrowErr(env); return nullptr; } size_t actLen = fread(buf, 1, len, filp); - if (actLen != static_cast(len) && ferror(filp)) { + if ((actLen != len && !feof(filp)) || ferror(filp)) { UniError(errno).ThrowErr(env); } @@ -84,7 +82,6 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info info) napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO)) { UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; @@ -118,22 +115,21 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info info) } void *buf = nullptr; - size_t len; - size_t position; - unique_ptr bufGuard; - bool hasPos = false; - tie(succ, bufGuard, buf, len, hasPos, position) = + size_t len = 0; + int64_t position = -1; + unique_ptr bufGuard = nullptr; + tie(succ, bufGuard, buf, len, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { return nullptr; } - if (hasPos && (fseek(filp, position, SEEK_SET) == -1)) { + if (position >= 0 && (fseek(filp, static_cast(position), SEEK_SET) == -1)) { UniError(errno).ThrowErr(env); return nullptr; } size_t writeLen = fwrite(buf, 1, len, filp); - if (writeLen != len) { + if ((writeLen == 0) && (writeLen != len)) { UniError(errno).ThrowErr(env); return nullptr; } @@ -143,7 +139,7 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info info) struct AsyncWrtieArg { NRef refWriteArrayBuf; - unique_ptr guardWriteStr; + unique_ptr guardWriteStr = nullptr; size_t actLen { 0 }; explicit AsyncWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {} @@ -161,8 +157,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) bool succ = false; FILE *filp = nullptr; - bool hasPosition = false; - size_t position; + int64_t position = -1; auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { UniError(EBADF).ThrowErr(env, "Stream may has been closed"); @@ -170,29 +165,23 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) } filp = streamEntity->fp.get(); - unique_ptr bufGuard; + unique_ptr bufGuard = nullptr; void *buf = nullptr; - size_t len; - tie(succ, bufGuard, buf, len, hasPosition, position) = + size_t len = 0; + tie(succ, bufGuard, buf, len, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { return nullptr; } - shared_ptr arg; - if (bufGuard) { - arg = make_shared(move(bufGuard)); - } else { - arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - } - - auto cbExec = [arg, buf, len, filp, hasPosition, position](napi_env env) -> UniError { - if (hasPosition && (fseek(filp, position, SEEK_SET) == -1)) { + auto arg = make_shared(move(bufGuard)); + auto cbExec = [arg, buf, len, filp, position](napi_env env) -> UniError { + if (position >= 0 && (fseek(filp, static_cast(position), SEEK_SET) == -1)) { UniError(errno).ThrowErr(env); return UniError(errno); } arg->actLen = fwrite(buf, 1, len, filp); - if (arg->actLen != static_cast(len) && ferror(filp)) { + if ((arg->actLen == 0) && (arg->actLen != len)) { return UniError(errno); } return UniError(ERRNO_NOERR); @@ -206,10 +195,12 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() != NARG_CNT::THREE) { + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOStreamWrite", cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); + NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOStreamWrite", cbExec, cbCompl).val_; } @@ -219,7 +210,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) struct AsyncReadArg { size_t lenRead { 0 }; NRef refReadBuf; - int offset { 0 }; + int64_t offset { 0 }; explicit AsyncReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} ~AsyncReadArg() = default; @@ -234,36 +225,33 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) } /* To get entity */ - FILE *filp = nullptr; auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { UniError(EBADF).ThrowErr(env, "Stream may have been closed"); return nullptr; - } else { - filp = streamEntity->fp.get(); } + FILE *filp = nullptr; + filp = streamEntity->fp.get(); bool succ = false; void *buf = nullptr; - int64_t len; - bool hasPosition = false; - size_t position; - int offset; - tie(succ, buf, len, hasPosition, position, offset) = + size_t len = 0; + int64_t position = -1; + int64_t offset = 0; + tie(succ, buf, len, position, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Failed GetReadArg"); return nullptr; } auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - auto cbExec = [arg, buf, position, filp, len, hasPosition, offset](napi_env env) -> UniError { - if (hasPosition && (fseek(filp, position, SEEK_SET) == -1)) { + auto cbExec = [arg, buf, position, filp, len, offset](napi_env env) -> UniError { + if (position >= 0 && (fseek(filp, static_cast(position), SEEK_SET) == -1)) { UniError(errno).ThrowErr(env); return UniError(errno); } size_t actLen = fread(buf, 1, len, filp); - if (actLen != static_cast(len) && ferror(filp)) { + if ((actLen != len && !feof(filp)) || ferror(filp)) { return UniError(errno); } else { arg->lenRead = actLen; @@ -286,10 +274,11 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() != NARG_CNT::THREE) { + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOStreamRead", cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); + NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOStreamRead", cbExec, cbCompl).val_; } diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index e03d5c9de..f9051717f 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -53,17 +53,17 @@ void InitOpenMode(napi_env env, napi_value exports) napi_set_named_property(env, exports, propertyName, obj); } -static tuple GetActualBuf(napi_env env, void *rawBuf, int64_t bufLen, NVal op) +static tuple GetActualBuf(napi_env env, void *rawBuf, size_t bufLen, NVal op) { bool succ = false; void *realBuf = nullptr; int64_t opOffset = 0; if (op.HasProp("offset")) { - tie(succ, opOffset) = op.GetProp("offset").ToInt64(); + tie(succ, opOffset) = op.GetProp("offset").ToInt64(opOffset); if (!succ || opOffset < 0) { UniError(EINVAL).ThrowErr(env, "Invalid option.offset, positive integer is desired"); return { false, nullptr, opOffset }; - } else if (opOffset > bufLen) { + } else if (opOffset > static_cast(bufLen)) { UniError(EINVAL).ThrowErr(env, "Invalid option.offset, buffer limit exceeded"); return { false, nullptr, opOffset }; } else { @@ -76,30 +76,20 @@ static tuple GetActualBuf(napi_env env, void *rawBuf, int64_t return { true, realBuf, opOffset }; } -static tuple GetActualLen(napi_env env, int64_t bufLen, int64_t bufOff, NVal op) +static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufOff, NVal op) { bool succ = false; - int64_t retLen; + size_t retLen = bufLen - bufOff; if (op.HasProp("length")) { - int64_t opLength; - tie(succ, opLength) = op.GetProp("length").ToInt64(); - if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid option.length, expect integer"); - return { false, 0 }; - } - if (opLength < 0) { - retLen = bufLen - bufOff; - } else if (opLength > bufLen - bufOff) { - UniError(EINVAL).ThrowErr(env, "Invalid option.length, buffer limit exceeded"); + int64_t opLength = 0; + tie(succ, opLength) = op.GetProp("length").ToInt64(static_cast(retLen)); + if (!succ || opLength < 0 || static_cast(opLength) > retLen) { + UniError(EINVAL).ThrowErr(env, "Invalid option.length"); return { false, 0 }; - } else { - retLen = opLength; } - } else { - retLen = bufLen - bufOff; + retLen = static_cast(opLength); } - return { true, retLen }; } @@ -132,32 +122,31 @@ static tuple GetActualLenV9(napi_env env, int64_t bufLen, int64_t return { true, retLen }; } -int CommonFunc::ConvertJsFlags(int &flags) +unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) { - static constexpr int USR_O_RDONLY = 00; - static constexpr int USR_O_WRONLY = 01; - static constexpr int USR_O_RDWR = 02; - static constexpr int USR_O_CREAT = 0100; - static constexpr int USR_O_EXCL = 0200; - static constexpr int USR_O_TRUNC = 01000; - static constexpr int USR_O_APPEND = 02000; - static constexpr int USR_O_NONBLOCK = 04000; - static constexpr int USR_O_DIRECTORY = 0200000; - static constexpr int USR_O_NOFOLLOW = 0400000; - static constexpr int USR_O_SYNC = 04010000; - - int flagsABI = 0; - flagsABI |= ((flags & USR_O_RDONLY) == USR_O_RDONLY) ? O_RDONLY : 0; - flagsABI |= ((flags & USR_O_WRONLY) == USR_O_WRONLY) ? O_WRONLY : 0; - flagsABI |= ((flags & USR_O_RDWR) == USR_O_RDWR) ? O_RDWR : 0; - flagsABI |= ((flags & USR_O_CREAT) == USR_O_CREAT) ? O_CREAT : 0; - flagsABI |= ((flags & USR_O_EXCL) == USR_O_EXCL) ? O_EXCL : 0; - flagsABI |= ((flags & USR_O_TRUNC) == USR_O_TRUNC) ? O_TRUNC : 0; - flagsABI |= ((flags & USR_O_APPEND) == USR_O_APPEND) ? O_APPEND : 0; - flagsABI |= ((flags & USR_O_NONBLOCK) == USR_O_NONBLOCK) ? O_NONBLOCK : 0; - flagsABI |= ((flags & USR_O_DIRECTORY) == USR_O_DIRECTORY) ? O_DIRECTORY : 0; - flagsABI |= ((flags & USR_O_NOFOLLOW) == USR_O_NOFOLLOW) ? O_NOFOLLOW : 0; - flagsABI |= ((flags & USR_O_SYNC) == USR_O_SYNC) ? O_SYNC : 0; + static constexpr unsigned int usrWriteOnly = 01; + static constexpr unsigned int usrReadWrite = 02; + static constexpr unsigned int usrCreate = 0100; + static constexpr unsigned int usrExecuteLock = 0200; + static constexpr unsigned int usrTruncate = 01000; + static constexpr unsigned int usrAppend = 02000; + static constexpr unsigned int usrNoneBlock = 04000; + static constexpr unsigned int usrDirectory = 0200000; + static constexpr unsigned int usrNoFollowed = 0400000; + static constexpr unsigned int usrSynchronous = 04010000; + + // default value is usrReadOnly 00 + unsigned int flagsABI = 0; + flagsABI |= ((flags & usrWriteOnly) == usrWriteOnly) ? O_WRONLY : 0; + flagsABI |= ((flags & usrReadWrite) == usrReadWrite) ? O_RDWR : 0; + flagsABI |= ((flags & usrCreate) == usrCreate) ? O_CREAT : 0; + flagsABI |= ((flags & usrExecuteLock) == usrExecuteLock) ? O_EXCL : 0; + flagsABI |= ((flags & usrTruncate) == usrTruncate) ? O_TRUNC : 0; + flagsABI |= ((flags & usrAppend) == usrAppend) ? O_APPEND : 0; + flagsABI |= ((flags & usrNoneBlock) == usrNoneBlock) ? O_NONBLOCK : 0; + flagsABI |= ((flags & usrDirectory) == usrDirectory) ? O_DIRECTORY : 0; + flagsABI |= ((flags & usrNoFollowed) == usrNoFollowed) ? O_NOFOLLOW : 0; + flagsABI |= ((flags & usrSynchronous) == usrSynchronous) ? O_SYNC : 0; flags = flagsABI; return flagsABI; } @@ -181,69 +170,63 @@ tuple, unique_ptr> CommonFunc::GetCopyPathArg(n return make_tuple(true, move(src), move(dest)); } -tuple CommonFunc::GetReadArg(napi_env env, - napi_value readBuf, - napi_value option) +tuple CommonFunc::GetReadArg(napi_env env, + napi_value readBuf, + napi_value option) { bool succ = false; void *retBuf = nullptr; - int64_t retLen = 0; - bool posAssigned = false; - int64_t position = 0; + size_t retLen = 0; + int64_t position = -1; NVal txt(env, readBuf); void *buf = nullptr; - int64_t bufLen = 0; - int offset = 0; + size_t bufLen = 0; + int64_t offset = 0; tie(succ, buf, bufLen) = txt.ToArraybuffer(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid read buffer, expect arraybuffer"); - return { false, nullptr, 0, posAssigned, position, offset }; + return { false, nullptr, 0, position, offset }; } - NVal op = NVal(env, option); + NVal op(env, option); tie(succ, retBuf, offset) = GetActualBuf(env, buf, bufLen, op); if (!succ) { - return { false, nullptr, 0, posAssigned, position, offset }; + return { false, nullptr, 0, position, offset }; } int64_t bufOff = static_cast(retBuf) - static_cast(buf); tie(succ, retLen) = GetActualLen(env, bufLen, bufOff, op); if (!succ) { - return { false, nullptr, 0, posAssigned, position, offset }; + return { false, nullptr, 0, position, offset }; } - if (op.HasProp("position")) { + if (op.HasProp("position") && !op.GetProp("position").TypeIs(napi_undefined)) { tie(succ, position) = op.GetProp("position").ToInt64(); - if (succ && position >= 0) { - posAssigned = true; - } else { + if (!succ || position < 0) { UniError(EINVAL).ThrowErr(env, "option.position shall be positive number"); - return { false, nullptr, 0, posAssigned, position, offset }; + return { false, nullptr, 0, position, offset }; } } - return { true, retBuf, retLen, posAssigned, position, offset }; + return { true, retBuf, retLen, position, offset }; } -static tuple, int64_t> DecodeString(napi_env env, NVal jsStr, NVal encoding) +static tuple, size_t> DecodeString(napi_env env, NVal jsStr, NVal encoding) { - unique_ptr buf; + unique_ptr buf = nullptr; if (!jsStr.TypeIs(napi_string)) { return { false, nullptr, 0 }; } - - bool succ = false; if (!encoding) { return jsStr.ToUTF8String(); } - unique_ptr encodingBuf; - tie(succ, encodingBuf, ignore) = encoding.ToUTF8String(); + auto [succ, encodingBuf, ignore] = encoding.ToUTF8String("utf-8"); if (!succ) { return { false, nullptr, 0 }; } - string encodingStr(encodingBuf.release()); + string_view encodingStr(encodingBuf.release()); if (encodingStr == "utf-8") { return jsStr.ToUTF8String(); } else if (encodingStr == "utf-16") { @@ -253,27 +236,25 @@ static tuple, int64_t> DecodeString(napi_env env, NVal } } -tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, - napi_value argWBuf, - napi_value argOption) +tuple, void *, size_t, int64_t> CommonFunc::GetWriteArg(napi_env env, + napi_value argWBuf, + napi_value argOption) { void *retBuf = nullptr; - int64_t retLen = 0; - bool hasPos = false; - int64_t retPos = 0; - + size_t retLen = 0; + int64_t position = -1; bool succ = false; void *buf = nullptr; - int64_t bufLen = 0; + size_t bufLen = 0; NVal op(env, argOption); NVal jsBuffer(env, argWBuf); - unique_ptr bufferGuard; + unique_ptr bufferGuard = nullptr; tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetProp("encoding")); if (!succ) { tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Illegal write buffer or encoding"); - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, position }; } } else { buf = bufferGuard.get(); @@ -281,28 +262,23 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW tie(succ, retBuf, ignore) = GetActualBuf(env, buf, bufLen, op); if (!succ) { - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, position }; } int64_t bufOff = static_cast(retBuf) - static_cast(buf); tie(succ, retLen) = GetActualLen(env, bufLen, bufOff, op); if (!succ) { - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, position }; } - if (op.HasProp("position")) { - int32_t position = 0; - tie(succ, position) = op.GetProp("position").ToInt32(); + if (op.HasProp("position") && !op.GetProp("position").TypeIs(napi_undefined)) { + tie(succ, position) = op.GetProp("position").ToInt64(); if (!succ || position < 0) { UniError(EINVAL).ThrowErr(env, "option.position shall be positive number"); - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, position }; } - hasPos = true; - retPos = position; - } else { - retPos = INVALID_POSITION; } - return { true, move(bufferGuard), retBuf, retLen, hasPos, retPos }; + return { true, move(bufferGuard), retBuf, retLen, position }; } tuple CommonFunc::GetReadArgV9(napi_env env, diff --git a/interfaces/kits/js/src/mod_fileio/common_func.h b/interfaces/kits/js/src/mod_fileio/common_func.h index 2ef467d28..b76ad5469 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.h +++ b/interfaces/kits/js/src/mod_fileio/common_func.h @@ -36,16 +36,16 @@ constexpr int SYNC = 04010000; void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { - static int ConvertJsFlags(int &flags); - static std::tuple GetReadArg(napi_env env, - napi_value readBuf, - napi_value option); + static unsigned int ConvertJsFlags(unsigned int &flags); + static std::tuple GetReadArg(napi_env env, + napi_value readBuf, + napi_value option); static std::tuple GetReadArgV9(napi_env env, napi_value readBuf, napi_value option); - static std::tuple, void *, int64_t, bool, int64_t> GetWriteArg(napi_env env, - napi_value argWBuf, - napi_value argOption); + static std::tuple, void *, size_t, int64_t> GetWriteArg(napi_env env, + napi_value argWBuf, + napi_value argOption); static std::tuple, void *, int64_t, bool, int64_t> GetWriteArgV9(napi_env env, napi_value argWBuf, napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, diff --git a/interfaces/kits/js/src/mod_fileio/properties/close.cpp b/interfaces/kits/js/src/mod_fileio/properties/close.cpp index 202ad04bc..802466024 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/close.cpp @@ -36,7 +36,7 @@ napi_value Close::Sync(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } @@ -58,7 +58,7 @@ napi_value Close::Async(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fileio/properties/copy_file.cpp index b9be7456f..b81501a40 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/copy_file.cpp @@ -39,20 +39,18 @@ constexpr int COPY_BLOCK_SIZE = 4096; struct FileInfo { bool isPath = false; - unique_ptr path; + unique_ptr path = nullptr; FDGuard fdg; }; static UniError CopyFileCore(FileInfo &srcFile, FileInfo &destFile) { - int res = EINVAL; if (srcFile.isPath) { - srcFile.fdg.SetFD(open(srcFile.path.get(), O_RDONLY), true); - res = errno; - } - - if (!srcFile.fdg) { - return UniError(res); + int ret = open(srcFile.path.get(), O_RDONLY); + if (ret < 0) { + return UniError(errno); + } + srcFile.fdg.SetFD(ret, true); } struct stat statbf; @@ -61,12 +59,11 @@ static UniError CopyFileCore(FileInfo &srcFile, FileInfo &destFile) } if (destFile.isPath) { - destFile.fdg.SetFD(open(destFile.path.get(), O_WRONLY | O_CREAT, statbf.st_mode), true); - res = errno; - } - - if (!destFile.fdg) { - return UniError(res); + int ret = open(destFile.path.get(), O_WRONLY | O_CREAT, statbf.st_mode); + if (ret < 0) { + return UniError(errno); + } + destFile.fdg.SetFD(ret, true); } auto copyBuf = make_unique(COPY_BLOCK_SIZE); @@ -89,27 +86,17 @@ static UniError CopyFileCore(FileInfo &srcFile, FileInfo &destFile) return UniError(ERRNO_NOERR); } -static tuple ParseJsModeAndProm(napi_env env, const NFuncArg &funcArg) +static tuple ParseJsModeAndProm(napi_env env, const NFuncArg &funcArg) { bool succ = false; - bool promise = false; - bool hasMode = false; - int mode = 0; - if (funcArg.GetArgc() == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) { - promise = true; - hasMode = true; - } else if (funcArg.GetArgc() == NARG_CNT::FOUR) { - hasMode = true; - } - - if (hasMode) { - tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); - if (!succ) { - return {false, mode, promise}; + int32_t mode = 0; + if (funcArg.GetArgc() >= NARG_CNT::THREE) { + tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); + if (!succ || mode) { + return { false, mode }; } } - - return {true, mode, promise}; + return { true, mode }; } static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg) @@ -120,7 +107,7 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg } auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); - if (isFd) { + if (isFd && fd > 0) { return {true, FileInfo{false, {}, {fd, false}}}; } @@ -142,7 +129,7 @@ napi_value CopyFile::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [succMode, mode, unused] = ParseJsModeAndProm(env, funcArg); + auto [succMode, mode] = ParseJsModeAndProm(env, funcArg); if (!succMode) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; @@ -184,7 +171,7 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto [succMode, mode, promise] = ParseJsModeAndProm(env, funcArg); + auto [succMode, mode] = ParseJsModeAndProm(env, funcArg); if (!succMode) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; @@ -204,13 +191,14 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return {NVal::CreateUndefined(env)}; }; - const string procedureName = "FileIOCopyFile"; + const string PROCEDURENAME = "FileIOCopyFile"; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::TWO || promise) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && + !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH)]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp b/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp index 461390164..0951fe5ca 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/create_randomaccessfile.cpp @@ -87,15 +87,15 @@ static tuple ParseJsFileAndFP(napi_env env, napi_value p static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg, FileInfo &fileInfo) { - int flags = O_RDONLY; - bool succ = false; + unsigned int flags = O_RDONLY; 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) { + auto [succ, mode] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!succ || mode < 0) { UniError(EINVAL).ThrowErr(env, "Invalid flags"); return { false, flags }; } + flags = static_cast(mode); (void)CommonFunc::ConvertJsFlags(flags); } } diff --git a/interfaces/kits/js/src/mod_fileio/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fileio/properties/fdopen_stream.cpp index f10adee6a..3cb25ab5c 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/fdopen_stream.cpp @@ -52,7 +52,7 @@ static NVal InstantiateStream(napi_env env, unique_ptr static tuple GetFdopenStreamArgs(napi_env env, const NFuncArg &funcArg) { auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Arg fd is required to be type integer"); return { false, -1, "" }; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/fstat.cpp b/interfaces/kits/js/src/mod_fileio/properties/fstat.cpp index 82525267f..a6dfddc44 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/fstat.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/fstat.cpp @@ -41,7 +41,7 @@ napi_value Fstat::Sync(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } @@ -81,7 +81,7 @@ napi_value Fstat::Async(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/fsync.cpp b/interfaces/kits/js/src/mod_fileio/properties/fsync.cpp index 060d94f18..1e0d27f10 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/fsync.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/fsync.cpp @@ -37,7 +37,7 @@ napi_value Fsync::Sync(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } @@ -59,7 +59,7 @@ napi_value Fsync::Async(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/ftruncate.cpp b/interfaces/kits/js/src/mod_fileio/properties/ftruncate.cpp index 6b936eab7..5052a1b23 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/ftruncate.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/ftruncate.cpp @@ -37,25 +37,21 @@ napi_value Ftruncate::Sync(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } - int ret = -1; - if (funcArg.GetArgc() == NARG_CNT::ONE) { - ret = ftruncate(fd, 0); - } else { - int len; + int64_t len = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { bool succ = false; - tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + tie(succ, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(len); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid len"); return nullptr; } - ret = ftruncate(fd, len); } - + int ret = ftruncate(fd, len); if (ret == -1) { UniError(errno).ThrowErr(env); return nullptr; @@ -73,20 +69,19 @@ napi_value Ftruncate::Async(napi_env env, napi_callback_info info) } auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!resGetFirstArg) { + if (!resGetFirstArg || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } - int len = 0; - size_t argc = funcArg.GetArgc(); - if (argc > NARG_CNT::ONE) { - auto [resGetSecondArg, length] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + int64_t len = 0; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + bool resGetSecondArg = false; + tie(resGetSecondArg, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(len); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid len"); return nullptr; } - len = length; } auto cbExec = [fd = fd, len = len](napi_env env) -> UniError { @@ -106,13 +101,14 @@ napi_value Ftruncate::Async(napi_env env, napi_callback_info info) } }; - const string procedureName = "fileIOFtruncate"; + const string PROCEDURENAME = "fileIOFtruncate"; NVal thisVar(env, funcArg.GetThisVar()); - if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbComplete).val_; } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbComplete).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/open.cpp b/interfaces/kits/js/src/mod_fileio/properties/open.cpp index cc84fd971..ad489c180 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open.cpp @@ -23,42 +23,13 @@ #include "../../common/napi/n_async/n_async_work_callback.h" #include "../../common/napi/n_async/n_async_work_promise.h" #include "../../common/napi/n_func_arg.h" +#include "../common_func.h" namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { using namespace std; -int AdaptToAbi(int &flags) -{ - static constexpr int USR_O_RDONLY = 00; - static constexpr int USR_O_WRONLY = 01; - static constexpr int USR_O_RDWR = 02; - static constexpr int USR_O_CREAT = 0100; - static constexpr int USR_O_EXCL = 0200; - static constexpr int USR_O_TRUNC = 01000; - static constexpr int USR_O_APPEND = 02000; - static constexpr int USR_O_NONBLOCK = 04000; - static constexpr int USR_O_DIRECTORY = 0200000; - static constexpr int USR_O_NOFOLLOW = 0400000; - static constexpr int USR_O_SYNC = 04010000; - - int flagsABI = 0; - flagsABI |= ((flags & USR_O_RDONLY) == USR_O_RDONLY) ? O_RDONLY : 0; - flagsABI |= ((flags & USR_O_WRONLY) == USR_O_WRONLY) ? O_WRONLY : 0; - flagsABI |= ((flags & USR_O_RDWR) == USR_O_RDWR) ? O_RDWR : 0; - flagsABI |= ((flags & USR_O_CREAT) == USR_O_CREAT) ? O_CREAT : 0; - flagsABI |= ((flags & USR_O_EXCL) == USR_O_EXCL) ? O_EXCL : 0; - flagsABI |= ((flags & USR_O_TRUNC) == USR_O_TRUNC) ? O_TRUNC : 0; - flagsABI |= ((flags & USR_O_APPEND) == USR_O_APPEND) ? O_APPEND : 0; - flagsABI |= ((flags & USR_O_NONBLOCK) == USR_O_NONBLOCK) ? O_NONBLOCK : 0; - flagsABI |= ((flags & USR_O_DIRECTORY) == USR_O_DIRECTORY) ? O_DIRECTORY : 0; - flagsABI |= ((flags & USR_O_NOFOLLOW) == USR_O_NOFOLLOW) ? O_NOFOLLOW : 0; - flagsABI |= ((flags & USR_O_SYNC) == USR_O_SYNC) ? O_SYNC : 0; - flags = flagsABI; - return flagsABI; -} - napi_value Open::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -68,45 +39,44 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) } bool succ = false; - unique_ptr path; + unique_ptr path = nullptr; tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - int flags = O_RDONLY; + unsigned int flags = O_RDONLY; if (funcArg.GetArgc() >= NARG_CNT::TWO) { - tie(succ, flags) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { + auto [succ, authFlags] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(O_RDONLY); + if (!succ || authFlags < 0) { UniError(EINVAL).ThrowErr(env, "Invalid flags"); return nullptr; } + flags = static_cast(authFlags); + (void)CommonFunc::ConvertJsFlags(flags); } - (void)AdaptToAbi(flags); + int fd = -1; if (ModuleRemoteUri::RemoteUri::IsRemoteUri(path.get(), fd, flags)) { return NVal::CreateInt64(env, fd).val_; } - size_t argc = funcArg.GetArgc(); int32_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; - if (argc != NARG_CNT::THREE) { + if (funcArg.GetArgc() != NARG_CNT::THREE) { size_t flagsFirst { flags }; if ((flagsFirst & O_CREAT) || (flagsFirst & O_TMPFILE)) { UniError(EINVAL).ThrowErr(env, "called with O_CREAT/O_TMPFILE but no mode"); return nullptr; } - fd = open(path.get(), flags, mode); } else { - tie(succ, mode) = NVal(env, funcArg.GetArg(NARG_POS::THIRD)).ToInt32(); + tie(succ, mode) = NVal(env, funcArg.GetArg(NARG_POS::THIRD)).ToInt32(mode); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; } - fd = open(path.get(), flags, mode); } - + fd = open(path.get(), flags, mode); if (fd == -1) { if (errno == ENAMETOOLONG) { UniError(errno).ThrowErr(env, "Filename too long"); @@ -119,7 +89,8 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) return NVal::CreateInt64(env, fd).val_; } -static UniError DoOpenExec(const std::string& path, const int flags, const int32_t mode, shared_ptr arg) +static UniError DoOpenExec(const std::string& path, const unsigned int flags, const int32_t mode, + shared_ptr arg) { int fd = -1; if (!ModuleRemoteUri::RemoteUri::IsRemoteUri(path, fd, flags)) { @@ -147,21 +118,21 @@ napi_value Open::Async(napi_env env, napi_callback_info info) return nullptr; } - int flags = O_RDONLY; - if (funcArg.GetArgc() >= NARG_CNT::TWO && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { - tie(succ, flags) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { + size_t argc = funcArg.GetArgc(); + unsigned int flags = O_RDONLY; + if (argc >= NARG_CNT::TWO) { + auto [succ, authFlags] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(O_RDONLY); + if (!succ || authFlags < 0) { UniError(EINVAL).ThrowErr(env, "Invalid flags"); return nullptr; } - (void)AdaptToAbi(flags); + flags = static_cast(authFlags); + (void)CommonFunc::ConvertJsFlags(flags); } int32_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; - size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::FOUR || - (argc == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) { - tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (argc >= NARG_CNT::THREE) { + tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; @@ -184,8 +155,9 @@ napi_value Open::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) || - (argc == NARG_CNT::THREE && (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)))) { + if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) || (argc == NARG_CNT::THREE && + !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOOpen", cbExec, cbComplCallback).val_; } else { NVal cb(env, funcArg[argc - 1]); 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..5f2dd809d 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 @@ -59,8 +59,9 @@ namespace DistributedFS { namespace ModuleFileIO { using namespace std; namespace { - static constexpr int MODE_RUO_RWX = 0750; + static constexpr int MODE_RUO_RWX = 0770; } + napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -69,28 +70,21 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) return nullptr; } - bool succ = false; - unique_ptr path; - tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - size_t argc = funcArg.GetArgc(); - int ret = -1; - if (argc == NARG_CNT::ONE) { - ret = access(path.get(), 0); - } else { - int mode; - tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { + int32_t mode = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(mode); + if (!succ || mode < 0) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; } - ret = access(path.get(), mode); } - + int ret = access(path.get(), mode); if (ret == -1) { UniError(errno).ThrowErr(env); return nullptr; @@ -99,51 +93,6 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env).val_; } -static tuple GetAccessArgs(napi_env env, const NFuncArg &funcArg) -{ - bool succ = false; - unique_ptr path; - tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid path"); - return { false, nullptr, 0, false }; - } - - size_t argc = funcArg.GetArgc(); - bool promise = true; - bool hasMode = false; - if (argc == NARG_CNT::ONE) { - hasMode = false; - promise = true; - } else if (argc == NARG_CNT::TWO) { - if (NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { - hasMode = false; - promise = false; - } else { - hasMode = true; - promise = true; - } - } else { - hasMode = true; - promise = false; - } - - int mode = 0; - if (hasMode) { - tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { - UniError(EINVAL).ThrowErr(env, "Invalid mode"); - return { false, nullptr, 0, false }; - } - } - - return { true, path.get(), mode, promise }; -} - -struct AsyncAccessArg { - unique_ptr fp = nullptr; -}; - napi_value PropNExporter::Access(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -152,17 +101,22 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - string path; - bool succ = false; - bool promise = false; - int mode; - tie(succ, path, mode, promise) = GetAccessArgs(env, funcArg); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - auto cbExec = [path = move(path), mode](napi_env env) -> UniError { + int32_t mode = 0; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(mode); + if (!succ || mode < 0) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + } + + auto cbExec = [path = string(path.get()), mode](napi_env env) -> UniError { int ret = access(path.c_str(), mode); if (ret == -1) { return UniError(errno); @@ -181,7 +135,8 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) const string procedureName = "FileIOAccess"; NVal thisVar(env, funcArg.GetThisVar()); - if (promise) { + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; } else { int cbInd = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); @@ -198,17 +153,13 @@ napi_value PropNExporter::Unlink(napi_env env, napi_callback_info info) return nullptr; } - string path; - unique_ptr tmp; - bool succ = false; - tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { - UniError(EINVAL).ThrowErr(env, "invalid path"); + UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - path = tmp.get(); - auto cbExec = [path](napi_env env) -> UniError { + auto cbExec = [path = string(path.get())](napi_env env) -> UniError { if (unlink(path.c_str()) == -1) { return UniError(errno); } else { @@ -241,26 +192,22 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) return nullptr; } - string path; - unique_ptr tmp; - bool succ = false; - tie(succ, tmp, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - path = tmp.get(); - int mode = 0775; - size_t argc = funcArg.GetArgc(); - if ((argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) || - argc == NARG_CNT::THREE) { - tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { + + int32_t mode = MODE_RUO_RWX; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + bool succ = false; + tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(mode); + if (!succ || mode < 0) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; } } - auto cbExec = [path, mode](napi_env env) -> UniError { + auto cbExec = [path = string(path.get()), mode](napi_env env) -> UniError { if (mkdir(path.c_str(), mode) == -1) { return UniError(errno); } @@ -276,10 +223,11 @@ napi_value PropNExporter::Mkdir(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); const string procedureName = "fileioMkdir"; - if ((argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) || argc == NARG_CNT::ONE) { + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; } else { - int cbIdx = ((argc == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; } @@ -293,30 +241,23 @@ napi_value PropNExporter::MkdirSync(napi_env env, napi_callback_info info) return nullptr; } - bool succ = false; - unique_ptr path; - tie(succ, path, ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + auto [succ, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } - int ret = -1; - size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::ONE) { - ret = mkdir(path.get(), MODE_RUO_RWX); - } else { - int mode; - tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - if (!succ) { + int32_t mode = MODE_RUO_RWX; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + bool succ = false; + tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(mode); + if (!succ || mode < 0) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); return nullptr; } - ret = mkdir(path.get(), mode); } - + int ret = mkdir(path.get(), mode); if (ret == -1) { - HILOGE("errno = %{public}d", errno); UniError(errno).ThrowErr(env); return nullptr; } @@ -333,14 +274,14 @@ napi_value PropNExporter::FchmodSync(napi_env env, napi_callback_info info) } bool succ = false; - int fd; + int fd = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } - int mode; + int mode = 0; tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid mode"); @@ -366,9 +307,9 @@ napi_value PropNExporter::FchownSync(napi_env env, napi_callback_info info) bool succ = false; - int fd; + int fd = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } @@ -405,26 +346,24 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) } bool succ = false; - - int fd; + int fd = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } void *buf = nullptr; - int64_t len; - bool hasPos = false; - int64_t pos; - tie(succ, buf, len, hasPos, pos, ignore) = + size_t len = 0; + int64_t pos = -1; + tie(succ, buf, len, pos, ignore) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { return nullptr; } - ssize_t actLen; - if (hasPos) { + ssize_t actLen = 0; + if (pos >= 0) { actLen = pread(fd, buf, len, pos); } else { actLen = read(fd, buf, len); @@ -440,7 +379,7 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) struct AsyncIOReadArg { ssize_t lenRead { 0 }; - int offset { 0 }; + int64_t offset { 0 }; NRef refReadBuf; explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} @@ -457,28 +396,26 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) bool succ = false; void *buf = nullptr; - int64_t len; - int fd; - bool hasPos = false; - int64_t pos; - int offset; + size_t len = 0; + int fd = -1; + int64_t pos = -1; + int64_t offset = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } - tie(succ, buf, len, hasPos, pos, offset) = + tie(succ, buf, len, pos, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Failed GetReadArg"); return nullptr; } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - auto cbExec = [arg, buf, len, fd, hasPos, pos, offset](napi_env env) -> UniError { - ssize_t actLen; - if (hasPos) { + auto cbExec = [arg, buf, len, fd, pos, offset](napi_env env) -> UniError { + ssize_t actLen = 0; + if (pos >= 0) { actLen = pread(fd, buf, len, pos); } else { actLen = read(fd, buf, len); @@ -506,32 +443,23 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - size_t argc = funcArg.GetArgc(); - bool hasOp = false; - if (argc == NARG_CNT::THREE) { - NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length")|| !op.TypeIs(napi_function)) { - hasOp = true; - } - } - - if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && + !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIORead", cbExec, cbCompl).val_; } else { - int cbIdx = ((argc == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIORead", cbExec, cbCompl).val_; } } -UniError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t position) +static UniError WriteExec(shared_ptr arg, void *buf, size_t len, int fd, int64_t position) { - if (position == (size_t)INVALID_POSITION) { - arg->actLen = write(fd, buf, len); - } else { + if (position >= 0) { arg->actLen = pwrite(fd, buf, len, position); + } else { + arg->actLen = write(fd, buf, len); } - if (arg->actLen == -1) { return UniError(errno); } @@ -548,31 +476,24 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) } bool succ = false; - int fd; + int fd = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } - unique_ptr bufGuard; + unique_ptr bufGuard = nullptr; void *buf = nullptr; - size_t len; - size_t position; - bool hasPos = false; - tie(succ, bufGuard, buf, len, hasPos, position) = + size_t len = 0; + int64_t position = -1; + tie(succ, bufGuard, buf, len, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env, "Failed GetWriteArg"); return nullptr; } - shared_ptr arg; - if (bufGuard) { - arg = make_shared(move(bufGuard)); - } else { - arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - } + auto arg = make_shared(move(bufGuard)); auto cbExec = [arg, buf, len, fd, position](napi_env env) -> UniError { return WriteExec(arg, buf, len, fd, position); }; @@ -585,20 +506,11 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - bool hasOp = false; - size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::THREE) { - NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("position") || op.HasProp("length") || - op.HasProp("encoding") || !op.TypeIs(napi_function)) { - hasOp = true; - } - } - - if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && hasOp)) { + if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == NARG_CNT::THREE && + !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOWrite", cbExec, cbCompl).val_; } else { - int cbIdx = ((argc == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); + int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); NVal cb(env, funcArg[cbIdx]); return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOWrite", cbExec, cbCompl).val_; } @@ -637,29 +549,28 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) } bool succ = false; - int fd; + int fd = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { UniError(EINVAL).ThrowErr(env, "Invalid fd"); return nullptr; } void *buf = nullptr; - size_t len; - size_t position; - unique_ptr bufGuard; - bool hasPos = false; - tie(succ, bufGuard, buf, len, hasPos, position) = + size_t len = 0; + int64_t position = -1; + unique_ptr bufGuard = nullptr; + tie(succ, bufGuard, buf, len, position) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { return nullptr; } - ssize_t writeLen; - if (position == (size_t)INVALID_POSITION) { - writeLen = write(fd, buf, len); - } else { + ssize_t writeLen = 0; + if (position >= 0) { writeLen = pwrite(fd, buf, len, position); + } else { + writeLen = write(fd, buf, len); } if (writeLen == -1) { diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.h index d42aa4b57..6fa7be33d 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.h +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.h @@ -26,7 +26,7 @@ namespace DistributedFS { namespace ModuleFileIO { struct AsyncIOWrtieArg { NRef refWriteArrayBuf_; - std::unique_ptr guardWriteStr_; + std::unique_ptr guardWriteStr_ = nullptr; ssize_t actLen = 0; explicit AsyncIOWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf_(refWriteArrayBuf) {} @@ -53,7 +53,6 @@ public: static napi_value Mkdir(napi_env env, napi_callback_info info); static napi_value Read(napi_env env, napi_callback_info info); static napi_value Write(napi_env env, napi_callback_info info); - static UniError WriteExec(std::shared_ptr arg, void *buf, size_t len, int fd, size_t position); bool Export() override; std::string GetClassName() override; diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp index afd1135f5..bc07193c2 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -30,46 +30,40 @@ namespace DistributedFS { namespace ModuleFileIO { using namespace std; -static tuple, bool> GetReadTextArg(napi_env env, napi_value argOption) +static tuple> GetReadTextArg(napi_env env, napi_value argOption) { NVal op(env, argOption); - ssize_t position = 0; - ssize_t len = 0; + int64_t position = -1; + size_t len = 0; bool succ = false; - bool hasOp = false; bool hasLen = false; - unique_ptr encoding; + unique_ptr encoding = nullptr; - if (op.HasProp("position")) { - tie(succ, position) = op.GetProp("position").ToInt32(); - if (!succ) { - return { false, position, hasLen, len, nullptr, hasOp }; + if (op.HasProp("position") && !op.GetProp("position").TypeIs(napi_undefined)) { + tie(succ, position) = op.GetProp("position").ToInt64(); + if (!succ || position < 0) { + return { false, position, hasLen, len, nullptr }; } - hasOp = true; } - if (op.HasProp("length")) { - tie(succ, len) = op.GetProp("length").ToInt32(); - if (!succ) { - return { false, position, hasLen, len, nullptr, hasOp }; + if (op.HasProp("length") && !op.GetProp("length").TypeIs(napi_undefined)) { + auto [succ, length] = op.GetProp("length").ToInt64(); + if (!succ || length < 0 || length > UINT_MAX) { + return { false, position, hasLen, len, nullptr }; } - hasOp = true; + len = static_cast(length); hasLen = true; } if (op.HasProp("encoding")) { - auto [succ, encoding, unuse] = op.GetProp("encoding").ToUTF8String(); - if (!succ) { - return { false, position, hasLen, len, nullptr, hasOp }; + auto [succ, encoding, unuse] = op.GetProp("encoding").ToUTF8String("utf-8"); + string_view encodingStr(encoding.get()); + if (!succ || encodingStr != "utf-8") { + return { false, position, hasLen, len, nullptr }; } - hasOp = true; } - if (!op.TypeIs(napi_function)) { - hasOp = true; - } - - return { true, position, hasLen, len, move(encoding), hasOp }; + return { true, position, hasLen, len, move(encoding) }; } napi_value ReadText::Sync(napi_env env, napi_callback_info info) @@ -86,14 +80,13 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [resGetReadTextArg, position, hasLen, len, encoding, useless] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + auto [resGetReadTextArg, position, hasLen, len, encoding] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); if (!resGetReadTextArg) { UniError(EINVAL).ThrowErr(env, "Invalid option"); return nullptr; } struct stat statbf; - int ret; FDGuard sfd; sfd.SetFD(open(path.get(), O_RDONLY)); if ((!sfd) || (fstat(sfd.GetFD(), &statbf) == -1)) { @@ -106,8 +99,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return nullptr; } - len = !hasLen ? statbf.st_size : len; - len = ((len < statbf.st_size) ? len : statbf.st_size); + len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; std::unique_ptr readbuf = std::make_unique(len + 1); if (readbuf == nullptr) { UniError(EINVAL).ThrowErr(env, "file is too large"); @@ -118,8 +110,12 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) UniError(errno).ThrowErr(env, "dfs mem error"); return nullptr; } - - ret = position > 0 ? pread(sfd.GetFD(), readbuf.get(), len, position) : read(sfd.GetFD(), readbuf.get(), len); + ssize_t ret = 0; + if (position >= 0) { + ret = pread(sfd.GetFD(), readbuf.get(), len, position); + } else { + ret = read(sfd.GetFD(), readbuf.get(), len); + } if (ret == -1) { UniError(EINVAL).ThrowErr(env, "Invalid read file"); return nullptr; @@ -128,8 +124,8 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, readbuf.get(), ret).val_; } -UniError ReadText::AsyncExec(const std::string &path, std::shared_ptr arg, ssize_t position, - bool hasLen, ssize_t len) +static UniError AsyncExec(const std::string &path, std::shared_ptr arg, int64_t position, + bool hasLen, size_t len) { if (arg == nullptr) { return UniError(ENOMEM); @@ -137,7 +133,6 @@ UniError ReadText::AsyncExec(const std::string &path, std::shared_ptrlen = len; sfd.SetFD(open(path.c_str(), O_RDONLY)); if (sfd.GetFD() == -1) { return UniError(EINVAL); @@ -151,20 +146,16 @@ UniError ReadText::AsyncExec(const std::string &path, std::shared_ptrlen = statbf.st_size; - } - - arg->len = ((arg->len < statbf.st_size) ? arg->len : statbf.st_size); - arg->buf = std::make_unique(arg->len); + len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; + arg->buf = std::make_unique(len); if (arg->buf == nullptr) { return UniError(ENOMEM); } - if (position > 0) { - arg->len = pread(sfd.GetFD(), arg->buf.get(), arg->len, position); + if (position >= 0) { + arg->len = pread(sfd.GetFD(), arg->buf.get(), len, position); } else { - arg->len = read(sfd.GetFD(), arg->buf.get(), arg->len); + arg->len = read(sfd.GetFD(), arg->buf.get(), len); } if (arg->len == -1) { @@ -188,7 +179,7 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) return nullptr; } - auto [resGetSecondArg, position, hasLen, len, encoding, hasOp] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + auto [resGetSecondArg, position, hasLen, len, encoding] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid option"); return nullptr; @@ -212,13 +203,12 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } }; - size_t argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); - if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && hasOp)) { + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule("FileIOReadText", cbExec, cbComplete).val_; } else { - int cbIdx = !hasOp ? NARG_POS::SECOND : NARG_POS::THIRD; - NVal cb(env, funcArg[cbIdx]); + NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); return NAsyncWorkCallback(env, thisVar, cb).Schedule("FileIOReadText", cbExec, cbComplete).val_; } } diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.h b/interfaces/kits/js/src/mod_fileio/properties/read_text.h index 882143773..b56fc7efc 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.h +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.h @@ -25,16 +25,14 @@ namespace DistributedFS { namespace ModuleFileIO { struct AsyncReadTextArg { NRef _refReadBuf; - std::unique_ptr buf; - ssize_t len = 0; + std::unique_ptr buf = nullptr; + int64_t len = 0; explicit AsyncReadTextArg(NVal refReadBuf) : _refReadBuf(refReadBuf) {}; ~AsyncReadTextArg() = default; }; class ReadText final { public: -static UniError AsyncExec(const std::string &path, std::shared_ptr arg, ssize_t position, - bool hasLen, ssize_t len); static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; diff --git a/interfaces/kits/js/src/mod_fileio/properties/truncate.cpp b/interfaces/kits/js/src/mod_fileio/properties/truncate.cpp index b86f59279..c2f06f611 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/truncate.cpp @@ -38,17 +38,15 @@ napi_value Truncate::Sync(napi_env env, napi_callback_info info) return nullptr; } - int ret = -1; - if (funcArg.GetArgc() == NARG_CNT::ONE) { - ret = truncate(path.get(), 0); - } else { - auto [resGetSecondArg, len] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + int64_t len = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + bool resGetSecondArg = false; + tie(resGetSecondArg, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(len); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid len"); } - ret = truncate(path.get(), len); } - + int ret = truncate(path.get(), len); if (ret == -1) { UniError(errno).ThrowErr(env); return nullptr; @@ -71,15 +69,14 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) return nullptr; } - int len = 0; - size_t argc = funcArg.GetArgc(); - if (argc > NARG_CNT::ONE) { - auto [resGetSecondArg, length] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + int64_t len = 0; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + bool resGetSecondArg = false; + tie(resGetSecondArg, len) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(len); if (!resGetSecondArg) { UniError(EINVAL).ThrowErr(env, "Invalid len"); return nullptr; } - len = length; } auto cbExec = [path = string(path.get()), len](napi_env env) -> UniError { @@ -100,12 +97,13 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - const string procedureName = "FileIOTruncate"; - if (argc == NARG_CNT::ONE || (argc == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + const string PROCEDURENAME = "FileIOTruncate"; + if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + NVal cb(env, funcArg[funcArg.GetArgc() - 1]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURENAME, cbExec, cbCompl).val_; } } } // namespace ModuleFileIO -- Gitee