0

valaの一部のディレクトリを非同期で監視するにはどうすればよいですか?必要なのは、ディレクトリの1つにあるファイルが次の場合にコールバックメソッドを呼び出すことだけです。

  • 作成した
  • 削除
  • 変更

GLib.FileMonitorを見つけましたが、使用方法がわかりません。

4

3 に答える 3

2

常に元のドキュメントにフォールバックします:http://developer.gnome.org/gio/unstable/GFileMonitor.html

GLib.Fileからモニターを作成し、変更された信号に接続します。

于 2011-07-16T06:36:32.437 に答える
2

ディレクトリを監視するには、最初にGLib.File.new_ *静的メソッドの1つを使用して、そのディレクトリから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 に答える
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 に答える