0

如何异步监控 vala 中的一些目录?我只需要一个回调方法,只要其中一个目录中的文件是:

  • 创建
  • 已删除
  • 修改的

我找到了 GLib.FileMonitor但我不确定如何使用它。

4

3 回答 3

2

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

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

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

编译时,需要包含--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();
}
于 2011-07-16T10:00:50.933 回答
2

始终回退到原始文档:http: //developer.gnome.org/gio/unstable/GFileMonitor.html

您从 GLib.File 创建一个监视器,然后连接到更改的信号。

于 2011-07-16T06:36:32.437 回答
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 );
}
于 2012-06-07T17:37:11.423 回答