GSettings: small internal refactor
[glib.git] / gio / gwin32inputstream.c
blob0d9f5c0235bf88716bb3fda4a6b797ae6e140d1e
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>
24 #include "config.h"
26 #include <windows.h>
28 #include <io.h>
30 #include <glib.h>
31 #include "gioerror.h"
32 #include "gsimpleasyncresult.h"
33 #include "gwin32inputstream.h"
34 #include "giowin32-priv.h"
35 #include "gcancellable.h"
36 #include "gasynchelper.h"
37 #include "glibintl.h"
40 /**
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>&lt;gio/gwin32inputstream.h&gt;</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.
54 enum {
55 PROP_0,
56 PROP_HANDLE,
57 PROP_CLOSE_HANDLE
60 struct _GWin32InputStreamPrivate {
61 HANDLE handle;
62 gboolean close_handle;
63 gint fd;
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,
69 guint prop_id,
70 const GValue *value,
71 GParamSpec *pspec);
72 static void g_win32_input_stream_get_property (GObject *object,
73 guint prop_id,
74 GValue *value,
75 GParamSpec *pspec);
76 static gssize g_win32_input_stream_read (GInputStream *stream,
77 void *buffer,
78 gsize count,
79 GCancellable *cancellable,
80 GError **error);
81 static gboolean g_win32_input_stream_close (GInputStream *stream,
82 GCancellable *cancellable,
83 GError **error);
85 static void
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;
97 /**
98 * GWin32InputStream:handle:
100 * The handle that the stream reads from.
102 * Since: 2.26
104 g_object_class_install_property (gobject_class,
105 PROP_HANDLE,
106 g_param_spec_pointer ("handle",
107 P_("File 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.
116 * Since: 2.26
118 g_object_class_install_property (gobject_class,
119 PROP_CLOSE_HANDLE,
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"),
123 TRUE,
124 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
127 static void
128 g_win32_input_stream_set_property (GObject *object,
129 guint prop_id,
130 const GValue *value,
131 GParamSpec *pspec)
133 GWin32InputStream *win32_stream;
135 win32_stream = G_WIN32_INPUT_STREAM (object);
137 switch (prop_id)
139 case PROP_HANDLE:
140 win32_stream->priv->handle = g_value_get_pointer (value);
141 break;
142 case PROP_CLOSE_HANDLE:
143 win32_stream->priv->close_handle = g_value_get_boolean (value);
144 break;
145 default:
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 break;
151 static void
152 g_win32_input_stream_get_property (GObject *object,
153 guint prop_id,
154 GValue *value,
155 GParamSpec *pspec)
157 GWin32InputStream *win32_stream;
159 win32_stream = G_WIN32_INPUT_STREAM (object);
161 switch (prop_id)
163 case PROP_HANDLE:
164 g_value_set_pointer (value, win32_stream->priv->handle);
165 break;
166 case PROP_CLOSE_HANDLE:
167 g_value_set_boolean (value, win32_stream->priv->close_handle);
168 break;
169 default:
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 static void
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
198 GInputStream *
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,
207 "handle", handle,
208 "close-handle", close_handle,
209 NULL);
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.
222 * Since: 2.26
224 void
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
247 * Since: 2.26
249 gboolean
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
265 * Since: 2.26
267 void *
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;
275 static gssize
276 g_win32_input_stream_read (GInputStream *stream,
277 void *buffer,
278 gsize count,
279 GCancellable *cancellable,
280 GError **error)
282 GWin32InputStream *win32_stream;
283 BOOL res;
284 DWORD nbytes, nread;
285 OVERLAPPED overlap = { 0, };
286 gssize retval = -1;
288 win32_stream = G_WIN32_INPUT_STREAM (stream);
290 if (g_cancellable_set_error_if_cancelled (cancellable, error))
291 return -1;
293 if (count > G_MAXINT)
294 nbytes = G_MAXINT;
295 else
296 nbytes = count;
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);
302 if (res)
303 retval = nread;
304 else
306 int errsv = GetLastError ();
308 if (errsv == ERROR_IO_PENDING &&
309 _g_win32_overlap_wait_result (win32_stream->priv->handle,
310 &overlap, &nread, cancellable))
312 retval = nread;
313 goto end;
316 if (g_cancellable_set_error_if_cancelled (cancellable, error))
317 goto end;
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 */
326 retval = nread;
327 goto end;
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.. */
337 retval = 0;
339 else
341 gchar *emsg;
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"),
347 emsg);
348 g_free (emsg);
352 end:
353 CloseHandle (overlap.hEvent);
354 return retval;
357 static gboolean
358 g_win32_input_stream_close (GInputStream *stream,
359 GCancellable *cancellable,
360 GError **error)
362 GWin32InputStream *win32_stream;
363 BOOL res;
365 win32_stream = G_WIN32_INPUT_STREAM (stream);
367 if (!win32_stream->priv->close_handle)
368 return TRUE;
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),
376 g_strerror (errno));
377 return FALSE;
380 else
382 res = CloseHandle (win32_stream->priv->handle);
383 if (!res)
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"),
391 emsg);
392 g_free (emsg);
393 return FALSE;
397 return TRUE;
400 GInputStream *
401 g_win32_input_stream_new_from_fd (gint fd,
402 gboolean close_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;