From 61c8a069297db217c1ae752e0b7c8d4ef007552c Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Fri, 3 Feb 2023 17:52:59 +0800 Subject: [PATCH 01/21] add watcher Signed-off-by: Cao Chuan --- interfaces/kits/js/BUILD.gn | 5 +- interfaces/kits/js/src/mod_fileio/module.cpp | 4 +- .../mod_fileio/properties/prop_n_exporter.cpp | 6 +- .../src/mod_fs/class_watcher/file_watcher.cpp | 100 +++++ .../src/mod_fs/class_watcher/file_watcher.h | 43 ++ .../class_watcher/watcher_entity.h | 40 +- .../class_watcher/watcher_n_exporter.cpp | 118 +++--- .../class_watcher/watcher_n_exporter.h | 38 +- interfaces/kits/js/src/mod_fs/module.cpp | 4 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 5 +- .../properties/watcher.cpp | 77 ++-- .../properties/watcher.h | 15 +- interfaces/test/unittest/BUILD.gn | 5 +- .../test/unittest/file_watcher/BUILD.gn | 43 ++ .../file_watcher/file_watcher_test.cpp | 380 ++++++++++++++++++ utils/filemgmt_libn/include/n_val.h | 3 + utils/filemgmt_libn/src/n_val.cpp | 30 ++ 17 files changed, 757 insertions(+), 159 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp create mode 100644 interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_watcher/watcher_entity.h (55%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_watcher/watcher_n_exporter.cpp (41%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/class_watcher/watcher_n_exporter.h (60%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/properties/watcher.cpp (43%) rename interfaces/kits/js/src/{mod_fileio => mod_fs}/properties/watcher.h (73%) create mode 100644 interfaces/test/unittest/file_watcher/BUILD.gn create mode 100644 interfaces/test/unittest/file_watcher/file_watcher_test.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 90795c6f8..37be21979 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -52,7 +52,6 @@ ohos_shared_library("fileio") { "src/mod_fileio/class_stat/stat_n_exporter.cpp", "src/mod_fileio/class_stream/flush.cpp", "src/mod_fileio/class_stream/stream_n_exporter.cpp", - "src/mod_fileio/class_watcher/watcher_n_exporter.cpp", "src/mod_fileio/common_func.cpp", "src/mod_fileio/module.cpp", "src/mod_fileio/properties/chmod.cpp", @@ -86,7 +85,6 @@ ohos_shared_library("fileio") { "src/mod_fileio/properties/stat.cpp", "src/mod_fileio/properties/symlink.cpp", "src/mod_fileio/properties/truncate.cpp", - "src/mod_fileio/properties/watcher.cpp", ] deps = [ @@ -119,6 +117,8 @@ ohos_shared_library("fs") { "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", + "src/mod_fs/class_watcher/file_watcher.cpp", + "src/mod_fs/class_watcher/watcher_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", "src/mod_fs/properties/close.cpp", @@ -137,6 +137,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/symlink.cpp", "src/mod_fs/properties/truncate.cpp", + "src/mod_fs/properties/watcher.cpp", ] deps = [ diff --git a/interfaces/kits/js/src/mod_fileio/module.cpp b/interfaces/kits/js/src/mod_fileio/module.cpp index b26592acb..0a0fcca0f 100644 --- a/interfaces/kits/js/src/mod_fileio/module.cpp +++ b/interfaces/kits/js/src/mod_fileio/module.cpp @@ -23,7 +23,6 @@ #include "class_randomaccessfile/randomaccessfile_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" -#include "class_watcher/watcher_n_exporter.h" #include "properties/prop_n_exporter.h" using namespace std; @@ -40,7 +39,6 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); - products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); for (auto &&product : products) { @@ -57,4 +55,4 @@ static napi_value Export(napi_env env, napi_value exports) NAPI_MODULE(fileio, Export) } // namespace ModuleFileIO } // namespace DistributedFS -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp index a7a1acbac..1c5fcdb81 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp @@ -20,7 +20,8 @@ #include #include #include - +#include +#include #include "../common_func.h" #include "chmod.h" #include "chown.h" @@ -52,7 +53,7 @@ #include "stat.h" #include "symlink.h" #include "truncate.h" -#include "watcher.h" + namespace OHOS { namespace DistributedFS { @@ -687,7 +688,6 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), NVal::DeclareNapiFunction("createStream", CreateStream::Async), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), - NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), NVal::DeclareNapiFunction("fchmod", Fchmod::Async), NVal::DeclareNapiFunction("fchmodSync", Fchmod::Sync), NVal::DeclareNapiFunction("fchown", Fchown::Async), diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp new file mode 100644 index 000000000..d6b0f8f69 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 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 "file_watcher.h" +#include +#include +#include "filemgmt_libhilog.h" +#include "uv.h" +namespace OHOS::FileManagement::ModuleFileIO { +using namespace OHOS::FileManagement::LibN; +const int BUF_SIZE = 1024; +FileWatcher::FileWatcher() {} + +FileWatcher::~FileWatcher() {} + +bool FileWatcher::InitNotify(int &fd) +{ + fd = inotify_init(); + if (fd == -1) { + HILOGE("FileWatcher InitNotify fail errCode:%{public}d", errno); + return false; + } + return true; +} + +bool FileWatcher::StartNotify(std::shared_ptr &arg) +{ + int wd = 0; + uint32_t watchEvent = 0; + for (auto event : arg->events) { + watchEvent = watchEvent | event; + } + wd = inotify_add_watch(arg->fd, arg->filename.c_str(), watchEvent); + if(wd == -1) { + HILOGE("FileWatcher StartNotify fail errCode:%{public}d", errno); + return false; + } + arg->wd = wd; + run_ = true; + return true; +} + +bool FileWatcher::StopNotify(std::shared_ptr &arg) +{ + run_ = false; + if (inotify_rm_watch(arg->fd, arg->wd) == -1) { + HILOGE("FileWatcher StopNotify fail errCode:%{public}d", errno); + return false; + } + close(arg->fd); + return true; +} + +void FileWatcher::HandleEvent(std::shared_ptr &arg, + const struct inotify_event *event, + WatcherCallback callback) +{ + if (event->wd == arg->wd) { + for (auto watchEvent : arg->events) { + if (event->mask == watchEvent || watchEvent == IN_ALL_EVENTS) { + std::string filename = arg->filename + "/" + event->name; + HILOGI("FileWatcher HandleEvent fileName:%{public}s, mask:%{public}d", filename.c_str(), event->mask); + callback(arg->env, arg->ref, filename, event->mask); + } + } + } +} + +void FileWatcher::GetNotifyEvent(std::shared_ptr &arg, WatcherCallback callback) +{ + char buf[BUF_SIZE] = {0}; + struct inotify_event *event = nullptr; + while (run_) { + int fd = arg->fd; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + if (select(fd + 1, &fds, NULL, NULL, NULL) > 0) { + int len, index = 0; + while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)); + while (index < len) { + event = (struct inotify_event *)(buf + index); + HandleEvent(arg, event, callback); + index += sizeof(struct inotify_event) + event->len; + } + } + } +} +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h new file mode 100644 index 000000000..ae11351ba --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 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 FILE_WATCHER_H +#define FILE_WATCHER_H +#include +#include +#include +#include "watcher_entity.h" +namespace OHOS::FileManagement::ModuleFileIO { +using WatcherCallback = void (*)(napi_env env, napi_ref callback, const std::string &filename, const uint32_t &event); +struct ErrorInfo { + int errCode; + std::string errMsg; +}; + +class FileWatcher { +public: + FileWatcher(); + ~FileWatcher(); + bool InitNotify(int &fd); + bool StartNotify(std::shared_ptr &arg); + bool StopNotify(std::shared_ptr &arg); + void GetNotifyEvent(std::shared_ptr &arg, WatcherCallback callback); + +private: + void HandleEvent(std::shared_ptr &arg, const struct inotify_event *event, WatcherCallback callback); + bool run_; +}; +} // namespace OHOS::FileManagement::ModuleFileIO +#endif diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h similarity index 55% rename from interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h rename to interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index ce604ee6b..e11891195 100644 --- a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -15,39 +15,23 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H +#include +#include +#include +#include "filemgmt_libn.h" +namespace OHOS::FileManagement::ModuleFileIO { -#include - -#include "../../common/napi/uni_header.h" - -namespace OHOS { -namespace DistributedFS { -namespace ModuleFileIO { -class WatcherHandleDeleter { -public: - void operator()(uv_fs_event_t *ptr) - { - if (ptr == nullptr) { - return; - } - - uv_fs_event_stop(ptr); - uv_handle_t *handle = reinterpret_cast(ptr); - uv_close(handle, [](uv_handle_t *handle) { delete handle; }); - } -}; - -struct WatcherInforArg { - int events = 0; +struct WatcherInfoArg { + std::string filename; + std::vector events; + int fd; + int wd; napi_env env = nullptr; napi_ref ref = nullptr; }; struct WatcherEntity { - std::unique_ptr data_; - std::unique_ptr fsEventReq_; + std::shared_ptr data_; }; -} // namespace ModuleFileIO -} // namespace DistributedFS -} // namespace OHOS +} // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS #endif diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp similarity index 41% rename from interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp rename to interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 2947b10cb..042bfee9a 100644 --- a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -19,112 +19,142 @@ #include #include #include - -#include "../../common/log.h" -#include "../../common/napi/n_async/n_async_work_callback.h" -#include "../../common/napi/n_async/n_async_work_promise.h" -#include "../../common/napi/n_class.h" -#include "../../common/napi/n_func_arg.h" -#include "../../common/uni_error.h" +#include "filemgmt_libn.h" +#include "filemgmt_libhilog.h" +#include "../common_func.h" #include "securec.h" -#include "watcher_entity.h" -namespace OHOS { -namespace DistributedFS { -namespace ModuleFileIO { +namespace OHOS::FileManagement::ModuleFileIO { using namespace std; +using namespace OHOS::FileManagement::LibN; +std::shared_ptr WatcherNExporter::watcherPtr_; napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } unique_ptr watcherEntity = make_unique(); if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { - UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat"); + NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat"); return nullptr; } + watcherPtr_ = make_shared(); return funcArg.GetThisVar(); } -napi_value WatcherNExporter::StopSync(napi_env env, napi_callback_info info) +napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!watchEntity) { - UniError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } - - watchEntity->fsEventReq_.reset(); + if (!watcherPtr_->StopNotify(watchEntity->data_)) { + NError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; } -napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) +napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!watchEntity) { - UniError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } + if(!watcherPtr_->StartNotify(watchEntity->data_)) { + NError(errno).ThrowErr(env); + return nullptr; + } - auto cbExec = [watchEntity](napi_env env) -> UniError { - watchEntity->fsEventReq_.reset(); - return UniError(ERRNO_NOERR); + auto cbExec = [watchEntity]() -> NError { + watcherPtr_->GetNotifyEvent(watchEntity->data_, WatcherCallback); + return NError(ERRNO_NOERR); }; - auto cbCompl = [](napi_env env, UniError err) -> NVal { + auto cbCompl = [](napi_env env, NError err) -> NVal { if (err) { - return { env, err.GetNapiErr(env) }; + return {env, err.GetNapiErr(env)}; } - return { NVal::CreateUndefined(env) }; + return {NVal::CreateUndefined(env)}; }; - const string procedureName = "FileIOCreaterWatcher"; + const string procedureName = "FileIOStartWatcher"; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ZERO) { - return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; - } else { - NVal cb(env, funcArg[NARG_POS::FIRST]); - return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; - } + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; +} + +void WatcherNExporter::WatcherCallback(napi_env env, + napi_ref callback, + const std::string &fileName, + const uint32_t &event) +{ + auto callbackContext = make_unique(); + callbackContext->env_ = env; + callbackContext->ref_ = callback; + callbackContext->fileName_ = fileName; + callbackContext->event_ = event; + napi_value resource = nullptr; + napi_create_string_utf8(env, "FileIOCreateWatcher", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, [](napi_env env, void *data) {}, + [](napi_env env, napi_status status, void *data) { + // JSCallbackData* jsCallbackData = (JSCallbackData*)data; + auto context = static_cast(data); + napi_value jsCallback = nullptr; + napi_get_reference_value(env, context->ref_, &jsCallback); + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, context->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, context->event_).val_); + napi_value global = nullptr; + napi_get_global(env, &global); + napi_value jsObj = nullptr; + napi_call_function(env, global, jsCallback, 1, &(objn.val_), &jsObj); + napi_delete_async_work(env, context->work_); + }, + static_cast(callbackContext.get()), &callbackContext->work_); + napi_queue_async_work(env, callbackContext->work_); + callbackContext.release(); } bool WatcherNExporter::Export() { vector props = { + NVal::DeclareNapiFunction("start", Start), NVal::DeclareNapiFunction("stop", Stop), - NVal::DeclareNapiFunction("stopSync", StopSync), }; string className = GetClassName(); - auto [resDefineClass, classValue] = NClass::DefineClass(exports_.env_, - className, - WatcherNExporter::Constructor, - std::move(props)); + auto [resDefineClass, classValue] = + NClass::DefineClass(exports_.env_, className, WatcherNExporter::Constructor, std::move(props)); if (!resDefineClass) { - UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class"); return false; } bool succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class"); + NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class"); return false; } @@ -139,6 +169,4 @@ string WatcherNExporter::GetClassName() WatcherNExporter::WatcherNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} WatcherNExporter::~WatcherNExporter() {} -} // namespace ModuleFileIO -} // namespace DistributedFS -} // namespace OHOS \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h similarity index 60% rename from interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h rename to interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index 3d3502b7b..b4e053386 100644 --- a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -15,27 +15,43 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H - -#include "../../common/napi/n_exporter.h" - -namespace OHOS { -namespace DistributedFS { -namespace ModuleFileIO { +#include +#include +#include "watcher_entity.h" +#include "file_watcher.h" +#include "filemgmt_libn.h" +namespace OHOS::FileManagement::ModuleFileIO { +using namespace OHOS::FileManagement::LibN; class WatcherNExporter final : public NExporter { public: + class JSCallbackContext { + public: + JSCallbackContext() {} + ~JSCallbackContext() {} + + public: + napi_env env_; + napi_ref ref_; + std::string fileName_; + uint32_t event_; + napi_async_work work_; + }; + inline static const std::string className_ = "Watcher"; bool Export() override; std::string GetClassName() override; static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Start(napi_env env, napi_callback_info info); static napi_value Stop(napi_env env, napi_callback_info info); - static napi_value StopSync(napi_env env, napi_callback_info info); + static void WatcherCallback(napi_env env, napi_ref callback, const std::string &fileName, const uint32_t &event); WatcherNExporter(napi_env env, napi_value exports); ~WatcherNExporter() override; + +private: + static std::shared_ptr watcherPtr_; }; -} // namespace ModuleFileIO -} // namespace DistributedFS -} // namespace OHOS -#endif +} // namespace OHOS::FileManagement::ModuleFileIO +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 6d2ae250b..4b39bd129 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -21,6 +21,7 @@ #include "class_file/file_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" +#include "class_watcher/watcher_n_exporter.h" #include "filemgmt_libhilog.h" #include "properties/prop_n_exporter.h" @@ -37,6 +38,7 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); for (auto &&product : products) { if (!product->Export()) { @@ -52,4 +54,4 @@ static napi_value Export(napi_env env, napi_value exports) NAPI_MODULE(fs, Export) } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index f20fe97f7..c6da56bc7 100755 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -43,7 +43,7 @@ #include "stat.h" #include "symlink.h" #include "truncate.h" - +#include "watcher.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { @@ -609,6 +609,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), + NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), }); } @@ -622,4 +623,4 @@ PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, PropNExporter::~PropNExporter() {} } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp similarity index 43% rename from interfaces/kits/js/src/mod_fileio/properties/watcher.cpp rename to interfaces/kits/js/src/mod_fs/properties/watcher.cpp index b822f9a3c..0115353b5 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -14,93 +14,68 @@ */ #include "watcher.h" - #include #include +#include #include #include - -#include "../../common/napi/n_async/n_ref.h" -#include "../../common/napi/n_class.h" -#include "../../common/napi/n_func_arg.h" -#include "../../common/napi/n_val.h" -#include "../../common/uni_error.h" - +#include "filemgmt_libhilog.h" +#include "../class_watcher/file_watcher.h" #include "../class_watcher/watcher_entity.h" #include "../class_watcher/watcher_n_exporter.h" -namespace OHOS { -namespace DistributedFS { -namespace ModuleFileIO { +namespace OHOS::FileManagement::ModuleFileIO { using namespace std; - -void Watcher::RunCommand(uv_fs_event_t *handle, const char *filename, int events, int status) -{ - WatcherInforArg *information = (WatcherInforArg *)handle->data; - uint32_t eventsFirst { events }; - uint32_t eventsSecond { information->events }; - if (eventsFirst & eventsSecond) { - napi_handle_scope scope = nullptr; - napi_open_handle_scope(information->env, &scope); - napi_value callback = nullptr; - napi_get_reference_value(information->env, information->ref, &callback); - vector argv; - argv.push_back(NVal::CreateInt64(information->env, events).val_); - napi_value global = nullptr; - napi_get_global(information->env, &global); - napi_value tmp = nullptr; - napi_call_function(information->env, global, callback, argv.size(), argv.data(), &tmp); - } -} - +using namespace OHOS::FileManagement::LibN; napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); return nullptr; } auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succGetPath) { - UniError(EINVAL).ThrowErr(env, "Invalid filename"); + NError(EINVAL).ThrowErr(env, "Invalid filename"); return nullptr; } - auto [succGetEvent, event] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + auto [succGetEvent, events, size] = NVal(env, funcArg[NARG_POS::SECOND]).ToUint32Array(); if (!succGetEvent) { - UniError(EINVAL).ThrowErr(env, "Invalid event"); + NError(EINVAL).ThrowErr(env, "Invalid event"); return nullptr; } - unique_ptr data = make_unique(); - data->events = event; + int fd = 0; + shared_ptr watcherPtr = make_shared(); + if (!watcherPtr->InitNotify(fd)) { + NError(errno).ThrowErr(env); + return nullptr; + } + + shared_ptr data = make_shared(); + data->events = events; data->env = env; + data->filename = string(filename.get()); + data->fd = fd; + NVal val = NVal(env, funcArg[NARG_POS::THIRD]); napi_create_reference(val.env_, val.val_, 1, &(data->ref)); - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env, &loop); - unique_ptr fsEventReq(new uv_fs_event_t); - uv_fs_event_init(loop, fsEventReq.get()); - fsEventReq->data = data.get(); - uv_fs_event_start(fsEventReq.get(), RunCommand, filename.get(), UV_FS_EVENT_RECURSIVE); + napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); if (!objWatcher) { - UniError(EINVAL).ThrowErr(env, "objWatcher create failed"); + NError(EINVAL).ThrowErr(env, "objWatcher create failed"); return nullptr; } auto watcherEntity = NClass::GetEntityOf(env, objWatcher); if (!watcherEntity) { - UniError(EINVAL).ThrowErr(env, "watcherEntity get failed"); + NError(EINVAL).ThrowErr(env, "watcherEntity get failed"); return nullptr; } - - watcherEntity->fsEventReq_ = std::move(fsEventReq); - watcherEntity->data_ = std::move(data); + watcherEntity->data_ = data; return objWatcher; } -} // namespace ModuleFileIO -} // namespace DistributedFS -} // namespace OHOS \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fileio/properties/watcher.h b/interfaces/kits/js/src/mod_fs/properties/watcher.h similarity index 73% rename from interfaces/kits/js/src/mod_fileio/properties/watcher.h rename to interfaces/kits/js/src/mod_fs/properties/watcher.h index 45c100cb2..22059672c 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/watcher.h +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.h @@ -15,21 +15,12 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_WATCHER_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_WATCHER_H +#include "filemgmt_libn.h" +namespace OHOS::FileManagement::ModuleFileIO { -#include -#include "../../common/napi/n_val.h" - -namespace OHOS { -namespace DistributedFS { -namespace ModuleFileIO { class Watcher final { public: static napi_value CreateWatcher(napi_env env, napi_callback_info info); - -private: - static void RunCommand(uv_fs_event_t *handle, const char *filename, int events, int status); }; -} // namespace ModuleFileIO -} // namespace DistributedFS -} // namespace OHOS +} // namespace OHOS::FileManagement::ModuleFileIO #endif \ No newline at end of file diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 4351bd3fe..d74288bc7 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -13,5 +13,8 @@ group("unittest") { testonly = true - deps = [ "remote_uri:remote_uri_test" ] + deps = [ + "remote_uri:remote_uri_test", + "file_watcher:file_watcher_test" + ] } diff --git a/interfaces/test/unittest/file_watcher/BUILD.gn b/interfaces/test/unittest/file_watcher/BUILD.gn new file mode 100644 index 000000000..fdfc92061 --- /dev/null +++ b/interfaces/test/unittest/file_watcher/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2022 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. + +import("//build/test.gni") + +ohos_unittest("file_watcher_test") { + module_out_path = "filemanagement/file_api" + + resource_config_file = "../resource/ohos_test.xml" + + sources = [ "file_watcher_test.cpp" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "//commonlibrary/c_utils/base/include", + "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi", + "//foundation/filemanagement/file_api/interfaces/kits/js/src/mod_fileio/class_watcher", + "//third_party/libuv/include", + ] + + deps = [ + "//foundation/filemanagement/file_api/interfaces/kits/js:fileio", + "//third_party/googletest:gtest_main", + "//foundation/arkui/napi:ace_napi", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "ipc:ipc_core", + ] +} diff --git a/interfaces/test/unittest/file_watcher/file_watcher_test.cpp b/interfaces/test/unittest/file_watcher/file_watcher_test.cpp new file mode 100644 index 000000000..0a1db2167 --- /dev/null +++ b/interfaces/test/unittest/file_watcher/file_watcher_test.cpp @@ -0,0 +1,380 @@ +/* +* Copyright (c) 2022 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 "file_watcher.h" +#include +#include +#include +#include +#include +#include +#include +#include +namespace { + using namespace std; + using namespace OHOS::DistributedFS::ModuleFileIO; + + class FileWatcherTest : public testing::Test { + public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(){}; + void SetUp(){}; + void TearDown(){}; + }; + + + void WatcherCallback(napi_env env, napi_ref callback, const std::string &filename, const uint32_t &event) + { + string path = "/data/data/test2"; + EXPECT_STREQ(path.c_str(), filename.c_str()); + if (event == IN_CLOSE || event == IN_DELETE) { + EXPECT_TRUE(true); + } else { + EXPECT_TRUE(false); + } + } + + void GetEvent(shared_ptr &fileWatcherPtr, std::shared_ptr &arg) + { + fileWatcherPtr->GetNotifyEvent(arg, WatcherCallback); + } + + /** + * @tc.name: Watcher_InitNotify_0000 + * @tc.desc: Test function of InitNotify() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_InitNotify_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_InitNotify_0000"; + int fd = 0; + int error = 0; + + shared_ptr fileWatcherPtr = make_shared(); + bool ret = fileWatcherPtr->InitNotify(fd, error); + + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(error == 0); + EXPECT_TRUE(ret == true); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_InitNotify_0000"; + } + + /** + * @tc.name: Watcher_StartNotify_0000 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0000"; + int error = 0; + std::shared_ptr arg = make_shared(); + shared_ptr fileWatcherPtr = make_shared(); + bool ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0000"; + } + + /** + * @tc.name: Watcher_StartNotify_0001 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0001, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0001"; + int error = 0; + std::shared_ptr arg = make_shared(); + arg->fd = 10; + + shared_ptr fileWatcherPtr = make_shared(); + bool ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0001"; + } + + /** + * @tc.name: Watcher_StartNotify_0002 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0002, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0002"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = ""; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0002"; + } + + /** + * @tc.name: Watcher_StartNotify_0003 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0003, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0003"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = "/data/data"; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0003"; + } + + /** + * @tc.name: Watcher_StartNotify_0004 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0004, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0004"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::vector events; + events.push_back(0); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = "/data/data"; + arg->events = events; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0004"; + } + + /** + * @tc.name: Watcher_StartNotify_0005 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0005, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0005"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::vector events; + events.push_back(IN_ACCESS); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = "/data/test"; + arg->events = events; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0005"; + } + + /** + * @tc.name: Watcher_StartNotify_0006 + * @tc.desc: Test function of StartNotify() interface for FAIL. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0006, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0006"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::vector events; + events.push_back(IN_ACCESS); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = "/data/test"; + arg->events = events; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == false); + EXPECT_TRUE(error > 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0006"; + } + + /** + * @tc.name: Watcher_StartNotify_0007 + * @tc.desc: Test function of StartNotify() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_StartNotify_0007, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0007"; + int fd = 0; + int error = 0; + bool ret = false; + string filename = "/data/data/test"; + int fileFd = open(filename.c_str(), O_CREAT); + EXPECT_TRUE(fileFd > 0); + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + std::vector events; + events.push_back(IN_ACCESS); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = filename; + arg->events = events; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(ret == true); + EXPECT_TRUE(error == 0); + + ret = fileWatcherPtr->StopNotify(arg, error); + EXPECT_TRUE(ret == true); + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0007"; + } + + /** + * @tc.name: Watcher_GetNotifyEvent_0000 + * @tc.desc: Test function of GetNotifyEvent() interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000HG8M4 + */ + HWTEST_F(FileWatcherTest, Watcher_GetNotifyEvent_0000, testing::ext::TestSize.Level1) + { + GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_GetNotifyEvent_0000"; + int fd = 0; + int error = 0; + bool ret = false; + + shared_ptr fileWatcherPtr = make_shared(); + ret = fileWatcherPtr->InitNotify(fd, error); + EXPECT_TRUE(fd > 0); + EXPECT_TRUE(ret == true); + + string filename = "/data/data/test2"; + int fileFd = open(filename.c_str(), O_CREAT); + EXPECT_TRUE(fileFd > 0); + + std::vector events; + events.push_back(IN_CLOSE); + events.push_back(IN_DELETE); + + std::shared_ptr arg = make_shared(); + arg->fd = fd; + arg->filename = filename; + arg->events = events; + + ret = fileWatcherPtr->StartNotify(arg, error); + EXPECT_TRUE(error == 0); + EXPECT_TRUE(ret == true); + + thread t(GetEvent, ref(fileWatcherPtr), ref(arg)); + t.detach(); + + this_thread::sleep_for(chrono::seconds(2)); + int clsoeRet = close(fileFd); + EXPECT_TRUE(clsoeRet == 0); + + this_thread::sleep_for(chrono::seconds(2)); + int removeRet = remove(filename.c_str()); + EXPECT_TRUE(removeRet == 0); + + GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_GetNotifyEvent_0000"; + } + +} // namespace \ No newline at end of file diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index c66b88a56..5f40f8fdd 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -52,7 +52,9 @@ public: std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); + std::tuple, uint32_t> ToUint32Array(); std::tuple ToUint64() const; + std::tuple ToUint32() const; std::tuple ToDouble() const; /* Static helpers to create js objects */ @@ -60,6 +62,7 @@ public: static NVal CreateBigInt64(napi_env env, int64_t val); static NVal CreateInt64(napi_env env, int64_t val); static NVal CreateInt32(napi_env env, int32_t val); + static NVal CreateUint32(napi_env env, int32_t val); static NVal CreateObject(napi_env env); static NVal CreateBool(napi_env env, bool val); static NVal CreateUTF8String(napi_env env, std::string str); diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index a2d6a5912..8eb6bd48e 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -131,6 +131,14 @@ tuple NVal::ToDouble() const return make_tuple(status == napi_ok, res); } +tuple NVal::ToUint32() const +{ + uint32_t res = 0; + napi_status status = napi_get_value_uint32(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + + tuple NVal::ToUint64() const { uint64_t res = 0; @@ -154,6 +162,21 @@ tuple, uint32_t> NVal::ToStringArray() return make_tuple(status == napi_ok, stringArray, size); } +tuple, uint32_t> NVal::ToUint32Array() +{ + napi_status status; + uint32_t size; + status = napi_get_array_length(env_, val_, &size); + vector uint32Array; + napi_value result; + for (uint32_t i = 0; i < size; i++) { + status = napi_get_element(env_, val_, i, &result); + auto [succ, uint32] = NVal(env_, result).ToUint32(); + uint32Array.push_back(uint32); + } + return make_tuple(status == napi_ok, uint32Array, size); +} + tuple NVal::ToArraybuffer() const { void *buf = nullptr; @@ -258,6 +281,13 @@ NVal NVal::CreateInt32(napi_env env, int32_t val) return {env, res}; } +NVal NVal::CreateUint32(napi_env env, int32_t val) +{ + napi_value res = nullptr; + napi_create_uint32(env, val, &res); + return {env, res}; +} + NVal NVal::CreateObject(napi_env env) { napi_value res = nullptr; -- Gitee From e6d06e04164294bbbc3639aaed3395fb9acfb5b8 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Mon, 6 Feb 2023 09:01:49 +0800 Subject: [PATCH 02/21] del tdd for watcher Signed-off-by: Cao Chuan --- interfaces/test/unittest/BUILD.gn | 5 +- .../test/unittest/file_watcher/BUILD.gn | 43 -- .../file_watcher/file_watcher_test.cpp | 380 ------------------ 3 files changed, 1 insertion(+), 427 deletions(-) delete mode 100644 interfaces/test/unittest/file_watcher/BUILD.gn delete mode 100644 interfaces/test/unittest/file_watcher/file_watcher_test.cpp diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index d74288bc7..4351bd3fe 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -13,8 +13,5 @@ group("unittest") { testonly = true - deps = [ - "remote_uri:remote_uri_test", - "file_watcher:file_watcher_test" - ] + deps = [ "remote_uri:remote_uri_test" ] } diff --git a/interfaces/test/unittest/file_watcher/BUILD.gn b/interfaces/test/unittest/file_watcher/BUILD.gn deleted file mode 100644 index fdfc92061..000000000 --- a/interfaces/test/unittest/file_watcher/BUILD.gn +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) 2022 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. - -import("//build/test.gni") - -ohos_unittest("file_watcher_test") { - module_out_path = "filemanagement/file_api" - - resource_config_file = "../resource/ohos_test.xml" - - sources = [ "file_watcher_test.cpp" ] - - include_dirs = [ - "include", - "//utils/system/safwk/native/include", - "//commonlibrary/c_utils/base/include", - "//foundation/filemanagement/file_api/interfaces/kits/js/src/common/napi", - "//foundation/filemanagement/file_api/interfaces/kits/js/src/mod_fileio/class_watcher", - "//third_party/libuv/include", - ] - - deps = [ - "//foundation/filemanagement/file_api/interfaces/kits/js:fileio", - "//third_party/googletest:gtest_main", - "//foundation/arkui/napi:ace_napi", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "c_utils:utils", - "ipc:ipc_core", - ] -} diff --git a/interfaces/test/unittest/file_watcher/file_watcher_test.cpp b/interfaces/test/unittest/file_watcher/file_watcher_test.cpp deleted file mode 100644 index 0a1db2167..000000000 --- a/interfaces/test/unittest/file_watcher/file_watcher_test.cpp +++ /dev/null @@ -1,380 +0,0 @@ -/* -* Copyright (c) 2022 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 "file_watcher.h" -#include -#include -#include -#include -#include -#include -#include -#include -namespace { - using namespace std; - using namespace OHOS::DistributedFS::ModuleFileIO; - - class FileWatcherTest : public testing::Test { - public: - static void SetUpTestCase(void){}; - static void TearDownTestCase(){}; - void SetUp(){}; - void TearDown(){}; - }; - - - void WatcherCallback(napi_env env, napi_ref callback, const std::string &filename, const uint32_t &event) - { - string path = "/data/data/test2"; - EXPECT_STREQ(path.c_str(), filename.c_str()); - if (event == IN_CLOSE || event == IN_DELETE) { - EXPECT_TRUE(true); - } else { - EXPECT_TRUE(false); - } - } - - void GetEvent(shared_ptr &fileWatcherPtr, std::shared_ptr &arg) - { - fileWatcherPtr->GetNotifyEvent(arg, WatcherCallback); - } - - /** - * @tc.name: Watcher_InitNotify_0000 - * @tc.desc: Test function of InitNotify() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_InitNotify_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_InitNotify_0000"; - int fd = 0; - int error = 0; - - shared_ptr fileWatcherPtr = make_shared(); - bool ret = fileWatcherPtr->InitNotify(fd, error); - - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(error == 0); - EXPECT_TRUE(ret == true); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_InitNotify_0000"; - } - - /** - * @tc.name: Watcher_StartNotify_0000 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0000"; - int error = 0; - std::shared_ptr arg = make_shared(); - shared_ptr fileWatcherPtr = make_shared(); - bool ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0000"; - } - - /** - * @tc.name: Watcher_StartNotify_0001 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0001, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0001"; - int error = 0; - std::shared_ptr arg = make_shared(); - arg->fd = 10; - - shared_ptr fileWatcherPtr = make_shared(); - bool ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0001"; - } - - /** - * @tc.name: Watcher_StartNotify_0002 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0002, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0002"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = ""; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0002"; - } - - /** - * @tc.name: Watcher_StartNotify_0003 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0003, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0003"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = "/data/data"; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0003"; - } - - /** - * @tc.name: Watcher_StartNotify_0004 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0004, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0004"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::vector events; - events.push_back(0); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = "/data/data"; - arg->events = events; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0004"; - } - - /** - * @tc.name: Watcher_StartNotify_0005 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0005, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0005"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::vector events; - events.push_back(IN_ACCESS); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = "/data/test"; - arg->events = events; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0005"; - } - - /** - * @tc.name: Watcher_StartNotify_0006 - * @tc.desc: Test function of StartNotify() interface for FAIL. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0006, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0006"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::vector events; - events.push_back(IN_ACCESS); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = "/data/test"; - arg->events = events; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == false); - EXPECT_TRUE(error > 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0006"; - } - - /** - * @tc.name: Watcher_StartNotify_0007 - * @tc.desc: Test function of StartNotify() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_StartNotify_0007, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_StartNotify_0007"; - int fd = 0; - int error = 0; - bool ret = false; - string filename = "/data/data/test"; - int fileFd = open(filename.c_str(), O_CREAT); - EXPECT_TRUE(fileFd > 0); - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - std::vector events; - events.push_back(IN_ACCESS); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = filename; - arg->events = events; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(ret == true); - EXPECT_TRUE(error == 0); - - ret = fileWatcherPtr->StopNotify(arg, error); - EXPECT_TRUE(ret == true); - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_StartNotify_0007"; - } - - /** - * @tc.name: Watcher_GetNotifyEvent_0000 - * @tc.desc: Test function of GetNotifyEvent() interface for SUCCESS. - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: AR000HG8M4 - */ - HWTEST_F(FileWatcherTest, Watcher_GetNotifyEvent_0000, testing::ext::TestSize.Level1) - { - GTEST_LOG_(INFO) << "FileWatcherTest-begin Watcher_GetNotifyEvent_0000"; - int fd = 0; - int error = 0; - bool ret = false; - - shared_ptr fileWatcherPtr = make_shared(); - ret = fileWatcherPtr->InitNotify(fd, error); - EXPECT_TRUE(fd > 0); - EXPECT_TRUE(ret == true); - - string filename = "/data/data/test2"; - int fileFd = open(filename.c_str(), O_CREAT); - EXPECT_TRUE(fileFd > 0); - - std::vector events; - events.push_back(IN_CLOSE); - events.push_back(IN_DELETE); - - std::shared_ptr arg = make_shared(); - arg->fd = fd; - arg->filename = filename; - arg->events = events; - - ret = fileWatcherPtr->StartNotify(arg, error); - EXPECT_TRUE(error == 0); - EXPECT_TRUE(ret == true); - - thread t(GetEvent, ref(fileWatcherPtr), ref(arg)); - t.detach(); - - this_thread::sleep_for(chrono::seconds(2)); - int clsoeRet = close(fileFd); - EXPECT_TRUE(clsoeRet == 0); - - this_thread::sleep_for(chrono::seconds(2)); - int removeRet = remove(filename.c_str()); - EXPECT_TRUE(removeRet == 0); - - GTEST_LOG_(INFO) << "FileWatcherTest-end Watcher_GetNotifyEvent_0000"; - } - -} // namespace \ No newline at end of file -- Gitee From ece775f6d461e6d23a0ecc772ecfadce0ec06c62 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Mon, 6 Feb 2023 10:26:19 +0800 Subject: [PATCH 03/21] Change watcher callback in uv loop mode Signed-off-by: Cao Chuan --- .../class_watcher/watcher_n_exporter.cpp | 75 +++++++++++++------ 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 042bfee9a..81f20d823 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -108,33 +108,64 @@ void WatcherNExporter::WatcherCallback(napi_env env, const std::string &fileName, const uint32_t &event) { + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env, &loop); + if (loop == nullptr) { + HILOGE("WatcherCallback loop is nullptr"); + return; + } + + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOGE("WatcherCallback work is nullptr"); + return; + } + auto callbackContext = make_unique(); callbackContext->env_ = env; callbackContext->ref_ = callback; callbackContext->fileName_ = fileName; callbackContext->event_ = event; - napi_value resource = nullptr; - napi_create_string_utf8(env, "FileIOCreateWatcher", NAPI_AUTO_LENGTH, &resource); - - napi_create_async_work( - env, nullptr, resource, [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - // JSCallbackData* jsCallbackData = (JSCallbackData*)data; - auto context = static_cast(data); - napi_value jsCallback = nullptr; - napi_get_reference_value(env, context->ref_, &jsCallback); - NVal objn = NVal::CreateObject(env); - objn.AddProp("fileName", NVal::CreateUTF8String(env, context->fileName_).val_); - objn.AddProp("event", NVal::CreateUint32(env, context->event_).val_); - napi_value global = nullptr; - napi_get_global(env, &global); - napi_value jsObj = nullptr; - napi_call_function(env, global, jsCallback, 1, &(objn.val_), &jsObj); - napi_delete_async_work(env, context->work_); - }, - static_cast(callbackContext.get()), &callbackContext->work_); - napi_queue_async_work(env, callbackContext->work_); - callbackContext.release(); + work->data = callbackContext.release(); + + int ret = uv_queue_work( + loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int stat) { + if (work == nullptr) { + HILOGE("uv_queue_work w is nullptr"); + return; + } + + JSCallbackContext *callbackContext = reinterpret_cast(work->data); + do { + if (callbackContext == nullptr) { + HILOGE("callbackContext is null"); + break; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = nullptr; + napi_status status = napi_get_reference_value(env, callbackContext->ref_, &jsCallback); + if (status != napi_ok) { + HILOGE("Create reference fail, status: %{public}d", status); + break; + } + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + napi_value retVal = nullptr; + status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + if (status != napi_ok) { + HILOGE("CallJs napi_call_function fail, status: %{public}d", status); + break; + } + } while (0); + delete callbackContext; + delete work; + }); + if (ret != 0) { + HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete work; + } } bool WatcherNExporter::Export() -- Gitee From 9c770f6f7de6d013d1674780fed73883d62f19bc Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Tue, 7 Feb 2023 16:01:16 +0800 Subject: [PATCH 04/21] modify the copyright of year Signed-off-by: Cao Chuan --- interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp | 4 ++-- interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h | 2 +- interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h | 2 +- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp | 2 +- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h | 4 ++-- interfaces/kits/js/src/mod_fs/properties/watcher.cpp | 2 +- interfaces/kits/js/src/mod_fs/properties/watcher.h | 4 ++-- utils/filemgmt_libn/include/n_val.h | 2 +- utils/filemgmt_libn/src/n_val.cpp | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index d6b0f8f69..75d0fc4c3 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -97,4 +97,4 @@ void FileWatcher::GetNotifyEvent(std::shared_ptr &arg, WatcherCa } } } -} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h index ae11351ba..0164ce86d 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index e11891195..1e239bf7f 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 81f20d823..2e1e88422 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index b4e053386..fb1063da9 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -54,4 +54,4 @@ private: static std::shared_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +#endif diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 0115353b5..3d68aaa34 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.h b/interfaces/kits/js/src/mod_fs/properties/watcher.h index 22059672c..29cb8a353 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.h +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -23,4 +23,4 @@ public: static napi_value CreateWatcher(napi_env env, napi_callback_info info); }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +#endif diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index 5f40f8fdd..962805850 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 8eb6bd48e..0a354c71e 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 -- Gitee From ffbacc67a8e8ddf0f04b04752b78eeec5e544c9d Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Thu, 9 Feb 2023 13:16:54 +0800 Subject: [PATCH 05/21] revert mod_fileio watcher Signed-off-by: Cao Chuan --- interfaces/kits/js/BUILD.gn | 2 + .../mod_fileio/class_watcher/watcher_entity.h | 53 +++++++ .../class_watcher/watcher_n_exporter.cpp | 144 ++++++++++++++++++ .../class_watcher/watcher_n_exporter.h | 41 +++++ interfaces/kits/js/src/mod_fileio/module.cpp | 2 + .../mod_fileio/properties/prop_n_exporter.cpp | 6 +- .../js/src/mod_fileio/properties/watcher.cpp | 106 +++++++++++++ .../js/src/mod_fileio/properties/watcher.h | 35 +++++ .../src/mod_fs/class_watcher/file_watcher.cpp | 2 +- .../src/mod_fs/class_watcher/file_watcher.h | 2 +- .../src/mod_fs/class_watcher/watcher_entity.h | 2 +- .../class_watcher/watcher_n_exporter.cpp | 2 +- .../mod_fs/class_watcher/watcher_n_exporter.h | 2 +- .../kits/js/src/mod_fs/properties/watcher.cpp | 2 +- .../kits/js/src/mod_fs/properties/watcher.h | 2 +- 15 files changed, 393 insertions(+), 10 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h create mode 100644 interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h create mode 100644 interfaces/kits/js/src/mod_fileio/properties/watcher.cpp create mode 100644 interfaces/kits/js/src/mod_fileio/properties/watcher.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 37be21979..1b67878f5 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -52,6 +52,7 @@ ohos_shared_library("fileio") { "src/mod_fileio/class_stat/stat_n_exporter.cpp", "src/mod_fileio/class_stream/flush.cpp", "src/mod_fileio/class_stream/stream_n_exporter.cpp", + "src/mod_fileio/class_watcher/watcher_n_exporter.cpp", "src/mod_fileio/common_func.cpp", "src/mod_fileio/module.cpp", "src/mod_fileio/properties/chmod.cpp", @@ -85,6 +86,7 @@ ohos_shared_library("fileio") { "src/mod_fileio/properties/stat.cpp", "src/mod_fileio/properties/symlink.cpp", "src/mod_fileio/properties/truncate.cpp", + "src/mod_fileio/properties/watcher.cpp", ] deps = [ diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h new file mode 100644 index 000000000..ce604ee6b --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_entity.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 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_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H + +#include + +#include "../../common/napi/uni_header.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class WatcherHandleDeleter { +public: + void operator()(uv_fs_event_t *ptr) + { + if (ptr == nullptr) { + return; + } + + uv_fs_event_stop(ptr); + uv_handle_t *handle = reinterpret_cast(ptr); + uv_close(handle, [](uv_handle_t *handle) { delete handle; }); + } +}; + +struct WatcherInforArg { + int events = 0; + napi_env env = nullptr; + napi_ref ref = nullptr; +}; + +struct WatcherEntity { + std::unique_ptr data_; + std::unique_ptr fsEventReq_; +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp new file mode 100644 index 000000000..2947b10cb --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2021 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 "watcher_n_exporter.h" + +#include +#include +#include +#include + +#include "../../common/log.h" +#include "../../common/napi/n_async/n_async_work_callback.h" +#include "../../common/napi/n_async/n_async_work_promise.h" +#include "../../common/napi/n_class.h" +#include "../../common/napi/n_func_arg.h" +#include "../../common/uni_error.h" +#include "securec.h" +#include "watcher_entity.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + unique_ptr watcherEntity = make_unique(); + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { + UniError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat"); + return nullptr; + } + + return funcArg.GetThisVar(); +} + +napi_value WatcherNExporter::StopSync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!watchEntity) { + UniError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + return nullptr; + } + + watchEntity->fsEventReq_.reset(); + return NVal::CreateUndefined(env).val_; +} + +napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!watchEntity) { + UniError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + return nullptr; + } + + auto cbExec = [watchEntity](napi_env env) -> UniError { + watchEntity->fsEventReq_.reset(); + return UniError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + return { NVal::CreateUndefined(env) }; + }; + + const string procedureName = "FileIOCreaterWatcher"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbCompl).val_; + } +} + +bool WatcherNExporter::Export() +{ + vector props = { + NVal::DeclareNapiFunction("stop", Stop), + NVal::DeclareNapiFunction("stopSync", StopSync), + }; + + string className = GetClassName(); + auto [resDefineClass, classValue] = NClass::DefineClass(exports_.env_, + className, + WatcherNExporter::Constructor, + std::move(props)); + if (!resDefineClass) { + UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class"); + return false; + } + + bool succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + UniError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +string WatcherNExporter::GetClassName() +{ + return WatcherNExporter::className_; +} + +WatcherNExporter::WatcherNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} + +WatcherNExporter::~WatcherNExporter() {} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h new file mode 100644 index 000000000..3d3502b7b --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/class_watcher/watcher_n_exporter.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 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_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H + +#include "../../common/napi/n_exporter.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class WatcherNExporter final : public NExporter { +public: + inline static const std::string className_ = "Watcher"; + + bool Export() override; + std::string GetClassName() override; + + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Stop(napi_env env, napi_callback_info info); + static napi_value StopSync(napi_env env, napi_callback_info info); + + WatcherNExporter(napi_env env, napi_value exports); + ~WatcherNExporter() override; +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif diff --git a/interfaces/kits/js/src/mod_fileio/module.cpp b/interfaces/kits/js/src/mod_fileio/module.cpp index 0a0fcca0f..e0abeac25 100644 --- a/interfaces/kits/js/src/mod_fileio/module.cpp +++ b/interfaces/kits/js/src/mod_fileio/module.cpp @@ -23,6 +23,7 @@ #include "class_randomaccessfile/randomaccessfile_n_exporter.h" #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_n_exporter.h" +#include "class_watcher/watcher_n_exporter.h" #include "properties/prop_n_exporter.h" using namespace std; @@ -39,6 +40,7 @@ static napi_value Export(napi_env env, napi_value exports) products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); + products.emplace_back(make_unique(env, exports)); products.emplace_back(make_unique(env, exports)); for (auto &&product : products) { diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp index 1c5fcdb81..a7a1acbac 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter.cpp @@ -20,8 +20,7 @@ #include #include #include -#include -#include + #include "../common_func.h" #include "chmod.h" #include "chown.h" @@ -53,7 +52,7 @@ #include "stat.h" #include "symlink.h" #include "truncate.h" - +#include "watcher.h" namespace OHOS { namespace DistributedFS { @@ -688,6 +687,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("createRandomAccessFileSync", CreateRandomAccessFile::Sync), NVal::DeclareNapiFunction("createStream", CreateStream::Async), NVal::DeclareNapiFunction("createStreamSync", CreateStream::Sync), + NVal::DeclareNapiFunction("createWatcher", Watcher::CreateWatcher), NVal::DeclareNapiFunction("fchmod", Fchmod::Async), NVal::DeclareNapiFunction("fchmodSync", Fchmod::Sync), NVal::DeclareNapiFunction("fchown", Fchown::Async), diff --git a/interfaces/kits/js/src/mod_fileio/properties/watcher.cpp b/interfaces/kits/js/src/mod_fileio/properties/watcher.cpp new file mode 100644 index 000000000..b822f9a3c --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/watcher.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 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 "watcher.h" + +#include +#include +#include +#include + +#include "../../common/napi/n_async/n_ref.h" +#include "../../common/napi/n_class.h" +#include "../../common/napi/n_func_arg.h" +#include "../../common/napi/n_val.h" +#include "../../common/uni_error.h" + +#include "../class_watcher/watcher_entity.h" +#include "../class_watcher/watcher_n_exporter.h" +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +using namespace std; + +void Watcher::RunCommand(uv_fs_event_t *handle, const char *filename, int events, int status) +{ + WatcherInforArg *information = (WatcherInforArg *)handle->data; + uint32_t eventsFirst { events }; + uint32_t eventsSecond { information->events }; + if (eventsFirst & eventsSecond) { + napi_handle_scope scope = nullptr; + napi_open_handle_scope(information->env, &scope); + napi_value callback = nullptr; + napi_get_reference_value(information->env, information->ref, &callback); + vector argv; + argv.push_back(NVal::CreateInt64(information->env, events).val_); + napi_value global = nullptr; + napi_get_global(information->env, &global); + napi_value tmp = nullptr; + napi_call_function(information->env, global, callback, argv.size(), argv.data(), &tmp); + } +} + +napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succGetPath) { + UniError(EINVAL).ThrowErr(env, "Invalid filename"); + return nullptr; + } + + auto [succGetEvent, event] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!succGetEvent) { + UniError(EINVAL).ThrowErr(env, "Invalid event"); + return nullptr; + } + + unique_ptr data = make_unique(); + data->events = event; + data->env = env; + NVal val = NVal(env, funcArg[NARG_POS::THIRD]); + napi_create_reference(val.env_, val.val_, 1, &(data->ref)); + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env, &loop); + unique_ptr fsEventReq(new uv_fs_event_t); + uv_fs_event_init(loop, fsEventReq.get()); + fsEventReq->data = data.get(); + uv_fs_event_start(fsEventReq.get(), RunCommand, filename.get(), UV_FS_EVENT_RECURSIVE); + + napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); + if (!objWatcher) { + UniError(EINVAL).ThrowErr(env, "objWatcher create failed"); + return nullptr; + } + + auto watcherEntity = NClass::GetEntityOf(env, objWatcher); + if (!watcherEntity) { + UniError(EINVAL).ThrowErr(env, "watcherEntity get failed"); + return nullptr; + } + + watcherEntity->fsEventReq_ = std::move(fsEventReq); + watcherEntity->data_ = std::move(data); + + return objWatcher; +} +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fileio/properties/watcher.h b/interfaces/kits/js/src/mod_fileio/properties/watcher.h new file mode 100644 index 000000000..45c100cb2 --- /dev/null +++ b/interfaces/kits/js/src/mod_fileio/properties/watcher.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 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_FILEIO_PROPERTIES_WATCHER_H +#define INTERFACES_KITS_JS_SRC_MOD_FILEIO_PROPERTIES_WATCHER_H + +#include +#include "../../common/napi/n_val.h" + +namespace OHOS { +namespace DistributedFS { +namespace ModuleFileIO { +class Watcher final { +public: + static napi_value CreateWatcher(napi_env env, napi_callback_info info); + +private: + static void RunCommand(uv_fs_event_t *handle, const char *filename, int events, int status); +}; +} // namespace ModuleFileIO +} // namespace DistributedFS +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 75d0fc4c3..4b4041aa2 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -97,4 +97,4 @@ void FileWatcher::GetNotifyEvent(std::shared_ptr &arg, WatcherCa } } } -} // namespace OHOS::FileManagement::ModuleFileIO +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h index 0164ce86d..1ff0e0697 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -40,4 +40,4 @@ private: bool run_; }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 1e239bf7f..4f2ca0a39 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -34,4 +34,4 @@ struct WatcherEntity { std::shared_ptr data_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS -#endif +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 2e1e88422..e032008bc 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -200,4 +200,4 @@ string WatcherNExporter::GetClassName() WatcherNExporter::WatcherNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} WatcherNExporter::~WatcherNExporter() {} -} // namespace OHOS::FileManagement::ModuleFileIO +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index fb1063da9..c573fb965 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -54,4 +54,4 @@ private: static std::shared_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 3d68aaa34..246a75e63 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -78,4 +78,4 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return objWatcher; } -} // namespace OHOS::FileManagement::ModuleFileIO +} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.h b/interfaces/kits/js/src/mod_fs/properties/watcher.h index 29cb8a353..2faad0528 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.h +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.h @@ -23,4 +23,4 @@ public: static napi_value CreateWatcher(napi_env env, napi_callback_info info); }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif +#endif \ No newline at end of file -- Gitee From 8ce26b370251a5ee8137743bd210e517f7a9f6f5 Mon Sep 17 00:00:00 2001 From: caochuan Date: Thu, 9 Feb 2023 07:51:10 +0000 Subject: [PATCH 06/21] modify createWatcher Signed-off-by: caochuan --- .../js/src/mod_fs/class_watcher/file_watcher.cpp | 15 +++------------ .../js/src/mod_fs/class_watcher/watcher_entity.h | 2 +- .../mod_fs/class_watcher/watcher_n_exporter.cpp | 2 ++ .../kits/js/src/mod_fs/properties/watcher.cpp | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 4b4041aa2..64ba7da8e 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -37,11 +37,7 @@ bool FileWatcher::InitNotify(int &fd) bool FileWatcher::StartNotify(std::shared_ptr &arg) { int wd = 0; - uint32_t watchEvent = 0; - for (auto event : arg->events) { - watchEvent = watchEvent | event; - } - wd = inotify_add_watch(arg->fd, arg->filename.c_str(), watchEvent); + wd = inotify_add_watch(arg->fd, arg->filename.c_str(), arg->events); if(wd == -1) { HILOGE("FileWatcher StartNotify fail errCode:%{public}d", errno); return false; @@ -67,13 +63,8 @@ void FileWatcher::HandleEvent(std::shared_ptr &arg, WatcherCallback callback) { if (event->wd == arg->wd) { - for (auto watchEvent : arg->events) { - if (event->mask == watchEvent || watchEvent == IN_ALL_EVENTS) { - std::string filename = arg->filename + "/" + event->name; - HILOGI("FileWatcher HandleEvent fileName:%{public}s, mask:%{public}d", filename.c_str(), event->mask); - callback(arg->env, arg->ref, filename, event->mask); - } - } + std::string filename = arg->filename + "/" + event->name; + callback(arg->env, arg->ref, filename, event->mask); } } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 4f2ca0a39..f2d51bf09 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -23,7 +23,7 @@ namespace OHOS::FileManagement::ModuleFileIO { struct WatcherInfoArg { std::string filename; - std::vector events; + uint32_t events; int fd; int wd; napi_env env = nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index e032008bc..49c0f3d34 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -160,10 +160,12 @@ void WatcherNExporter::WatcherCallback(napi_env env, } } while (0); delete callbackContext; + delete(work->data); delete work; }); if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete(work->data); delete work; } } diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 246a75e63..cadb2ca6b 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -40,7 +40,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return nullptr; } - auto [succGetEvent, events, size] = NVal(env, funcArg[NARG_POS::SECOND]).ToUint32Array(); + auto [succGetEvent, events] = NVal(env, funcArg[NARG_POS::SECOND]).ToUint32(); if (!succGetEvent) { NError(EINVAL).ThrowErr(env, "Invalid event"); return nullptr; -- Gitee From 5d59e007fe91a4c3e327ed340b1a0e2e0c54979c Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Thu, 9 Feb 2023 21:04:29 +0800 Subject: [PATCH 07/21] modify code format Signed-off-by: Cao Chuan --- .../src/mod_fs/class_watcher/file_watcher.cpp | 39 ++++++++++--------- .../src/mod_fs/class_watcher/file_watcher.h | 15 ++++--- .../src/mod_fs/class_watcher/watcher_entity.h | 5 ++- .../class_watcher/watcher_n_exporter.cpp | 5 +-- .../mod_fs/class_watcher/watcher_n_exporter.h | 3 +- .../kits/js/src/mod_fs/properties/watcher.cpp | 19 ++++----- .../kits/js/src/mod_fs/properties/watcher.h | 2 +- 7 files changed, 47 insertions(+), 41 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 64ba7da8e..67aca2e72 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -13,13 +13,14 @@ * limitations under the License. */ #include "file_watcher.h" + #include #include + #include "filemgmt_libhilog.h" #include "uv.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace OHOS::FileManagement::LibN; -const int BUF_SIZE = 1024; FileWatcher::FileWatcher() {} FileWatcher::~FileWatcher() {} @@ -34,58 +35,58 @@ bool FileWatcher::InitNotify(int &fd) return true; } -bool FileWatcher::StartNotify(std::shared_ptr &arg) +bool FileWatcher::StartNotify(WatcherInfoArg &arg) { - int wd = 0; - wd = inotify_add_watch(arg->fd, arg->filename.c_str(), arg->events); + int wd = inotify_add_watch(arg.fd, arg.filename.c_str(), arg.events); if(wd == -1) { HILOGE("FileWatcher StartNotify fail errCode:%{public}d", errno); return false; } - arg->wd = wd; + arg.wd = wd; run_ = true; return true; } -bool FileWatcher::StopNotify(std::shared_ptr &arg) +bool FileWatcher::StopNotify(const WatcherInfoArg &arg) { run_ = false; - if (inotify_rm_watch(arg->fd, arg->wd) == -1) { + if (inotify_rm_watch(arg.fd, arg.wd) == -1) { HILOGE("FileWatcher StopNotify fail errCode:%{public}d", errno); return false; } - close(arg->fd); + close(arg.fd); return true; } -void FileWatcher::HandleEvent(std::shared_ptr &arg, +void FileWatcher::HandleEvent(const WatcherInfoArg &arg, const struct inotify_event *event, WatcherCallback callback) { - if (event->wd == arg->wd) { - std::string filename = arg->filename + "/" + event->name; - callback(arg->env, arg->ref, filename, event->mask); + if (event->wd != arg.wd) { + return; } + std::string filename = arg.filename + "/" + event->name; + callback(arg.env, arg.ref, filename, event->mask); } -void FileWatcher::GetNotifyEvent(std::shared_ptr &arg, WatcherCallback callback) +void FileWatcher::GetNotifyEvent(const WatcherInfoArg &arg, WatcherCallback callback) { char buf[BUF_SIZE] = {0}; struct inotify_event *event = nullptr; while (run_) { - int fd = arg->fd; + int fd = arg.fd; fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); if (select(fd + 1, &fds, NULL, NULL, NULL) > 0) { int len, index = 0; while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)); - while (index < len) { - event = (struct inotify_event *)(buf + index); - HandleEvent(arg, event, callback); - index += sizeof(struct inotify_event) + event->len; + while (index < len) { + event = (struct inotify_event *)(buf + index); + HandleEvent(arg, event, callback); + index += sizeof(struct inotify_event) + event->len; } } } } -} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h index 1ff0e0697..388cd1168 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -18,6 +18,7 @@ #include #include #include + #include "watcher_entity.h" namespace OHOS::FileManagement::ModuleFileIO { using WatcherCallback = void (*)(napi_env env, napi_ref callback, const std::string &filename, const uint32_t &event); @@ -31,13 +32,15 @@ public: FileWatcher(); ~FileWatcher(); bool InitNotify(int &fd); - bool StartNotify(std::shared_ptr &arg); - bool StopNotify(std::shared_ptr &arg); - void GetNotifyEvent(std::shared_ptr &arg, WatcherCallback callback); + bool StartNotify(WatcherInfoArg &arg); + bool StopNotify(const WatcherInfoArg &arg); + void GetNotifyEvent(const WatcherInfoArg &arg, WatcherCallback callback); private: - void HandleEvent(std::shared_ptr &arg, const struct inotify_event *event, WatcherCallback callback); - bool run_; + void HandleEvent(const WatcherInfoArg &arg, const struct inotify_event *event, + WatcherCallback callback); + static constexpr int BUF_SIZE = 1024; + bool run_ = false; }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +#endif diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index f2d51bf09..ec58a5e0d 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -18,6 +18,7 @@ #include #include #include + #include "filemgmt_libn.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -31,7 +32,7 @@ struct WatcherInfoArg { }; struct WatcherEntity { - std::shared_ptr data_; + WatcherInfoArg data_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS -#endif \ No newline at end of file +#endif diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 49c0f3d34..b6321fa19 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -19,6 +19,7 @@ #include #include #include + #include "filemgmt_libn.h" #include "filemgmt_libhilog.h" #include "../common_func.h" @@ -160,12 +161,10 @@ void WatcherNExporter::WatcherCallback(napi_env env, } } while (0); delete callbackContext; - delete(work->data); delete work; }); if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); - delete(work->data); delete work; } } @@ -202,4 +201,4 @@ string WatcherNExporter::GetClassName() WatcherNExporter::WatcherNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {} WatcherNExporter::~WatcherNExporter() {} -} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index c573fb965..537fcd68d 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -17,6 +17,7 @@ #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H #include #include + #include "watcher_entity.h" #include "file_watcher.h" #include "filemgmt_libn.h" @@ -54,4 +55,4 @@ private: static std::shared_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO -#endif \ No newline at end of file +#endif diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index cadb2ca6b..a4db1d7c2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 @@ -14,6 +14,7 @@ */ #include "watcher.h" + #include #include #include @@ -46,21 +47,21 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return nullptr; } - int fd = 0; + int fd = -1; shared_ptr watcherPtr = make_shared(); if (!watcherPtr->InitNotify(fd)) { NError(errno).ThrowErr(env); return nullptr; } - shared_ptr data = make_shared(); - data->events = events; - data->env = env; - data->filename = string(filename.get()); - data->fd = fd; + WatcherInfoArg data; + data.events = events; + data.env = env; + data.filename = string(filename.get()); + data.fd = fd; NVal val = NVal(env, funcArg[NARG_POS::THIRD]); - napi_create_reference(val.env_, val.val_, 1, &(data->ref)); + napi_create_reference(val.env_, val.val_, 1, &(data.ref)); napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); @@ -78,4 +79,4 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return objWatcher; } -} // namespace OHOS::FileManagement::ModuleFileIO \ No newline at end of file +} // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.h b/interfaces/kits/js/src/mod_fs/properties/watcher.h index 2faad0528..4d2f0a4fc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.h +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 -- Gitee From d07ad0a97e06c0bc0d719aa02b788c962783d1c3 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Thu, 9 Feb 2023 21:07:19 +0800 Subject: [PATCH 08/21] modify format Signed-off-by: Cao Chuan --- interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 67aca2e72..8e4976752 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -85,7 +85,7 @@ void FileWatcher::GetNotifyEvent(const WatcherInfoArg &arg, WatcherCallback call event = (struct inotify_event *)(buf + index); HandleEvent(arg, event, callback); index += sizeof(struct inotify_event) + event->len; - } + } } } } -- Gitee From d41b72f8af83938f9ef3da23cea2a1bf8a0aa477 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Fri, 10 Feb 2023 08:49:54 +0800 Subject: [PATCH 09/21] modify code format Signed-off-by: Cao Chuan --- interfaces/kits/js/src/mod_fs/properties/watcher.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index a4db1d7c2..335d0ad3e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -20,6 +20,7 @@ #include #include #include + #include "filemgmt_libhilog.h" #include "../class_watcher/file_watcher.h" #include "../class_watcher/watcher_entity.h" -- Gitee From 73dc07908977a996b010347b248179fca0d6dcf9 Mon Sep 17 00:00:00 2001 From: caochuan Date: Fri, 10 Feb 2023 13:27:23 +0000 Subject: [PATCH 10/21] Fix mem leaked Signed-off-by: caochuan --- .../src/mod_fs/class_watcher/file_watcher.cpp | 6 +-- .../src/mod_fs/class_watcher/file_watcher.h | 7 ++-- .../src/mod_fs/class_watcher/watcher_entity.h | 9 +++-- .../class_watcher/watcher_n_exporter.cpp | 38 +++++++++++-------- .../mod_fs/class_watcher/watcher_n_exporter.h | 9 ++--- .../kits/js/src/mod_fs/properties/watcher.cpp | 21 ++++------ 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 8e4976752..8d8847a82 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -58,7 +58,7 @@ bool FileWatcher::StopNotify(const WatcherInfoArg &arg) return true; } -void FileWatcher::HandleEvent(const WatcherInfoArg &arg, +void FileWatcher::HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, WatcherCallback callback) { @@ -66,10 +66,10 @@ void FileWatcher::HandleEvent(const WatcherInfoArg &arg, return; } std::string filename = arg.filename + "/" + event->name; - callback(arg.env, arg.ref, filename, event->mask); + callback(arg.env, arg.nRef, filename, event->mask); } -void FileWatcher::GetNotifyEvent(const WatcherInfoArg &arg, WatcherCallback callback) +void FileWatcher::GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback) { char buf[BUF_SIZE] = {0}; struct inotify_event *event = nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h index 388cd1168..0eb2146e2 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -21,7 +21,8 @@ #include "watcher_entity.h" namespace OHOS::FileManagement::ModuleFileIO { -using WatcherCallback = void (*)(napi_env env, napi_ref callback, const std::string &filename, const uint32_t &event); +using WatcherCallback = void (*)(napi_env env, LibN::NRef &callback, + const std::string &filename, const uint32_t &event); struct ErrorInfo { int errCode; std::string errMsg; @@ -34,10 +35,10 @@ public: bool InitNotify(int &fd); bool StartNotify(WatcherInfoArg &arg); bool StopNotify(const WatcherInfoArg &arg); - void GetNotifyEvent(const WatcherInfoArg &arg, WatcherCallback callback); + void GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback); private: - void HandleEvent(const WatcherInfoArg &arg, const struct inotify_event *event, + void HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, WatcherCallback callback); static constexpr int BUF_SIZE = 1024; bool run_ = false; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index ec58a5e0d..f3a0691d1 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -28,11 +28,14 @@ struct WatcherInfoArg { int fd; int wd; napi_env env = nullptr; - napi_ref ref = nullptr; + LibN::NRef nRef; + + explicit WatcherInfoArg(LibN::NVal jsVal) : nRef(jsVal) {} + ~WatcherInfoArg() = default; }; struct WatcherEntity { - WatcherInfoArg data_; + std::unique_ptr data_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS -#endif +#endif \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index b6321fa19..d6c52575c 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -29,7 +29,7 @@ namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -std::shared_ptr WatcherNExporter::watcherPtr_; +std::unique_ptr WatcherNExporter::watcherPtr_ = nullptr; napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -44,7 +44,7 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - watcherPtr_ = make_shared(); + watcherPtr_ = make_unique(); return funcArg.GetThisVar(); } @@ -61,7 +61,7 @@ napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } - if (!watcherPtr_->StopNotify(watchEntity->data_)) { + if (!watcherPtr_->StopNotify(*(watchEntity->data_))) { NError(errno).ThrowErr(env); return nullptr; } @@ -82,13 +82,13 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } - if(!watcherPtr_->StartNotify(watchEntity->data_)) { + if(!watcherPtr_->StartNotify(*(watchEntity->data_))) { NError(errno).ThrowErr(env); return nullptr; } auto cbExec = [watchEntity]() -> NError { - watcherPtr_->GetNotifyEvent(watchEntity->data_, WatcherCallback); + watcherPtr_->GetNotifyEvent(*(watchEntity->data_), WatcherCallback); return NError(ERRNO_NOERR); }; @@ -105,7 +105,7 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) } void WatcherNExporter::WatcherCallback(napi_env env, - napi_ref callback, + NRef &callback, const std::string &fileName, const uint32_t &event) { @@ -122,12 +122,16 @@ void WatcherNExporter::WatcherCallback(napi_env env, return; } - auto callbackContext = make_unique(); + if (!callback) { + HILOGE("Pass watcher callback fail"); + return; + } + + JSCallbackContext *callbackContext = new (std::nothrow) JSCallbackContext(callback); callbackContext->env_ = env; - callbackContext->ref_ = callback; callbackContext->fileName_ = fileName; callbackContext->event_ = event; - work->data = callbackContext.release(); + work->data = reinterpret_cast(callbackContext); int ret = uv_queue_work( loop, work, [](uv_work_t *work) {}, @@ -143,18 +147,21 @@ void WatcherNExporter::WatcherCallback(napi_env env, HILOGE("callbackContext is null"); break; } - napi_env env = callbackContext->env_; - napi_value jsCallback = nullptr; - napi_status status = napi_get_reference_value(env, callbackContext->ref_, &jsCallback); - if (status != napi_ok) { - HILOGE("Create reference fail, status: %{public}d", status); + if (!callbackContext->ref_) { + HILOGE("callbackContext nref is null"); break; } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + + napi_valuetype valueType; + napi_typeof(env, jsCallback, &valueType); + NVal objn = NVal::CreateObject(env); objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); napi_value retVal = nullptr; - status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + napi_status status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); if (status != napi_ok) { HILOGE("CallJs napi_call_function fail, status: %{public}d", status); break; @@ -165,6 +172,7 @@ void WatcherNExporter::WatcherCallback(napi_env env, }); if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete callbackContext; delete work; } } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index 537fcd68d..9b44160c9 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -27,15 +27,14 @@ class WatcherNExporter final : public NExporter { public: class JSCallbackContext { public: - JSCallbackContext() {} + explicit JSCallbackContext(NRef &ref) : ref_(ref) {} ~JSCallbackContext() {} public: napi_env env_; - napi_ref ref_; + NRef &ref_; std::string fileName_; uint32_t event_; - napi_async_work work_; }; inline static const std::string className_ = "Watcher"; @@ -46,13 +45,13 @@ public: static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value Start(napi_env env, napi_callback_info info); static napi_value Stop(napi_env env, napi_callback_info info); - static void WatcherCallback(napi_env env, napi_ref callback, const std::string &fileName, const uint32_t &event); + static void WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event); WatcherNExporter(napi_env env, napi_value exports); ~WatcherNExporter() override; private: - static std::shared_ptr watcherPtr_; + static std::unique_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO #endif diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 335d0ad3e..1a5a5cb15 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -49,21 +49,11 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr = make_shared(); + unique_ptr watcherPtr = make_unique(); if (!watcherPtr->InitNotify(fd)) { NError(errno).ThrowErr(env); return nullptr; - } - - WatcherInfoArg data; - data.events = events; - data.env = env; - data.filename = string(filename.get()); - data.fd = fd; - - NVal val = NVal(env, funcArg[NARG_POS::THIRD]); - napi_create_reference(val.env_, val.val_, 1, &(data.ref)); - + } napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); if (!objWatcher) { @@ -76,7 +66,12 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env, "watcherEntity get failed"); return nullptr; } - watcherEntity->data_ = data; + + watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + watcherEntity->data_->events = events; + watcherEntity->data_->env = env; + watcherEntity->data_->filename = string(filename.get()); + watcherEntity->data_->fd = fd; return objWatcher; } -- Gitee From fbfa029c19e25628eef9c296505bc9a15595df51 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Mon, 13 Feb 2023 14:12:29 +0800 Subject: [PATCH 11/21] modify code format Signed-off-by: Cao Chuan --- .../js/src/mod_fs/class_watcher/file_watcher.cpp | 10 +++++----- .../js/src/mod_fs/class_watcher/file_watcher.h | 7 +------ .../js/src/mod_fs/class_watcher/watcher_entity.h | 10 +++++----- utils/filemgmt_libn/include/n_val.h | 1 - utils/filemgmt_libn/src/n_val.cpp | 15 --------------- 5 files changed, 11 insertions(+), 32 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp index 8d8847a82..d6d33e3e8 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp @@ -14,13 +14,13 @@ */ #include "file_watcher.h" +#include #include -#include #include "filemgmt_libhilog.h" #include "uv.h" namespace OHOS::FileManagement::ModuleFileIO { -using namespace OHOS::FileManagement::LibN; +using namespace OHOS::FileManagement::LibN; FileWatcher::FileWatcher() {} FileWatcher::~FileWatcher() {} @@ -38,7 +38,7 @@ bool FileWatcher::InitNotify(int &fd) bool FileWatcher::StartNotify(WatcherInfoArg &arg) { int wd = inotify_add_watch(arg.fd, arg.filename.c_str(), arg.events); - if(wd == -1) { + if (wd == -1) { HILOGE("FileWatcher StartNotify fail errCode:%{public}d", errno); return false; } @@ -78,9 +78,9 @@ void FileWatcher::GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback) fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); - if (select(fd + 1, &fds, NULL, NULL, NULL) > 0) { + if (select(fd + 1, &fds, nullptr, nullptr, nullptr) > 0) { int len, index = 0; - while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)); + while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)) {}; while (index < len) { event = (struct inotify_event *)(buf + index); HandleEvent(arg, event, callback); diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h index 0eb2146e2..8160f1c33 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h @@ -23,11 +23,7 @@ namespace OHOS::FileManagement::ModuleFileIO { using WatcherCallback = void (*)(napi_env env, LibN::NRef &callback, const std::string &filename, const uint32_t &event); -struct ErrorInfo { - int errCode; - std::string errMsg; -}; - +constexpr int BUF_SIZE = 1024; class FileWatcher { public: FileWatcher(); @@ -40,7 +36,6 @@ public: private: void HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, WatcherCallback callback); - static constexpr int BUF_SIZE = 1024; bool run_ = false; }; } // namespace OHOS::FileManagement::ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index f3a0691d1..81d6c9435 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -23,10 +23,10 @@ namespace OHOS::FileManagement::ModuleFileIO { struct WatcherInfoArg { - std::string filename; - uint32_t events; - int fd; - int wd; + std::string filename = ""; + uint32_t events = 0; + int fd = -1; + int wd = -1; napi_env env = nullptr; LibN::NRef nRef; @@ -38,4 +38,4 @@ struct WatcherEntity { std::unique_ptr data_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS -#endif \ No newline at end of file +#endif diff --git a/utils/filemgmt_libn/include/n_val.h b/utils/filemgmt_libn/include/n_val.h index 962805850..487a0d35c 100644 --- a/utils/filemgmt_libn/include/n_val.h +++ b/utils/filemgmt_libn/include/n_val.h @@ -52,7 +52,6 @@ public: std::tuple ToArraybuffer() const; std::tuple ToTypedArray() const; std::tuple, uint32_t> ToStringArray(); - std::tuple, uint32_t> ToUint32Array(); std::tuple ToUint64() const; std::tuple ToUint32() const; std::tuple ToDouble() const; diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 0a354c71e..cc40313ab 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -162,21 +162,6 @@ tuple, uint32_t> NVal::ToStringArray() return make_tuple(status == napi_ok, stringArray, size); } -tuple, uint32_t> NVal::ToUint32Array() -{ - napi_status status; - uint32_t size; - status = napi_get_array_length(env_, val_, &size); - vector uint32Array; - napi_value result; - for (uint32_t i = 0; i < size; i++) { - status = napi_get_element(env_, val_, i, &result); - auto [succ, uint32] = NVal(env_, result).ToUint32(); - uint32Array.push_back(uint32); - } - return make_tuple(status == napi_ok, uint32Array, size); -} - tuple NVal::ToArraybuffer() const { void *buf = nullptr; -- Gitee From 1cf37d9407d69601daef8a13ef5eda29f972a47b Mon Sep 17 00:00:00 2001 From: caochuan Date: Mon, 13 Feb 2023 07:40:01 +0000 Subject: [PATCH 12/21] Fix code style check issue in WatcherNExporter Signed-off-by: caochuan --- .../class_watcher/watcher_n_exporter.cpp | 94 ++++++++++--------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index d6c52575c..cded49194 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -82,9 +82,10 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } - if(!watcherPtr_->StartNotify(*(watchEntity->data_))) { - NError(errno).ThrowErr(env); - return nullptr; + + if (!watcherPtr_->StartNotify(*(watchEntity->data_))) { + NError(errno).ThrowErr(env); + return nullptr; } auto cbExec = [watchEntity]() -> NError { @@ -104,26 +105,61 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbCompl).val_; } -void WatcherNExporter::WatcherCallback(napi_env env, - NRef &callback, - const std::string &fileName, - const uint32_t &event) +static void WatcherCallbackComplete(uv_work_t *work, int stat) +{ + if (work == nullptr) { + HILOGE("Failed to get uv_queue_work pointer"); + return; + } + + WatcherNExporter::JSCallbackContext *callbackContext = + reinterpret_cast(work->data); + do { + if (callbackContext == nullptr) { + HILOGE("Failed to create context pointer"); + break; + } + if (!callbackContext->ref_) { + HILOGE("Failed to get nref reference"); + break; + } + napi_env env = callbackContext->env_; + napi_value jsCallback = callbackContext->ref_.Deref(env).val_; + + napi_valuetype valueType; + napi_typeof(env, jsCallback, &valueType); + + NVal objn = NVal::CreateObject(env); + objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); + objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + napi_value retVal = nullptr; + napi_status status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); + if (status != napi_ok) { + HILOGE("Failed to call napi_call_function, status: %{public}d", status); + break; + } + } while (0); + delete callbackContext; + delete work; +} + +void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event) { uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); if (loop == nullptr) { - HILOGE("WatcherCallback loop is nullptr"); + HILOGE("Failed to get uv event loop"); return; } uv_work_t *work = new (std::nothrow) uv_work_t; if (work == nullptr) { - HILOGE("WatcherCallback work is nullptr"); + HILOGE("Failed to create uv_work_t pointer"); return; } if (!callback) { - HILOGE("Pass watcher callback fail"); + HILOGE("Failed to pass watcher callback"); return; } @@ -134,42 +170,8 @@ void WatcherNExporter::WatcherCallback(napi_env env, work->data = reinterpret_cast(callbackContext); int ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int stat) { - if (work == nullptr) { - HILOGE("uv_queue_work w is nullptr"); - return; - } - - JSCallbackContext *callbackContext = reinterpret_cast(work->data); - do { - if (callbackContext == nullptr) { - HILOGE("callbackContext is null"); - break; - } - if (!callbackContext->ref_) { - HILOGE("callbackContext nref is null"); - break; - } - napi_env env = callbackContext->env_; - napi_value jsCallback = callbackContext->ref_.Deref(env).val_; - - napi_valuetype valueType; - napi_typeof(env, jsCallback, &valueType); - - NVal objn = NVal::CreateObject(env); - objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); - objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); - napi_value retVal = nullptr; - napi_status status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); - if (status != napi_ok) { - HILOGE("CallJs napi_call_function fail, status: %{public}d", status); - break; - } - } while (0); - delete callbackContext; - delete work; - }); + loop, work, [](uv_work_t *work) {}, reinterpret_cast(WatcherCallbackComplete)); + if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); delete callbackContext; -- Gitee From 0d1545dd33214878f1008ac2498bb513b99f6f0c Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Mon, 13 Feb 2023 17:21:01 +0800 Subject: [PATCH 13/21] modify file_watcher to watcher_entity Signed-off-by: Cao Chuan --- interfaces/kits/js/BUILD.gn | 2 +- .../src/mod_fs/class_watcher/file_watcher.h | 42 ------------------- .../{file_watcher.cpp => watcher_entity.cpp} | 2 +- .../src/mod_fs/class_watcher/watcher_entity.h | 23 ++++++++-- .../class_watcher/watcher_n_exporter.cpp | 15 +++---- .../mod_fs/class_watcher/watcher_n_exporter.h | 5 +-- .../kits/js/src/mod_fs/properties/watcher.cpp | 3 +- 7 files changed, 31 insertions(+), 61 deletions(-) delete mode 100644 interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h rename interfaces/kits/js/src/mod_fs/class_watcher/{file_watcher.cpp => watcher_entity.cpp} (98%) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 1b67878f5..e4d5cacc9 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -119,7 +119,7 @@ ohos_shared_library("fs") { "src/mod_fs/class_stat/stat_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", "src/mod_fs/class_stream/stream_n_exporter.cpp", - "src/mod_fs/class_watcher/file_watcher.cpp", + "src/mod_fs/class_watcher/watcher_entity.cpp", "src/mod_fs/class_watcher/watcher_n_exporter.cpp", "src/mod_fs/common_func.cpp", "src/mod_fs/module.cpp", diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h b/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h deleted file mode 100644 index 8160f1c33..000000000 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 FILE_WATCHER_H -#define FILE_WATCHER_H -#include -#include -#include - -#include "watcher_entity.h" -namespace OHOS::FileManagement::ModuleFileIO { -using WatcherCallback = void (*)(napi_env env, LibN::NRef &callback, - const std::string &filename, const uint32_t &event); -constexpr int BUF_SIZE = 1024; -class FileWatcher { -public: - FileWatcher(); - ~FileWatcher(); - bool InitNotify(int &fd); - bool StartNotify(WatcherInfoArg &arg); - bool StopNotify(const WatcherInfoArg &arg); - void GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback); - -private: - void HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, - WatcherCallback callback); - bool run_ = false; -}; -} // namespace OHOS::FileManagement::ModuleFileIO -#endif diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp similarity index 98% rename from interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp rename to interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index d6d33e3e8..edcc025cf 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/file_watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "file_watcher.h" +#include "watcher_entity.h" #include #include diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 81d6c9435..2b888630b 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -12,16 +12,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H #include #include +#include #include #include "filemgmt_libn.h" namespace OHOS::FileManagement::ModuleFileIO { - +using WatcherCallback = void (*)(napi_env env, LibN::NRef &callback, + const std::string &filename, const uint32_t &event); +constexpr int BUF_SIZE = 1024; struct WatcherInfoArg { std::string filename = ""; uint32_t events = 0; @@ -29,13 +31,28 @@ struct WatcherInfoArg { int wd = -1; napi_env env = nullptr; LibN::NRef nRef; - explicit WatcherInfoArg(LibN::NVal jsVal) : nRef(jsVal) {} ~WatcherInfoArg() = default; }; +class FileWatcher { +public: + FileWatcher(); + ~FileWatcher(); + bool InitNotify(int &fd); + bool StartNotify(WatcherInfoArg &arg); + bool StopNotify(const WatcherInfoArg &arg); + void GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback); + +private: + void HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, + WatcherCallback callback); + bool run_ = false; +}; + struct WatcherEntity { std::unique_ptr data_; + std::unique_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS #endif diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index cded49194..4a59b6cbc 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -20,16 +20,15 @@ #include #include +#include "../common_func.h" #include "filemgmt_libn.h" #include "filemgmt_libhilog.h" -#include "../common_func.h" #include "securec.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -std::unique_ptr WatcherNExporter::watcherPtr_ = nullptr; napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -43,8 +42,6 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat"); return nullptr; } - - watcherPtr_ = make_unique(); return funcArg.GetThisVar(); } @@ -61,7 +58,7 @@ napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); return nullptr; } - if (!watcherPtr_->StopNotify(*(watchEntity->data_))) { + if (!watchEntity->watcherPtr_->StopNotify(*(watchEntity->data_))) { NError(errno).ThrowErr(env); return nullptr; } @@ -83,13 +80,13 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) return nullptr; } - if (!watcherPtr_->StartNotify(*(watchEntity->data_))) { - NError(errno).ThrowErr(env); - return nullptr; + if (!watchEntity->watcherPtr_->StartNotify(*(watchEntity->data_))) { + NError(errno).ThrowErr(env); + return nullptr; } auto cbExec = [watchEntity]() -> NError { - watcherPtr_->GetNotifyEvent(*(watchEntity->data_), WatcherCallback); + watchEntity->watcherPtr_->GetNotifyEvent(*(watchEntity->data_), WatcherCallback); return NError(ERRNO_NOERR); }; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index 9b44160c9..4bb21a9d5 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -15,12 +15,10 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_N_EXPORTER_H -#include #include -#include "watcher_entity.h" -#include "file_watcher.h" #include "filemgmt_libn.h" +#include "watcher_entity.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace OHOS::FileManagement::LibN; class WatcherNExporter final : public NExporter { @@ -51,7 +49,6 @@ public: ~WatcherNExporter() override; private: - static std::unique_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO #endif diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 1a5a5cb15..9f9e2703f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -22,7 +22,6 @@ #include #include "filemgmt_libhilog.h" -#include "../class_watcher/file_watcher.h" #include "../class_watcher/watcher_entity.h" #include "../class_watcher/watcher_n_exporter.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -73,6 +72,8 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) watcherEntity->data_->filename = string(filename.get()); watcherEntity->data_->fd = fd; + watcherEntity->watcherPtr_ = std::move(watcherPtr); + return objWatcher; } } // namespace OHOS::FileManagement::ModuleFileIO -- Gitee From 984a332b80fdcfd16a81b126b607069396c6a0ba Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Tue, 14 Feb 2023 08:54:43 +0800 Subject: [PATCH 14/21] modify cast Signed-off-by: Cao Chuan --- .../kits/js/src/mod_fs/class_watcher/watcher_entity.cpp | 2 +- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index edcc025cf..4817bf4ee 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -82,7 +82,7 @@ void FileWatcher::GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback) int len, index = 0; while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR)) {}; while (index < len) { - event = (struct inotify_event *)(buf + index); + event = reinterpret_cast(buf + index); HandleEvent(arg, event, callback); index += sizeof(struct inotify_event) + event->len; } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 4a59b6cbc..8d17ad2b6 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -81,8 +81,8 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) } if (!watchEntity->watcherPtr_->StartNotify(*(watchEntity->data_))) { - NError(errno).ThrowErr(env); - return nullptr; + NError(errno).ThrowErr(env); + return nullptr; } auto cbExec = [watchEntity]() -> NError { @@ -168,7 +168,6 @@ void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std:: int ret = uv_queue_work( loop, work, [](uv_work_t *work) {}, reinterpret_cast(WatcherCallbackComplete)); - if (ret != 0) { HILOGE("Failed to execute libuv work queue, ret: %{public}d", ret); delete callbackContext; -- Gitee From d05f426a098dbf2f8f0f439ada5bf9ea40304b72 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Tue, 14 Feb 2023 09:07:49 +0800 Subject: [PATCH 15/21] del error message Signed-off-by: Cao Chuan --- .../mod_fs/class_watcher/watcher_n_exporter.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 8d17ad2b6..8e2854119 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -33,13 +33,13 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } unique_ptr watcherEntity = make_unique(); if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { - NError(EIO).ThrowErr(env, "INNER BUG. Failed to wrap entity for obj stat"); + NError(EIO).ThrowErr(env); return nullptr; } return funcArg.GetThisVar(); @@ -49,13 +49,13 @@ napi_value WatcherNExporter::Stop(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!watchEntity) { - NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + NError(EINVAL).ThrowErr(env); return nullptr; } if (!watchEntity->watcherPtr_->StopNotify(*(watchEntity->data_))) { @@ -70,13 +70,13 @@ napi_value WatcherNExporter::Start(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto watchEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); if (!watchEntity) { - NError(EINVAL).ThrowErr(env, "get watcherEntity fail"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -186,13 +186,13 @@ bool WatcherNExporter::Export() auto [resDefineClass, classValue] = NClass::DefineClass(exports_.env_, className, WatcherNExporter::Constructor, std::move(props)); if (!resDefineClass) { - NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to define class"); + NError(EIO).ThrowErr(exports_.env_); return false; } bool succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { - NError(EIO).ThrowErr(exports_.env_, "INNER BUG. Failed to save class"); + NError(EIO).ThrowErr(exports_.env_); return false; } -- Gitee From c807095338aa67dd8db7bbe8d273c44771d62c12 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Tue, 14 Feb 2023 09:34:18 +0800 Subject: [PATCH 16/21] del error message Signed-off-by: Cao Chuan --- interfaces/kits/js/src/mod_fs/properties/watcher.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 9f9e2703f..b8b255ac0 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -31,19 +31,19 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::THREE)) { - NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto [succGetPath, filename, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succGetPath) { - NError(EINVAL).ThrowErr(env, "Invalid filename"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto [succGetEvent, events] = NVal(env, funcArg[NARG_POS::SECOND]).ToUint32(); if (!succGetEvent) { - NError(EINVAL).ThrowErr(env, "Invalid event"); + NError(EINVAL).ThrowErr(env); return nullptr; } @@ -56,13 +56,13 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); if (!objWatcher) { - NError(EINVAL).ThrowErr(env, "objWatcher create failed"); + NError(EINVAL).ThrowErr(env); return nullptr; } auto watcherEntity = NClass::GetEntityOf(env, objWatcher); if (!watcherEntity) { - NError(EINVAL).ThrowErr(env, "watcherEntity get failed"); + NError(EINVAL).ThrowErr(env); return nullptr; } -- Gitee From 40b6267dacb06bcaa8e2c7c4ed09d3d576a51981 Mon Sep 17 00:00:00 2001 From: caochuan Date: Tue, 14 Feb 2023 02:31:17 +0000 Subject: [PATCH 17/21] Modify copyright and remove code no used Signed-off-by: caochuan --- .../kits/js/src/mod_fs/class_watcher/watcher_entity.h | 2 +- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp | 6 +----- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 2b888630b..75fe22161 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 8e2854119..1e2051a28 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 @@ -122,10 +122,6 @@ static void WatcherCallbackComplete(uv_work_t *work, int stat) } napi_env env = callbackContext->env_; napi_value jsCallback = callbackContext->ref_.Deref(env).val_; - - napi_valuetype valueType; - napi_typeof(env, jsCallback, &valueType); - NVal objn = NVal::CreateObject(env); objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index 4bb21a9d5..b9eb34f3f 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * 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 -- Gitee From cfae90fa1fed3928c0c1feaeb31b04ea817620cf Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Tue, 14 Feb 2023 15:59:16 +0800 Subject: [PATCH 18/21] modify header Signed-off-by: Cao Chuan --- interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 75fe22161..ed17c7565 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -14,10 +14,10 @@ */ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_CLASS_WATCHER_WATCHER_ENTITY_H -#include +#include #include #include -#include +#include #include "filemgmt_libn.h" namespace OHOS::FileManagement::ModuleFileIO { -- Gitee From b05adabc1826bde2285e0658e4ef805ed53571e7 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Mon, 20 Feb 2023 15:58:23 +0800 Subject: [PATCH 19/21] modify code format Signed-off-by: Cao Chuan --- utils/filemgmt_libn/src/n_val.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index cc40313ab..11fb78aab 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -138,7 +138,6 @@ tuple NVal::ToUint32() const return make_tuple(status == napi_ok, res); } - tuple NVal::ToUint64() const { uint64_t res = 0; -- Gitee From 8f4c297216c697f8a0b396aa406cfbb8b1bad17d Mon Sep 17 00:00:00 2001 From: caochuan Date: Fri, 24 Feb 2023 05:25:36 +0000 Subject: [PATCH 20/21] Add cookie for callback Signed-off-by: caochuan --- .../kits/js/src/mod_fs/class_watcher/watcher_entity.cpp | 2 +- .../kits/js/src/mod_fs/class_watcher/watcher_entity.h | 7 +++++-- .../js/src/mod_fs/class_watcher/watcher_n_exporter.cpp | 5 ++++- .../kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h | 4 +++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index 4817bf4ee..a62293e60 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -66,7 +66,7 @@ void FileWatcher::HandleEvent(WatcherInfoArg &arg, return; } std::string filename = arg.filename + "/" + event->name; - callback(arg.env, arg.nRef, filename, event->mask); + callback(arg.env, arg.nRef, filename, event->mask, event->cookie); } void FileWatcher::GetNotifyEvent(WatcherInfoArg &arg, WatcherCallback callback) diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index ed17c7565..42ddf7bee 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -21,8 +21,11 @@ #include "filemgmt_libn.h" namespace OHOS::FileManagement::ModuleFileIO { -using WatcherCallback = void (*)(napi_env env, LibN::NRef &callback, - const std::string &filename, const uint32_t &event); +using WatcherCallback = void (*)(napi_env env, + LibN::NRef &callback, + const std::string &filename, + const uint32_t &event, + const uint32_t &cookie); constexpr int BUF_SIZE = 1024; struct WatcherInfoArg { std::string filename = ""; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 1e2051a28..38c5b82b2 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -125,6 +125,7 @@ static void WatcherCallbackComplete(uv_work_t *work, int stat) NVal objn = NVal::CreateObject(env); objn.AddProp("fileName", NVal::CreateUTF8String(env, callbackContext->fileName_).val_); objn.AddProp("event", NVal::CreateUint32(env, callbackContext->event_).val_); + objn.AddProp("cookie", NVal::CreateUint32(env, callbackContext->cookie_).val_); napi_value retVal = nullptr; napi_status status = napi_call_function(env, nullptr, jsCallback, 1, &(objn.val_), &retVal); if (status != napi_ok) { @@ -136,7 +137,8 @@ static void WatcherCallbackComplete(uv_work_t *work, int stat) delete work; } -void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event) +void WatcherNExporter::WatcherCallback(napi_env env, NRef & callback, const std::string &fileName, + const uint32_t &event, const uint32_t &cookie) { uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); @@ -160,6 +162,7 @@ void WatcherNExporter::WatcherCallback(napi_env env, NRef &callback, const std:: callbackContext->env_ = env; callbackContext->fileName_ = fileName; callbackContext->event_ = event; + callbackContext->cookie_ = cookie; work->data = reinterpret_cast(callbackContext); int ret = uv_queue_work( diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index b9eb34f3f..54dbd91b8 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h @@ -33,6 +33,7 @@ public: NRef &ref_; std::string fileName_; uint32_t event_; + uint32_t cookie_; }; inline static const std::string className_ = "Watcher"; @@ -43,7 +44,8 @@ public: static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value Start(napi_env env, napi_callback_info info); static napi_value Stop(napi_env env, napi_callback_info info); - static void WatcherCallback(napi_env env, NRef &callback, const std::string &fileName, const uint32_t &event); + static void WatcherCallback(napi_env env, NRef & callback, const std::string &fileName, const uint32_t &event, + const uint32_t &cookie); WatcherNExporter(napi_env env, napi_value exports); ~WatcherNExporter() override; -- Gitee From 6e4afcb40039d683dc88c29d7498758b88053384 Mon Sep 17 00:00:00 2001 From: Cao Chuan Date: Fri, 24 Feb 2023 13:46:16 +0800 Subject: [PATCH 21/21] modify code review Signed-off-by: Cao Chuan --- interfaces/kits/js/BUILD.gn | 1 + .../mod_fs/class_watcher/watcher_entity.cpp | 11 +++++---- .../src/mod_fs/class_watcher/watcher_entity.h | 2 +- .../kits/js/src/mod_fs/properties/watcher.cpp | 23 ++++++++++++++++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e4d5cacc9..cc1bcf303 100755 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -150,6 +150,7 @@ ohos_shared_library("fs") { external_deps = [ "ability_base:zuri", "ability_runtime:abilitykit_native", + "access_token:libtokenid_sdk", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp index a62293e60..1078fb821 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.cpp @@ -29,7 +29,7 @@ bool FileWatcher::InitNotify(int &fd) { fd = inotify_init(); if (fd == -1) { - HILOGE("FileWatcher InitNotify fail errCode:%{public}d", errno); + HILOGE("Failed to init notify fail errCode:%{public}d", errno); return false; } return true; @@ -39,7 +39,7 @@ bool FileWatcher::StartNotify(WatcherInfoArg &arg) { int wd = inotify_add_watch(arg.fd, arg.filename.c_str(), arg.events); if (wd == -1) { - HILOGE("FileWatcher StartNotify fail errCode:%{public}d", errno); + HILOGE("Failed to start notify fail errCode:%{public}d", errno); return false; } arg.wd = wd; @@ -51,7 +51,7 @@ bool FileWatcher::StopNotify(const WatcherInfoArg &arg) { run_ = false; if (inotify_rm_watch(arg.fd, arg.wd) == -1) { - HILOGE("FileWatcher StopNotify fail errCode:%{public}d", errno); + HILOGE("Failed to stop notify fail errCode:%{public}d", errno); return false; } close(arg.fd); @@ -65,7 +65,10 @@ void FileWatcher::HandleEvent(WatcherInfoArg &arg, if (event->wd != arg.wd) { return; } - std::string filename = arg.filename + "/" + event->name; + std::string filename = arg.filename; + if ((event->name)[0] ! = '\0') { + filename += event->name; + } callback(arg.env, arg.nRef, filename, event->mask, event->cookie); } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h index 42ddf7bee..fad6ec27a 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_entity.h @@ -55,7 +55,7 @@ private: struct WatcherEntity { std::unique_ptr data_; - std::unique_ptr watcherPtr_; + std::shared_ptr watcherPtr_; }; } // namespace OHOS::FileManagement::ModuleFileIO namespace OHOS #endif diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index b8b255ac0..37e2d83ab 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -20,15 +20,32 @@ #include #include #include - +#include "ipc_skeleton.h" #include "filemgmt_libhilog.h" +#include "tokenid_kit.h" #include "../class_watcher/watcher_entity.h" #include "../class_watcher/watcher_n_exporter.h" namespace OHOS::FileManagement::ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; + +namespace { + const std::string STORAGE_DATA_PATH = "/data"; + bool IsSystemApp() + { + uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId); + } +} + + napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) { + if (!IsSystemApp()) { + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::THREE)) { NError(EINVAL).ThrowErr(env); @@ -48,7 +65,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - unique_ptr watcherPtr = make_unique(); + shared_ptr watcherPtr = make_shared(); if (!watcherPtr->InitNotify(fd)) { NError(errno).ThrowErr(env); return nullptr; @@ -72,7 +89,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) watcherEntity->data_->filename = string(filename.get()); watcherEntity->data_->fd = fd; - watcherEntity->watcherPtr_ = std::move(watcherPtr); + watcherEntity->watcherPtr_ = watcherPtr; return objWatcher; } -- Gitee