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