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 f3f513c05eda5982855310a4ca753f9537e9cbb3..74098271a104a5a8259f56d71d4edf67d59cf05d 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 @@ -59,9 +59,12 @@ static tuple ParseJsFP(napi_env env, napi_value FPFromJs) static tuple GetRAFReadArg(napi_env env, napi_value ReadBuf, napi_value option) { - bool succ = false, hasPos = false; + bool succ = false; + bool hasPos = false; void *buf = nullptr; - size_t len, pos, offset; + size_t len = 0; + size_t pos = 0; + size_t offset = 0; tie(succ, buf, len, hasPos, pos, offset) = CommonFunc::GetReadArg(env, ReadBuf, option); if (!succ) { return { false, nullptr, 0, false, 0, 0 }; @@ -72,9 +75,11 @@ static tuple GetRAFReadArg(napi_env e static tuple GetRAFWriteArg(napi_env env, napi_value WriteBuf, napi_value option) { - bool succ = false, hasPos = false; + bool succ = false; + bool hasPos = false; void *buf = nullptr; - size_t len, pos; + size_t len = 0; + size_t pos = 0; tie(succ, ignore, buf, len, hasPos, pos) = CommonFunc::GetWriteArg(env, WriteBuf, option); if (!succ) { return { false, nullptr, 0, false, 0 }; @@ -187,19 +192,17 @@ struct AsyncIOReadArg { ~AsyncIOReadArg() = default; }; -napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info) +static napi_value ReadExec(napi_env env, NFuncArg &funcArg) { - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); - return nullptr; - } auto rafEntity = GetRAFEntity(env, funcArg.GetThisVar()); if (!rafEntity) { return nullptr; } - bool succ = false, hasPos = false; - size_t len, pos, offset; + bool succ = false; + bool hasPos = false; + size_t len = 0; + size_t pos = 0; + size_t offset = 0; void* buf = nullptr; tie(succ, buf, len, hasPos, pos, offset) = GetRAFReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { @@ -218,7 +221,6 @@ napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info rafEntity->fpointer += actLen; return UniError(ERRNO_NOERR); }; - auto cbCompl = [arg](napi_env env, UniError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; @@ -243,6 +245,17 @@ napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info } } +napi_value RandomAccessFileNExporter::Read(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + return ReadExec(env, funcArg); +} + napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -256,9 +269,11 @@ napi_value RandomAccessFileNExporter::WriteSync(napi_env env, napi_callback_info return nullptr; } - bool succ = false, hasPos = false; + bool succ = false; + bool hasPos = false; void *buf = nullptr; - size_t len, pos; + size_t len = 0; + size_t pos = 0; tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); @@ -297,9 +312,11 @@ napi_value RandomAccessFileNExporter::Write(napi_env env, napi_callback_info inf if (!rafEntity) { return nullptr; } - bool succ = false, hasPos = false; + bool succ = false; + bool hasPos = false; void *buf = nullptr; - size_t len, pos; + size_t len = 0; + size_t pos = 0; tie(succ, buf, len, hasPos, pos) = GetRAFWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid buffer/options"); 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 44ba3246b0882cd4d07e7c4820e35eb141c1aa15..7a79f7d49323c1b8333f944c33d218c7e2f1e56c 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 @@ -35,9 +35,9 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info info) +napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -68,18 +68,18 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info info) } size_t actLen = fread(buf, 1, len, filp); - if (actLen != static_cast(len) && ferror(filp)) { + if ((actLen != static_cast(len) && !feof(filp)) || ferror(filp)) { HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); - NError(errno).ThrowErr(env); + NError(EIO).ThrowErr(env); return nullptr; } return NVal::CreateInt64(env, actLen).val_; } -napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info) +napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -98,9 +98,9 @@ napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info) return NVal::CreateUndefined(env).val_; } -napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info info) +napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -130,9 +130,9 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info info) } size_t writeLen = fwrite(buf, 1, len, filp); - if (writeLen != len && ferror(filp)) { + if (writeLen != static_cast(len) && ferror(filp)) { HILOGE("Failed to fwrite with len, writeLen: %{public}zu, len: %{public}" PRId64, writeLen, len); - NError(errno).ThrowErr(env); + NError(EIO).ThrowErr(env); return nullptr; } @@ -148,9 +148,9 @@ static bool HasOption(napi_env env, napi_value optionFromJsArg) return false; } -napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) +napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -189,7 +189,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) arg->actLen = fwrite(buf, 1, len, filp); if ((arg->actLen != static_cast(len)) && ferror(filp)) { HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", arg->actLen); - return NError(errno); + return NError(EIO); } return NError(ERRNO_NOERR); }; @@ -215,9 +215,9 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info info) } } -napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) +napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -254,9 +254,9 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) return NError(errno); } size_t actLen = fread(buf, 1, len, filp); - if (actLen != static_cast(len) && ferror(filp)) { + if ((actLen != static_cast(len) && !feof(filp)) || ferror(filp)) { HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); - return NError(errno); + return NError(EIO); } else { arg->lenRead = actLen; return NError(ERRNO_NOERR); @@ -284,9 +284,9 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info info) } } -napi_value StreamNExporter::Close(napi_env env, napi_callback_info info) +napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); @@ -328,9 +328,9 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info info) } } -napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info info) +napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) { - NFuncArg funcArg(env, info); + NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h index afb2579e6e5e80c65111c14017ea3797823a372b..647a1173b7b2dcfed40adcd52e952ebf3f3043d7 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h @@ -29,15 +29,15 @@ public: bool Export() override; std::string GetClassName() override; - static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value Constructor(napi_env env, napi_callback_info cbInfo); - static napi_value WriteSync(napi_env env, napi_callback_info cbinfo); - static napi_value ReadSync(napi_env env, napi_callback_info cbinfo); - static napi_value CloseSync(napi_env env, napi_callback_info cbinfo); + static napi_value WriteSync(napi_env env, napi_callback_info cbInfo); + static napi_value ReadSync(napi_env env, napi_callback_info cbInfo); + static napi_value CloseSync(napi_env env, napi_callback_info cbInfo); - static napi_value Write(napi_env env, napi_callback_info cbinfo); - static napi_value Read(napi_env env, napi_callback_info cbinfo); - static napi_value Close(napi_env env, napi_callback_info cbinfo); + static napi_value Write(napi_env env, napi_callback_info cbInfo); + static napi_value Read(napi_env env, napi_callback_info cbInfo); + static napi_value Close(napi_env env, napi_callback_info cbInfo); StreamNExporter(napi_env env, napi_value exports); ~StreamNExporter() override; diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 21aaf19678b317fe9df45e178637883abf7a5898..575f6ecc55c543f592e61d7b605da80950aba2ad 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -21,6 +21,10 @@ #include #include +#include "class_stat/stat_entity.h" +#include "class_stat/stat_n_exporter.h" +#include "class_stream/stream_entity.h" +#include "class_stream/stream_n_exporter.h" #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" @@ -122,6 +126,46 @@ int CommonFunc::ConvertJsFlags(int &flags) return flagsABI; } +NVal CommonFunc::InstantiateStat(napi_env env, struct stat &buf) +{ + napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); + if (!objStat) { + HILOGE("Failed to instantiate stat class"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + auto statEntity = NClass::GetEntityOf(env, objStat); + if (!statEntity) { + HILOGE("Failed to get stat entity"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + statEntity->stat_ = buf; + return { env, objStat }; +} + +NVal CommonFunc::InstantiateStream(napi_env env, unique_ptr fp) +{ + napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {}); + if (!objStream) { + HILOGE("INNER BUG. Cannot instantiate stream"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + auto streamEntity = NClass::GetEntityOf(env, objStream); + if (!streamEntity) { + HILOGE("Cannot instantiate stream because of void entity"); + NError(EIO).ThrowErr(env); + return NVal(); + } + + streamEntity->fp.swap(fp); + return { env, objStream }; +} + void CommonFunc::fs_req_cleanup(uv_fs_t* req) { uv_fs_req_cleanup(req); diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 062b085e1e717155044d643b08b10a83895be39b..1fe648de49265e3d616b4932da95254eb6a149d8 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -46,6 +46,8 @@ void InitOpenMode(napi_env env, napi_value exports); struct CommonFunc { static int ConvertJsFlags(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); 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 029d1472b66d459d19cfeb528b76d5a675c58b07..21251406f233170974afe8eca12f0b9a5d9054a2 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -58,8 +58,8 @@ static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to request heap memory."); return NError(ERRNO_NOERR); } - int ret = uv_fs_open(nullptr, open_req.get(), srcFile.path.get(), O_RDONLY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + int ret = uv_fs_open(nullptr, open_req.get(), srcFile.path.get(), O_RDWR, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); @@ -94,7 +94,7 @@ static NError SendFileCore(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to request heap memory."); return NError(ERRNO_NOERR); } - int ret = uv_fs_sendfile(nullptr, sendfile_req.get(), srcFile.fdg.GetFD(), destFile.fdg.GetFD(), 0, + int ret = uv_fs_sendfile(nullptr, sendfile_req.get(), destFile.fdg.GetFD(), srcFile.fdg.GetFD(), 0, statbf.st_size, nullptr); if (ret < 0) { HILOGE("Failed to sendfile by ret : %{public}d", ret); @@ -116,8 +116,9 @@ static tuple ParseJsModeAndProm(napi_env env, const NFuncArg& f hasMode = true; } + bool succ = false; if (hasMode) { - auto [succ, mode] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + tie(succ, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); if (!succ) { return { false, mode, promise }; } @@ -159,7 +160,7 @@ napi_value CopyFile::Sync(napi_env env, napi_callback_info info) } auto [succMode, mode, unused] = ParseJsModeAndProm(env, funcArg); - if (!succMode) { + if (!succMode || mode) { HILOGE("Failed to convert mode to int32"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -199,7 +200,7 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) } auto [succMode, mode, promise] = ParseJsModeAndProm(env, funcArg); - if (!succMode) { + if (!succMode || mode) { HILOGE("Failed to convert mode to int32"); NError(EINVAL).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index e63178df283257f0ea7fc0861927159fb3e1c7f5..5ea0beb50ae40bddbf8a9e74309e1259fa516582 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -20,6 +20,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -28,26 +29,6 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static NVal InstantiateStream(napi_env env, unique_ptr fp) -{ - napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {}); - if (!objStream) { - HILOGE("INNER BUG. Cannot instantiate stream"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - auto streamEntity = NClass::GetEntityOf(env, objStream); - if (!streamEntity) { - HILOGE("Cannot instantiate stream because of void entity"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - streamEntity->fp.swap(fp); - return { env, objStream }; -} - static tuple GetCreateStreamArgs(napi_env env, const NFuncArg &funcArg) { auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); @@ -86,7 +67,7 @@ napi_value CreateStream::Sync(napi_env env, napi_callback_info info) return nullptr; } - return InstantiateStream(env, move(fp)).val_; + return CommonFunc::InstantiateStream(env, move(fp)).val_; } napi_value CreateStream::Async(napi_env env, napi_callback_info info) @@ -105,7 +86,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -123,7 +104,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - return InstantiateStream(env, move(arg->fp)); + return CommonFunc::InstantiateStream(env, move(arg->fp)); }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp index f5dbc0f4f24b670de0a610e7c6e71abd6fcaf3ee..54dae67b94d23a26c28a2a59c171829823393a67 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.cpp @@ -100,13 +100,12 @@ napi_value Fdatasync::Async(napi_env env, napi_callback_info info) return {NVal::CreateUndefined(env)}; }; - const string procedureName = "FileIOFdatasync"; NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplCallback).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_FDATASYNC_NAME, cbExec, cbComplCallback).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplCallback).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_FDATASYNC_NAME, cbExec, cbComplCallback).val_; } } } // OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdatasync.h b/interfaces/kits/js/src/mod_fs/properties/fdatasync.h index f4c3ba0e5a17ce4a5a6c9926e3b0fd7c6e8bdc07..6c109c642037b527c6b47503e17061b718f4aa53 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fdatasync.h +++ b/interfaces/kits/js/src/mod_fs/properties/fdatasync.h @@ -24,5 +24,6 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); }; +const std::string PROCEDURE_FDATASYNC_NAME = "FileIOFdatasync"; } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDATASYNC_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index 2fab9f21a5a9d230349632fca72a4705ecda6ad7..eee88be73082119802619b83ae1d78e17725b36f 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -20,6 +20,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -28,26 +29,6 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static NVal InstantiateStream(napi_env env, unique_ptr fp) -{ - napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {}); - if (!objStream) { - HILOGE("INNER BUG. Cannot instantiate stream"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - auto streamEntity = NClass::GetEntityOf(env, objStream); - if (!streamEntity) { - HILOGE("Cannot instantiate stream because of void entity"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - streamEntity->fp.swap(fp); - return { env, objStream }; -} - static tuple GetFdopenStreamArgs(napi_env env, const NFuncArg &funcArg) { auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); @@ -73,7 +54,7 @@ napi_value FdopenStream::Sync(napi_env env, napi_callback_info info) } auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg); - if (!resGetFdopenStreamArgs) { + if (!resGetFdopenStreamArgs || fd < 0) { HILOGE("Invalid fd or mode from JS arugments"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -86,7 +67,7 @@ napi_value FdopenStream::Sync(napi_env env, napi_callback_info info) return nullptr; } - return InstantiateStream(env, move(fp)).val_; + return CommonFunc::InstantiateStream(env, move(fp)).val_; } napi_value FdopenStream::Async(napi_env env, napi_callback_info info) @@ -99,13 +80,13 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) } auto [resGetFdopenStreamArgs, fd, mode] = GetFdopenStreamArgs(env, funcArg); - if (!resGetFdopenStreamArgs) { + if (!resGetFdopenStreamArgs || fd < 0) { NError(EINVAL).ThrowErr(env); return nullptr; } auto arg = make_shared(); - if (arg == nullptr) { + if (!arg) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -123,7 +104,7 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - return InstantiateStream(env, move(arg->fp)); + return CommonFunc::InstantiateStream(env, move(arg->fp)); }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp index 422389c64e1cab3f7c12d5d6e159f38f3c49a20e..6fcb230fdfb8d1db6075a8d57fe2903b37171d2d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.cpp @@ -98,14 +98,13 @@ napi_value Fsync::Async(napi_env env, napi_callback_info info) } }; - const string procedureName = "FileIOFsync"; size_t argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); if (argc == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_FSYNC_NAME, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_FSYNC_NAME, cbExec, cbComplete).val_; } } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/fsync.h b/interfaces/kits/js/src/mod_fs/properties/fsync.h index e033909780a250129397c17807ac877618f23faa..f52573afe09a7cb625edfc13b58e0a2240c12274 100644 --- a/interfaces/kits/js/src/mod_fs/properties/fsync.h +++ b/interfaces/kits/js/src/mod_fs/properties/fsync.h @@ -24,5 +24,6 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); }; +const std::string PROCEDURE_FSYNC_NAME = "FileIOFsync"; } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FSYNC_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 06ecdf6398ef2a29929fd90236e214dbe4187ba8..315564b84ca75798c9be02ed600e590de60fe808 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -17,35 +17,15 @@ #include #include -#include "../common_func.h" #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static NVal InstantiateStat(napi_env env, struct stat &buf) -{ - napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); - if (!objStat) { - HILOGE("Failed to instantiate stat class"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - auto statEntity = NClass::GetEntityOf(env, objStat); - if (!statEntity) { - HILOGE("Failed to get stat entity"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - statEntity->stat_ = buf; - return { env, objStat }; -} - napi_value Lstat::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -70,7 +50,7 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) return nullptr; } - auto stat = InstantiateStat(env, buf).val_; + auto stat = CommonFunc::InstantiateStat(env, buf).val_; return stat; } @@ -110,17 +90,16 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - auto stat = InstantiateStat(env, arg->stat_); + auto stat = CommonFunc::InstantiateStat(env, arg->stat_); return stat; }; NVal thisVar(env, funcArg.GetThisVar()); - const string procedureName = "FileIOLstat"; if (funcArg.GetArgc() == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LSTAT_NAME, cbExec, cbCompl).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LSTAT_NAME, cbExec, cbCompl).val_; } } } // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.h b/interfaces/kits/js/src/mod_fs/properties/lstat.h index 00adc462766c3024aa4f3f1d356974c069f0c5d8..6d28f82a26cbd382433f1574dff5f63582c17b3f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.h +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.h @@ -24,5 +24,6 @@ public: static napi_value Async(napi_env env, napi_callback_info info); static napi_value Sync(napi_env env, napi_callback_info info); }; +const std::string PROCEDURE_LSTAT_NAME = "FileIOLstat"; } // namespace OHOS::FileManagement::ModuleFileIO #endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_LSTAT_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 43ab54a2b316e8cb2b2b9309d38c9f3b86bc4da5..2beb0445eb356f4060a48f4854d52bd7f1ac3a1b 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -50,7 +50,7 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) } int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path.c_str()), nullptr); if (ret < 0) { - HILOGE("Failed to create a temporary directory with path: %{public}s", path.c_str()); + HILOGE("Failed to create a temporary directory with path"); NError(errno).ThrowErr(env); return nullptr; } @@ -75,16 +75,16 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - auto cbExec = [path = tmp.get(), arg]() -> NError { + auto cbExec = [path = string(tmp.get()), arg]() -> NError { std::unique_ptr mkdtemp_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!mkdtemp_req) { HILOGE("Failed to request heap memory."); return NError(ERRNO_NOERR); } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), const_cast(path), nullptr); + int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); if (ret < 0) { - HILOGE("Failed to create a temporary directory with path: %{public}s", path); + HILOGE("Failed to create a temporary directory with path"); return NError(errno); } else { *arg = mkdtemp_req->path; @@ -100,14 +100,13 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) } }; - const string procedureName = "FileIOmkdtemp"; size_t argc = funcArg.GetArgc(); NVal thisVar(env, funcArg.GetThisVar()); if (argc == NARG_CNT::ONE) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_MKDTEMP_NAME, cbExec, cbComplete).val_; } } } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h index ac968c32e5ca7221a54cad2ae7fc13b399111f20..4d04ac3de18d07c95651bf4647afd83b28b073a7 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.h @@ -28,6 +28,7 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); static napi_value Async(napi_env env, napi_callback_info info); }; +const std::string PROCEDURE_MKDTEMP_NAME = "FileIOMkdtemp"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index dc97db32ccb97b95f1721572323b88d6d88cb907..bb9aa36fb8e34877828e2074413485cc38a6b04b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -19,13 +19,12 @@ #include #include -#include "../common_func.h" #include "ability.h" #include "class_file/file_entity.h" #include "class_file/file_n_exporter.h" +#include "common_func.h" #include "datashare_helper.h" #include "filemgmt_libhilog.h" -#include "filemgmt_libn.h" #include "remote_uri.h" namespace OHOS { @@ -83,7 +82,7 @@ static int OpenFileByDatashare(napi_env env, napi_value argv, string path, int f std::shared_ptr dataShareHelper = nullptr; int fd = -1; sptr remote = new (std::nothrow) IRemoteStub(); - if (remote == nullptr) { + if (!remote) { return ENOMEM; } @@ -130,7 +129,7 @@ napi_value Open::Sync(napi_env env, napi_callback_info info) return nullptr; } int ret = uv_fs_open(nullptr, open_req.get(), path.get(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); NError(errno).ThrowErr(env); @@ -186,7 +185,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) return NError(ERRNO_NOERR); } int ret = uv_fs_open(nullptr, open_req.get(), path.c_str(), mode, S_IRUSR | - S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { HILOGE("Failed to open file for libuv error %{public}d", ret); return NError(errno); diff --git a/interfaces/kits/js/src/mod_fs/properties/open.h b/interfaces/kits/js/src/mod_fs/properties/open.h index 4103e5408d1fb2b296257e8e8d06068e4d7c2bd3..7ff6662b99e58e3d4a1ab355351edc10ab4e0e86 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.h +++ b/interfaces/kits/js/src/mod_fs/properties/open.h @@ -16,8 +16,8 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_OPEN_H #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_OPEN_H +#include "filemgmt_libn.h" #include "iremote_broker.h" -#include "../../common/napi/uni_header.h" namespace OHOS { namespace FileManagement { 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 00606c0e9aee65c32123ad709ff2581b944c65ea..f20fe97f7b45d3c4e027e187bfff53cd4cd98552 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -103,7 +103,7 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) } auto result = make_shared(); - if (result == nullptr) { + if (!result) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; @@ -348,9 +348,9 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) return NVal::CreateInt64(env, actLen).val_; } -static NError ReadExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) +static NError ReadExec(shared_ptr arg, char *buf, size_t len, int fd, size_t offset) { - uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_buf_t buffer = uv_buf_init(buf, len); std::unique_ptr read_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { @@ -402,8 +402,9 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf = buf, len = len, fd = fd, offset = offset]() -> NError { - return ReadExec(arg, buf, len, fd, offset); + + auto cbExec = [arg, buf, len, fd, offset]() -> NError { + return ReadExec(arg, static_cast(buf), len, fd, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { @@ -430,9 +431,9 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) } } -NError PropNExporter::WriteExec(shared_ptr arg, void *buf, size_t len, int fd, size_t offset) +static NError WriteExec(shared_ptr arg, char *buf, size_t len, int fd, size_t offset) { - uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_buf_t buffer = uv_buf_init(buf, len); std::unique_ptr write_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!write_req) { @@ -485,8 +486,9 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf = buf, len = len, fd = fd, offset = offset]() -> NError { - return WriteExec(arg, buf, len, fd, offset); + + auto cbExec = [arg, buf, len, fd, offset]() -> NError { + return WriteExec(arg, static_cast(buf), len, fd, offset); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h index dce2a3ca40a41c3b8a13839bd60b9e4674557e4e..29181014a7121b383569ae209005c7ba4a48fd64 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h @@ -59,7 +59,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 NError 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_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 5ff8590e2dd0cc0096be7767c6e3732d4dee845d..b577ec89e4c589f9d44996b55809759c2f71698d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -59,7 +59,7 @@ static tuple, bool> GetReadText } if (op.HasProp("encoding")) { - auto [succ, encoding, unuse] = op.GetProp("encoding").ToUTF8String(); + tie(succ, encoding, ignore) = op.GetProp("encoding").ToUTF8String(); if (!succ) { HILOGE("Illegal option.encoding parameter"); return { false, offset, hasLen, len, nullptr, hasOp }; @@ -74,7 +74,7 @@ static tuple, bool> GetReadText return { true, offset, hasLen, len, move(encoding), hasOp }; } -static NError ReadTextAsync(const std::string path, std::shared_ptr arg, ssize_t offset, +static NError ReadTextAsync(const std::string &path, std::shared_ptr arg, ssize_t offset, bool hasLen, ssize_t len) { OHOS::DistributedFS::FDGuard sfd; @@ -86,7 +86,7 @@ static NError ReadTextAsync(const std::string path, std::shared_ptr #include -#include "../common_func.h" #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static NVal InstantiateStat(napi_env env, struct stat &buf) -{ - napi_value objStat = NClass::InstantiateClass(env, StatNExporter::className_, {}); - if (!objStat) { - HILOGE("Failed to instantiate stat class"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - auto statEntity = NClass::GetEntityOf(env, objStat); - if (!statEntity) { - HILOGE("Failed to get stat entity"); - NError(EIO).ThrowErr(env); - return NVal(); - } - - statEntity->stat_ = buf; - return { env, objStat }; -} - static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJsArg) { auto [isPath, path, ignore] = NVal(env, pathOrFdFromJsArg).ToUTF8String(); @@ -94,7 +74,7 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) } } - auto stat = InstantiateStat(env, buf).val_; + auto stat = CommonFunc::InstantiateStat(env, buf).val_; return stat; } @@ -134,7 +114,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - auto stat = InstantiateStat(env, arg->stat_); + auto stat = CommonFunc::InstantiateStat(env, arg->stat_); return stat; }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index dabaa217385e3ab898bc06e99222a1b8dbdd0eb2..e6c13b33a31fa0b81f1203f2105c5d1f7f9e73bd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -19,7 +19,7 @@ #include #include -#include "../common_func.h" +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -41,70 +41,75 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { true, FileInfo { false, {}, { fd, false } } }; }; -napi_value Truncate::Sync(napi_env env, napi_callback_info info) +static NError TruncateCore(napi_env env, FileInfo &fileInfo, int truncateLen) { - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); - if (!succ) { - NError(EINVAL).ThrowErr(env); - return nullptr; - } - int truncateLen = 0; - if (funcArg.GetArgc() == NARG_CNT::TWO) { - tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); - if (!succ) { - HILOGE("Invalid truncate length"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - } if (fileInfo.isPath) { std::unique_ptr open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!open_req) { HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; + return NError(ENOMEM); } int ret = uv_fs_open(nullptr, open_req.get(), fileInfo.path.get(), O_RDWR, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr); if (ret < 0) { - NError(errno).ThrowErr(env); - return nullptr; + return NError(errno); } std::unique_ptr ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!ftruncate_req) { HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; + return NError(ENOMEM); } ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), ret, truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by path"); - NError(errno).ThrowErr(env); - return nullptr; + return NError(errno); } } else { std::unique_ptr ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!ftruncate_req) { HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; + return NError(ENOMEM); } int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo.fdg.GetFD(), truncateLen, nullptr); if (ret < 0) { HILOGE("Failed to truncate file by fd"); + return NError(EINVAL); + } + } + return NError(ERRNO_NOERR); +} + +napi_value Truncate::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); + if (!succ) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + int truncateLen = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt64(); + if (!succ) { + HILOGE("Invalid truncate length"); NError(EINVAL).ThrowErr(env); return nullptr; } } + auto err = TruncateCore(env, fileInfo, truncateLen); + if (err) { + err.ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; } @@ -130,41 +135,7 @@ napi_value Truncate::Async(napi_env env, napi_callback_info info) } } auto cbExec = [fileInfo = make_shared(move(fileInfo)), truncateLen, env = env]() -> NError { - using uv_fs_unique_ptr_type = std::unique_ptr; - if (fileInfo->isPath) { - uv_fs_unique_ptr_type open_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!open_req) { - HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); - } - int ret = uv_fs_open(nullptr, open_req.get(), fileInfo->path.get(), O_RDWR, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, nullptr); - if (ret < 0) { - return NError(errno); - } - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!ftruncate_req) { - HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); - } - ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), ret, truncateLen, nullptr); - if (ret < 0) { - HILOGE("Failed to truncate file by path"); - return NError(errno); - } - } else { - uv_fs_unique_ptr_type ftruncate_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!ftruncate_req) { - HILOGE("Failed to request heap memory."); - return NError(ERRNO_NOERR); - } - int ret = uv_fs_ftruncate(nullptr, ftruncate_req.get(), fileInfo->fdg.GetFD(), truncateLen, nullptr); - if (ret < 0) { - HILOGE("Failed to truncate file by fd"); - return NError(errno); - } - } - return NError(ERRNO_NOERR); + return TruncateCore(env, *fileInfo, truncateLen); }; auto cbCompl = [](napi_env env, NError err) -> NVal { if (err) { diff --git a/interfaces/kits/js/src/mod_hash/hash.cpp b/interfaces/kits/js/src/mod_hash/hash.cpp index 3158df16e7f027fbf02ef7c99e2c3d2edc2f2e65..044ccab58e8bd634d781ba86bd879111ca16f7f7 100644 --- a/interfaces/kits/js/src/mod_hash/hash.cpp +++ b/interfaces/kits/js/src/mod_hash/hash.cpp @@ -57,7 +57,7 @@ static tuple, HASH_ALGORITHM_TYPE, bool> GetHashArgs(na return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; } - if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { HILOGE("Invalid callback"); NError(EINVAL).ThrowErr(env); return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise }; @@ -104,13 +104,12 @@ napi_value Hash::Async(napi_env env, napi_callback_info info) return { NVal::CreateUTF8String(env, *arg) }; }; - const string procedureName = "FileIOHash"; NVal thisVar(env, funcArg.GetThisVar()); if (isPromise) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_HASH_NAME, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[NARG_POS::THIRD]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_HASH_NAME, cbExec, cbComplete).val_; } } diff --git a/interfaces/kits/js/src/mod_hash/hash.h b/interfaces/kits/js/src/mod_hash/hash.h index 1a9f85505256d5322b4f936c2b4c2f378b467c54..0ff53fb96d6442a331f7865b2697e4f109bf1ef0 100644 --- a/interfaces/kits/js/src/mod_hash/hash.h +++ b/interfaces/kits/js/src/mod_hash/hash.h @@ -50,6 +50,7 @@ public: HashNExporter(napi_env env, napi_value exports); ~HashNExporter() = default; }; +const std::string PROCEDURE_HASH_NAME = "FileIOHash"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS