溫馨提示×

Linux inotify API如何使用

小樊
38
2025-08-03 10:03:57
欄目: 智能運維

Linux的inotify API允許應用程序監控文件系統事件,如文件的創建、刪除、修改等。以下是使用inotify API的基本步驟:

  1. 包含必要的頭文件

    #include <sys/inotify.h>
    
  2. 創建inotify實例: 使用inotify_init()函數創建一個inotify實例。

    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        return -1;
    }
    
  3. 添加監控: 使用inotify_add_watch()函數添加一個監控項。你需要指定inotify實例的文件描述符、要監控的文件或目錄的路徑,以及你感興趣的事件。

    int wd = inotify_add_watch(fd, "/path/to/directory", IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        return -1;
    }
    
  4. 讀取事件: 使用read()函數從inotify實例中讀取事件。事件會被存儲在一個inotify_event結構體數組中。

    char buffer[4096];
    ssize_t length = read(fd, buffer, sizeof(buffer));
    if (length < 0) {
        perror("read");
        close(fd);
        return -1;
    }
    
    int i = 0;
    while (i < length) {
        struct inotify_event *event = (struct inotify_event *) &buffer[i];
        if (event->len) {
            if (event->mask & IN_CREATE) {
                printf("File %s was created.\n", event->name);
            }
            if (event->mask & IN_DELETE) {
                printf("File %s was deleted.\n", event->name);
            }
            if (event->mask & IN_MODIFY) {
                printf("File %s was modified.\n", event->name);
            }
        }
        i += sizeof(struct inotify_event) + event->len;
    }
    
  5. 移除監控: 當不再需要監控時,使用inotify_rm_watch()函數移除監控項。

    inotify_rm_watch(fd, wd);
    
  6. 關閉inotify實例: 最后,使用close()函數關閉inotify實例。

    close(fd);
    

示例代碼

以下是一個完整的示例程序,監控指定目錄中的文件創建、刪除和修改事件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
        return 1;
    }

    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        return 1;
    }

    int wd = inotify_add_watch(fd, argv[1], IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        return 1;
    }

    printf("Monitoring directory %s for events...\n", argv[1]);

    char buffer[4096];
    while (1) {
        ssize_t length = read(fd, buffer, sizeof(buffer));
        if (length < 0) {
            perror("read");
            break;
        }

        int i = 0;
        while (i < length) {
            struct inotify_event *event = (struct inotify_event *) &buffer[i];
            if (event->len) {
                if (event->mask & IN_CREATE) {
                    printf("File %s was created.\n", event->name);
                }
                if (event->mask & IN_DELETE) {
                    printf("File %s was deleted.\n", event->name);
                }
                if (event->mask & IN_MODIFY) {
                    printf("File %s was modified.\n", event->name);
                }
            }
            i += sizeof(struct inotify_event) + event->len;
        }
    }

    inotify_rm_watch(fd, wd);
    close(fd);

    return 0;
}

編譯并運行這個程序:

gcc -o monitor monitor.c
./monitor /path/to/directory

這個程序會持續監控指定目錄,并在檢測到文件創建、刪除或修改事件時打印相應的消息。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女