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