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"
37 * SECTION:gfileoutputstream
38 * @short_description: File output streaming operations
40 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
42 * GFileOutputStream provides output streams that write their
45 * GFileOutputStream implements #GSeekable, which allows the output
46 * stream to jump to arbitrary positions in the file and to truncate
47 * the file, provided the filesystem of the file supports these
50 * To find the position of a file output stream, use g_seekable_tell().
51 * To find out if a file output stream supports seeking, use
52 * g_seekable_can_seek().To position a file output stream, use
53 * g_seekable_seek(). To find out if a file output stream supports
54 * truncating, use g_seekable_can_truncate(). To truncate a file output
55 * stream, use g_seekable_truncate().
58 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
59 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
60 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
61 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
64 GCancellable *cancellable,
66 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
67 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
69 GCancellable *cancellable,
71 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
72 const char *attributes,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
81 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
82 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
83 g_file_output_stream_seekable_iface_init));
85 struct _GFileOutputStreamPrivate {
86 GAsyncReadyCallback outstanding_callback;
90 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
92 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
94 klass->query_info_async = g_file_output_stream_real_query_info_async;
95 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
99 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
101 iface->tell = g_file_output_stream_seekable_tell;
102 iface->can_seek = g_file_output_stream_seekable_can_seek;
103 iface->seek = g_file_output_stream_seekable_seek;
104 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
105 iface->truncate_fn = g_file_output_stream_seekable_truncate;
109 g_file_output_stream_init (GFileOutputStream *stream)
111 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
112 G_TYPE_FILE_OUTPUT_STREAM,
113 GFileOutputStreamPrivate);
117 * g_file_output_stream_query_info:
118 * @stream: a #GFileOutputStream.
119 * @attributes: a file attribute query string.
120 * @cancellable: optional #GCancellable object, %NULL to ignore.
121 * @error: a #GError, %NULL to ignore.
123 * Queries a file output stream for the given @attributes.
124 * This function blocks while querying the stream. For the asynchronous
125 * version of this function, see g_file_output_stream_query_info_async().
126 * While the stream is blocked, the stream will set the pending flag
127 * internally, and any other operations on the stream will fail with
128 * %G_IO_ERROR_PENDING.
130 * Can fail if the stream was already closed (with @error being set to
131 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
132 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
133 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
134 * all cases of failure, %NULL will be returned.
136 * If @cancellable is not %NULL, then the operation can be cancelled by
137 * triggering the cancellable object from another thread. If the operation
138 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
141 * Returns: a #GFileInfo for the @stream, or %NULL on error.
144 g_file_output_stream_query_info (GFileOutputStream *stream,
145 const char *attributes,
146 GCancellable *cancellable,
149 GFileOutputStreamClass *class;
150 GOutputStream *output_stream;
153 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
155 output_stream = G_OUTPUT_STREAM (stream);
157 if (!g_output_stream_set_pending (output_stream, error))
163 g_cancellable_push_current (cancellable);
165 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
166 if (class->query_info)
167 info = class->query_info (stream, attributes, cancellable, error);
169 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
170 _("Stream doesn't support query_info"));
173 g_cancellable_pop_current (cancellable);
175 g_output_stream_clear_pending (output_stream);
181 async_ready_callback_wrapper (GObject *source_object,
185 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
187 g_output_stream_clear_pending (G_OUTPUT_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_output_stream_query_info_async:
195 * @stream: a #GFileOutputStream.
196 * @attributes: a file attribute query string.
197 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
199 * @cancellable: optional #GCancellable object, %NULL to ignore.
200 * @callback: callback to call when the request is satisfied
201 * @user_data: 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_output_stream_query_info_finish().
207 * For the synchronous version of this function, see
208 * g_file_output_stream_query_info().
212 g_file_output_stream_query_info_async (GFileOutputStream *stream,
213 const char *attributes,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
219 GFileOutputStreamClass *klass;
220 GOutputStream *output_stream;
221 GError *error = NULL;
223 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
225 output_stream = G_OUTPUT_STREAM (stream);
227 if (!g_output_stream_set_pending (output_stream, &error))
229 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
233 g_error_free (error);
237 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
239 stream->priv->outstanding_callback = callback;
240 g_object_ref (stream);
241 klass->query_info_async (stream, attributes, io_priority, cancellable,
242 async_ready_callback_wrapper, user_data);
246 * g_file_output_stream_query_info_finish:
247 * @stream: a #GFileOutputStream.
248 * @result: a #GAsyncResult.
249 * @error: a #GError, %NULL to ignore.
251 * Finalizes the asynchronous query started
252 * by g_file_output_stream_query_info_async().
254 * Returns: A #GFileInfo for the finished query.
257 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
258 GAsyncResult *result,
261 GSimpleAsyncResult *simple;
262 GFileOutputStreamClass *class;
264 g_return_val_if_fail (G_IS_FILE_OUTPUT_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_OUTPUT_STREAM_GET_CLASS (stream);
275 return class->query_info_finish (stream, result, error);
279 * g_file_output_stream_get_etag:
280 * @stream: a #GFileOutputStream.
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.
289 g_file_output_stream_get_etag (GFileOutputStream *stream)
291 GFileOutputStreamClass *class;
292 GOutputStream *output_stream;
295 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
297 output_stream = G_OUTPUT_STREAM (stream);
299 if (!g_output_stream_is_closed (output_stream))
301 g_warning ("stream is not closed yet, can't get etag");
307 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
309 etag = class->get_etag (stream);
315 g_file_output_stream_tell (GFileOutputStream *stream)
317 GFileOutputStreamClass *class;
320 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
322 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
326 offset = class->tell (stream);
332 g_file_output_stream_seekable_tell (GSeekable *seekable)
334 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
338 g_file_output_stream_can_seek (GFileOutputStream *stream)
340 GFileOutputStreamClass *class;
343 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
345 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
352 can_seek = class->can_seek (stream);
359 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
361 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
365 g_file_output_stream_seek (GFileOutputStream *stream,
368 GCancellable *cancellable,
371 GFileOutputStreamClass *class;
372 GOutputStream *output_stream;
375 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
377 output_stream = G_OUTPUT_STREAM (stream);
378 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
382 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
383 _("Seek not supported on stream"));
387 if (!g_output_stream_set_pending (output_stream, error))
391 g_cancellable_push_current (cancellable);
393 res = class->seek (stream, offset, type, cancellable, error);
396 g_cancellable_pop_current (cancellable);
398 g_output_stream_clear_pending (output_stream);
404 g_file_output_stream_seekable_seek (GSeekable *seekable,
407 GCancellable *cancellable,
410 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
411 offset, type, cancellable, error);
415 g_file_output_stream_can_truncate (GFileOutputStream *stream)
417 GFileOutputStreamClass *class;
418 gboolean can_truncate;
420 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
422 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
424 can_truncate = FALSE;
425 if (class->truncate_fn)
428 if (class->can_truncate)
429 can_truncate = class->can_truncate (stream);
436 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
438 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
442 g_file_output_stream_truncate (GFileOutputStream *stream,
444 GCancellable *cancellable,
447 GFileOutputStreamClass *class;
448 GOutputStream *output_stream;
451 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
453 output_stream = G_OUTPUT_STREAM (stream);
454 class = G_FILE_OUTPUT_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"));
463 if (!g_output_stream_set_pending (output_stream, error))
467 g_cancellable_push_current (cancellable);
469 res = class->truncate_fn (stream, size, cancellable, error);
472 g_cancellable_pop_current (cancellable);
474 g_output_stream_clear_pending (output_stream);
480 g_file_output_stream_seekable_truncate (GSeekable *seekable,
482 GCancellable *cancellable,
485 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
486 size, cancellable, error);
488 /********************************************
489 * Default implementation of async ops *
490 ********************************************/
495 } QueryInfoAsyncData;
498 query_info_data_free (QueryInfoAsyncData *data)
501 g_object_unref (data->info);
502 g_free (data->attributes);
507 query_info_async_thread (GSimpleAsyncResult *res,
509 GCancellable *cancellable)
511 GFileOutputStreamClass *class;
512 GError *error = NULL;
513 QueryInfoAsyncData *data;
516 data = g_simple_async_result_get_op_res_gpointer (res);
520 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
521 if (class->query_info)
522 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
524 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
525 _("Stream doesn't support query_info"));
529 g_simple_async_result_set_from_error (res, error);
530 g_error_free (error);
537 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
538 const char *attributes,
540 GCancellable *cancellable,
541 GAsyncReadyCallback callback,
544 GSimpleAsyncResult *res;
545 QueryInfoAsyncData *data;
547 data = g_new0 (QueryInfoAsyncData, 1);
548 data->attributes = g_strdup (attributes);
550 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
551 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
553 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
554 g_object_unref (res);
558 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
562 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
563 QueryInfoAsyncData *data;
565 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
567 data = g_simple_async_result_get_op_res_gpointer (simple);
569 return g_object_ref (data->info);
574 #define __G_FILE_OUTPUT_STREAM_C__
575 #include "gioaliasdef.c"