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>
32 #include "gsimpleasyncresult.h"
33 #include "gwin32inputstream.h"
34 #include "gcancellable.h"
35 #include "gasynchelper.h"
40 * SECTION:gwin32inputstream
41 * @short_description: Streaming input operations for Windows file handles
42 * @include: gio/gwin32inputstream.h
43 * @see_also: #GInputStream
45 * #GWin32InputStream implements #GInputStream for reading from a
46 * Windows file handle.
48 * Note that <filename><gio/gwin32inputstream.h></filename> belongs
49 * to the Windows-specific GIO interfaces, thus you have to use the
50 * <filename>gio-windows-2.0.pc</filename> pkg-config file when using it.
59 struct _GWin32InputStreamPrivate {
61 gboolean close_handle;
64 G_DEFINE_TYPE_WITH_PRIVATE (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM)
66 static void g_win32_input_stream_set_property (GObject *object,
70 static void g_win32_input_stream_get_property (GObject *object,
74 static gssize g_win32_input_stream_read (GInputStream *stream,
77 GCancellable *cancellable,
79 static gboolean g_win32_input_stream_close (GInputStream *stream,
80 GCancellable *cancellable,
84 g_win32_input_stream_class_init (GWin32InputStreamClass *klass)
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
89 gobject_class->get_property = g_win32_input_stream_get_property;
90 gobject_class->set_property = g_win32_input_stream_set_property;
92 stream_class->read_fn = g_win32_input_stream_read;
93 stream_class->close_fn = g_win32_input_stream_close;
96 * GWin32InputStream:handle:
98 * The handle that the stream reads from.
102 g_object_class_install_property (gobject_class,
104 g_param_spec_pointer ("handle",
106 P_("The file handle to read from"),
107 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
110 * GWin32InputStream:close-handle:
112 * Whether to close the file handle when the stream is closed.
116 g_object_class_install_property (gobject_class,
118 g_param_spec_boolean ("close-handle",
119 P_("Close file handle"),
120 P_("Whether to close the file handle when the stream is closed"),
122 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
126 g_win32_input_stream_set_property (GObject *object,
131 GWin32InputStream *win32_stream;
133 win32_stream = G_WIN32_INPUT_STREAM (object);
138 win32_stream->priv->handle = g_value_get_pointer (value);
140 case PROP_CLOSE_HANDLE:
141 win32_stream->priv->close_handle = g_value_get_boolean (value);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 g_win32_input_stream_get_property (GObject *object,
155 GWin32InputStream *win32_stream;
157 win32_stream = G_WIN32_INPUT_STREAM (object);
162 g_value_set_pointer (value, win32_stream->priv->handle);
164 case PROP_CLOSE_HANDLE:
165 g_value_set_boolean (value, win32_stream->priv->close_handle);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 g_win32_input_stream_init (GWin32InputStream *win32_stream)
175 win32_stream->priv = g_win32_input_stream_get_private (win32_stream);
176 win32_stream->priv->handle = NULL;
177 win32_stream->priv->close_handle = TRUE;
181 * g_win32_input_stream_new:
182 * @handle: a Win32 file handle
183 * @close_handle: %TRUE to close the handle when done
185 * Creates a new #GWin32InputStream for the given @handle.
187 * If @close_handle is %TRUE, the handle will be closed
188 * when the stream is closed.
190 * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
191 * as used in the Windows C libraries.
193 * Returns: a new #GWin32InputStream
196 g_win32_input_stream_new (void *handle,
197 gboolean close_handle)
199 GWin32InputStream *stream;
201 g_return_val_if_fail (handle != NULL, NULL);
203 stream = g_object_new (G_TYPE_WIN32_INPUT_STREAM,
205 "close-handle", close_handle,
208 return G_INPUT_STREAM (stream);
212 * g_win32_input_stream_set_close_handle:
213 * @stream: a #GWin32InputStream
214 * @close_handle: %TRUE to close the handle when done
216 * Sets whether the handle of @stream shall be closed
217 * when the stream is closed.
222 g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
223 gboolean close_handle)
225 g_return_if_fail (G_IS_WIN32_INPUT_STREAM (stream));
227 close_handle = close_handle != FALSE;
228 if (stream->priv->close_handle != close_handle)
230 stream->priv->close_handle = close_handle;
231 g_object_notify (G_OBJECT (stream), "close-handle");
236 * g_win32_input_stream_get_close_handle:
237 * @stream: a #GWin32InputStream
239 * Returns whether the handle of @stream will be
240 * closed when the stream is closed.
242 * Return value: %TRUE if the handle is closed when done
247 g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
249 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), FALSE);
251 return stream->priv->close_handle;
255 * g_win32_input_stream_get_handle:
256 * @stream: a #GWin32InputStream
258 * Return the Windows file handle that the stream reads from.
260 * Return value: The file handle of @stream
265 g_win32_input_stream_get_handle (GWin32InputStream *stream)
267 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), NULL);
269 return stream->priv->handle;
273 g_win32_input_stream_read (GInputStream *stream,
276 GCancellable *cancellable,
279 GWin32InputStream *win32_stream;
282 OVERLAPPED overlap = { 0, };
285 win32_stream = G_WIN32_INPUT_STREAM (stream);
287 if (g_cancellable_set_error_if_cancelled (cancellable, error))
290 if (count > G_MAXINT)
295 overlap.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
296 g_return_val_if_fail (overlap.hEvent != NULL, -1);
298 res = ReadFile (win32_stream->priv->handle, buffer, nbytes, &nread, &overlap);
303 int errsv = GetLastError ();
305 if (errsv == ERROR_IO_PENDING &&
306 _g_win32_overlap_wait_result (win32_stream->priv->handle,
307 &overlap, &nread, cancellable))
313 if (g_cancellable_set_error_if_cancelled (cancellable, error))
316 errsv = GetLastError ();
317 if (errsv == ERROR_MORE_DATA)
319 /* If a named pipe is being read in message mode and the
320 * next message is longer than the nNumberOfBytesToRead
321 * parameter specifies, ReadFile returns FALSE and
322 * GetLastError returns ERROR_MORE_DATA */
326 else if (errsv == ERROR_HANDLE_EOF ||
327 errsv == ERROR_BROKEN_PIPE)
329 /* TODO: the other end of a pipe may call the WriteFile
330 * function with nNumberOfBytesToWrite set to zero. In this
331 * case, it's not possible for the caller to know if it's
332 * broken pipe or a read of 0. Perhaps we should add a
333 * is_broken flag for this win32 case.. */
340 emsg = g_win32_error_message (errsv);
341 g_set_error (error, G_IO_ERROR,
342 g_io_error_from_win32_error (errsv),
343 _("Error reading from handle: %s"),
350 CloseHandle (overlap.hEvent);
355 g_win32_input_stream_close (GInputStream *stream,
356 GCancellable *cancellable,
359 GWin32InputStream *win32_stream;
362 win32_stream = G_WIN32_INPUT_STREAM (stream);
364 if (!win32_stream->priv->close_handle)
367 res = CloseHandle (win32_stream->priv->handle);
370 int errsv = GetLastError ();
371 gchar *emsg = g_win32_error_message (errsv);
373 g_set_error (error, G_IO_ERROR,
374 g_io_error_from_win32_error (errsv),
375 _("Error closing handle: %s"),