/src/glib/gio/gfileoutputstream.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
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. |
11 | | * |
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. |
16 | | * |
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/>. |
19 | | * |
20 | | * Author: Alexander Larsson <alexl@redhat.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include <glib.h> |
26 | | #include <gfileoutputstream.h> |
27 | | #include <gseekable.h> |
28 | | #include "gasyncresult.h" |
29 | | #include "gtask.h" |
30 | | #include "gcancellable.h" |
31 | | #include "gioerror.h" |
32 | | #include "glibintl.h" |
33 | | |
34 | | |
35 | | /** |
36 | | * SECTION:gfileoutputstream |
37 | | * @short_description: File output streaming operations |
38 | | * @include: gio/gio.h |
39 | | * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable |
40 | | * |
41 | | * GFileOutputStream provides output streams that write their |
42 | | * content to a file. |
43 | | * |
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 |
47 | | * operations. |
48 | | * |
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(). |
55 | | **/ |
56 | | |
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, |
61 | | goffset offset, |
62 | | GSeekType type, |
63 | | GCancellable *cancellable, |
64 | | GError **error); |
65 | | static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable); |
66 | | static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable, |
67 | | goffset offset, |
68 | | GCancellable *cancellable, |
69 | | GError **error); |
70 | | static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream, |
71 | | const char *attributes, |
72 | | int io_priority, |
73 | | GCancellable *cancellable, |
74 | | GAsyncReadyCallback callback, |
75 | | gpointer user_data); |
76 | | static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream, |
77 | | GAsyncResult *result, |
78 | | GError **error); |
79 | | |
80 | | struct _GFileOutputStreamPrivate { |
81 | | GAsyncReadyCallback outstanding_callback; |
82 | | }; |
83 | | |
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)); |
88 | | |
89 | | static void |
90 | | g_file_output_stream_class_init (GFileOutputStreamClass *klass) |
91 | 0 | { |
92 | 0 | klass->query_info_async = g_file_output_stream_real_query_info_async; |
93 | 0 | klass->query_info_finish = g_file_output_stream_real_query_info_finish; |
94 | 0 | } |
95 | | |
96 | | static void |
97 | | g_file_output_stream_seekable_iface_init (GSeekableIface *iface) |
98 | 0 | { |
99 | 0 | iface->tell = g_file_output_stream_seekable_tell; |
100 | 0 | iface->can_seek = g_file_output_stream_seekable_can_seek; |
101 | 0 | iface->seek = g_file_output_stream_seekable_seek; |
102 | 0 | iface->can_truncate = g_file_output_stream_seekable_can_truncate; |
103 | 0 | iface->truncate_fn = g_file_output_stream_seekable_truncate; |
104 | 0 | } |
105 | | |
106 | | static void |
107 | | g_file_output_stream_init (GFileOutputStream *stream) |
108 | 0 | { |
109 | 0 | stream->priv = g_file_output_stream_get_instance_private (stream); |
110 | 0 | } |
111 | | |
112 | | /** |
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. |
118 | | * |
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. |
125 | | * |
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. |
131 | | * |
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 |
135 | | * be returned. |
136 | | * |
137 | | * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error. |
138 | | **/ |
139 | | GFileInfo * |
140 | | g_file_output_stream_query_info (GFileOutputStream *stream, |
141 | | const char *attributes, |
142 | | GCancellable *cancellable, |
143 | | GError **error) |
144 | 0 | { |
145 | 0 | GFileOutputStreamClass *class; |
146 | 0 | GOutputStream *output_stream; |
147 | 0 | GFileInfo *info; |
148 | | |
149 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL); |
150 | | |
151 | 0 | output_stream = G_OUTPUT_STREAM (stream); |
152 | | |
153 | 0 | if (!g_output_stream_set_pending (output_stream, error)) |
154 | 0 | return NULL; |
155 | | |
156 | 0 | info = NULL; |
157 | | |
158 | 0 | if (cancellable) |
159 | 0 | g_cancellable_push_current (cancellable); |
160 | | |
161 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
162 | 0 | if (class->query_info) |
163 | 0 | info = class->query_info (stream, attributes, cancellable, error); |
164 | 0 | else |
165 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
166 | 0 | _("Stream doesn’t support query_info")); |
167 | | |
168 | 0 | if (cancellable) |
169 | 0 | g_cancellable_pop_current (cancellable); |
170 | | |
171 | 0 | g_output_stream_clear_pending (output_stream); |
172 | | |
173 | 0 | return info; |
174 | 0 | } |
175 | | |
176 | | static void |
177 | | async_ready_callback_wrapper (GObject *source_object, |
178 | | GAsyncResult *res, |
179 | | gpointer user_data) |
180 | 0 | { |
181 | 0 | GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object); |
182 | |
|
183 | 0 | g_output_stream_clear_pending (G_OUTPUT_STREAM (stream)); |
184 | 0 | if (stream->priv->outstanding_callback) |
185 | 0 | (*stream->priv->outstanding_callback) (source_object, res, user_data); |
186 | 0 | g_object_unref (stream); |
187 | 0 | } |
188 | | |
189 | | /** |
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 |
197 | | * |
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(). |
201 | | * |
202 | | * For the synchronous version of this function, see |
203 | | * g_file_output_stream_query_info(). |
204 | | * |
205 | | **/ |
206 | | void |
207 | | g_file_output_stream_query_info_async (GFileOutputStream *stream, |
208 | | const char *attributes, |
209 | | int io_priority, |
210 | | GCancellable *cancellable, |
211 | | GAsyncReadyCallback callback, |
212 | | gpointer user_data) |
213 | 0 | { |
214 | 0 | GFileOutputStreamClass *klass; |
215 | 0 | GOutputStream *output_stream; |
216 | 0 | GError *error = NULL; |
217 | |
|
218 | 0 | g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream)); |
219 | | |
220 | 0 | output_stream = G_OUTPUT_STREAM (stream); |
221 | | |
222 | 0 | if (!g_output_stream_set_pending (output_stream, &error)) |
223 | 0 | { |
224 | 0 | g_task_report_error (stream, callback, user_data, |
225 | 0 | g_file_output_stream_query_info_async, |
226 | 0 | error); |
227 | 0 | return; |
228 | 0 | } |
229 | | |
230 | 0 | klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
231 | |
|
232 | 0 | stream->priv->outstanding_callback = callback; |
233 | 0 | g_object_ref (stream); |
234 | 0 | klass->query_info_async (stream, attributes, io_priority, cancellable, |
235 | 0 | async_ready_callback_wrapper, user_data); |
236 | 0 | } |
237 | | |
238 | | /** |
239 | | * g_file_output_stream_query_info_finish: |
240 | | * @stream: a #GFileOutputStream. |
241 | | * @result: a #GAsyncResult. |
242 | | * @error: a #GError, %NULL to ignore. |
243 | | * |
244 | | * Finalizes the asynchronous query started |
245 | | * by g_file_output_stream_query_info_async(). |
246 | | * |
247 | | * Returns: (transfer full): A #GFileInfo for the finished query. |
248 | | **/ |
249 | | GFileInfo * |
250 | | g_file_output_stream_query_info_finish (GFileOutputStream *stream, |
251 | | GAsyncResult *result, |
252 | | GError **error) |
253 | 0 | { |
254 | 0 | GFileOutputStreamClass *class; |
255 | |
|
256 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL); |
257 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL); |
258 | | |
259 | 0 | if (g_async_result_legacy_propagate_error (result, error)) |
260 | 0 | return NULL; |
261 | 0 | else if (g_async_result_is_tagged (result, g_file_output_stream_query_info_async)) |
262 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
263 | | |
264 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
265 | 0 | return class->query_info_finish (stream, result, error); |
266 | 0 | } |
267 | | |
268 | | /** |
269 | | * g_file_output_stream_get_etag: |
270 | | * @stream: a #GFileOutputStream. |
271 | | * |
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. |
275 | | * |
276 | | * Returns: (nullable) (transfer full): the entity tag for the stream. |
277 | | **/ |
278 | | char * |
279 | | g_file_output_stream_get_etag (GFileOutputStream *stream) |
280 | 0 | { |
281 | 0 | GFileOutputStreamClass *class; |
282 | 0 | GOutputStream *output_stream; |
283 | 0 | char *etag; |
284 | | |
285 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL); |
286 | | |
287 | 0 | output_stream = G_OUTPUT_STREAM (stream); |
288 | | |
289 | 0 | if (!g_output_stream_is_closed (output_stream)) |
290 | 0 | { |
291 | 0 | g_warning ("stream is not closed yet, can't get etag"); |
292 | 0 | return NULL; |
293 | 0 | } |
294 | | |
295 | 0 | etag = NULL; |
296 | | |
297 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
298 | 0 | if (class->get_etag) |
299 | 0 | etag = class->get_etag (stream); |
300 | | |
301 | 0 | return etag; |
302 | 0 | } |
303 | | |
304 | | static goffset |
305 | | g_file_output_stream_tell (GFileOutputStream *stream) |
306 | 0 | { |
307 | 0 | GFileOutputStreamClass *class; |
308 | 0 | goffset offset; |
309 | | |
310 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0); |
311 | | |
312 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
313 | |
|
314 | 0 | offset = 0; |
315 | 0 | if (class->tell) |
316 | 0 | offset = class->tell (stream); |
317 | |
|
318 | 0 | return offset; |
319 | 0 | } |
320 | | |
321 | | static goffset |
322 | | g_file_output_stream_seekable_tell (GSeekable *seekable) |
323 | 0 | { |
324 | 0 | return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable)); |
325 | 0 | } |
326 | | |
327 | | static gboolean |
328 | | g_file_output_stream_can_seek (GFileOutputStream *stream) |
329 | 0 | { |
330 | 0 | GFileOutputStreamClass *class; |
331 | 0 | gboolean can_seek; |
332 | |
|
333 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE); |
334 | | |
335 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
336 | |
|
337 | 0 | can_seek = FALSE; |
338 | 0 | if (class->seek) |
339 | 0 | { |
340 | 0 | can_seek = TRUE; |
341 | 0 | if (class->can_seek) |
342 | 0 | can_seek = class->can_seek (stream); |
343 | 0 | } |
344 | | |
345 | 0 | return can_seek; |
346 | 0 | } |
347 | | |
348 | | static gboolean |
349 | | g_file_output_stream_seekable_can_seek (GSeekable *seekable) |
350 | 0 | { |
351 | 0 | return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable)); |
352 | 0 | } |
353 | | |
354 | | static gboolean |
355 | | g_file_output_stream_seek (GFileOutputStream *stream, |
356 | | goffset offset, |
357 | | GSeekType type, |
358 | | GCancellable *cancellable, |
359 | | GError **error) |
360 | 0 | { |
361 | 0 | GFileOutputStreamClass *class; |
362 | 0 | GOutputStream *output_stream; |
363 | 0 | gboolean res; |
364 | |
|
365 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE); |
366 | | |
367 | 0 | output_stream = G_OUTPUT_STREAM (stream); |
368 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
369 | |
|
370 | 0 | if (!class->seek) |
371 | 0 | { |
372 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
373 | 0 | _("Seek not supported on stream")); |
374 | 0 | return FALSE; |
375 | 0 | } |
376 | | |
377 | 0 | if (!g_output_stream_set_pending (output_stream, error)) |
378 | 0 | return FALSE; |
379 | | |
380 | 0 | if (cancellable) |
381 | 0 | g_cancellable_push_current (cancellable); |
382 | | |
383 | 0 | res = class->seek (stream, offset, type, cancellable, error); |
384 | | |
385 | 0 | if (cancellable) |
386 | 0 | g_cancellable_pop_current (cancellable); |
387 | |
|
388 | 0 | g_output_stream_clear_pending (output_stream); |
389 | | |
390 | 0 | return res; |
391 | 0 | } |
392 | | |
393 | | static gboolean |
394 | | g_file_output_stream_seekable_seek (GSeekable *seekable, |
395 | | goffset offset, |
396 | | GSeekType type, |
397 | | GCancellable *cancellable, |
398 | | GError **error) |
399 | 0 | { |
400 | 0 | return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable), |
401 | 0 | offset, type, cancellable, error); |
402 | 0 | } |
403 | | |
404 | | static gboolean |
405 | | g_file_output_stream_can_truncate (GFileOutputStream *stream) |
406 | 0 | { |
407 | 0 | GFileOutputStreamClass *class; |
408 | 0 | gboolean can_truncate; |
409 | |
|
410 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE); |
411 | | |
412 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
413 | |
|
414 | 0 | can_truncate = FALSE; |
415 | 0 | if (class->truncate_fn) |
416 | 0 | { |
417 | 0 | can_truncate = TRUE; |
418 | 0 | if (class->can_truncate) |
419 | 0 | can_truncate = class->can_truncate (stream); |
420 | 0 | } |
421 | | |
422 | 0 | return can_truncate; |
423 | 0 | } |
424 | | |
425 | | static gboolean |
426 | | g_file_output_stream_seekable_can_truncate (GSeekable *seekable) |
427 | 0 | { |
428 | 0 | return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable)); |
429 | 0 | } |
430 | | |
431 | | static gboolean |
432 | | g_file_output_stream_truncate (GFileOutputStream *stream, |
433 | | goffset size, |
434 | | GCancellable *cancellable, |
435 | | GError **error) |
436 | 0 | { |
437 | 0 | GFileOutputStreamClass *class; |
438 | 0 | GOutputStream *output_stream; |
439 | 0 | gboolean res; |
440 | |
|
441 | 0 | g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE); |
442 | | |
443 | 0 | output_stream = G_OUTPUT_STREAM (stream); |
444 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
445 | |
|
446 | 0 | if (!class->truncate_fn) |
447 | 0 | { |
448 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
449 | 0 | _("Truncate not supported on stream")); |
450 | 0 | return FALSE; |
451 | 0 | } |
452 | | |
453 | 0 | if (!g_output_stream_set_pending (output_stream, error)) |
454 | 0 | return FALSE; |
455 | | |
456 | 0 | if (cancellable) |
457 | 0 | g_cancellable_push_current (cancellable); |
458 | | |
459 | 0 | res = class->truncate_fn (stream, size, cancellable, error); |
460 | | |
461 | 0 | if (cancellable) |
462 | 0 | g_cancellable_pop_current (cancellable); |
463 | |
|
464 | 0 | g_output_stream_clear_pending (output_stream); |
465 | | |
466 | 0 | return res; |
467 | 0 | } |
468 | | |
469 | | static gboolean |
470 | | g_file_output_stream_seekable_truncate (GSeekable *seekable, |
471 | | goffset size, |
472 | | GCancellable *cancellable, |
473 | | GError **error) |
474 | 0 | { |
475 | 0 | return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable), |
476 | 0 | size, cancellable, error); |
477 | 0 | } |
478 | | /******************************************** |
479 | | * Default implementation of async ops * |
480 | | ********************************************/ |
481 | | |
482 | | static void |
483 | | query_info_async_thread (GTask *task, |
484 | | gpointer source_object, |
485 | | gpointer task_data, |
486 | | GCancellable *cancellable) |
487 | 0 | { |
488 | 0 | GFileOutputStream *stream = source_object; |
489 | 0 | const char *attributes = task_data; |
490 | 0 | GFileOutputStreamClass *class; |
491 | 0 | GError *error = NULL; |
492 | 0 | GFileInfo *info = NULL; |
493 | |
|
494 | 0 | class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream); |
495 | 0 | if (class->query_info) |
496 | 0 | info = class->query_info (stream, attributes, cancellable, &error); |
497 | 0 | else |
498 | 0 | g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
499 | 0 | _("Stream doesn’t support query_info")); |
500 | |
|
501 | 0 | if (info == NULL) |
502 | 0 | g_task_return_error (task, error); |
503 | 0 | else |
504 | 0 | g_task_return_pointer (task, info, g_object_unref); |
505 | 0 | } |
506 | | |
507 | | static void |
508 | | g_file_output_stream_real_query_info_async (GFileOutputStream *stream, |
509 | | const char *attributes, |
510 | | int io_priority, |
511 | | GCancellable *cancellable, |
512 | | GAsyncReadyCallback callback, |
513 | | gpointer user_data) |
514 | 0 | { |
515 | 0 | GTask *task; |
516 | |
|
517 | 0 | task = g_task_new (stream, cancellable, callback, user_data); |
518 | 0 | g_task_set_source_tag (task, g_file_output_stream_real_query_info_async); |
519 | 0 | g_task_set_task_data (task, g_strdup (attributes), g_free); |
520 | 0 | g_task_set_priority (task, io_priority); |
521 | | |
522 | 0 | g_task_run_in_thread (task, query_info_async_thread); |
523 | 0 | g_object_unref (task); |
524 | 0 | } |
525 | | |
526 | | static GFileInfo * |
527 | | g_file_output_stream_real_query_info_finish (GFileOutputStream *stream, |
528 | | GAsyncResult *res, |
529 | | GError **error) |
530 | 0 | { |
531 | 0 | g_return_val_if_fail (g_task_is_valid (res, stream), NULL); |
532 | | |
533 | 0 | return g_task_propagate_pointer (G_TASK (res), error); |
534 | 0 | } |