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