2011-07-14 27 views

回答

2

要监视目录,您需要先使用GLib.File.new_ *静态方法之一从该目录创建一个GLib.File。 new_for_path可能是你想要的。

然后您需要使用GLib.File对象的monitor_directory方法为该目录创建一个GLib.FileMonitor。

然后,您可以连接到GLib.FIleMonitor对象的changed信号。

当您编译时,您需要包含--pkg gio-2.0

实施例:

void on_change() { 
    print("changed\n"); 
} 

void main() { 
    GLib.File usr_share_applications = File.new_for_path(
     "/usr/share/applications" 
    ); 
    GLib.File local_share_applications = File.new_for_commandline_arg(
     GLib.Environment.get_user_data_dir() + "/applications" 
    ); 

    GLib.FileMonitor mon1; 
    GLib.FileMonitor mon2; 

    try { 
     mon1 = usr_share_applications.monitor_directory(
      GLib.FileMonitorFlags.NONE 
     ); 
     mon1.changed.connect(on_change); 
     print("Monitoring: "+usr_share_applications.get_path()+"\n"); 
    } catch (GLib.Error e) { 
     print("Error: "+e.message+"\n"); 
    } 
    try { 
     mon2 = local_share_applications.monitor_directory(
      GLib.FileMonitorFlags.NONE 
     ); 
     mon2.changed.connect(on_change); 
     print("Monitoring: "+local_share_applications.get_path()+"\n"); 
    } catch (GLib.Error e) { 
     print("Error: "+e.message+"\n"); 
    } 

    GLib.MainLoop loop = new GLib.MainLoop(); 
    loop.run(); 
} 
0

这里是仅使用inotify的一个C示例。虽然它是独立的,它可以被修改为一个空闲进程(而不是作为一个while(1)),并调用回调(代替了printf)

/* inotify us of the file changes in directory */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <sys/types.h> 
#include <sys/inotify.h> 

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

int main(int argc, char** argv){ 
int length, i, watch, fd = inotify_init(); 
char buffer[EVENT_BUF_LEN]; 

if (fd < 0) perror("inotify init failed"); 

watch = inotify_add_watch(fd, argv[1], /* should check if argv[1] is a dir */ 
    IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO | IN_ATTRIB); 

while (1){ 
i=0; 
length = read(fd, buffer, EVENT_BUF_LEN); 
if (length < 0) perror("reading inotify fd"); 
    while (i < length) { 
    struct inotify_event *event = (struct inotify_event *) &buffer[ i ]; 
    if (event->len) { 
     if (event->mask & IN_ATTRIB)printf("%s sttributes changed\n",event->name); 
     if (event->mask & IN_CREATE)printf("%s created\n",event->name); 
     if (event->mask & IN_DELETE)printf("%s deleted\n",event->name); 
     if (event->mask & IN_MODIFY)printf("%s modified\n",event->name); 
     if (event->mask & IN_MOVED_FROM)printf("%s moved out\n",event->name); 
     if (event->mask & IN_MOVED_TO)printf("%s moved in\n",event->name); 
    } 
    i += EVENT_SIZE + event->len; 
    } 
} 
inotify_rm_watch(fd, watch); 
close(fd); 
}