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>
31 #include <glib/gstdio.h>
33 #include "gwin32outputstream.h"
34 #include "gcancellable.h"
35 #include "gsimpleasyncresult.h"
36 #include "gasynchelper.h"
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><gio/gwin32outputstream.h></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.
60 struct _GWin32OutputStreamPrivate {
62 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;
183 * g_win32_output_stream_new:
184 * @handle: a Win32 file handle
185 * @close_handle: %TRUE to close the handle when done
187 * Creates a new #GWin32OutputStream for the given @handle.
189 * If @close_handle, is %TRUE, the handle will be closed when the
190 * output stream is destroyed.
192 * Returns: a new #GOutputStream
197 g_win32_output_stream_new (void *handle,
198 gboolean close_handle)
200 GWin32OutputStream *stream;
202 g_return_val_if_fail (handle != NULL, NULL);
204 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
206 "close-handle", close_handle,
209 return G_OUTPUT_STREAM (stream);
213 * g_win32_output_stream_set_close_handle:
214 * @stream: a #GWin32OutputStream
215 * @close_handle: %TRUE to close the handle when done
217 * Sets whether the handle of @stream shall be closed when the stream
223 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
224 gboolean close_handle)
226 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
228 close_handle = close_handle != FALSE;
229 if (stream->priv->close_handle != close_handle)
231 stream->priv->close_handle = close_handle;
232 g_object_notify (G_OBJECT (stream), "close-handle");
237 * g_win32_output_stream_get_close_handle:
238 * @stream: a #GWin32OutputStream
240 * Returns whether the handle of @stream will be closed when the
243 * Return value: %TRUE if the handle is closed when done
248 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
250 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
252 return stream->priv->close_handle;
256 * g_win32_output_stream_get_handle:
257 * @stream: a #GWin32OutputStream
259 * Return the Windows handle that the stream writes to.
261 * Return value: The handle descriptor of @stream
266 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
268 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
270 return stream->priv->handle;
274 g_win32_output_stream_write (GOutputStream *stream,
277 GCancellable *cancellable,
280 GWin32OutputStream *win32_stream;
282 DWORD nbytes, nwritten;
283 OVERLAPPED overlap = { 0, };
286 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
288 if (g_cancellable_set_error_if_cancelled (cancellable, error))
291 if (count > G_MAXINT)
296 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
297 g_return_val_if_fail (overlap.hEvent != NULL, -1);
299 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
304 int errsv = GetLastError ();
306 if (errsv == ERROR_IO_PENDING &&
307 _g_win32_overlap_wait_result (win32_stream->priv->handle,
308 &overlap, &nwritten, cancellable))
314 if (g_cancellable_set_error_if_cancelled (cancellable, error))
317 errsv = GetLastError ();
318 if (errsv == ERROR_HANDLE_EOF ||
319 errsv == ERROR_BROKEN_PIPE)
327 emsg = g_win32_error_message (errsv);
328 g_set_error (error, G_IO_ERROR,
329 g_io_error_from_win32_error (errsv),
330 _("Error writing to handle: %s"),
337 CloseHandle (overlap.hEvent);
342 g_win32_output_stream_close (GOutputStream *stream,
343 GCancellable *cancellable,
346 GWin32OutputStream *win32_stream;
349 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
351 if (!win32_stream->priv->close_handle)
354 res = CloseHandle (win32_stream->priv->handle);
357 int errsv = GetLastError ();
358 gchar *emsg = g_win32_error_message (errsv);
360 g_set_error (error, G_IO_ERROR,
361 g_io_error_from_win32_error (errsv),
362 _("Error closing handle: %s"),