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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include <gfileoutputstream.h>
25 #include <gseekable.h>
26 #include "gasyncresult.h"
28 #include "gcancellable.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
47 * To find the position of a file output stream, use g_seekable_tell().
48 * To find out if a file output stream supports seeking, use
49 * g_seekable_can_seek().To position a file output stream, use
50 * g_seekable_seek(). To find out if a file output stream supports
51 * truncating, use g_seekable_can_truncate(). To truncate a file output
52 * stream, use g_seekable_truncate().
55 static void g_file_output_stream_seekable_iface_init (GSeekableIface
*iface
);
56 static goffset
g_file_output_stream_seekable_tell (GSeekable
*seekable
);
57 static gboolean
g_file_output_stream_seekable_can_seek (GSeekable
*seekable
);
58 static gboolean
g_file_output_stream_seekable_seek (GSeekable
*seekable
,
61 GCancellable
*cancellable
,
63 static gboolean
g_file_output_stream_seekable_can_truncate (GSeekable
*seekable
);
64 static gboolean
g_file_output_stream_seekable_truncate (GSeekable
*seekable
,
66 GCancellable
*cancellable
,
68 static void g_file_output_stream_real_query_info_async (GFileOutputStream
*stream
,
69 const char *attributes
,
71 GCancellable
*cancellable
,
72 GAsyncReadyCallback callback
,
74 static GFileInfo
*g_file_output_stream_real_query_info_finish (GFileOutputStream
*stream
,
78 struct _GFileOutputStreamPrivate
{
79 GAsyncReadyCallback outstanding_callback
;
82 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream
, g_file_output_stream
, G_TYPE_OUTPUT_STREAM
,
83 G_ADD_PRIVATE (GFileOutputStream
)
84 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE
,
85 g_file_output_stream_seekable_iface_init
));
88 g_file_output_stream_class_init (GFileOutputStreamClass
*klass
)
90 klass
->query_info_async
= g_file_output_stream_real_query_info_async
;
91 klass
->query_info_finish
= g_file_output_stream_real_query_info_finish
;
95 g_file_output_stream_seekable_iface_init (GSeekableIface
*iface
)
97 iface
->tell
= g_file_output_stream_seekable_tell
;
98 iface
->can_seek
= g_file_output_stream_seekable_can_seek
;
99 iface
->seek
= g_file_output_stream_seekable_seek
;
100 iface
->can_truncate
= g_file_output_stream_seekable_can_truncate
;
101 iface
->truncate_fn
= g_file_output_stream_seekable_truncate
;
105 g_file_output_stream_init (GFileOutputStream
*stream
)
107 stream
->priv
= g_file_output_stream_get_instance_private (stream
);
111 * g_file_output_stream_query_info:
112 * @stream: a #GFileOutputStream.
113 * @attributes: a file attribute query string.
114 * @cancellable: optional #GCancellable object, %NULL to ignore.
115 * @error: a #GError, %NULL to ignore.
117 * Queries a file output stream for the given @attributes.
118 * This function blocks while querying the stream. For the asynchronous
119 * version of this function, see g_file_output_stream_query_info_async().
120 * While the stream is blocked, the stream will set the pending flag
121 * internally, and any other operations on the stream will fail with
122 * %G_IO_ERROR_PENDING.
124 * Can fail if the stream was already closed (with @error being set to
125 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
126 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
127 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
128 * all cases of failure, %NULL will be returned.
130 * If @cancellable is not %NULL, then the operation can be cancelled by
131 * triggering the cancellable object from another thread. If the operation
132 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
135 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
138 g_file_output_stream_query_info (GFileOutputStream
*stream
,
139 const char *attributes
,
140 GCancellable
*cancellable
,
143 GFileOutputStreamClass
*class;
144 GOutputStream
*output_stream
;
147 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), NULL
);
149 output_stream
= G_OUTPUT_STREAM (stream
);
151 if (!g_output_stream_set_pending (output_stream
, error
))
157 g_cancellable_push_current (cancellable
);
159 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
160 if (class->query_info
)
161 info
= class->query_info (stream
, attributes
, cancellable
, error
);
163 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
164 _("Stream doesn’t support query_info"));
167 g_cancellable_pop_current (cancellable
);
169 g_output_stream_clear_pending (output_stream
);
175 async_ready_callback_wrapper (GObject
*source_object
,
179 GFileOutputStream
*stream
= G_FILE_OUTPUT_STREAM (source_object
);
181 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream
));
182 if (stream
->priv
->outstanding_callback
)
183 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
184 g_object_unref (stream
);
188 * g_file_output_stream_query_info_async:
189 * @stream: a #GFileOutputStream.
190 * @attributes: a file attribute query string.
191 * @io_priority: the [I/O priority][gio-GIOScheduler] of the request
192 * @cancellable: optional #GCancellable object, %NULL to ignore.
193 * @callback: callback to call when the request is satisfied
194 * @user_data: the data to pass to callback function
196 * Asynchronously queries the @stream for a #GFileInfo. When completed,
197 * @callback will be called with a #GAsyncResult which can be used to
198 * finish the operation with g_file_output_stream_query_info_finish().
200 * For the synchronous version of this function, see
201 * g_file_output_stream_query_info().
205 g_file_output_stream_query_info_async (GFileOutputStream
*stream
,
206 const char *attributes
,
208 GCancellable
*cancellable
,
209 GAsyncReadyCallback callback
,
212 GFileOutputStreamClass
*klass
;
213 GOutputStream
*output_stream
;
214 GError
*error
= NULL
;
216 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
));
218 output_stream
= G_OUTPUT_STREAM (stream
);
220 if (!g_output_stream_set_pending (output_stream
, &error
))
222 g_task_report_error (stream
, callback
, user_data
,
223 g_file_output_stream_query_info_async
,
228 klass
= G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
230 stream
->priv
->outstanding_callback
= callback
;
231 g_object_ref (stream
);
232 klass
->query_info_async (stream
, attributes
, io_priority
, cancellable
,
233 async_ready_callback_wrapper
, user_data
);
237 * g_file_output_stream_query_info_finish:
238 * @stream: a #GFileOutputStream.
239 * @result: a #GAsyncResult.
240 * @error: a #GError, %NULL to ignore.
242 * Finalizes the asynchronous query started
243 * by g_file_output_stream_query_info_async().
245 * Returns: (transfer full): A #GFileInfo for the finished query.
248 g_file_output_stream_query_info_finish (GFileOutputStream
*stream
,
249 GAsyncResult
*result
,
252 GFileOutputStreamClass
*class;
254 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), NULL
);
255 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), NULL
);
257 if (g_async_result_legacy_propagate_error (result
, error
))
259 else if (g_async_result_is_tagged (result
, g_file_output_stream_query_info_async
))
260 return g_task_propagate_pointer (G_TASK (result
), error
);
262 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
263 return class->query_info_finish (stream
, result
, error
);
267 * g_file_output_stream_get_etag:
268 * @stream: a #GFileOutputStream.
270 * Gets the entity tag for the file when it has been written.
271 * This must be called after the stream has been written
272 * and closed, as the etag can change while writing.
274 * Returns: the entity tag for the stream.
277 g_file_output_stream_get_etag (GFileOutputStream
*stream
)
279 GFileOutputStreamClass
*class;
280 GOutputStream
*output_stream
;
283 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), NULL
);
285 output_stream
= G_OUTPUT_STREAM (stream
);
287 if (!g_output_stream_is_closed (output_stream
))
289 g_warning ("stream is not closed yet, can't get etag");
295 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
297 etag
= class->get_etag (stream
);
303 g_file_output_stream_tell (GFileOutputStream
*stream
)
305 GFileOutputStreamClass
*class;
308 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), 0);
310 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
314 offset
= class->tell (stream
);
320 g_file_output_stream_seekable_tell (GSeekable
*seekable
)
322 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable
));
326 g_file_output_stream_can_seek (GFileOutputStream
*stream
)
328 GFileOutputStreamClass
*class;
331 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), FALSE
);
333 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
340 can_seek
= class->can_seek (stream
);
347 g_file_output_stream_seekable_can_seek (GSeekable
*seekable
)
349 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable
));
353 g_file_output_stream_seek (GFileOutputStream
*stream
,
356 GCancellable
*cancellable
,
359 GFileOutputStreamClass
*class;
360 GOutputStream
*output_stream
;
363 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), FALSE
);
365 output_stream
= G_OUTPUT_STREAM (stream
);
366 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
370 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
371 _("Seek not supported on stream"));
375 if (!g_output_stream_set_pending (output_stream
, error
))
379 g_cancellable_push_current (cancellable
);
381 res
= class->seek (stream
, offset
, type
, cancellable
, error
);
384 g_cancellable_pop_current (cancellable
);
386 g_output_stream_clear_pending (output_stream
);
392 g_file_output_stream_seekable_seek (GSeekable
*seekable
,
395 GCancellable
*cancellable
,
398 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable
),
399 offset
, type
, cancellable
, error
);
403 g_file_output_stream_can_truncate (GFileOutputStream
*stream
)
405 GFileOutputStreamClass
*class;
406 gboolean can_truncate
;
408 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), FALSE
);
410 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
412 can_truncate
= FALSE
;
413 if (class->truncate_fn
)
416 if (class->can_truncate
)
417 can_truncate
= class->can_truncate (stream
);
424 g_file_output_stream_seekable_can_truncate (GSeekable
*seekable
)
426 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable
));
430 g_file_output_stream_truncate (GFileOutputStream
*stream
,
432 GCancellable
*cancellable
,
435 GFileOutputStreamClass
*class;
436 GOutputStream
*output_stream
;
439 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream
), FALSE
);
441 output_stream
= G_OUTPUT_STREAM (stream
);
442 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
444 if (!class->truncate_fn
)
446 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
447 _("Truncate not supported on stream"));
451 if (!g_output_stream_set_pending (output_stream
, error
))
455 g_cancellable_push_current (cancellable
);
457 res
= class->truncate_fn (stream
, size
, cancellable
, error
);
460 g_cancellable_pop_current (cancellable
);
462 g_output_stream_clear_pending (output_stream
);
468 g_file_output_stream_seekable_truncate (GSeekable
*seekable
,
470 GCancellable
*cancellable
,
473 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable
),
474 size
, cancellable
, error
);
476 /********************************************
477 * Default implementation of async ops *
478 ********************************************/
481 query_info_async_thread (GTask
*task
,
482 gpointer source_object
,
484 GCancellable
*cancellable
)
486 GFileOutputStream
*stream
= source_object
;
487 const char *attributes
= task_data
;
488 GFileOutputStreamClass
*class;
489 GError
*error
= NULL
;
490 GFileInfo
*info
= NULL
;
492 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream
);
493 if (class->query_info
)
494 info
= class->query_info (stream
, attributes
, cancellable
, &error
);
496 g_set_error_literal (&error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
497 _("Stream doesn’t support query_info"));
500 g_task_return_error (task
, error
);
502 g_task_return_pointer (task
, info
, g_object_unref
);
506 g_file_output_stream_real_query_info_async (GFileOutputStream
*stream
,
507 const char *attributes
,
509 GCancellable
*cancellable
,
510 GAsyncReadyCallback callback
,
515 task
= g_task_new (stream
, cancellable
, callback
, user_data
);
516 g_task_set_source_tag (task
, g_file_output_stream_real_query_info_async
);
517 g_task_set_task_data (task
, g_strdup (attributes
), g_free
);
518 g_task_set_priority (task
, io_priority
);
520 g_task_run_in_thread (task
, query_info_async_thread
);
521 g_object_unref (task
);
525 g_file_output_stream_real_query_info_finish (GFileOutputStream
*stream
,
529 g_return_val_if_fail (g_task_is_valid (res
, stream
), NULL
);
531 return g_task_propagate_pointer (G_TASK (res
), error
);