hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / gio / gwin32outputstream.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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  * Author: Tor Lillqvist <tml@iki.fi>
20  */
21
22 #include "config.h"
23
24 #include <windows.h>
25
26 #include <io.h>
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include "gioerror.h"
31 #include "gwin32outputstream.h"
32 #include "giowin32-priv.h"
33 #include "gcancellable.h"
34 #include "gsimpleasyncresult.h"
35 #include "gasynchelper.h"
36 #include "glibintl.h"
37
38
39 /**
40  * SECTION:gwin32outputstream
41  * @short_description: Streaming output operations for Windows file handles
42  * @include: gio/gwin32outputstream.h
43  * @see_also: #GOutputStream
44  *
45  * #GWin32OutputStream implements #GOutputStream for writing to a
46  * Windows file handle.
47  *
48  * Note that `<gio/gwin32outputstream.h>` belongs to the Windows-specific GIO
49  * interfaces, thus you have to use the `gio-windows-2.0.pc` pkg-config file
50  * when using it.
51  */
52
53 enum {
54   PROP_0,
55   PROP_HANDLE,
56   PROP_CLOSE_HANDLE
57 };
58
59 struct _GWin32OutputStreamPrivate {
60   HANDLE handle;
61   gboolean close_handle;
62   gint fd;
63 };
64
65 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
66
67 static void     g_win32_output_stream_set_property (GObject              *object,
68                                                     guint                 prop_id,
69                                                     const GValue         *value,
70                                                     GParamSpec           *pspec);
71 static void     g_win32_output_stream_get_property (GObject              *object,
72                                                     guint                 prop_id,
73                                                     GValue               *value,
74                                                     GParamSpec           *pspec);
75 static gssize   g_win32_output_stream_write        (GOutputStream        *stream,
76                                                     const void           *buffer,
77                                                     gsize                 count,
78                                                     GCancellable         *cancellable,
79                                                     GError              **error);
80 static gboolean g_win32_output_stream_close        (GOutputStream        *stream,
81                                                     GCancellable         *cancellable,
82                                                     GError              **error);
83
84
85 static void
86 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
87 {
88   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
90
91   gobject_class->get_property = g_win32_output_stream_get_property;
92   gobject_class->set_property = g_win32_output_stream_set_property;
93
94   stream_class->write_fn = g_win32_output_stream_write;
95   stream_class->close_fn = g_win32_output_stream_close;
96
97    /**
98    * GWin32OutputStream:handle:
99    *
100    * The file handle that the stream writes to.
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 write to"),
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    * GWin32OutputStream: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_output_stream_set_property (GObject         *object,
129                                     guint            prop_id,
130                                     const GValue    *value,
131                                     GParamSpec      *pspec)
132 {
133   GWin32OutputStream *win32_stream;
134
135   win32_stream = G_WIN32_OUTPUT_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_output_stream_get_property (GObject    *object,
153                                     guint       prop_id,
154                                     GValue     *value,
155                                     GParamSpec *pspec)
156 {
157   GWin32OutputStream *win32_stream;
158
159   win32_stream = G_WIN32_OUTPUT_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_output_stream_init (GWin32OutputStream *win32_stream)
176 {
177   win32_stream->priv = g_win32_output_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_output_stream_new:
185  * @handle: a Win32 file handle
186  * @close_handle: %TRUE to close the handle when done
187  *
188  * Creates a new #GWin32OutputStream for the given @handle.
189  *
190  * If @close_handle, is %TRUE, the handle will be closed when the
191  * output stream is destroyed.
192  *
193  * Returns: a new #GOutputStream
194  *
195  * Since: 2.26
196 **/
197 GOutputStream *
198 g_win32_output_stream_new (void    *handle,
199                            gboolean close_handle)
200 {
201   GWin32OutputStream *stream;
202
203   g_return_val_if_fail (handle != NULL, NULL);
204
205   stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
206                          "handle", handle,
207                          "close-handle", close_handle,
208                          NULL);
209
210   return G_OUTPUT_STREAM (stream);
211 }
212
213 /**
214  * g_win32_output_stream_set_close_handle:
215  * @stream: a #GWin32OutputStream
216  * @close_handle: %TRUE to close the handle when done
217  *
218  * Sets whether the handle of @stream shall be closed when the stream
219  * is closed.
220  *
221  * Since: 2.26
222  */
223 void
224 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
225                                         gboolean           close_handle)
226 {
227   g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
228
229   close_handle = close_handle != FALSE;
230   if (stream->priv->close_handle != close_handle)
231     {
232       stream->priv->close_handle = close_handle;
233       g_object_notify (G_OBJECT (stream), "close-handle");
234     }
235 }
236
237 /**
238  * g_win32_output_stream_get_close_handle:
239  * @stream: a #GWin32OutputStream
240  *
241  * Returns whether the handle of @stream will be closed when the
242  * stream is closed.
243  *
244  * Returns: %TRUE if the handle is closed when done
245  *
246  * Since: 2.26
247  */
248 gboolean
249 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
250 {
251   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
252
253   return stream->priv->close_handle;
254 }
255
256 /**
257  * g_win32_output_stream_get_handle:
258  * @stream: a #GWin32OutputStream
259  *
260  * Return the Windows handle that the stream writes to.
261  *
262  * Returns: The handle descriptor of @stream
263  *
264  * Since: 2.26
265  */
266 void *
267 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
268 {
269   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
270
271   return stream->priv->handle;
272 }
273
274 static gssize
275 g_win32_output_stream_write (GOutputStream  *stream,
276                             const void     *buffer,
277                             gsize           count,
278                             GCancellable   *cancellable,
279                             GError        **error)
280 {
281   GWin32OutputStream *win32_stream;
282   BOOL res;
283   DWORD nbytes, nwritten;
284   OVERLAPPED overlap = { 0, };
285   gssize retval = -1;
286
287   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
288
289   if (g_cancellable_set_error_if_cancelled (cancellable, error))
290     return -1;
291
292   if (count > G_MAXINT)
293     nbytes = G_MAXINT;
294   else
295     nbytes = count;
296
297   overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
298   g_return_val_if_fail (overlap.hEvent != NULL, -1);
299
300   res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
301   if (res)
302     retval = nwritten;
303   else
304     {
305       int errsv = GetLastError ();
306
307       if (errsv == ERROR_IO_PENDING &&
308           _g_win32_overlap_wait_result (win32_stream->priv->handle,
309                                         &overlap, &nwritten, cancellable))
310         {
311           retval = nwritten;
312           goto end;
313         }
314
315       if (g_cancellable_set_error_if_cancelled (cancellable, error))
316         goto end;
317
318       errsv = GetLastError ();
319       if (errsv == ERROR_HANDLE_EOF ||
320           errsv == ERROR_BROKEN_PIPE)
321         {
322           retval = 0;
323         }
324       else
325         {
326           gchar *emsg;
327
328           emsg = g_win32_error_message (errsv);
329           g_set_error (error, G_IO_ERROR,
330                        g_io_error_from_win32_error (errsv),
331                        _("Error writing to handle: %s"),
332                        emsg);
333           g_free (emsg);
334         }
335     }
336
337 end:
338   CloseHandle (overlap.hEvent);
339   return retval;
340 }
341
342 static gboolean
343 g_win32_output_stream_close (GOutputStream  *stream,
344                              GCancellable   *cancellable,
345                              GError        **error)
346 {
347   GWin32OutputStream *win32_stream;
348   BOOL res;
349
350   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
351
352   if (!win32_stream->priv->close_handle)
353     return TRUE;
354
355   if (win32_stream->priv->fd != -1)
356     {
357       if (close (win32_stream->priv->fd) < 0)
358         {
359           g_set_error_literal (error, G_IO_ERROR,
360                                g_io_error_from_errno (errno),
361                                g_strerror (errno));
362           return FALSE;
363         }
364     }
365   else
366     {
367       res = CloseHandle (win32_stream->priv->handle);
368       if (!res)
369         {
370           int errsv = GetLastError ();
371           gchar *emsg = g_win32_error_message (errsv);
372
373           g_set_error (error, G_IO_ERROR,
374                        g_io_error_from_win32_error (errsv),
375                        _("Error closing handle: %s"),
376                        emsg);
377           g_free (emsg);
378           return FALSE;
379         }
380     }
381
382   return TRUE;
383 }
384
385 GOutputStream *
386 g_win32_output_stream_new_from_fd (gint      fd,
387                                   gboolean  close_fd)
388 {
389   GWin32OutputStream *win32_stream;
390
391   win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
392   win32_stream->priv->fd = fd;
393
394   return (GOutputStream*)win32_stream;
395 }