Make GSettingsSchemaKey public
[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 "giowin32-priv.h"
35 #include "gcancellable.h"
36 #include "gasynchelper.h"
37 #include "glibintl.h"
38
39
40 /**
41  * SECTION:gwin32inputstream
42  * @short_description: Streaming input operations for Windows file handles
43  * @include: gio/gwin32inputstream.h
44  * @see_also: #GInputStream
45  *
46  * #GWin32InputStream implements #GInputStream for reading from a
47  * Windows file handle.
48  *
49  * Note that <filename>&lt;gio/gwin32inputstream.h&gt;</filename> belongs
50  * to the Windows-specific GIO interfaces, thus you have to use the
51  * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
52  */
53
54 enum {
55   PROP_0,
56   PROP_HANDLE,
57   PROP_CLOSE_HANDLE
58 };
59
60 struct _GWin32InputStreamPrivate {
61   HANDLE handle;
62   gboolean close_handle;
63   gint fd;
64 };
65
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM)
67
68 static void     g_win32_input_stream_set_property (GObject              *object,
69                                                    guint                 prop_id,
70                                                    const GValue         *value,
71                                                    GParamSpec           *pspec);
72 static void     g_win32_input_stream_get_property (GObject              *object,
73                                                    guint                 prop_id,
74                                                    GValue               *value,
75                                                    GParamSpec           *pspec);
76 static gssize   g_win32_input_stream_read         (GInputStream         *stream,
77                                                    void                 *buffer,
78                                                    gsize                 count,
79                                                    GCancellable         *cancellable,
80                                                    GError              **error);
81 static gboolean g_win32_input_stream_close        (GInputStream         *stream,
82                                                    GCancellable         *cancellable,
83                                                    GError              **error);
84
85 static void
86 g_win32_input_stream_class_init (GWin32InputStreamClass *klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
90
91   gobject_class->get_property = g_win32_input_stream_get_property;
92   gobject_class->set_property = g_win32_input_stream_set_property;
93
94   stream_class->read_fn = g_win32_input_stream_read;
95   stream_class->close_fn = g_win32_input_stream_close;
96
97   /**
98    * GWin32InputStream:handle:
99    *
100    * The handle that the stream reads from.
101    *
102    * Since: 2.26
103    */
104   g_object_class_install_property (gobject_class,
105                                    PROP_HANDLE,
106                                    g_param_spec_pointer ("handle",
107                                                          P_("File handle"),
108                                                          P_("The file handle to read from"),
109                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
110
111   /**
112    * GWin32InputStream:close-handle:
113    *
114    * Whether to close the file handle when the stream is closed.
115    *
116    * Since: 2.26
117    */
118   g_object_class_install_property (gobject_class,
119                                    PROP_CLOSE_HANDLE,
120                                    g_param_spec_boolean ("close-handle",
121                                                          P_("Close file handle"),
122                                                          P_("Whether to close the file handle when the stream is closed"),
123                                                          TRUE,
124                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
125 }
126
127 static void
128 g_win32_input_stream_set_property (GObject         *object,
129                                    guint            prop_id,
130                                    const GValue    *value,
131                                    GParamSpec      *pspec)
132 {
133   GWin32InputStream *win32_stream;
134
135   win32_stream = G_WIN32_INPUT_STREAM (object);
136
137   switch (prop_id)
138     {
139     case PROP_HANDLE:
140       win32_stream->priv->handle = g_value_get_pointer (value);
141       break;
142     case PROP_CLOSE_HANDLE:
143       win32_stream->priv->close_handle = g_value_get_boolean (value);
144       break;
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148     }
149 }
150
151 static void
152 g_win32_input_stream_get_property (GObject    *object,
153                                    guint       prop_id,
154                                    GValue     *value,
155                                    GParamSpec *pspec)
156 {
157   GWin32InputStream *win32_stream;
158
159   win32_stream = G_WIN32_INPUT_STREAM (object);
160
161   switch (prop_id)
162     {
163     case PROP_HANDLE:
164       g_value_set_pointer (value, win32_stream->priv->handle);
165       break;
166     case PROP_CLOSE_HANDLE:
167       g_value_set_boolean (value, win32_stream->priv->close_handle);
168       break;
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171     }
172 }
173
174 static void
175 g_win32_input_stream_init (GWin32InputStream *win32_stream)
176 {
177   win32_stream->priv = g_win32_input_stream_get_instance_private (win32_stream);
178   win32_stream->priv->handle = NULL;
179   win32_stream->priv->close_handle = TRUE;
180   win32_stream->priv->fd = -1;
181 }
182
183 /**
184  * g_win32_input_stream_new:
185  * @handle: a Win32 file handle
186  * @close_handle: %TRUE to close the handle when done
187  *
188  * Creates a new #GWin32InputStream for the given @handle.
189  *
190  * If @close_handle is %TRUE, the handle will be closed
191  * when the stream is closed.
192  *
193  * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
194  * as used in the Windows C libraries.
195  *
196  * Returns: a new #GWin32InputStream
197  **/
198 GInputStream *
199 g_win32_input_stream_new (void     *handle,
200                           gboolean close_handle)
201 {
202   GWin32InputStream *stream;
203
204   g_return_val_if_fail (handle != NULL, NULL);
205
206   stream = g_object_new (G_TYPE_WIN32_INPUT_STREAM,
207                          "handle", handle,
208                          "close-handle", close_handle,
209                          NULL);
210
211   return G_INPUT_STREAM (stream);
212 }
213
214 /**
215  * g_win32_input_stream_set_close_handle:
216  * @stream: a #GWin32InputStream
217  * @close_handle: %TRUE to close the handle when done
218  *
219  * Sets whether the handle of @stream shall be closed
220  * when the stream is closed.
221  *
222  * Since: 2.26
223  */
224 void
225 g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
226                                        gboolean          close_handle)
227 {
228   g_return_if_fail (G_IS_WIN32_INPUT_STREAM (stream));
229
230   close_handle = close_handle != FALSE;
231   if (stream->priv->close_handle != close_handle)
232     {
233       stream->priv->close_handle = close_handle;
234       g_object_notify (G_OBJECT (stream), "close-handle");
235     }
236 }
237
238 /**
239  * g_win32_input_stream_get_close_handle:
240  * @stream: a #GWin32InputStream
241  *
242  * Returns whether the handle of @stream will be
243  * closed when the stream is closed.
244  *
245  * Return value: %TRUE if the handle is closed when done
246  *
247  * Since: 2.26
248  */
249 gboolean
250 g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
251 {
252   g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), FALSE);
253
254   return stream->priv->close_handle;
255 }
256
257 /**
258  * g_win32_input_stream_get_handle:
259  * @stream: a #GWin32InputStream
260  *
261  * Return the Windows file handle that the stream reads from.
262  *
263  * Return value: The file handle of @stream
264  *
265  * Since: 2.26
266  */
267 void *
268 g_win32_input_stream_get_handle (GWin32InputStream *stream)
269 {
270   g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), NULL);
271
272   return stream->priv->handle;
273 }
274
275 static gssize
276 g_win32_input_stream_read (GInputStream  *stream,
277                            void          *buffer,
278                            gsize          count,
279                            GCancellable  *cancellable,
280                            GError       **error)
281 {
282   GWin32InputStream *win32_stream;
283   BOOL res;
284   DWORD nbytes, nread;
285   OVERLAPPED overlap = { 0, };
286   gssize retval = -1;
287
288   win32_stream = G_WIN32_INPUT_STREAM (stream);
289
290   if (g_cancellable_set_error_if_cancelled (cancellable, error))
291     return -1;
292
293   if (count > G_MAXINT)
294     nbytes = G_MAXINT;
295   else
296     nbytes = count;
297
298   overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
299   g_return_val_if_fail (overlap.hEvent != NULL, -1);
300
301   res = ReadFile (win32_stream->priv->handle, buffer, nbytes, &nread, &overlap);
302   if (res)
303     retval = nread;
304   else
305     {
306       int errsv = GetLastError ();
307
308       if (errsv == ERROR_IO_PENDING &&
309           _g_win32_overlap_wait_result (win32_stream->priv->handle,
310                                         &overlap, &nread, cancellable))
311         {
312           retval = nread;
313           goto end;
314         }
315
316       if (g_cancellable_set_error_if_cancelled (cancellable, error))
317         goto end;
318
319       errsv = GetLastError ();
320       if (errsv == ERROR_MORE_DATA)
321         {
322           /* If a named pipe is being read in message mode and the
323            * next message is longer than the nNumberOfBytesToRead
324            * parameter specifies, ReadFile returns FALSE and
325            * GetLastError returns ERROR_MORE_DATA */
326           retval = nread;
327           goto end;
328         }
329       else if (errsv == ERROR_HANDLE_EOF ||
330                errsv == ERROR_BROKEN_PIPE)
331         {
332           /* TODO: the other end of a pipe may call the WriteFile
333            * function with nNumberOfBytesToWrite set to zero. In this
334            * case, it's not possible for the caller to know if it's
335            * broken pipe or a read of 0. Perhaps we should add a
336            * is_broken flag for this win32 case.. */
337           retval = 0;
338         }
339       else
340         {
341           gchar *emsg;
342
343           emsg = g_win32_error_message (errsv);
344           g_set_error (error, G_IO_ERROR,
345                        g_io_error_from_win32_error (errsv),
346                        _("Error reading from handle: %s"),
347                        emsg);
348           g_free (emsg);
349         }
350     }
351
352 end:
353   CloseHandle (overlap.hEvent);
354   return retval;
355 }
356
357 static gboolean
358 g_win32_input_stream_close (GInputStream  *stream,
359                            GCancellable  *cancellable,
360                            GError       **error)
361 {
362   GWin32InputStream *win32_stream;
363   BOOL res;
364
365   win32_stream = G_WIN32_INPUT_STREAM (stream);
366
367   if (!win32_stream->priv->close_handle)
368     return TRUE;
369
370   if (win32_stream->priv->fd != -1)
371     {
372       if (close (win32_stream->priv->fd) < 0)
373         {
374           g_set_error_literal (error, G_IO_ERROR,
375                                g_io_error_from_errno (errno),
376                                g_strerror (errno));
377           return FALSE;
378         }
379     }
380   else
381     {
382       res = CloseHandle (win32_stream->priv->handle);
383       if (!res)
384         {
385           int errsv = GetLastError ();
386           gchar *emsg = g_win32_error_message (errsv);
387
388           g_set_error (error, G_IO_ERROR,
389                        g_io_error_from_win32_error (errsv),
390                        _("Error closing handle: %s"),
391                        emsg);
392           g_free (emsg);
393           return FALSE;
394         }
395     }
396
397   return TRUE;
398 }
399
400 GInputStream *
401 g_win32_input_stream_new_from_fd (gint      fd,
402                                   gboolean  close_fd)
403 {
404   GWin32InputStream *win32_stream;
405
406   win32_stream = G_WIN32_INPUT_STREAM (g_win32_input_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
407   win32_stream->priv->fd = fd;
408
409   return (GInputStream*)win32_stream;
410 }