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.1 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 "gasynchelper.h"
38 * SECTION:gwin32outputstream
39 * @short_description: Streaming output operations for Windows file handles
40 * @include: gio/gwin32outputstream.h
41 * @see_also: #GOutputStream
43 * #GWin32OutputStream implements #GOutputStream for writing to a
44 * Windows file handle.
46 * Note that `<gio/gwin32outputstream.h>` belongs to the Windows-specific GIO
47 * interfaces, thus you have to use the `gio-windows-2.0.pc` pkg-config file
51 struct _GWin32OutputStreamPrivate
{
53 gboolean close_handle
;
64 static GParamSpec
*props
[LAST_PROP
];
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32OutputStream
, g_win32_output_stream
, G_TYPE_OUTPUT_STREAM
)
69 g_win32_output_stream_set_property (GObject
*object
,
74 GWin32OutputStream
*win32_stream
;
76 win32_stream
= G_WIN32_OUTPUT_STREAM (object
);
81 win32_stream
->priv
->handle
= g_value_get_pointer (value
);
83 case PROP_CLOSE_HANDLE
:
84 win32_stream
->priv
->close_handle
= g_value_get_boolean (value
);
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
93 g_win32_output_stream_get_property (GObject
*object
,
98 GWin32OutputStream
*win32_stream
;
100 win32_stream
= G_WIN32_OUTPUT_STREAM (object
);
105 g_value_set_pointer (value
, win32_stream
->priv
->handle
);
107 case PROP_CLOSE_HANDLE
:
108 g_value_set_boolean (value
, win32_stream
->priv
->close_handle
);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
116 g_win32_output_stream_write (GOutputStream
*stream
,
119 GCancellable
*cancellable
,
122 GWin32OutputStream
*win32_stream
;
124 DWORD nbytes
, nwritten
;
125 OVERLAPPED overlap
= { 0, };
128 win32_stream
= G_WIN32_OUTPUT_STREAM (stream
);
130 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
133 if (count
> G_MAXINT
)
138 overlap
.hEvent
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
139 g_return_val_if_fail (overlap
.hEvent
!= NULL
, -1);
141 res
= WriteFile (win32_stream
->priv
->handle
, buffer
, nbytes
, &nwritten
, &overlap
);
146 int errsv
= GetLastError ();
148 if (errsv
== ERROR_IO_PENDING
&&
149 _g_win32_overlap_wait_result (win32_stream
->priv
->handle
,
150 &overlap
, &nwritten
, cancellable
))
156 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
159 errsv
= GetLastError ();
160 if (errsv
== ERROR_HANDLE_EOF
||
161 errsv
== ERROR_BROKEN_PIPE
)
169 emsg
= g_win32_error_message (errsv
);
170 g_set_error (error
, G_IO_ERROR
,
171 g_io_error_from_win32_error (errsv
),
172 _("Error writing to handle: %s"),
179 CloseHandle (overlap
.hEvent
);
184 g_win32_output_stream_close (GOutputStream
*stream
,
185 GCancellable
*cancellable
,
188 GWin32OutputStream
*win32_stream
;
191 win32_stream
= G_WIN32_OUTPUT_STREAM (stream
);
193 if (!win32_stream
->priv
->close_handle
)
196 if (win32_stream
->priv
->fd
!= -1)
198 if (close (win32_stream
->priv
->fd
) < 0)
202 g_set_error (error
, G_IO_ERROR
,
203 g_io_error_from_errno (errsv
),
204 _("Error closing file descriptor: %s"),
211 res
= CloseHandle (win32_stream
->priv
->handle
);
214 int errsv
= GetLastError ();
215 gchar
*emsg
= g_win32_error_message (errsv
);
217 g_set_error (error
, G_IO_ERROR
,
218 g_io_error_from_win32_error (errsv
),
219 _("Error closing handle: %s"),
230 g_win32_output_stream_class_init (GWin32OutputStreamClass
*klass
)
232 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
233 GOutputStreamClass
*stream_class
= G_OUTPUT_STREAM_CLASS (klass
);
235 gobject_class
->get_property
= g_win32_output_stream_get_property
;
236 gobject_class
->set_property
= g_win32_output_stream_set_property
;
238 stream_class
->write_fn
= g_win32_output_stream_write
;
239 stream_class
->close_fn
= g_win32_output_stream_close
;
242 * GWin32OutputStream:handle:
244 * The file handle that the stream writes to.
249 g_param_spec_pointer ("handle",
251 P_("The file handle to write to"),
254 G_PARAM_CONSTRUCT_ONLY
|
255 G_PARAM_STATIC_STRINGS
);
258 * GWin32OutputStream:close-handle:
260 * Whether to close the file handle when the stream is closed.
264 props
[PROP_CLOSE_HANDLE
] =
265 g_param_spec_boolean ("close-handle",
266 P_("Close file handle"),
267 P_("Whether to close the file handle when the stream is closed"),
271 G_PARAM_STATIC_STRINGS
);
273 g_object_class_install_properties (gobject_class
, LAST_PROP
, props
);
277 g_win32_output_stream_init (GWin32OutputStream
*win32_stream
)
279 win32_stream
->priv
= g_win32_output_stream_get_instance_private (win32_stream
);
280 win32_stream
->priv
->handle
= NULL
;
281 win32_stream
->priv
->close_handle
= TRUE
;
282 win32_stream
->priv
->fd
= -1;
286 * g_win32_output_stream_new:
287 * @handle: a Win32 file handle
288 * @close_handle: %TRUE to close the handle when done
290 * Creates a new #GWin32OutputStream for the given @handle.
292 * If @close_handle, is %TRUE, the handle will be closed when the
293 * output stream is destroyed.
295 * Returns: a new #GOutputStream
300 g_win32_output_stream_new (void *handle
,
301 gboolean close_handle
)
303 GWin32OutputStream
*stream
;
305 g_return_val_if_fail (handle
!= NULL
, NULL
);
307 stream
= g_object_new (G_TYPE_WIN32_OUTPUT_STREAM
,
309 "close-handle", close_handle
,
312 return G_OUTPUT_STREAM (stream
);
316 * g_win32_output_stream_set_close_handle:
317 * @stream: a #GWin32OutputStream
318 * @close_handle: %TRUE to close the handle when done
320 * Sets whether the handle of @stream shall be closed when the stream
326 g_win32_output_stream_set_close_handle (GWin32OutputStream
*stream
,
327 gboolean close_handle
)
329 g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream
));
331 close_handle
= close_handle
!= FALSE
;
332 if (stream
->priv
->close_handle
!= close_handle
)
334 stream
->priv
->close_handle
= close_handle
;
335 g_object_notify (G_OBJECT (stream
), "close-handle");
340 * g_win32_output_stream_get_close_handle:
341 * @stream: a #GWin32OutputStream
343 * Returns whether the handle of @stream will be closed when the
346 * Returns: %TRUE if the handle is closed when done
351 g_win32_output_stream_get_close_handle (GWin32OutputStream
*stream
)
353 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream
), FALSE
);
355 return stream
->priv
->close_handle
;
359 * g_win32_output_stream_get_handle:
360 * @stream: a #GWin32OutputStream
362 * Return the Windows handle that the stream writes to.
364 * Returns: The handle descriptor of @stream
369 g_win32_output_stream_get_handle (GWin32OutputStream
*stream
)
371 g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream
), NULL
);
373 return stream
->priv
->handle
;
377 g_win32_output_stream_new_from_fd (gint fd
,
380 GWin32OutputStream
*win32_stream
;
382 win32_stream
= G_WIN32_OUTPUT_STREAM (g_win32_output_stream_new ((HANDLE
) _get_osfhandle (fd
), close_fd
));
383 win32_stream
->priv
->fd
= fd
;
385 return (GOutputStream
*)win32_stream
;