From 445a82aa8f38601718dd5f629a4c3ad0e8634336 Mon Sep 17 00:00:00 2001 From: huangxin Date: Fri, 10 Feb 2023 09:03:01 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E5=A2=9E=E5=8A=A0=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6/=E7=9B=AE=E5=BD=95=20=E6=94=B9=E5=8F=98=E7=9B=91?= =?UTF-8?q?=E8=A7=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/CMakeLists.txt | 1 + srcs/libs/fs_watch/fs_mon.c | 106 ++++++++++++++++++++++++++++++++ srcs/libs/include/fs_watch.h | 24 ++++++++ srcs/libs/include/zlog_module.h | 1 + 4 files changed, 132 insertions(+) create mode 100644 srcs/libs/fs_watch/fs_mon.c create mode 100644 srcs/libs/include/fs_watch.h diff --git a/srcs/libs/CMakeLists.txt b/srcs/libs/CMakeLists.txt index 3ad2f70..9cd29fe 100644 --- a/srcs/libs/CMakeLists.txt +++ b/srcs/libs/CMakeLists.txt @@ -23,6 +23,7 @@ AUX_SOURCE_DIRECTORY(cmdline C_SRC) AUX_SOURCE_DIRECTORY(crypto C_SRC) AUX_SOURCE_DIRECTORY(hardware C_SRC) AUX_SOURCE_DIRECTORY(protocol C_SRC) +AUX_SOURCE_DIRECTORY(fs_watch C_SRC) AUX_SOURCE_DIRECTORY(zlog_module C_SRC) IF (USED_REDIS) diff --git a/srcs/libs/fs_watch/fs_mon.c b/srcs/libs/fs_watch/fs_mon.c new file mode 100644 index 0000000..dce9ce0 --- /dev/null +++ b/srcs/libs/fs_watch/fs_mon.c @@ -0,0 +1,106 @@ +// +// Created by xajhuang on 2023/2/9. +// +#include +#include "uthash/uthash.h" +#include "misc.h" +#include "user_errno.h" +#include "zlog_module.h" +#include "task_manager.h" +#include "fs_watch.h" + +typedef struct { + char watchPath[MAX_PATH]; + watch_cb pCb; + + uv_fs_event_t *uv_event; + UT_hash_handle hh; ///< UT Hash handle +} WATCH_CONTENT, *PWATCH_CONTENT; + +PWATCH_CONTENT g_watchContent = NULL; + +static void monitor_cb(uv_fs_event_t *handle, const char *filename, int events, int status) { + PWATCH_CONTENT pCtx = (PWATCH_CONTENT)handle->data; + char path[1024]; + size_t size = 1023; + + uv_fs_event_getpath(handle, path, &size); + path[size] = '\0'; + + if (pCtx && pCtx->pCb) { + pCtx->pCb(path, filename, events, pCtx->watchPath); + } +#if 1 + fprintf(stderr, "Change detected in %s: ", path); + if (events & UV_RENAME) { + fprintf(stderr, "renamed"); + } + if (events & UV_CHANGE) { + fprintf(stderr, "changed"); + } + + fprintf(stderr, " %s\n", filename ? filename : ""); +#endif +} + +int add_watch_path(const char *pFsPath, watch_cb cb) { + PWATCH_CONTENT pCtx; + + if (cb == NULL || pFsPath == NULL || !file_exists(pFsPath)) { + LOG_MOD(debug, ZLOG_MOD_WATCH, "Watch path [%s] notify function <%p>\n", pFsPath ? pFsPath : "", cb); + return -ERR_INPUT_PARAMS; + } + + HASH_FIND_STR(g_watchContent, pFsPath, pCtx); + + if (pCtx != NULL) { + return -ERR_ITEM_EXISTS; + } + + pCtx = (PWATCH_CONTENT)malloc(sizeof(WATCH_CONTENT)); + + if (pCtx == NULL) { + LOG_MOD(error, ZLOG_MOD_WATCH, "Malloc memory error of %ld size\n", sizeof(WATCH_CONTENT)); + return -ERR_MALLOC_MEMORY; + } + + pCtx->uv_event = (uv_fs_event_t *)malloc(sizeof(uv_fs_event_t)); + + if (pCtx->uv_event == NULL) { + LOG_MOD(error, ZLOG_MOD_WATCH, "Malloc memory error of %ld size\n", sizeof(uv_fs_event_t)); + free(pCtx); + return -ERR_MALLOC_MEMORY; + } + + memset(pCtx, 0, sizeof(WATCH_CONTENT)); + + strcpy(pCtx->watchPath, pFsPath); + pCtx->pCb = cb; + pCtx->uv_event->data = pCtx; + + uv_fs_event_init(get_task_manager(), pCtx->uv_event); + uv_fs_event_start(pCtx->uv_event, monitor_cb, pFsPath, UV_FS_EVENT_RECURSIVE); + + HASH_ADD_STR(g_watchContent, watchPath, pCtx); + return ERR_SUCCESS; +} + +int remove_watch_path(const char *pFsPath) { + PWATCH_CONTENT pCtx; + + if (pFsPath == NULL || !file_exists(pFsPath)) { + LOG_MOD(debug, ZLOG_MOD_WATCH, "Remove Watch path [%s]\n", pFsPath ? pFsPath : ""); + return -ERR_INPUT_PARAMS; + } + + HASH_FIND_STR(g_watchContent, pFsPath, pCtx); + + if (pCtx != NULL) { + HASH_DEL(g_watchContent, pCtx); + uv_fs_event_stop(pCtx->uv_event); + free(pCtx->uv_event); + free(pCtx); + } + + return ERR_SUCCESS; +} \ No newline at end of file diff --git a/srcs/libs/include/fs_watch.h b/srcs/libs/include/fs_watch.h new file mode 100644 index 0000000..d3db3fc --- /dev/null +++ b/srcs/libs/include/fs_watch.h @@ -0,0 +1,24 @@ +// +// Created by xajhuang on 2023/2/9. +// + +#ifndef VCPE_FS_WATCH_H +#define VCPE_FS_WATCH_H +#ifdef __cplusplus +extern "C" { +#endif +/** + * + * @param pPath 发生改变的文件路径 + * @param filename 发生改变的文件名 + * @param events 发生改变的类型 @see uv_fs_event {UV_RENAME = 1, UV_CHANGE = 2} + * @param pWatch 监视的路径 + */ +typedef void (*watch_cb)(const char *pPath, const char *filename, int events, const char *pWatch); + +int add_watch_path(const char *pFsPath, watch_cb cb); +int remove_watch_path(const char *pFsPath); +#ifdef __cplusplus +} +#endif +#endif //VCPE_FS_WATCH_H diff --git a/srcs/libs/include/zlog_module.h b/srcs/libs/include/zlog_module.h index 05f01dc..509e11a 100644 --- a/srcs/libs/include/zlog_module.h +++ b/srcs/libs/include/zlog_module.h @@ -14,6 +14,7 @@ extern "C" { ZLOG_MOD(ZLOG_MOD_TASK, "TASK") \ ZLOG_MOD(ZLOG_MOD_INIT, "INIT") \ ZLOG_MOD(ZLOG_MOD_MISC, "MISC") \ + ZLOG_MOD(ZLOG_MOD_WATCH, "WATCH") \ ZLOG_MOD(ZLOG_MOD_CONFIG, "CONFIG") \ ZLOG_MOD(ZLOG_MOD_NET, "NET") \ ZLOG_MOD(ZLOG_MOD_CRYPTO, "CRYPTO") \