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 a8b24e3fe21f9d041d8f2519c3cf9c6ada43c18a..1500dfcfd4f9c268bbe8370d32799e60c91878da 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 @@ -50,6 +50,10 @@ bool FileWatcher::StartNotify(WatcherInfoArg &arg) bool FileWatcher::StopNotify(const WatcherInfoArg &arg) { run_ = false; + if (arg.events == IN_DELETE_SELF) { + close(arg.fd); + return true; + } if (inotify_rm_watch(arg.fd, arg.wd) == -1) { HILOGE("Failed to stop notify fail errCode:%{public}d", errno); return false; @@ -58,18 +62,17 @@ bool FileWatcher::StopNotify(const WatcherInfoArg &arg) return true; } -void FileWatcher::HandleEvent(WatcherInfoArg &arg, - const struct inotify_event *event, - WatcherCallback callback) +void FileWatcher::HandleEvent(WatcherInfoArg &arg, const struct inotify_event *event, WatcherCallback callback) { if (event->wd != arg.wd) { return; } - std::string filename = arg.filename; - if ((event->name)[0] != '\0') { - filename += event->name; + std::string fileName = arg.filename; + if (event->len > 0) { + fileName.append(std::string("/")); + fileName.append(std::string(event->name)); } - callback(arg.env, arg.nRef, filename, event->mask, event->cookie); + 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_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.h index 54dbd91b85f9a27c5a89abbcfae32cd7db06231d..077e90ba4437c9969fec8895d6abf57d76b46df2 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 @@ -44,7 +44,7 @@ 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); diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 985ae117f90492b1afea42ee5b188999474458e7..45e72c26484236bcce03978698f346201ec53dc4 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -82,6 +82,11 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return nullptr; } + if (!NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); watcherEntity->data_->events = events; watcherEntity->data_->env = env;