Dear everyone:
I am very sorry to trouble you, but I can not handle a problem about my
program for a long time, I hope someone can help me, thank you very much!
The program is to monitor directories and report the events occoured in
directory shuch as deleting files , creating files and so on. My program can
monitor the directory and its child diectroy, and report the event that
happened on a single file or directory, but it can only monitor the toppest
directory and do not report the events when copy diredtory which contains many
files.
My code is in attachment, I would appreciate very much if someone help me to
handle this problem, thank you very much!
/* vi: set sw=4 ts=4: */
/*
* ibox_glib_inodify.c: This file is part of ____
*
* Copyright (C) 2011 oubang <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
#include <gio/gio.h>
GMainLoop *loop;
guint timeout;
int ibox_glib_add_inotify_monitor(const char *file_absolute_path);
static int add_monitor_to_existed_directory(const char *absolute_file_path);
static void ibox_glib_inotify_handle(GFileMonitor *monitor_, GFile *file, GFile *other_file, GFileMonitorEvent event_type, gpointer data)
{
//g_printf("ibox glib inotify handle!\n");
gchar* filename = NULL;
filename = (gchar *)g_file_get_path(file);
if (event_type == G_FILE_MONITOR_EVENT_CREATED)
{
#ifdef WIN32
g_printf("CREATED : %s\n", g_win32_locale_filename_from_utf8(filename));
#else
g_printf("CREATED : %s\n", filename);
#endif
if (g_file_test(filename, G_FILE_TEST_IS_DIR))
{
g_printf("hello world! filename : %s\n", filename);
ibox_glib_add_inotify_monitor(filename);
//add_monitor_to_existed_directory(filename);
}
}
if (event_type == G_FILE_MONITOR_EVENT_DELETED)
{
if (g_file_test(filename, G_FILE_TEST_IS_DIR))
{
g_file_monitor_cancel(monitor_);
}
#ifdef WIN32
g_printf("DELETED: %s\n", g_win32_local_filename_from_utf8(filename));
#else
g_printf("DELETED: %s\n", filename);
#endif
}
if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
{
#ifdef WIN32
g_printf("CHANGED: %s\n", g_win32_locale_filename_from_utf8(filename));
#else
g_printf("CHANGED: %s\n", filename);
#endif
}
g_free(filename);
}
static gboolean quit_idle(gpointer user_data)
{
g_main_loop_quit(loop);
return FALSE;
}
int ibox_glib_add_inotify_monitor(const char *file_absolute_path)
{
GFile* inotify_file = NULL;
GFileMonitor* monitor = NULL;
GError* error = NULL;
inotify_file = g_file_new_for_path(file_absolute_path);
monitor = g_file_monitor_directory(inotify_file, G_FILE_MONITOR_NONE, NULL, &error);
if (monitor != NULL)
{
g_signal_connect(monitor, "changed", G_CALLBACK(ibox_glib_inotify_handle), NULL);
if (inotify_file != NULL)
{
g_object_unref(inotify_file);
}
}
else
{
g_warning("failed to setup monitor: %s", error->message);
g_error_free(error);
if (inotify_file != NULL)
{
g_object_unref(inotify_file);
}
return -1;
}
return 0;
}
static int add_monitor_to_existed_directory(const char *absolute_file_path)
{
if (absolute_file_path == NULL)
{
return -1;
}
GDir *dir = NULL;
GError *error = NULL;
gchar *file = NULL;
gchar *add_monitor_file= NULL;
/* open directory */
if ((dir = g_dir_open(absolute_file_path, 0, &error)) == NULL)
{
g_printf("failed to open directory: %s %s\n", absolute_file_path, &error);
g_free(error);
return -1;
}
/* read directory */
while ((file = (gchar *)g_dir_read_name(dir)) != NULL)
{
add_monitor_file = g_build_filename(absolute_file_path, file, NULL);
if (g_file_test(add_monitor_file, G_FILE_TEST_IS_DIR))
{
/* add monitor failed */
if (ibox_glib_add_inotify_monitor(add_monitor_file))
{
g_printf("add monitor failed: %s\n", add_monitor_file);
break;
}
else
{
//g_printf("add monitor: %s\n", add_monitor_file);
add_monitor_to_existed_directory(add_monitor_file);
g_free(add_monitor_file);
}
}
}
/* close directory */
g_dir_close(dir);
return 0;
}
int main(int argc, char *argv[])
{
g_printf("************ibox glib inotify*******************\n");
const char *inotify_directory_path = "/home/oubang/bangbang";
if (!g_thread_supported())
{
g_thread_init(NULL);
g_type_init();
}
loop = g_main_loop_new(NULL, FALSE);
add_monitor_to_existed_directory(inotify_directory_path);
ibox_glib_add_inotify_monitor(inotify_directory_path);
timeout = g_timeout_add(50000*60, quit_idle, NULL);
g_main_loop_run(loop);
g_main_loop_unref(loop);
return 0;
}
/*
vi:ts=4:wrap:ai:
*/
_______________________________________________
gtk-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtk-list