Improve test coverage for GSubprocess
[platform/upstream/glib.git] / gio / gfileoutputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 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  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <gfileoutputstream.h>
27 #include <gseekable.h>
28 #include "gasyncresult.h"
29 #include "gtask.h"
30 #include "gcancellable.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33
34
35 /**
36  * SECTION:gfileoutputstream
37  * @short_description: File output streaming operations
38  * @include: gio/gio.h
39  * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
40  * 
41  * GFileOutputStream provides output streams that write their
42  * content to a file.
43  *
44  * GFileOutputStream implements #GSeekable, which allows the output 
45  * stream to jump to arbitrary positions in the file and to truncate
46  * the file, provided the filesystem of the file supports these 
47  * operations.
48  *
49  * To find the position of a file output stream, use g_seekable_tell().
50  * To find out if a file output stream supports seeking, use
51  * g_seekable_can_seek().To position a file output stream, use
52  * g_seekable_seek(). To find out if a file output stream supports
53  * truncating, use g_seekable_can_truncate(). To truncate a file output
54  * stream, use g_seekable_truncate().
55  **/
56
57 static void       g_file_output_stream_seekable_iface_init    (GSeekableIface       *iface);
58 static goffset    g_file_output_stream_seekable_tell          (GSeekable            *seekable);
59 static gboolean   g_file_output_stream_seekable_can_seek      (GSeekable            *seekable);
60 static gboolean   g_file_output_stream_seekable_seek          (GSeekable            *seekable,
61                                                                goffset               offset,
62                                                                GSeekType             type,
63                                                                GCancellable         *cancellable,
64                                                                GError              **error);
65 static gboolean   g_file_output_stream_seekable_can_truncate  (GSeekable            *seekable);
66 static gboolean   g_file_output_stream_seekable_truncate      (GSeekable            *seekable,
67                                                                goffset               offset,
68                                                                GCancellable         *cancellable,
69                                                                GError              **error);
70 static void       g_file_output_stream_real_query_info_async  (GFileOutputStream    *stream,
71                                                                const char           *attributes,
72                                                                int                   io_priority,
73                                                                GCancellable         *cancellable,
74                                                                GAsyncReadyCallback   callback,
75                                                                gpointer              user_data);
76 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream    *stream,
77                                                                GAsyncResult         *result,
78                                                                GError              **error);
79
80 struct _GFileOutputStreamPrivate {
81   GAsyncReadyCallback outstanding_callback;
82 };
83
84 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
85                          G_ADD_PRIVATE (GFileOutputStream)
86                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
87                                                 g_file_output_stream_seekable_iface_init));
88
89 static void
90 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
91 {
92   klass->query_info_async = g_file_output_stream_real_query_info_async;
93   klass->query_info_finish = g_file_output_stream_real_query_info_finish;
94 }
95
96 static void
97 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
98 {
99   iface->tell = g_file_output_stream_seekable_tell;
100   iface->can_seek = g_file_output_stream_seekable_can_seek;
101   iface->seek = g_file_output_stream_seekable_seek;
102   iface->can_truncate = g_file_output_stream_seekable_can_truncate;
103   iface->truncate_fn = g_file_output_stream_seekable_truncate;
104 }
105
106 static void
107 g_file_output_stream_init (GFileOutputStream *stream)
108 {
109   stream->priv = g_file_output_stream_get_instance_private (stream);
110 }
111
112 /**
113  * g_file_output_stream_query_info:
114  * @stream: a #GFileOutputStream.
115  * @attributes: a file attribute query string.
116  * @cancellable: optional #GCancellable object, %NULL to ignore. 
117  * @error: a #GError, %NULL to ignore.
118  *
119  * Queries a file output stream for the given @attributes. 
120  * This function blocks while querying the stream. For the asynchronous 
121  * version of this function, see g_file_output_stream_query_info_async(). 
122  * While the stream is blocked, the stream will set the pending flag 
123  * internally, and any other operations on the stream will fail with 
124  * %G_IO_ERROR_PENDING.
125  * 
126  * Can fail if the stream was already closed (with @error being set to 
127  * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
128  * set to %G_IO_ERROR_PENDING), or if querying info is not supported for 
129  * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
130  * all cases of failure, %NULL will be returned.
131  * 
132  * If @cancellable is not %NULL, then the operation can be cancelled by
133  * triggering the cancellable object from another thread. If the operation
134  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will 
135  * be returned. 
136  * 
137  * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
138  **/
139 GFileInfo *
140 g_file_output_stream_query_info (GFileOutputStream      *stream,
141                                     const char             *attributes,
142                                     GCancellable           *cancellable,
143                                     GError                **error)
144 {
145   GFileOutputStreamClass *class;
146   GOutputStream *output_stream;
147   GFileInfo *info;
148   
149   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
150   
151   output_stream = G_OUTPUT_STREAM (stream);
152   
153   if (!g_output_stream_set_pending (output_stream, error))
154     return NULL;
155       
156   info = NULL;
157   
158   if (cancellable)
159     g_cancellable_push_current (cancellable);
160   
161   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
162   if (class->query_info)
163     info = class->query_info (stream, attributes, cancellable, error);
164   else
165     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
166                          _("Stream doesn't support query_info"));
167   
168   if (cancellable)
169     g_cancellable_pop_current (cancellable);
170   
171   g_output_stream_clear_pending (output_stream);
172   
173   return info;
174 }
175
176 static void
177 async_ready_callback_wrapper (GObject *source_object,
178                               GAsyncResult *res,
179                               gpointer      user_data)
180 {
181   GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
182
183   g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
184   if (stream->priv->outstanding_callback)
185     (*stream->priv->outstanding_callback) (source_object, res, user_data);
186   g_object_unref (stream);
187 }
188
189 /**
190  * g_file_output_stream_query_info_async:
191  * @stream: a #GFileOutputStream.
192  * @attributes: a file attribute query string.
193  * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link> 
194  *     of the request.
195  * @cancellable: optional #GCancellable object, %NULL to ignore. 
196  * @callback: callback to call when the request is satisfied
197  * @user_data: the data to pass to callback function
198  * 
199  * Asynchronously queries the @stream for a #GFileInfo. When completed,
200  * @callback will be called with a #GAsyncResult which can be used to 
201  * finish the operation with g_file_output_stream_query_info_finish().
202  * 
203  * For the synchronous version of this function, see 
204  * g_file_output_stream_query_info().
205  *
206  **/
207 void
208 g_file_output_stream_query_info_async (GFileOutputStream     *stream,
209                                           const char           *attributes,
210                                           int                   io_priority,
211                                           GCancellable         *cancellable,
212                                           GAsyncReadyCallback   callback,
213                                           gpointer              user_data)
214 {
215   GFileOutputStreamClass *klass;
216   GOutputStream *output_stream;
217   GError *error = NULL;
218
219   g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
220
221   output_stream = G_OUTPUT_STREAM (stream);
222  
223   if (!g_output_stream_set_pending (output_stream, &error))
224     {
225       g_task_report_error (stream, callback, user_data,
226                            g_file_output_stream_query_info_async,
227                            error);
228       return;
229     }
230
231   klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
232
233   stream->priv->outstanding_callback = callback;
234   g_object_ref (stream);
235   klass->query_info_async (stream, attributes, io_priority, cancellable,
236                            async_ready_callback_wrapper, user_data);
237 }
238
239 /**
240  * g_file_output_stream_query_info_finish:
241  * @stream: a #GFileOutputStream.
242  * @result: a #GAsyncResult.
243  * @error: a #GError, %NULL to ignore.
244  * 
245  * Finalizes the asynchronous query started 
246  * by g_file_output_stream_query_info_async().
247  * 
248  * Returns: (transfer full): A #GFileInfo for the finished query.
249  **/
250 GFileInfo *
251 g_file_output_stream_query_info_finish (GFileOutputStream     *stream,
252                                            GAsyncResult         *result,
253                                            GError              **error)
254 {
255   GFileOutputStreamClass *class;
256
257   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
258   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
259   
260   if (g_async_result_legacy_propagate_error (result, error))
261     return NULL;
262   else if (g_async_result_is_tagged (result, g_file_output_stream_query_info_async))
263     return g_task_propagate_pointer (G_TASK (result), error);
264
265   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
266   return class->query_info_finish (stream, result, error);
267 }
268
269 /**
270  * g_file_output_stream_get_etag:
271  * @stream: a #GFileOutputStream.
272  * 
273  * Gets the entity tag for the file when it has been written.
274  * This must be called after the stream has been written
275  * and closed, as the etag can change while writing.
276  * 
277  * Returns: the entity tag for the stream.
278  **/
279 char *
280 g_file_output_stream_get_etag (GFileOutputStream  *stream)
281 {
282   GFileOutputStreamClass *class;
283   GOutputStream *output_stream;
284   char *etag;
285   
286   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
287   
288   output_stream = G_OUTPUT_STREAM (stream);
289   
290   if (!g_output_stream_is_closed (output_stream))
291     {
292       g_warning ("stream is not closed yet, can't get etag");
293       return NULL;
294     }
295
296   etag = NULL;
297   
298   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
299   if (class->get_etag)
300     etag = class->get_etag (stream);
301   
302   return etag;
303 }
304
305 static goffset
306 g_file_output_stream_tell (GFileOutputStream  *stream)
307 {
308   GFileOutputStreamClass *class;
309   goffset offset;
310   
311   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);  
312
313   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
314
315   offset = 0;
316   if (class->tell)
317     offset = class->tell (stream);
318
319   return offset;
320 }
321
322 static goffset
323 g_file_output_stream_seekable_tell (GSeekable *seekable)
324 {
325   return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
326 }
327
328 static gboolean
329 g_file_output_stream_can_seek (GFileOutputStream  *stream)
330 {
331   GFileOutputStreamClass *class;
332   gboolean can_seek;
333
334   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
335
336   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
337
338   can_seek = FALSE;
339   if (class->seek)
340     {
341       can_seek = TRUE;
342       if (class->can_seek)
343         can_seek = class->can_seek (stream);
344     }
345   
346   return can_seek;
347 }
348
349 static gboolean
350 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
351 {
352   return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
353 }
354
355 static gboolean
356 g_file_output_stream_seek (GFileOutputStream  *stream,
357                            goffset             offset,
358                            GSeekType           type,
359                            GCancellable       *cancellable,
360                            GError            **error)
361 {
362   GFileOutputStreamClass *class;
363   GOutputStream *output_stream;
364   gboolean res;
365
366   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
367
368   output_stream = G_OUTPUT_STREAM (stream);
369   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
370
371   if (!class->seek)
372     {
373       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
374                            _("Seek not supported on stream"));
375       return FALSE;
376     }
377
378   if (!g_output_stream_set_pending (output_stream, error))
379     return FALSE;
380   
381   if (cancellable)
382     g_cancellable_push_current (cancellable);
383   
384   res = class->seek (stream, offset, type, cancellable, error);
385   
386   if (cancellable)
387     g_cancellable_pop_current (cancellable);
388
389   g_output_stream_clear_pending (output_stream);
390   
391   return res;
392 }
393
394 static gboolean
395 g_file_output_stream_seekable_seek (GSeekable  *seekable,
396                                     goffset     offset,
397                                     GSeekType   type,
398                                     GCancellable  *cancellable,
399                                     GError    **error)
400 {
401   return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
402                                     offset, type, cancellable, error);
403 }
404
405 static gboolean
406 g_file_output_stream_can_truncate (GFileOutputStream  *stream)
407 {
408   GFileOutputStreamClass *class;
409   gboolean can_truncate;
410
411   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
412
413   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
414
415   can_truncate = FALSE;
416   if (class->truncate_fn)
417     {
418       can_truncate = TRUE;
419       if (class->can_truncate)
420         can_truncate = class->can_truncate (stream);
421     }
422   
423   return can_truncate;
424 }
425
426 static gboolean
427 g_file_output_stream_seekable_can_truncate (GSeekable  *seekable)
428 {
429   return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
430 }
431
432 static gboolean
433 g_file_output_stream_truncate (GFileOutputStream  *stream,
434                                goffset             size,
435                                GCancellable       *cancellable,
436                                GError            **error)
437 {
438   GFileOutputStreamClass *class;
439   GOutputStream *output_stream;
440   gboolean res;
441
442   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
443
444   output_stream = G_OUTPUT_STREAM (stream);
445   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
446
447   if (!class->truncate_fn)
448     {
449       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
450                            _("Truncate not supported on stream"));
451       return FALSE;
452     }
453
454   if (!g_output_stream_set_pending (output_stream, error))
455     return FALSE;
456   
457   if (cancellable)
458     g_cancellable_push_current (cancellable);
459   
460   res = class->truncate_fn (stream, size, cancellable, error);
461   
462   if (cancellable)
463     g_cancellable_pop_current (cancellable);
464
465   g_output_stream_clear_pending (output_stream);
466   
467   return res;
468 }
469
470 static gboolean
471 g_file_output_stream_seekable_truncate (GSeekable     *seekable,
472                                         goffset        size,
473                                         GCancellable  *cancellable,
474                                         GError       **error)
475 {
476   return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
477                                         size, cancellable, error);
478 }
479 /********************************************
480  *   Default implementation of async ops    *
481  ********************************************/
482
483 static void
484 query_info_async_thread (GTask        *task,
485                          gpointer      source_object,
486                          gpointer      task_data,
487                          GCancellable *cancellable)
488 {
489   GFileOutputStream *stream = source_object;
490   const char *attributes = task_data;
491   GFileOutputStreamClass *class;
492   GError *error = NULL;
493   GFileInfo *info = NULL;
494
495   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
496   if (class->query_info)
497     info = class->query_info (stream, attributes, cancellable, &error);
498   else
499     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
500                          _("Stream doesn't support query_info"));
501
502   if (info == NULL)
503     g_task_return_error (task, error);
504   else
505     g_task_return_pointer (task, info, g_object_unref);
506 }
507
508 static void
509 g_file_output_stream_real_query_info_async (GFileOutputStream     *stream,
510                                                const char           *attributes,
511                                                int                   io_priority,
512                                                GCancellable         *cancellable,
513                                                GAsyncReadyCallback   callback,
514                                                gpointer              user_data)
515 {
516   GTask *task;
517
518   task = g_task_new (stream, cancellable, callback, user_data);
519   g_task_set_task_data (task, g_strdup (attributes), g_free);
520   g_task_set_priority (task, io_priority);
521   
522   g_task_run_in_thread (task, query_info_async_thread);
523   g_object_unref (task);
524 }
525
526 static GFileInfo *
527 g_file_output_stream_real_query_info_finish (GFileOutputStream     *stream,
528                                              GAsyncResult         *res,
529                                              GError              **error)
530 {
531   g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
532
533   return g_task_propagate_pointer (G_TASK (res), error);
534 }