2012-11-12 35 views
6

我想在C ..中運行inotify的一個例子,但它不工作。 我想監視文件的修改(該文件是tmp.cfg),但它不工作..我不知道我是否正確運行,因爲我知道如何監視目錄,但不是單個文件 Here's的例子:inotify文件在C

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

#define EVENT_SIZE (sizeof (struct inotify_event)) 
#define BUF_LEN  (1024 * (EVENT_SIZE + 16)) 

int main(int argc, char **argv) 
{ 
    int length, i = 0; 
    int fd; 
    int wd; 
    char buffer[BUF_LEN]; 

    fd = inotify_init(); 

    if (fd < 0) { 
    perror("inotify_init"); 
    } 

    wd = inotify_add_watch(fd, "/home/name/tmp.cfg", 
         IN_MODIFY | IN_CREATE | IN_DELETE); 
    length = read(fd, buffer, BUF_LEN); 

    if (length < 0) { 
    perror("read"); 
    } 

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

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

    return 0; 
} 

一旦我運行它,如果我寫的東西文件,然後保存它,什麼都不會發生。 我已經tryed調試it..the問題似乎是,如果(事件 - >面膜& IN_MODIFY),因爲它不承認它作爲一個修改

回答

1

我覺得你不使用你的用戶名,這是你的主目錄,而你不檢查的inotify_add_watch返回這可能失敗:

"/home/name/tmp.cfg" 

編輯:好的第二個問題,你不應該打印name因爲

名稱字段僅在針對目錄目錄內的文件 返回事件時才存在;

EDIT2:第三個問題,因爲你在文件中添加了監視的文件必須在運行程序之前,我建議你從inotify_add_watch

+0

name是我的用戶名 –

+0

我試過調試它..這個問題似乎是if(event-> mask&IN_MODIFY),因爲它不認爲它是一個修改 –

+0

@ user1693049我試了代碼它在這裏工作,沒有看到任何問題 – iabdalkader

2

檢查錯誤你有2個問題。首先,據我所知,inotify並不能真正處理文件 - 它需要目錄名稱來監視。

二,你錯過了if (event->len) {裏面的while循環。

此代碼對我的作品用於創建,刪除和在當前目錄下修改文件:

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

#define EVENT_SIZE (sizeof(struct inotify_event)) 
#define BUF_LEN  (1024 * (EVENT_SIZE + 16)) 

int main(int argc, char **argv) { 
    int length, i = 0; 
    int fd; 
    int wd; 
    char buffer[BUF_LEN]; 

    fd = inotify_init(); 

    if (fd < 0) { 
     perror("inotify_init"); 
    } 

    wd = inotify_add_watch(fd, ".", 
     IN_MODIFY | IN_CREATE | IN_DELETE); 
    length = read(fd, buffer, BUF_LEN); 

    if (length < 0) { 
     perror("read"); 
    } 

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

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

    return 0; 
} 
+1

它在文件上工作,閱讀手冊頁 – iabdalkader

+0

你可能是對的。但是,如果我使用文件名,更改通知似乎不適用於我 – mvp

+0

不應該打印'name',因爲只有當監視目錄中的文件發生更改時纔會出現該名稱。 – iabdalkader

1

在看文件,如果文件是由你可以做編輯,並創建一個編輯操作改變,它很可能會做一些操作,導致您要求看到被刪除的原始文件。因此,如果你只看一個文件,通知就會停止。