gio: Add g_async_result_legacy_propagate_error()
[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 "gsimpleasyncresult.h"
29 #include "gasyncresult.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 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
81                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
82                                                 g_file_output_stream_seekable_iface_init));
83
84 struct _GFileOutputStreamPrivate {
85   GAsyncReadyCallback outstanding_callback;
86 };
87
88 static void
89 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
90 {
91   g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
92
93   klass->query_info_async = g_file_output_stream_real_query_info_async;
94   klass->query_info_finish = g_file_output_stream_real_query_info_finish;
95 }
96
97 static void
98 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
99 {
100   iface->tell = g_file_output_stream_seekable_tell;
101   iface->can_seek = g_file_output_stream_seekable_can_seek;
102   iface->seek = g_file_output_stream_seekable_seek;
103   iface->can_truncate = g_file_output_stream_seekable_can_truncate;
104   iface->truncate_fn = g_file_output_stream_seekable_truncate;
105 }
106
107 static void
108 g_file_output_stream_init (GFileOutputStream *stream)
109 {
110   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
111                                               G_TYPE_FILE_OUTPUT_STREAM,
112                                               GFileOutputStreamPrivate);
113 }
114
115 /**
116  * g_file_output_stream_query_info:
117  * @stream: a #GFileOutputStream.
118  * @attributes: a file attribute query string.
119  * @cancellable: optional #GCancellable object, %NULL to ignore. 
120  * @error: a #GError, %NULL to ignore.
121  *
122  * Queries a file output stream for the given @attributes. 
123  * This function blocks while querying the stream. For the asynchronous 
124  * version of this function, see g_file_output_stream_query_info_async(). 
125  * While the stream is blocked, the stream will set the pending flag 
126  * internally, and any other operations on the stream will fail with 
127  * %G_IO_ERROR_PENDING.
128  * 
129  * Can fail if the stream was already closed (with @error being set to 
130  * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
131  * set to %G_IO_ERROR_PENDING), or if querying info is not supported for 
132  * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
133  * all cases of failure, %NULL will be returned.
134  * 
135  * If @cancellable is not %NULL, then the operation can be cancelled by
136  * triggering the cancellable object from another thread. If the operation
137  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will 
138  * be returned. 
139  * 
140  * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
141  **/
142 GFileInfo *
143 g_file_output_stream_query_info (GFileOutputStream      *stream,
144                                     const char             *attributes,
145                                     GCancellable           *cancellable,
146                                     GError                **error)
147 {
148   GFileOutputStreamClass *class;
149   GOutputStream *output_stream;
150   GFileInfo *info;
151   
152   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
153   
154   output_stream = G_OUTPUT_STREAM (stream);
155   
156   if (!g_output_stream_set_pending (output_stream, error))
157     return NULL;
158       
159   info = NULL;
160   
161   if (cancellable)
162     g_cancellable_push_current (cancellable);
163   
164   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
165   if (class->query_info)
166     info = class->query_info (stream, attributes, cancellable, error);
167   else
168     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
169                          _("Stream doesn't support query_info"));
170   
171   if (cancellable)
172     g_cancellable_pop_current (cancellable);
173   
174   g_output_stream_clear_pending (output_stream);
175   
176   return info;
177 }
178
179 static void
180 async_ready_callback_wrapper (GObject *source_object,
181                               GAsyncResult *res,
182                               gpointer      user_data)
183 {
184   GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
185
186   g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
187   if (stream->priv->outstanding_callback)
188     (*stream->priv->outstanding_callback) (source_object, res, user_data);
189   g_object_unref (stream);
190 }
191
192 /**
193  * g_file_output_stream_query_info_async:
194  * @stream: a #GFileOutputStream.
195  * @attributes: a file attribute query string.
196  * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link> 
197  *     of the request.
198  * @cancellable: optional #GCancellable object, %NULL to ignore. 
199  * @callback: callback to call when the request is satisfied
200  * @user_data: the data to pass to callback function
201  * 
202  * Asynchronously queries the @stream for a #GFileInfo. When completed,
203  * @callback will be called with a #GAsyncResult which can be used to 
204  * finish the operation with g_file_output_stream_query_info_finish().
205  * 
206  * For the synchronous version of this function, see 
207  * g_file_output_stream_query_info().
208  *
209  **/
210 void
211 g_file_output_stream_query_info_async (GFileOutputStream     *stream,
212                                           const char           *attributes,
213                                           int                   io_priority,
214                                           GCancellable         *cancellable,
215                                           GAsyncReadyCallback   callback,
216                                           gpointer              user_data)
217 {
218   GFileOutputStreamClass *klass;
219   GOutputStream *output_stream;
220   GError *error = NULL;
221
222   g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
223
224   output_stream = G_OUTPUT_STREAM (stream);
225  
226   if (!g_output_stream_set_pending (output_stream, &error))
227     {
228       g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
229                                             callback,
230                                             user_data,
231                                             error);
232       return;
233     }
234
235   klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
236
237   stream->priv->outstanding_callback = callback;
238   g_object_ref (stream);
239   klass->query_info_async (stream, attributes, io_priority, cancellable,
240                               async_ready_callback_wrapper, user_data);
241 }
242
243 /**
244  * g_file_output_stream_query_info_finish:
245  * @stream: a #GFileOutputStream.
246  * @result: a #GAsyncResult.
247  * @error: a #GError, %NULL to ignore.
248  * 
249  * Finalizes the asynchronous query started 
250  * by g_file_output_stream_query_info_async().
251  * 
252  * Returns: (transfer full): A #GFileInfo for the finished query.
253  **/
254 GFileInfo *
255 g_file_output_stream_query_info_finish (GFileOutputStream     *stream,
256                                            GAsyncResult         *result,
257                                            GError              **error)
258 {
259   GFileOutputStreamClass *class;
260
261   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
262   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
263   
264   if (g_async_result_legacy_propagate_error (result, error))
265     return NULL;
266
267   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
268   return class->query_info_finish (stream, result, error);
269 }
270
271 /**
272  * g_file_output_stream_get_etag:
273  * @stream: a #GFileOutputStream.
274  * 
275  * Gets the entity tag for the file when it has been written.
276  * This must be called after the stream has been written
277  * and closed, as the etag can change while writing.
278  * 
279  * Returns: the entity tag for the stream.
280  **/
281 char *
282 g_file_output_stream_get_etag (GFileOutputStream  *stream)
283 {
284   GFileOutputStreamClass *class;
285   GOutputStream *output_stream;
286   char *etag;
287   
288   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
289   
290   output_stream = G_OUTPUT_STREAM (stream);
291   
292   if (!g_output_stream_is_closed (output_stream))
293     {
294       g_warning ("stream is not closed yet, can't get etag");
295       return NULL;
296     }
297
298   etag = NULL;
299   
300   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
301   if (class->get_etag)
302     etag = class->get_etag (stream);
303   
304   return etag;
305 }
306
307 static goffset
308 g_file_output_stream_tell (GFileOutputStream  *stream)
309 {
310   GFileOutputStreamClass *class;
311   goffset offset;
312   
313   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);  
314
315   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
316
317   offset = 0;
318   if (class->tell)
319     offset = class->tell (stream);
320
321   return offset;
322 }
323
324 static goffset
325 g_file_output_stream_seekable_tell (GSeekable *seekable)
326 {
327   return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
328 }
329
330 static gboolean
331 g_file_output_stream_can_seek (GFileOutputStream  *stream)
332 {
333   GFileOutputStreamClass *class;
334   gboolean can_seek;
335
336   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
337
338   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
339
340   can_seek = FALSE;
341   if (class->seek)
342     {
343       can_seek = TRUE;
344       if (class->can_seek)
345         can_seek = class->can_seek (stream);
346     }
347   
348   return can_seek;
349 }
350
351 static gboolean
352 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
353 {
354   return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
355 }
356
357 static gboolean
358 g_file_output_stream_seek (GFileOutputStream  *stream,
359                            goffset             offset,
360                            GSeekType           type,
361                            GCancellable       *cancellable,
362                            GError            **error)
363 {
364   GFileOutputStreamClass *class;
365   GOutputStream *output_stream;
366   gboolean res;
367
368   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
369
370   output_stream = G_OUTPUT_STREAM (stream);
371   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
372
373   if (!class->seek)
374     {
375       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
376                            _("Seek not supported on stream"));
377       return FALSE;
378     }
379
380   if (!g_output_stream_set_pending (output_stream, error))
381     return FALSE;
382   
383   if (cancellable)
384     g_cancellable_push_current (cancellable);
385   
386   res = class->seek (stream, offset, type, cancellable, error);
387   
388   if (cancellable)
389     g_cancellable_pop_current (cancellable);
390
391   g_output_stream_clear_pending (output_stream);
392   
393   return res;
394 }
395
396 static gboolean
397 g_file_output_stream_seekable_seek (GSeekable  *seekable,
398                                     goffset     offset,
399                                     GSeekType   type,
400                                     GCancellable  *cancellable,
401                                     GError    **error)
402 {
403   return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
404                                     offset, type, cancellable, error);
405 }
406
407 static gboolean
408 g_file_output_stream_can_truncate (GFileOutputStream  *stream)
409 {
410   GFileOutputStreamClass *class;
411   gboolean can_truncate;
412
413   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
414
415   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
416
417   can_truncate = FALSE;
418   if (class->truncate_fn)
419     {
420       can_truncate = TRUE;
421       if (class->can_truncate)
422         can_truncate = class->can_truncate (stream);
423     }
424   
425   return can_truncate;
426 }
427
428 static gboolean
429 g_file_output_stream_seekable_can_truncate (GSeekable  *seekable)
430 {
431   return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
432 }
433
434 static gboolean
435 g_file_output_stream_truncate (GFileOutputStream  *stream,
436                                goffset             size,
437                                GCancellable       *cancellable,
438                                GError            **error)
439 {
440   GFileOutputStreamClass *class;
441   GOutputStream *output_stream;
442   gboolean res;
443
444   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
445
446   output_stream = G_OUTPUT_STREAM (stream);
447   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
448
449   if (!class->truncate_fn)
450     {
451       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
452                            _("Truncate not supported on stream"));
453       return FALSE;
454     }
455
456   if (!g_output_stream_set_pending (output_stream, error))
457     return FALSE;
458   
459   if (cancellable)
460     g_cancellable_push_current (cancellable);
461   
462   res = class->truncate_fn (stream, size, cancellable, error);
463   
464   if (cancellable)
465     g_cancellable_pop_current (cancellable);
466
467   g_output_stream_clear_pending (output_stream);
468   
469   return res;
470 }
471
472 static gboolean
473 g_file_output_stream_seekable_truncate (GSeekable     *seekable,
474                                         goffset        size,
475                                         GCancellable  *cancellable,
476                                         GError       **error)
477 {
478   return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
479                                         size, cancellable, error);
480 }
481 /********************************************
482  *   Default implementation of async ops    *
483  ********************************************/
484
485 typedef struct {
486   char *attributes;
487   GFileInfo *info;
488 } QueryInfoAsyncData;
489
490 static void
491 query_info_data_free (QueryInfoAsyncData *data)
492 {
493   if (data->info)
494     g_object_unref (data->info);
495   g_free (data->attributes);
496   g_free (data);
497 }
498
499 static void
500 query_info_async_thread (GSimpleAsyncResult *res,
501                        GObject *object,
502                        GCancellable *cancellable)
503 {
504   GFileOutputStreamClass *class;
505   GError *error = NULL;
506   QueryInfoAsyncData *data;
507   GFileInfo *info;
508   
509   data = g_simple_async_result_get_op_res_gpointer (res);
510
511   info = NULL;
512   
513   class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
514   if (class->query_info)
515     info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
516   else
517     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
518                          _("Stream doesn't support query_info"));
519
520   if (info == NULL)
521     g_simple_async_result_take_error (res, error);
522   else
523     data->info = info;
524 }
525
526 static void
527 g_file_output_stream_real_query_info_async (GFileOutputStream     *stream,
528                                                const char           *attributes,
529                                                int                   io_priority,
530                                                GCancellable         *cancellable,
531                                                GAsyncReadyCallback   callback,
532                                                gpointer              user_data)
533 {
534   GSimpleAsyncResult *res;
535   QueryInfoAsyncData *data;
536
537   data = g_new0 (QueryInfoAsyncData, 1);
538   data->attributes = g_strdup (attributes);
539   
540   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
541   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
542   
543   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
544   g_object_unref (res);
545 }
546
547 static GFileInfo *
548 g_file_output_stream_real_query_info_finish (GFileOutputStream     *stream,
549                                              GAsyncResult         *res,
550                                              GError              **error)
551 {
552   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
553   QueryInfoAsyncData *data;
554
555   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
556
557   if (g_simple_async_result_propagate_error (simple, error))
558     return NULL;
559
560   data = g_simple_async_result_get_op_res_gpointer (simple);
561   if (data->info)
562     return g_object_ref (data->info);
563   
564   return NULL;
565 }