関連投稿
libuvに関する覚書(1) : タイマー uv_timer_t
libuvに関する覚書(3) : パイプ uv_pipe_t
libuvに関する覚書(4) : スレッド uv_work_t, uv_async_t
uv_fs_event_start
の引数で監視するタイプを指定する。サンプルではUV_FS_EVENT_STAT
を指定して、特定のファイルを監視する内容になる。
uv_fs_event_t — FS Event handle
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <uv.h>
uv_loop_t *loop;
uv_fs_event_t fs_event_req;
void callback_fsevent(uv_fs_event_t *handle, const char *filename, int events, int status) {
// modified : events=2, rename or delete : events = 1
// ok: status = 0
printf("callback_fsevent. filename=%s, events=%d, status=%d\n", filename, events, status);
}
// uv_fs_event_t sample
int main(int argc, char**argv){
loop = uv_default_loop();
uv_fs_event_init(loop, &fs_event_req);
//UV_FS_EVENT_STAT UV_FS_EVENT_RECURSIVE
uv_fs_event_start(&fs_event_req, callback_fsevent, argv[1], UV_FS_EVENT_STAT);
uv_run(loop, UV_RUN_DEFAULT);
}
サンプルでは、引数で監視するファイルパスを指定する。
> clang -luv fsevent.c -o fsevent
> ./fsevent test.txt
別のコンソールから、ファイルの内容を変更してみる。
> echo hoge > test.txt
ファイルを変更すると、そのファイルパスとevents=2が返る。
./fsevent test.txt
callback_fsevent. filename=test.txt, events=2, status=0
callback_fsevent. filename=test.txt, events=2, status=0
> rm test.txt
削除やファイル名を変更した場合は、events=1が返る。
./fsevent test.txt
callback_fsevent. filename=test.txt, events=1, status=0
callback_fsevent. filename=test.txt, events=1, status=0