kdbus: Fixup signal subscription
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 "gasynchelper.h"
37 #include "glibintl.h"
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 struct _GWin32OutputStreamPrivate {
54   HANDLE handle;
55   gboolean close_handle;
56   gint fd;
57 };
58
59 enum {
60   PROP_0,
61   PROP_HANDLE,
62   PROP_CLOSE_HANDLE,
63   LAST_PROP
64 };
65
66 static GParamSpec *props[LAST_PROP];
67
68 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
69
70 static void
71 g_win32_output_stream_set_property (GObject         *object,
72                                     guint            prop_id,
73                                     const GValue    *value,
74                                     GParamSpec      *pspec)
75 {
76   GWin32OutputStream *win32_stream;
77
78   win32_stream = G_WIN32_OUTPUT_STREAM (object);
79
80   switch (prop_id)
81     {
82     case PROP_HANDLE:
83       win32_stream->priv->handle = g_value_get_pointer (value);
84       break;
85     case PROP_CLOSE_HANDLE:
86       win32_stream->priv->close_handle = g_value_get_boolean (value);
87       break;
88     default:
89       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90       break;
91     }
92 }
93
94 static void
95 g_win32_output_stream_get_property (GObject    *object,
96                                     guint       prop_id,
97                                     GValue     *value,
98                                     GParamSpec *pspec)
99 {
100   GWin32OutputStream *win32_stream;
101
102   win32_stream = G_WIN32_OUTPUT_STREAM (object);
103
104   switch (prop_id)
105     {
106     case PROP_HANDLE:
107       g_value_set_pointer (value, win32_stream->priv->handle);
108       break;
109     case PROP_CLOSE_HANDLE:
110       g_value_set_boolean (value, win32_stream->priv->close_handle);
111       break;
112     default:
113       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114     }
115 }
116
117 static gssize
118 g_win32_output_stream_write (GOutputStream  *stream,
119                             const void     *buffer,
120                             gsize           count,
121                             GCancellable   *cancellable,
122                             GError        **error)
123 {
124   GWin32OutputStream *win32_stream;
125   BOOL res;
126   DWORD nbytes, nwritten;
127   OVERLAPPED overlap = { 0, };
128   gssize retval = -1;
129
130   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
131
132   if (g_cancellable_set_error_if_cancelled (cancellable, error))
133     return -1;
134
135   if (count > G_MAXINT)
136     nbytes = G_MAXINT;
137   else
138     nbytes = count;
139
140   overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
141   g_return_val_if_fail (overlap.hEvent != NULL, -1);
142
143   res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
144   if (res)
145     retval = nwritten;
146   else
147     {
148       int errsv = GetLastError ();
149
150       if (errsv == ERROR_IO_PENDING &&
151           _g_win32_overlap_wait_result (win32_stream->priv->handle,
152                                         &overlap, &nwritten, cancellable))
153         {
154           retval = nwritten;
155           goto end;
156         }
157
158       if (g_cancellable_set_error_if_cancelled (cancellable, error))
159         goto end;
160
161       errsv = GetLastError ();
162       if (errsv == ERROR_HANDLE_EOF ||
163           errsv == ERROR_BROKEN_PIPE)
164         {
165           retval = 0;
166         }
167       else
168         {
169           gchar *emsg;
170
171           emsg = g_win32_error_message (errsv);
172           g_set_error (error, G_IO_ERROR,
173                        g_io_error_from_win32_error (errsv),
174                        _("Error writing to handle: %s"),
175                        emsg);
176           g_free (emsg);
177         }
178     }
179
180 end:
181   CloseHandle (overlap.hEvent);
182   return retval;
183 }
184
185 static gboolean
186 g_win32_output_stream_close (GOutputStream  *stream,
187                              GCancellable   *cancellable,
188                              GError        **error)
189 {
190   GWin32OutputStream *win32_stream;
191   BOOL res;
192
193   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
194
195   if (!win32_stream->priv->close_handle)
196     return TRUE;
197
198   if (win32_stream->priv->fd != -1)
199     {
200       if (close (win32_stream->priv->fd) < 0)
201         {
202           int errsv = errno;
203
204           g_set_error (error, G_IO_ERROR,
205                        g_io_error_from_errno (errsv),
206                        _("Error closing file descriptor: %s"),
207                        g_strerror (errsv));
208           return FALSE;
209         }
210     }
211   else
212     {
213       res = CloseHandle (win32_stream->priv->handle);
214       if (!res)
215         {
216           int errsv = GetLastError ();
217           gchar *emsg = g_win32_error_message (errsv);
218
219           g_set_error (error, G_IO_ERROR,
220                        g_io_error_from_win32_error (errsv),
221                        _("Error closing handle: %s"),
222                        emsg);
223           g_free (emsg);
224           return FALSE;
225         }
226     }
227
228   return TRUE;
229 }
230
231 static void
232 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
233 {
234   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
235   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
236
237   gobject_class->get_property = g_win32_output_stream_get_property;
238   gobject_class->set_property = g_win32_output_stream_set_property;
239
240   stream_class->write_fn = g_win32_output_stream_write;
241   stream_class->close_fn = g_win32_output_stream_close;
242
243    /**
244    * GWin32OutputStream:handle:
245    *
246    * The file handle that the stream writes to.
247    *
248    * Since: 2.26
249    */
250   props[PROP_HANDLE] =
251     g_param_spec_pointer ("handle",
252                           P_("File handle"),
253                           P_("The file handle to write to"),
254                           G_PARAM_READABLE |
255                           G_PARAM_WRITABLE |
256                           G_PARAM_CONSTRUCT_ONLY |
257                           G_PARAM_STATIC_STRINGS);
258
259   /**
260    * GWin32OutputStream:close-handle:
261    *
262    * Whether to close the file handle when the stream is closed.
263    *
264    * Since: 2.26
265    */
266   props[PROP_CLOSE_HANDLE] =
267     g_param_spec_boolean ("close-handle",
268                           P_("Close file handle"),
269                           P_("Whether to close the file handle when the stream is closed"),
270                           TRUE,
271                           G_PARAM_READABLE |
272                           G_PARAM_WRITABLE |
273                           G_PARAM_STATIC_STRINGS);
274
275   g_object_class_install_properties (gobject_class, LAST_PROP, props);
276 }
277
278 static void
279 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
280 {
281   win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
282   win32_stream->priv->handle = NULL;
283   win32_stream->priv->close_handle = TRUE;
284   win32_stream->priv->fd = -1;
285 }
286
287 /**
288  * g_win32_output_stream_new:
289  * @handle: a Win32 file handle
290  * @close_handle: %TRUE to close the handle when done
291  *
292  * Creates a new #GWin32OutputStream for the given @handle.
293  *
294  * If @close_handle, is %TRUE, the handle will be closed when the
295  * output stream is destroyed.
296  *
297  * Returns: a new #GOutputStream
298  *
299  * Since: 2.26
300 **/
301 GOutputStream *
302 g_win32_output_stream_new (void    *handle,
303                            gboolean close_handle)
304 {
305   GWin32OutputStream *stream;
306
307   g_return_val_if_fail (handle != NULL, NULL);
308
309   stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
310                          "handle", handle,
311                          "close-handle", close_handle,
312                          NULL);
313
314   return G_OUTPUT_STREAM (stream);
315 }
316
317 /**
318  * g_win32_output_stream_set_close_handle:
319  * @stream: a #GWin32OutputStream
320  * @close_handle: %TRUE to close the handle when done
321  *
322  * Sets whether the handle of @stream shall be closed when the stream
323  * is closed.
324  *
325  * Since: 2.26
326  */
327 void
328 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
329                                         gboolean           close_handle)
330 {
331   g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
332
333   close_handle = close_handle != FALSE;
334   if (stream->priv->close_handle != close_handle)
335     {
336       stream->priv->close_handle = close_handle;
337       g_object_notify (G_OBJECT (stream), "close-handle");
338     }
339 }
340
341 /**
342  * g_win32_output_stream_get_close_handle:
343  * @stream: a #GWin32OutputStream
344  *
345  * Returns whether the handle of @stream will be closed when the
346  * stream is closed.
347  *
348  * Returns: %TRUE if the handle is closed when done
349  *
350  * Since: 2.26
351  */
352 gboolean
353 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
354 {
355   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
356
357   return stream->priv->close_handle;
358 }
359
360 /**
361  * g_win32_output_stream_get_handle:
362  * @stream: a #GWin32OutputStream
363  *
364  * Return the Windows handle that the stream writes to.
365  *
366  * Returns: The handle descriptor of @stream
367  *
368  * Since: 2.26
369  */
370 void *
371 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
372 {
373   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
374
375   return stream->priv->handle;
376 }
377
378 GOutputStream *
379 g_win32_output_stream_new_from_fd (gint      fd,
380                                    gboolean  close_fd)
381 {
382   GWin32OutputStream *win32_stream;
383
384   win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
385   win32_stream->priv->fd = fd;
386
387   return (GOutputStream*)win32_stream;
388 }