From 49f8e2554ba006b4ac870688227fc0e053c99d13 Mon Sep 17 00:00:00 2001 From: fengjq Date: Tue, 23 Jan 2024 17:46:33 +0800 Subject: [PATCH 1/2] Clear the warning from code check Signed-off-by: fengjq --- .../src/mod_fs/properties/prop_n_exporter.cpp | 66 +++++++++++-------- .../src/mod_fs/properties/prop_n_exporter.h | 3 + .../kits/js/src/mod_fs/properties/stat.cpp | 35 +++++----- 3 files changed, 61 insertions(+), 43 deletions(-) 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 542aaea07..7cc695fcf 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 @@ -605,9 +605,43 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) return NVal::CreateInt64(env, static_cast(ret)).val_; } -bool PropNExporter::Export() +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) +bool PropNExporter::ExportAdvance() { return exports_.AddProp({ + NVal::DeclareNapiFunction("copyDir", CopyDir::Async), + NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), + NVal::DeclareNapiFunction("copyFile", CopyFile::Async), + NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), + NVal::DeclareNapiFunction("copy", Copy::Async), + NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), + NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), + NVal::DeclareNapiFunction("createStream", CreateStream::Async), + NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), + NVal::DeclareNapiFunction("dup", Dup::Sync), + NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), + NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), + NVal::DeclareNapiFunction("listFile", ListFile::Async), + NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), + NVal::DeclareNapiFunction("lseek", Lseek::Sync), + NVal::DeclareNapiFunction("moveDir", MoveDir::Async), + NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), + NVal::DeclareNapiFunction("moveFile", Move::Async), + NVal::DeclareNapiFunction("moveFileSync", Move::Sync), + NVal::DeclareNapiFunction("readLines", ReadLines::Async), + NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), + NVal::DeclareNapiFunction("readText", ReadText::Async), + NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), + NVal::DeclareNapiFunction("symlink", Symlink::Async), + NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), + NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), + }); +} +#endif + +bool PropNExporter::Export() +{ + int ret = exports_.AddProp({ NVal::DeclareNapiFunction("access", Access), NVal::DeclareNapiFunction("accessSync", AccessSync), NVal::DeclareNapiFunction("close", Close::Async), @@ -639,35 +673,11 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("utimes", Utimes::Sync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), + }); #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - NVal::DeclareNapiFunction("copyDir", CopyDir::Async), - NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), - NVal::DeclareNapiFunction("copyFile", CopyFile::Async), - NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), - NVal::DeclareNapiFunction("copy", Copy::Async), - NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), - NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), - NVal::DeclareNapiFunction("createStream", CreateStream::Async), - NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), - NVal::DeclareNapiFunction("dup", Dup::Sync), - NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), - NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), - NVal::DeclareNapiFunction("listFile", ListFile::Async), - NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), - NVal::DeclareNapiFunction("lseek", Lseek::Sync), - NVal::DeclareNapiFunction("moveDir", MoveDir::Async), - NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), - NVal::DeclareNapiFunction("moveFile", Move::Async), - NVal::DeclareNapiFunction("moveFileSync", Move::Sync), - NVal::DeclareNapiFunction("readLines", ReadLines::Async), - NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), - NVal::DeclareNapiFunction("readText", ReadText::Async), - NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), - NVal::DeclareNapiFunction("symlink", Symlink::Async), - NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), - NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), + ret = ExportAdvance(); #endif - }); + return ret; } #ifdef WIN_PLATFORM 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 7333185a4..e38af3a53 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 @@ -60,6 +60,9 @@ public: static napi_value Read(napi_env env, napi_callback_info info); static napi_value Write(napi_env env, napi_callback_info info); bool Export() override; +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) + bool ExportAdvance(); +#endif #ifdef WIN_PLATFORM std::string GetNExporterName() override; #else diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 0bd0b8451..8fa6a0535 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -110,6 +110,25 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) return stat; } +static NError StatCore(shared_ptr arg, shared_ptr fileInfo) +{ + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!stat_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + auto err = CheckFsStat(*fileInfo, stat_req.get()); + if (err) { + return err; + } + arg->stat_ = stat_req->statbuf; +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) + arg->fileInfo_ = fileInfo; +#endif + return NError(ERRNO_NOERR); +} + napi_value Stat::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -129,21 +148,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { - std::unique_ptr stat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!stat_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - auto err = CheckFsStat(*fileInfo, stat_req.get()); - if (err) { - return err; - } - arg->stat_ = stat_req->statbuf; -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - arg->fileInfo_ = fileInfo; -#endif - return NError(ERRNO_NOERR); + return StatCore(arg, fileInfo); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { if (err) { -- Gitee From 147b2102287148a7201aa070b9a8266187fb6327 Mon Sep 17 00:00:00 2001 From: fengjq Date: Mon, 29 Jan 2024 22:31:21 +0800 Subject: [PATCH 2/2] Reduced the unlikely behavior of the main branch of the move interface code Signed-off-by: fengjq --- .../kits/js/src/mod_fs/properties/move.cpp | 40 ++++----- .../src/mod_fs/properties/prop_n_exporter.cpp | 82 ++++++++++--------- 2 files changed, 57 insertions(+), 65 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index 2c6c29957..ba5235490 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -81,29 +81,23 @@ static tuple, unique_ptr, int> ParseJsOperand(n return { true, move(src), move(dest), mode }; } -static int CopyAndDeleteFile(const string &src, const string &dest) +static int CopyAndDeleteFile(const string &src, const string &dest, int mode) { - std::unique_ptr stat_req = { + std::unique_ptr access_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!stat_req) { + if (access_req == nullptr) { HILOGE("Failed to request heap memory."); return ENOMEM; } - int ret = uv_fs_stat(nullptr, stat_req.get(), src.c_str(), nullptr); + int ret = uv_fs_access(nullptr, access_req.get(), src.c_str(), W_OK, nullptr); if (ret < 0) { - HILOGE("Failed to stat srcPath"); + HILOGE("Failed to move src file due to hasn't write permission"); return ret; } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) filesystem::path dstPath(dest); - std::error_code errCode; - if (filesystem::exists(dstPath)) { - if (!filesystem::remove(dstPath, errCode)) { - HILOGE("Failed to remove dest file, error code: %{public}d", errCode.value()); - return errCode.value(); - } - } filesystem::path srcPath(src); + std::error_code errCode; if (!filesystem::copy_file(srcPath, dstPath, filesystem::copy_options::overwrite_existing, errCode)) { HILOGE("Failed to copy file, error code: %{public}d", errCode.value()); return errCode.value(); @@ -135,9 +129,13 @@ static int CopyAndDeleteFile(const string &src, const string &dest) static int RenameFile(const string &src, const string &dest) { - int ret = 0; - uv_fs_t rename_req; - ret = uv_fs_rename(nullptr, &rename_req, src.c_str(), dest.c_str(), nullptr); + std::unique_ptr rename_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (rename_req == nullptr) { + HILOGE("Failed to request heap memory."); + return ENOMEM; + } + int ret = uv_fs_rename(nullptr, &rename_req, src.c_str(), dest.c_str(), nullptr); if (ret < 0 && (string_view(uv_err_name(ret)) == "EXDEV")) { return CopyAndDeleteFile(src, dest); } @@ -150,15 +148,9 @@ static int RenameFile(const string &src, const string &dest) static int MoveFile(const string &src, const string &dest, int mode) { - uv_fs_t access_req; - int ret = uv_fs_access(nullptr, &access_req, src.c_str(), W_OK, nullptr); - if (ret < 0) { - HILOGE("Failed to move src file due to doesn't exist or hasn't write permission"); - uv_fs_req_cleanup(&access_req); - return ret; - } if (mode == MODE_THROW_ERR) { - ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); + uv_fs_t access_req; + int ret = uv_fs_access(nullptr, &access_req, dest.c_str(), 0, nullptr); uv_fs_req_cleanup(&access_req); if (ret == 0) { HILOGE("Failed to move file due to existing destPath with MODE_THROW_ERR."); @@ -168,8 +160,6 @@ static int MoveFile(const string &src, const string &dest, int mode) HILOGE("Failed to access destPath with MODE_THROW_ERR."); return ret; } - } else { - uv_fs_req_cleanup(&access_req); } return RenameFile(src, dest); } 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 7cc695fcf..0bd0310e3 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 @@ -604,80 +604,82 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) return NVal::CreateInt64(env, static_cast(ret)).val_; } - -#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) -bool PropNExporter::ExportAdvance() +bool PropNExporter::ExportSync() { return exports_.AddProp({ - NVal::DeclareNapiFunction("copyDir", CopyDir::Async), + NVal::DeclareNapiFunction("accessSync", AccessSync), + NVal::DeclareNapiFunction("closeSync", Close::Sync), + NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), + NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), + NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), + NVal::DeclareNapiFunction("mkdirSync", MkdirSync), + NVal::DeclareNapiFunction("mkdtempSync", Mkdtemp::Sync), + NVal::DeclareNapiFunction("openSync", Open::Sync), + NVal::DeclareNapiFunction("readSync", ReadSync), + NVal::DeclareNapiFunction("renameSync", Rename::Sync), + NVal::DeclareNapiFunction("rmdirSync", Rmdirent::Sync), + NVal::DeclareNapiFunction("statSync", Stat::Sync), + NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), + NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), + NVal::DeclareNapiFunction("utimes", Utimes::Sync), + NVal::DeclareNapiFunction("writeSync", WriteSync), +#if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) NVal::DeclareNapiFunction("copyDirSync", CopyDir::Sync), - NVal::DeclareNapiFunction("copyFile", CopyFile::Async), NVal::DeclareNapiFunction("copyFileSync", CopyFile::Sync), - NVal::DeclareNapiFunction("copy", Copy::Async), - NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), - NVal::DeclareNapiFunction("createStream", CreateStream::Async), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), NVal::DeclareNapiFunction("dup", Dup::Sync), - NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), NVal::DeclareNapiFunction("fdopenStreamSync", FdopenStream::Sync), - NVal::DeclareNapiFunction("listFile", ListFile::Async), NVal::DeclareNapiFunction("listFileSync", ListFile::Sync), NVal::DeclareNapiFunction("lseek", Lseek::Sync), - NVal::DeclareNapiFunction("moveDir", MoveDir::Async), NVal::DeclareNapiFunction("moveDirSync", MoveDir::Sync), - NVal::DeclareNapiFunction("moveFile", Move::Async), NVal::DeclareNapiFunction("moveFileSync", Move::Sync), - NVal::DeclareNapiFunction("readLines", ReadLines::Async), NVal::DeclareNapiFunction("readLinesSync", ReadLines::Sync), - NVal::DeclareNapiFunction("readText", ReadText::Async), NVal::DeclareNapiFunction("readTextSync", ReadText::Sync), - NVal::DeclareNapiFunction("symlink", Symlink::Async), NVal::DeclareNapiFunction("symlinkSync", Symlink::Sync), - NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), +#endif }); } -#endif -bool PropNExporter::Export() +bool PropNExporter::ExportAsync() { - int ret = exports_.AddProp({ + return exports_.AddProp({ NVal::DeclareNapiFunction("access", Access), - NVal::DeclareNapiFunction("accessSync", AccessSync), NVal::DeclareNapiFunction("close", Close::Async), - NVal::DeclareNapiFunction("closeSync", Close::Sync), NVal::DeclareNapiFunction("fdatasync", Fdatasync::Async), - NVal::DeclareNapiFunction("fdatasyncSync", Fdatasync::Sync), NVal::DeclareNapiFunction("fsync", Fsync::Async), - NVal::DeclareNapiFunction("fsyncSync", Fsync::Sync), NVal::DeclareNapiFunction("lstat", Lstat::Async), - NVal::DeclareNapiFunction("lstatSync", Lstat::Sync), NVal::DeclareNapiFunction("mkdir", Mkdir), - NVal::DeclareNapiFunction("mkdirSync", MkdirSync), NVal::DeclareNapiFunction("mkdtemp", Mkdtemp::Async), - NVal::DeclareNapiFunction("mkdtempSync", Mkdtemp::Sync), NVal::DeclareNapiFunction("open", Open::Async), - NVal::DeclareNapiFunction("openSync", Open::Sync), - NVal::DeclareNapiFunction("read", Read), - NVal::DeclareNapiFunction("readSync", ReadSync), NVal::DeclareNapiFunction("rename", Rename::Async), - NVal::DeclareNapiFunction("renameSync", Rename::Sync), NVal::DeclareNapiFunction("rmdir", Rmdirent::Async), - NVal::DeclareNapiFunction("rmdirSync", Rmdirent::Sync), NVal::DeclareNapiFunction("stat", Stat::Async), - NVal::DeclareNapiFunction("statSync", Stat::Sync), NVal::DeclareNapiFunction("truncate", Truncate::Async), - NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), - NVal::DeclareNapiFunction("unlink", Unlink), - NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), - NVal::DeclareNapiFunction("utimes", Utimes::Sync), + NVal::DeclareNapiFunction("read", Read), NVal::DeclareNapiFunction("write", Write), - NVal::DeclareNapiFunction("writeSync", WriteSync), - }); + NVal::DeclareNapiFunction("unlink", Unlink), #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - ret = ExportAdvance(); + NVal::DeclareNapiFunction("copyDir", CopyDir::Async), + NVal::DeclareNapiFunction("copyFile", CopyFile::Async), + NVal::DeclareNapiFunction("copy", Copy::Async), + NVal::DeclareNapiFunction("createRandomAccessFile", CreateRandomAccessFile::Async), + NVal::DeclareNapiFunction("createStream", CreateStream::Async), + NVal::DeclareNapiFunction("fdopenStream", FdopenStream::Async), + NVal::DeclareNapiFunction("listFile", ListFile::Async), + NVal::DeclareNapiFunction("moveDir", MoveDir::Async), + NVal::DeclareNapiFunction("moveFile", Move::Async), + NVal::DeclareNapiFunction("readLines", ReadLines::Async), + NVal::DeclareNapiFunction("readText", ReadText::Async), + NVal::DeclareNapiFunction("symlink", Symlink::Async), + NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), #endif - return ret; + }); +} + +bool PropNExporter::Export() +{ + return ExportSync() && ExportAsync(); } #ifdef WIN_PLATFORM -- Gitee