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