From fc53ac392b1e84f4f9ad2e6cbf5a6d4538aff861 Mon Sep 17 00:00:00 2001 From: fengjq Date: Mon, 24 Apr 2023 17:13:04 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=9B=9E=E5=90=88release3.2=E3=80=91?= =?UTF-8?q?=E6=94=AF=E6=8C=81option=E4=BC=A0=E5=85=A5undefined=EF=BC=8C?= =?UTF-8?q?=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 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 19 ++--- .../mod_fs/class_stream/stream_n_exporter.cpp | 79 +++++++++---------- interfaces/kits/js/src/mod_fs/common_func.cpp | 77 +++++++----------- interfaces/kits/js/src/mod_fs/common_func.h | 11 +-- .../js/src/mod_fs/properties/copy_file.cpp | 39 ++++----- .../js/src/mod_fs/properties/listfile.cpp | 60 +++++++------- .../kits/js/src/mod_fs/properties/move.cpp | 7 +- .../kits/js/src/mod_fs/properties/open.cpp | 18 ++--- .../src/mod_fs/properties/prop_n_exporter.cpp | 53 +++---------- .../js/src/mod_fs/properties/read_text.cpp | 59 ++++++-------- .../kits/js/src/mod_fs/properties/read_text.h | 2 +- .../js/src/mod_fs/properties/truncate.cpp | 29 +++---- utils/filemgmt_libn/include/n_val.h | 4 + utils/filemgmt_libn/src/n_val.cpp | 39 +++++++++ 14 files changed, 229 insertions(+), 267 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 69018444d..4ab935a24 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -66,21 +66,16 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) static bool GetExclusive(napi_env env, NFuncArg &funcArg, bool &exclusive) { - bool succ = true; - if (funcArg.GetArgc() >= NARG_CNT::ONE && !(NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_function))) { - if (NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_boolean)) { - tie(succ, exclusive) = NVal(env, funcArg[NARG_POS::FIRST]).ToBool(); - if (!succ) { - HILOGE("Invalid exclusive"); - NError(EINVAL).ThrowErr(env); - } - } else { + if (funcArg.GetArgc() >= NARG_CNT::ONE) { + bool succ = false; + tie(succ, exclusive) = NVal(env, funcArg[NARG_POS::FIRST]).ToBool(exclusive); + if (!succ) { HILOGE("Invalid exclusive"); NError(EINVAL).ThrowErr(env); - succ = false; + return false; } } - return succ; + return true; } napi_value FileNExporter::Lock(napi_env env, napi_callback_info info) @@ -124,7 +119,7 @@ napi_value FileNExporter::Lock(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ZERO || (funcArg.GetArgc() == NARG_CNT::ONE && - NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_boolean))) { + !NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCK_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::FIRST); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 703cbad8c..5bfd00d7a 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -53,18 +53,20 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) FILE *filp = nullptr; filp = streamEntity->fp.get(); - auto [succ, buf, len, hasOffset, offset] = + auto [succ, buf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); return nullptr; } - int ret = fseek(filp, offset, SEEK_SET); - if (hasOffset && (ret < 0)) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - NError(errno).ThrowErr(env); - return nullptr; + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } } size_t actLen = fread(buf, 1, len, filp); @@ -116,17 +118,19 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) FILE *filp = nullptr; filp = streamEntity->fp.get(); - auto [succ, bufGuard, buf, len, hasOffset, offset] = + auto [succ, bufGuard, buf, len, offset] = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); return nullptr; } - int ret = fseek(filp, static_cast(offset), SEEK_SET); - if (hasOffset && (ret < 0)) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - NError(errno).ThrowErr(env); - return nullptr; + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } } size_t writeLen = fwrite(buf, 1, len, filp); @@ -139,15 +143,6 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) return NVal::CreateInt64(env, static_cast(writeLen)).val_; } -static bool HasOption(napi_env env, napi_value optionFromJsArg) -{ - NVal op = NVal(env, optionFromJsArg); - if (op.HasProp("offset") || op.HasProp("length") || op.HasProp("encoding") || !op.TypeIs(napi_function)) { - return true; - } - return false; -} - napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -167,7 +162,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) FILE *filp = nullptr; filp = streamEntity->fp.get(); - auto [succ, bufGuard, buf, len, hasOffset, offset] = + auto [succ, bufGuard, buf, len, offset] = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); @@ -180,11 +175,13 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf = buf, len = len, filp, hasOffset = hasOffset, offset = offset]() -> NError { - int ret = fseek(filp, static_cast(offset), SEEK_SET); - if (hasOffset && (ret < 0)) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - return NError(errno); + auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + return NError(errno); + } } arg->actLen = fwrite(buf, 1, len, filp); if ((arg->actLen == 0) && (arg->actLen != len)) { @@ -202,11 +199,8 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) }; NVal thisVar(env, funcArg.GetThisVar()); - bool hasOp = false; - if (funcArg.GetArgc() == NARG_CNT::TWO) { - hasOp = HasOption(env, funcArg[NARG_POS::SECOND]); - } - if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == 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(PROCEDURE_STREAM_WRITE_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); @@ -233,7 +227,7 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) FILE *filp = nullptr; filp = streamEntity->fp.get(); - auto [succ, buf, len, hasOffset, offset] = + auto [succ, buf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { HILOGE("Failed to resolve buf and options"); @@ -247,11 +241,13 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf = buf, len = len, filp, hasOffset = hasOffset, offset = offset]() -> NError { - int ret = fseek(filp, offset, SEEK_SET); - if (hasOffset && (ret < 0)) { - HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); - return NError(errno); + auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { + if (offset >= 0) { + int ret = fseek(filp, static_cast(offset), SEEK_SET); + if (ret < 0) { + HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); + return NError(errno); + } } size_t actLen = fread(buf, 1, len, filp); if ((actLen != static_cast(len) && !feof(filp)) || ferror(filp)) { @@ -271,11 +267,8 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) }; NVal thisVar(env, funcArg.GetThisVar()); - bool hasOp = false; - if (funcArg.GetArgc() == NARG_CNT::TWO) { - hasOp = HasOption(env, funcArg[NARG_POS::SECOND]); - } - if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == 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(PROCEDURE_STREAM_READ_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD); diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 98ae9e395..3be49d2a4 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -70,29 +70,18 @@ void InitOpenMode(napi_env env, napi_value exports) static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufOff, NVal op) { bool succ = false; - size_t retLen = 0; + size_t retLen = bufLen - bufOff; if (op.HasProp("length")) { int64_t opLength = 0; - tie(succ, opLength) = op.GetProp("length").ToInt64(); - if (!succ) { - HILOGE("Invalid option.length, expect integer"); + tie(succ, opLength) = op.GetProp("length").ToInt64(static_cast(retLen)); + if (!succ || opLength < 0 || static_cast(opLength) > retLen) { + HILOGE("Invalid option.length"); NError(EINVAL).ThrowErr(env); return { false, 0 }; } - if (opLength < 0) { - retLen = bufLen - bufOff; - } else if (static_cast(opLength) > bufLen - bufOff) { - HILOGE("Invalid option.length, buffer limit exceeded"); - NError(EINVAL).ThrowErr(env); - return { false, 0 }; - } else { - retLen = static_cast(opLength); - } - } else { - retLen = bufLen - bufOff; + retLen = static_cast(opLength); } - return { true, retLen }; } @@ -217,17 +206,18 @@ static tuple, size_t> DecodeString(napi_env env, NVal j if (!jsStr.TypeIs(napi_string)) { return { false, nullptr, 0 }; } - - bool succ = false; if (!encoding) { return jsStr.ToUTF8String(); } + bool succ = false; unique_ptr encodingBuf = nullptr; - tie(succ, encodingBuf, ignore) = encoding.ToUTF8String(); + tie(succ, encodingBuf, ignore) = encoding.ToUTF8String("utf-8"); if (!succ) { + HILOGE("Failed to convert encoding to UTF8"); return { false, nullptr, 0 }; } + string_view encodingStr(encodingBuf.release()); if (encodingStr == "utf-8") { return jsStr.ToUTF8String(); @@ -239,13 +229,12 @@ static tuple, size_t> DecodeString(napi_env env, NVal j } } -tuple CommonFunc::GetReadArg(napi_env env, +tuple CommonFunc::GetReadArg(napi_env env, napi_value readBuf, napi_value option) { size_t retLen = 0; - int64_t position = 0; + int64_t offset = -1; bool succ = false; - bool posAssigned = false; NVal txt(env, readBuf); void *buf = nullptr; @@ -254,35 +243,30 @@ tuple CommonFunc::GetReadArg(napi_env env, if (!succ || bufLen > UINT_MAX) { HILOGE("Invalid arraybuffer"); NError(EINVAL).ThrowErr(env); - return { false, nullptr, 0, posAssigned, position }; + return { false, nullptr, retLen, offset }; } NVal op = NVal(env, option); tie(succ, retLen) = GetActualLen(env, bufLen, 0, op); if (!succ) { HILOGE("Failed to get actual length"); - return { false, nullptr, 0, posAssigned, position }; + return { false, nullptr, retLen, offset }; } - - if (op.HasProp("offset")) { - tie(succ, position) = op.GetProp("offset").ToInt64(); - if (succ && position >= 0) { - posAssigned = true; - } else { + if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { + tie(succ, offset) = op.GetProp("offset").ToInt64(); + if (!succ || offset < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); - return { false, nullptr, 0, posAssigned, position }; + return { false, nullptr, retLen, offset }; } } - - return { true, buf, retLen, posAssigned, position }; + return { true, buf, retLen, offset }; } -tuple, void *, size_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, +tuple, void *, size_t, int64_t> CommonFunc::GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption) { - int64_t retPos = 0; size_t bufLen = 0; - bool hasPos = false; + int64_t offset = -1; bool succ = false; void *buf = nullptr; NVal op(env, argOption); @@ -294,7 +278,7 @@ tuple, void *, size_t, bool, int64_t> CommonFunc::GetWr if (!succ) { HILOGE("Illegal write buffer or encoding"); NError(EINVAL).ThrowErr(env); - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, offset }; } } else { buf = bufferGuard.get(); @@ -302,29 +286,24 @@ tuple, void *, size_t, bool, int64_t> CommonFunc::GetWr if (bufLen > UINT_MAX) { HILOGE("The Size of buffer is too large"); NError(EINVAL).ThrowErr(env); - return { false, nullptr, nullptr, 0, hasPos, retPos} ; + return { false, nullptr, nullptr, 0, offset }; } size_t retLen = 0; tie(succ, retLen) = GetActualLen(env, bufLen, 0, op); if (!succ) { HILOGE("Failed to get actual length"); - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, offset }; } - if (op.HasProp("offset")) { - int64_t position = 0; - tie(succ, position) = op.GetProp("offset").ToInt64(); - if (!succ || position < 0) { + if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { + tie(succ, offset) = op.GetProp("offset").ToInt64(); + if (!succ || offset < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); - return { false, nullptr, nullptr, 0, hasPos, retPos }; + return { false, nullptr, nullptr, 0, offset }; } - hasPos = true; - retPos = position; - } else { - retPos = INVALID_POSITION; } - return { true, move(bufferGuard), buf, retLen, hasPos, retPos }; + return { true, move(bufferGuard), buf, retLen, offset }; } } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 12188741f..58fe64825 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -24,7 +24,6 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { -constexpr int64_t INVALID_POSITION = std::numeric_limits::max(); constexpr int RDONLY = 00; constexpr int WRONLY = 01; constexpr int RDWR = 02; @@ -48,12 +47,10 @@ struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); static LibN::NVal InstantiateStat(napi_env env, struct stat &buf); static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); - static std::tuple GetReadArg(napi_env env, - napi_value readBuf, - napi_value option); - static std::tuple, void *, size_t, bool, int64_t> GetWriteArg(napi_env env, - napi_value argWBuf, - napi_value argOption); + static std::tuple GetReadArg(napi_env env, napi_value readBuf, napi_value option); + static std::tuple, void *, size_t, int64_t> GetWriteArg(napi_env env, + napi_value argWBuf, + napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath); diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index bfe815431..3b5959854 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -115,28 +115,17 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) return SendFileCore(srcFile, destFile, statbf); } -static tuple ParseJsModeAndProm(napi_env env, const NFuncArg& funcArg) +static tuple ParseJsMode(napi_env env, const NFuncArg& funcArg) { - bool promise = false; bool succ = false; int mode = 0; - if (funcArg.GetArgc() == NARG_CNT::TWO) { - return { true, mode, true }; - } - if (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) { - if (funcArg.GetArgc() == NARG_CNT::THREE) { - promise = true; - } - tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); - if (succ && !mode) { - return { true, mode, promise }; + if (funcArg.GetArgc() >= NARG_CNT::THREE) { + tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); + if (!succ || mode) { + return { false, mode }; } } - if (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { - return { true, mode, promise }; - } - - return { false, mode, promise }; + return { true, mode }; } static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg) @@ -149,6 +138,11 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { auto fdg = make_unique(fd, false); + if (!fdg) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } @@ -172,8 +166,8 @@ napi_value CopyFile::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [succMode, mode, unused] = ParseJsModeAndProm(env, funcArg); - if (!succMode || mode) { + auto [succMode, mode] = ParseJsMode(env, funcArg); + if (!succMode) { HILOGE("Failed to convert mode to int32"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -212,8 +206,8 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto [succMode, mode, promise] = ParseJsModeAndProm(env, funcArg); - if (!succMode || mode) { + auto [succMode, mode] = ParseJsMode(env, funcArg); + if (!succMode) { HILOGE("Failed to convert mode to int32"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -234,7 +228,8 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::TWO || promise) { + 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(PROCEDURE_COPYFILE_NAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH)]); diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 40c4ea904..ddc3fe013 100644 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -53,7 +53,7 @@ static bool CheckSuffix(const vector &suffixs) static bool GetFileFilterParam(const NVal &argv, OHOS::DistributedFS::FileFilter *filter) { bool ret = false; - if (argv.HasProp("suffix")) { + if (argv.HasProp("suffix") && !argv.GetProp("suffix").TypeIs(napi_undefined)) { vector suffixs; tie(ret, suffixs, ignore) = argv.GetProp("suffix").ToStringArray(); if (!ret) { @@ -66,7 +66,7 @@ static bool GetFileFilterParam(const NVal &argv, OHOS::DistributedFS::FileFilter } filter->SetSuffix(suffixs); } - if (argv.HasProp("displayName")) { + if (argv.HasProp("displayName") && !argv.GetProp("displayName").TypeIs(napi_undefined)) { vector displayNames; tie(ret, displayNames, ignore) = argv.GetProp("displayName").ToStringArray(); if (!ret) { @@ -79,7 +79,7 @@ static bool GetFileFilterParam(const NVal &argv, OHOS::DistributedFS::FileFilter } filter->SetDisplayName(displayNames); } - if (argv.HasProp("fileSizeOver")) { + if (argv.HasProp("fileSizeOver") && !argv.GetProp("fileSizeOver").TypeIs(napi_undefined)) { int64_t fileSizeOver; tie(ret, fileSizeOver) = argv.GetProp("fileSizeOver").ToInt64(); if (!ret || fileSizeOver < 0) { @@ -88,7 +88,7 @@ static bool GetFileFilterParam(const NVal &argv, OHOS::DistributedFS::FileFilter } filter->SetFileSizeOver(fileSizeOver); } - if (argv.HasProp("lastModifiedAfter")) { + if (argv.HasProp("lastModifiedAfter") && !argv.GetProp("lastModifiedAfter").TypeIs(napi_undefined)) { double lastModifiedAfter; tie(ret, lastModifiedAfter) = argv.GetProp("lastModifiedAfter").ToDouble(); if (!ret || lastModifiedAfter < 0) { @@ -104,18 +104,15 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) { bool succ = false; if (argv.HasProp("listNum")) { - tie(succ, optionArgs->listNum) = argv.GetProp("listNum").ToInt64(); - if (!succ) { - HILOGE("Failed to get listNum prop."); - return false; - } else if (optionArgs->listNum < 0) { - HILOGE("Invalid listNum."); + tie(succ, optionArgs->listNum) = argv.GetProp("listNum").ToInt64(0); + if (!succ || optionArgs->listNum < 0) { + HILOGE("Failed to get listNum prop"); return false; } } if (argv.HasProp("recursion")) { - tie(succ, optionArgs->recursion) = argv.GetProp("recursion").ToBool(); + tie(succ, optionArgs->recursion) = argv.GetProp("recursion").ToBool(false); if (!succ) { HILOGE("Failed to get recursion prop."); return false; @@ -123,11 +120,13 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) } if (argv.HasProp("filter")) { - NVal(filterProp) = argv.GetProp("filter"); - auto ret = GetFileFilterParam(NVal(filterProp), &optionArgs->filter); - if (!ret) { - HILOGE("Failed to get filter prop."); - return false; + NVal filterProp = argv.GetProp("filter"); + if (!filterProp.TypeIs(napi_undefined)) { + auto ret = GetFileFilterParam(filterProp, &optionArgs->filter); + if (!ret) { + HILOGE("Failed to get filter prop."); + return false; + } } } return true; @@ -135,18 +134,19 @@ static bool GetOptionParam(const NVal &argv, OptionArgs *optionArgs) static bool GetOptionArg(napi_env env, const NFuncArg &funcArg, OptionArgs &optionArgs, const string &path) { - auto options = NVal(env, funcArg[NARG_POS::SECOND]); - if (funcArg.GetArgc() >= NARG_CNT::TWO && options.TypeIs(napi_object)) { - if (!GetOptionParam(options, &optionArgs)) { - HILOGE("Invalid options"); - return false; + optionArgs.path = path; + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return true; + } + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + auto options = NVal(env, funcArg[NARG_POS::SECOND]); + if (options.TypeIs(napi_object)) { + return GetOptionParam(options, &optionArgs); + } else if (options.TypeIs(napi_undefined) || options.TypeIs(napi_function)) { + return true; } - } else if (funcArg.GetArgc() >= NARG_CNT::TWO && !options.TypeIs(napi_function)) { - HILOGE("Invalid options"); - return false; } - optionArgs.path = path; - return true; + return false; } static bool FilterSuffix(const vector& suffixs, const struct dirent& filename) @@ -314,6 +314,7 @@ napi_value ListFile::Sync(napi_env env, napi_callback_info info) return nullptr; } if (!GetOptionArg(env, funcArg, g_optionArgs, string(path.get()))) { + HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -345,6 +346,7 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) OptionArgs optionArgsTmp = {}; if (!GetOptionArg(env, funcArg, optionArgsTmp, string(path.get()))) { + HILOGE("Invalid options"); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -375,13 +377,11 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && - NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_object))) { + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(LIST_FILE_PRODUCE_NAME, cbExec, cbCompl).val_; } else { - int32_t cbIdx = (funcArg.GetArgc() == NARG_CNT::TWO) ? 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(LIST_FILE_PRODUCE_NAME, cbExec, cbCompl).val_; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 8185a673f..c7cce98e7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -71,9 +71,9 @@ static tuple, unique_ptr, int> ParseJsOperand(n return { false, nullptr, nullptr, 0 }; } int mode = 0; - if (funcArg.GetArgc() >= NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) { + if (funcArg.GetArgc() >= NARG_CNT::THREE) { bool resGetThirdArg = false; - tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!resGetThirdArg || (mode != MODE_FORCE_MOVE && mode != MODE_THROW_ERR)) { HILOGE("Invalid mode"); return { false, nullptr, nullptr, 0 }; @@ -192,7 +192,8 @@ napi_value Move::Async(napi_env env, napi_callback_info info) NVal thisVar(env, funcArg.GetThisVar()); size_t argc = funcArg.GetArgc(); - if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number))) { + if (argc == NARG_CNT::TWO || (argc == NARG_CNT::THREE && + !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MOVE_NAME, cbExec, cbComplCallback).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 5102cbdf7..6167e2e44 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -42,19 +42,19 @@ using namespace OHOS::AppExecFwk; static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) { - unsigned int mode = O_RDONLY; - bool succ = false; + unsigned int flags = O_RDONLY; if (funcArg.GetArgc() >= NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { - tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); - unsigned int invalidMode = (O_WRONLY | O_RDWR); - if (!succ || ((mode & invalidMode) == invalidMode)) { + auto [succ, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(O_RDONLY); + int32_t invalidMode = (O_WRONLY | O_RDWR); + if (!succ || mode < 0 || ((mode & invalidMode) == invalidMode)) { HILOGE("Invalid mode"); NError(EINVAL).ThrowErr(env); - return { false, mode }; + return { false, flags }; } - (void)CommonFunc::ConvertJsFlags(mode); + flags = static_cast(mode); + (void)CommonFunc::ConvertJsFlags(flags); } - return { true, mode }; + return { true, flags }; } static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) @@ -302,7 +302,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE || (funcArg.GetArgc() == NARG_CNT::TWO && - NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { + !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function))) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_OPEN_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::SECOND); 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 3a234482f..df371a7eb 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 @@ -321,18 +321,14 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) void *buf = nullptr; size_t len = 0; - bool hasOffset = false; - int64_t offset = 0; - tie(succ, buf, len, hasOffset, offset) = + int64_t offset = -1; + tie(succ, buf, len, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!hasOffset) { - offset = -1; - } uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr read_req = { @@ -383,8 +379,7 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) void *buf = nullptr; size_t len = 0; int32_t fd = 0; - bool hasOffset = false; - int64_t offset = 0; + int64_t offset = -1; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); @@ -392,16 +387,13 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - tie(succ, buf, len, hasOffset, offset) = + tie(succ, buf, len, offset) = CommonFunc::GetReadArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!hasOffset) { - offset = -1; - } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); if (!arg) { @@ -422,14 +414,8 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - bool hasOp = false; - if (funcArg.GetArgc() == NARG_CNT::THREE) { - NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("length") || !op.TypeIs(napi_function)) { - hasOp = true; - } - } - if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == 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(PROCEDURE_READ_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); @@ -477,18 +463,14 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) unique_ptr bufGuard = nullptr; void *buf = nullptr; size_t len = 0; - int64_t offset = 0; - bool hasOffset = false; - tie(succ, bufGuard, buf, len, hasOffset, offset) = + int64_t offset = -1; + tie(succ, bufGuard, buf, len, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { HILOGE("Failed to resolve buf and options"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!hasOffset) { - offset = -1; - } auto arg = make_shared(move(bufGuard)); if (!arg) { @@ -510,15 +492,8 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - bool hasOp = false; - if (funcArg.GetArgc() == NARG_CNT::THREE) { - NVal op = NVal(env, funcArg[NARG_POS::THIRD]); - if (op.HasProp("offset") || op.HasProp("length") || op.HasProp("encoding") || !op.TypeIs(napi_function)) { - hasOp = true; - } - } - - if (funcArg.GetArgc() == NARG_CNT::TWO || (funcArg.GetArgc() == 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(PROCEDURE_WRITE_NAME, cbExec, cbCompl).val_; } else { int cbIdx = ((funcArg.GetArgc() == NARG_CNT::THREE) ? NARG_POS::THIRD : NARG_POS::FOURTH); @@ -547,18 +522,14 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) void *buf = nullptr; size_t len = 0; - int64_t offset = 0; + int64_t offset = -1; unique_ptr bufGuard = nullptr; - bool hasOffset = false; - tie(succ, bufGuard, buf, len, hasOffset, offset) = + tie(succ, bufGuard, buf, len, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { HILOGE("Failed to resolve buf and options"); return nullptr; } - if (!hasOffset) { - offset = -1; - } uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr write_req = { diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index d22b99b0a..f61b09c9c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -14,6 +14,7 @@ */ #include "read_text.h" +#include #include #include #include @@ -29,53 +30,46 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -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 offset = 0; - ssize_t len = 0; + int64_t offset = -1; + int64_t len = 0; bool succ = false; - bool hasOp = false; bool hasLen = false; unique_ptr encoding; - if (op.HasProp("offset")) { - tie(succ, offset) = op.GetProp("offset").ToInt32(); + if (op.HasProp("offset") && !op.GetProp("offset").TypeIs(napi_undefined)) { + tie(succ, offset) = op.GetProp("offset").ToInt64(); if (!succ || offset < 0) { HILOGE("Illegal option.offset parameter"); - return { false, offset, hasLen, len, nullptr, hasOp }; + return { false, offset, hasLen, len, nullptr }; } - hasOp = true; } - if (op.HasProp("length")) { - tie(succ, len) = op.GetProp("length").ToInt32(); - if (!succ || len < 0) { + if (op.HasProp("length") && !op.GetProp("length").TypeIs(napi_undefined)) { + tie(succ, len) = op.GetProp("length").ToInt64(); + if (!succ || len < 0 || len > UINT_MAX) { HILOGE("Illegal option.length parameter"); - return { false, offset, hasLen, len, nullptr, hasOp }; + return { false, offset, hasLen, len, nullptr }; } - hasOp = true; hasLen = true; } if (op.HasProp("encoding")) { - tie(succ, encoding, ignore) = op.GetProp("encoding").ToUTF8String(); - if (!succ) { + tie(succ, encoding, ignore) = op.GetProp("encoding").ToUTF8String("utf-8"); + string_view encodingStr(encoding.get()); + if (!succ || encodingStr != "utf-8") { HILOGE("Illegal option.encoding parameter"); - return { false, offset, hasLen, len, nullptr, hasOp }; + return { false, offset, hasLen, len, nullptr }; } - hasOp = true; } - if (!op.TypeIs(napi_function)) { - hasOp = true; - } - - return { true, offset, hasLen, len, move(encoding), hasOp }; + return { true, offset, hasLen, len, move(encoding) }; } -static NError ReadTextAsync(const std::string &path, std::shared_ptr arg, ssize_t offset, - bool hasLen, ssize_t len) +static NError ReadTextAsync(const std::string &path, std::shared_ptr arg, int64_t offset, + bool hasLen, int64_t len) { OHOS::DistributedFS::FDGuard sfd; struct stat statbf; @@ -109,7 +103,7 @@ static NError ReadTextAsync(const std::string &path, std::shared_ptr statbf.st_size) ? statbf.st_size : len; string buffer(len, '\0'); - uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), len); + uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); std::unique_ptr read_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { @@ -141,7 +135,7 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto [resGetReadTextArg, offset, hasLen, len, encoding, useless] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + auto [resGetReadTextArg, offset, hasLen, len, encoding] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); if (!resGetReadTextArg) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -171,14 +165,14 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) } if (offset > statbf.st_size) { - HILOGE("Invalid offset: %{public}zd", offset); + HILOGE("Invalid offset: %{public}" PRIu64, offset); NError(EINVAL).ThrowErr(env); return nullptr; } len = (!hasLen || len > statbf.st_size) ? statbf.st_size : len; string buffer(len, '\0'); - uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), len); + uv_buf_t readbuf = uv_buf_init(const_cast(buffer.c_str()), static_cast(len)); std::unique_ptr read_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { @@ -212,7 +206,7 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) return nullptr; } - auto [resGetSecondArg, offset, hasLen, len, encoding, hasOp] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); + auto [resGetSecondArg, offset, hasLen, len, encoding] = GetReadTextArg(env, funcArg[NARG_POS::SECOND]); if (!resGetSecondArg) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -231,13 +225,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(PROCEDURE_READTEXT_NAME, 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(PROCEDURE_READTEXT_NAME, cbExec, cbComplete).val_; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.h b/interfaces/kits/js/src/mod_fs/properties/read_text.h index 71dac4eb6..8483d1d96 100644 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.h +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.h @@ -31,7 +31,7 @@ public: struct AsyncReadTextArg { NRef _refReadBuf; std::string buffer; - ssize_t len = 0; + int64_t len = 0; explicit AsyncReadTextArg(NVal refReadBuf) : _refReadBuf(refReadBuf){}; ~AsyncReadTextArg() = default; diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index bb3f49d6a..a67ce8911 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -47,7 +47,7 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { true, FileInfo { false, {}, move(fdg) } }; }; -static NError TruncateCore(napi_env env, FileInfo &fileInfo, int truncateLen) +static NError TruncateCore(napi_env env, FileInfo &fileInfo, int64_t truncateLen) { if (fileInfo.isPath) { std::unique_ptr open_req = { @@ -101,10 +101,10 @@ napi_value Truncate::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - int truncateLen = 0; + int64_t truncateLen = 0; if (funcArg.GetArgc() == NARG_CNT::TWO) { - tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); - if (!succ) { + tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(truncateLen); + if (!succ || truncateLen < 0) { HILOGE("Invalid truncate length"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -131,10 +131,10 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) if (!succ) { return nullptr; } - int truncateLen = 0; - if (funcArg.GetArgc() > NARG_CNT::ONE && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { - tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); - if (!succ) { + int64_t truncateLen = 0; + if (funcArg.GetArgc() >= NARG_CNT::TWO) { + tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(truncateLen); + if (!succ || truncateLen < 0) { HILOGE("Invalid truncate length"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -151,17 +151,12 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) } }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE || - (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number))) { + 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(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; } else { - if (funcArg.GetArgc() == NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { - NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; - } + NVal cb(env, funcArg[((funcArg.GetArgc() == NARG_CNT::TWO) ? NARG_POS::SECOND : NARG_POS::THIRD)]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_TRUNCATE_NAME, cbExec, cbCompl).val_; } } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index 538c7cad7..bc57ca853 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -44,11 +44,15 @@ public: /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ std::tuple, size_t> ToUTF8String() const; + std::tuple, size_t> ToUTF8String(std::string 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/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 0c87c93bd..cb8ead026 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -72,6 +72,21 @@ tuple, size_t> NVal::ToUTF8String() const return make_tuple(status == napi_ok, move(str), strLen); } +tuple, size_t> NVal::ToUTF8String(string defaultValue) const +{ + if (TypeIs(napi_undefined) || TypeIs(napi_function)) { + unique_ptr str = make_unique(defaultValue.size() + 1); + if (str == nullptr) { + HILOGE("Failed to request heap memory"); + return { false, nullptr, 0 }; + } + copy(defaultValue.begin(), defaultValue.end(), str.get()); + str[defaultValue.size()] = '\0'; + return make_tuple(true, move(str), defaultValue.size()); + } + return ToUTF8String(); +} + tuple, size_t> NVal::ToUTF16String() const { #ifdef FILE_SUBSYSTEM_DEBUG_LOCAL @@ -110,6 +125,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 +140,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 +155,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; -- Gitee