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 "giowin32-priv.h"
35 #include "gcancellable.h"
36 #include "gasynchelper.h"
41 * SECTION:gwin32inputstream
42 * @short_description: Streaming input operations for Windows file handles
43 * @include: gio/gwin32inputstream.h
44 * @see_also: #GInputStream
46 * #GWin32InputStream implements #GInputStream for reading from a
47 * Windows file handle.
49 * Note that <filename><gio/gwin32inputstream.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 _GWin32InputStreamPrivate {
62 gboolean close_handle;
66 G_DEFINE_TYPE_WITH_PRIVATE (GWin32InputStream, g_win32_input_stream, G_TYPE_INPUT_STREAM)
68 static void g_win32_input_stream_set_property (GObject *object,
72 static void g_win32_input_stream_get_property (GObject *object,
76 static gssize g_win32_input_stream_read (GInputStream *stream,
79 GCancellable *cancellable,
81 static gboolean g_win32_input_stream_close (GInputStream *stream,
82 GCancellable *cancellable,
86 g_win32_input_stream_class_init (GWin32InputStreamClass *klass)
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
91 gobject_class->get_property = g_win32_input_stream_get_property;
92 gobject_class->set_property = g_win32_input_stream_set_property;
94 stream_class->read_fn = g_win32_input_stream_read;
95 stream_class->close_fn = g_win32_input_stream_close;
98 * GWin32InputStream:handle:
100 * The handle that the stream reads from.
104 g_object_class_install_property (gobject_class,
106 g_param_spec_pointer ("handle",
108 P_("The file handle to read from"),
109 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
112 * GWin32InputStream:close-handle:
114 * Whether to close the file handle when the stream is closed.
118 g_object_class_install_property (gobject_class,
120 g_param_spec_boolean ("close-handle",
121 P_("Close file handle"),
122 P_("Whether to close the file handle when the stream is closed"),
124 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
128 g_win32_input_stream_set_property (GObject *object,
133 GWin32InputStream *win32_stream;
135 win32_stream = G_WIN32_INPUT_STREAM (object);
140 win32_stream->priv->handle = g_value_get_pointer (value);
142 case PROP_CLOSE_HANDLE:
143 win32_stream->priv->close_handle = g_value_get_boolean (value);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 g_win32_input_stream_get_property (GObject *object,
157 GWin32InputStream *win32_stream;
159 win32_stream = G_WIN32_INPUT_STREAM (object);
164 g_value_set_pointer (value, win32_stream->priv->handle);
166 case PROP_CLOSE_HANDLE:
167 g_value_set_boolean (value, win32_stream->priv->close_handle);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 g_win32_input_stream_init (GWin32InputStream *win32_stream)
177 win32_stream->priv = g_win32_input_stream_get_instance_private (win32_stream);
178 win32_stream->priv->handle = NULL;
179 win32_stream->priv->close_handle = TRUE;
180 win32_stream->priv->fd = -1;
184 * g_win32_input_stream_new:
185 * @handle: a Win32 file handle
186 * @close_handle: %TRUE to close the handle when done
188 * Creates a new #GWin32InputStream for the given @handle.
190 * If @close_handle is %TRUE, the handle will be closed
191 * when the stream is closed.
193 * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
194 * as used in the Windows C libraries.
196 * Returns: a new #GWin32InputStream
199 g_win32_input_stream_new (void *handle,
200 gboolean close_handle)
202 GWin32InputStream *stream;
204 g_return_val_if_fail (handle != NULL, NULL);
206 stream = g_object_new (G_TYPE_WIN32_INPUT_STREAM,
208 "close-handle", close_handle,
211 return G_INPUT_STREAM (stream);
215 * g_win32_input_stream_set_close_handle:
216 * @stream: a #GWin32InputStream
217 * @close_handle: %TRUE to close the handle when done
219 * Sets whether the handle of @stream shall be closed
220 * when the stream is closed.
225 g_win32_input_stream_set_close_handle (GWin32InputStream *stream,
226 gboolean close_handle)
228 g_return_if_fail (G_IS_WIN32_INPUT_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_input_stream_get_close_handle:
240 * @stream: a #GWin32InputStream
242 * Returns whether the handle of @stream will be
243 * closed when the stream is closed.
245 * Return value: %TRUE if the handle is closed when done
250 g_win32_input_stream_get_close_handle (GWin32InputStream *stream)
252 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), FALSE);
254 return stream->priv->close_handle;
258 * g_win32_input_stream_get_handle:
259 * @stream: a #GWin32InputStream
261 * Return the Windows file handle that the stream reads from.
263 * Return value: The file handle of @stream
268 g_win32_input_stream_get_handle (GWin32InputStream *stream)
270 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream), NULL);
272 return stream->priv->handle;
276 g_win32_input_stream_read (GInputStream *stream,
279 GCancellable *cancellable,
282 GWin32InputStream *win32_stream;
285 OVERLAPPED overlap = { 0, };
288 win32_stream = G_WIN32_INPUT_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 = ReadFile (win32_stream->priv->handle, buffer, nbytes, &nread, &overlap);
306 int errsv = GetLastError ();
308 if (errsv == ERROR_IO_PENDING &&
309 _g_win32_overlap_wait_result (win32_stream->priv->handle,
310 &overlap, &nread, cancellable))
316 if (g_cancellable_set_error_if_cancelled (cancellable, error))
319 errsv = GetLastError ();
320 if (errsv == ERROR_MORE_DATA)
322 /* If a named pipe is being read in message mode and the
323 * next message is longer than the nNumberOfBytesToRead
324 * parameter specifies, ReadFile returns FALSE and
325 * GetLastError returns ERROR_MORE_DATA */
329 else if (errsv == ERROR_HANDLE_EOF ||
330 errsv == ERROR_BROKEN_PIPE)
332 /* TODO: the other end of a pipe may call the WriteFile
333 * function with nNumberOfBytesToWrite set to zero. In this
334 * case, it's not possible for the caller to know if it's
335 * broken pipe or a read of 0. Perhaps we should add a
336 * is_broken flag for this win32 case.. */
343 emsg = g_win32_error_message (errsv);
344 g_set_error (error, G_IO_ERROR,
345 g_io_error_from_win32_error (errsv),
346 _("Error reading from handle: %s"),
353 CloseHandle (overlap.hEvent);
358 g_win32_input_stream_close (GInputStream *stream,
359 GCancellable *cancellable,
362 GWin32InputStream *win32_stream;
365 win32_stream = G_WIN32_INPUT_STREAM (stream);
367 if (!win32_stream->priv->close_handle)
370 if (win32_stream->priv->fd != -1)
372 if (close (win32_stream->priv->fd) < 0)
374 g_set_error_literal (error, G_IO_ERROR,
375 g_io_error_from_errno (errno),
382 res = CloseHandle (win32_stream->priv->handle);
385 int errsv = GetLastError ();
386 gchar *emsg = g_win32_error_message (errsv);
388 g_set_error (error, G_IO_ERROR,
389 g_io_error_from_win32_error (errsv),
390 _("Error closing handle: %s"),
401 g_win32_input_stream_new_from_fd (gint fd,
404 GWin32InputStream *win32_stream;
406 win32_stream = G_WIN32_INPUT_STREAM (g_win32_input_stream_new ((HANDLE) _get_osfhandle (fd), close_fd));
407 win32_stream->priv->fd = fd;
409 return (GInputStream*)win32_stream;