diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d1e895ed637accc50e96235fe86dc2088be301ba..c98a406bb5488209c9abbf2ab2f5b24a0c018fc3 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 0000000000000000000000000000000000000000..4f7f7f037347be39d071876de55a6c747c4ac4a2 --- /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 0000000000000000000000000000000000000000..aa7be7012cdc5ecf146f7b23bd6006e1c8d0ed43 --- /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 dc048fac11418064fed9336cbbbf67bd9f0527a6..e42c21e087548d9f24c893da868cda130585b139 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 a9bc267a79d8c7a8f2bf2b4fe426301cca6f9dbd..6e161d3e6917eb8061f17565b4cd06fa4aaafe4d 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 8b2a05bc78a4a585abd75c873de3e8346cebcf93..e36f9ed1801e96fca784eea41b8a8f0780c76dea 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 06b3b127a98a639d59aa7364547a3b844563e18a..7ace8cc18f18203b3a9d5608a0996b230ba38187 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 66965860fb84aa3bdac2727219e23fc6715daefd..1e9d7e6843f484461b2694322392fddec7e653d1 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 4029464edb7c624e33ddab79d506628f7671b2bb..1d6ababb4050afb4343169edb8255c62835e250d 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 ed43f3d3bd8b4a71499dfdaadbb997d42e7488f2..4aaa92fda121660ec5dac6fe4f95ebefc12d6621 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";