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 the output
43 * stream to jump to arbitrary positions in the file and to truncate
44 * the file, provided the filesystem of the file supports these
45 * operations. In addition to the generic g_seekable_ API,
46 * GFileOutputStream has its own API for seeking and positioning.
47 * To find the position of a file output stream, use
48 * g_file_output_stream_tell(). To find out if a file output
49 * stream supports seeking, use g_file_output_stream_can_seek().
50 * To position a file output stream, use g_file_output_stream_seek().
51 * To find out if a file output stream supports truncating, use
52 * g_file_output_stream_can_truncate(). To truncate a file output
53 * stream, use g_file_output_stream_truncate().
56 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
57 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
58 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
59 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
62 GCancellable *cancellable,
64 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
65 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
67 GCancellable *cancellable,
69 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
72 GCancellable *cancellable,
73 GAsyncReadyCallback callback,
75 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
79 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
80 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
81 g_file_output_stream_seekable_iface_init));
83 struct _GFileOutputStreamPrivate {
84 GAsyncReadyCallback outstanding_callback;
88 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
90 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
92 klass->query_info_async = g_file_output_stream_real_query_info_async;
93 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
97 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
99 iface->tell = g_file_output_stream_seekable_tell;
100 iface->can_seek = g_file_output_stream_seekable_can_seek;
101 iface->seek = g_file_output_stream_seekable_seek;
102 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
103 iface->truncate_fn = g_file_output_stream_seekable_truncate;
107 g_file_output_stream_init (GFileOutputStream *stream)
109 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
110 G_TYPE_FILE_OUTPUT_STREAM,
111 GFileOutputStreamPrivate);
115 * g_file_output_stream_query_info:
116 * @stream: a #GFileOutputStream.
117 * @attributes: a file attribute query string.
118 * @cancellable: optional #GCancellable object, %NULL to ignore.
119 * @error: a #GError, %NULL to ignore.
121 * Queries a file output stream for the given @attributes.
122 * This function blocks while querying the stream. For the asynchronous
123 * version of this function, see g_file_output_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). In
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
139 * Returns: a #GFileInfo for the @stream, or %NULL on error.
142 g_file_output_stream_query_info (GFileOutputStream *stream,
144 GCancellable *cancellable,
147 GFileOutputStreamClass *class;
148 GOutputStream *output_stream;
151 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
153 output_stream = G_OUTPUT_STREAM (stream);
155 if (!g_output_stream_set_pending (output_stream, error))
161 g_cancellable_push_current (cancellable);
163 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
164 if (class->query_info)
165 info = class->query_info (stream, attributes, cancellable, error);
167 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
168 _("Stream doesn't support query_info"));
171 g_cancellable_pop_current (cancellable);
173 g_output_stream_clear_pending (output_stream);
179 async_ready_callback_wrapper (GObject *source_object,
183 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
185 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
186 if (stream->priv->outstanding_callback)
187 (*stream->priv->outstanding_callback) (source_object, res, user_data);
188 g_object_unref (stream);
192 * g_file_output_stream_query_info_async:
193 * @stream: a #GFileOutputStream.
194 * @attributes: a file attribute query string.
195 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
197 * @cancellable: optional #GCancellable object, %NULL to ignore.
198 * @callback: callback to call when the request is satisfied
199 * @user_data: the data to pass to callback function
201 * Asynchronously queries the @stream for a #GFileInfo. When completed,
202 * @callback will be called with a #GAsyncResult which can be used to
203 * finish the operation with g_file_output_stream_query_info_finish().
205 * For the synchronous version of this function, see
206 * g_file_output_stream_query_info().
210 g_file_output_stream_query_info_async (GFileOutputStream *stream,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
217 GFileOutputStreamClass *klass;
218 GOutputStream *output_stream;
219 GError *error = NULL;
221 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
223 output_stream = G_OUTPUT_STREAM (stream);
225 if (!g_output_stream_set_pending (output_stream, &error))
227 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
231 g_error_free (error);
235 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
237 stream->priv->outstanding_callback = callback;
238 g_object_ref (stream);
239 klass->query_info_async (stream, attributes, io_priority, cancellable,
240 async_ready_callback_wrapper, user_data);
244 * g_file_output_stream_query_info_finish:
245 * @stream: a #GFileOutputStream.
246 * @result: a #GAsyncResult.
247 * @error: a #GError, %NULL to ignore.
249 * Finalizes the asynchronous query started
250 * by g_file_output_stream_query_info_async().
252 * Returns: A #GFileInfo for the finished query.
255 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
256 GAsyncResult *result,
259 GSimpleAsyncResult *simple;
260 GFileOutputStreamClass *class;
262 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
263 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
265 if (G_IS_SIMPLE_ASYNC_RESULT (result))
267 simple = G_SIMPLE_ASYNC_RESULT (result);
268 if (g_simple_async_result_propagate_error (simple, error))
272 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
273 return class->query_info_finish (stream, result, error);
277 * g_file_output_stream_get_etag:
278 * @stream: a #GFileOutputStream.
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.
287 g_file_output_stream_get_etag (GFileOutputStream *stream)
289 GFileOutputStreamClass *class;
290 GOutputStream *output_stream;
293 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
295 output_stream = G_OUTPUT_STREAM (stream);
297 if (!g_output_stream_is_closed (output_stream))
299 g_warning ("stream is not closed yet, can't get etag");
305 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
307 etag = class->get_etag (stream);
313 g_file_output_stream_tell (GFileOutputStream *stream)
315 GFileOutputStreamClass *class;
318 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
320 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
324 offset = class->tell (stream);
330 g_file_output_stream_seekable_tell (GSeekable *seekable)
332 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
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 (GFileOutputStream *stream,
366 GCancellable *cancellable,
369 GFileOutputStreamClass *class;
370 GOutputStream *output_stream;
373 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
375 output_stream = G_OUTPUT_STREAM (stream);
376 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
380 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
381 _("Seek not supported on stream"));
385 if (!g_output_stream_set_pending (output_stream, error))
389 g_cancellable_push_current (cancellable);
391 res = class->seek (stream, offset, type, cancellable, error);
394 g_cancellable_pop_current (cancellable);
396 g_output_stream_clear_pending (output_stream);
402 g_file_output_stream_seekable_seek (GSeekable *seekable,
405 GCancellable *cancellable,
408 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
409 offset, type, cancellable, error);
413 g_file_output_stream_can_truncate (GFileOutputStream *stream)
415 GFileOutputStreamClass *class;
416 gboolean can_truncate;
418 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
420 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
422 can_truncate = FALSE;
423 if (class->truncate_fn)
426 if (class->can_truncate)
427 can_truncate = class->can_truncate (stream);
434 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
436 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
440 g_file_output_stream_truncate (GFileOutputStream *stream,
442 GCancellable *cancellable,
445 GFileOutputStreamClass *class;
446 GOutputStream *output_stream;
449 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
451 output_stream = G_OUTPUT_STREAM (stream);
452 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
454 if (!class->truncate_fn)
456 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
457 _("Truncate not supported on stream"));
461 if (!g_output_stream_set_pending (output_stream, error))
465 g_cancellable_push_current (cancellable);
467 res = class->truncate_fn (stream, size, cancellable, error);
470 g_cancellable_pop_current (cancellable);
472 g_output_stream_clear_pending (output_stream);
478 g_file_output_stream_seekable_truncate (GSeekable *seekable,
480 GCancellable *cancellable,
483 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
484 size, cancellable, error);
486 /********************************************
487 * Default implementation of async ops *
488 ********************************************/
493 } QueryInfoAsyncData;
496 query_info_data_free (QueryInfoAsyncData *data)
499 g_object_unref (data->info);
500 g_free (data->attributes);
505 query_info_async_thread (GSimpleAsyncResult *res,
507 GCancellable *cancellable)
509 GFileOutputStreamClass *class;
510 GError *error = NULL;
511 QueryInfoAsyncData *data;
514 data = g_simple_async_result_get_op_res_gpointer (res);
518 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
519 if (class->query_info)
520 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
522 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
523 _("Stream doesn't support query_info"));
527 g_simple_async_result_set_from_error (res, error);
528 g_error_free (error);
535 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
538 GCancellable *cancellable,
539 GAsyncReadyCallback callback,
542 GSimpleAsyncResult *res;
543 QueryInfoAsyncData *data;
545 data = g_new0 (QueryInfoAsyncData, 1);
546 data->attributes = g_strdup (attributes);
548 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
549 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
551 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
552 g_object_unref (res);
556 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
560 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
561 QueryInfoAsyncData *data;
563 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
565 data = g_simple_async_result_get_op_res_gpointer (simple);
567 return g_object_ref (data->info);
572 #define __G_FILE_OUTPUT_STREAM_C__
573 #include "gioaliasdef.c"