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