1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <gfileoutputstream.h>
27 #include <gseekable.h>
28 #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 struct _GFileOutputStreamPrivate {
81 GAsyncReadyCallback outstanding_callback;
84 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
85 G_ADD_PRIVATE (GFileOutputStream)
86 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
87 g_file_output_stream_seekable_iface_init));
90 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
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_file_output_stream_get_instance_private (stream);
113 * g_file_output_stream_query_info:
114 * @stream: a #GFileOutputStream.
115 * @attributes: a file attribute query string.
116 * @cancellable: optional #GCancellable object, %NULL to ignore.
117 * @error: a #GError, %NULL to ignore.
119 * Queries a file output stream for the given @attributes.
120 * This function blocks while querying the stream. For the asynchronous
121 * version of this function, see g_file_output_stream_query_info_async().
122 * While the stream is blocked, the stream will set the pending flag
123 * internally, and any other operations on the stream will fail with
124 * %G_IO_ERROR_PENDING.
126 * Can fail if the stream was already closed (with @error being set to
127 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
128 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
129 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
130 * all cases of failure, %NULL will be returned.
132 * If @cancellable is not %NULL, then the operation can be cancelled by
133 * triggering the cancellable object from another thread. If the operation
134 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
137 * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error.
140 g_file_output_stream_query_info (GFileOutputStream *stream,
141 const char *attributes,
142 GCancellable *cancellable,
145 GFileOutputStreamClass *class;
146 GOutputStream *output_stream;
149 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
151 output_stream = G_OUTPUT_STREAM (stream);
153 if (!g_output_stream_set_pending (output_stream, error))
159 g_cancellable_push_current (cancellable);
161 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
162 if (class->query_info)
163 info = class->query_info (stream, attributes, cancellable, error);
165 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
166 _("Stream doesn’t support query_info"));
169 g_cancellable_pop_current (cancellable);
171 g_output_stream_clear_pending (output_stream);
177 async_ready_callback_wrapper (GObject *source_object,
181 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
183 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
184 if (stream->priv->outstanding_callback)
185 (*stream->priv->outstanding_callback) (source_object, res, user_data);
186 g_object_unref (stream);
190 * g_file_output_stream_query_info_async:
191 * @stream: a #GFileOutputStream.
192 * @attributes: a file attribute query string.
193 * @io_priority: the [I/O priority][gio-GIOScheduler] of the request
194 * @cancellable: optional #GCancellable object, %NULL to ignore.
195 * @callback: callback to call when the request is satisfied
196 * @user_data: the data to pass to callback function
198 * Asynchronously queries the @stream for a #GFileInfo. When completed,
199 * @callback will be called with a #GAsyncResult which can be used to
200 * finish the operation with g_file_output_stream_query_info_finish().
202 * For the synchronous version of this function, see
203 * g_file_output_stream_query_info().
207 g_file_output_stream_query_info_async (GFileOutputStream *stream,
208 const char *attributes,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
214 GFileOutputStreamClass *klass;
215 GOutputStream *output_stream;
216 GError *error = NULL;
218 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
220 output_stream = G_OUTPUT_STREAM (stream);
222 if (!g_output_stream_set_pending (output_stream, &error))
224 g_task_report_error (stream, callback, user_data,
225 g_file_output_stream_query_info_async,
230 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
232 stream->priv->outstanding_callback = callback;
233 g_object_ref (stream);
234 klass->query_info_async (stream, attributes, io_priority, cancellable,
235 async_ready_callback_wrapper, user_data);
239 * g_file_output_stream_query_info_finish:
240 * @stream: a #GFileOutputStream.
241 * @result: a #GAsyncResult.
242 * @error: a #GError, %NULL to ignore.
244 * Finalizes the asynchronous query started
245 * by g_file_output_stream_query_info_async().
247 * Returns: (transfer full): A #GFileInfo for the finished query.
250 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
251 GAsyncResult *result,
254 GFileOutputStreamClass *class;
256 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
257 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
259 if (g_async_result_legacy_propagate_error (result, error))
261 else if (g_async_result_is_tagged (result, g_file_output_stream_query_info_async))
262 return g_task_propagate_pointer (G_TASK (result), error);
264 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
265 return class->query_info_finish (stream, result, error);
269 * g_file_output_stream_get_etag:
270 * @stream: a #GFileOutputStream.
272 * Gets the entity tag for the file when it has been written.
273 * This must be called after the stream has been written
274 * and closed, as the etag can change while writing.
276 * Returns: (nullable) (transfer full): the entity tag for the stream.
279 g_file_output_stream_get_etag (GFileOutputStream *stream)
281 GFileOutputStreamClass *class;
282 GOutputStream *output_stream;
285 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
287 output_stream = G_OUTPUT_STREAM (stream);
289 if (!g_output_stream_is_closed (output_stream))
291 g_warning ("stream is not closed yet, can't get etag");
297 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
299 etag = class->get_etag (stream);
305 g_file_output_stream_tell (GFileOutputStream *stream)
307 GFileOutputStreamClass *class;
310 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
312 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
316 offset = class->tell (stream);
322 g_file_output_stream_seekable_tell (GSeekable *seekable)
324 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
328 g_file_output_stream_can_seek (GFileOutputStream *stream)
330 GFileOutputStreamClass *class;
333 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
335 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
342 can_seek = class->can_seek (stream);
349 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
351 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
355 g_file_output_stream_seek (GFileOutputStream *stream,
358 GCancellable *cancellable,
361 GFileOutputStreamClass *class;
362 GOutputStream *output_stream;
365 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
367 output_stream = G_OUTPUT_STREAM (stream);
368 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
372 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
373 _("Seek not supported on stream"));
377 if (!g_output_stream_set_pending (output_stream, error))
381 g_cancellable_push_current (cancellable);
383 res = class->seek (stream, offset, type, cancellable, error);
386 g_cancellable_pop_current (cancellable);
388 g_output_stream_clear_pending (output_stream);
394 g_file_output_stream_seekable_seek (GSeekable *seekable,
397 GCancellable *cancellable,
400 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
401 offset, type, cancellable, error);
405 g_file_output_stream_can_truncate (GFileOutputStream *stream)
407 GFileOutputStreamClass *class;
408 gboolean can_truncate;
410 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
412 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
414 can_truncate = FALSE;
415 if (class->truncate_fn)
418 if (class->can_truncate)
419 can_truncate = class->can_truncate (stream);
426 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
428 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
432 g_file_output_stream_truncate (GFileOutputStream *stream,
434 GCancellable *cancellable,
437 GFileOutputStreamClass *class;
438 GOutputStream *output_stream;
441 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
443 output_stream = G_OUTPUT_STREAM (stream);
444 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
446 if (!class->truncate_fn)
448 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
449 _("Truncate not supported on stream"));
453 if (!g_output_stream_set_pending (output_stream, error))
457 g_cancellable_push_current (cancellable);
459 res = class->truncate_fn (stream, size, cancellable, error);
462 g_cancellable_pop_current (cancellable);
464 g_output_stream_clear_pending (output_stream);
470 g_file_output_stream_seekable_truncate (GSeekable *seekable,
472 GCancellable *cancellable,
475 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
476 size, cancellable, error);
478 /********************************************
479 * Default implementation of async ops *
480 ********************************************/
483 query_info_async_thread (GTask *task,
484 gpointer source_object,
486 GCancellable *cancellable)
488 GFileOutputStream *stream = source_object;
489 const char *attributes = task_data;
490 GFileOutputStreamClass *class;
491 GError *error = NULL;
492 GFileInfo *info = NULL;
494 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
495 if (class->query_info)
496 info = class->query_info (stream, attributes, cancellable, &error);
498 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
499 _("Stream doesn’t support query_info"));
502 g_task_return_error (task, error);
504 g_task_return_pointer (task, info, g_object_unref);
508 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
509 const char *attributes,
511 GCancellable *cancellable,
512 GAsyncReadyCallback callback,
517 task = g_task_new (stream, cancellable, callback, user_data);
518 g_task_set_source_tag (task, g_file_output_stream_real_query_info_async);
519 g_task_set_task_data (task, g_strdup (attributes), g_free);
520 g_task_set_priority (task, io_priority);
522 g_task_run_in_thread (task, query_info_async_thread);
523 g_object_unref (task);
527 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
531 g_return_val_if_fail (g_task_is_valid (res, stream), NULL);
533 return g_task_propagate_pointer (G_TASK (res), error);