gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[platform/upstream/glib.git] / gio / gwin32outputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  * Author: Tor Lillqvist <tml@iki.fi>
22  */
23
24 #include "config.h"
25
26 #include <windows.h>
27
28 #include <io.h>
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32 #include "gioerror.h"
33 #include "gwin32outputstream.h"
34 #include "gcancellable.h"
35 #include "gsimpleasyncresult.h"
36 #include "gasynchelper.h"
37 #include "glibintl.h"
38
39
40 /**
41  * SECTION:gwin32outputstream
42  * @short_description: Streaming output operations for Windows file handles
43  * @include: gio/gwin32outputstream.h
44  * @see_also: #GOutputStream
45  *
46  * #GWin32OutputStream implements #GOutputStream for writing to a
47  * Windows file handle.
48  *
49  * Note that <filename>&lt;gio/gwin32outputstream.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.
52  */
53
54 enum {
55   PROP_0,
56   PROP_HANDLE,
57   PROP_CLOSE_HANDLE
58 };
59
60 G_DEFINE_TYPE (GWin32OutputStream, g_win32_output_stream, G_TYPE_OUTPUT_STREAM);
61
62
63 struct _GWin32OutputStreamPrivate {
64   HANDLE handle;
65   gboolean close_handle;
66 };
67
68 static void     g_win32_output_stream_set_property (GObject              *object,
69                                                     guint                 prop_id,
70                                                     const GValue         *value,
71                                                     GParamSpec           *pspec);
72 static void     g_win32_output_stream_get_property (GObject              *object,
73                                                     guint                 prop_id,
74                                                     GValue               *value,
75                                                     GParamSpec           *pspec);
76 static gssize   g_win32_output_stream_write        (GOutputStream        *stream,
77                                                     const void           *buffer,
78                                                     gsize                 count,
79                                                     GCancellable         *cancellable,
80                                                     GError              **error);
81 static gboolean g_win32_output_stream_close        (GOutputStream        *stream,
82                                                     GCancellable         *cancellable,
83                                                     GError              **error);
84
85
86 static void
87 g_win32_output_stream_finalize (GObject *object)
88 {
89   G_OBJECT_CLASS (g_win32_output_stream_parent_class)->finalize (object);
90 }
91
92 static void
93 g_win32_output_stream_class_init (GWin32OutputStreamClass *klass)
94 {
95   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
97
98   g_type_class_add_private (klass, sizeof (GWin32OutputStreamPrivate));
99
100   gobject_class->get_property = g_win32_output_stream_get_property;
101   gobject_class->set_property = g_win32_output_stream_set_property;
102   gobject_class->finalize = g_win32_output_stream_finalize;
103
104   stream_class->write_fn = g_win32_output_stream_write;
105   stream_class->close_fn = g_win32_output_stream_close;
106
107    /**
108    * GWin32OutputStream:handle:
109    *
110    * The file handle that the stream writes to.
111    *
112    * Since: 2.26
113    */
114   g_object_class_install_property (gobject_class,
115                                    PROP_HANDLE,
116                                    g_param_spec_pointer ("handle",
117                                                          P_("File handle"),
118                                                          P_("The file handle to write to"),
119                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
120
121   /**
122    * GWin32OutputStream:close-handle:
123    *
124    * Whether to close the file handle when the stream is closed.
125    *
126    * Since: 2.26
127    */
128   g_object_class_install_property (gobject_class,
129                                    PROP_CLOSE_HANDLE,
130                                    g_param_spec_boolean ("close-handle",
131                                                          P_("Close file handle"),
132                                                          P_("Whether to close the file handle when the stream is closed"),
133                                                          TRUE,
134                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
135 }
136
137 static void
138 g_win32_output_stream_set_property (GObject         *object,
139                                     guint            prop_id,
140                                     const GValue    *value,
141                                     GParamSpec      *pspec)
142 {
143   GWin32OutputStream *win32_stream;
144
145   win32_stream = G_WIN32_OUTPUT_STREAM (object);
146
147   switch (prop_id)
148     {
149     case PROP_HANDLE:
150       win32_stream->priv->handle = g_value_get_pointer (value);
151       break;
152     case PROP_CLOSE_HANDLE:
153       win32_stream->priv->close_handle = g_value_get_boolean (value);
154       break;
155     default:
156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157       break;
158     }
159 }
160
161 static void
162 g_win32_output_stream_get_property (GObject    *object,
163                                     guint       prop_id,
164                                     GValue     *value,
165                                     GParamSpec *pspec)
166 {
167   GWin32OutputStream *win32_stream;
168
169   win32_stream = G_WIN32_OUTPUT_STREAM (object);
170
171   switch (prop_id)
172     {
173     case PROP_HANDLE:
174       g_value_set_pointer (value, win32_stream->priv->handle);
175       break;
176     case PROP_CLOSE_HANDLE:
177       g_value_set_boolean (value, win32_stream->priv->close_handle);
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181     }
182 }
183
184 static void
185 g_win32_output_stream_init (GWin32OutputStream *win32_stream)
186 {
187   win32_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (win32_stream,
188                                                     G_TYPE_WIN32_OUTPUT_STREAM,
189                                                     GWin32OutputStreamPrivate);
190
191   win32_stream->priv->handle = NULL;
192   win32_stream->priv->close_handle = TRUE;
193 }
194
195 /**
196  * g_win32_output_stream_new:
197  * @handle: a Win32 file handle
198  * @close_handle: %TRUE to close the handle when done
199  *
200  * Creates a new #GWin32OutputStream for the given @handle.
201  *
202  * If @close_handle, is %TRUE, the handle will be closed when the
203  * output stream is destroyed.
204  *
205  * Returns: a new #GOutputStream
206  *
207  * Since: 2.26
208 **/
209 GOutputStream *
210 g_win32_output_stream_new (void    *handle,
211                            gboolean close_handle)
212 {
213   GWin32OutputStream *stream;
214
215   g_return_val_if_fail (handle != NULL, NULL);
216
217   stream = g_object_new (G_TYPE_WIN32_OUTPUT_STREAM,
218                          "handle", handle,
219                          "close-handle", close_handle,
220                          NULL);
221
222   return G_OUTPUT_STREAM (stream);
223 }
224
225 /**
226  * g_win32_output_stream_set_close_handle:
227  * @stream: a #GWin32OutputStream
228  * @close_handle: %TRUE to close the handle when done
229  *
230  * Sets whether the handle of @stream shall be closed when the stream
231  * is closed.
232  *
233  * Since: 2.26
234  */
235 void
236 g_win32_output_stream_set_close_handle (GWin32OutputStream *stream,
237                                         gboolean           close_handle)
238 {
239   g_return_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream));
240
241   close_handle = close_handle != FALSE;
242   if (stream->priv->close_handle != close_handle)
243     {
244       stream->priv->close_handle = close_handle;
245       g_object_notify (G_OBJECT (stream), "close-handle");
246     }
247 }
248
249 /**
250  * g_win32_output_stream_get_close_handle:
251  * @stream: a #GWin32OutputStream
252  *
253  * Returns whether the handle of @stream will be closed when the
254  * stream is closed.
255  *
256  * Return value: %TRUE if the handle is closed when done
257  *
258  * Since: 2.26
259  */
260 gboolean
261 g_win32_output_stream_get_close_handle (GWin32OutputStream *stream)
262 {
263   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), FALSE);
264
265   return stream->priv->close_handle;
266 }
267
268 /**
269  * g_win32_output_stream_get_handle:
270  * @stream: a #GWin32OutputStream
271  *
272  * Return the Windows handle that the stream writes to.
273  *
274  * Return value: The handle descriptor of @stream
275  *
276  * Since: 2.26
277  */
278 void *
279 g_win32_output_stream_get_handle (GWin32OutputStream *stream)
280 {
281   g_return_val_if_fail (G_IS_WIN32_OUTPUT_STREAM (stream), NULL);
282
283   return stream->priv->handle;
284 }
285
286 static gssize
287 g_win32_output_stream_write (GOutputStream  *stream,
288                             const void     *buffer,
289                             gsize           count,
290                             GCancellable   *cancellable,
291                             GError        **error)
292 {
293   GWin32OutputStream *win32_stream;
294   BOOL res;
295   DWORD nbytes, nwritten;
296
297   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
298
299   if (g_cancellable_set_error_if_cancelled (cancellable, error))
300     return -1;
301
302   if (count > G_MAXINT)
303     nbytes = G_MAXINT;
304   else
305     nbytes = count;
306
307   res = WriteFile (win32_stream->priv->handle, buffer, nbytes, &nwritten, NULL);
308   if (!res)
309     {
310       int errsv = GetLastError ();
311       gchar *emsg = g_win32_error_message (errsv);
312
313       if (errsv == ERROR_HANDLE_EOF)
314         return 0;
315
316       g_set_error (error, G_IO_ERROR,
317                    g_io_error_from_win32_error (errsv),
318                    _("Error writing to handle: %s"),
319                    emsg);
320       g_free (emsg);
321       return -1;
322     }
323
324   return nwritten;
325 }
326
327 static gboolean
328 g_win32_output_stream_close (GOutputStream  *stream,
329                              GCancellable   *cancellable,
330                              GError        **error)
331 {
332   GWin32OutputStream *win32_stream;
333   BOOL res;
334
335   win32_stream = G_WIN32_OUTPUT_STREAM (stream);
336
337   if (!win32_stream->priv->close_handle)
338     return TRUE;
339
340   res = CloseHandle (win32_stream->priv->handle);
341   if (!res)
342     {
343       int errsv = GetLastError ();
344       gchar *emsg = g_win32_error_message (errsv);
345
346       g_set_error (error, G_IO_ERROR,
347                    g_io_error_from_win32_error (errsv),
348                    _("Error closing handle: %s"),
349                    emsg);
350       g_free (emsg);
351       return FALSE;
352     }
353
354   return TRUE;
355 }