Added. Added. Added. Added.
[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 = 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_is_closed (output_stream))
142     {
143       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
144                    _("Stream is already closed"));
145       return NULL;
146     }
147   
148   if (g_output_stream_has_pending (output_stream))
149     {
150       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
151                    _("Stream has outstanding operation"));
152       return NULL;
153     }
154       
155   info = NULL;
156   
157   g_output_stream_set_pending (output_stream, TRUE);
158   
159   if (cancellable)
160     g_push_current_cancellable (cancellable);
161   
162   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
163   if (class->query_info)
164     info = class->query_info (stream, attributes, cancellable, error);
165   else
166     g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
167                  _("Stream doesn't support query_info"));
168   
169   if (cancellable)
170     g_pop_current_cancellable (cancellable);
171   
172   g_output_stream_set_pending (output_stream, FALSE);
173   
174   return info;
175 }
176
177 static void
178 async_ready_callback_wrapper (GObject *source_object,
179                               GAsyncResult *res,
180                               gpointer      user_data)
181 {
182   GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
183
184   g_output_stream_set_pending (G_OUTPUT_STREAM (stream), FALSE);
185   if (stream->priv->outstanding_callback)
186     (*stream->priv->outstanding_callback) (source_object, res, user_data);
187   g_object_unref (stream);
188 }
189
190 /**
191  * g_file_output_stream_query_info_async:
192  * @stream: a #GFileOutputStream.
193  * @attributes: a file attribute query string.
194  * @io_priority: the io priority 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                                           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
218   g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
219
220   output_stream = G_OUTPUT_STREAM (stream);
221  
222   if (g_output_stream_is_closed (output_stream))
223     {
224       g_simple_async_report_error_in_idle (G_OBJECT (stream),
225                                            callback,
226                                            user_data,
227                                            G_IO_ERROR, G_IO_ERROR_CLOSED,
228                                            _("Stream is already closed"));
229       return;
230     }
231   
232   if (g_output_stream_has_pending (output_stream))
233     {
234       g_simple_async_report_error_in_idle (G_OBJECT (stream),
235                                            callback,
236                                            user_data,
237                                            G_IO_ERROR, G_IO_ERROR_PENDING,
238                                            _("Stream has outstanding operation"));
239       return;
240     }
241
242   klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
243
244   g_output_stream_set_pending (output_stream, TRUE);
245   stream->priv->outstanding_callback = callback;
246   g_object_ref (stream);
247   klass->query_info_async (stream, attributes, io_priority, cancellable,
248                               async_ready_callback_wrapper, user_data);
249 }
250
251 /**
252  * g_file_output_stream_query_info_finish:
253  * @stream: a #GFileOutputStream.
254  * @result: a #GAsyncResult.
255  * @error: a #GError, %NULL to ignore.
256  * 
257  * Finalizes the asynchronous query started 
258  * by g_file_output_stream_query_info_async().
259  * 
260  * Returns: A #GFileInfo for the finished query.
261  **/
262 GFileInfo *
263 g_file_output_stream_query_info_finish (GFileOutputStream     *stream,
264                                            GAsyncResult         *result,
265                                            GError              **error)
266 {
267   GSimpleAsyncResult *simple;
268   GFileOutputStreamClass *class;
269
270   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
271   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
272   
273   if (G_IS_SIMPLE_ASYNC_RESULT (result))
274     {
275       simple = G_SIMPLE_ASYNC_RESULT (result);
276       if (g_simple_async_result_propagate_error (simple, error))
277         return NULL;
278     }
279
280   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
281   return class->query_info_finish (stream, result, error);
282 }
283
284 /**
285  * g_file_output_stream_get_etag:
286  * @stream: a #GFileOutputStream.
287  * 
288  * Gets the entity tag for the file output stream.
289  * 
290  * Returns: the entity tag for the stream.
291  **/
292 char *
293 g_file_output_stream_get_etag (GFileOutputStream  *stream)
294 {
295   GFileOutputStreamClass *class;
296   GOutputStream *output_stream;
297   char *etag;
298   
299   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
300   
301   output_stream = G_OUTPUT_STREAM (stream);
302   
303   if (!g_output_stream_is_closed (output_stream))
304     {
305       g_warning ("stream is not closed yet, can't get etag");
306       return NULL;
307     }
308
309   etag = NULL;
310   
311   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
312   if (class->get_etag)
313     etag = class->get_etag (stream);
314   
315   return etag;
316 }
317
318 /**
319  * g_file_output_stream_tell:
320  * @stream: a #GFileOutputStream.
321  * 
322  * Gets the current location within the stream.
323  * 
324  * Returns: a #goffset of the location within the stream.
325  **/
326 goffset
327 g_file_output_stream_tell (GFileOutputStream  *stream)
328 {
329   GFileOutputStreamClass *class;
330   goffset offset;
331   
332   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);  
333
334   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
335
336   offset = 0;
337   if (class->tell)
338     offset = class->tell (stream);
339
340   return offset;
341 }
342
343 static goffset
344 g_file_output_stream_seekable_tell (GSeekable *seekable)
345 {
346   return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
347 }
348
349 /**
350  * g_file_output_stream_can_seek:
351  * @stream: a #GFileOutputStream.
352  * 
353  * Checks if the stream can be seeked.
354  * 
355  * Returns: %TRUE if seeking is supported by the stream.
356  **/
357 gboolean
358 g_file_output_stream_can_seek (GFileOutputStream  *stream)
359 {
360   GFileOutputStreamClass *class;
361   gboolean can_seek;
362
363   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
364
365   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
366
367   can_seek = FALSE;
368   if (class->seek)
369     {
370       can_seek = TRUE;
371       if (class->can_seek)
372         can_seek = class->can_seek (stream);
373     }
374   
375   return can_seek;
376 }
377
378 static gboolean
379 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
380 {
381   return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
382 }
383
384 /**
385  * g_file_output_stream_seek:
386  * @stream: a #GFileOutputStream.
387  * @offset: a #goffset to seek.
388  * @type: a #GSeekType.
389  * @cancellable: optional #GCancellable object, %NULL to ignore. 
390  * @error: a #GError, %NULL to ignore.
391  * 
392  * Seeks to a location in a file output stream.
393  * 
394  * Returns: %TRUE if the seek was successful. %FALSE otherwise.
395  **/
396 gboolean
397 g_file_output_stream_seek (GFileOutputStream  *stream,
398                            goffset             offset,
399                            GSeekType           type,
400                            GCancellable       *cancellable,
401                            GError            **error)
402 {
403   GFileOutputStreamClass *class;
404   GOutputStream *output_stream;
405   gboolean res;
406
407   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
408
409   output_stream = G_OUTPUT_STREAM (stream);
410   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
411
412   if (g_output_stream_is_closed (output_stream))
413     {
414       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
415                    _("Stream is already closed"));
416       return FALSE;
417     }
418   
419   if (g_output_stream_has_pending (output_stream))
420     {
421       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
422                    _("Stream has outstanding operation"));
423       return FALSE;
424     }
425   
426   if (!class->seek)
427     {
428       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
429                    _("Seek not supported on stream"));
430       return FALSE;
431     }
432
433   g_output_stream_set_pending (output_stream, TRUE);
434   
435   if (cancellable)
436     g_push_current_cancellable (cancellable);
437   
438   res = class->seek (stream, offset, type, cancellable, error);
439   
440   if (cancellable)
441     g_pop_current_cancellable (cancellable);
442
443   g_output_stream_set_pending (output_stream, FALSE);
444   
445   return res;
446 }
447
448 static gboolean
449 g_file_output_stream_seekable_seek (GSeekable  *seekable,
450                                     goffset     offset,
451                                     GSeekType   type,
452                                     GCancellable  *cancellable,
453                                     GError    **error)
454 {
455   return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
456                                     offset, type, cancellable, error);
457 }
458
459 /**
460  * g_file_output_stream_can_truncate:
461  * @stream: a #GFileOutputStream.
462  * 
463  * Checks if the stream can be truncated.
464  * 
465  * Returns: %TRUE if stream can be truncated.
466  **/
467 gboolean
468 g_file_output_stream_can_truncate (GFileOutputStream  *stream)
469 {
470   GFileOutputStreamClass *class;
471   gboolean can_truncate;
472
473   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
474
475   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
476
477   can_truncate = FALSE;
478   if (class->truncate)
479     {
480       can_truncate = TRUE;
481       if (class->can_truncate)
482         can_truncate = class->can_truncate (stream);
483     }
484   
485   return can_truncate;
486 }
487
488 static gboolean
489 g_file_output_stream_seekable_can_truncate (GSeekable  *seekable)
490 {
491   return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
492 }
493
494 /**
495  * g_file_output_stream_truncate:
496  * @stream: a #GFileOutputStream.
497  * @size: a #goffset to truncate the stream at.
498  * @cancellable: optional #GCancellable object, %NULL to ignore. 
499  * @error: a #GError, %NULL to ignore.
500  * 
501  * Truncates a file output stream.
502  * 
503  * Returns: %TRUE if @stream is truncated successfully.
504  **/
505 gboolean
506 g_file_output_stream_truncate (GFileOutputStream  *stream,
507                                goffset             size,
508                                GCancellable       *cancellable,
509                                GError            **error)
510 {
511   GFileOutputStreamClass *class;
512   GOutputStream *output_stream;
513   gboolean res;
514
515   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
516
517   output_stream = G_OUTPUT_STREAM (stream);
518   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
519
520   if (g_output_stream_is_closed (output_stream))
521     {
522       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
523                    _("Stream is already closed"));
524       return FALSE;
525     }
526   
527   if (g_output_stream_has_pending (output_stream))
528     {
529       g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
530                    _("Stream has outstanding operation"));
531       return FALSE;
532     }
533   
534   if (!class->truncate)
535     {
536       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
537                    _("Truncate not supported on stream"));
538       return FALSE;
539     }
540
541   g_output_stream_set_pending (output_stream, TRUE);
542   
543   if (cancellable)
544     g_push_current_cancellable (cancellable);
545   
546   res = class->truncate (stream, size, cancellable, error);
547   
548   if (cancellable)
549     g_pop_current_cancellable (cancellable);
550
551   g_output_stream_set_pending (output_stream, FALSE);
552   
553   return res;
554 }
555
556 static gboolean
557 g_file_output_stream_seekable_truncate (GSeekable     *seekable,
558                                         goffset        size,
559                                         GCancellable  *cancellable,
560                                         GError       **error)
561 {
562   return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
563                                         size, cancellable, error);
564 }
565 /********************************************
566  *   Default implementation of async ops    *
567  ********************************************/
568
569 typedef struct {
570   char *attributes;
571   GFileInfo *info;
572 } QueryInfoAsyncData;
573
574 static void
575 query_info_data_free (QueryInfoAsyncData *data)
576 {
577   if (data->info)
578     g_object_unref (data->info);
579   g_free (data->attributes);
580   g_free (data);
581 }
582
583 static void
584 query_info_async_thread (GSimpleAsyncResult *res,
585                        GObject *object,
586                        GCancellable *cancellable)
587 {
588   GFileOutputStreamClass *class;
589   GError *error = NULL;
590   QueryInfoAsyncData *data;
591   GFileInfo *info;
592   
593   data = g_simple_async_result_get_op_res_gpointer (res);
594
595   info = NULL;
596   
597   class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
598   if (class->query_info)
599     info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
600   else
601     g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
602                  _("Stream doesn't support query_info"));
603
604   if (info == NULL)
605     {
606       g_simple_async_result_set_from_error (res, error);
607       g_error_free (error);
608     }
609   else
610     data->info = info;
611 }
612
613 static void
614 g_file_output_stream_real_query_info_async (GFileOutputStream     *stream,
615                                                char                 *attributes,
616                                                int                   io_priority,
617                                                GCancellable         *cancellable,
618                                                GAsyncReadyCallback   callback,
619                                                gpointer              user_data)
620 {
621   GSimpleAsyncResult *res;
622   QueryInfoAsyncData *data;
623
624   data = g_new0 (QueryInfoAsyncData, 1);
625   data->attributes = g_strdup (attributes);
626   
627   res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
628   g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
629   
630   g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
631   g_object_unref (res);
632 }
633
634 static GFileInfo *
635 g_file_output_stream_real_query_info_finish (GFileOutputStream     *stream,
636                                                 GAsyncResult         *res,
637                                                 GError              **error)
638 {
639   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
640   QueryInfoAsyncData *data;
641
642   g_assert (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
643
644   data = g_simple_async_result_get_op_res_gpointer (simple);
645   if (data->info)
646     return g_object_ref (data->info);
647   
648   return NULL;
649 }
650
651 #define __G_FILE_OUTPUT_STREAM_C__
652 #include "gioaliasdef.c"