glib gio win32 directory monitor



Hello list,

I don't know if there's any interest / work in progress on this, but noticing that the new Gio implementation lacks a directory monitor for windows I wrote one (patch against 2.15.4 attached, should work on 2.15.5 too)

Some mentions:
- it is based on the native ReadDirectoryChanges Win32 call. It is only a directory watcher, I saw a polling file watcher exists already - it registers itself as a GSource - it wouldn't have been necessary if the glib main loop would have used MsgWaitForMultipleObjectsEx (see http://weblogs.asp.net/kennykerr/archive/2007/12/12/parallel-programming-with-c-part-2-asynchronous-procedure-calls-and-window-messages.aspx) - it's a GModule, like the fam monitor. Thinking that inotify is compiled-in on linux, I think the same can be said for windows about ReadDirectoryWatcher
- it may have bugs / may need code cleanup? it's here to be tested..

Regards,
Vlad Grecescu
Index: configure.in
===================================================================
--- configure.in	(revision 6532)
+++ configure.in	(working copy)
@@ -3216,6 +3216,7 @@
 gio/xdgmime/Makefile
 gio/inotify/Makefile
 gio/fam/Makefile
+gio/win32/Makefile
 gio/tests/Makefile
 po/Makefile.in
 docs/Makefile
Index: gio/Makefile.am
===================================================================
--- gio/Makefile.am	(revision 6532)
+++ gio/Makefile.am	(working copy)
@@ -8,6 +8,10 @@
 SUBDIRS += xdgmime
 endif
 
+if OS_WIN32
+SUBDIRS += win32
+endif
+
 gio.def: gio.symbols
 	(echo -e EXPORTS; $(CPP) -P -DINCLUDE_VARIABLES -DINCLUDE_INTERNAL_SYMBOLS -DG_OS_WIN32 -DALL_FILES - <$(srcdir)/gio.symbols | sed -e '/^$$/d' -e 's/^/	/' -e 's/G_GNUC_[^ ]*//g' | sort) > gio.def.tmp && \
 	  mv gio.def.tmp gio.def
Index: gio/win32/gwin32module.c
===================================================================
--- gio/win32/gwin32module.c	(revision 0)
+++ gio/win32/gwin32module.c	(revision 0)
@@ -0,0 +1,39 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+
+
+
+#include "gwin32directorymonitor.h"
+#include "gfilemonitor.h"
+#include "giomodule.h"
+
+void
+g_io_module_load (GIOModule *module)
+{
+  g_win32_directory_monitor_register (module);
+}
+
+void
+g_io_module_unload (GIOModule *module)
+{
+}
Index: gio/win32/gwin32directorymonitor.c
===================================================================
--- gio/win32/gwin32directorymonitor.c	(revision 0)
+++ gio/win32/gwin32directorymonitor.c	(revision 0)
@@ -0,0 +1,235 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+
+#include "config.h"
+#include "gwin32directorymonitor.h"
+#include "giomodule.h"
+#include <windows.h>
+
+G_DEFINE_DYNAMIC_TYPE (GWin32DirectoryMonitor, g_win32_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR)
+
+struct _GWin32DirectoryMonitorSource {
+	GSource notify_source;
+	/** win32 overlapped IO structure; contains a hEvent for the notify_source */
+	OVERLAPPED overlapped;
+	DWORD buffer_allocated_bytes;
+	gchar* file_notify_buffer;
+	DWORD buffer_filled_bytes;
+	HANDLE hDirectory;
+	GPollFD pollFD;
+	/** needed in the GSource dispach () where we only have this struct */
+	GFileMonitor * self;
+};
+
+static void g_win32_directory_monitor_finalize (GObject* base);
+static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base);
+static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
+
+
+static gboolean g_win32_directory_monitor_is_supported(void) {
+	return TRUE; //if it's compiled, it's supported
+}
+
+static void g_win32_directory_monitor_finalize (GObject* base) {
+	GWin32DirectoryMonitor * self;
+	self = G_WIN32_DIRECTORY_MONITOR (base);
+
+	g_win32_directory_monitor_cancel (base);
+	
+	g_free (self->source->file_notify_buffer);
+	g_source_destroy ((GSource*)self->source);
+	self->source = NULL;
+	
+	if (G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize)
+		(*G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize) (base);
+}
+
+
+static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base) {
+	GWin32DirectoryMonitor * self;
+	self = G_WIN32_DIRECTORY_MONITOR (base);
+	
+	CloseHandle (self->source->hDirectory);
+	self->source->hDirectory = 0;
+	CloseHandle (self->source->overlapped.hEvent);
+	self->source->overlapped.hEvent = 0;
+	
+	if (G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel)
+    	(*G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel) (base);
+	return TRUE;
+}
+
+static gboolean g_win32_directory_monitor_source_check (GSource *source)
+{
+	GWin32DirectoryMonitorSource *self_source = (GWin32DirectoryMonitorSource*)source;
+	return GetOverlappedResult (self_source->hDirectory, &self_source->overlapped, &self_source->buffer_filled_bytes, FALSE);
+}
+
+static gboolean g_win32_directory_monitor_source_prepare (GSource *source, gint *timeout)
+{
+	*timeout = -1;
+	return FALSE;
+}
+
+static gboolean g_win32_directory_monitor_source_finalize (GSource *source)
+{
+	return TRUE;
+}
+
+static gboolean g_win32_directory_monitor_source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
+{
+	GWin32DirectoryMonitorSource *self_source = (GWin32DirectoryMonitorSource*)source;
+	gulong offset;
+	PFILE_NOTIFY_INFORMATION pfile_notify_walker;
+	gulong file_name_len;
+	gchar file_name[MAX_PATH];
+	GFile * file;
+
+	static GFileMonitorEvent events[] = {0, 
+		G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_ADDED            */
+		G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_REMOVED          */
+		G_FILE_MONITOR_EVENT_CHANGED, /* FILE_ACTION_MODIFIED         */
+		G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_RENAMED_OLD_NAME */
+		G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_RENAMED_NEW_NAME */
+	};
+
+	if (self_source->overlapped.hEvent)
+		ResetEvent (self_source->overlapped.hEvent);
+
+	offset = 0;
+	do {
+		pfile_notify_walker = (PFILE_NOTIFY_INFORMATION)(self_source->file_notify_buffer + offset);
+		offset += pfile_notify_walker->NextEntryOffset;
+
+		file_name_len = WideCharToMultiByte(CP_ACP, 0, pfile_notify_walker->FileName, pfile_notify_walker->FileNameLength / sizeof(WCHAR),
+			file_name, MAX_PATH - 1, NULL, NULL);
+		file_name[ file_name_len ] = 0;
+		file = g_file_new_for_path (file_name);	
+		g_file_monitor_emit_event (self_source->self, file, NULL, events [pfile_notify_walker->Action]);
+		g_object_unref (file);
+	} while (pfile_notify_walker->NextEntryOffset);
+	
+	ReadDirectoryChangesW (self_source->hDirectory, (gpointer)self_source->file_notify_buffer, self_source->buffer_allocated_bytes, FALSE, 
+		FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
+		FILE_NOTIFY_CHANGE_SIZE, &self_source->buffer_filled_bytes, &self_source->overlapped, NULL);
+
+	
+	return TRUE;
+}
+
+static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
+	GObject * obj;
+	GWin32DirectoryMonitorClass * klass;
+	GObjectClass * parent_class;
+	GWin32DirectoryMonitor * self;
+	gchar * dirname;
+	gboolean result;
+	GSourceFuncs* notify_source_funcs;
+	
+	/* Invoke parent constructor. */
+	klass = G_WIN32_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_WIN32_DIRECTORY_MONITOR));
+	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
+	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
+	self = G_WIN32_DIRECTORY_MONITOR (obj);
+	dirname = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
+	
+	notify_source_funcs = (GSourceFuncs*)g_new0 (GSourceFuncs, 1);
+
+	notify_source_funcs->check = g_win32_directory_monitor_source_check;
+	notify_source_funcs->prepare = g_win32_directory_monitor_source_prepare;
+	notify_source_funcs->dispatch = g_win32_directory_monitor_source_dispatch;
+	notify_source_funcs->finalize = g_win32_directory_monitor_source_finalize;
+	
+	
+	/* we allocate our priv like this because GSource is opaque */
+	self->source = (GWin32DirectoryMonitorSource*)g_source_new (notify_source_funcs, sizeof( GWin32DirectoryMonitorSource ));
+	g_assert (self->source != 0);
+
+	memset (&self->source->overlapped, 0, sizeof (OVERLAPPED));
+	self->source->buffer_allocated_bytes = 32768;
+	self->source->file_notify_buffer = g_new0 (gchar, self->source->buffer_allocated_bytes);
+	g_assert (self->source->file_notify_buffer);
+	self->source->self = G_FILE_MONITOR (self);
+
+	
+	self->source->overlapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
+	
+	g_message("dir: %s", dirname);
+	self->source->hDirectory = CreateFile (dirname, FILE_LIST_DIRECTORY, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
+		NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL); 
+	/* harsh */
+	g_assert (self->source->hDirectory != INVALID_HANDLE_VALUE);
+
+	self->source->pollFD.fd = self->source->overlapped.hEvent;
+	self->source->pollFD.events = G_IO_IN;
+	self->source->pollFD.revents = 0;
+	
+	g_source_add_poll ((GSource*)self->source, &self->source->pollFD);
+	g_source_attach ((GSource*)self->source, NULL);
+
+	result = ReadDirectoryChangesW (self->source->hDirectory, (gpointer)self->source->file_notify_buffer, self->source->buffer_allocated_bytes, FALSE, 
+		FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
+		FILE_NOTIFY_CHANGE_SIZE, &self->source->buffer_filled_bytes, &self->source->overlapped, NULL);
+	/* harsh */
+	g_assert (result);
+	
+
+	return obj;
+}
+
+
+static void g_win32_directory_monitor_class_init (GWin32DirectoryMonitorClass * klass) {
+	
+	g_win32_directory_monitor_parent_class = g_type_class_peek_parent (klass);
+
+	G_OBJECT_CLASS (klass)->constructor = g_win32_directory_monitor_constructor;
+	G_OBJECT_CLASS (klass)->finalize = g_win32_directory_monitor_finalize;
+	G_FILE_MONITOR_CLASS (klass)->cancel = g_win32_directory_monitor_cancel;
+	
+	G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->mount_notify = FALSE;
+	G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->is_supported = g_win32_directory_monitor_is_supported;
+	
+	
+}
+
+
+static void g_win32_directory_monitor_init (GWin32DirectoryMonitor * self) {
+	
+}
+
+
+void g_win32_directory_monitor_register (GIOModule *module)
+{
+  g_win32_directory_monitor_register_type (G_TYPE_MODULE (module));
+  g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
+				  G_TYPE_WIN32_DIRECTORY_MONITOR,
+				  "read_directory_changes",
+				  20);
+}
+
+static void g_win32_directory_monitor_class_finalize (GWin32DirectoryMonitorClass *klass)
+{
+}
+
+
+
Index: gio/win32/Makefile.am
===================================================================
--- gio/win32/Makefile.am	(revision 0)
+++ gio/win32/Makefile.am	(revision 0)
@@ -0,0 +1,35 @@
+include $(top_srcdir)/Makefile.decl
+
+NULL =
+
+module_flags = -export_dynamic -avoid-version -module -no-undefined -export-symbols-regex '^g_io_module_(load|unload)'
+
+giomodule_LTLIBRARIES = libgiowin32.la
+giomoduledir = $(libdir)/gio/modules
+
+libgiowin32_la_SOURCES = 		\
+	gwin32directorymonitor.c		\
+	gwin32directorymonitor.h		\
+	gwin32module.c		\
+	$(NULL)
+
+libgiowin32_la_CFLAGS = \
+  -g		\
+	-DG_LOG_DOMAIN=\"GLib-GIO\"	\
+	-I$(top_srcdir) 		\
+	-I$(top_srcdir)/glib 		\
+	-I$(top_srcdir)/gmodule		\
+	-I$(top_srcdir)/gio 		\
+	$(GLIB_DEBUG_FLAGS)		\
+	-DGIO_MODULE_DIR=\"$(GIO_MODULE_DIR)\"	\
+	-DGIO_COMPILATION		\
+	-DG_DISABLE_DEPRECATED
+
+libgiowin32_la_LDFLAGS = $(module_flags)
+libgiowin32_la_LIBADD = \
+		$(top_builddir)/gio/libgio-2.0.la \
+		$(top_builddir)/gobject/libgobject-2.0.la \
+		$(top_builddir)/glib/libglib-2.0.la \
+		$(GLIB_LIBS) \
+		$(NULL)
+
Index: gio/win32/gwin32directorymonitor.h
===================================================================
--- gio/win32/gwin32directorymonitor.h	(revision 0)
+++ gio/win32/gwin32directorymonitor.h	(revision 0)
@@ -0,0 +1,62 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+#ifndef __G_WIN32_DIRECTORY_MONITOR_H__
+#define __G_WIN32_DIRECTORY_MONITOR_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "glocaldirectorymonitor.h"
+#include "giomodule.h"
+
+G_BEGIN_DECLS
+
+
+#define G_TYPE_WIN32_DIRECTORY_MONITOR (g_win32_directory_monitor_get_type ())
+#define G_WIN32_DIRECTORY_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitor))
+#define G_WIN32_DIRECTORY_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitorClass))
+#define G_IS_WIN32_DIRECTORY_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR))
+#define G_IS_WIN32_DIRECTORY_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_WIN32_DIRECTORY_MONITOR))
+#define G_WIN32_DIRECTORY_MONITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitorClass))
+
+typedef struct _GWin32DirectoryMonitor GWin32DirectoryMonitor;
+typedef struct _GWin32DirectoryMonitorClass GWin32DirectoryMonitorClass;
+typedef struct _GWin32DirectoryMonitorSource GWin32DirectoryMonitorSource;
+
+struct _GWin32DirectoryMonitor {
+	GLocalDirectoryMonitor parent_instance;
+	GWin32DirectoryMonitorSource * source;
+};
+struct _GWin32DirectoryMonitorClass {
+	GLocalDirectoryMonitorClass parent_class;
+};
+
+GType g_win32_directory_monitor_get_type (void);
+void g_win32_directory_monitor_register (GIOModule *module);
+
+G_END_DECLS
+
+#endif /* __G_WIN32_DIRECTORY_MONITOR_H__ */
Index: gio/win32/gwin32directorymonitor.c
===================================================================
--- gio/win32/gwin32directorymonitor.c	(revision 0)
+++ gio/win32/gwin32directorymonitor.c	(revision 0)
@@ -0,0 +1,235 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+
+#include "config.h"
+#include "gwin32directorymonitor.h"
+#include "giomodule.h"
+#include <windows.h>
+
+G_DEFINE_DYNAMIC_TYPE (GWin32DirectoryMonitor, g_win32_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR)
+
+struct _GWin32DirectoryMonitorSource {
+	GSource notify_source;
+	/** win32 overlapped IO structure; contains a hEvent for the notify_source */
+	OVERLAPPED overlapped;
+	DWORD buffer_allocated_bytes;
+	gchar* file_notify_buffer;
+	DWORD buffer_filled_bytes;
+	HANDLE hDirectory;
+	GPollFD pollFD;
+	/** needed in the GSource dispach () where we only have this struct */
+	GFileMonitor * self;
+};
+
+static void g_win32_directory_monitor_finalize (GObject* base);
+static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base);
+static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
+
+
+static gboolean g_win32_directory_monitor_is_supported(void) {
+	return TRUE; //if it's compiled, it's supported
+}
+
+static void g_win32_directory_monitor_finalize (GObject* base) {
+	GWin32DirectoryMonitor * self;
+	self = G_WIN32_DIRECTORY_MONITOR (base);
+
+	g_win32_directory_monitor_cancel (base);
+	
+	g_free (self->source->file_notify_buffer);
+	g_source_destroy ((GSource*)self->source);
+	self->source = NULL;
+	
+	if (G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize)
+		(*G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize) (base);
+}
+
+
+static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base) {
+	GWin32DirectoryMonitor * self;
+	self = G_WIN32_DIRECTORY_MONITOR (base);
+	
+	CloseHandle (self->source->hDirectory);
+	self->source->hDirectory = 0;
+	CloseHandle (self->source->overlapped.hEvent);
+	self->source->overlapped.hEvent = 0;
+	
+	if (G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel)
+    	(*G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel) (base);
+	return TRUE;
+}
+
+static gboolean g_win32_directory_monitor_source_check (GSource *source)
+{
+	GWin32DirectoryMonitorSource *self_source = (GWin32DirectoryMonitorSource*)source;
+	return GetOverlappedResult (self_source->hDirectory, &self_source->overlapped, &self_source->buffer_filled_bytes, FALSE);
+}
+
+static gboolean g_win32_directory_monitor_source_prepare (GSource *source, gint *timeout)
+{
+	*timeout = -1;
+	return FALSE;
+}
+
+static gboolean g_win32_directory_monitor_source_finalize (GSource *source)
+{
+	return TRUE;
+}
+
+static gboolean g_win32_directory_monitor_source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
+{
+	GWin32DirectoryMonitorSource *self_source = (GWin32DirectoryMonitorSource*)source;
+	gulong offset;
+	PFILE_NOTIFY_INFORMATION pfile_notify_walker;
+	gulong file_name_len;
+	gchar file_name[MAX_PATH];
+	GFile * file;
+
+	static GFileMonitorEvent events[] = {0, 
+		G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_ADDED            */
+		G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_REMOVED          */
+		G_FILE_MONITOR_EVENT_CHANGED, /* FILE_ACTION_MODIFIED         */
+		G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_RENAMED_OLD_NAME */
+		G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_RENAMED_NEW_NAME */
+	};
+
+	if (self_source->overlapped.hEvent)
+		ResetEvent (self_source->overlapped.hEvent);
+
+	offset = 0;
+	do {
+		pfile_notify_walker = (PFILE_NOTIFY_INFORMATION)(self_source->file_notify_buffer + offset);
+		offset += pfile_notify_walker->NextEntryOffset;
+
+		file_name_len = WideCharToMultiByte(CP_ACP, 0, pfile_notify_walker->FileName, pfile_notify_walker->FileNameLength / sizeof(WCHAR),
+			file_name, MAX_PATH - 1, NULL, NULL);
+		file_name[ file_name_len ] = 0;
+		file = g_file_new_for_path (file_name);	
+		g_file_monitor_emit_event (self_source->self, file, NULL, events [pfile_notify_walker->Action]);
+		g_object_unref (file);
+	} while (pfile_notify_walker->NextEntryOffset);
+	
+	ReadDirectoryChangesW (self_source->hDirectory, (gpointer)self_source->file_notify_buffer, self_source->buffer_allocated_bytes, FALSE, 
+		FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
+		FILE_NOTIFY_CHANGE_SIZE, &self_source->buffer_filled_bytes, &self_source->overlapped, NULL);
+
+	
+	return TRUE;
+}
+
+static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
+	GObject * obj;
+	GWin32DirectoryMonitorClass * klass;
+	GObjectClass * parent_class;
+	GWin32DirectoryMonitor * self;
+	gchar * dirname;
+	gboolean result;
+	GSourceFuncs* notify_source_funcs;
+	
+	/* Invoke parent constructor. */
+	klass = G_WIN32_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_WIN32_DIRECTORY_MONITOR));
+	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
+	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
+	self = G_WIN32_DIRECTORY_MONITOR (obj);
+	dirname = G_LOCAL_DIRECTORY_MONITOR (obj)->dirname;
+	
+	notify_source_funcs = (GSourceFuncs*)g_new0 (GSourceFuncs, 1);
+
+	notify_source_funcs->check = g_win32_directory_monitor_source_check;
+	notify_source_funcs->prepare = g_win32_directory_monitor_source_prepare;
+	notify_source_funcs->dispatch = g_win32_directory_monitor_source_dispatch;
+	notify_source_funcs->finalize = g_win32_directory_monitor_source_finalize;
+	
+	
+	/* we allocate our priv like this because GSource is opaque */
+	self->source = (GWin32DirectoryMonitorSource*)g_source_new (notify_source_funcs, sizeof( GWin32DirectoryMonitorSource ));
+	g_assert (self->source != 0);
+
+	memset (&self->source->overlapped, 0, sizeof (OVERLAPPED));
+	self->source->buffer_allocated_bytes = 32768;
+	self->source->file_notify_buffer = g_new0 (gchar, self->source->buffer_allocated_bytes);
+	g_assert (self->source->file_notify_buffer);
+	self->source->self = G_FILE_MONITOR (self);
+
+	
+	self->source->overlapped.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
+	
+	g_message("dir: %s", dirname);
+	self->source->hDirectory = CreateFile (dirname, FILE_LIST_DIRECTORY, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
+		NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL); 
+	/* harsh */
+	g_assert (self->source->hDirectory != INVALID_HANDLE_VALUE);
+
+	self->source->pollFD.fd = self->source->overlapped.hEvent;
+	self->source->pollFD.events = G_IO_IN;
+	self->source->pollFD.revents = 0;
+	
+	g_source_add_poll ((GSource*)self->source, &self->source->pollFD);
+	g_source_attach ((GSource*)self->source, NULL);
+
+	result = ReadDirectoryChangesW (self->source->hDirectory, (gpointer)self->source->file_notify_buffer, self->source->buffer_allocated_bytes, FALSE, 
+		FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
+		FILE_NOTIFY_CHANGE_SIZE, &self->source->buffer_filled_bytes, &self->source->overlapped, NULL);
+	/* harsh */
+	g_assert (result);
+	
+
+	return obj;
+}
+
+
+static void g_win32_directory_monitor_class_init (GWin32DirectoryMonitorClass * klass) {
+	
+	g_win32_directory_monitor_parent_class = g_type_class_peek_parent (klass);
+
+	G_OBJECT_CLASS (klass)->constructor = g_win32_directory_monitor_constructor;
+	G_OBJECT_CLASS (klass)->finalize = g_win32_directory_monitor_finalize;
+	G_FILE_MONITOR_CLASS (klass)->cancel = g_win32_directory_monitor_cancel;
+	
+	G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->mount_notify = FALSE;
+	G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->is_supported = g_win32_directory_monitor_is_supported;
+	
+	
+}
+
+
+static void g_win32_directory_monitor_init (GWin32DirectoryMonitor * self) {
+	
+}
+
+
+void g_win32_directory_monitor_register (GIOModule *module)
+{
+  g_win32_directory_monitor_register_type (G_TYPE_MODULE (module));
+  g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
+				  G_TYPE_WIN32_DIRECTORY_MONITOR,
+				  "read_directory_changes",
+				  20);
+}
+
+static void g_win32_directory_monitor_class_finalize (GWin32DirectoryMonitorClass *klass)
+{
+}
+
+
+
Index: gio/win32/gwin32directorymonitor.h
===================================================================
--- gio/win32/gwin32directorymonitor.h	(revision 0)
+++ gio/win32/gwin32directorymonitor.h	(revision 0)
@@ -0,0 +1,62 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+#ifndef __G_WIN32_DIRECTORY_MONITOR_H__
+#define __G_WIN32_DIRECTORY_MONITOR_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "glocaldirectorymonitor.h"
+#include "giomodule.h"
+
+G_BEGIN_DECLS
+
+
+#define G_TYPE_WIN32_DIRECTORY_MONITOR (g_win32_directory_monitor_get_type ())
+#define G_WIN32_DIRECTORY_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitor))
+#define G_WIN32_DIRECTORY_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitorClass))
+#define G_IS_WIN32_DIRECTORY_MONITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR))
+#define G_IS_WIN32_DIRECTORY_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_WIN32_DIRECTORY_MONITOR))
+#define G_WIN32_DIRECTORY_MONITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_WIN32_DIRECTORY_MONITOR, GWin32DirectoryMonitorClass))
+
+typedef struct _GWin32DirectoryMonitor GWin32DirectoryMonitor;
+typedef struct _GWin32DirectoryMonitorClass GWin32DirectoryMonitorClass;
+typedef struct _GWin32DirectoryMonitorSource GWin32DirectoryMonitorSource;
+
+struct _GWin32DirectoryMonitor {
+	GLocalDirectoryMonitor parent_instance;
+	GWin32DirectoryMonitorSource * source;
+};
+struct _GWin32DirectoryMonitorClass {
+	GLocalDirectoryMonitorClass parent_class;
+};
+
+GType g_win32_directory_monitor_get_type (void);
+void g_win32_directory_monitor_register (GIOModule *module);
+
+G_END_DECLS
+
+#endif /* __G_WIN32_DIRECTORY_MONITOR_H__ */
Index: gio/win32/gwin32module.c
===================================================================
--- gio/win32/gwin32module.c	(revision 0)
+++ gio/win32/gwin32module.c	(revision 0)
@@ -0,0 +1,39 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Vlad Grecescu <b100dian gmail com>
+ * 
+ */
+
+
+
+#include "gwin32directorymonitor.h"
+#include "gfilemonitor.h"
+#include "giomodule.h"
+
+void
+g_io_module_load (GIOModule *module)
+{
+  g_win32_directory_monitor_register (module);
+}
+
+void
+g_io_module_unload (GIOModule *module)
+{
+}
Index: gio/win32/Makefile.am
===================================================================
--- gio/win32/Makefile.am	(revision 0)
+++ gio/win32/Makefile.am	(revision 0)
@@ -0,0 +1,35 @@
+include $(top_srcdir)/Makefile.decl
+
+NULL =
+
+module_flags = -export_dynamic -avoid-version -module -no-undefined -export-symbols-regex '^g_io_module_(load|unload)'
+
+giomodule_LTLIBRARIES = libgiowin32.la
+giomoduledir = $(libdir)/gio/modules
+
+libgiowin32_la_SOURCES = 		\
+	gwin32directorymonitor.c		\
+	gwin32directorymonitor.h		\
+	gwin32module.c		\
+	$(NULL)
+
+libgiowin32_la_CFLAGS = \
+  -g		\
+	-DG_LOG_DOMAIN=\"GLib-GIO\"	\
+	-I$(top_srcdir) 		\
+	-I$(top_srcdir)/glib 		\
+	-I$(top_srcdir)/gmodule		\
+	-I$(top_srcdir)/gio 		\
+	$(GLIB_DEBUG_FLAGS)		\
+	-DGIO_MODULE_DIR=\"$(GIO_MODULE_DIR)\"	\
+	-DGIO_COMPILATION		\
+	-DG_DISABLE_DEPRECATED
+
+libgiowin32_la_LDFLAGS = $(module_flags)
+libgiowin32_la_LIBADD = \
+		$(top_builddir)/gio/libgio-2.0.la \
+		$(top_builddir)/gobject/libgobject-2.0.la \
+		$(top_builddir)/glib/libglib-2.0.la \
+		$(GLIB_LIBS) \
+		$(NULL)
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]