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