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"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
36 * SECTION:gfileoutputstream
37 * @short_description: File output streaming operations
39 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
41 * GFileOutputStream provides output streams that write their
44 * GFileOutputStream implements #GSeekable, which allows the output
45 * stream to jump to arbitrary positions in the file and to truncate
46 * the file, provided the filesystem of the file supports these
49 * To find the position of a file output stream, use g_seekable_tell().
50 * To find out if a file output stream supports seeking, use
51 * g_seekable_can_seek().To position a file output stream, use
52 * g_seekable_seek(). To find out if a file output stream supports
53 * truncating, use g_seekable_can_truncate(). To truncate a file output
54 * stream, use g_seekable_truncate().
57 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
58 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
59 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
60 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
63 GCancellable *cancellable,
65 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
66 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
68 GCancellable *cancellable,
70 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
71 const char *attributes,
73 GCancellable *cancellable,
74 GAsyncReadyCallback callback,
76 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
80 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
81 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
82 g_file_output_stream_seekable_iface_init));
84 struct _GFileOutputStreamPrivate {
85 GAsyncReadyCallback outstanding_callback;
89 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
91 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
93 klass->query_info_async = g_file_output_stream_real_query_info_async;
94 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
98 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
100 iface->tell = g_file_output_stream_seekable_tell;
101 iface->can_seek = g_file_output_stream_seekable_can_seek;
102 iface->seek = g_file_output_stream_seekable_seek;
103 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
104 iface->truncate_fn = g_file_output_stream_seekable_truncate;
108 g_file_output_stream_init (GFileOutputStream *stream)
110 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
111 G_TYPE_FILE_OUTPUT_STREAM,
112 GFileOutputStreamPrivate);
116 * g_file_output_stream_query_info:
117 * @stream: a #GFileOutputStream.
118 * @attributes: a file attribute query string.
119 * @cancellable: optional #GCancellable object, %NULL to ignore.
120 * @error: a #GError, %NULL to ignore.
122 * Queries a file output stream for the given @attributes.
123 * This function blocks while querying the stream. For the asynchronous
124 * version of this function, see g_file_output_stream_query_info_async().
125 * While the stream is blocked, the stream will set the pending flag
126 * internally, and any other operations on the stream will fail with
127 * %G_IO_ERROR_PENDING.
129 * Can fail if the stream was already closed (with @error being set to
130 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
131 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
132 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
133 * all cases of failure, %NULL will be returned.
135 * If @cancellable is not %NULL, then the operation can be cancelled by
136 * triggering the cancellable object from another thread. If the operation
137 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
140 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
143 g_file_output_stream_query_info (GFileOutputStream *stream,
144 const char *attributes,
145 GCancellable *cancellable,
148 GFileOutputStreamClass *class;
149 GOutputStream *output_stream;
152 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
154 output_stream = G_OUTPUT_STREAM (stream);
156 if (!g_output_stream_set_pending (output_stream, error))
162 g_cancellable_push_current (cancellable);
164 class = G_FILE_OUTPUT_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_output_stream_clear_pending (output_stream);
180 async_ready_callback_wrapper (GObject *source_object,
184 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
186 g_output_stream_clear_pending (G_OUTPUT_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_output_stream_query_info_async:
194 * @stream: a #GFileOutputStream.
195 * @attributes: a file attribute query string.
196 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
198 * @cancellable: optional #GCancellable object, %NULL to ignore.
199 * @callback: callback to call when the request is satisfied
200 * @user_data: 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_output_stream_query_info_finish().
206 * For the synchronous version of this function, see
207 * g_file_output_stream_query_info().
211 g_file_output_stream_query_info_async (GFileOutputStream *stream,
212 const char *attributes,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
218 GFileOutputStreamClass *klass;
219 GOutputStream *output_stream;
220 GError *error = NULL;
222 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
224 output_stream = G_OUTPUT_STREAM (stream);
226 if (!g_output_stream_set_pending (output_stream, &error))
228 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
232 g_error_free (error);
236 klass = G_FILE_OUTPUT_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_output_stream_query_info_finish:
246 * @stream: a #GFileOutputStream.
247 * @result: a #GAsyncResult.
248 * @error: a #GError, %NULL to ignore.
250 * Finalizes the asynchronous query started
251 * by g_file_output_stream_query_info_async().
253 * Returns: (transfer full): A #GFileInfo for the finished query.
256 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
257 GAsyncResult *result,
260 GSimpleAsyncResult *simple;
261 GFileOutputStreamClass *class;
263 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
264 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
266 if (G_IS_SIMPLE_ASYNC_RESULT (result))
268 simple = G_SIMPLE_ASYNC_RESULT (result);
269 if (g_simple_async_result_propagate_error (simple, error))
273 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
274 return class->query_info_finish (stream, result, error);
278 * g_file_output_stream_get_etag:
279 * @stream: a #GFileOutputStream.
281 * Gets the entity tag for the file when it has been written.
282 * This must be called after the stream has been written
283 * and closed, as the etag can change while writing.
285 * Returns: the entity tag for the stream.
288 g_file_output_stream_get_etag (GFileOutputStream *stream)
290 GFileOutputStreamClass *class;
291 GOutputStream *output_stream;
294 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
296 output_stream = G_OUTPUT_STREAM (stream);
298 if (!g_output_stream_is_closed (output_stream))
300 g_warning ("stream is not closed yet, can't get etag");
306 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
308 etag = class->get_etag (stream);
314 g_file_output_stream_tell (GFileOutputStream *stream)
316 GFileOutputStreamClass *class;
319 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
321 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
325 offset = class->tell (stream);
331 g_file_output_stream_seekable_tell (GSeekable *seekable)
333 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
337 g_file_output_stream_can_seek (GFileOutputStream *stream)
339 GFileOutputStreamClass *class;
342 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
344 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
351 can_seek = class->can_seek (stream);
358 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
360 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
364 g_file_output_stream_seek (GFileOutputStream *stream,
367 GCancellable *cancellable,
370 GFileOutputStreamClass *class;
371 GOutputStream *output_stream;
374 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
376 output_stream = G_OUTPUT_STREAM (stream);
377 class = G_FILE_OUTPUT_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_output_stream_set_pending (output_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_output_stream_clear_pending (output_stream);
403 g_file_output_stream_seekable_seek (GSeekable *seekable,
406 GCancellable *cancellable,
409 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
410 offset, type, cancellable, error);
414 g_file_output_stream_can_truncate (GFileOutputStream *stream)
416 GFileOutputStreamClass *class;
417 gboolean can_truncate;
419 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
421 class = G_FILE_OUTPUT_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_output_stream_seekable_can_truncate (GSeekable *seekable)
437 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
441 g_file_output_stream_truncate (GFileOutputStream *stream,
443 GCancellable *cancellable,
446 GFileOutputStreamClass *class;
447 GOutputStream *output_stream;
450 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
452 output_stream = G_OUTPUT_STREAM (stream);
453 class = G_FILE_OUTPUT_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_output_stream_set_pending (output_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_output_stream_clear_pending (output_stream);
479 g_file_output_stream_seekable_truncate (GSeekable *seekable,
481 GCancellable *cancellable,
484 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
485 size, cancellable, error);
487 /********************************************
488 * Default implementation of async ops *
489 ********************************************/
494 } QueryInfoAsyncData;
497 query_info_data_free (QueryInfoAsyncData *data)
500 g_object_unref (data->info);
501 g_free (data->attributes);
506 query_info_async_thread (GSimpleAsyncResult *res,
508 GCancellable *cancellable)
510 GFileOutputStreamClass *class;
511 GError *error = NULL;
512 QueryInfoAsyncData *data;
515 data = g_simple_async_result_get_op_res_gpointer (res);
519 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
520 if (class->query_info)
521 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
523 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
524 _("Stream doesn't support query_info"));
528 g_simple_async_result_set_from_error (res, error);
529 g_error_free (error);
536 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
537 const char *attributes,
539 GCancellable *cancellable,
540 GAsyncReadyCallback callback,
543 GSimpleAsyncResult *res;
544 QueryInfoAsyncData *data;
546 data = g_new0 (QueryInfoAsyncData, 1);
547 data->attributes = g_strdup (attributes);
549 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
550 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
552 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
553 g_object_unref (res);
557 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
561 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
562 QueryInfoAsyncData *data;
564 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
566 data = g_simple_async_result_get_op_res_gpointer (simple);
568 return g_object_ref (data->info);