From bfaf53b0f559e76b693a6faa86c5da8a512f3325 Mon Sep 17 00:00:00 2001 From: wangluyao Date: Sun, 8 Sep 2024 18:44:09 +0800 Subject: [PATCH] =?UTF-8?q?createStream=E9=81=97=E7=95=99=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E9=97=AD=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangluyao --- interfaces/kits/js/BUILD.gn | 1 + .../kits/js/src/mod_fs/class_stream/flush.cpp | 113 ++++++++++++++++++ .../kits/js/src/mod_fs/class_stream/flush.h | 34 ++++++ .../src/mod_fs/class_stream/stream_entity.h | 2 +- .../mod_fs/class_stream/stream_n_exporter.cpp | 101 +++------------- .../mod_fs/class_stream/stream_n_exporter.h | 8 -- .../src/mod_fs/properties/create_stream.cpp | 4 +- .../js/src/mod_fs/properties/create_stream.h | 2 +- .../src/mod_fs/properties/fdopen_stream.cpp | 16 +-- .../js/src/mod_fs/properties/fdopen_stream.h | 3 +- 10 files changed, 178 insertions(+), 106 deletions(-) create mode 100755 interfaces/kits/js/src/mod_fs/class_stream/flush.cpp create mode 100755 interfaces/kits/js/src/mod_fs/class_stream/flush.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d1e895ed6..c98a406bb 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -214,6 +214,7 @@ ohos_shared_library("fs") { sources += [ "src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp", "src/mod_fs/class_readeriterator/readeriterator_n_exporter.cpp", + "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", "src/mod_fs/class_tasksignal/task_signal_entity.cpp", "src/mod_fs/class_tasksignal/task_signal_n_exporter.cpp", diff --git a/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp b/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp new file mode 100755 index 000000000..4f7f7f037 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_stream/flush.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "flush.h" + +#include +#include + +#include "class_stat/stat_entity.h" +#include "class_stat/stat_n_exporter.h" +#include "filemgmt_libhilog.h" +#include "stream_entity.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +static std::shared_ptr GetFilePtr(StreamEntity *streamEntity) +{ + if (streamEntity) { + return streamEntity->fp; + } + return nullptr; +} + +napi_value Flush::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + auto fp = GetFilePtr(streamEntity); + if (fp == nullptr) { + HILOGE("Failed to get entity of Stream"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + int ret = fflush(fp.get()); + if (ret < 0) { + HILOGE("Failed to fflush file in the stream, ret: %{public}d", ret); + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value Flush::Async(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + auto fp = GetFilePtr(streamEntity); + if (fp == nullptr) { + HILOGE("Failed to get entity of Stream"); + NError(EIO).ThrowErr(env); + return nullptr; + } + + auto cbExec = [fp]() -> NError { + if (!fp) { + HILOGE("Stream has been closed in flush cbExec possibly"); + return NError(EIO); + } + int ret = fflush(fp.get()); + if (ret < 0) { + HILOGE("Failed to fflush file in the stream"); + return NError(errno); + } else { + return NError(ERRNO_NOERR); + } + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_; + } +} + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_stream/flush.h b/interfaces/kits/js/src/mod_fs/class_stream/flush.h new file mode 100755 index 000000000..aa7be7012 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_stream/flush.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_FLUSH_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_FLUSH_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class Flush final { +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_STREAM_FLUSH_NAME = "FileIOStreamFlush"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_FLUSH_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_entity.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_entity.h index dc048fac1..e42c21e08 100755 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_entity.h @@ -21,7 +21,7 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { struct StreamEntity { - std::shared_ptr fp{ nullptr}; + std::shared_ptr fp{ nullptr }; }; } // namespace ModuleFileIO 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 a9bc267a7..6e161d3e6 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 @@ -27,6 +27,7 @@ #include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" +#include "flush.h" #include "rust_file.h" #include "stream_entity.h" @@ -35,88 +36,13 @@ namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -std::mutex StreamNExporter::mutex; -std::shared_ptr StreamNExporter::GetFilePtr(StreamEntity *streamEntity) +static std::shared_ptr GetFilePtr(StreamEntity *streamEntity) { - std::lock_guard lock(mutex); if (streamEntity) { return streamEntity->fp; } return nullptr; } - -napi_value StreamNExporter::FlushSync(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - auto fp = GetFilePtr(streamEntity); - if (fp == nullptr) { - HILOGE("Failed to get entity of Stream"); - NError(EIO).ThrowErr(env); - return nullptr; - } - - int ret = fflush(fp.get()); - if (ret < 0) { - HILOGE("Failed to fflush file in the stream, ret: %{public}d", ret); - NError(errno).ThrowErr(env); - return nullptr; - } - return NVal::CreateUndefined(env).val_; -} - -napi_value StreamNExporter::Flush(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - HILOGE("Number of arguments unmatched"); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); - auto fp = GetFilePtr(streamEntity); - if (fp == nullptr) { - HILOGE("Failed to get entity of Stream"); - NError(EIO).ThrowErr(env); - return nullptr; - } - - auto cbExec = [fp]() -> NError { - if (!fp) { - HILOGE("Stream has been closed in flush cbExec possibly"); - return NError(EIO); - } - int ret = fflush(fp.get()); - if (ret < 0) { - HILOGE("Failed to fflush file in the stream"); - return NError(errno); - } else { - return NError(ERRNO_NOERR); - } - }; - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return { env, err.GetNapiErr(env) }; - } - return { NVal::CreateUndefined(env) }; - }; - - NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_STREAM_FLUSH_NAME, cbExec, cbCompl).val_; - } -} - napi_value StreamNExporter::ReadSync(napi_env env, napi_callback_info cbInfo) { NFuncArg funcArg(env, cbInfo); @@ -169,10 +95,13 @@ napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info cbInfo) NError(EINVAL).ThrowErr(env); return nullptr; } - { - std::lock_guard lock(mutex); - (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!streamEntity) { + HILOGE("Failed to get entity of Stream, may closed twice"); + NError(EIO).ThrowErr(env); + return nullptr; } + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); return NVal::CreateUndefined(env).val_; } @@ -371,11 +300,13 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info cbInfo) NError(EINVAL).ThrowErr(env); return nullptr; } - StreamEntity* ret = nullptr; - { - std::lock_guard lock(mutex); - ret = NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + auto streamEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!streamEntity) { + HILOGE("Failed to get entity of Stream, may closed twice"); + NError(EIO).ThrowErr(env); + return nullptr; } + StreamEntity* ret = NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); if (!ret) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -480,8 +411,8 @@ bool StreamNExporter::Export() { vector props = { NVal::DeclareNapiFunction("writeSync", WriteSync), - NVal::DeclareNapiFunction("flush", Flush), - NVal::DeclareNapiFunction("flushSync", FlushSync), + NVal::DeclareNapiFunction("flush", Flush::Sync), + NVal::DeclareNapiFunction("flushSync", Flush::Async), NVal::DeclareNapiFunction("readSync", ReadSync), NVal::DeclareNapiFunction("closeSync", CloseSync), NVal::DeclareNapiFunction("write", Write), 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 8b2a05bc7..e36f9ed18 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 @@ -18,7 +18,6 @@ #include "filemgmt_libn.h" -#include #include "stream_entity.h" namespace OHOS { namespace FileManagement { @@ -26,8 +25,6 @@ namespace ModuleFileIO { using namespace OHOS::FileManagement::LibN; class StreamNExporter final : public NExporter { public: - static std::mutex mutex; - inline static const std::string className_ = "FsStream"; bool Export() override; @@ -38,15 +35,11 @@ public: 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 FlushSync(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 Seek(napi_env env, napi_callback_info cbInfo); - static napi_value Flush(napi_env env, napi_callback_info cbInfo); - - static std::shared_ptr GetFilePtr(StreamEntity *streamEntity); StreamNExporter(napi_env env, napi_value exports); ~StreamNExporter() override; @@ -73,7 +66,6 @@ struct AsyncWriteArg { const std::string PROCEDURE_STREAM_WRITE_NAME = "FileIOStreamWrite"; const std::string PROCEDURE_STREAM_READ_NAME = "FileIOStreamRead"; const std::string PROCEDURE_STREAM_CLOSE_NAME = "FileIOStreamClose"; -const std::string PROCEDURE_STREAM_FLUSH_NAME = "FileIOStreamFlush"; } // namespace ModuleFileIO } // namespace FileManagement 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 06b3b127a..7ace8cc18 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -67,7 +67,7 @@ napi_value CreateStream::Sync(napi_env env, napi_callback_info info) return nullptr; } std::shared_ptr fp(file, fclose); - return CommonFunc::InstantiateStream(env, fp).val_; + return CommonFunc::InstantiateStream(env, move(fp)).val_; } napi_value CreateStream::Async(napi_env env, napi_callback_info info) @@ -105,7 +105,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - return CommonFunc::InstantiateStream(env, arg->fp); + return CommonFunc::InstantiateStream(env, move(arg->fp)); }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.h b/interfaces/kits/js/src/mod_fs/properties/create_stream.h index 66965860f..1e9d7e684 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.h +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.h @@ -28,7 +28,7 @@ public: }; struct AsyncCreateStreamArg { - std::shared_ptr fp; + std::shared_ptr fp{ nullptr }; }; const std::string PROCEDURE_CREATESTREAM_NAME = "FileIOCreateStream"; 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 4029464ed..1d6ababb4 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -60,14 +60,13 @@ napi_value FdopenStream::Sync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - unique_ptr fp = { fdopen(fd, mode.c_str()), fclose }; - if (!fp) { - HILOGE("Failed to fdopen file by fd:%{public}d", fd); + FILE *file = fdopen(fd, mode.c_str()); + if (!file) { + HILOGE("Failed to fdopen file by path"); NError(errno).ThrowErr(env); return nullptr; } - + std::shared_ptr fp(file, fclose); return CommonFunc::InstantiateStream(env, move(fp)).val_; } @@ -93,11 +92,12 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [arg, fd = fd, mode = mode]() -> NError { - arg->fp = { fdopen(fd, mode.c_str()), fclose }; - if (!arg->fp) { - HILOGE("Failed to fdopen file by fd:%{public}d", fd); + FILE *file = fdopen(fd, mode.c_str()); + if (!file) { + HILOGE("Failed to fdopen file by path"); return NError(errno); } + arg->fp = std::shared_ptr(file, fclose); return NError(ERRNO_NOERR); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h index ed43f3d3b..4aaa92fda 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h @@ -17,6 +17,7 @@ #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_H #include "filemgmt_libn.h" +#include namespace OHOS { namespace FileManagement { @@ -28,7 +29,7 @@ public: }; struct AsyncFdopenStreamArg { - std::unique_ptr fp = { nullptr, fclose }; + std::shared_ptr fp{ nullptr }; }; const std::string PROCEDURE_FDOPENSTREAM_NAME = "FileIOFdopenStream"; -- Gitee