如何监控 vala 中的目录?

发布于 2024-11-23 22:15:59 字数 245 浏览 1 评论 0原文

如何异步监控vala中的某些目录?我所需要的只是每当目录之一中的文件出现以下情况时调用回调方法:

  • 创建
  • 删除
  • 修改

我发现 GLib.FileMonitor 但我不确定如何使用它。

How can I asynchronously monitor some directories in vala? All I need is for a callback method to be called whenever a file in one of the directories is:

  • created
  • deleted
  • modified

I found GLib.FileMonitor but I am unsure how to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-11-30 22:15:59

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

您可以从 GLib.File 创建监视器,然后连接到已更改信号。

Always fallback to the original documentation: http://developer.gnome.org/gio/unstable/GFileMonitor.html

You create a monitor from a GLib.File, then connect to the changed signal.

三生池水覆流年 2024-11-30 22:15:59

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

然后,您需要使用 monitor_directory 方法。

然后,您可以连接到已更改 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();
}

To monitor a directory, you need to first create a GLib.File from that directory by using one of the GLib.File.new_* static methods. new_for_path is probably what you want.

You then need to create a GLib.FileMonitor for that directory using the monitor_directory method of the GLib.File object.

You can then connect to the changed signal of the GLib.FIleMonitor object.

When you compile, you will need to include --pkg gio-2.0.

Example:

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();
}
GRAY°灰色天空 2024-11-30 22:15:59

这是一个仅使用 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 );
}

Here is a C example using only inotify. Although it is standalone, it can be modified to work as an idle process (instead of as while(1)) and call a callback (instead of 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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文