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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
19 * Author: Tor Lillqvist <tml@iki.fi>
29 #include <glib/gstdio.h>
31 #include "gwin32outputstream.h"
32 #include "giowin32-priv.h"
33 #include "gcancellable.h"
34 #include "gsimpleasyncresult.h"
35 #include "gasynchelper.h"
40 * SECTION:gwin32outputstream
41 * @short_description: Streaming output operations for Windows file handles
42 * @include: gio/gwin32outputstream.h
43 * @see_also: #GOutputStream
45 * #GWin32OutputStream implements #GOutputStream for writing to a
46 * Windows file handle.
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
59 struct _GWin32OutputStreamPrivate {
61 gboolean close_handle;
65 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
67 static void g_win32_output_stream_set_property (GObject *object,
71 static void g_win32_output_stream_get_property (GObject *object,
75 static gssize g_win32_output_stream_write (GOutputStream *stream,
78 GCancellable *cancellable,
80 static gboolean g_win32_output_stream_close (GOutputStream *stream,
81 GCancellable *cancellable,
86 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
91 gobject_class->get_property = g_win32_output_stream_get_property;
92 gobject_class->set_property = g_win32_output_stream_set_property;
94 stream_class->write_fn = g_win32_output_stream_write;
95 stream_class->close_fn = g_win32_output_stream_close;
98 * GWin32OutputStream:handle:
100 * The file handle that the stream writes to.
104 g_object_class_install_property (gobject_class,
106 g_param_spec_pointer ("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));
112 * GWin32OutputStream:close-handle:
114 * Whether to close the file handle when the stream is closed.
118 g_object_class_install_property (gobject_class,
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"),
124 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
128 g_win32_output_stream_set_property (GObject *object,
133 GWin32OutputStream *win32_stream;
135 win32_stream = G_WIN32_OUTPUT_STREAM (object);
140 win32_stream->priv->handle = g_value_get_pointer (value);
142 case PROP_CLOSE_HANDLE:
143 win32_stream->priv->close_handle = g_value_get_boolean (value);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 g_win32_output_stream_get_property (GObject *object,
157 GWin32OutputStream *win32_stream;
159 win32_stream = G_WIN32_OUTPUT_STREAM (object);
164 g_value_set_pointer (value, win32_stream->priv->handle);
166 case PROP_CLOSE_HANDLE:
167 g_value_set_boolean (value, win32_stream->priv->close_handle);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
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;
184 * g_win32_output_stream_new:
185 * @handle: a Win32 file handle
186 * @close_handle: %TRUE to close the handle when done
188 * Creates a new #GWin32OutputStream for the given @handle.
190 * If @close_handle, is %TRUE, the handle will be closed when the
191 * output stream is destroyed.
193 * Returns: a new #GOutputStream
198 g_win32_output_stream_new (void *handle,
199 gboolean close_handle)
201 GWin32OutputStream *stream;
203 g_return_val_if_fail (handle != NULL, NULL);
205 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
207 "close-handle", close_handle,
210 return G_OUTPUT_STREAM (stream);
214 * g_win32_output_stream_set_close_handle:
215 * @stream: a #GWin32OutputStream
216 * @close_handle: %TRUE to close the handle when done
218 * Sets whether the handle of @stream shall be closed when the stream
224 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
225 gboolean close_handle)
227 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
229 close_handle = close_handle != FALSE;
230 if (stream->priv->close_handle != close_handle)
232 stream->priv->close_handle = close_handle;
233 g_object_notify (G_OBJECT (stream), "close-handle");
238 * g_win32_output_stream_get_close_handle:
239 * @stream: a #GWin32OutputStream
241 * Returns whether the handle of @stream will be closed when the
244 * Returns: %TRUE if the handle is closed when done
249 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
251 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
253 return stream->priv->close_handle;
257 * g_win32_output_stream_get_handle:
258 * @stream: a #GWin32OutputStream
260 * Return the Windows handle that the stream writes to.
262 * Returns: The handle descriptor of @stream
267 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
269 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
271 return stream->priv->handle;
275 g_win32_output_stream_write (GOutputStream *stream,
278 GCancellable *cancellable,
281 GWin32OutputStream *win32_stream;
283 DWORD nbytes, nwritten;
284 OVERLAPPED overlap = { 0, };
287 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
289 if (g_cancellable_set_error_if_cancelled (cancellable, error))
292 if (count > G_MAXINT)
297 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
298 g_return_val_if_fail (overlap.hEvent != NULL, -1);
300 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
305 int errsv = GetLastError ();
307 if (errsv == ERROR_IO_PENDING &&
308 _g_win32_overlap_wait_result (win32_stream->priv->handle,
309 &overlap, &nwritten, cancellable))
315 if (g_cancellable_set_error_if_cancelled (cancellable, error))
318 errsv = GetLastError ();
319 if (errsv == ERROR_HANDLE_EOF ||
320 errsv == ERROR_BROKEN_PIPE)
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"),
338 CloseHandle (overlap.hEvent);
343 g_win32_output_stream_close (GOutputStream *stream,
344 GCancellable *cancellable,
347 GWin32OutputStream *win32_stream;
350 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
352 if (!win32_stream->priv->close_handle)
355 if (win32_stream->priv->fd != -1)
357 if (close (win32_stream->priv->fd) < 0)
359 g_set_error_literal (error, G_IO_ERROR,
360 g_io_error_from_errno (errno),
367 res = CloseHandle (win32_stream->priv->handle);
370 int errsv = GetLastError ();
371 gchar *emsg = g_win32_error_message (errsv);
373 g_set_error (error, G_IO_ERROR,
374 g_io_error_from_win32_error (errsv),
375 _("Error closing handle: %s"),
386 g_win32_output_stream_new_from_fd (gint fd,
389 GWin32OutputStream *win32_stream;
391 win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
392 win32_stream->priv->fd = fd;
394 return (GOutputStream*)win32_stream;