1 /* GIO - GLib Input, Output 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 <gfileoutputstream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
34 * SECTION:gfileoutputstream
35 * @short_description: File output streaming operations
37 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
39 * GFileOutputStream provides output streams that write their
42 * GFileOutputStream implements #GSeekable, which allows
43 * the output stream to jump to arbitrary positions in the file.
46 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
47 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
48 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
49 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
52 GCancellable *cancellable,
54 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
55 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
57 GCancellable *cancellable,
59 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
62 GCancellable *cancellable,
63 GAsyncReadyCallback callback,
65 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
69 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
70 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
71 g_file_output_stream_seekable_iface_init));
73 struct _GFileOutputStreamPrivate {
74 GAsyncReadyCallback outstanding_callback;
78 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
80 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
82 klass->query_info_async = g_file_output_stream_real_query_info_async;
83 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
87 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
89 iface->tell = g_file_output_stream_seekable_tell;
90 iface->can_seek = g_file_output_stream_seekable_can_seek;
91 iface->seek = g_file_output_stream_seekable_seek;
92 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
93 iface->truncate_fn = g_file_output_stream_seekable_truncate;
97 g_file_output_stream_init (GFileOutputStream *stream)
99 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
100 G_TYPE_FILE_OUTPUT_STREAM,
101 GFileOutputStreamPrivate);
105 * g_file_output_stream_query_info:
106 * @stream: a #GFileOutputStream.
107 * @attributes: a file attribute query string.
108 * @cancellable: optional #GCancellable object, %NULL to ignore.
109 * @error: a #GError, %NULL to ignore.
111 * Queries a file output stream for the given @attributes.
112 * This function blocks while querying the stream. For the asynchronous
113 * version of this function, see g_file_output_stream_query_info_async().
114 * While the stream is blocked, the stream will set the pending flag
115 * internally, and any other operations on the stream will fail with
116 * %G_IO_ERROR_PENDING.
118 * Can fail if the stream was already closed (with @error being set to
119 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
120 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
121 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
122 * all cases of failure, %NULL will be returned.
124 * If @cancellable is not %NULL, then the operation can be cancelled by
125 * triggering the cancellable object from another thread. If the operation
126 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
129 * Returns: a #GFileInfo for the @stream, or %NULL on error.
132 g_file_output_stream_query_info (GFileOutputStream *stream,
134 GCancellable *cancellable,
137 GFileOutputStreamClass *class;
138 GOutputStream *output_stream;
141 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
143 output_stream = G_OUTPUT_STREAM (stream);
145 if (!g_output_stream_set_pending (output_stream, error))
151 g_cancellable_push_current (cancellable);
153 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
154 if (class->query_info)
155 info = class->query_info (stream, attributes, cancellable, error);
157 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
158 _("Stream doesn't support query_info"));
161 g_cancellable_pop_current (cancellable);
163 g_output_stream_clear_pending (output_stream);
169 async_ready_callback_wrapper (GObject *source_object,
173 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
175 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
176 if (stream->priv->outstanding_callback)
177 (*stream->priv->outstanding_callback) (source_object, res, user_data);
178 g_object_unref (stream);
182 * g_file_output_stream_query_info_async:
183 * @stream: a #GFileOutputStream.
184 * @attributes: a file attribute query string.
185 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
187 * @cancellable: optional #GCancellable object, %NULL to ignore.
188 * @callback: callback to call when the request is satisfied
189 * @user_data: the data to pass to callback function
191 * Asynchronously queries the @stream for a #GFileInfo. When completed,
192 * @callback will be called with a #GAsyncResult which can be used to
193 * finish the operation with g_file_output_stream_query_info_finish().
195 * For the synchronous version of this function, see
196 * g_file_output_stream_query_info().
200 g_file_output_stream_query_info_async (GFileOutputStream *stream,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
207 GFileOutputStreamClass *klass;
208 GOutputStream *output_stream;
209 GError *error = NULL;
211 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
213 output_stream = G_OUTPUT_STREAM (stream);
215 if (!g_output_stream_set_pending (output_stream, &error))
217 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
221 g_error_free (error);
225 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
227 stream->priv->outstanding_callback = callback;
228 g_object_ref (stream);
229 klass->query_info_async (stream, attributes, io_priority, cancellable,
230 async_ready_callback_wrapper, user_data);
234 * g_file_output_stream_query_info_finish:
235 * @stream: a #GFileOutputStream.
236 * @result: a #GAsyncResult.
237 * @error: a #GError, %NULL to ignore.
239 * Finalizes the asynchronous query started
240 * by g_file_output_stream_query_info_async().
242 * Returns: A #GFileInfo for the finished query.
245 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
246 GAsyncResult *result,
249 GSimpleAsyncResult *simple;
250 GFileOutputStreamClass *class;
252 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
253 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
255 if (G_IS_SIMPLE_ASYNC_RESULT (result))
257 simple = G_SIMPLE_ASYNC_RESULT (result);
258 if (g_simple_async_result_propagate_error (simple, error))
262 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
263 return class->query_info_finish (stream, result, error);
267 * g_file_output_stream_get_etag:
268 * @stream: a #GFileOutputStream.
270 * Gets the entity tag for the file when its been written.
271 * This must be called after the stream has been written
272 * and closed. As the etag can change while writing.
274 * Returns: the entity tag for the stream.
277 g_file_output_stream_get_etag (GFileOutputStream *stream)
279 GFileOutputStreamClass *class;
280 GOutputStream *output_stream;
283 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
285 output_stream = G_OUTPUT_STREAM (stream);
287 if (!g_output_stream_is_closed (output_stream))
289 g_warning ("stream is not closed yet, can't get etag");
295 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
297 etag = class->get_etag (stream);
303 * g_file_output_stream_tell:
304 * @stream: a #GFileOutputStream.
306 * Gets the current location within the stream.
308 * Returns: a #goffset of the location within the stream.
311 g_file_output_stream_tell (GFileOutputStream *stream)
313 GFileOutputStreamClass *class;
316 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
318 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
322 offset = class->tell (stream);
328 g_file_output_stream_seekable_tell (GSeekable *seekable)
330 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
334 * g_file_output_stream_can_seek:
335 * @stream: a #GFileOutputStream.
337 * Checks if the stream can be seeked.
339 * Returns: %TRUE if seeking is supported by the stream.
342 g_file_output_stream_can_seek (GFileOutputStream *stream)
344 GFileOutputStreamClass *class;
347 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
349 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
356 can_seek = class->can_seek (stream);
363 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
365 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
369 * g_file_output_stream_seek:
370 * @stream: a #GFileOutputStream.
371 * @offset: a #goffset to seek.
372 * @type: a #GSeekType.
373 * @cancellable: optional #GCancellable object, %NULL to ignore.
374 * @error: a #GError, %NULL to ignore.
376 * Seeks to a location in a file output stream.
378 * Returns: %TRUE if the seek was successful. %FALSE otherwise.
381 g_file_output_stream_seek (GFileOutputStream *stream,
384 GCancellable *cancellable,
387 GFileOutputStreamClass *class;
388 GOutputStream *output_stream;
391 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
393 output_stream = G_OUTPUT_STREAM (stream);
394 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
398 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
399 _("Seek not supported on stream"));
403 if (!g_output_stream_set_pending (output_stream, error))
407 g_cancellable_push_current (cancellable);
409 res = class->seek (stream, offset, type, cancellable, error);
412 g_cancellable_pop_current (cancellable);
414 g_output_stream_clear_pending (output_stream);
420 g_file_output_stream_seekable_seek (GSeekable *seekable,
423 GCancellable *cancellable,
426 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
427 offset, type, cancellable, error);
431 * g_file_output_stream_can_truncate:
432 * @stream: a #GFileOutputStream.
434 * Checks if the stream can be truncated.
436 * Returns: %TRUE if stream can be truncated.
439 g_file_output_stream_can_truncate (GFileOutputStream *stream)
441 GFileOutputStreamClass *class;
442 gboolean can_truncate;
444 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
446 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
448 can_truncate = FALSE;
449 if (class->truncate_fn)
452 if (class->can_truncate)
453 can_truncate = class->can_truncate (stream);
460 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
462 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
466 * g_file_output_stream_truncate:
467 * @stream: a #GFileOutputStream.
468 * @size: a #goffset to truncate the stream at.
469 * @cancellable: optional #GCancellable object, %NULL to ignore.
470 * @error: a #GError, %NULL to ignore.
472 * Truncates a file output stream.
474 * Returns: %TRUE if @stream is truncated successfully.
477 g_file_output_stream_truncate (GFileOutputStream *stream,
479 GCancellable *cancellable,
482 GFileOutputStreamClass *class;
483 GOutputStream *output_stream;
486 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
488 output_stream = G_OUTPUT_STREAM (stream);
489 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
491 if (!class->truncate_fn)
493 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
494 _("Truncate not supported on stream"));
498 if (!g_output_stream_set_pending (output_stream, error))
502 g_cancellable_push_current (cancellable);
504 res = class->truncate_fn (stream, size, cancellable, error);
507 g_cancellable_pop_current (cancellable);
509 g_output_stream_clear_pending (output_stream);
515 g_file_output_stream_seekable_truncate (GSeekable *seekable,
517 GCancellable *cancellable,
520 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
521 size, cancellable, error);
523 /********************************************
524 * Default implementation of async ops *
525 ********************************************/
530 } QueryInfoAsyncData;
533 query_info_data_free (QueryInfoAsyncData *data)
536 g_object_unref (data->info);
537 g_free (data->attributes);
542 query_info_async_thread (GSimpleAsyncResult *res,
544 GCancellable *cancellable)
546 GFileOutputStreamClass *class;
547 GError *error = NULL;
548 QueryInfoAsyncData *data;
551 data = g_simple_async_result_get_op_res_gpointer (res);
555 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
556 if (class->query_info)
557 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
559 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
560 _("Stream doesn't support query_info"));
564 g_simple_async_result_set_from_error (res, error);
565 g_error_free (error);
572 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
575 GCancellable *cancellable,
576 GAsyncReadyCallback callback,
579 GSimpleAsyncResult *res;
580 QueryInfoAsyncData *data;
582 data = g_new0 (QueryInfoAsyncData, 1);
583 data->attributes = g_strdup (attributes);
585 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
586 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
588 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
589 g_object_unref (res);
593 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
597 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
598 QueryInfoAsyncData *data;
600 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
602 data = g_simple_async_result_get_op_res_gpointer (simple);
604 return g_object_ref (data->info);
609 #define __G_FILE_OUTPUT_STREAM_C__
610 #include "gioaliasdef.c"