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 11cdae1dc8c70fa34f5804158e6b4c575a94ac5e..c3c2253ed538b52a349e09fd8d5310e6f22c3440 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 @@ -16,11 +16,13 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_STREAM_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_STREAM_STREAM_ENTITY_H +#include "common_func.h" + namespace OHOS { namespace FileManagement { namespace ModuleFileIO { struct StreamEntity { - std::unique_ptr fp = { nullptr, fclose }; + std::unique_ptr fp = { nullptr, CommonFunc::StreamFclose }; }; } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 092a9523881d7011333a4b741dfcf144473920ed..9ac9bbfe80ecbaafe39af0237add393fc8bed31f 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -223,7 +223,7 @@ NVal CommonFunc::InstantiateFile(napi_env env, int fd, const string &pathOrUri, return { env, objFile }; } -NVal CommonFunc::InstantiateStream(napi_env env, unique_ptr fp) +NVal CommonFunc::InstantiateStream(napi_env env, unique_ptr fp) { napi_value objStream = NClass::InstantiateClass(env, StreamNExporter::className_, {}); if (!objStream) { @@ -253,6 +253,15 @@ void CommonFunc::fs_req_cleanup(uv_fs_t* req) } } +void CommonFunc::StreamFclose(FILE *stream) +{ + fclose(stream); + if (stream != nullptr) { + stream = nullptr; + HILOGD("Stream has been close."); + } +} + string CommonFunc::GetModeFromFlags(unsigned int flags) { const string readMode = "r"; diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 0b02b3340f502464a9097e7bb8ea7f5a28b8b06a..2ebfede1c6ffe69a74335c00292f5a252f914a10 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -64,9 +64,10 @@ struct CommonFunc { #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) static LibN::NVal InstantiateStat(napi_env env, const uv_stat_t &buf, std::shared_ptr fileInfo); #endif + static void StreamFclose(FILE *stream); #ifndef WIN_PLATFORM static LibN::NVal InstantiateFile(napi_env env, int fd, const std::string &pathOrUri, bool isUri); - static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); + static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); #endif static std::tuple GetReadArg(napi_env env, napi_value readBuf, 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 b2ac7d695ff4aa58eb5bd451e4b8857a34f01283..1dadff0a43a4d721aed4cee6244f96627d58e0cd 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -61,7 +61,8 @@ napi_value CreateStream::Sync(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; + unique_ptr fp = { + fopen(argPath.c_str(), argMode.c_str()), CommonFunc::StreamFclose }; if (!fp) { HILOGE("Failed to fdopen file by path"); NError(errno).ThrowErr(env); @@ -93,7 +94,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError { - arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; + arg->fp = { fopen(argPath.c_str(), argMode.c_str()), CommonFunc::StreamFclose }; if (!arg->fp) { HILOGE("Failed to fdopen file by path"); return NError(errno); 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 9e7b01493dc6ff432791cd841773072dbdf40522..345186161c9f4b44a8e1e3a98e066357c25ff2d5 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.h +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.h @@ -16,6 +16,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAM_H #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_CREATE_STREAM_H +#include "common_func.h" #include "filemgmt_libn.h" namespace OHOS { @@ -28,7 +29,7 @@ public: }; struct AsyncCreateStreamArg { - std::unique_ptr fp = { nullptr, fclose }; + std::unique_ptr fp = { nullptr, CommonFunc::StreamFclose }; }; 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..2aed3ac873803ac885b3ff1bea6b8db758734ec2 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -20,7 +20,6 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" -#include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" @@ -61,7 +60,7 @@ napi_value FdopenStream::Sync(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr fp = { fdopen(fd, mode.c_str()), fclose }; + unique_ptr fp = { fdopen(fd, mode.c_str()), CommonFunc::StreamFclose }; if (!fp) { HILOGE("Failed to fdopen file by fd:%{public}d", fd); NError(errno).ThrowErr(env); @@ -93,7 +92,7 @@ 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 }; + arg->fp = { fdopen(fd, mode.c_str()), CommonFunc::StreamFclose }; if (!arg->fp) { HILOGE("Failed to fdopen file by fd:%{public}d", fd); return NError(errno); 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..d3783d7a672df7eb11eb474f3c3c3a9198e7a6d8 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.h @@ -16,6 +16,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_H #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_FDOPEN_STREAM_H +#include "common_func.h" #include "filemgmt_libn.h" namespace OHOS { @@ -28,7 +29,7 @@ public: }; struct AsyncFdopenStreamArg { - std::unique_ptr fp = { nullptr, fclose }; + std::unique_ptr fp = { nullptr, CommonFunc::StreamFclose }; }; const std::string PROCEDURE_FDOPENSTREAM_NAME = "FileIOFdopenStream";