OCT 1. 增加 文件/目录 改变监视功能

This commit is contained in:
huangxin 2023-02-10 09:03:01 +08:00
parent 342741d175
commit 445a82aa8f
4 changed files with 132 additions and 0 deletions

View File

@ -23,6 +23,7 @@ AUX_SOURCE_DIRECTORY(cmdline C_SRC)
AUX_SOURCE_DIRECTORY(crypto C_SRC) AUX_SOURCE_DIRECTORY(crypto C_SRC)
AUX_SOURCE_DIRECTORY(hardware C_SRC) AUX_SOURCE_DIRECTORY(hardware C_SRC)
AUX_SOURCE_DIRECTORY(protocol C_SRC) AUX_SOURCE_DIRECTORY(protocol C_SRC)
AUX_SOURCE_DIRECTORY(fs_watch C_SRC)
AUX_SOURCE_DIRECTORY(zlog_module C_SRC) AUX_SOURCE_DIRECTORY(zlog_module C_SRC)
IF (USED_REDIS) IF (USED_REDIS)

106
srcs/libs/fs_watch/fs_mon.c Normal file
View File

@ -0,0 +1,106 @@
//
// Created by xajhuang on 2023/2/9.
//
#include <uv.h>
#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 : "<NULL>", 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 : "<NULL>");
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;
}

View File

@ -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

View File

@ -14,6 +14,7 @@ extern "C" {
ZLOG_MOD(ZLOG_MOD_TASK, "TASK") \ ZLOG_MOD(ZLOG_MOD_TASK, "TASK") \
ZLOG_MOD(ZLOG_MOD_INIT, "INIT") \ ZLOG_MOD(ZLOG_MOD_INIT, "INIT") \
ZLOG_MOD(ZLOG_MOD_MISC, "MISC") \ ZLOG_MOD(ZLOG_MOD_MISC, "MISC") \
ZLOG_MOD(ZLOG_MOD_WATCH, "WATCH") \
ZLOG_MOD(ZLOG_MOD_CONFIG, "CONFIG") \ ZLOG_MOD(ZLOG_MOD_CONFIG, "CONFIG") \
ZLOG_MOD(ZLOG_MOD_NET, "NET") \ ZLOG_MOD(ZLOG_MOD_NET, "NET") \
ZLOG_MOD(ZLOG_MOD_CRYPTO, "CRYPTO") \ ZLOG_MOD(ZLOG_MOD_CRYPTO, "CRYPTO") \