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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include <gfileiostream.h>
25 #include <gseekable.h>
26 #include "gasyncresult.h"
28 #include "gcancellable.h"
30 #include "gfileoutputstream.h"
35 * SECTION:gfileiostream
36 * @short_description: File read and write streaming operations
38 * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
40 * GFileIOStream provides io streams that both read and write to the same
43 * GFileIOStream implements #GSeekable, which allows the io
44 * stream to jump to arbitrary positions in the file and to truncate
45 * the file, provided the filesystem of the file supports these
48 * To find the position of a file io stream, use
51 * To find out if a file io stream supports seeking, use g_seekable_can_seek().
52 * To position a file io stream, use g_seekable_seek().
53 * To find out if a file io stream supports truncating, use
54 * g_seekable_can_truncate(). To truncate a file io
55 * stream, use g_seekable_truncate().
57 * The default implementation of all the #GFileIOStream operations
58 * and the implementation of #GSeekable just call into the same operations
59 * on the output stream.
63 static void g_file_io_stream_seekable_iface_init (GSeekableIface *iface);
64 static goffset g_file_io_stream_seekable_tell (GSeekable *seekable);
65 static gboolean g_file_io_stream_seekable_can_seek (GSeekable *seekable);
66 static gboolean g_file_io_stream_seekable_seek (GSeekable *seekable,
69 GCancellable *cancellable,
71 static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable);
72 static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable,
74 GCancellable *cancellable,
76 static void g_file_io_stream_real_query_info_async (GFileIOStream *stream,
77 const char *attributes,
79 GCancellable *cancellable,
80 GAsyncReadyCallback callback,
82 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
86 struct _GFileIOStreamPrivate {
87 GAsyncReadyCallback outstanding_callback;
90 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
91 G_ADD_PRIVATE (GFileIOStream)
92 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
93 g_file_io_stream_seekable_iface_init))
96 g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
98 iface->tell = g_file_io_stream_seekable_tell;
99 iface->can_seek = g_file_io_stream_seekable_can_seek;
100 iface->seek = g_file_io_stream_seekable_seek;
101 iface->can_truncate = g_file_io_stream_seekable_can_truncate;
102 iface->truncate_fn = g_file_io_stream_seekable_truncate;
106 g_file_io_stream_init (GFileIOStream *stream)
108 stream->priv = g_file_io_stream_get_instance_private (stream);
112 * g_file_io_stream_query_info:
113 * @stream: a #GFileIOStream.
114 * @attributes: a file attribute query string.
115 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
116 * @error: a #GError, %NULL to ignore.
118 * Queries a file io stream for the given @attributes.
119 * This function blocks while querying the stream. For the asynchronous
120 * version of this function, see g_file_io_stream_query_info_async().
121 * While the stream is blocked, the stream will set the pending flag
122 * internally, and any other operations on the stream will fail with
123 * %G_IO_ERROR_PENDING.
125 * Can fail if the stream was already closed (with @error being set to
126 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
127 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
128 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I
129 * all cases of failure, %NULL will be returned.
131 * If @cancellable is not %NULL, then the operation can be cancelled by
132 * triggering the cancellable object from another thread. If the operation
133 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
136 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
141 g_file_io_stream_query_info (GFileIOStream *stream,
142 const char *attributes,
143 GCancellable *cancellable,
146 GFileIOStreamClass *class;
147 GIOStream *io_stream;
150 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
152 io_stream = G_IO_STREAM (stream);
154 if (!g_io_stream_set_pending (io_stream, error))
160 g_cancellable_push_current (cancellable);
162 class = G_FILE_IO_STREAM_GET_CLASS (stream);
163 if (class->query_info)
164 info = class->query_info (stream, attributes, cancellable, error);
166 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
167 _("Stream doesn't support query_info"));
170 g_cancellable_pop_current (cancellable);
172 g_io_stream_clear_pending (io_stream);
178 async_ready_callback_wrapper (GObject *source_object,
182 GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
184 g_io_stream_clear_pending (G_IO_STREAM (stream));
185 if (stream->priv->outstanding_callback)
186 (*stream->priv->outstanding_callback) (source_object, res, user_data);
187 g_object_unref (stream);
191 * g_file_io_stream_query_info_async:
192 * @stream: a #GFileIOStream.
193 * @attributes: a file attribute query string.
194 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
196 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
197 * @callback: (scope async): callback to call when the request is satisfied
198 * @user_data: (closure): the data to pass to callback function
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_io_stream_query_info_finish().
204 * For the synchronous version of this function, see
205 * g_file_io_stream_query_info().
210 g_file_io_stream_query_info_async (GFileIOStream *stream,
211 const char *attributes,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
217 GFileIOStreamClass *klass;
218 GIOStream *io_stream;
219 GError *error = NULL;
221 g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
223 io_stream = G_IO_STREAM (stream);
225 if (!g_io_stream_set_pending (io_stream, &error))
227 g_task_report_error (stream, callback, user_data,
228 g_file_io_stream_query_info_async,
233 klass = G_FILE_IO_STREAM_GET_CLASS (stream);
235 stream->priv->outstanding_callback = callback;
236 g_object_ref (stream);
237 klass->query_info_async (stream, attributes, io_priority, cancellable,
238 async_ready_callback_wrapper, user_data);
242 * g_file_io_stream_query_info_finish:
243 * @stream: a #GFileIOStream.
244 * @result: a #GAsyncResult.
245 * @error: a #GError, %NULL to ignore.
247 * Finalizes the asynchronous query started
248 * by g_file_io_stream_query_info_async().
250 * Returns: (transfer full): A #GFileInfo for the finished query.
255 g_file_io_stream_query_info_finish (GFileIOStream *stream,
256 GAsyncResult *result,
259 GFileIOStreamClass *class;
261 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
262 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
264 if (g_async_result_legacy_propagate_error (result, error))
266 else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async))
267 return g_task_propagate_pointer (G_TASK (result), error);
269 class = G_FILE_IO_STREAM_GET_CLASS (stream);
270 return class->query_info_finish (stream, result, error);
274 * g_file_io_stream_get_etag:
275 * @stream: a #GFileIOStream.
277 * Gets the entity tag for the file when it has been written.
278 * This must be called after the stream has been written
279 * and closed, as the etag can change while writing.
281 * Returns: the entity tag for the stream.
286 g_file_io_stream_get_etag (GFileIOStream *stream)
288 GFileIOStreamClass *class;
289 GIOStream *io_stream;
292 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
294 io_stream = G_IO_STREAM (stream);
296 if (!g_io_stream_is_closed (io_stream))
298 g_warning ("stream is not closed yet, can't get etag");
304 class = G_FILE_IO_STREAM_GET_CLASS (stream);
306 etag = class->get_etag (stream);
312 g_file_io_stream_tell (GFileIOStream *stream)
314 GFileIOStreamClass *class;
317 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
319 class = G_FILE_IO_STREAM_GET_CLASS (stream);
323 offset = class->tell (stream);
329 g_file_io_stream_seekable_tell (GSeekable *seekable)
331 return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
335 g_file_io_stream_can_seek (GFileIOStream *stream)
337 GFileIOStreamClass *class;
340 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
342 class = G_FILE_IO_STREAM_GET_CLASS (stream);
349 can_seek = class->can_seek (stream);
356 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
358 return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
362 g_file_io_stream_seek (GFileIOStream *stream,
365 GCancellable *cancellable,
368 GFileIOStreamClass *class;
369 GIOStream *io_stream;
372 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
374 io_stream = G_IO_STREAM (stream);
375 class = G_FILE_IO_STREAM_GET_CLASS (stream);
379 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
380 _("Seek not supported on stream"));
384 if (!g_io_stream_set_pending (io_stream, error))
388 g_cancellable_push_current (cancellable);
390 res = class->seek (stream, offset, type, cancellable, error);
393 g_cancellable_pop_current (cancellable);
395 g_io_stream_clear_pending (io_stream);
401 g_file_io_stream_seekable_seek (GSeekable *seekable,
404 GCancellable *cancellable,
407 return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
408 offset, type, cancellable, error);
412 g_file_io_stream_can_truncate (GFileIOStream *stream)
414 GFileIOStreamClass *class;
415 gboolean can_truncate;
417 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
419 class = G_FILE_IO_STREAM_GET_CLASS (stream);
421 can_truncate = FALSE;
422 if (class->truncate_fn)
425 if (class->can_truncate)
426 can_truncate = class->can_truncate (stream);
433 g_file_io_stream_seekable_can_truncate (GSeekable *seekable)
435 return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
439 g_file_io_stream_truncate (GFileIOStream *stream,
441 GCancellable *cancellable,
444 GFileIOStreamClass *class;
445 GIOStream *io_stream;
448 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
450 io_stream = G_IO_STREAM (stream);
451 class = G_FILE_IO_STREAM_GET_CLASS (stream);
453 if (!class->truncate_fn)
455 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
456 _("Truncate not supported on stream"));
460 if (!g_io_stream_set_pending (io_stream, error))
464 g_cancellable_push_current (cancellable);
466 res = class->truncate_fn (stream, size, cancellable, error);
469 g_cancellable_pop_current (cancellable);
471 g_io_stream_clear_pending (io_stream);
477 g_file_io_stream_seekable_truncate (GSeekable *seekable,
479 GCancellable *cancellable,
482 return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
483 size, cancellable, error);
485 /*****************************************************
486 * Default implementations based on output stream *
487 *****************************************************/
490 g_file_io_stream_real_tell (GFileIOStream *stream)
495 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
496 seekable = G_SEEKABLE (out);
498 return g_seekable_tell (seekable);
502 g_file_io_stream_real_can_seek (GFileIOStream *stream)
507 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
508 seekable = G_SEEKABLE (out);
510 return g_seekable_can_seek (seekable);
514 g_file_io_stream_real_seek (GFileIOStream *stream,
517 GCancellable *cancellable,
523 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
524 seekable = G_SEEKABLE (out);
526 return g_seekable_seek (seekable, offset, type, cancellable, error);
530 g_file_io_stream_real_can_truncate (GFileIOStream *stream)
535 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
536 seekable = G_SEEKABLE (out);
538 return g_seekable_can_truncate (seekable);
542 g_file_io_stream_real_truncate_fn (GFileIOStream *stream,
544 GCancellable *cancellable,
550 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
551 seekable = G_SEEKABLE (out);
553 return g_seekable_truncate (seekable, size, cancellable, error);
557 g_file_io_stream_real_get_etag (GFileIOStream *stream)
560 GFileOutputStream *file_out;
562 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
563 file_out = G_FILE_OUTPUT_STREAM (out);
565 return g_file_output_stream_get_etag (file_out);
569 g_file_io_stream_real_query_info (GFileIOStream *stream,
570 const char *attributes,
571 GCancellable *cancellable,
575 GFileOutputStream *file_out;
577 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
578 file_out = G_FILE_OUTPUT_STREAM (out);
580 return g_file_output_stream_query_info (file_out,
581 attributes, cancellable, error);
586 GAsyncReadyCallback callback;
590 static AsyncOpWrapper *
591 async_op_wrapper_new (gpointer object,
592 GAsyncReadyCallback callback,
595 AsyncOpWrapper *data;
597 data = g_new0 (AsyncOpWrapper, 1);
598 data->object = g_object_ref (object);
599 data->callback = callback;
600 data->user_data = user_data;
606 async_op_wrapper_callback (GObject *source_object,
610 AsyncOpWrapper *data = user_data;
611 data->callback (data->object, res, data->user_data);
612 g_object_unref (data->object);
617 g_file_io_stream_real_query_info_async (GFileIOStream *stream,
618 const char *attributes,
620 GCancellable *cancellable,
621 GAsyncReadyCallback callback,
625 GFileOutputStream *file_out;
626 AsyncOpWrapper *data;
628 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
629 file_out = G_FILE_OUTPUT_STREAM (out);
631 data = async_op_wrapper_new (stream, callback, user_data);
632 g_file_output_stream_query_info_async (file_out,
633 attributes, io_priority,
634 cancellable, async_op_wrapper_callback, data);
638 g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
643 GFileOutputStream *file_out;
645 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
646 file_out = G_FILE_OUTPUT_STREAM (out);
648 return g_file_output_stream_query_info_finish (file_out, res, error);
652 g_file_io_stream_class_init (GFileIOStreamClass *klass)
654 klass->tell = g_file_io_stream_real_tell;
655 klass->can_seek = g_file_io_stream_real_can_seek;
656 klass->seek = g_file_io_stream_real_seek;
657 klass->can_truncate = g_file_io_stream_real_can_truncate;
658 klass->truncate_fn = g_file_io_stream_real_truncate_fn;
659 klass->query_info = g_file_io_stream_real_query_info;
660 klass->query_info_async = g_file_io_stream_real_query_info_async;
661 klass->query_info_finish = g_file_io_stream_real_query_info_finish;
662 klass->get_etag = g_file_io_stream_real_get_etag;