Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / win32 / gwin32directorymonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Vlad Grecescu <b100dian@gmail.com>
21  * 
22  */
23
24 #define _WIN32_WINNT 0x0400
25
26 #include "config.h"
27 #include "gwin32directorymonitor.h"
28 #include "giomodule.h"
29 #include <windows.h>
30
31 G_DEFINE_TYPE_WITH_CODE (GWin32DirectoryMonitor,
32                          g_win32_directory_monitor,
33                          G_TYPE_LOCAL_DIRECTORY_MONITOR,
34                          g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
35                                                          g_define_type_id,
36                                                          "readdirectorychanges",
37                                                          20))
38
39 struct _GWin32DirectoryMonitorPrivate {
40   OVERLAPPED overlapped;
41   DWORD buffer_allocated_bytes;
42   gchar *file_notify_buffer;
43   DWORD buffer_filled_bytes;
44   HANDLE hDirectory;
45   /* Needed in the APC where we only have this private struct */
46   GFileMonitor *self;
47 };
48
49 static void g_win32_directory_monitor_finalize (GObject *base);
50 static gboolean g_win32_directory_monitor_cancel (GFileMonitor *base);
51
52 static GObject *g_win32_directory_monitor_constructor (GType                  type,
53                                                        guint                  n_construct_properties,
54                                                        GObjectConstructParam *construct_properties);
55
56 static gboolean
57 g_win32_directory_monitor_is_supported (void)
58 {
59   return TRUE;
60 }
61
62 static void
63 g_win32_directory_monitor_finalize (GObject *base)
64 {
65   GWin32DirectoryMonitor *self;
66   self = G_WIN32_DIRECTORY_MONITOR (base);
67
68   if (self->priv->hDirectory == INVALID_HANDLE_VALUE)
69     {
70       /* If we don't have a directory handle we can free
71        * self->priv->file_notify_buffer and self->priv here. The
72        * callback won't be called obviously any more (and presumably
73        * never has been called).
74        */
75       g_free (self->priv->file_notify_buffer);
76       self->priv->file_notify_buffer = NULL;
77       g_free (self->priv);
78     }
79   else
80     {
81       /* If we have a directory handle, the OVERLAPPED struct is
82        * passed once more to the callback as a result of the
83        * CloseHandle() done in the cancel method, so self->priv has to
84        * be kept around. The GWin32DirectoryMonitor object is
85        * disappearing, so can't leave a pointer to it in
86        * self->priv->self.
87        */
88       self->priv->self = NULL;
89     }
90
91   if (G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize)
92     (*G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize) (base);
93 }
94
95 static gboolean
96 g_win32_directory_monitor_cancel (GFileMonitor *base)
97 {
98   GWin32DirectoryMonitor *self;
99   self = G_WIN32_DIRECTORY_MONITOR (base);
100
101   /* This triggers a last callback() with nBytes==0. */
102
103   /* Actually I am not so sure about that, it seems to trigger a last
104    * callback allright, but the way to recognize that it is the final
105    * one is not to check for nBytes==0, I think that was a
106    * misunderstanding.
107    */ 
108   if (self->priv->hDirectory != INVALID_HANDLE_VALUE)
109     CloseHandle (self->priv->hDirectory);
110
111   if (G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel)
112     (*G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel) (base);
113   return TRUE;
114 }
115
116 static void CALLBACK 
117 g_win32_directory_monitor_callback (DWORD        error,
118                                     DWORD        nBytes,
119                                     LPOVERLAPPED lpOverlapped)
120 {
121   gulong offset;
122   PFILE_NOTIFY_INFORMATION pfile_notify_walker;
123   glong file_name_len;
124   gchar *file_name;
125   gchar *path;
126   GFile *file;
127   GWin32DirectoryMonitorPrivate *priv = (GWin32DirectoryMonitorPrivate *) lpOverlapped;
128
129   static GFileMonitorEvent events[] =
130     {
131       0, 
132       G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_ADDED            */
133       G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_REMOVED          */
134       G_FILE_MONITOR_EVENT_CHANGED, /* FILE_ACTION_MODIFIED         */
135       G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_RENAMED_OLD_NAME */
136       G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_RENAMED_NEW_NAME */
137     };
138
139   /* If priv->self is NULL the GWin32DirectoryMonitor object has been destroyed. */
140   if (priv->self == NULL ||
141       g_file_monitor_is_cancelled (priv->self) ||
142       priv->file_notify_buffer == NULL)
143     {
144       g_free (priv->file_notify_buffer);
145       g_free (priv);
146       return;
147     }
148
149   offset = 0;
150   do {
151     pfile_notify_walker = (PFILE_NOTIFY_INFORMATION)(priv->file_notify_buffer + offset);
152     if (pfile_notify_walker->Action > 0)
153       {
154         file_name = g_utf16_to_utf8 (pfile_notify_walker->FileName, pfile_notify_walker->FileNameLength / sizeof(WCHAR), NULL, &file_name_len, NULL);
155         path = g_build_filename(G_LOCAL_DIRECTORY_MONITOR (priv->self)->dirname, file_name, NULL);
156         file = g_file_new_for_path (path);
157         g_file_monitor_emit_event (priv->self, file, NULL, events [pfile_notify_walker->Action]);
158         g_object_unref (file);
159         g_free (path);
160         g_free (file_name);
161       }
162     offset += pfile_notify_walker->NextEntryOffset;
163   } while (pfile_notify_walker->NextEntryOffset);
164
165   ReadDirectoryChangesW (priv->hDirectory,
166                          (gpointer)priv->file_notify_buffer,
167                          priv->buffer_allocated_bytes,
168                          FALSE, 
169                          FILE_NOTIFY_CHANGE_FILE_NAME |
170                          FILE_NOTIFY_CHANGE_DIR_NAME |
171                          FILE_NOTIFY_CHANGE_ATTRIBUTES |
172                          FILE_NOTIFY_CHANGE_SIZE,
173                          &priv->buffer_filled_bytes,
174                          &priv->overlapped,
175                          g_win32_directory_monitor_callback);
176 }
177
178 static GObject *
179 g_win32_directory_monitor_constructor (GType                  type,
180                                        guint                  n_construct_properties,
181                                        GObjectConstructParam *construct_properties) {
182   GObject *obj;
183   GWin32DirectoryMonitorClass *klass;
184   GObjectClass *parent_class;
185   GWin32DirectoryMonitor *self;
186   wchar_t *wdirname;
187   gboolean result;
188
189   klass = G_WIN32_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_WIN32_DIRECTORY_MONITOR));
190   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
191   obj = parent_class->constructor (type, n_construct_properties, construct_properties);
192   self = G_WIN32_DIRECTORY_MONITOR (obj);
193   wdirname = g_utf8_to_utf16 (G_LOCAL_DIRECTORY_MONITOR (obj)->dirname, -1, NULL, NULL, NULL);
194
195   self->priv->hDirectory = CreateFileW (wdirname,
196                                         FILE_LIST_DIRECTORY,
197                                         FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
198
199                                         NULL,
200                                         OPEN_EXISTING,
201                                         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
202                                         NULL); 
203   g_free (wdirname);
204   if (self->priv->hDirectory == INVALID_HANDLE_VALUE)
205     {
206       /* Ignore errors */
207       return obj;
208     }
209
210   result = ReadDirectoryChangesW (self->priv->hDirectory,
211                                   (gpointer)self->priv->file_notify_buffer,
212                                   self->priv->buffer_allocated_bytes,
213                                   FALSE, 
214                                   FILE_NOTIFY_CHANGE_FILE_NAME |
215                                   FILE_NOTIFY_CHANGE_DIR_NAME |
216                                   FILE_NOTIFY_CHANGE_ATTRIBUTES |
217                                   FILE_NOTIFY_CHANGE_SIZE,
218                                   &self->priv->buffer_filled_bytes,
219                                   &self->priv->overlapped,
220                                   g_win32_directory_monitor_callback);
221   /* Ignore errors */
222
223   return obj;
224 }
225
226 static void 
227 g_win32_directory_monitor_class_init (GWin32DirectoryMonitorClass *klass)
228 {
229   g_win32_directory_monitor_parent_class = g_type_class_peek_parent (klass);
230
231   G_OBJECT_CLASS (klass)->constructor = g_win32_directory_monitor_constructor;
232   G_OBJECT_CLASS (klass)->finalize = g_win32_directory_monitor_finalize;
233   G_FILE_MONITOR_CLASS (klass)->cancel = g_win32_directory_monitor_cancel;
234
235   G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->mount_notify = FALSE;
236   G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->is_supported = g_win32_directory_monitor_is_supported;
237 }
238
239 static void
240 g_win32_directory_monitor_init (GWin32DirectoryMonitor *self) 
241 {
242   self->priv = (GWin32DirectoryMonitorPrivate*)g_new0 (GWin32DirectoryMonitorPrivate, 1);
243   g_assert (self->priv != 0);
244         
245   self->priv->buffer_allocated_bytes = 32768;
246   self->priv->file_notify_buffer = g_new0 (gchar, self->priv->buffer_allocated_bytes);
247   g_assert (self->priv->file_notify_buffer);
248         
249   self->priv->self = G_FILE_MONITOR (self);
250 }