gmain: Fall back to pipes if kernel doesn't support EFD_CLOEXEC for eventfd()
[glib.git] / gio / gwin32outputstream.c
blob609e4f4239a3e418d89ddcc2cef96aeabd5be120
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2010 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * Author: Tor Lillqvist <tml@iki.fi>
24 #include "config.h"
26 #include <windows.h>
28 #include <io.h>
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"
40 /**
41 * SECTION:gwin32outputstream
42 * @short_description: Streaming output operations for Windows file handles
43 * @include: gio/gwin32outputstream.h
44 * @see_also: #GOutputStream
46 * #GWin32OutputStream implements #GOutputStream for writing to a
47 * Windows file handle.
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.
54 enum {
55 PROP_0,
56 PROP_HANDLE,
57 PROP_CLOSE_HANDLE
60 G_DEFINE_TYPE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM);
63 struct _GWin32OutputStreamPrivate {
64 HANDLE handle;
65 gboolean close_handle;
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);
86 static void
87 g_win32_output_stream_finalize (GObject *object)
89 G_OBJECT_CLASS (g_win32_output_stream_parent_class)->finalize (object);
92 static void
93 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
98 g_type_class_add_private (klass, sizeof (GWin32OutputStreamPrivate));
100 gobject_class->get_property = g_win32_output_stream_get_property;
101 gobject_class->set_property = g_win32_output_stream_set_property;
102 gobject_class->finalize = g_win32_output_stream_finalize;
104 stream_class->write_fn = g_win32_output_stream_write;
105 stream_class->close_fn = g_win32_output_stream_close;
108 * GWin32OutputStream:handle:
110 * The file handle that the stream writes to.
112 * Since: 2.26
114 g_object_class_install_property (gobject_class,
115 PROP_HANDLE,
116 g_param_spec_pointer ("handle",
117 P_("File handle"),
118 P_("The file handle to write to"),
119 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
122 * GWin32OutputStream:close-handle:
124 * Whether to close the file handle when the stream is closed.
126 * Since: 2.26
128 g_object_class_install_property (gobject_class,
129 PROP_CLOSE_HANDLE,
130 g_param_spec_boolean ("close-handle",
131 P_("Close file handle"),
132 P_("Whether to close the file handle when the stream is closed"),
133 TRUE,
134 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
137 static void
138 g_win32_output_stream_set_property (GObject *object,
139 guint prop_id,
140 const GValue *value,
141 GParamSpec *pspec)
143 GWin32OutputStream *win32_stream;
145 win32_stream = G_WIN32_OUTPUT_STREAM (object);
147 switch (prop_id)
149 case PROP_HANDLE:
150 win32_stream->priv->handle = g_value_get_pointer (value);
151 break;
152 case PROP_CLOSE_HANDLE:
153 win32_stream->priv->close_handle = g_value_get_boolean (value);
154 break;
155 default:
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 break;
161 static void
162 g_win32_output_stream_get_property (GObject *object,
163 guint prop_id,
164 GValue *value,
165 GParamSpec *pspec)
167 GWin32OutputStream *win32_stream;
169 win32_stream = G_WIN32_OUTPUT_STREAM (object);
171 switch (prop_id)
173 case PROP_HANDLE:
174 g_value_set_pointer (value, win32_stream->priv->handle);
175 break;
176 case PROP_CLOSE_HANDLE:
177 g_value_set_boolean (value, win32_stream->priv->close_handle);
178 break;
179 default:
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 static void
185 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
187 win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
188 G_TYPE_WIN32_OUTPUT_STREAM,
189 GWin32OutputStreamPrivate);
191 win32_stream->priv->handle = NULL;
192 win32_stream->priv->close_handle = TRUE;
196 * g_win32_output_stream_new:
197 * @handle: a Win32 file handle
198 * @close_handle: %TRUE to close the handle when done
200 * Creates a new #GWin32OutputStream for the given @handle.
202 * If @close_handle, is %TRUE, the handle will be closed when the
203 * output stream is destroyed.
205 * Returns: a new #GOutputStream
207 * Since: 2.26
209 GOutputStream *
210 g_win32_output_stream_new (void *handle,
211 gboolean close_handle)
213 GWin32OutputStream *stream;
215 g_return_val_if_fail (handle != NULL, NULL);
217 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
218 "handle", handle,
219 "close-handle", close_handle,
220 NULL);
222 return G_OUTPUT_STREAM (stream);
226 * g_win32_output_stream_set_close_handle:
227 * @stream: a #GWin32OutputStream
228 * @close_handle: %TRUE to close the handle when done
230 * Sets whether the handle of @stream shall be closed when the stream
231 * is closed.
233 * Since: 2.26
235 void
236 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
237 gboolean close_handle)
239 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
241 close_handle = close_handle != FALSE;
242 if (stream->priv->close_handle != close_handle)
244 stream->priv->close_handle = close_handle;
245 g_object_notify (G_OBJECT (stream), "close-handle");
250 * g_win32_output_stream_get_close_handle:
251 * @stream: a #GWin32OutputStream
253 * Returns whether the handle of @stream will be closed when the
254 * stream is closed.
256 * Return value: %TRUE if the handle is closed when done
258 * Since: 2.26
260 gboolean
261 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
263 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
265 return stream->priv->close_handle;
269 * g_win32_output_stream_get_handle:
270 * @stream: a #GWin32OutputStream
272 * Return the Windows handle that the stream writes to.
274 * Return value: The handle descriptor of @stream
276 * Since: 2.26
278 void *
279 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
281 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
283 return stream->priv->handle;
286 static gssize
287 g_win32_output_stream_write (GOutputStream *stream,
288 const void *buffer,
289 gsize count,
290 GCancellable *cancellable,
291 GError **error)
293 GWin32OutputStream *win32_stream;
294 BOOL res;
295 DWORD nbytes, nwritten;
297 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
299 if (g_cancellable_set_error_if_cancelled (cancellable, error))
300 return -1;
302 if (count > G_MAXINT)
303 nbytes = G_MAXINT;
304 else
305 nbytes = count;
307 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, NULL);
308 if (!res)
310 int errsv = GetLastError ();
311 gchar *emsg = g_win32_error_message (errsv);
313 if (errsv == ERROR_HANDLE_EOF)
314 return 0;
316 g_set_error (error, G_IO_ERROR,
317 g_io_error_from_win32_error (errsv),
318 _("Error writing to handle: %s"),
319 emsg);
320 g_free (emsg);
321 return -1;
324 return nwritten;
327 static gboolean
328 g_win32_output_stream_close (GOutputStream *stream,
329 GCancellable *cancellable,
330 GError **error)
332 GWin32OutputStream *win32_stream;
333 BOOL res;
335 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
337 if (!win32_stream->priv->close_handle)
338 return TRUE;
340 res = CloseHandle (win32_stream->priv->handle);
341 if (!res)
343 int errsv = GetLastError ();
344 gchar *emsg = g_win32_error_message (errsv);
346 g_set_error (error, G_IO_ERROR,
347 g_io_error_from_win32_error (errsv),
348 _("Error closing handle: %s"),
349 emsg);
350 g_free (emsg);
351 return FALSE;
354 return TRUE;