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
36 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
42 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
43 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
44 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
45 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
48 GCancellable *cancellable,
50 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
51 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
53 GCancellable *cancellable,
55 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
58 GCancellable *cancellable,
59 GAsyncReadyCallback callback,
61 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
65 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
66 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
67 g_file_output_stream_seekable_iface_init));
69 struct _GFileOutputStreamPrivate {
70 GAsyncReadyCallback outstanding_callback;
74 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
76 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
78 klass->query_info_async = g_file_output_stream_real_query_info_async;
79 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
83 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
85 iface->tell = g_file_output_stream_seekable_tell;
86 iface->can_seek = g_file_output_stream_seekable_can_seek;
87 iface->seek = g_file_output_stream_seekable_seek;
88 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
89 iface->truncate = g_file_output_stream_seekable_truncate;
93 g_file_output_stream_init (GFileOutputStream *stream)
95 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
96 G_TYPE_FILE_OUTPUT_STREAM,
97 GFileOutputStreamPrivate);
101 * g_file_output_stream_query_info:
102 * @stream: a #GFileOutputStream.
103 * @attributes: a file attribute query string.
104 * @cancellable: optional #GCancellable object, %NULL to ignore.
105 * @error: a #GError, %NULL to ignore.
107 * Queries a file output stream for the given @attributes.
108 * This function blocks while querying the stream. For the asynchronous
109 * version of this function, see g_file_output_stream_query_info_async().
110 * While the stream is blocked, the stream will set the pending flag
111 * internally, and any other operations on the stream will fail with
112 * %G_IO_ERROR_PENDING.
114 * Can fail if the stream was already closed (with @error being set to
115 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
116 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
117 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
118 * all cases of failure, %NULL will be returned.
120 * If @cancellable is not %NULL, then the operation can be cancelled by
121 * triggering the cancellable object from another thread. If the operation
122 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
125 * Returns: a #GFileInfo for the @stream, or %NULL on error.
128 g_file_output_stream_query_info (GFileOutputStream *stream,
130 GCancellable *cancellable,
133 GFileOutputStreamClass *class;
134 GOutputStream *output_stream;
137 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
139 output_stream = G_OUTPUT_STREAM (stream);
141 if (!g_output_stream_set_pending (output_stream, error))
147 g_push_current_cancellable (cancellable);
149 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
150 if (class->query_info)
151 info = class->query_info (stream, attributes, cancellable, error);
153 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
154 _("Stream doesn't support query_info"));
157 g_pop_current_cancellable (cancellable);
159 g_output_stream_clear_pending (output_stream);
165 async_ready_callback_wrapper (GObject *source_object,
169 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
171 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
172 if (stream->priv->outstanding_callback)
173 (*stream->priv->outstanding_callback) (source_object, res, user_data);
174 g_object_unref (stream);
178 * g_file_output_stream_query_info_async:
179 * @stream: a #GFileOutputStream.
180 * @attributes: a file attribute query string.
181 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
183 * @cancellable: optional #GCancellable object, %NULL to ignore.
184 * @callback: callback to call when the request is satisfied
185 * @user_data: the data to pass to callback function
187 * Asynchronously queries the @stream for a #GFileInfo. When completed,
188 * @callback will be called with a #GAsyncResult which can be used to
189 * finish the operation with g_file_output_stream_query_info_finish().
191 * For the synchronous version of this function, see
192 * g_file_output_stream_query_info().
196 g_file_output_stream_query_info_async (GFileOutputStream *stream,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
203 GFileOutputStreamClass *klass;
204 GOutputStream *output_stream;
205 GError *error = NULL;
207 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
209 output_stream = G_OUTPUT_STREAM (stream);
211 if (!g_output_stream_set_pending (output_stream, &error))
213 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
217 g_error_free (error);
221 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
223 stream->priv->outstanding_callback = callback;
224 g_object_ref (stream);
225 klass->query_info_async (stream, attributes, io_priority, cancellable,
226 async_ready_callback_wrapper, user_data);
230 * g_file_output_stream_query_info_finish:
231 * @stream: a #GFileOutputStream.
232 * @result: a #GAsyncResult.
233 * @error: a #GError, %NULL to ignore.
235 * Finalizes the asynchronous query started
236 * by g_file_output_stream_query_info_async().
238 * Returns: A #GFileInfo for the finished query.
241 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
242 GAsyncResult *result,
245 GSimpleAsyncResult *simple;
246 GFileOutputStreamClass *class;
248 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
249 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
251 if (G_IS_SIMPLE_ASYNC_RESULT (result))
253 simple = G_SIMPLE_ASYNC_RESULT (result);
254 if (g_simple_async_result_propagate_error (simple, error))
258 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
259 return class->query_info_finish (stream, result, error);
263 * g_file_output_stream_get_etag:
264 * @stream: a #GFileOutputStream.
266 * Gets the entity tag for the file output stream.
268 * Returns: the entity tag for the stream.
271 g_file_output_stream_get_etag (GFileOutputStream *stream)
273 GFileOutputStreamClass *class;
274 GOutputStream *output_stream;
277 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
279 output_stream = G_OUTPUT_STREAM (stream);
281 if (!g_output_stream_is_closed (output_stream))
283 g_warning ("stream is not closed yet, can't get etag");
289 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
291 etag = class->get_etag (stream);
297 * g_file_output_stream_tell:
298 * @stream: a #GFileOutputStream.
300 * Gets the current location within the stream.
302 * Returns: a #goffset of the location within the stream.
305 g_file_output_stream_tell (GFileOutputStream *stream)
307 GFileOutputStreamClass *class;
310 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
312 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
316 offset = class->tell (stream);
322 g_file_output_stream_seekable_tell (GSeekable *seekable)
324 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
328 * g_file_output_stream_can_seek:
329 * @stream: a #GFileOutputStream.
331 * Checks if the stream can be seeked.
333 * Returns: %TRUE if seeking is supported by the stream.
336 g_file_output_stream_can_seek (GFileOutputStream *stream)
338 GFileOutputStreamClass *class;
341 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
343 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
350 can_seek = class->can_seek (stream);
357 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
359 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
363 * g_file_output_stream_seek:
364 * @stream: a #GFileOutputStream.
365 * @offset: a #goffset to seek.
366 * @type: a #GSeekType.
367 * @cancellable: optional #GCancellable object, %NULL to ignore.
368 * @error: a #GError, %NULL to ignore.
370 * Seeks to a location in a file output stream.
372 * Returns: %TRUE if the seek was successful. %FALSE otherwise.
375 g_file_output_stream_seek (GFileOutputStream *stream,
378 GCancellable *cancellable,
381 GFileOutputStreamClass *class;
382 GOutputStream *output_stream;
385 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
387 output_stream = G_OUTPUT_STREAM (stream);
388 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
392 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
393 _("Seek not supported on stream"));
397 if (!g_output_stream_set_pending (output_stream, error))
401 g_push_current_cancellable (cancellable);
403 res = class->seek (stream, offset, type, cancellable, error);
406 g_pop_current_cancellable (cancellable);
408 g_output_stream_clear_pending (output_stream);
414 g_file_output_stream_seekable_seek (GSeekable *seekable,
417 GCancellable *cancellable,
420 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
421 offset, type, cancellable, error);
425 * g_file_output_stream_can_truncate:
426 * @stream: a #GFileOutputStream.
428 * Checks if the stream can be truncated.
430 * Returns: %TRUE if stream can be truncated.
433 g_file_output_stream_can_truncate (GFileOutputStream *stream)
435 GFileOutputStreamClass *class;
436 gboolean can_truncate;
438 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
440 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
442 can_truncate = FALSE;
446 if (class->can_truncate)
447 can_truncate = class->can_truncate (stream);
454 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
456 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
460 * g_file_output_stream_truncate:
461 * @stream: a #GFileOutputStream.
462 * @size: a #goffset to truncate the stream at.
463 * @cancellable: optional #GCancellable object, %NULL to ignore.
464 * @error: a #GError, %NULL to ignore.
466 * Truncates a file output stream.
468 * Returns: %TRUE if @stream is truncated successfully.
471 g_file_output_stream_truncate (GFileOutputStream *stream,
473 GCancellable *cancellable,
476 GFileOutputStreamClass *class;
477 GOutputStream *output_stream;
480 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
482 output_stream = G_OUTPUT_STREAM (stream);
483 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
485 if (!class->truncate)
487 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
488 _("Truncate not supported on stream"));
492 if (!g_output_stream_set_pending (output_stream, error))
496 g_push_current_cancellable (cancellable);
498 res = class->truncate (stream, size, cancellable, error);
501 g_pop_current_cancellable (cancellable);
503 g_output_stream_clear_pending (output_stream);
509 g_file_output_stream_seekable_truncate (GSeekable *seekable,
511 GCancellable *cancellable,
514 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
515 size, cancellable, error);
517 /********************************************
518 * Default implementation of async ops *
519 ********************************************/
524 } QueryInfoAsyncData;
527 query_info_data_free (QueryInfoAsyncData *data)
530 g_object_unref (data->info);
531 g_free (data->attributes);
536 query_info_async_thread (GSimpleAsyncResult *res,
538 GCancellable *cancellable)
540 GFileOutputStreamClass *class;
541 GError *error = NULL;
542 QueryInfoAsyncData *data;
545 data = g_simple_async_result_get_op_res_gpointer (res);
549 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
550 if (class->query_info)
551 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
553 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
554 _("Stream doesn't support query_info"));
558 g_simple_async_result_set_from_error (res, error);
559 g_error_free (error);
566 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
569 GCancellable *cancellable,
570 GAsyncReadyCallback callback,
573 GSimpleAsyncResult *res;
574 QueryInfoAsyncData *data;
576 data = g_new0 (QueryInfoAsyncData, 1);
577 data->attributes = g_strdup (attributes);
579 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
580 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
582 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
583 g_object_unref (res);
587 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
591 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
592 QueryInfoAsyncData *data;
594 g_assert (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
596 data = g_simple_async_result_get_op_res_gpointer (simple);
598 return g_object_ref (data->info);
603 #define __G_FILE_OUTPUT_STREAM_C__
604 #include "gioaliasdef.c"