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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
19 * Author: Tor Lillqvist <tml@iki.fi>
30 #include "gwin32inputstream.h"
31 #include "giowin32-priv.h"
32 #include "gcancellable.h"
33 #include "gasynchelper.h"
37 * SECTION:gwin32inputstream
38 * @short_description: Streaming input operations for Windows file handles
39 * @include: gio/gwin32inputstream.h
40 * @see_also: #GInputStream
42 * #GWin32InputStream implements #GInputStream for reading from a
43 * Windows file handle.
45 * Note that `<gio/gwin32inputstream.h>` belongs to the Windows-specific GIO
46 * interfaces, thus you have to use the `gio-windows-2.0.pc` pkg-config file
50 struct _GWin32InputStreamPrivate
{
52 gboolean close_handle
;
63 static GParamSpec
*props
[LAST_PROP
];
65 G_DEFINE_TYPE_WITH_PRIVATE (GWin32InputStream
, g_win32_input_stream
, G_TYPE_INPUT_STREAM
)
68 g_win32_input_stream_set_property (GObject
*object
,
73 GWin32InputStream
*win32_stream
;
75 win32_stream
= G_WIN32_INPUT_STREAM (object
);
80 win32_stream
->priv
->handle
= g_value_get_pointer (value
);
82 case PROP_CLOSE_HANDLE
:
83 win32_stream
->priv
->close_handle
= g_value_get_boolean (value
);
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
92 g_win32_input_stream_get_property (GObject
*object
,
97 GWin32InputStream
*win32_stream
;
99 win32_stream
= G_WIN32_INPUT_STREAM (object
);
104 g_value_set_pointer (value
, win32_stream
->priv
->handle
);
106 case PROP_CLOSE_HANDLE
:
107 g_value_set_boolean (value
, win32_stream
->priv
->close_handle
);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
115 g_win32_input_stream_read (GInputStream
*stream
,
118 GCancellable
*cancellable
,
121 GWin32InputStream
*win32_stream
;
124 OVERLAPPED overlap
= { 0, };
127 win32_stream
= G_WIN32_INPUT_STREAM (stream
);
129 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
132 if (count
> G_MAXINT
)
137 overlap
.hEvent
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
138 g_return_val_if_fail (overlap
.hEvent
!= NULL
, -1);
140 res
= ReadFile (win32_stream
->priv
->handle
, buffer
, nbytes
, &nread
, &overlap
);
145 int errsv
= GetLastError ();
147 if (errsv
== ERROR_IO_PENDING
&&
148 _g_win32_overlap_wait_result (win32_stream
->priv
->handle
,
149 &overlap
, &nread
, cancellable
))
155 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
158 errsv
= GetLastError ();
159 if (errsv
== ERROR_MORE_DATA
)
161 /* If a named pipe is being read in message mode and the
162 * next message is longer than the nNumberOfBytesToRead
163 * parameter specifies, ReadFile returns FALSE and
164 * GetLastError returns ERROR_MORE_DATA */
168 else if (errsv
== ERROR_HANDLE_EOF
||
169 errsv
== ERROR_BROKEN_PIPE
)
171 /* TODO: the other end of a pipe may call the WriteFile
172 * function with nNumberOfBytesToWrite set to zero. In this
173 * case, it's not possible for the caller to know if it's
174 * broken pipe or a read of 0. Perhaps we should add a
175 * is_broken flag for this win32 case.. */
182 emsg
= g_win32_error_message (errsv
);
183 g_set_error (error
, G_IO_ERROR
,
184 g_io_error_from_win32_error (errsv
),
185 _("Error reading from handle: %s"),
192 CloseHandle (overlap
.hEvent
);
197 g_win32_input_stream_close (GInputStream
*stream
,
198 GCancellable
*cancellable
,
201 GWin32InputStream
*win32_stream
;
204 win32_stream
= G_WIN32_INPUT_STREAM (stream
);
206 if (!win32_stream
->priv
->close_handle
)
209 if (win32_stream
->priv
->fd
!= -1)
211 if (close (win32_stream
->priv
->fd
) < 0)
215 g_set_error (error
, G_IO_ERROR
,
216 g_io_error_from_errno (errsv
),
217 _("Error closing file descriptor: %s"),
224 res
= CloseHandle (win32_stream
->priv
->handle
);
227 int errsv
= GetLastError ();
228 gchar
*emsg
= g_win32_error_message (errsv
);
230 g_set_error (error
, G_IO_ERROR
,
231 g_io_error_from_win32_error (errsv
),
232 _("Error closing handle: %s"),
243 g_win32_input_stream_class_init (GWin32InputStreamClass
*klass
)
245 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
246 GInputStreamClass
*stream_class
= G_INPUT_STREAM_CLASS (klass
);
248 gobject_class
->get_property
= g_win32_input_stream_get_property
;
249 gobject_class
->set_property
= g_win32_input_stream_set_property
;
251 stream_class
->read_fn
= g_win32_input_stream_read
;
252 stream_class
->close_fn
= g_win32_input_stream_close
;
255 * GWin32InputStream:handle:
257 * The handle that the stream reads from.
262 g_param_spec_pointer ("handle",
264 P_("The file handle to read from"),
267 G_PARAM_CONSTRUCT_ONLY
|
268 G_PARAM_STATIC_STRINGS
);
271 * GWin32InputStream:close-handle:
273 * Whether to close the file handle when the stream is closed.
277 props
[PROP_CLOSE_HANDLE
] =
278 g_param_spec_boolean ("close-handle",
279 P_("Close file handle"),
280 P_("Whether to close the file handle when the stream is closed"),
284 G_PARAM_STATIC_STRINGS
);
286 g_object_class_install_properties (gobject_class
, LAST_PROP
, props
);
290 g_win32_input_stream_init (GWin32InputStream
*win32_stream
)
292 win32_stream
->priv
= g_win32_input_stream_get_instance_private (win32_stream
);
293 win32_stream
->priv
->handle
= NULL
;
294 win32_stream
->priv
->close_handle
= TRUE
;
295 win32_stream
->priv
->fd
= -1;
299 * g_win32_input_stream_new:
300 * @handle: a Win32 file handle
301 * @close_handle: %TRUE to close the handle when done
303 * Creates a new #GWin32InputStream for the given @handle.
305 * If @close_handle is %TRUE, the handle will be closed
306 * when the stream is closed.
308 * Note that "handle" here means a Win32 HANDLE, not a "file descriptor"
309 * as used in the Windows C libraries.
311 * Returns: a new #GWin32InputStream
314 g_win32_input_stream_new (void *handle
,
315 gboolean close_handle
)
317 GWin32InputStream
*stream
;
319 g_return_val_if_fail (handle
!= NULL
, NULL
);
321 stream
= g_object_new (G_TYPE_WIN32_INPUT_STREAM
,
323 "close-handle", close_handle
,
326 return G_INPUT_STREAM (stream
);
330 * g_win32_input_stream_set_close_handle:
331 * @stream: a #GWin32InputStream
332 * @close_handle: %TRUE to close the handle when done
334 * Sets whether the handle of @stream shall be closed
335 * when the stream is closed.
340 g_win32_input_stream_set_close_handle (GWin32InputStream
*stream
,
341 gboolean close_handle
)
343 g_return_if_fail (G_IS_WIN32_INPUT_STREAM (stream
));
345 close_handle
= close_handle
!= FALSE
;
346 if (stream
->priv
->close_handle
!= close_handle
)
348 stream
->priv
->close_handle
= close_handle
;
349 g_object_notify (G_OBJECT (stream
), "close-handle");
354 * g_win32_input_stream_get_close_handle:
355 * @stream: a #GWin32InputStream
357 * Returns whether the handle of @stream will be
358 * closed when the stream is closed.
360 * Returns: %TRUE if the handle is closed when done
365 g_win32_input_stream_get_close_handle (GWin32InputStream
*stream
)
367 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream
), FALSE
);
369 return stream
->priv
->close_handle
;
373 * g_win32_input_stream_get_handle:
374 * @stream: a #GWin32InputStream
376 * Return the Windows file handle that the stream reads from.
378 * Returns: The file handle of @stream
383 g_win32_input_stream_get_handle (GWin32InputStream
*stream
)
385 g_return_val_if_fail (G_IS_WIN32_INPUT_STREAM (stream
), NULL
);
387 return stream
->priv
->handle
;
391 g_win32_input_stream_new_from_fd (gint fd
,
394 GWin32InputStream
*win32_stream
;
396 win32_stream
= G_WIN32_INPUT_STREAM (g_win32_input_stream_new ((HANDLE
) _get_osfhandle (fd
), close_fd
));
397 win32_stream
->priv
->fd
= fd
;
399 return (GInputStream
*)win32_stream
;