From dbbdb961ab30d4354d347423f05b1be58f0d1690 Mon Sep 17 00:00:00 2001 From: fengjq Date: Tue, 29 Aug 2023 19:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=9B=9E=E5=90=88monthly0815=E3=80=91?= =?UTF-8?q?mod=5Ffs=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81bug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= 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 | 4 + .../randomaccessfile_n_exporter.cpp | 8 ++ .../kits/js/src/mod_fs/class_stream/flush.cpp | 8 +- .../mod_fs/class_stream/stream_n_exporter.cpp | 79 +++++++++++-------- .../mod_fs/class_stream/stream_n_exporter.h | 8 +- .../js/src/mod_fs/properties/copy_file.cpp | 17 ++-- .../js/src/mod_fs/properties/listfile.cpp | 4 +- .../kits/js/src/mod_fs/properties/movedir.cpp | 17 +++- 8 files changed, 93 insertions(+), 52 deletions(-) mode change 100755 => 100644 interfaces/kits/js/src/mod_fs/class_stream/flush.cpp mode change 100755 => 100644 interfaces/kits/js/src/mod_fs/properties/listfile.cpp 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 9e3c9ea0e..0fe49afad 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 @@ -167,6 +167,10 @@ napi_value FileNExporter::Lock(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [exclusive, fileEntity]() -> NError { + if (!fileEntity || !fileEntity->fd_.get()) { + HILOGE("File has been closed in Lock cbExec possibly"); + return NError(EIO); + } int ret = 0; auto mode = exclusive ? LOCK_EX : LOCK_SH; ret = flock(fileEntity->fd_.get()->GetFD(), mode); diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp index a32c4d8fa..2b7ec3508 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -190,6 +190,10 @@ static napi_value ReadExec(napi_env env, NFuncArg &funcArg, RandomAccessFileEnti } offset = CalculateOffset(offset, rafEntity->filePointer); auto cbExec = [env, arg, buf, len, offset, rafEntity]() -> NError { + if (!rafEntity || !rafEntity->fd.get()) { + HILOGE("RandomAccessFile has been closed in read cbExec possibly"); + return NError(EIO); + } int actLen = DoReadRAF(env, buf, len, rafEntity->fd.get()->GetFD(), offset); if (actLen < 0) { return NError(actLen); @@ -293,6 +297,10 @@ static napi_value WriteExec(napi_env env, NFuncArg &funcArg, RandomAccessFileEnt } offset = CalculateOffset(offset, rafEntity->filePointer); auto cbExec = [env, arg, buf, len, fd = rafEntity->fd.get()->GetFD(), offset, rafEntity]() -> NError { + if (!rafEntity || !rafEntity->fd.get()) { + HILOGE("RandomAccessFile has been closed in write cbExec possibly"); + return NError(EIO); + } int writeLen = DoWriteRAF(env, buf, len, fd, offset); if (writeLen < 0) { HILOGE("Failed to write file for %{public}d", writeLen); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp b/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp old mode 100755 new mode 100644 index 6d21dd570..105eab7ba --- a/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp @@ -39,7 +39,7 @@ napi_value Flush::Sync(napi_env env, napi_callback_info info) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may has been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } @@ -64,12 +64,16 @@ napi_value Flush::Async(napi_env env, napi_callback_info info) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may has been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } auto cbExec = [streamEntity]() -> NError { + if (!streamEntity || !streamEntity->fp) { + HILOGE("Stream has been closed in flush cbExec possibly"); + return NError(EIO); + } int ret = fflush(streamEntity->fp.get()); if (ret < 0) { HILOGE("Failed to fflush file in the stream"); 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 6ecdf56b2..bf11f3609 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 @@ -47,7 +47,7 @@ napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may have been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } @@ -91,7 +91,7 @@ napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info cbInfo) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may have been closed yet"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } @@ -112,7 +112,7 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may has been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } @@ -144,25 +144,8 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) return NVal::CreateInt64(env, static_cast(writeLen)).val_; } -napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) +static napi_value WriteExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) { - NFuncArg funcArg(env, cbInfo); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may has been closed"); - NError(EIO).ThrowErr(env); - return nullptr; - } - - FILE *filp = nullptr; - filp = streamEntity->fp.get(); - auto [succ, bufGuard, buf, len, offset] = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { @@ -170,21 +153,25 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = CreateSharedPtr(move(bufGuard)); + auto arg = CreateSharedPtr(move(bufGuard)); if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { + auto cbExec = [arg, buf = buf, len = len, streamEntity, offset = offset]() -> NError { + if (!streamEntity || !streamEntity->fp.get()) { + HILOGE("Stream has been closed in write cbExec possibly"); + return NError(EIO); + } if (offset >= 0) { - int ret = fseek(filp, static_cast(offset), SEEK_SET); + int ret = fseek(streamEntity->fp.get(), 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); + arg->actLen = fwrite(buf, 1, len, streamEntity->fp.get()); if ((arg->actLen == 0) && (arg->actLen != len)) { HILOGE("Failed to fwrite stream"); return NError(EIO); @@ -210,7 +197,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) } } -napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) +napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { @@ -221,13 +208,15 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may have been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } - FILE *filp = nullptr; - filp = streamEntity->fp.get(); + return WriteExec(env, funcArg, streamEntity); +} +static napi_value ReadExec(napi_env env, NFuncArg &funcArg, StreamEntity *streamEntity) +{ auto [succ, buf, len, offset] = CommonFunc::GetReadArg(env, funcArg[NARG_POS::FIRST], funcArg[NARG_POS::SECOND]); if (!succ) { @@ -242,16 +231,20 @@ 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, offset = offset]() -> NError { + auto cbExec = [arg, buf = buf, len = len, streamEntity, offset = offset]() -> NError { + if (!streamEntity || !streamEntity->fp.get()) { + HILOGE("Stream has been closed in read cbExec possibly"); + return NError(EIO); + } if (offset >= 0) { - int ret = fseek(filp, static_cast(offset), SEEK_SET); + int ret = fseek(streamEntity->fp.get(), 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)) { + size_t actLen = fread(buf, 1, len, streamEntity->fp.get()); + if ((actLen != static_cast(len) && !feof(streamEntity->fp.get())) || ferror(streamEntity->fp.get())) { HILOGE("Invalid buffer size and pointer, actlen: %{public}zu", actLen); return NError(EIO); } else { @@ -278,6 +271,24 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) } } +napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) +{ + NFuncArg funcArg(env, cbInfo); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!streamEntity || !streamEntity->fp) { + HILOGE("Failed to get entity of Stream"); + NError(EIO).ThrowErr(env); + return nullptr; + } + return ReadExec(env, funcArg, streamEntity); +} + napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -289,7 +300,7 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!streamEntity || !streamEntity->fp) { - HILOGE("Stream may has been closed"); + HILOGE("Failed to get entity of Stream"); NError(EIO).ThrowErr(env); return nullptr; } 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 dc97b5ba2..c68c246ad 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 @@ -51,14 +51,14 @@ struct AsyncReadArg { ~AsyncReadArg() = default; }; -struct AsyncWrtieArg { +struct AsyncWriteArg { NRef refWriteArrayBuf; std::unique_ptr guardWriteStr = nullptr; size_t actLen = 0; - explicit AsyncWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {} - explicit AsyncWrtieArg(std::unique_ptr &&guardWriteStr) : guardWriteStr(std::move(guardWriteStr)) {} - ~AsyncWrtieArg() = default; + explicit AsyncWriteArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {} + explicit AsyncWriteArg(std::unique_ptr &&guardWriteStr) : guardWriteStr(std::move(guardWriteStr)) {} + ~AsyncWriteArg() = default; }; const std::string PROCEDURE_STREAM_WRITE_NAME = "FileIOStreamWrite"; 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 97ba9ff71..89003eccc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -71,21 +71,22 @@ static NError SendFileCore(FileInfo& srcFdg, FileInfo& destFdg, struct stat& sta int64_t offset = 0; size_t size = static_cast(statbf.st_size); int ret = 0; - while (size > MAX_SIZE) { + while (size > 0) { ret = uv_fs_sendfile(nullptr, sendfile_req.get(), destFdg.fdg->GetFD(), srcFdg.fdg->GetFD(), offset, MAX_SIZE, nullptr); if (ret < 0) { HILOGE("Failed to sendfile by ret : %{public}d", ret); return NError(ret); } - offset += MAX_SIZE; - size -= MAX_SIZE; + offset += static_cast(ret); + size -= static_cast(ret); + if (ret == 0) { + break; + } } - ret = uv_fs_sendfile(nullptr, sendfile_req.get(), destFdg.fdg->GetFD(), srcFdg.fdg->GetFD(), - offset, size, nullptr); - if (ret < 0) { - HILOGE("Failed to sendfile by ret : %{public}d", ret); - return NError(ret); + if (size != 0) { + HILOGE("The execution of the sendfile task was terminated, remaining file size %{public}zu", size); + return NError(EIO); } return NError(ERRNO_NOERR); } diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp old mode 100755 new mode 100644 index bab52a41a..e046e1745 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -266,7 +266,7 @@ static int FilterFileRes(const string &path, vector &dirents) HILOGE("Failed to request heap memory."); return ENOMEM; } - int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, nullptr); if (num < 0) { HILOGE("Failed to scan dir"); return errno; @@ -285,7 +285,7 @@ static int RecursiveFunc(const string &path, vector &dirents) HILOGE("Failed to request heap memory."); return ENOMEM; } - int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, alphasort); + int num = scandir(path.c_str(), &(pNameList->namelist), FilterFunc, nullptr); if (num < 0) { HILOGE("Failed to scan dir"); return errno; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 42f89e8d9..c3e97c0aa 100644 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -36,6 +36,18 @@ using namespace OHOS::FileManagement::LibN; static int RecurMoveDir(const string &srcPath, const string &destPath, const int mode, vector &errfiles); +static tuple JudgeExistAndEmpty(const string &path) +{ + filesystem::path pathName(path); + if (filesystem::exists(pathName)) { + if (filesystem::is_empty(pathName)) { + return { true, true }; + } + return { true, false }; + } + return { false, false }; +} + static int RmDirectory(const string &path) { filesystem::path pathName(path); @@ -223,8 +235,8 @@ static int MoveDirFunc(const string &src, const string &dest, const int mode, ve } string dirName = string(src).substr(found); string destStr = dest + dirName; - filesystem::path dstPath(destStr); - if (filesystem::exists(dstPath)) { + auto [destStrExist, destStrEmpty] = JudgeExistAndEmpty(destStr); + if (destStrExist && !destStrEmpty) { if (mode == DIRMODE_DIRECTORY_REPLACE) { int removeRes = RmDirectory(destStr); if (removeRes) { @@ -233,6 +245,7 @@ static int MoveDirFunc(const string &src, const string &dest, const int mode, ve } } if (mode == DIRMODE_DIRECTORY_THROW_ERR) { + HILOGE("Failed to move directory in DIRMODE_DIRECTORY_THROW_ERR"); return ENOTEMPTY; } } -- Gitee