gio: GCancellable can be used concurrently
[platform/upstream/glib.git] / gio / gwin32inputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2010 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: Alexander Larsson <alexl@redhat.com>
21  * Author: Tor Lillqvist <tml@iki.fi>
22  */
23
24 #include "config.h"
25
26 #include <windows.h>
27
28 #include <io.h>
29
30 #include <glib.h>
31 #include "gioerror.h"
32 #include "gsimpleasyncresult.h"
33 #include "gwin32inputstream.h"
34 #include "gcancellable.h"
35 #include "gasynchelper.h"
36 #include "glibintl.h"
37
38
39 /**
40  * SECTION:gwin32inputstream
41  * @short_description: Streaming input operations for Windows file handles
42  * @include: gio/gwin32inputstream.h
43  * @see_also: #GInputStream
44  *
45  * #GWin32InputStream implements #GInputStream for reading from a
46  * Windows file handle.
47  *
48  * Note that <filename>&lt;gio/gwin32inputstream.h&gt;</filename> belongs
49  * to the Windows-specific GIO interfaces, thus you have to use the
50  * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
51  */
52
53 enum {
54   PROP_0,
55   PROP_HANDLE,
56   PROP_CLOSE_HANDLE
57 };
58
59 G_DEFINE_TYPE (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM);
60
61 struct _GWin32InputStreamPrivate {
62   HANDLE handle;
63   gboolean close_handle;
64 };
65
66 static void     g_win32_input_stream_set_property (GObject              *object,
67                                                    guint                 prop_id,
68                                                    const GValue         *value,
69                                                    GParamSpec           *pspec);
70 static void     g_win32_input_stream_get_property (GObject              *object,
71                                                    guint                 prop_id,
72                                                    GValue               *value,
73                                                    GParamSpec           *pspec);
74 static gssize   g_win32_input_stream_read         (GInputStream         *stream,
75                                                    void                 *buffer,
76                                                    gsize                 count,
77                                                    GCancellable         *cancellable,
78                                                    GError              **error);
79 static gboolean g_win32_input_stream_close        (GInputStream         *stream,
80                                                    GCancellable         *cancellable,
81                                                    GError              **error);
82
83 static void
84 g_win32_input_stream_finalize (GObject *object)
85 {
86   G_OBJECT_CLASS (g_win32_input_stream_parent_class)->finalize (object);
87 }
88
89 static void
90 g_win32_input_stream_class_init (GWin32InputStreamClass *klass)
91 {
92   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
94
95   g_type_class_add_private (klass, sizeof (GWin32InputStreamPrivate));
96
97   gobject_class->get_property = g_win32_input_stream_get_property;
98   gobject_class->set_property = g_win32_input_stream_set_property;
99   gobject_class->finalize = g_win32_input_stream_finalize;
100
101   stream_class->read_fn = g_win32_input_stream_read;
102   stream_class->close_fn = g_win32_input_stream_close;
103
104   /**
105    * GWin32InputStream:handle:
106    *
107    * The handle that the stream reads from.
108    *
109    * Since: 2.26
110    */
111   g_object_class_install_property (gobject_class,
112                                    PROP_HANDLE,
113                                    g_param_spec_pointer ("handle",
114                                                          P_("File handle"),
115                                                          P_("The file handle to read from"),
116                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
117
118   /**
119    * GWin32InputStream:close-handle:
120    *
121    * Whether to close the file handle when the stream is closed.
122    *
123    * Since: 2.26
124    */
125   g_object_class_install_property (gobject_class,
126                                    PROP_CLOSE_HANDLE,
127                                    g_param_spec_boolean ("close-handle",
128                                                          P_("Close file handle"),
129                                                          P_("Whether to close the file handle when the stream is closed"),
130                                                          TRUE,
131                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
132 }
133
134 static void
135 g_win32_input_stream_set_property (GObject         *object,
136                                    guint            prop_id,
137                                    const GValue    *value,
138                                    GParamSpec      *pspec)
139 {
140   GWin32InputStream *win32_stream;
141
142   win32_stream = G_WIN32_INPUT_STREAM (object);
143
144   switch (prop_id)
145     {
146     case PROP_HANDLE:
147       win32_stream->priv->handle = g_value_get_pointer (value);
148       break;
149     case PROP_CLOSE_HANDLE:
150       win32_stream->priv->close_handle = g_value_get_boolean (value);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155     }
156 }
157
158 static void
159 g_win32_input_stream_get_property (GObject    *object,
160                                    guint       prop_id,
161                                    GValue     *value,
162                                    GParamSpec *pspec)
163 {
164   GWin32InputStream *win32_stream;
165
166   win32_stream = G_WIN32_INPUT_STREAM (object);
167
168   switch (prop_id)
169     {
170     case PROP_HANDLE:
171       g_value_set_pointer (value, win32_stream->priv->handle);
172       break;
173     case PROP_CLOSE_HANDLE:
174       g_value_set_boolean (value, win32_stream->priv->close_handle);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178     }
179 }
180
181 static void
182 g_win32_input_stream_init (GWin32InputStream *win32_stream)
183 {
184   win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
185                                                     G_TYPE_WIN32_INPUT_STREAM,
186                                                     GWin32InputStreamPrivate);
187
188   win32_stream->priv->handle = NULL;
189   win32_stream->priv->close_handle = TRUE;
190 }
191
192 /**
193  * g_win32_input_stream_new:
194  * @handle: a Win32 file handle
195  * @close_fd: %TRUE to close the handle when done
196  *
197  * Creates a new #GWin32InputStream for the given @fd.
198  *
199  * If @close_handle is %TRUE, the handle will be closed
200  * when the stream is closed.
201  *
202  * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
203  * as used in the Windows C libraries.
204  *
205  * Returns: a new #GWin32InputStream
206  **/
207 GInputStream *
208 g_win32_input_stream_new (void     *handle,
209                           gboolean close_handle)
210 {
211   GWin32InputStream *stream;
212
213   g_return_val_if_fail (handle != NULL, NULL);
214
215   stream = g_object_new (G_TYPE_WIN32_INPUT_STREAM,
216                          "handle", handle,
217                          "close-handle", close_handle,
218                          NULL);
219
220   return G_INPUT_STREAM (stream);
221 }
222
223 /**
224  * g_win32_input_stream_set_close_handle:
225  * @stream: a #GWin32InputStream
226  * @close_handle: %TRUE to close the handle when done
227  *
228  * Sets whether the handle of @stream shall be closed
229  * when the stream is closed.
230  *
231  * Since: 2.26
232  */
233 void
234 g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
235                                        gboolean          close_handle)
236 {
237   g_return_if_fail (G_IS_WIN32_INPUT_STREAM (stream));
238
239   close_handle = close_handle != FALSE;
240   if (stream->priv->close_handle != close_handle)
241     {
242       stream->priv->close_handle = close_handle;
243       g_object_notify (G_OBJECT (stream), "close-handle");
244     }
245 }
246
247 /**
248  * g_win32_input_stream_get_close_handle:
249  * @stream: a #GWin32InputStream
250  *
251  * Returns whether the handle of @stream will be
252  * closed when the stream is closed.
253  *
254  * Return value: %TRUE if the handle is closed when done
255  *
256  * Since: 2.26
257  */
258 gboolean
259 g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
260 {
261   g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), FALSE);
262
263   return stream->priv->close_handle;
264 }
265
266 /**
267  * g_win32_input_stream_get_handle:
268  * @stream: a #GWin32InputStream
269  *
270  * Return the Windows file handle that the stream reads from.
271  *
272  * Return value: The file handle of @stream
273  *
274  * Since: 2.26
275  */
276 void *
277 g_win32_input_stream_get_handle (GWin32InputStream *stream)
278 {
279   g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), NULL);
280
281   return stream->priv->handle;
282 }
283
284 static gssize
285 g_win32_input_stream_read (GInputStream  *stream,
286                            void          *buffer,
287                            gsize          count,
288                            GCancellable  *cancellable,
289                            GError       **error)
290 {
291   GWin32InputStream *win32_stream;
292   BOOL res;
293   DWORD nbytes, nread;
294
295   win32_stream = G_WIN32_INPUT_STREAM (stream);
296
297   if (g_cancellable_set_error_if_cancelled (cancellable, error))
298     return -1;
299
300   if (count > G_MAXINT)
301     nbytes = G_MAXINT;
302   else
303     nbytes = count;
304
305   res = ReadFile (win32_stream->priv->handle, buffer, nbytes, &nread, NULL);
306   if (!res)
307     {
308       int errsv = GetLastError ();
309       gchar *emsg;
310
311       if (errsv == ERROR_HANDLE_EOF ||
312           errsv == ERROR_BROKEN_PIPE)
313         return 0;
314
315       emsg = g_win32_error_message (errsv);
316       g_set_error (error, G_IO_ERROR,
317                    g_io_error_from_win32_error (errsv),
318                    _("Error reading from handle: %s"),
319                    emsg);
320       g_free (emsg);
321       return -1;
322     }
323
324   return nread;
325 }
326
327 static gboolean
328 g_win32_input_stream_close (GInputStream  *stream,
329                            GCancellable  *cancellable,
330                            GError       **error)
331 {
332   GWin32InputStream *win32_stream;
333   BOOL res;
334
335   win32_stream = G_WIN32_INPUT_STREAM (stream);
336
337   if (!win32_stream->priv->close_handle)
338     return TRUE;
339
340   res = CloseHandle (win32_stream->priv->handle);
341   if (!res)
342     {
343       int errsv = GetLastError ();
344       gchar *emsg = g_win32_error_message (errsv);
345
346       g_set_error (error, G_IO_ERROR,
347                    g_io_error_from_win32_error (errsv),
348                    _("Error closing handle: %s"),
349                    emsg);
350       g_free (emsg);
351       return FALSE;
352     }
353
354   return TRUE;
355 }