
在Linux系统中,readdir 函数是一种常用的系统调用,用于从目录中读取条目。若要实现目录监控,可以结合 readdir 与其他Linux特性,比如 inotify。以下是一个简化的实例,展示了如何利用 readdir 和 inotify 来监控目录的变化。
readdir 读取目录内容首先,让我们看看如何使用 readdir 来读取目录中的内容:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
<p>int main(int argc, char <em>argv[]) {
DIR </em>dir;
struct dirent *entry;</p><pre class="brush:php;toolbar:false;"><code>if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>", argv[0]); return EXIT_FAILURE; }
<code>dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s</code>", entry->d_name); }
<code>closedir(dir); return EXIT_SUCCESS;</code>
}
inotify 监控目录inotify 是Linux内核提供的一个功能,它允许用户空间的应用程序监控文件系统事件。通过 inotify 可以监控目录的变化,并在有变动时采取相应的操作。
下面的例子展示了如何使用 inotify 来监控目录的变化:
#include <stdio.h></p><h1>include <stdlib.h></h1><h1>include <string.h></h1><h1>include <sys/inotify.h></h1><h1>include <unistd.h></h1><h1>define EVENT_SIZE (sizeof(struct inotify_event))</h1><h1>define BUF_LEN (1024 * (EVENT_SIZE + 16))</h1><p>int main(int argc, char *argv[]) {
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];</p><pre class="brush:php;toolbar:false;"><code>if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>", argv[0]); return EXIT_FAILURE; }
<code>fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
return EXIT_FAILURE;
}
wd = inotify_add_watch(fd, argv[1], IN_CREATE | IN_DELETE | IN_MODIFY);
if (wd < 0) {
perror("inotify_add_watch");
return EXIT_FAILURE;
}
while (1) {
length = read(fd, buffer, BUF_LEN);
if (length < 0) {
perror("read");
break;
}
for (i = 0; i < length; ) {
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len) {
if (event->mask & IN_CREATE) {
printf("File %s created.</code>", event->name); } else if (event->mask & IN_DELETE) { printf("File %s deleted. ", event->name); } else if (event->mask & IN_MODIFY) { printf("File %s modified. ", event->name); } } i += EVENT_SIZE + event->len; } i = 0; }
<code>(void) inotify_rm_watch(fd, wd); (void) close(fd); return EXIT_SUCCESS;</code>
}
readdir 和 inotify
你还可以将 readdir 和 inotify 结合起来,创建一个更复杂的目录监控解决方案。例如,在检测到目录发生变化时,重新读取目录内容并显示变化。
#include <stdio.h></p><h1>include <stdlib.h></h1><h1>include <string.h></h1><h1>include <sys/inotify.h></h1><h1>include <unistd.h></h1><h1>include <dirent.h></h1><h1>define EVENT_SIZE (sizeof(struct inotify_event))</h1><h1>define BUF_LEN (1024 * (EVENT_SIZE + 16))</h1><p>void read_directory(const char <em>path) {
DIR </em>dir;
struct dirent *entry;</p><pre class="brush:php;toolbar:false;"><code>dir = opendir(path);
if (dir == NULL) {
perror("opendir");
return;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s", entry->d_name); }
<code>closedir(dir);</code>
}
int main(int argc, char *argv[]) { int length, i = 0; int fd; int wd; char buffer[BUF_LEN];
<code>if (argc != 2) {
fprintf(stderr, "Usage: %s <directory></code>", argv[0]); return EXIT_FAILURE; }
<code>fd = inotify_init();
if (fd < 0) {
perror("inotify_init");
return EXIT_FAILURE;
}
wd = inotify_add_watch(fd, argv[1], IN_CREATE | IN_DELETE | IN_MODIFY);
if (wd < 0) {
perror("inotify_add_watch");
return EXIT_FAILURE;
}
read_directory(argv[1]);
while (1) {
length = read(fd, buffer, BUF_LEN);
if (length < 0) {
perror("read");
break;
}
for (i = 0; i < length; ) {
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len) {
printf("Event type: %d</code>", event->mask); if (event->mask & IN_CREATE) { printf("File %s created. ", event->name); } else if (event->mask & IN_DELETE) { printf("File %s deleted. ", event->name); } else if (event->mask & IN_MODIFY) { printf("File %s modified. ", event->name); } // 重新读取目录内容 read_directory(argv[1]); } i += EVENT_SIZE + event->len; } i = 0; }
<code>(void) inotify_rm_watch(fd, wd); (void) close(fd); return EXIT_SUCCESS;</code>
}
上述示例程序会在检测到目录变化时重新读取目录内容并输出变化。你可以根据具体需求进一步扩展此程序,增加更多功能。
以上就是Linux readdir如何实现目录监控的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号