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