这听起来像是 inotify 的一个很好的用例。 man page 有一个很好的例子,很容易转移到你的问题上。
这是一个小程序,可以像这样使用
$ myprog /tmp '*.o' '*.a'
查看目录/tmp 以创建*.o 和*.a 文件。请注意,引用模式是为了防止外壳扩展。程序永远运行,直到被SIGINT 中断(按Ctrl + C)。
我正在使用fnmatch 将创建的文件的名称与模式进行匹配,并为SIGINT 安装一个设置全局标志的信号处理程序。
#include <assert.h>
#include <errno.h>
#include <fnmatch.h>
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
我使用 GCC 的 __attribute__ 扩展定义了一个小的预处理器宏来对齐缓冲区。稍后我们实际使用此宏时会详细介绍。
#ifdef __GNUC__
# define ALIGNAS(TYPE) __attribute__ ((aligned(__alignof__(TYPE))))
#else
# define ALIGNAS(TYPE) /* empty */
#endif
这是一个全局标志,我们将在信号处理程序中设置它以指示程序应该正常退出。
static volatile int interrupted = 0;
这是信号处理程序本身。
static void
interruption_handler(const int s)
{
if (s == SIGINT)
interrupted = 1;
}
这个函数是程序的主力。
static int
monitor_directory(const char *const directory,
const char *const *const patterns,
const size_t pattern_count)
{
int notifyfd = -1;
int watchfd = -1;
int ret = 0;
const char * errmsg = "unknown error";
首先,我们初始化inotify。 inotify_init 将返回一个文件描述符,我们可以从中获得 read() 通知。我使用阻塞 I/O,所以 read() 将阻塞直到事件发生。
notifyfd = inotify_init();
if (notifyfd < 0)
{
errmsg = "inotify_init";
goto catch;
}
现在我们注册要观看的文件。在我们的例子中,我们希望观察一个目录 (directory) 以创建新文件 (IN_CREATE)。返回的文件描述符可用于告诉(如果发生事件)它属于哪个监视文件。但由于我们只观察一个文件(恰好是一个目录),所以我们并不需要这些信息。
watchfd = inotify_add_watch(notifyfd, directory, IN_CREATE);
if (watchfd < 0)
{
errmsg = "inotify_add_watch";
goto catch;
}
现在一切都设置好了,我们可以从通知文件描述符开始read()ing。
while (1)
{
事先不知道从 inotify 描述符调用read 会读取多少,所以我们必须读入char 缓冲区。我们希望适当地调整它,艰难的。有关此内容的更多 cmets,请参见手册页。
char buffer[sizeof(struct inotify_event) + NAME_MAX + 1] ALIGNAS(struct inotify_event);
const struct inotify_event * event_ptr;
read() 来自文件描述符。如果我们被打断,read() 将解除阻塞并返回 –1,就像发生错误时一样。
ssize_t count = read(notifyfd, buffer, sizeof(buffer));
if (count < 0)
{
if (interrupted)
goto finally;
errmsg = "read";
goto catch;
}
我们有一个新事件,处理它。
event_ptr = (const struct inotify_event *) buffer;
assert(event_ptr->wd == watchfd);
assert(event_ptr->mask & IN_CREATE);
if (event_ptr->len)
{
size_t i;
尝试将文件名与我们的每个模式匹配。
for (i = 0; i < pattern_count; ++i)
{
switch (fnmatch(patterns[i], event_ptr->name, FNM_PATHNAME))
{
case 0:
/* Your application logic here... */
if (printf("%s\n", event_ptr->name) < 0)
{
errmsg = "printf";
goto catch;
}
break;
case FNM_NOMATCH:
break;
default:
errmsg = "fnmatch";
goto catch;
}
}
}
}
最后,我们必须做一些清理工作。 close()ing 由 inotify 创建的文件描述符将导致它释放任何关联的资源。
finally:
if (watchfd >= 0)
{
int status = close(watchfd);
watchfd = -1;
if (status < 0)
{
errmsg = "close(watchfd)";
goto catch;
}
}
if (notifyfd >= 0)
{
int status = close(notifyfd);
notifyfd = -1;
if (status < 0)
{
errmsg = "close(notifyfd)";
goto catch;
}
}
return ret;
catch:
if (errmsg && errno)
perror(errmsg);
ret = -1;
goto finally;
}
这就是我们将所有内容连接在一起并运行程序的方式。
int
main(const int argc, const char *const *const argv)
{
if (argc < 3)
{
fprintf(stderr, "usage: %s DIRECTORY PATTERN...\n", argv[0]);
return EXIT_FAILURE;
}
{
struct sigaction sa;
sa.sa_handler = interruption_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
}
if (monitor_directory(argv[1], argv + 2, argc - 2) < 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}