1 /* GIO - GLib Input, IO and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <gfileiostream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
32 #include "gfileoutputstream.h"
38 * SECTION:gfileiostream
39 * @short_description: File read and write streaming operations
41 * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
43 * GFileIOStream provides io streams that both read and write to the same
46 * GFileIOStream implements #GSeekable, which allows the io
47 * stream to jump to arbitrary positions in the file and to truncate
48 * the file, provided the filesystem of the file supports these
51 * To find the position of a file io stream, use
54 * To find out if a file io stream supports seeking, use g_seekable_can_seek().
55 * To position a file io stream, use g_seekable_seek().
56 * To find out if a file io stream supports truncating, use
57 * g_seekable_can_truncate(). To truncate a file io
58 * stream, use g_seekable_truncate().
60 * The default implementation of all the #GFileIOStream operations
61 * and the implementation of #GSeekable just call into the same operations
62 * on the output stream.
66 static void g_file_io_stream_seekable_iface_init (GSeekableIface *iface);
67 static goffset g_file_io_stream_seekable_tell (GSeekable *seekable);
68 static gboolean g_file_io_stream_seekable_can_seek (GSeekable *seekable);
69 static gboolean g_file_io_stream_seekable_seek (GSeekable *seekable,
72 GCancellable *cancellable,
74 static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable);
75 static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable,
77 GCancellable *cancellable,
79 static void g_file_io_stream_real_query_info_async (GFileIOStream *stream,
80 const char *attributes,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
89 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
90 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
91 g_file_io_stream_seekable_iface_init));
93 struct _GFileIOStreamPrivate {
94 GAsyncReadyCallback outstanding_callback;
98 g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
100 iface->tell = g_file_io_stream_seekable_tell;
101 iface->can_seek = g_file_io_stream_seekable_can_seek;
102 iface->seek = g_file_io_stream_seekable_seek;
103 iface->can_truncate = g_file_io_stream_seekable_can_truncate;
104 iface->truncate_fn = g_file_io_stream_seekable_truncate;
108 g_file_io_stream_init (GFileIOStream *stream)
110 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
111 G_TYPE_FILE_IO_STREAM,
112 GFileIOStreamPrivate);
116 * g_file_io_stream_query_info:
117 * @stream: a #GFileIOStream.
118 * @attributes: a file attribute query string.
119 * @cancellable: optional #GCancellable object, %NULL to ignore.
120 * @error: a #GError, %NULL to ignore.
122 * Queries a file io stream for the given @attributes.
123 * This function blocks while querying the stream. For the asynchronous
124 * version of this function, see g_file_io_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.
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). I
133 * all cases of failure, %NULL will be returned.
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
140 * Returns: a #GFileInfo for the @stream, or %NULL on error.
145 g_file_io_stream_query_info (GFileIOStream *stream,
146 const char *attributes,
147 GCancellable *cancellable,
150 GFileIOStreamClass *class;
151 GIOStream *io_stream;
154 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
156 io_stream = G_IO_STREAM (stream);
158 if (!g_io_stream_set_pending (io_stream, error))
164 g_cancellable_push_current (cancellable);
166 class = G_FILE_IO_STREAM_GET_CLASS (stream);
167 if (class->query_info)
168 info = class->query_info (stream, attributes, cancellable, error);
170 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
171 _("Stream doesn't support query_info"));
174 g_cancellable_pop_current (cancellable);
176 g_io_stream_clear_pending (io_stream);
182 async_ready_callback_wrapper (GObject *source_object,
186 GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
188 g_io_stream_clear_pending (G_IO_STREAM (stream));
189 if (stream->priv->outstanding_callback)
190 (*stream->priv->outstanding_callback) (source_object, res, user_data);
191 g_object_unref (stream);
195 * g_file_io_stream_query_info_async:
196 * @stream: a #GFileIOStream.
197 * @attributes: a file attribute query string.
198 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
200 * @cancellable: optional #GCancellable object, %NULL to ignore.
201 * @callback: callback to call when the request is satisfied
202 * @user_data: the data to pass to callback function
204 * Asynchronously queries the @stream for a #GFileInfo. When completed,
205 * @callback will be called with a #GAsyncResult which can be used to
206 * finish the operation with g_file_io_stream_query_info_finish().
208 * For the synchronous version of this function, see
209 * g_file_io_stream_query_info().
214 g_file_io_stream_query_info_async (GFileIOStream *stream,
215 const char *attributes,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
221 GFileIOStreamClass *klass;
222 GIOStream *io_stream;
223 GError *error = NULL;
225 g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
227 io_stream = G_IO_STREAM (stream);
229 if (!g_io_stream_set_pending (io_stream, &error))
231 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
235 g_error_free (error);
239 klass = G_FILE_IO_STREAM_GET_CLASS (stream);
241 stream->priv->outstanding_callback = callback;
242 g_object_ref (stream);
243 klass->query_info_async (stream, attributes, io_priority, cancellable,
244 async_ready_callback_wrapper, user_data);
248 * g_file_io_stream_query_info_finish:
249 * @stream: a #GFileIOStream.
250 * @result: a #GAsyncResult.
251 * @error: a #GError, %NULL to ignore.
253 * Finalizes the asynchronous query started
254 * by g_file_io_stream_query_info_async().
256 * Returns: A #GFileInfo for the finished query.
261 g_file_io_stream_query_info_finish (GFileIOStream *stream,
262 GAsyncResult *result,
265 GSimpleAsyncResult *simple;
266 GFileIOStreamClass *class;
268 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
269 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
271 if (G_IS_SIMPLE_ASYNC_RESULT (result))
273 simple = G_SIMPLE_ASYNC_RESULT (result);
274 if (g_simple_async_result_propagate_error (simple, error))
278 class = G_FILE_IO_STREAM_GET_CLASS (stream);
279 return class->query_info_finish (stream, result, error);
283 * g_file_io_stream_get_etag:
284 * @stream: a #GFileIOStream.
286 * Gets the entity tag for the file when it has been written.
287 * This must be called after the stream has been written
288 * and closed, as the etag can change while writing.
290 * Returns: the entity tag for the stream.
295 g_file_io_stream_get_etag (GFileIOStream *stream)
297 GFileIOStreamClass *class;
298 GIOStream *io_stream;
301 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
303 io_stream = G_IO_STREAM (stream);
305 if (!g_io_stream_is_closed (io_stream))
307 g_warning ("stream is not closed yet, can't get etag");
313 class = G_FILE_IO_STREAM_GET_CLASS (stream);
315 etag = class->get_etag (stream);
321 g_file_io_stream_tell (GFileIOStream *stream)
323 GFileIOStreamClass *class;
326 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
328 class = G_FILE_IO_STREAM_GET_CLASS (stream);
332 offset = class->tell (stream);
338 g_file_io_stream_seekable_tell (GSeekable *seekable)
340 return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
344 g_file_io_stream_can_seek (GFileIOStream *stream)
346 GFileIOStreamClass *class;
349 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
351 class = G_FILE_IO_STREAM_GET_CLASS (stream);
358 can_seek = class->can_seek (stream);
365 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
367 return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
371 g_file_io_stream_seek (GFileIOStream *stream,
374 GCancellable *cancellable,
377 GFileIOStreamClass *class;
378 GIOStream *io_stream;
381 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
383 io_stream = G_IO_STREAM (stream);
384 class = G_FILE_IO_STREAM_GET_CLASS (stream);
388 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
389 _("Seek not supported on stream"));
393 if (!g_io_stream_set_pending (io_stream, error))
397 g_cancellable_push_current (cancellable);
399 res = class->seek (stream, offset, type, cancellable, error);
402 g_cancellable_pop_current (cancellable);
404 g_io_stream_clear_pending (io_stream);
410 g_file_io_stream_seekable_seek (GSeekable *seekable,
413 GCancellable *cancellable,
416 return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
417 offset, type, cancellable, error);
421 g_file_io_stream_can_truncate (GFileIOStream *stream)
423 GFileIOStreamClass *class;
424 gboolean can_truncate;
426 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
428 class = G_FILE_IO_STREAM_GET_CLASS (stream);
430 can_truncate = FALSE;
431 if (class->truncate_fn)
434 if (class->can_truncate)
435 can_truncate = class->can_truncate (stream);
442 g_file_io_stream_seekable_can_truncate (GSeekable *seekable)
444 return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
448 g_file_io_stream_truncate (GFileIOStream *stream,
450 GCancellable *cancellable,
453 GFileIOStreamClass *class;
454 GIOStream *io_stream;
457 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
459 io_stream = G_IO_STREAM (stream);
460 class = G_FILE_IO_STREAM_GET_CLASS (stream);
462 if (!class->truncate_fn)
464 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
465 _("Truncate not supported on stream"));
469 if (!g_io_stream_set_pending (io_stream, error))
473 g_cancellable_push_current (cancellable);
475 res = class->truncate_fn (stream, size, cancellable, error);
478 g_cancellable_pop_current (cancellable);
480 g_io_stream_clear_pending (io_stream);
486 g_file_io_stream_seekable_truncate (GSeekable *seekable,
488 GCancellable *cancellable,
491 return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
492 size, cancellable, error);
494 /*****************************************************
495 * Default implementations based on output stream *
496 *****************************************************/
499 g_file_io_stream_real_tell (GFileIOStream *stream)
504 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
505 seekable = G_SEEKABLE (out);
507 return g_seekable_tell (seekable);
511 g_file_io_stream_real_can_seek (GFileIOStream *stream)
516 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
517 seekable = G_SEEKABLE (out);
519 return g_seekable_can_seek (seekable);
523 g_file_io_stream_real_seek (GFileIOStream *stream,
526 GCancellable *cancellable,
532 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
533 seekable = G_SEEKABLE (out);
535 return g_seekable_seek (seekable, offset, type, cancellable, error);
539 g_file_io_stream_real_can_truncate (GFileIOStream *stream)
544 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
545 seekable = G_SEEKABLE (out);
547 return g_seekable_can_truncate (seekable);
551 g_file_io_stream_real_truncate_fn (GFileIOStream *stream,
553 GCancellable *cancellable,
559 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
560 seekable = G_SEEKABLE (out);
562 return g_seekable_truncate (seekable, size, cancellable, error);
566 g_file_io_stream_real_get_etag (GFileIOStream *stream)
569 GFileOutputStream *file_out;
571 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
572 file_out = G_FILE_OUTPUT_STREAM (out);
574 return g_file_output_stream_get_etag (file_out);
578 g_file_io_stream_real_query_info (GFileIOStream *stream,
579 const char *attributes,
580 GCancellable *cancellable,
584 GFileOutputStream *file_out;
586 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
587 file_out = G_FILE_OUTPUT_STREAM (out);
589 return g_file_output_stream_query_info (file_out,
590 attributes, cancellable, error);
595 GAsyncReadyCallback callback;
599 static AsyncOpWrapper *
600 async_op_wrapper_new (gpointer object,
601 GAsyncReadyCallback callback,
604 AsyncOpWrapper *data;
606 data = g_new0 (AsyncOpWrapper, 1);
607 data->object = g_object_ref (object);
608 data->callback = callback;
609 data->user_data = user_data;
615 async_op_wrapper_callback (GObject *source_object,
619 AsyncOpWrapper *data = user_data;
620 data->callback (data->object, res, data->user_data);
621 g_object_unref (data->object);
626 g_file_io_stream_real_query_info_async (GFileIOStream *stream,
627 const char *attributes,
629 GCancellable *cancellable,
630 GAsyncReadyCallback callback,
634 GFileOutputStream *file_out;
635 AsyncOpWrapper *data;
637 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
638 file_out = G_FILE_OUTPUT_STREAM (out);
640 data = async_op_wrapper_new (stream, callback, user_data);
641 g_file_output_stream_query_info_async (file_out,
642 attributes, io_priority,
643 cancellable, async_op_wrapper_callback, data);
647 g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
652 GFileOutputStream *file_out;
654 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
655 file_out = G_FILE_OUTPUT_STREAM (out);
657 return g_file_output_stream_query_info_finish (file_out, res, error);
661 g_file_io_stream_class_init (GFileIOStreamClass *klass)
663 g_type_class_add_private (klass, sizeof (GFileIOStreamPrivate));
665 klass->tell = g_file_io_stream_real_tell;
666 klass->can_seek = g_file_io_stream_real_can_seek;
667 klass->seek = g_file_io_stream_real_seek;
668 klass->can_truncate = g_file_io_stream_real_can_truncate;
669 klass->truncate_fn = g_file_io_stream_real_truncate_fn;
670 klass->query_info = g_file_io_stream_real_query_info;
671 klass->query_info_async = g_file_io_stream_real_query_info_async;
672 klass->query_info_finish = g_file_io_stream_real_query_info_finish;
673 klass->get_etag = g_file_io_stream_real_get_etag;
676 #define __G_FILE_IO_STREAM_C__
677 #include "gioaliasdef.c"