gio: Add private API to create win32 streams from fds
[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, 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 <glib/gstdio.h>
32 #include "gioerror.h"
33 #include "gwin32outputstream.h"
34 #include "gcancellable.h"
35 #include "gsimpleasyncresult.h"
36 #include "gasynchelper.h"
37 #include "glibintl.h"
38
39
40 /**
41  * SECTION:gwin32outputstream
42  * @short_description: Streaming output operations for Windows file handles
43  * @include: gio/gwin32outputstream.h
44  * @see_also: #GOutputStream
45  *
46  * #GWin32OutputStream implements #GOutputStream for writing to a
47  * Windows file handle.
48  *
49  * Note that <filename>&lt;gio/gwin32outputstream.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 _GWin32OutputStreamPrivate {
61   HANDLE handle;
62   gboolean close_handle;
63   gint fd;
64 };
65
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
67
68 static void     g_win32_output_stream_set_property (GObject              *object,
69                                                     guint                 prop_id,
70                                                     const GValue         *value,
71                                                     GParamSpec           *pspec);
72 static void     g_win32_output_stream_get_property (GObject              *object,
73                                                     guint                 prop_id,
74                                                     GValue               *value,
75                                                     GParamSpec           *pspec);
76 static gssize   g_win32_output_stream_write        (GOutputStream        *stream,
77                                                     const void           *buffer,
78                                                     gsize                 count,
79                                                     GCancellable         *cancellable,
80                                                     GError              **error);
81 static gboolean g_win32_output_stream_close        (GOutputStream        *stream,
82                                                     GCancellable         *cancellable,
83                                                     GError              **error);
84
85
86 static void
87 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
88 {
89   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
91
92   gobject_class->get_property = g_win32_output_stream_get_property;
93   gobject_class->set_property = g_win32_output_stream_set_property;
94
95   stream_class->write_fn = g_win32_output_stream_write;
96   stream_class->close_fn = g_win32_output_stream_close;
97
98    /**
99    * GWin32OutputStream:handle:
100    *
101    * The file handle that the stream writes to.
102    *
103    * Since: 2.26
104    */
105   g_object_class_install_property (gobject_class,
106                                    PROP_HANDLE,
107                                    g_param_spec_pointer ("handle",
108                                                          P_("File handle"),
109                                                          P_("The file handle to write to"),
110                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
111
112   /**
113    * GWin32OutputStream:close-handle:
114    *
115    * Whether to close the file handle when the stream is closed.
116    *
117    * Since: 2.26
118    */
119   g_object_class_install_property (gobject_class,
120                                    PROP_CLOSE_HANDLE,
121                                    g_param_spec_boolean ("close-handle",
122                                                          P_("Close file handle"),
123                                                          P_("Whether to close the file handle when the stream is closed"),
124                                                          TRUE,
125                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
126 }
127
128 static void
129 g_win32_output_stream_set_property (GObject         *object,
130                                     guint            prop_id,
131                                     const GValue    *value,
132                                     GParamSpec      *pspec)
133 {
134   GWin32OutputStream *win32_stream;
135
136   win32_stream = G_WIN32_OUTPUT_STREAM (object);
137
138   switch (prop_id)
139     {
140     case PROP_HANDLE:
141       win32_stream->priv->handle = g_value_get_pointer (value);
142       break;
143     case PROP_CLOSE_HANDLE:
144       win32_stream->priv->close_handle = g_value_get_boolean (value);
145       break;
146     default:
147       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148       break;
149     }
150 }
151
152 static void
153 g_win32_output_stream_get_property (GObject    *object,
154                                     guint       prop_id,
155                                     GValue     *value,
156                                     GParamSpec *pspec)
157 {
158   GWin32OutputStream *win32_stream;
159
160   win32_stream = G_WIN32_OUTPUT_STREAM (object);
161
162   switch (prop_id)
163     {
164     case PROP_HANDLE:
165       g_value_set_pointer (value, win32_stream->priv->handle);
166       break;
167     case PROP_CLOSE_HANDLE:
168       g_value_set_boolean (value, win32_stream->priv->close_handle);
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172     }
173 }
174
175 static void
176 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
177 {
178   win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
179   win32_stream->priv->handle = NULL;
180   win32_stream->priv->close_handle = TRUE;
181   win32_stream->priv->fd = -1;
182 }
183
184 /**
185  * g_win32_output_stream_new:
186  * @handle: a Win32 file handle
187  * @close_handle: %TRUE to close the handle when done
188  *
189  * Creates a new #GWin32OutputStream for the given @handle.
190  *
191  * If @close_handle, is %TRUE, the handle will be closed when the
192  * output stream is destroyed.
193  *
194  * Returns: a new #GOutputStream
195  *
196  * Since: 2.26
197 **/
198 GOutputStream *
199 g_win32_output_stream_new (void    *handle,
200                            gboolean close_handle)
201 {
202   GWin32OutputStream *stream;
203
204   g_return_val_if_fail (handle != NULL, NULL);
205
206   stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
207                          "handle", handle,
208                          "close-handle", close_handle,
209                          NULL);
210
211   return G_OUTPUT_STREAM (stream);
212 }
213
214 /**
215  * g_win32_output_stream_set_close_handle:
216  * @stream: a #GWin32OutputStream
217  * @close_handle: %TRUE to close the handle when done
218  *
219  * Sets whether the handle of @stream shall be closed when the stream
220  * is closed.
221  *
222  * Since: 2.26
223  */
224 void
225 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
226                                         gboolean           close_handle)
227 {
228   g_return_if_fail (G_IS_WIN32_OUTPUT_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_output_stream_get_close_handle:
240  * @stream: a #GWin32OutputStream
241  *
242  * Returns whether the handle of @stream will be closed when the
243  * 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_output_stream_get_close_handle (GWin32OutputStream *stream)
251 {
252   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
253
254   return stream->priv->close_handle;
255 }
256
257 /**
258  * g_win32_output_stream_get_handle:
259  * @stream: a #GWin32OutputStream
260  *
261  * Return the Windows handle that the stream writes to.
262  *
263  * Return value: The handle descriptor of @stream
264  *
265  * Since: 2.26
266  */
267 void *
268 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
269 {
270   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
271
272   return stream->priv->handle;
273 }
274
275 static gssize
276 g_win32_output_stream_write (GOutputStream  *stream,
277                             const void     *buffer,
278                             gsize           count,
279                             GCancellable   *cancellable,
280                             GError        **error)
281 {
282   GWin32OutputStream *win32_stream;
283   BOOL res;
284   DWORD nbytes, nwritten;
285   OVERLAPPED overlap = { 0, };
286   gssize retval = -1;
287
288   win32_stream = G_WIN32_OUTPUT_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 = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
302   if (res)
303     retval = nwritten;
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, &nwritten, cancellable))
311         {
312           retval = nwritten;
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_HANDLE_EOF ||
321           errsv == ERROR_BROKEN_PIPE)
322         {
323           retval = 0;
324         }
325       else
326         {
327           gchar *emsg;
328
329           emsg = g_win32_error_message (errsv);
330           g_set_error (error, G_IO_ERROR,
331                        g_io_error_from_win32_error (errsv),
332                        _("Error writing to handle: %s"),
333                        emsg);
334           g_free (emsg);
335         }
336     }
337
338 end:
339   CloseHandle (overlap.hEvent);
340   return retval;
341 }
342
343 static gboolean
344 g_win32_output_stream_close (GOutputStream  *stream,
345                              GCancellable   *cancellable,
346                              GError        **error)
347 {
348   GWin32OutputStream *win32_stream;
349   BOOL res;
350
351   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
352
353   if (!win32_stream->priv->close_handle)
354     return TRUE;
355
356   if (win32_stream->priv->fd != -1)
357     {
358       if (close (win32_stream->priv->fd) < 0)
359         {
360           g_set_error_literal (error, G_IO_ERROR,
361                                g_io_error_from_errno (errno),
362                                g_strerror (errno));
363           return FALSE;
364         }
365     }
366   else
367     {
368       res = CloseHandle (win32_stream->priv->handle);
369       if (!res)
370         {
371           int errsv = GetLastError ();
372           gchar *emsg = g_win32_error_message (errsv);
373
374           g_set_error (error, G_IO_ERROR,
375                        g_io_error_from_win32_error (errsv),
376                        _("Error closing handle: %s"),
377                        emsg);
378           g_free (emsg);
379           return FALSE;
380         }
381     }
382
383   return TRUE;
384 }
385
386 GOutputStream *
387 g_win32_output_stream_new_from_fd (gint      fd,
388                                   gboolean  close_fd)
389 {
390   GWin32OutputStream *win32_stream;
391
392   win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
393   win32_stream->priv->fd = fd;
394
395   return (GOutputStream*)win32_stream;
396 }