Don't write the Content-Length header ourselves, WinHttpSendRequest()
[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   g_free (self->priv->file_notify_buffer);
69   g_free (self->priv);
70
71   if (G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize)
72     (*G_OBJECT_CLASS (g_win32_directory_monitor_parent_class)->finalize) (base);
73 }
74
75 static gboolean
76 g_win32_directory_monitor_cancel (GFileMonitor *base)
77 {
78   GWin32DirectoryMonitor *self;
79   self = G_WIN32_DIRECTORY_MONITOR (base);
80
81   /* This triggers a last callback() with nBytes=0 */ 
82   if (self->priv->hDirectory != INVALID_HANDLE_VALUE)
83     CloseHandle (self->priv->hDirectory);
84
85   if (G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel)
86     (*G_FILE_MONITOR_CLASS (g_win32_directory_monitor_parent_class)->cancel) (base);
87   return TRUE;
88 }
89
90 static void CALLBACK 
91 g_win32_directory_monitor_callback (DWORD        error,
92                                     DWORD        nBytes,
93                                     LPOVERLAPPED lpOverlapped)
94 {
95   gulong offset;
96   PFILE_NOTIFY_INFORMATION pfile_notify_walker;
97   gulong file_name_len;
98   gchar *file_name;
99   GFile *file;
100   GWin32DirectoryMonitorPrivate *priv = (GWin32DirectoryMonitorPrivate *) lpOverlapped;
101
102   static GFileMonitorEvent events[] =
103     {
104       0, 
105       G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_ADDED            */
106       G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_REMOVED          */
107       G_FILE_MONITOR_EVENT_CHANGED, /* FILE_ACTION_MODIFIED         */
108       G_FILE_MONITOR_EVENT_DELETED, /* FILE_ACTION_RENAMED_OLD_NAME */
109       G_FILE_MONITOR_EVENT_CREATED, /* FILE_ACTION_RENAMED_NEW_NAME */
110     };
111
112   if (!nBytes) /* Monitor was cancelled/finalized */
113     return;
114
115   if (g_file_monitor_is_cancelled (G_FILE_MONITOR (priv->self)))
116     return; /* and ReadDirectoryChangesW doesn't get called this time */
117
118   offset = 0;
119   do {
120     pfile_notify_walker = (PFILE_NOTIFY_INFORMATION)(priv->file_notify_buffer + offset);
121     offset += pfile_notify_walker->NextEntryOffset;
122     file_name = g_utf16_to_utf8 (pfile_notify_walker->FileName, pfile_notify_walker->FileNameLength / sizeof(WCHAR), NULL, &file_name_len, NULL);
123     file = g_file_new_for_path (file_name);     
124     g_file_monitor_emit_event (priv->self, file, NULL, events [pfile_notify_walker->Action]);
125     g_object_unref (file);
126     g_free (file_name);
127   } while (pfile_notify_walker->NextEntryOffset);
128
129   ReadDirectoryChangesW (priv->hDirectory,
130                          (gpointer)priv->file_notify_buffer,
131                          priv->buffer_allocated_bytes,
132                          FALSE, 
133                          FILE_NOTIFY_CHANGE_FILE_NAME |
134                          FILE_NOTIFY_CHANGE_DIR_NAME |
135                          FILE_NOTIFY_CHANGE_ATTRIBUTES |
136                          FILE_NOTIFY_CHANGE_SIZE,
137                          &priv->buffer_filled_bytes,
138                          &priv->overlapped,
139                          g_win32_directory_monitor_callback);
140 }
141
142 static GObject *
143 g_win32_directory_monitor_constructor (GType                  type,
144                                        guint                  n_construct_properties,
145                                        GObjectConstructParam *construct_properties) {
146   GObject *obj;
147   GWin32DirectoryMonitorClass *klass;
148   GObjectClass *parent_class;
149   GWin32DirectoryMonitor *self;
150   wchar_t *wdirname;
151   gboolean result;
152
153   klass = G_WIN32_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_WIN32_DIRECTORY_MONITOR));
154   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
155   obj = parent_class->constructor (type, n_construct_properties, construct_properties);
156   self = G_WIN32_DIRECTORY_MONITOR (obj);
157   wdirname = g_utf8_to_utf16 (G_LOCAL_DIRECTORY_MONITOR (obj)->dirname, -1, NULL, NULL, NULL);
158
159   self->priv->hDirectory = CreateFileW (wdirname,
160                                         FILE_LIST_DIRECTORY,
161                                         FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
162
163                                         NULL,
164                                         OPEN_EXISTING,
165                                         FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
166                                         NULL); 
167   g_free (wdirname);
168   if (self->priv->hDirectory == INVALID_HANDLE_VALUE)
169     {
170       /* Ignore errors */
171       return obj;
172     }
173
174   result = ReadDirectoryChangesW (self->priv->hDirectory,
175                                   (gpointer)self->priv->file_notify_buffer,
176                                   self->priv->buffer_allocated_bytes,
177                                   FALSE, 
178                                   FILE_NOTIFY_CHANGE_FILE_NAME |
179                                   FILE_NOTIFY_CHANGE_DIR_NAME |
180                                   FILE_NOTIFY_CHANGE_ATTRIBUTES |
181                                   FILE_NOTIFY_CHANGE_SIZE,
182                                   &self->priv->buffer_filled_bytes,
183                                   &self->priv->overlapped,
184                                   g_win32_directory_monitor_callback);
185   /* Ignore errors */
186
187   return obj;
188 }
189
190 static void 
191 g_win32_directory_monitor_class_init (GWin32DirectoryMonitorClass *klass)
192 {
193   g_win32_directory_monitor_parent_class = g_type_class_peek_parent (klass);
194
195   G_OBJECT_CLASS (klass)->constructor = g_win32_directory_monitor_constructor;
196   G_OBJECT_CLASS (klass)->finalize = g_win32_directory_monitor_finalize;
197   G_FILE_MONITOR_CLASS (klass)->cancel = g_win32_directory_monitor_cancel;
198
199   G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->mount_notify = FALSE;
200   G_LOCAL_DIRECTORY_MONITOR_CLASS (klass)->is_supported = g_win32_directory_monitor_is_supported;
201 }
202
203 static void
204 g_win32_directory_monitor_init (GWin32DirectoryMonitor *self) 
205 {
206   self->priv = (GWin32DirectoryMonitorPrivate*)g_new0 (GWin32DirectoryMonitorPrivate, 1);
207   g_assert (self->priv != 0);
208         
209   self->priv->buffer_allocated_bytes = 32768;
210   self->priv->file_notify_buffer = g_new0 (gchar, self->priv->buffer_allocated_bytes);
211   g_assert (self->priv->file_notify_buffer);
212         
213   self->priv->self = G_FILE_MONITOR (self);
214 }