溫馨提示×

Ubuntu inotify API如何正確調用

小樊
39
2025-10-08 04:19:30
欄目: 智能運維

在Ubuntu系統中,inotify API用于監控文件系統事件,如文件的創建、刪除、修改等。要正確調用inotify API,你需要遵循以下步驟:

  1. 包含必要的頭文件

    #include <sys/inotify.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
  2. 初始化inotify實例: 使用inotify_init()函數來創建一個inotify實例。

    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }
    
  3. 添加監控: 使用inotify_add_watch()函數來添加對特定文件或目錄的監控。

    int wd = inotify_add_watch(fd, "/path/to/file_or_directory", IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        exit(EXIT_FAILURE);
    }
    
  4. 讀取事件: 使用read()函數從inotify文件描述符中讀取事件。

    char buffer[4096];
    ssize_t length = read(fd, buffer, sizeof(buffer));
    if (length < 0) {
        perror("read");
        close(fd);
        exit(EXIT_FAILURE);
    }
    
    // 解析buffer中的事件
    
  5. 解析事件inotify事件存儲在緩沖區中,需要解析這些事件以獲取詳細信息。

    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;
    }
    
  6. 移除監控并關閉inotify實例: 當不再需要監控時,使用inotify_rm_watch()函數移除監控,并使用close()函數關閉inotify文件描述符。

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

示例代碼

以下是一個完整的示例代碼,展示了如何使用inotify API監控文件的變化:

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

int main() {
    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        exit(EXIT_FAILURE);
    }

    int wd = inotify_add_watch(fd, "/path/to/file_or_directory", IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        exit(EXIT_FAILURE);
    }

    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;
}

請將/path/to/file_or_directory替換為你想要監控的實際文件或目錄路徑。這個示例程序會持續運行,直到你手動終止它。

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