答案 0 :(得分:2)
始终回退到原始文档:http://developer.gnome.org/gio/unstable/GFileMonitor.html
您可以从GLib.File创建一个监视器,然后连接到已更改的信号。
答案 1 :(得分: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();
}
答案 2 :(得分: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 );
}