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 "giowin32-priv.h"
35 #include "gcancellable.h"
36 #include "gsimpleasyncresult.h"
37 #include "gasynchelper.h"
42 * SECTION:gwin32outputstream
43 * @short_description: Streaming output operations for Windows file handles
44 * @include: gio/gwin32outputstream.h
45 * @see_also: #GOutputStream
47 * #GWin32OutputStream implements #GOutputStream for writing to a
48 * Windows file handle.
50 * Note that <filename><gio/gwin32outputstream.h></filename> belongs
51 * to the Windows-specific GIO interfaces, thus you have to use the
52 * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
61 struct _GWin32OutputStreamPrivate {
63 gboolean close_handle;
67 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
69 static void g_win32_output_stream_set_property (GObject *object,
73 static void g_win32_output_stream_get_property (GObject *object,
77 static gssize g_win32_output_stream_write (GOutputStream *stream,
80 GCancellable *cancellable,
82 static gboolean g_win32_output_stream_close (GOutputStream *stream,
83 GCancellable *cancellable,
88 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
90 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
93 gobject_class->get_property = g_win32_output_stream_get_property;
94 gobject_class->set_property = g_win32_output_stream_set_property;
96 stream_class->write_fn = g_win32_output_stream_write;
97 stream_class->close_fn = g_win32_output_stream_close;
100 * GWin32OutputStream:handle:
102 * The file handle that the stream writes to.
106 g_object_class_install_property (gobject_class,
108 g_param_spec_pointer ("handle",
110 P_("The file handle to write to"),
111 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
114 * GWin32OutputStream:close-handle:
116 * Whether to close the file handle when the stream is closed.
120 g_object_class_install_property (gobject_class,
122 g_param_spec_boolean ("close-handle",
123 P_("Close file handle"),
124 P_("Whether to close the file handle when the stream is closed"),
126 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
130 g_win32_output_stream_set_property (GObject *object,
135 GWin32OutputStream *win32_stream;
137 win32_stream = G_WIN32_OUTPUT_STREAM (object);
142 win32_stream->priv->handle = g_value_get_pointer (value);
144 case PROP_CLOSE_HANDLE:
145 win32_stream->priv->close_handle = g_value_get_boolean (value);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 g_win32_output_stream_get_property (GObject *object,
159 GWin32OutputStream *win32_stream;
161 win32_stream = G_WIN32_OUTPUT_STREAM (object);
166 g_value_set_pointer (value, win32_stream->priv->handle);
168 case PROP_CLOSE_HANDLE:
169 g_value_set_boolean (value, win32_stream->priv->close_handle);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
179 win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
180 win32_stream->priv->handle = NULL;
181 win32_stream->priv->close_handle = TRUE;
182 win32_stream->priv->fd = -1;
186 * g_win32_output_stream_new:
187 * @handle: a Win32 file handle
188 * @close_handle: %TRUE to close the handle when done
190 * Creates a new #GWin32OutputStream for the given @handle.
192 * If @close_handle, is %TRUE, the handle will be closed when the
193 * output stream is destroyed.
195 * Returns: a new #GOutputStream
200 g_win32_output_stream_new (void *handle,
201 gboolean close_handle)
203 GWin32OutputStream *stream;
205 g_return_val_if_fail (handle != NULL, NULL);
207 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
209 "close-handle", close_handle,
212 return G_OUTPUT_STREAM (stream);
216 * g_win32_output_stream_set_close_handle:
217 * @stream: a #GWin32OutputStream
218 * @close_handle: %TRUE to close the handle when done
220 * Sets whether the handle of @stream shall be closed when the stream
226 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
227 gboolean close_handle)
229 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
231 close_handle = close_handle != FALSE;
232 if (stream->priv->close_handle != close_handle)
234 stream->priv->close_handle = close_handle;
235 g_object_notify (G_OBJECT (stream), "close-handle");
240 * g_win32_output_stream_get_close_handle:
241 * @stream: a #GWin32OutputStream
243 * Returns whether the handle of @stream will be closed when the
246 * Return value: %TRUE if the handle is closed when done
251 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
253 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
255 return stream->priv->close_handle;
259 * g_win32_output_stream_get_handle:
260 * @stream: a #GWin32OutputStream
262 * Return the Windows handle that the stream writes to.
264 * Return value: The handle descriptor of @stream
269 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
271 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
273 return stream->priv->handle;
277 g_win32_output_stream_write (GOutputStream *stream,
280 GCancellable *cancellable,
283 GWin32OutputStream *win32_stream;
285 DWORD nbytes, nwritten;
286 OVERLAPPED overlap = { 0, };
289 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
291 if (g_cancellable_set_error_if_cancelled (cancellable, error))
294 if (count > G_MAXINT)
299 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
300 g_return_val_if_fail (overlap.hEvent != NULL, -1);
302 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
307 int errsv = GetLastError ();
309 if (errsv == ERROR_IO_PENDING &&
310 _g_win32_overlap_wait_result (win32_stream->priv->handle,
311 &overlap, &nwritten, cancellable))
317 if (g_cancellable_set_error_if_cancelled (cancellable, error))
320 errsv = GetLastError ();
321 if (errsv == ERROR_HANDLE_EOF ||
322 errsv == ERROR_BROKEN_PIPE)
330 emsg = g_win32_error_message (errsv);
331 g_set_error (error, G_IO_ERROR,
332 g_io_error_from_win32_error (errsv),
333 _("Error writing to handle: %s"),
340 CloseHandle (overlap.hEvent);
345 g_win32_output_stream_close (GOutputStream *stream,
346 GCancellable *cancellable,
349 GWin32OutputStream *win32_stream;
352 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
354 if (!win32_stream->priv->close_handle)
357 if (win32_stream->priv->fd != -1)
359 if (close (win32_stream->priv->fd) < 0)
361 g_set_error_literal (error, G_IO_ERROR,
362 g_io_error_from_errno (errno),
369 res = CloseHandle (win32_stream->priv->handle);
372 int errsv = GetLastError ();
373 gchar *emsg = g_win32_error_message (errsv);
375 g_set_error (error, G_IO_ERROR,
376 g_io_error_from_win32_error (errsv),
377 _("Error closing handle: %s"),
388 g_win32_output_stream_new_from_fd (gint fd,
391 GWin32OutputStream *win32_stream;
393 win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
394 win32_stream->priv->fd = fd;
396 return (GOutputStream*)win32_stream;