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;
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
68 static void g_win32_output_stream_set_property (GObject *object,
72 static void g_win32_output_stream_get_property (GObject *object,
76 static gssize g_win32_output_stream_write (GOutputStream *stream,
79 GCancellable *cancellable,
81 static gboolean g_win32_output_stream_close (GOutputStream *stream,
82 GCancellable *cancellable,
87 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
92 gobject_class->get_property = g_win32_output_stream_get_property;
93 gobject_class->set_property = g_win32_output_stream_set_property;
95 stream_class->write_fn = g_win32_output_stream_write;
96 stream_class->close_fn = g_win32_output_stream_close;
99 * GWin32OutputStream:handle:
101 * The file handle that the stream writes to.
105 g_object_class_install_property (gobject_class,
107 g_param_spec_pointer ("handle",
109 P_("The file handle to write to"),
110 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
113 * GWin32OutputStream:close-handle:
115 * Whether to close the file handle when the stream is closed.
119 g_object_class_install_property (gobject_class,
121 g_param_spec_boolean ("close-handle",
122 P_("Close file handle"),
123 P_("Whether to close the file handle when the stream is closed"),
125 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
129 g_win32_output_stream_set_property (GObject *object,
134 GWin32OutputStream *win32_stream;
136 win32_stream = G_WIN32_OUTPUT_STREAM (object);
141 win32_stream->priv->handle = g_value_get_pointer (value);
143 case PROP_CLOSE_HANDLE:
144 win32_stream->priv->close_handle = g_value_get_boolean (value);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 g_win32_output_stream_get_property (GObject *object,
158 GWin32OutputStream *win32_stream;
160 win32_stream = G_WIN32_OUTPUT_STREAM (object);
165 g_value_set_pointer (value, win32_stream->priv->handle);
167 case PROP_CLOSE_HANDLE:
168 g_value_set_boolean (value, win32_stream->priv->close_handle);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
178 win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
179 win32_stream->priv->handle = NULL;
180 win32_stream->priv->close_handle = TRUE;
181 win32_stream->priv->fd = -1;
185 * g_win32_output_stream_new:
186 * @handle: a Win32 file handle
187 * @close_handle: %TRUE to close the handle when done
189 * Creates a new #GWin32OutputStream for the given @handle.
191 * If @close_handle, is %TRUE, the handle will be closed when the
192 * output stream is destroyed.
194 * Returns: a new #GOutputStream
199 g_win32_output_stream_new (void *handle,
200 gboolean close_handle)
202 GWin32OutputStream *stream;
204 g_return_val_if_fail (handle != NULL, NULL);
206 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
208 "close-handle", close_handle,
211 return G_OUTPUT_STREAM (stream);
215 * g_win32_output_stream_set_close_handle:
216 * @stream: a #GWin32OutputStream
217 * @close_handle: %TRUE to close the handle when done
219 * Sets whether the handle of @stream shall be closed when the stream
225 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
226 gboolean close_handle)
228 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
230 close_handle = close_handle != FALSE;
231 if (stream->priv->close_handle != close_handle)
233 stream->priv->close_handle = close_handle;
234 g_object_notify (G_OBJECT (stream), "close-handle");
239 * g_win32_output_stream_get_close_handle:
240 * @stream: a #GWin32OutputStream
242 * Returns whether the handle of @stream will be closed when the
245 * Return value: %TRUE if the handle is closed when done
250 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
252 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
254 return stream->priv->close_handle;
258 * g_win32_output_stream_get_handle:
259 * @stream: a #GWin32OutputStream
261 * Return the Windows handle that the stream writes to.
263 * Return value: The handle descriptor of @stream
268 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
270 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
272 return stream->priv->handle;
276 g_win32_output_stream_write (GOutputStream *stream,
279 GCancellable *cancellable,
282 GWin32OutputStream *win32_stream;
284 DWORD nbytes, nwritten;
285 OVERLAPPED overlap = { 0, };
288 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
290 if (g_cancellable_set_error_if_cancelled (cancellable, error))
293 if (count > G_MAXINT)
298 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
299 g_return_val_if_fail (overlap.hEvent != NULL, -1);
301 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
306 int errsv = GetLastError ();
308 if (errsv == ERROR_IO_PENDING &&
309 _g_win32_overlap_wait_result (win32_stream->priv->handle,
310 &overlap, &nwritten, cancellable))
316 if (g_cancellable_set_error_if_cancelled (cancellable, error))
319 errsv = GetLastError ();
320 if (errsv == ERROR_HANDLE_EOF ||
321 errsv == ERROR_BROKEN_PIPE)
329 emsg = g_win32_error_message (errsv);
330 g_set_error (error, G_IO_ERROR,
331 g_io_error_from_win32_error (errsv),
332 _("Error writing to handle: %s"),
339 CloseHandle (overlap.hEvent);
344 g_win32_output_stream_close (GOutputStream *stream,
345 GCancellable *cancellable,
348 GWin32OutputStream *win32_stream;
351 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
353 if (!win32_stream->priv->close_handle)
356 if (win32_stream->priv->fd != -1)
358 if (close (win32_stream->priv->fd) < 0)
360 g_set_error_literal (error, G_IO_ERROR,
361 g_io_error_from_errno (errno),
368 res = CloseHandle (win32_stream->priv->handle);
371 int errsv = GetLastError ();
372 gchar *emsg = g_win32_error_message (errsv);
374 g_set_error (error, G_IO_ERROR,
375 g_io_error_from_win32_error (errsv),
376 _("Error closing handle: %s"),
387 g_win32_output_stream_new_from_fd (gint fd,
390 GWin32OutputStream *win32_stream;
392 win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
393 win32_stream->priv->fd = fd;
395 return (GOutputStream*)win32_stream;