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
48 * operations. In addition to the generic g_seekable_ API,
49 * GFileOutputStream has its own API for seeking and positioning.
50 * To find the position of a file output stream, use
51 * g_file_output_stream_tell(). To find out if a file output
52 * stream supports seeking, use g_file_output_stream_can_seek().
53 * To position a file output stream, use g_file_output_stream_seek().
54 * To find out if a file output stream supports truncating, use
55 * g_file_output_stream_can_truncate(). To truncate a file output
56 * stream, use g_file_output_stream_truncate().
59 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
60 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
61 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
62 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
65 GCancellable *cancellable,
67 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
68 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
70 GCancellable *cancellable,
72 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
78 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
82 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
83 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
84 g_file_output_stream_seekable_iface_init));
86 struct _GFileOutputStreamPrivate {
87 GAsyncReadyCallback outstanding_callback;
91 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
93 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
95 klass->query_info_async = g_file_output_stream_real_query_info_async;
96 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
100 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
102 iface->tell = g_file_output_stream_seekable_tell;
103 iface->can_seek = g_file_output_stream_seekable_can_seek;
104 iface->seek = g_file_output_stream_seekable_seek;
105 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
106 iface->truncate_fn = g_file_output_stream_seekable_truncate;
110 g_file_output_stream_init (GFileOutputStream *stream)
112 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
113 G_TYPE_FILE_OUTPUT_STREAM,
114 GFileOutputStreamPrivate);
118 * g_file_output_stream_query_info:
119 * @stream: a #GFileOutputStream.
120 * @attributes: a file attribute query string.
121 * @cancellable: optional #GCancellable object, %NULL to ignore.
122 * @error: a #GError, %NULL to ignore.
124 * Queries a file output stream for the given @attributes.
125 * This function blocks while querying the stream. For the asynchronous
126 * version of this function, see g_file_output_stream_query_info_async().
127 * While the stream is blocked, the stream will set the pending flag
128 * internally, and any other operations on the stream will fail with
129 * %G_IO_ERROR_PENDING.
131 * Can fail if the stream was already closed (with @error being set to
132 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
133 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
134 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
135 * all cases of failure, %NULL will be returned.
137 * If @cancellable is not %NULL, then the operation can be cancelled by
138 * triggering the cancellable object from another thread. If the operation
139 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
142 * Returns: a #GFileInfo for the @stream, or %NULL on error.
145 g_file_output_stream_query_info (GFileOutputStream *stream,
147 GCancellable *cancellable,
150 GFileOutputStreamClass *class;
151 GOutputStream *output_stream;
154 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
156 output_stream = G_OUTPUT_STREAM (stream);
158 if (!g_output_stream_set_pending (output_stream, error))
164 g_cancellable_push_current (cancellable);
166 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
167 if (class->query_info)
168 info = class->query_info (stream, attributes, cancellable, error);
170 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
171 _("Stream doesn't support query_info"));
174 g_cancellable_pop_current (cancellable);
176 g_output_stream_clear_pending (output_stream);
182 async_ready_callback_wrapper (GObject *source_object,
186 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
188 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
189 if (stream->priv->outstanding_callback)
190 (*stream->priv->outstanding_callback) (source_object, res, user_data);
191 g_object_unref (stream);
195 * g_file_output_stream_query_info_async:
196 * @stream: a #GFileOutputStream.
197 * @attributes: a file attribute query string.
198 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
200 * @cancellable: optional #GCancellable object, %NULL to ignore.
201 * @callback: callback to call when the request is satisfied
202 * @user_data: the data to pass to callback function
204 * Asynchronously queries the @stream for a #GFileInfo. When completed,
205 * @callback will be called with a #GAsyncResult which can be used to
206 * finish the operation with g_file_output_stream_query_info_finish().
208 * For the synchronous version of this function, see
209 * g_file_output_stream_query_info().
213 g_file_output_stream_query_info_async (GFileOutputStream *stream,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
220 GFileOutputStreamClass *klass;
221 GOutputStream *output_stream;
222 GError *error = NULL;
224 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
226 output_stream = G_OUTPUT_STREAM (stream);
228 if (!g_output_stream_set_pending (output_stream, &error))
230 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
234 g_error_free (error);
238 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
240 stream->priv->outstanding_callback = callback;
241 g_object_ref (stream);
242 klass->query_info_async (stream, attributes, io_priority, cancellable,
243 async_ready_callback_wrapper, user_data);
247 * g_file_output_stream_query_info_finish:
248 * @stream: a #GFileOutputStream.
249 * @result: a #GAsyncResult.
250 * @error: a #GError, %NULL to ignore.
252 * Finalizes the asynchronous query started
253 * by g_file_output_stream_query_info_async().
255 * Returns: A #GFileInfo for the finished query.
258 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
259 GAsyncResult *result,
262 GSimpleAsyncResult *simple;
263 GFileOutputStreamClass *class;
265 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
266 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
268 if (G_IS_SIMPLE_ASYNC_RESULT (result))
270 simple = G_SIMPLE_ASYNC_RESULT (result);
271 if (g_simple_async_result_propagate_error (simple, error))
275 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
276 return class->query_info_finish (stream, result, error);
280 * g_file_output_stream_get_etag:
281 * @stream: a #GFileOutputStream.
283 * Gets the entity tag for the file when it has been written.
284 * This must be called after the stream has been written
285 * and closed, as the etag can change while writing.
287 * Returns: the entity tag for the stream.
290 g_file_output_stream_get_etag (GFileOutputStream *stream)
292 GFileOutputStreamClass *class;
293 GOutputStream *output_stream;
296 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
298 output_stream = G_OUTPUT_STREAM (stream);
300 if (!g_output_stream_is_closed (output_stream))
302 g_warning ("stream is not closed yet, can't get etag");
308 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
310 etag = class->get_etag (stream);
316 g_file_output_stream_tell (GFileOutputStream *stream)
318 GFileOutputStreamClass *class;
321 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
323 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
327 offset = class->tell (stream);
333 g_file_output_stream_seekable_tell (GSeekable *seekable)
335 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
339 g_file_output_stream_can_seek (GFileOutputStream *stream)
341 GFileOutputStreamClass *class;
344 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
346 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
353 can_seek = class->can_seek (stream);
360 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
362 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
366 g_file_output_stream_seek (GFileOutputStream *stream,
369 GCancellable *cancellable,
372 GFileOutputStreamClass *class;
373 GOutputStream *output_stream;
376 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
378 output_stream = G_OUTPUT_STREAM (stream);
379 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
383 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
384 _("Seek not supported on stream"));
388 if (!g_output_stream_set_pending (output_stream, error))
392 g_cancellable_push_current (cancellable);
394 res = class->seek (stream, offset, type, cancellable, error);
397 g_cancellable_pop_current (cancellable);
399 g_output_stream_clear_pending (output_stream);
405 g_file_output_stream_seekable_seek (GSeekable *seekable,
408 GCancellable *cancellable,
411 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
412 offset, type, cancellable, error);
416 g_file_output_stream_can_truncate (GFileOutputStream *stream)
418 GFileOutputStreamClass *class;
419 gboolean can_truncate;
421 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
423 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
425 can_truncate = FALSE;
426 if (class->truncate_fn)
429 if (class->can_truncate)
430 can_truncate = class->can_truncate (stream);
437 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
439 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
443 g_file_output_stream_truncate (GFileOutputStream *stream,
445 GCancellable *cancellable,
448 GFileOutputStreamClass *class;
449 GOutputStream *output_stream;
452 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
454 output_stream = G_OUTPUT_STREAM (stream);
455 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
457 if (!class->truncate_fn)
459 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
460 _("Truncate not supported on stream"));
464 if (!g_output_stream_set_pending (output_stream, error))
468 g_cancellable_push_current (cancellable);
470 res = class->truncate_fn (stream, size, cancellable, error);
473 g_cancellable_pop_current (cancellable);
475 g_output_stream_clear_pending (output_stream);
481 g_file_output_stream_seekable_truncate (GSeekable *seekable,
483 GCancellable *cancellable,
486 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
487 size, cancellable, error);
489 /********************************************
490 * Default implementation of async ops *
491 ********************************************/
496 } QueryInfoAsyncData;
499 query_info_data_free (QueryInfoAsyncData *data)
502 g_object_unref (data->info);
503 g_free (data->attributes);
508 query_info_async_thread (GSimpleAsyncResult *res,
510 GCancellable *cancellable)
512 GFileOutputStreamClass *class;
513 GError *error = NULL;
514 QueryInfoAsyncData *data;
517 data = g_simple_async_result_get_op_res_gpointer (res);
521 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
522 if (class->query_info)
523 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
525 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
526 _("Stream doesn't support query_info"));
530 g_simple_async_result_set_from_error (res, error);
531 g_error_free (error);
538 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
541 GCancellable *cancellable,
542 GAsyncReadyCallback callback,
545 GSimpleAsyncResult *res;
546 QueryInfoAsyncData *data;
548 data = g_new0 (QueryInfoAsyncData, 1);
549 data->attributes = g_strdup (attributes);
551 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
552 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
554 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
555 g_object_unref (res);
559 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
563 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
564 QueryInfoAsyncData *data;
566 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
568 data = g_simple_async_result_get_op_res_gpointer (simple);
570 return g_object_ref (data->info);
575 #define __G_FILE_OUTPUT_STREAM_C__
576 #include "gioaliasdef.c"