首页 > 系统教程 > LINUX > 正文

Linux readdir如何实现目录监控

幻夢星雲
发布: 2025-05-24 13:46:01
原创
238人浏览过

linux readdir如何实现目录监控

在Linux系统中,readdir 函数是一种常用的系统调用,用于从目录中读取条目。若要实现目录监控,可以结合 readdir 与其他Linux特性,比如 inotify。以下是一个简化的实例,展示了如何利用 readdirinotify 来监控目录的变化。

使用 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>
登录后复制

}

易笔AI论文
易笔AI论文

专业AI论文生成,免费生成论文大纲,在线生成选题/综述/开题报告等论文模板

易笔AI论文 103
查看详情 易笔AI论文

使用 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>
登录后复制

}

结合 readdirinotify

你还可以将 readdirinotify 结合起来,创建一个更复杂的目录监控解决方案。例如,在检测到目录发生变化时,重新读取目录内容并显示变化。

#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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号