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