1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 "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
53 struct _GWin32OutputStreamPrivate {
55 gboolean close_handle;
66 static GParamSpec *props[LAST_PROP];
68 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM)
71 g_win32_output_stream_set_property (GObject *object,
76 GWin32OutputStream *win32_stream;
78 win32_stream = G_WIN32_OUTPUT_STREAM (object);
83 win32_stream->priv->handle = g_value_get_pointer (value);
85 case PROP_CLOSE_HANDLE:
86 win32_stream->priv->close_handle = g_value_get_boolean (value);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_win32_output_stream_get_property (GObject *object,
100 GWin32OutputStream *win32_stream;
102 win32_stream = G_WIN32_OUTPUT_STREAM (object);
107 g_value_set_pointer (value, win32_stream->priv->handle);
109 case PROP_CLOSE_HANDLE:
110 g_value_set_boolean (value, win32_stream->priv->close_handle);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_win32_output_stream_write (GOutputStream *stream,
121 GCancellable *cancellable,
124 GWin32OutputStream *win32_stream;
126 DWORD nbytes, nwritten;
127 OVERLAPPED overlap = { 0, };
130 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
132 if (g_cancellable_set_error_if_cancelled (cancellable, error))
135 if (count > G_MAXINT)
140 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
141 g_return_val_if_fail (overlap.hEvent != NULL, -1);
143 res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, &overlap);
148 int errsv = GetLastError ();
150 if (errsv == ERROR_IO_PENDING &&
151 _g_win32_overlap_wait_result (win32_stream->priv->handle,
152 &overlap, &nwritten, cancellable))
158 if (g_cancellable_set_error_if_cancelled (cancellable, error))
161 errsv = GetLastError ();
162 if (errsv == ERROR_HANDLE_EOF ||
163 errsv == ERROR_BROKEN_PIPE)
171 emsg = g_win32_error_message (errsv);
172 g_set_error (error, G_IO_ERROR,
173 g_io_error_from_win32_error (errsv),
174 _("Error writing to handle: %s"),
181 CloseHandle (overlap.hEvent);
186 g_win32_output_stream_close (GOutputStream *stream,
187 GCancellable *cancellable,
190 GWin32OutputStream *win32_stream;
193 win32_stream = G_WIN32_OUTPUT_STREAM (stream);
195 if (!win32_stream->priv->close_handle)
198 if (win32_stream->priv->fd != -1)
200 if (close (win32_stream->priv->fd) < 0)
204 g_set_error (error, G_IO_ERROR,
205 g_io_error_from_errno (errsv),
206 _("Error closing file descriptor: %s"),
213 res = CloseHandle (win32_stream->priv->handle);
216 int errsv = GetLastError ();
217 gchar *emsg = g_win32_error_message (errsv);
219 g_set_error (error, G_IO_ERROR,
220 g_io_error_from_win32_error (errsv),
221 _("Error closing handle: %s"),
232 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
235 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
237 gobject_class->get_property = g_win32_output_stream_get_property;
238 gobject_class->set_property = g_win32_output_stream_set_property;
240 stream_class->write_fn = g_win32_output_stream_write;
241 stream_class->close_fn = g_win32_output_stream_close;
244 * GWin32OutputStream:handle:
246 * The file handle that the stream writes to.
251 g_param_spec_pointer ("handle",
253 P_("The file handle to write to"),
256 G_PARAM_CONSTRUCT_ONLY |
257 G_PARAM_STATIC_STRINGS);
260 * GWin32OutputStream:close-handle:
262 * Whether to close the file handle when the stream is closed.
266 props[PROP_CLOSE_HANDLE] =
267 g_param_spec_boolean ("close-handle",
268 P_("Close file handle"),
269 P_("Whether to close the file handle when the stream is closed"),
273 G_PARAM_STATIC_STRINGS);
275 g_object_class_install_properties (gobject_class, LAST_PROP, props);
279 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
281 win32_stream->priv = g_win32_output_stream_get_instance_private (win32_stream);
282 win32_stream->priv->handle = NULL;
283 win32_stream->priv->close_handle = TRUE;
284 win32_stream->priv->fd = -1;
288 * g_win32_output_stream_new:
289 * @handle: a Win32 file handle
290 * @close_handle: %TRUE to close the handle when done
292 * Creates a new #GWin32OutputStream for the given @handle.
294 * If @close_handle, is %TRUE, the handle will be closed when the
295 * output stream is destroyed.
297 * Returns: a new #GOutputStream
302 g_win32_output_stream_new (void *handle,
303 gboolean close_handle)
305 GWin32OutputStream *stream;
307 g_return_val_if_fail (handle != NULL, NULL);
309 stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
311 "close-handle", close_handle,
314 return G_OUTPUT_STREAM (stream);
318 * g_win32_output_stream_set_close_handle:
319 * @stream: a #GWin32OutputStream
320 * @close_handle: %TRUE to close the handle when done
322 * Sets whether the handle of @stream shall be closed when the stream
328 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
329 gboolean close_handle)
331 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
333 close_handle = close_handle != FALSE;
334 if (stream->priv->close_handle != close_handle)
336 stream->priv->close_handle = close_handle;
337 g_object_notify (G_OBJECT (stream), "close-handle");
342 * g_win32_output_stream_get_close_handle:
343 * @stream: a #GWin32OutputStream
345 * Returns whether the handle of @stream will be closed when the
348 * Returns: %TRUE if the handle is closed when done
353 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
355 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
357 return stream->priv->close_handle;
361 * g_win32_output_stream_get_handle:
362 * @stream: a #GWin32OutputStream
364 * Return the Windows handle that the stream writes to.
366 * Returns: The handle descriptor of @stream
371 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
373 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
375 return stream->priv->handle;
379 g_win32_output_stream_new_from_fd (gint fd,
382 GWin32OutputStream *win32_stream;
384 win32_stream = G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
385 win32_stream->priv->fd = fd;
387 return (GOutputStream*)win32_stream;