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: read write streams for File
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
49 * operations. In addition to the generic g_seekable_ API,
50 * GFileIOStream has its own API for seeking and positioning.
51 * To find the position of a file io stream, use
52 * g_file_io_stream_tell(). To find out if a file io
53 * stream supports seeking, use g_file_io_stream_can_seek().
54 * To position a file io stream, use g_file_io_stream_seek().
55 * To find out if a file io stream supports truncating, use
56 * g_file_io_stream_can_truncate(). To truncate a file io
57 * stream, use g_file_io_stream_truncate().
62 static void g_file_io_stream_seekable_iface_init (GSeekableIface *iface);
63 static goffset g_file_io_stream_seekable_tell (GSeekable *seekable);
64 static gboolean g_file_io_stream_seekable_can_seek (GSeekable *seekable);
65 static gboolean g_file_io_stream_seekable_seek (GSeekable *seekable,
68 GCancellable *cancellable,
70 static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable);
71 static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable,
73 GCancellable *cancellable,
75 static void g_file_io_stream_real_query_info_async (GFileIOStream *stream,
76 const char *attributes,
78 GCancellable *cancellable,
79 GAsyncReadyCallback callback,
81 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
85 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
86 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
87 g_file_io_stream_seekable_iface_init));
89 struct _GFileIOStreamPrivate {
90 GAsyncReadyCallback outstanding_callback;
94 g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
96 iface->tell = g_file_io_stream_seekable_tell;
97 iface->can_seek = g_file_io_stream_seekable_can_seek;
98 iface->seek = g_file_io_stream_seekable_seek;
99 iface->can_truncate = g_file_io_stream_seekable_can_truncate;
100 iface->truncate_fn = g_file_io_stream_seekable_truncate;
104 g_file_io_stream_init (GFileIOStream *stream)
106 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
107 G_TYPE_FILE_IO_STREAM,
108 GFileIOStreamPrivate);
112 * g_file_io_stream_query_info:
113 * @stream: a #GFileIOStream.
114 * @attributes: a file attribute query string.
115 * @cancellable: 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: 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: 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
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_simple_async_report_gerror_in_idle (G_OBJECT (stream),
231 g_error_free (error);
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: A #GFileInfo for the finished query.
257 g_file_io_stream_query_info_finish (GFileIOStream *stream,
258 GAsyncResult *result,
261 GSimpleAsyncResult *simple;
262 GFileIOStreamClass *class;
264 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
265 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
267 if (G_IS_SIMPLE_ASYNC_RESULT (result))
269 simple = G_SIMPLE_ASYNC_RESULT (result);
270 if (g_simple_async_result_propagate_error (simple, error))
274 class = G_FILE_IO_STREAM_GET_CLASS (stream);
275 return class->query_info_finish (stream, result, error);
279 * g_file_io_stream_get_etag:
280 * @stream: a #GFileIOStream.
282 * Gets the entity tag for the file when it has been written.
283 * This must be called after the stream has been written
284 * and closed, as the etag can change while writing.
286 * Returns: the entity tag for the stream.
291 g_file_io_stream_get_etag (GFileIOStream *stream)
293 GFileIOStreamClass *class;
294 GIOStream *io_stream;
297 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
299 io_stream = G_IO_STREAM (stream);
301 if (!g_io_stream_is_closed (io_stream))
303 g_warning ("stream is not closed yet, can't get etag");
309 class = G_FILE_IO_STREAM_GET_CLASS (stream);
311 etag = class->get_etag (stream);
317 g_file_io_stream_tell (GFileIOStream *stream)
319 GFileIOStreamClass *class;
322 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
324 class = G_FILE_IO_STREAM_GET_CLASS (stream);
328 offset = class->tell (stream);
334 g_file_io_stream_seekable_tell (GSeekable *seekable)
336 return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
340 g_file_io_stream_can_seek (GFileIOStream *stream)
342 GFileIOStreamClass *class;
345 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
347 class = G_FILE_IO_STREAM_GET_CLASS (stream);
354 can_seek = class->can_seek (stream);
361 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
363 return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
367 g_file_io_stream_seek (GFileIOStream *stream,
370 GCancellable *cancellable,
373 GFileIOStreamClass *class;
374 GIOStream *io_stream;
377 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
379 io_stream = G_IO_STREAM (stream);
380 class = G_FILE_IO_STREAM_GET_CLASS (stream);
384 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
385 _("Seek not supported on stream"));
389 if (!g_io_stream_set_pending (io_stream, error))
393 g_cancellable_push_current (cancellable);
395 res = class->seek (stream, offset, type, cancellable, error);
398 g_cancellable_pop_current (cancellable);
400 g_io_stream_clear_pending (io_stream);
406 g_file_io_stream_seekable_seek (GSeekable *seekable,
409 GCancellable *cancellable,
412 return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
413 offset, type, cancellable, error);
417 g_file_io_stream_can_truncate (GFileIOStream *stream)
419 GFileIOStreamClass *class;
420 gboolean can_truncate;
422 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
424 class = G_FILE_IO_STREAM_GET_CLASS (stream);
426 can_truncate = FALSE;
427 if (class->truncate_fn)
430 if (class->can_truncate)
431 can_truncate = class->can_truncate (stream);
438 g_file_io_stream_seekable_can_truncate (GSeekable *seekable)
440 return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
444 g_file_io_stream_truncate (GFileIOStream *stream,
446 GCancellable *cancellable,
449 GFileIOStreamClass *class;
450 GIOStream *io_stream;
453 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
455 io_stream = G_IO_STREAM (stream);
456 class = G_FILE_IO_STREAM_GET_CLASS (stream);
458 if (!class->truncate_fn)
460 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
461 _("Truncate not supported on stream"));
465 if (!g_io_stream_set_pending (io_stream, error))
469 g_cancellable_push_current (cancellable);
471 res = class->truncate_fn (stream, size, cancellable, error);
474 g_cancellable_pop_current (cancellable);
476 g_io_stream_clear_pending (io_stream);
482 g_file_io_stream_seekable_truncate (GSeekable *seekable,
484 GCancellable *cancellable,
487 return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
488 size, cancellable, error);
490 /*****************************************************
491 * Default implementations based on output stream *
492 *****************************************************/
495 g_file_io_stream_real_tell (GFileIOStream *stream)
500 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
501 seekable = G_SEEKABLE (out);
503 return g_seekable_tell (seekable);
507 g_file_io_stream_real_can_seek (GFileIOStream *stream)
512 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
513 seekable = G_SEEKABLE (out);
515 return g_seekable_can_seek (seekable);
519 g_file_io_stream_real_seek (GFileIOStream *stream,
522 GCancellable *cancellable,
528 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
529 seekable = G_SEEKABLE (out);
531 return g_seekable_seek (seekable, offset, type, cancellable, error);
535 g_file_io_stream_real_can_truncate (GFileIOStream *stream)
540 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
541 seekable = G_SEEKABLE (out);
543 return g_seekable_can_truncate (seekable);
547 g_file_io_stream_real_truncate_fn (GFileIOStream *stream,
549 GCancellable *cancellable,
555 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
556 seekable = G_SEEKABLE (out);
558 return g_seekable_truncate (seekable, size, cancellable, error);
562 g_file_io_stream_real_get_etag (GFileIOStream *stream)
565 GFileOutputStream *file_out;
567 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
568 file_out = G_FILE_OUTPUT_STREAM (out);
570 return g_file_output_stream_get_etag (file_out);
574 g_file_io_stream_real_query_info (GFileIOStream *stream,
575 const char *attributes,
576 GCancellable *cancellable,
580 GFileOutputStream *file_out;
582 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
583 file_out = G_FILE_OUTPUT_STREAM (out);
585 return g_file_output_stream_query_info (file_out,
586 attributes, cancellable, error);
591 GAsyncReadyCallback callback;
595 static AsyncOpWrapper *
596 async_op_wrapper_new (gpointer object,
597 GAsyncReadyCallback callback,
600 AsyncOpWrapper *data;
602 data = g_new0 (AsyncOpWrapper, 1);
603 data->object = g_object_ref (object);
604 data->callback = callback;
605 data->user_data = user_data;
611 async_op_wrapper_callback (GObject *source_object,
615 AsyncOpWrapper *data = user_data;
616 data->callback (data->object, res, data->user_data);
617 g_object_unref (data->object);
622 g_file_io_stream_real_query_info_async (GFileIOStream *stream,
623 const char *attributes,
625 GCancellable *cancellable,
626 GAsyncReadyCallback callback,
630 GFileOutputStream *file_out;
631 AsyncOpWrapper *data;
633 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
634 file_out = G_FILE_OUTPUT_STREAM (out);
636 data = async_op_wrapper_new (stream, callback, user_data);
637 g_file_output_stream_query_info_async (file_out,
638 attributes, io_priority,
639 cancellable, async_op_wrapper_callback, data);
643 g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
648 GFileOutputStream *file_out;
650 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
651 file_out = G_FILE_OUTPUT_STREAM (out);
653 return g_file_output_stream_query_info_finish (file_out, res, error);
657 g_file_io_stream_class_init (GFileIOStreamClass *klass)
659 g_type_class_add_private (klass, sizeof (GFileIOStreamPrivate));
661 klass->tell = g_file_io_stream_real_tell;
662 klass->can_seek = g_file_io_stream_real_can_seek;
663 klass->seek = g_file_io_stream_real_seek;
664 klass->can_truncate = g_file_io_stream_real_can_truncate;
665 klass->truncate_fn = g_file_io_stream_real_truncate_fn;
666 klass->query_info = g_file_io_stream_real_query_info;
667 klass->query_info_async = g_file_io_stream_real_query_info_async;
668 klass->query_info_finish = g_file_io_stream_real_query_info_finish;
669 klass->get_etag = g_file_io_stream_real_get_etag;
672 #define __G_FILE_IO_STREAM_C__
673 #include "gioaliasdef.c"