Updated FSF's address
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include <glib.h>
24 #include <gfileoutputstream.h>
25 #include <gseekable.h>
26 #include "gasyncresult.h"
27 #include "gtask.h"
28 #include "gcancellable.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gfileoutputstream
35  * @short_description: File output streaming operations
36  * @include: gio/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.
46  *
47  * To find the position of a file output stream, use g_seekable_tell().
48  * To find out if a file output stream supports seeking, use
49  * g_seekable_can_seek().To position a file output stream, use
50  * g_seekable_seek(). To find out if a file output stream supports
51  * truncating, use g_seekable_can_truncate(). To truncate a file output
52  * stream, use g_seekable_truncate().
53  **/
54
55 static void       g_file_output_stream_seekable_iface_init    (GSeekableIface       *iface);
56 static goffset    g_file_output_stream_seekable_tell          (GSeekable            *seekable);
57 static gboolean   g_file_output_stream_seekable_can_seek      (GSeekable            *seekable);
58 static gboolean   g_file_output_stream_seekable_seek          (GSeekable            *seekable,
59                                                                goffset               offset,
60                                                                GSeekType             type,
61                                                                GCancellable         *cancellable,
62                                                                GError              **error);
63 static gboolean   g_file_output_stream_seekable_can_truncate  (GSeekable            *seekable);
64 static gboolean   g_file_output_stream_seekable_truncate      (GSeekable            *seekable,
65                                                                goffset               offset,
66                                                                GCancellable         *cancellable,
67                                                                GError              **error);
68 static void       g_file_output_stream_real_query_info_async  (GFileOutputStream    *stream,
69                                                                const char           *attributes,
70                                                                int                   io_priority,
71                                                                GCancellable         *cancellable,
72                                                                GAsyncReadyCallback   callback,
73                                                                gpointer              user_data);
74 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream    *stream,
75                                                                GAsyncResult         *result,
76                                                                GError              **error);
77
78 struct _GFileOutputStreamPrivate {
79   GAsyncReadyCallback outstanding_callback;
80 };
81
82 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
83                          G_ADD_PRIVATE (GFileOutputStream)
84                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
85                                                 g_file_output_stream_seekable_iface_init));
86
87 static void
88 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
89 {
90   klass->query_info_async = g_file_output_stream_real_query_info_async;
91   klass->query_info_finish = g_file_output_stream_real_query_info_finish;
92 }
93
94 static void
95 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
96 {
97   iface->tell = g_file_output_stream_seekable_tell;
98   iface->can_seek = g_file_output_stream_seekable_can_seek;
99   iface->seek = g_file_output_stream_seekable_seek;
100   iface->can_truncate = g_file_output_stream_seekable_can_truncate;
101   iface->truncate_fn = g_file_output_stream_seekable_truncate;
102 }
103
104 static void
105 g_file_output_stream_init (GFileOutputStream *stream)
106 {
107   stream->priv = g_file_output_stream_get_instance_private (stream);
108 }
109
110 /**
111  * g_file_output_stream_query_info:
112  * @stream: a #GFileOutputStream.
113  * @attributes: a file attribute query string.
114  * @cancellable: optional #GCancellable object, %NULL to ignore. 
115  * @error: a #GError, %NULL to ignore.
116  *
117  * Queries a file output stream for the given @attributes. 
118  * This function blocks while querying the stream. For the asynchronous 
119  * version of this function, see g_file_output_stream_query_info_async(). 
120  * While the stream is blocked, the stream will set the pending flag 
121  * internally, and any other operations on the stream will fail with 
122  * %G_IO_ERROR_PENDING.
123  * 
124  * Can fail if the stream was already closed (with @error being set to 
125  * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
126  * set to %G_IO_ERROR_PENDING), or if querying info is not supported for 
127  * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
128  * all cases of failure, %NULL will be returned.
129  * 
130  * If @cancellable is not %NULL, then the operation can be cancelled by
131  * triggering the cancellable object from another thread. If the operation
132  * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will 
133  * be returned. 
134  * 
135  * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
136  **/
137 GFileInfo *
138 g_file_output_stream_query_info (GFileOutputStream      *stream,
139                                     const char             *attributes,
140                                     GCancellable           *cancellable,
141                                     GError                **error)
142 {
143   GFileOutputStreamClass *class;
144   GOutputStream *output_stream;
145   GFileInfo *info;
146   
147   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
148   
149   output_stream = G_OUTPUT_STREAM (stream);
150   
151   if (!g_output_stream_set_pending (output_stream, error))
152     return NULL;
153       
154   info = NULL;
155   
156   if (cancellable)
157     g_cancellable_push_current (cancellable);
158   
159   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
160   if (class->query_info)
161     info = class->query_info (stream, attributes, cancellable, error);
162   else
163     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
164                          _("Stream doesn't support query_info"));
165   
166   if (cancellable)
167     g_cancellable_pop_current (cancellable);
168   
169   g_output_stream_clear_pending (output_stream);
170   
171   return info;
172 }
173
174 static void
175 async_ready_callback_wrapper (GObject *source_object,
176                               GAsyncResult *res,
177                               gpointer      user_data)
178 {
179   GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
180
181   g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
182   if (stream->priv->outstanding_callback)
183     (*stream->priv->outstanding_callback) (source_object, res, user_data);
184   g_object_unref (stream);
185 }
186
187 /**
188  * g_file_output_stream_query_info_async:
189  * @stream: a #GFileOutputStream.
190  * @attributes: a file attribute query string.
191  * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link> 
192  *     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                                           const 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   GError *error = NULL;
216
217   g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
218
219   output_stream = G_OUTPUT_STREAM (stream);
220  
221   if (!g_output_stream_set_pending (output_stream, &error))
222     {
223       g_task_report_error (stream, callback, user_data,
224                            g_file_output_stream_query_info_async,
225                            error);
226       return;
227     }
228
229   klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
230
231   stream->priv->outstanding_callback = callback;
232   g_object_ref (stream);
233   klass->query_info_async (stream, attributes, io_priority, cancellable,
234                            async_ready_callback_wrapper, user_data);
235 }
236
237 /**
238  * g_file_output_stream_query_info_finish:
239  * @stream: a #GFileOutputStream.
240  * @result: a #GAsyncResult.
241  * @error: a #GError, %NULL to ignore.
242  * 
243  * Finalizes the asynchronous query started 
244  * by g_file_output_stream_query_info_async().
245  * 
246  * Returns: (transfer full): A #GFileInfo for the finished query.
247  **/
248 GFileInfo *
249 g_file_output_stream_query_info_finish (GFileOutputStream     *stream,
250                                            GAsyncResult         *result,
251                                            GError              **error)
252 {
253   GFileOutputStreamClass *class;
254
255   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
256   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
257   
258   if (g_async_result_legacy_propagate_error (result, error))
259     return NULL;
260   else if (g_async_result_is_tagged (result, g_file_output_stream_query_info_async))
261     return g_task_propagate_pointer (G_TASK (result), error);
262
263   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
264   return class->query_info_finish (stream, result, error);
265 }
266
267 /**
268  * g_file_output_stream_get_etag:
269  * @stream: a #GFileOutputStream.
270  * 
271  * Gets the entity tag for the file when it has been written.
272  * This must be called after the stream has been written
273  * and closed, as the etag can change while writing.
274  * 
275  * Returns: the entity tag for the stream.
276  **/
277 char *
278 g_file_output_stream_get_etag (GFileOutputStream  *stream)
279 {
280   GFileOutputStreamClass *class;
281   GOutputStream *output_stream;
282   char *etag;
283   
284   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
285   
286   output_stream = G_OUTPUT_STREAM (stream);
287   
288   if (!g_output_stream_is_closed (output_stream))
289     {
290       g_warning ("stream is not closed yet, can't get etag");
291       return NULL;
292     }
293
294   etag = NULL;
295   
296   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
297   if (class->get_etag)
298     etag = class->get_etag (stream);
299   
300   return etag;
301 }
302
303 static goffset
304 g_file_output_stream_tell (GFileOutputStream  *stream)
305 {
306   GFileOutputStreamClass *class;
307   goffset offset;
308   
309   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);  
310
311   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
312
313   offset = 0;
314   if (class->tell)
315     offset = class->tell (stream);
316
317   return offset;
318 }
319
320 static goffset
321 g_file_output_stream_seekable_tell (GSeekable *seekable)
322 {
323   return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
324 }
325
326 static gboolean
327 g_file_output_stream_can_seek (GFileOutputStream  *stream)
328 {
329   GFileOutputStreamClass *class;
330   gboolean can_seek;
331
332   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
333
334   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
335
336   can_seek = FALSE;
337   if (class->seek)
338     {
339       can_seek = TRUE;
340       if (class->can_seek)
341         can_seek = class->can_seek (stream);
342     }
343   
344   return can_seek;
345 }
346
347 static gboolean
348 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
349 {
350   return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
351 }
352
353 static gboolean
354 g_file_output_stream_seek (GFileOutputStream  *stream,
355                            goffset             offset,
356                            GSeekType           type,
357                            GCancellable       *cancellable,
358                            GError            **error)
359 {
360   GFileOutputStreamClass *class;
361   GOutputStream *output_stream;
362   gboolean res;
363
364   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
365
366   output_stream = G_OUTPUT_STREAM (stream);
367   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
368
369   if (!class->seek)
370     {
371       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
372                            _("Seek not supported on stream"));
373       return FALSE;
374     }
375
376   if (!g_output_stream_set_pending (output_stream, error))
377     return FALSE;
378   
379   if (cancellable)
380     g_cancellable_push_current (cancellable);
381   
382   res = class->seek (stream, offset, type, cancellable, error);
383   
384   if (cancellable)
385     g_cancellable_pop_current (cancellable);
386
387   g_output_stream_clear_pending (output_stream);
388   
389   return res;
390 }
391
392 static gboolean
393 g_file_output_stream_seekable_seek (GSeekable  *seekable,
394                                     goffset     offset,
395                                     GSeekType   type,
396                                     GCancellable  *cancellable,
397                                     GError    **error)
398 {
399   return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
400                                     offset, type, cancellable, error);
401 }
402
403 static gboolean
404 g_file_output_stream_can_truncate (GFileOutputStream  *stream)
405 {
406   GFileOutputStreamClass *class;
407   gboolean can_truncate;
408
409   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
410
411   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
412
413   can_truncate = FALSE;
414   if (class->truncate_fn)
415     {
416       can_truncate = TRUE;
417       if (class->can_truncate)
418         can_truncate = class->can_truncate (stream);
419     }
420   
421   return can_truncate;
422 }
423
424 static gboolean
425 g_file_output_stream_seekable_can_truncate (GSeekable  *seekable)
426 {
427   return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
428 }
429
430 static gboolean
431 g_file_output_stream_truncate (GFileOutputStream  *stream,
432                                goffset             size,
433                                GCancellable       *cancellable,
434                                GError            **error)
435 {
436   GFileOutputStreamClass *class;
437   GOutputStream *output_stream;
438   gboolean res;
439
440   g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
441
442   output_stream = G_OUTPUT_STREAM (stream);
443   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
444
445   if (!class->truncate_fn)
446     {
447       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
448                            _("Truncate not supported on stream"));
449       return FALSE;
450     }
451
452   if (!g_output_stream_set_pending (output_stream, error))
453     return FALSE;
454   
455   if (cancellable)
456     g_cancellable_push_current (cancellable);
457   
458   res = class->truncate_fn (stream, size, cancellable, error);
459   
460   if (cancellable)
461     g_cancellable_pop_current (cancellable);
462
463   g_output_stream_clear_pending (output_stream);
464   
465   return res;
466 }
467
468 static gboolean
469 g_file_output_stream_seekable_truncate (GSeekable     *seekable,
470                                         goffset        size,
471                                         GCancellable  *cancellable,
472                                         GError       **error)
473 {
474   return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
475                                         size, cancellable, error);
476 }
477 /********************************************
478  *   Default implementation of async ops    *
479  ********************************************/
480
481 static void
482 query_info_async_thread (GTask        *task,
483                          gpointer      source_object,
484                          gpointer      task_data,
485                          GCancellable *cancellable)
486 {
487   GFileOutputStream *stream = source_object;
488   const char *attributes = task_data;
489   GFileOutputStreamClass *class;
490   GError *error = NULL;
491   GFileInfo *info = NULL;
492
493   class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
494   if (class->query_info)
495     info = class->query_info (stream, attributes, cancellable, &error);
496   else
497     g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
498                          _("Stream doesn't support query_info"));
499
500   if (info == NULL)
501     g_task_return_error (task, error);
502   else
503     g_task_return_pointer (task, info, g_object_unref);
504 }
505
506 static void
507 g_file_output_stream_real_query_info_async (GFileOutputStream     *stream,
508                                                const char           *attributes,
509                                                int                   io_priority,
510                                                GCancellable         *cancellable,
511                                                GAsyncReadyCallback   callback,
512                                                gpointer              user_data)
513 {
514   GTask *task;
515
516   task = g_task_new (stream, cancellable, callback, user_data);
517   g_task_set_task_data (task, g_strdup (attributes), g_free);
518   g_task_set_priority (task, io_priority);
519   
520   g_task_run_in_thread (task, query_info_async_thread);
521   g_object_unref (task);
522 }
523
524 static GFileInfo *
525 g_file_output_stream_real_query_info_finish (GFileOutputStream     *stream,
526                                              GAsyncResult         *res,
527                                              GError              **error)
528 {
529   g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
530
531   return g_task_propagate_pointer (G_TASK (res), error);
532 }