Make prototype match LPOVERLAPPED_COMPLETION_ROUTINE to avoid warning.
[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, g_win32_directory_monitor, G_TYPE_LOCAL_DIRECTORY_MONITOR,
32 g_io_extension_point_implement (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME,
33                                                          g_define_type_id,
34                                                          "readdirectorychanges",
35                                                          20))
36
37 struct _GWin32DirectoryMonitorPrivate {
38         OVERLAPPED overlapped;
39         DWORD buffer_allocated_bytes;
40         gchar* file_notify_buffer;
41         DWORD buffer_filled_bytes;
42         HANDLE hDirectory;
43         /** needed in the APC where we only have this private struct */
44         GFileMonitor * self;
45 };
46
47 static void g_win32_directory_monitor_finalize (GObject* base);
48 static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base);
49 static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
50
51 static gboolean g_win32_directory_monitor_is_supported(void) {
52         return TRUE;
53 }
54
55 static void g_win32_directory_monitor_finalize (GObject* base) {
56         GWin32DirectoryMonitor * self;
57         self = G_WIN32_DIRECTORY_MONITOR (base);
58
59         g_free (self->priv->file_notify_buffer);
60         g_free (self->priv);
61         
62         if (G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize)
63                 (*G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize) (base);
64 }
65
66
67 static gboolean g_win32_directory_monitor_cancel (GFileMonitor* base) {
68         GWin32DirectoryMonitor * self;
69         self = G_WIN32_DIRECTORY_MONITOR (base);
70         
71         /* this triggers a last callback() with nBytes=0 */ 
72         if (self->priv->hDirectory != INVALID_HANDLE_VALUE)
73           CloseHandle (self->priv->hDirectory);
74
75         if (G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel)
76         (*G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel) (base);
77         return TRUE;
78 }
79
80 void CALLBACK g_win32_directory_monitor_callback (DWORD error, DWORD nBytes, LPOVERLAPPED lpOverlapped)
81 {
82         gulong offset;
83         PFILE_NOTIFY_INFORMATION pfile_notify_walker;
84         gulong file_name_len;
85         gchar* file_name;
86         GFile * file;
87         GWin32DirectoryMonitorPrivate *priv = (GWin32DirectoryMonitorPrivate *) lpOverlapped;
88
89         static GFileMonitorEvent events[] = {0, 
90                 G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_ADDED            */
91                 G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_REMOVED          */
92                 G_FILE_MONITOR_EVENT_CHANGED, /* FILE_ACTION_MODIFIED         */
93                 G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_RENAMED_OLD_NAME */
94                 G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_RENAMED_NEW_NAME */
95         };
96
97         if (!nBytes) /* monitor was cancelled/finalized */
98                 return;
99         
100         if (g_file_monitor_is_cancelled (G_FILE_MONITOR (priv->self)))
101                 return; /* and ReadDirectoryChangesW doesn't get called this time */
102
103         offset = 0;
104         do {
105                 pfile_notify_walker = (PFILE_NOTIFY_INFORMATION)(priv->file_notify_buffer + offset);
106                 offset += pfile_notify_walker->NextEntryOffset;
107                 file_name = g_utf16_to_utf8 (pfile_notify_walker->FileName, pfile_notify_walker->FileNameLength / sizeof(WCHAR), NULL, &file_name_len, NULL);
108                 file = g_file_new_for_path (file_name); 
109                 g_file_monitor_emit_event (priv->self, file, NULL, events [pfile_notify_walker->Action]);
110                 g_object_unref (file);
111                 g_free (file_name);
112         } while (pfile_notify_walker->NextEntryOffset);
113         
114         ReadDirectoryChangesW (priv->hDirectory, (gpointer)priv->file_notify_buffer, priv->buffer_allocated_bytes, FALSE, 
115                 FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
116                 FILE_NOTIFY_CHANGE_SIZE, &priv->buffer_filled_bytes, &priv->overlapped, g_win32_directory_monitor_callback);
117 }
118
119 static GObject * g_win32_directory_monitor_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
120         GObject * obj;
121         GWin32DirectoryMonitorClass * klass;
122         GObjectClass * parent_class;
123         GWin32DirectoryMonitor * self;
124         wchar_t * wdirname;
125         gboolean result;
126         
127         klass = G_WIN32_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_WIN32_DIRECTORY_MONITOR));
128         parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
129         obj = parent_class->constructor (type, n_construct_properties, construct_properties);
130         self = G_WIN32_DIRECTORY_MONITOR (obj);
131         wdirname = g_utf8_to_utf16 (G_LOCAL_DIRECTORY_MONITOR (obj)->dirname, -1, NULL, NULL, NULL);
132         
133         self->priv->hDirectory = CreateFileW (wdirname, FILE_LIST_DIRECTORY, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
134                 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL); 
135         g_free (wdirname);
136         if (self->priv->hDirectory == INVALID_HANDLE_VALUE)
137           {
138             /* Ignore errors */
139             return obj;
140           }
141
142         result = ReadDirectoryChangesW (self->priv->hDirectory, (gpointer)self->priv->file_notify_buffer, self->priv->buffer_allocated_bytes, FALSE, 
143                 FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES |
144                 FILE_NOTIFY_CHANGE_SIZE, &self->priv->buffer_filled_bytes, &self->priv->overlapped, g_win32_directory_monitor_callback);
145         /* Ignore errors */
146         
147         return obj;
148 }
149
150 static void g_win32_directory_monitor_class_init (GWin32DirectoryMonitorClass * klass) {
151         
152         g_win32_directory_monitor_parent_class = g_type_class_peek_parent (klass);
153
154         G_OBJECT_CLASS (klass)->constructor = g_win32_directory_monitor_constructor;
155         G_OBJECT_CLASS (klass)->finalize = g_win32_directory_monitor_finalize;
156         G_FILE_MONITOR_CLASS (klass)->cancel = g_win32_directory_monitor_cancel;
157         
158         G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->mount_notify = FALSE;
159         G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->is_supported = g_win32_directory_monitor_is_supported;
160 }
161
162 static void g_win32_directory_monitor_init (GWin32DirectoryMonitor * self) 
163 {
164         self->priv = (GWin32DirectoryMonitorPrivate*)g_new0 (GWin32DirectoryMonitorPrivate, 1);
165         g_assert (self->priv != 0);
166         
167         self->priv->buffer_allocated_bytes = 32768;
168         self->priv->file_notify_buffer = g_new0 (gchar, self->priv->buffer_allocated_bytes);
169         g_assert (self->priv->file_notify_buffer);
170         
171         self->priv->self = G_FILE_MONITOR (self);
172 }