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 9e3c9ea0e57e9c3f9043e6d2ede3dc273e8763af..0fe49afade4fcb0e41d0eec0b23ab0087c768cec 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 a32c4d8fa91cc06c9d9fdb2eface93bb0416c580..2b7ec3508df3587c1cf12fce3efcfab66bf1b745 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 6d21dd570eea097df208504ab5805517874a794d..105eab7bae4f85d7b8704967f58955742d78fcd5 --- 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 6ecdf56b28d10cbbf66dab9af01ad6683b615bf2..bf11f36096fd4280b51e9c3c97054179dce56e9f 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 dc97b5ba2a5f56de539613e80b1089ec57b00c38..c68c246ad98fdef2a6d8169317b685d35cbe6e11 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 97ba9ff71705f1ea6b4489a8f5cb494efba95adf..89003eccc87c3def516588f5eafe01fda89b4b97 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 bab52a41ac0154dfc9e5c205f0c2f03efd999582..e046e174517d0d2fb836b0c6b712efc23adc7655 --- 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 42f89e8d9c06d6b5ef407671facee2ed787afd54..c3e97c0aa6205db07c94e019e181d1fe48523f13 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; } }