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 "gasyncresult.h"
30 #include "gcancellable.h"
32 #include "gfileoutputstream.h"
37 * SECTION:gfileiostream
38 * @short_description: File read and write streaming operations
40 * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
42 * GFileIOStream provides io streams that both read and write to the same
45 * GFileIOStream implements #GSeekable, which allows the io
46 * stream to jump to arbitrary positions in the file and to truncate
47 * the file, provided the filesystem of the file supports these
50 * To find the position of a file io stream, use
53 * To find out if a file io stream supports seeking, use g_seekable_can_seek().
54 * To position a file io stream, use g_seekable_seek().
55 * To find out if a file io stream supports truncating, use
56 * g_seekable_can_truncate(). To truncate a file io
57 * stream, use g_seekable_truncate().
59 * The default implementation of all the #GFileIOStream operations
60 * and the implementation of #GSeekable just call into the same operations
61 * on the output stream.
65 static void g_file_io_stream_seekable_iface_init (GSeekableIface *iface);
66 static goffset g_file_io_stream_seekable_tell (GSeekable *seekable);
67 static gboolean g_file_io_stream_seekable_can_seek (GSeekable *seekable);
68 static gboolean g_file_io_stream_seekable_seek (GSeekable *seekable,
71 GCancellable *cancellable,
73 static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable);
74 static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable,
76 GCancellable *cancellable,
78 static void g_file_io_stream_real_query_info_async (GFileIOStream *stream,
79 const char *attributes,
81 GCancellable *cancellable,
82 GAsyncReadyCallback callback,
84 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
88 struct _GFileIOStreamPrivate {
89 GAsyncReadyCallback outstanding_callback;
92 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
93 G_ADD_PRIVATE (GFileIOStream)
94 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
95 g_file_io_stream_seekable_iface_init))
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_file_io_stream_get_private (stream);
114 * g_file_io_stream_query_info:
115 * @stream: a #GFileIOStream.
116 * @attributes: a file attribute query string.
117 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
118 * @error: a #GError, %NULL to ignore.
120 * Queries a file io stream for the given @attributes.
121 * This function blocks while querying the stream. For the asynchronous
122 * version of this function, see g_file_io_stream_query_info_async().
123 * While the stream is blocked, the stream will set the pending flag
124 * internally, and any other operations on the stream will fail with
125 * %G_IO_ERROR_PENDING.
127 * Can fail if the stream was already closed (with @error being set to
128 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
129 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
130 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I
131 * all cases of failure, %NULL will be returned.
133 * If @cancellable is not %NULL, then the operation can be cancelled by
134 * triggering the cancellable object from another thread. If the operation
135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
138 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
143 g_file_io_stream_query_info (GFileIOStream *stream,
144 const char *attributes,
145 GCancellable *cancellable,
148 GFileIOStreamClass *class;
149 GIOStream *io_stream;
152 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
154 io_stream = G_IO_STREAM (stream);
156 if (!g_io_stream_set_pending (io_stream, error))
162 g_cancellable_push_current (cancellable);
164 class = G_FILE_IO_STREAM_GET_CLASS (stream);
165 if (class->query_info)
166 info = class->query_info (stream, attributes, cancellable, error);
168 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
169 _("Stream doesn't support query_info"));
172 g_cancellable_pop_current (cancellable);
174 g_io_stream_clear_pending (io_stream);
180 async_ready_callback_wrapper (GObject *source_object,
184 GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
186 g_io_stream_clear_pending (G_IO_STREAM (stream));
187 if (stream->priv->outstanding_callback)
188 (*stream->priv->outstanding_callback) (source_object, res, user_data);
189 g_object_unref (stream);
193 * g_file_io_stream_query_info_async:
194 * @stream: a #GFileIOStream.
195 * @attributes: a file attribute query string.
196 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
198 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
199 * @callback: (scope async): callback to call when the request is satisfied
200 * @user_data: (closure): the data to pass to callback function
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_io_stream_query_info_finish().
206 * For the synchronous version of this function, see
207 * g_file_io_stream_query_info().
212 g_file_io_stream_query_info_async (GFileIOStream *stream,
213 const char *attributes,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
219 GFileIOStreamClass *klass;
220 GIOStream *io_stream;
221 GError *error = NULL;
223 g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
225 io_stream = G_IO_STREAM (stream);
227 if (!g_io_stream_set_pending (io_stream, &error))
229 g_task_report_error (stream, callback, user_data,
230 g_file_io_stream_query_info_async,
235 klass = G_FILE_IO_STREAM_GET_CLASS (stream);
237 stream->priv->outstanding_callback = callback;
238 g_object_ref (stream);
239 klass->query_info_async (stream, attributes, io_priority, cancellable,
240 async_ready_callback_wrapper, user_data);
244 * g_file_io_stream_query_info_finish:
245 * @stream: a #GFileIOStream.
246 * @result: a #GAsyncResult.
247 * @error: a #GError, %NULL to ignore.
249 * Finalizes the asynchronous query started
250 * by g_file_io_stream_query_info_async().
252 * Returns: (transfer full): A #GFileInfo for the finished query.
257 g_file_io_stream_query_info_finish (GFileIOStream *stream,
258 GAsyncResult *result,
261 GFileIOStreamClass *class;
263 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
264 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
266 if (g_async_result_legacy_propagate_error (result, error))
268 else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async))
269 return g_task_propagate_pointer (G_TASK (result), error);
271 class = G_FILE_IO_STREAM_GET_CLASS (stream);
272 return class->query_info_finish (stream, result, error);
276 * g_file_io_stream_get_etag:
277 * @stream: a #GFileIOStream.
279 * Gets the entity tag for the file when it has been written.
280 * This must be called after the stream has been written
281 * and closed, as the etag can change while writing.
283 * Returns: the entity tag for the stream.
288 g_file_io_stream_get_etag (GFileIOStream *stream)
290 GFileIOStreamClass *class;
291 GIOStream *io_stream;
294 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
296 io_stream = G_IO_STREAM (stream);
298 if (!g_io_stream_is_closed (io_stream))
300 g_warning ("stream is not closed yet, can't get etag");
306 class = G_FILE_IO_STREAM_GET_CLASS (stream);
308 etag = class->get_etag (stream);
314 g_file_io_stream_tell (GFileIOStream *stream)
316 GFileIOStreamClass *class;
319 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
321 class = G_FILE_IO_STREAM_GET_CLASS (stream);
325 offset = class->tell (stream);
331 g_file_io_stream_seekable_tell (GSeekable *seekable)
333 return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
337 g_file_io_stream_can_seek (GFileIOStream *stream)
339 GFileIOStreamClass *class;
342 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
344 class = G_FILE_IO_STREAM_GET_CLASS (stream);
351 can_seek = class->can_seek (stream);
358 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
360 return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
364 g_file_io_stream_seek (GFileIOStream *stream,
367 GCancellable *cancellable,
370 GFileIOStreamClass *class;
371 GIOStream *io_stream;
374 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
376 io_stream = G_IO_STREAM (stream);
377 class = G_FILE_IO_STREAM_GET_CLASS (stream);
381 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
382 _("Seek not supported on stream"));
386 if (!g_io_stream_set_pending (io_stream, error))
390 g_cancellable_push_current (cancellable);
392 res = class->seek (stream, offset, type, cancellable, error);
395 g_cancellable_pop_current (cancellable);
397 g_io_stream_clear_pending (io_stream);
403 g_file_io_stream_seekable_seek (GSeekable *seekable,
406 GCancellable *cancellable,
409 return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
410 offset, type, cancellable, error);
414 g_file_io_stream_can_truncate (GFileIOStream *stream)
416 GFileIOStreamClass *class;
417 gboolean can_truncate;
419 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
421 class = G_FILE_IO_STREAM_GET_CLASS (stream);
423 can_truncate = FALSE;
424 if (class->truncate_fn)
427 if (class->can_truncate)
428 can_truncate = class->can_truncate (stream);
435 g_file_io_stream_seekable_can_truncate (GSeekable *seekable)
437 return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
441 g_file_io_stream_truncate (GFileIOStream *stream,
443 GCancellable *cancellable,
446 GFileIOStreamClass *class;
447 GIOStream *io_stream;
450 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
452 io_stream = G_IO_STREAM (stream);
453 class = G_FILE_IO_STREAM_GET_CLASS (stream);
455 if (!class->truncate_fn)
457 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
458 _("Truncate not supported on stream"));
462 if (!g_io_stream_set_pending (io_stream, error))
466 g_cancellable_push_current (cancellable);
468 res = class->truncate_fn (stream, size, cancellable, error);
471 g_cancellable_pop_current (cancellable);
473 g_io_stream_clear_pending (io_stream);
479 g_file_io_stream_seekable_truncate (GSeekable *seekable,
481 GCancellable *cancellable,
484 return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
485 size, cancellable, error);
487 /*****************************************************
488 * Default implementations based on output stream *
489 *****************************************************/
492 g_file_io_stream_real_tell (GFileIOStream *stream)
497 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
498 seekable = G_SEEKABLE (out);
500 return g_seekable_tell (seekable);
504 g_file_io_stream_real_can_seek (GFileIOStream *stream)
509 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
510 seekable = G_SEEKABLE (out);
512 return g_seekable_can_seek (seekable);
516 g_file_io_stream_real_seek (GFileIOStream *stream,
519 GCancellable *cancellable,
525 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
526 seekable = G_SEEKABLE (out);
528 return g_seekable_seek (seekable, offset, type, cancellable, error);
532 g_file_io_stream_real_can_truncate (GFileIOStream *stream)
537 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
538 seekable = G_SEEKABLE (out);
540 return g_seekable_can_truncate (seekable);
544 g_file_io_stream_real_truncate_fn (GFileIOStream *stream,
546 GCancellable *cancellable,
552 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
553 seekable = G_SEEKABLE (out);
555 return g_seekable_truncate (seekable, size, cancellable, error);
559 g_file_io_stream_real_get_etag (GFileIOStream *stream)
562 GFileOutputStream *file_out;
564 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
565 file_out = G_FILE_OUTPUT_STREAM (out);
567 return g_file_output_stream_get_etag (file_out);
571 g_file_io_stream_real_query_info (GFileIOStream *stream,
572 const char *attributes,
573 GCancellable *cancellable,
577 GFileOutputStream *file_out;
579 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
580 file_out = G_FILE_OUTPUT_STREAM (out);
582 return g_file_output_stream_query_info (file_out,
583 attributes, cancellable, error);
588 GAsyncReadyCallback callback;
592 static AsyncOpWrapper *
593 async_op_wrapper_new (gpointer object,
594 GAsyncReadyCallback callback,
597 AsyncOpWrapper *data;
599 data = g_new0 (AsyncOpWrapper, 1);
600 data->object = g_object_ref (object);
601 data->callback = callback;
602 data->user_data = user_data;
608 async_op_wrapper_callback (GObject *source_object,
612 AsyncOpWrapper *data = user_data;
613 data->callback (data->object, res, data->user_data);
614 g_object_unref (data->object);
619 g_file_io_stream_real_query_info_async (GFileIOStream *stream,
620 const char *attributes,
622 GCancellable *cancellable,
623 GAsyncReadyCallback callback,
627 GFileOutputStream *file_out;
628 AsyncOpWrapper *data;
630 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
631 file_out = G_FILE_OUTPUT_STREAM (out);
633 data = async_op_wrapper_new (stream, callback, user_data);
634 g_file_output_stream_query_info_async (file_out,
635 attributes, io_priority,
636 cancellable, async_op_wrapper_callback, data);
640 g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
645 GFileOutputStream *file_out;
647 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
648 file_out = G_FILE_OUTPUT_STREAM (out);
650 return g_file_output_stream_query_info_finish (file_out, res, error);
654 g_file_io_stream_class_init (GFileIOStreamClass *klass)
656 klass->tell = g_file_io_stream_real_tell;
657 klass->can_seek = g_file_io_stream_real_can_seek;
658 klass->seek = g_file_io_stream_real_seek;
659 klass->can_truncate = g_file_io_stream_real_can_truncate;
660 klass->truncate_fn = g_file_io_stream_real_truncate_fn;
661 klass->query_info = g_file_io_stream_real_query_info;
662 klass->query_info_async = g_file_io_stream_real_query_info_async;
663 klass->query_info_finish = g_file_io_stream_real_query_info_finish;
664 klass->get_etag = g_file_io_stream_real_get_etag;