Improvde #include order consistency
[glib.git] / gio / gfileiostream.c
blob7eb6186355b35070bc7c4153efbce8719c0401fb
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>
23 #include "config.h"
25 #include <glib.h>
26 #include <gfileiostream.h>
27 #include <gseekable.h>
28 #include "gasyncresult.h"
29 #include "gtask.h"
30 #include "gcancellable.h"
31 #include "gioerror.h"
32 #include "gfileoutputstream.h"
33 #include "glibintl.h"
36 /**
37 * SECTION:gfileiostream
38 * @short_description: File read and write streaming operations
39 * @include: gio/gio.h
40 * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable
42 * GFileIOStream provides io streams that both read and write to the same
43 * file handle.
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
48 * operations.
50 * To find the position of a file io stream, use
51 * g_seekable_tell().
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.
62 * Since: 2.22
63 **/
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,
69 goffset offset,
70 GSeekType type,
71 GCancellable *cancellable,
72 GError **error);
73 static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable);
74 static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable,
75 goffset offset,
76 GCancellable *cancellable,
77 GError **error);
78 static void g_file_io_stream_real_query_info_async (GFileIOStream *stream,
79 const char *attributes,
80 int io_priority,
81 GCancellable *cancellable,
82 GAsyncReadyCallback callback,
83 gpointer user_data);
84 static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
85 GAsyncResult *result,
86 GError **error);
88 G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM,
89 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
90 g_file_io_stream_seekable_iface_init));
92 struct _GFileIOStreamPrivate {
93 GAsyncReadyCallback outstanding_callback;
96 static void
97 g_file_io_stream_seekable_iface_init (GSeekableIface *iface)
99 iface->tell = g_file_io_stream_seekable_tell;
100 iface->can_seek = g_file_io_stream_seekable_can_seek;
101 iface->seek = g_file_io_stream_seekable_seek;
102 iface->can_truncate = g_file_io_stream_seekable_can_truncate;
103 iface->truncate_fn = g_file_io_stream_seekable_truncate;
106 static void
107 g_file_io_stream_init (GFileIOStream *stream)
109 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
110 G_TYPE_FILE_IO_STREAM,
111 GFileIOStreamPrivate);
115 * g_file_io_stream_query_info:
116 * @stream: a #GFileIOStream.
117 * @attributes: a file attribute query string.
118 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
119 * @error: a #GError, %NULL to ignore.
121 * Queries a file io stream for the given @attributes.
122 * This function blocks while querying the stream. For the asynchronous
123 * version of this function, see g_file_io_stream_query_info_async().
124 * While the stream is blocked, the stream will set the pending flag
125 * internally, and any other operations on the stream will fail with
126 * %G_IO_ERROR_PENDING.
128 * Can fail if the stream was already closed (with @error being set to
129 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
130 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
131 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I
132 * all cases of failure, %NULL will be returned.
134 * If @cancellable is not %NULL, then the operation can be cancelled by
135 * triggering the cancellable object from another thread. If the operation
136 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
137 * be returned.
139 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
141 * Since: 2.22
143 GFileInfo *
144 g_file_io_stream_query_info (GFileIOStream *stream,
145 const char *attributes,
146 GCancellable *cancellable,
147 GError **error)
149 GFileIOStreamClass *class;
150 GIOStream *io_stream;
151 GFileInfo *info;
153 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
155 io_stream = G_IO_STREAM (stream);
157 if (!g_io_stream_set_pending (io_stream, error))
158 return NULL;
160 info = NULL;
162 if (cancellable)
163 g_cancellable_push_current (cancellable);
165 class = G_FILE_IO_STREAM_GET_CLASS (stream);
166 if (class->query_info)
167 info = class->query_info (stream, attributes, cancellable, error);
168 else
169 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
170 _("Stream doesn't support query_info"));
172 if (cancellable)
173 g_cancellable_pop_current (cancellable);
175 g_io_stream_clear_pending (io_stream);
177 return info;
180 static void
181 async_ready_callback_wrapper (GObject *source_object,
182 GAsyncResult *res,
183 gpointer user_data)
185 GFileIOStream *stream = G_FILE_IO_STREAM (source_object);
187 g_io_stream_clear_pending (G_IO_STREAM (stream));
188 if (stream->priv->outstanding_callback)
189 (*stream->priv->outstanding_callback) (source_object, res, user_data);
190 g_object_unref (stream);
194 * g_file_io_stream_query_info_async:
195 * @stream: a #GFileIOStream.
196 * @attributes: a file attribute query string.
197 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
198 * of the request.
199 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
200 * @callback: (scope async): callback to call when the request is satisfied
201 * @user_data: (closure): the data to pass to callback function
203 * Asynchronously queries the @stream for a #GFileInfo. When completed,
204 * @callback will be called with a #GAsyncResult which can be used to
205 * finish the operation with g_file_io_stream_query_info_finish().
207 * For the synchronous version of this function, see
208 * g_file_io_stream_query_info().
210 * Since: 2.22
212 void
213 g_file_io_stream_query_info_async (GFileIOStream *stream,
214 const char *attributes,
215 int io_priority,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
218 gpointer user_data)
220 GFileIOStreamClass *klass;
221 GIOStream *io_stream;
222 GError *error = NULL;
224 g_return_if_fail (G_IS_FILE_IO_STREAM (stream));
226 io_stream = G_IO_STREAM (stream);
228 if (!g_io_stream_set_pending (io_stream, &error))
230 g_task_report_error (stream, callback, user_data,
231 g_file_io_stream_query_info_async,
232 error);
233 return;
236 klass = G_FILE_IO_STREAM_GET_CLASS (stream);
238 stream->priv->outstanding_callback = callback;
239 g_object_ref (stream);
240 klass->query_info_async (stream, attributes, io_priority, cancellable,
241 async_ready_callback_wrapper, user_data);
245 * g_file_io_stream_query_info_finish:
246 * @stream: a #GFileIOStream.
247 * @result: a #GAsyncResult.
248 * @error: a #GError, %NULL to ignore.
250 * Finalizes the asynchronous query started
251 * by g_file_io_stream_query_info_async().
253 * Returns: (transfer full): A #GFileInfo for the finished query.
255 * Since: 2.22
257 GFileInfo *
258 g_file_io_stream_query_info_finish (GFileIOStream *stream,
259 GAsyncResult *result,
260 GError **error)
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_async_result_legacy_propagate_error (result, error))
268 return NULL;
269 else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async))
270 return g_task_propagate_pointer (G_TASK (result), error);
272 class = G_FILE_IO_STREAM_GET_CLASS (stream);
273 return class->query_info_finish (stream, result, error);
277 * g_file_io_stream_get_etag:
278 * @stream: a #GFileIOStream.
280 * Gets the entity tag for the file when it has been written.
281 * This must be called after the stream has been written
282 * and closed, as the etag can change while writing.
284 * Returns: the entity tag for the stream.
286 * Since: 2.22
288 char *
289 g_file_io_stream_get_etag (GFileIOStream *stream)
291 GFileIOStreamClass *class;
292 GIOStream *io_stream;
293 char *etag;
295 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL);
297 io_stream = G_IO_STREAM (stream);
299 if (!g_io_stream_is_closed (io_stream))
301 g_warning ("stream is not closed yet, can't get etag");
302 return NULL;
305 etag = NULL;
307 class = G_FILE_IO_STREAM_GET_CLASS (stream);
308 if (class->get_etag)
309 etag = class->get_etag (stream);
311 return etag;
314 static goffset
315 g_file_io_stream_tell (GFileIOStream *stream)
317 GFileIOStreamClass *class;
318 goffset offset;
320 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0);
322 class = G_FILE_IO_STREAM_GET_CLASS (stream);
324 offset = 0;
325 if (class->tell)
326 offset = class->tell (stream);
328 return offset;
331 static goffset
332 g_file_io_stream_seekable_tell (GSeekable *seekable)
334 return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable));
337 static gboolean
338 g_file_io_stream_can_seek (GFileIOStream *stream)
340 GFileIOStreamClass *class;
341 gboolean can_seek;
343 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
345 class = G_FILE_IO_STREAM_GET_CLASS (stream);
347 can_seek = FALSE;
348 if (class->seek)
350 can_seek = TRUE;
351 if (class->can_seek)
352 can_seek = class->can_seek (stream);
355 return can_seek;
358 static gboolean
359 g_file_io_stream_seekable_can_seek (GSeekable *seekable)
361 return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable));
364 static gboolean
365 g_file_io_stream_seek (GFileIOStream *stream,
366 goffset offset,
367 GSeekType type,
368 GCancellable *cancellable,
369 GError **error)
371 GFileIOStreamClass *class;
372 GIOStream *io_stream;
373 gboolean res;
375 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
377 io_stream = G_IO_STREAM (stream);
378 class = G_FILE_IO_STREAM_GET_CLASS (stream);
380 if (!class->seek)
382 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
383 _("Seek not supported on stream"));
384 return FALSE;
387 if (!g_io_stream_set_pending (io_stream, error))
388 return FALSE;
390 if (cancellable)
391 g_cancellable_push_current (cancellable);
393 res = class->seek (stream, offset, type, cancellable, error);
395 if (cancellable)
396 g_cancellable_pop_current (cancellable);
398 g_io_stream_clear_pending (io_stream);
400 return res;
403 static gboolean
404 g_file_io_stream_seekable_seek (GSeekable *seekable,
405 goffset offset,
406 GSeekType type,
407 GCancellable *cancellable,
408 GError **error)
410 return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable),
411 offset, type, cancellable, error);
414 static gboolean
415 g_file_io_stream_can_truncate (GFileIOStream *stream)
417 GFileIOStreamClass *class;
418 gboolean can_truncate;
420 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
422 class = G_FILE_IO_STREAM_GET_CLASS (stream);
424 can_truncate = FALSE;
425 if (class->truncate_fn)
427 can_truncate = TRUE;
428 if (class->can_truncate)
429 can_truncate = class->can_truncate (stream);
432 return can_truncate;
435 static gboolean
436 g_file_io_stream_seekable_can_truncate (GSeekable *seekable)
438 return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable));
441 static gboolean
442 g_file_io_stream_truncate (GFileIOStream *stream,
443 goffset size,
444 GCancellable *cancellable,
445 GError **error)
447 GFileIOStreamClass *class;
448 GIOStream *io_stream;
449 gboolean res;
451 g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE);
453 io_stream = G_IO_STREAM (stream);
454 class = G_FILE_IO_STREAM_GET_CLASS (stream);
456 if (!class->truncate_fn)
458 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
459 _("Truncate not supported on stream"));
460 return FALSE;
463 if (!g_io_stream_set_pending (io_stream, error))
464 return FALSE;
466 if (cancellable)
467 g_cancellable_push_current (cancellable);
469 res = class->truncate_fn (stream, size, cancellable, error);
471 if (cancellable)
472 g_cancellable_pop_current (cancellable);
474 g_io_stream_clear_pending (io_stream);
476 return res;
479 static gboolean
480 g_file_io_stream_seekable_truncate (GSeekable *seekable,
481 goffset size,
482 GCancellable *cancellable,
483 GError **error)
485 return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable),
486 size, cancellable, error);
488 /*****************************************************
489 * Default implementations based on output stream *
490 *****************************************************/
492 static goffset
493 g_file_io_stream_real_tell (GFileIOStream *stream)
495 GOutputStream *out;
496 GSeekable *seekable;
498 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
499 seekable = G_SEEKABLE (out);
501 return g_seekable_tell (seekable);
504 static gboolean
505 g_file_io_stream_real_can_seek (GFileIOStream *stream)
507 GOutputStream *out;
508 GSeekable *seekable;
510 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
511 seekable = G_SEEKABLE (out);
513 return g_seekable_can_seek (seekable);
516 static gboolean
517 g_file_io_stream_real_seek (GFileIOStream *stream,
518 goffset offset,
519 GSeekType type,
520 GCancellable *cancellable,
521 GError **error)
523 GOutputStream *out;
524 GSeekable *seekable;
526 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
527 seekable = G_SEEKABLE (out);
529 return g_seekable_seek (seekable, offset, type, cancellable, error);
532 static gboolean
533 g_file_io_stream_real_can_truncate (GFileIOStream *stream)
535 GOutputStream *out;
536 GSeekable *seekable;
538 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
539 seekable = G_SEEKABLE (out);
541 return g_seekable_can_truncate (seekable);
544 static gboolean
545 g_file_io_stream_real_truncate_fn (GFileIOStream *stream,
546 goffset size,
547 GCancellable *cancellable,
548 GError **error)
550 GOutputStream *out;
551 GSeekable *seekable;
553 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
554 seekable = G_SEEKABLE (out);
556 return g_seekable_truncate (seekable, size, cancellable, error);
559 static char *
560 g_file_io_stream_real_get_etag (GFileIOStream *stream)
562 GOutputStream *out;
563 GFileOutputStream *file_out;
565 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
566 file_out = G_FILE_OUTPUT_STREAM (out);
568 return g_file_output_stream_get_etag (file_out);
571 static GFileInfo *
572 g_file_io_stream_real_query_info (GFileIOStream *stream,
573 const char *attributes,
574 GCancellable *cancellable,
575 GError **error)
577 GOutputStream *out;
578 GFileOutputStream *file_out;
580 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
581 file_out = G_FILE_OUTPUT_STREAM (out);
583 return g_file_output_stream_query_info (file_out,
584 attributes, cancellable, error);
587 typedef struct {
588 GObject *object;
589 GAsyncReadyCallback callback;
590 gpointer user_data;
591 } AsyncOpWrapper;
593 static AsyncOpWrapper *
594 async_op_wrapper_new (gpointer object,
595 GAsyncReadyCallback callback,
596 gpointer user_data)
598 AsyncOpWrapper *data;
600 data = g_new0 (AsyncOpWrapper, 1);
601 data->object = g_object_ref (object);
602 data->callback = callback;
603 data->user_data = user_data;
605 return data;
608 static void
609 async_op_wrapper_callback (GObject *source_object,
610 GAsyncResult *res,
611 gpointer user_data)
613 AsyncOpWrapper *data = user_data;
614 data->callback (data->object, res, data->user_data);
615 g_object_unref (data->object);
616 g_free (data);
619 static void
620 g_file_io_stream_real_query_info_async (GFileIOStream *stream,
621 const char *attributes,
622 int io_priority,
623 GCancellable *cancellable,
624 GAsyncReadyCallback callback,
625 gpointer user_data)
627 GOutputStream *out;
628 GFileOutputStream *file_out;
629 AsyncOpWrapper *data;
631 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
632 file_out = G_FILE_OUTPUT_STREAM (out);
634 data = async_op_wrapper_new (stream, callback, user_data);
635 g_file_output_stream_query_info_async (file_out,
636 attributes, io_priority,
637 cancellable, async_op_wrapper_callback, data);
640 static GFileInfo *
641 g_file_io_stream_real_query_info_finish (GFileIOStream *stream,
642 GAsyncResult *res,
643 GError **error)
645 GOutputStream *out;
646 GFileOutputStream *file_out;
648 out = g_io_stream_get_output_stream (G_IO_STREAM (stream));
649 file_out = G_FILE_OUTPUT_STREAM (out);
651 return g_file_output_stream_query_info_finish (file_out, res, error);
654 static void
655 g_file_io_stream_class_init (GFileIOStreamClass *klass)
657 g_type_class_add_private (klass, sizeof (GFileIOStreamPrivate));
659 klass->tell = g_file_io_stream_real_tell;
660 klass->can_seek = g_file_io_stream_real_can_seek;
661 klass->seek = g_file_io_stream_real_seek;
662 klass->can_truncate = g_file_io_stream_real_can_truncate;
663 klass->truncate_fn = g_file_io_stream_real_truncate_fn;
664 klass->query_info = g_file_io_stream_real_query_info;
665 klass->query_info_async = g_file_io_stream_real_query_info_async;
666 klass->query_info_finish = g_file_io_stream_real_query_info_finish;
667 klass->get_etag = g_file_io_stream_real_get_etag;