这听起来像是inotify 的一个很好的用例。该手册页具有容易转移到你的问题的一个很好的例子。
这是一个小程序,可以像
$ myprog /tmp '*.o' '*.a'
Run Code Online (Sandbox Code Playgroud)
观看目录/tmp以创建*.o和*.a文件。请注意,引用模式是为了防止 shell 扩展。程序将永远运行,直到被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>
Run Code Online (Sandbox Code Playgroud)
我定义了一个小的预处理器宏来使用 GCC 的__attribute__扩展来对齐缓冲区。稍后我们实际使用这个宏时会详细介绍。
#ifdef __GNUC__
# define ALIGNAS(TYPE) __attribute__ ((aligned(__alignof__(TYPE))))
#else
# define ALIGNAS(TYPE) /* empty */
#endif
Run Code Online (Sandbox Code Playgroud)
这是一个全局标志,我们将在信号处理程序中设置它以指示程序应该正常退出。
static volatile int interrupted = 0;
Run Code Online (Sandbox Code Playgroud)
这是信号处理程序本身。
static void
interruption_handler(const int s)
{
if (s == SIGINT)
interrupted = 1;
}
Run Code Online (Sandbox Code Playgroud)
这个函数是程序的主力。
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";
Run Code Online (Sandbox Code Playgroud)
首先,我们初始化inotify。 inotify_init将返回一个文件描述符,我们可以从中read() 通知。我使用阻塞 I/O,所以read()会阻塞直到事件发生。
notifyfd = inotify_init();
if (notifyfd < 0)
{
errmsg = "inotify_init";
goto catch;
}
Run Code Online (Sandbox Code Playgroud)
现在我们注册要观看的文件。在我们的例子中,我们希望观察单个目录 ( directory) 以创建新文件 ( IN_CREATE)。返回的文件描述符可用于告诉(如果发生事件)它属于哪个监视文件。但是因为我们只观察一个文件(恰好是一个目录),所以我们真的不需要这个信息。
watchfd = inotify_add_watch(notifyfd, directory, IN_CREATE);
if (watchfd < 0)
{
errmsg = "inotify_add_watch";
goto catch;
}
Run Code Online (Sandbox Code Playgroud)
现在一切都设置正确,我们可以read()从通知文件描述符开始。
while (1)
{
Run Code Online (Sandbox Code Playgroud)
事先不知道read从inotify描述符中调用多少会读取,因此我们必须读入char缓冲区。我们希望正确地对齐它,强硬。有关更多评论,请参阅手册页。
char buffer[sizeof(struct inotify_event) + NAME_MAX + 1] ALIGNAS(struct inotify_event);
const struct inotify_event * event_ptr;
Run Code Online (Sandbox Code Playgroud)
read()从文件描述符。如果我们被打断,read()它将解除阻塞并返回-1,就像发生错误时一样。
ssize_t count = read(notifyfd, buffer, sizeof(buffer));
if (count < 0)
{
if (interrupted)
goto finally;
errmsg = "read";
goto catch;
}
Run Code Online (Sandbox Code Playgroud)
我们有一个新事件,处理它。
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;
Run Code Online (Sandbox Code Playgroud)
尝试将文件名与我们的每个模式进行匹配。
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我们必须做一些清理工作。 close()使用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;
}
Run Code Online (Sandbox Code Playgroud)
这就是我们将所有东西连接在一起并运行程序的方式。
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;
}
Run Code Online (Sandbox Code Playgroud)