/src/rauc/subprojects/glib-2.76.5/gio/gfileiostream.c
Line | Count | Source |
1 | | /* GIO - GLib Input, IO 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 <gfileiostream.h> |
27 | | #include <gseekable.h> |
28 | | #include "gasyncresult.h" |
29 | | #include "gtask.h" |
30 | | #include "gcancellable.h" |
31 | | #include "gioerror.h" |
32 | | #include "gfileoutputstream.h" |
33 | | #include "glibintl.h" |
34 | | |
35 | | |
36 | | /** |
37 | | * SECTION:gfileiostream |
38 | | * @short_description: File read and write streaming operations |
39 | | * @include: gio/gio.h |
40 | | * @see_also: #GIOStream, #GFileInputStream, #GFileOutputStream, #GSeekable |
41 | | * |
42 | | * GFileIOStream provides io streams that both read and write to the same |
43 | | * file handle. |
44 | | * |
45 | | * GFileIOStream implements #GSeekable, which allows the io |
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. |
49 | | * |
50 | | * To find the position of a file io stream, use |
51 | | * g_seekable_tell(). |
52 | | * |
53 | | * To find out if a file io stream supports seeking, use g_seekable_can_seek(). |
54 | | * To position a file io stream, use g_seekable_seek(). |
55 | | * To find out if a file io stream supports truncating, use |
56 | | * g_seekable_can_truncate(). To truncate a file io |
57 | | * stream, use g_seekable_truncate(). |
58 | | * |
59 | | * The default implementation of all the #GFileIOStream operations |
60 | | * and the implementation of #GSeekable just call into the same operations |
61 | | * on the output stream. |
62 | | * Since: 2.22 |
63 | | **/ |
64 | | |
65 | | static void g_file_io_stream_seekable_iface_init (GSeekableIface *iface); |
66 | | static goffset g_file_io_stream_seekable_tell (GSeekable *seekable); |
67 | | static gboolean g_file_io_stream_seekable_can_seek (GSeekable *seekable); |
68 | | static gboolean g_file_io_stream_seekable_seek (GSeekable *seekable, |
69 | | goffset offset, |
70 | | GSeekType type, |
71 | | GCancellable *cancellable, |
72 | | GError **error); |
73 | | static gboolean g_file_io_stream_seekable_can_truncate (GSeekable *seekable); |
74 | | static gboolean g_file_io_stream_seekable_truncate (GSeekable *seekable, |
75 | | goffset offset, |
76 | | GCancellable *cancellable, |
77 | | GError **error); |
78 | | static void g_file_io_stream_real_query_info_async (GFileIOStream *stream, |
79 | | const char *attributes, |
80 | | int io_priority, |
81 | | GCancellable *cancellable, |
82 | | GAsyncReadyCallback callback, |
83 | | gpointer user_data); |
84 | | static GFileInfo *g_file_io_stream_real_query_info_finish (GFileIOStream *stream, |
85 | | GAsyncResult *result, |
86 | | GError **error); |
87 | | |
88 | | struct _GFileIOStreamPrivate { |
89 | | GAsyncReadyCallback outstanding_callback; |
90 | | }; |
91 | | |
92 | 0 | G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM, |
93 | 0 | G_ADD_PRIVATE (GFileIOStream) |
94 | 0 | G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE, |
95 | 0 | g_file_io_stream_seekable_iface_init)) |
96 | 0 |
|
97 | 0 | static void |
98 | 0 | g_file_io_stream_seekable_iface_init (GSeekableIface *iface) |
99 | 0 | { |
100 | 0 | iface->tell = g_file_io_stream_seekable_tell; |
101 | 0 | iface->can_seek = g_file_io_stream_seekable_can_seek; |
102 | 0 | iface->seek = g_file_io_stream_seekable_seek; |
103 | 0 | iface->can_truncate = g_file_io_stream_seekable_can_truncate; |
104 | 0 | iface->truncate_fn = g_file_io_stream_seekable_truncate; |
105 | 0 | } |
106 | | |
107 | | static void |
108 | | g_file_io_stream_init (GFileIOStream *stream) |
109 | 0 | { |
110 | 0 | stream->priv = g_file_io_stream_get_instance_private (stream); |
111 | 0 | } |
112 | | |
113 | | /** |
114 | | * g_file_io_stream_query_info: |
115 | | * @stream: a #GFileIOStream. |
116 | | * @attributes: a file attribute query string. |
117 | | * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. |
118 | | * @error: a #GError, %NULL to ignore. |
119 | | * |
120 | | * Queries a file io stream for the given @attributes. |
121 | | * This function blocks while querying the stream. For the asynchronous |
122 | | * version of this function, see g_file_io_stream_query_info_async(). |
123 | | * While the stream is blocked, the stream will set the pending flag |
124 | | * internally, and any other operations on the stream will fail with |
125 | | * %G_IO_ERROR_PENDING. |
126 | | * |
127 | | * Can fail if the stream was already closed (with @error being set to |
128 | | * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being |
129 | | * set to %G_IO_ERROR_PENDING), or if querying info is not supported for |
130 | | * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). I |
131 | | * all cases of failure, %NULL will be returned. |
132 | | * |
133 | | * If @cancellable is not %NULL, then the operation can be cancelled by |
134 | | * triggering the cancellable object from another thread. If the operation |
135 | | * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will |
136 | | * be returned. |
137 | | * |
138 | | * Returns: (transfer full): a #GFileInfo for the @stream, or %NULL on error. |
139 | | * |
140 | | * Since: 2.22 |
141 | | **/ |
142 | | GFileInfo * |
143 | | g_file_io_stream_query_info (GFileIOStream *stream, |
144 | | const char *attributes, |
145 | | GCancellable *cancellable, |
146 | | GError **error) |
147 | 0 | { |
148 | 0 | GFileIOStreamClass *class; |
149 | 0 | GIOStream *io_stream; |
150 | 0 | GFileInfo *info; |
151 | |
|
152 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL); |
153 | | |
154 | 0 | io_stream = G_IO_STREAM (stream); |
155 | |
|
156 | 0 | if (!g_io_stream_set_pending (io_stream, error)) |
157 | 0 | return NULL; |
158 | | |
159 | 0 | info = NULL; |
160 | |
|
161 | 0 | if (cancellable) |
162 | 0 | g_cancellable_push_current (cancellable); |
163 | |
|
164 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
165 | 0 | if (class->query_info) |
166 | 0 | info = class->query_info (stream, attributes, cancellable, error); |
167 | 0 | else |
168 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
169 | 0 | _("Stream doesn’t support query_info")); |
170 | |
|
171 | 0 | if (cancellable) |
172 | 0 | g_cancellable_pop_current (cancellable); |
173 | |
|
174 | 0 | g_io_stream_clear_pending (io_stream); |
175 | |
|
176 | 0 | return info; |
177 | 0 | } |
178 | | |
179 | | static void |
180 | | async_ready_callback_wrapper (GObject *source_object, |
181 | | GAsyncResult *res, |
182 | | gpointer user_data) |
183 | 0 | { |
184 | 0 | GFileIOStream *stream = G_FILE_IO_STREAM (source_object); |
185 | |
|
186 | 0 | g_io_stream_clear_pending (G_IO_STREAM (stream)); |
187 | 0 | if (stream->priv->outstanding_callback) |
188 | 0 | (*stream->priv->outstanding_callback) (source_object, res, user_data); |
189 | 0 | g_object_unref (stream); |
190 | 0 | } |
191 | | |
192 | | /** |
193 | | * g_file_io_stream_query_info_async: |
194 | | * @stream: a #GFileIOStream. |
195 | | * @attributes: a file attribute query string. |
196 | | * @io_priority: the [I/O priority][gio-GIOScheduler] of the request |
197 | | * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore. |
198 | | * @callback: (scope async): callback to call when the request is satisfied |
199 | | * @user_data: (closure): the data to pass to callback function |
200 | | * |
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_io_stream_query_info_finish(). |
204 | | * |
205 | | * For the synchronous version of this function, see |
206 | | * g_file_io_stream_query_info(). |
207 | | * |
208 | | * Since: 2.22 |
209 | | **/ |
210 | | void |
211 | | g_file_io_stream_query_info_async (GFileIOStream *stream, |
212 | | const char *attributes, |
213 | | int io_priority, |
214 | | GCancellable *cancellable, |
215 | | GAsyncReadyCallback callback, |
216 | | gpointer user_data) |
217 | 0 | { |
218 | 0 | GFileIOStreamClass *klass; |
219 | 0 | GIOStream *io_stream; |
220 | 0 | GError *error = NULL; |
221 | |
|
222 | 0 | g_return_if_fail (G_IS_FILE_IO_STREAM (stream)); |
223 | | |
224 | 0 | io_stream = G_IO_STREAM (stream); |
225 | |
|
226 | 0 | if (!g_io_stream_set_pending (io_stream, &error)) |
227 | 0 | { |
228 | 0 | g_task_report_error (stream, callback, user_data, |
229 | 0 | g_file_io_stream_query_info_async, |
230 | 0 | error); |
231 | 0 | return; |
232 | 0 | } |
233 | | |
234 | 0 | klass = G_FILE_IO_STREAM_GET_CLASS (stream); |
235 | |
|
236 | 0 | stream->priv->outstanding_callback = callback; |
237 | 0 | g_object_ref (stream); |
238 | 0 | klass->query_info_async (stream, attributes, io_priority, cancellable, |
239 | 0 | async_ready_callback_wrapper, user_data); |
240 | 0 | } |
241 | | |
242 | | /** |
243 | | * g_file_io_stream_query_info_finish: |
244 | | * @stream: a #GFileIOStream. |
245 | | * @result: a #GAsyncResult. |
246 | | * @error: a #GError, %NULL to ignore. |
247 | | * |
248 | | * Finalizes the asynchronous query started |
249 | | * by g_file_io_stream_query_info_async(). |
250 | | * |
251 | | * Returns: (transfer full): A #GFileInfo for the finished query. |
252 | | * |
253 | | * Since: 2.22 |
254 | | **/ |
255 | | GFileInfo * |
256 | | g_file_io_stream_query_info_finish (GFileIOStream *stream, |
257 | | GAsyncResult *result, |
258 | | GError **error) |
259 | 0 | { |
260 | 0 | GFileIOStreamClass *class; |
261 | |
|
262 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL); |
263 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL); |
264 | | |
265 | 0 | if (g_async_result_legacy_propagate_error (result, error)) |
266 | 0 | return NULL; |
267 | 0 | else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async)) |
268 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
269 | | |
270 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
271 | 0 | return class->query_info_finish (stream, result, error); |
272 | 0 | } |
273 | | |
274 | | /** |
275 | | * g_file_io_stream_get_etag: |
276 | | * @stream: a #GFileIOStream. |
277 | | * |
278 | | * Gets the entity tag for the file when it has been written. |
279 | | * This must be called after the stream has been written |
280 | | * and closed, as the etag can change while writing. |
281 | | * |
282 | | * Returns: (nullable) (transfer full): the entity tag for the stream. |
283 | | * |
284 | | * Since: 2.22 |
285 | | **/ |
286 | | char * |
287 | | g_file_io_stream_get_etag (GFileIOStream *stream) |
288 | 0 | { |
289 | 0 | GFileIOStreamClass *class; |
290 | 0 | GIOStream *io_stream; |
291 | 0 | char *etag; |
292 | |
|
293 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL); |
294 | | |
295 | 0 | io_stream = G_IO_STREAM (stream); |
296 | |
|
297 | 0 | if (!g_io_stream_is_closed (io_stream)) |
298 | 0 | { |
299 | 0 | g_warning ("stream is not closed yet, can't get etag"); |
300 | 0 | return NULL; |
301 | 0 | } |
302 | | |
303 | 0 | etag = NULL; |
304 | |
|
305 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
306 | 0 | if (class->get_etag) |
307 | 0 | etag = class->get_etag (stream); |
308 | |
|
309 | 0 | return etag; |
310 | 0 | } |
311 | | |
312 | | static goffset |
313 | | g_file_io_stream_tell (GFileIOStream *stream) |
314 | 0 | { |
315 | 0 | GFileIOStreamClass *class; |
316 | 0 | goffset offset; |
317 | |
|
318 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0); |
319 | | |
320 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
321 | |
|
322 | 0 | offset = 0; |
323 | 0 | if (class->tell) |
324 | 0 | offset = class->tell (stream); |
325 | |
|
326 | 0 | return offset; |
327 | 0 | } |
328 | | |
329 | | static goffset |
330 | | g_file_io_stream_seekable_tell (GSeekable *seekable) |
331 | 0 | { |
332 | 0 | return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable)); |
333 | 0 | } |
334 | | |
335 | | static gboolean |
336 | | g_file_io_stream_can_seek (GFileIOStream *stream) |
337 | 0 | { |
338 | 0 | GFileIOStreamClass *class; |
339 | 0 | gboolean can_seek; |
340 | |
|
341 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
342 | | |
343 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
344 | |
|
345 | 0 | can_seek = FALSE; |
346 | 0 | if (class->seek) |
347 | 0 | { |
348 | 0 | can_seek = TRUE; |
349 | 0 | if (class->can_seek) |
350 | 0 | can_seek = class->can_seek (stream); |
351 | 0 | } |
352 | |
|
353 | 0 | return can_seek; |
354 | 0 | } |
355 | | |
356 | | static gboolean |
357 | | g_file_io_stream_seekable_can_seek (GSeekable *seekable) |
358 | 0 | { |
359 | 0 | return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable)); |
360 | 0 | } |
361 | | |
362 | | static gboolean |
363 | | g_file_io_stream_seek (GFileIOStream *stream, |
364 | | goffset offset, |
365 | | GSeekType type, |
366 | | GCancellable *cancellable, |
367 | | GError **error) |
368 | 0 | { |
369 | 0 | GFileIOStreamClass *class; |
370 | 0 | GIOStream *io_stream; |
371 | 0 | gboolean res; |
372 | |
|
373 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
374 | | |
375 | 0 | io_stream = G_IO_STREAM (stream); |
376 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
377 | |
|
378 | 0 | if (!class->seek) |
379 | 0 | { |
380 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
381 | 0 | _("Seek not supported on stream")); |
382 | 0 | return FALSE; |
383 | 0 | } |
384 | | |
385 | 0 | if (!g_io_stream_set_pending (io_stream, error)) |
386 | 0 | return FALSE; |
387 | | |
388 | 0 | if (cancellable) |
389 | 0 | g_cancellable_push_current (cancellable); |
390 | |
|
391 | 0 | res = class->seek (stream, offset, type, cancellable, error); |
392 | |
|
393 | 0 | if (cancellable) |
394 | 0 | g_cancellable_pop_current (cancellable); |
395 | |
|
396 | 0 | g_io_stream_clear_pending (io_stream); |
397 | |
|
398 | 0 | return res; |
399 | 0 | } |
400 | | |
401 | | static gboolean |
402 | | g_file_io_stream_seekable_seek (GSeekable *seekable, |
403 | | goffset offset, |
404 | | GSeekType type, |
405 | | GCancellable *cancellable, |
406 | | GError **error) |
407 | 0 | { |
408 | 0 | return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable), |
409 | 0 | offset, type, cancellable, error); |
410 | 0 | } |
411 | | |
412 | | static gboolean |
413 | | g_file_io_stream_can_truncate (GFileIOStream *stream) |
414 | 0 | { |
415 | 0 | GFileIOStreamClass *class; |
416 | 0 | gboolean can_truncate; |
417 | |
|
418 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
419 | | |
420 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
421 | |
|
422 | 0 | can_truncate = FALSE; |
423 | 0 | if (class->truncate_fn) |
424 | 0 | { |
425 | 0 | can_truncate = TRUE; |
426 | 0 | if (class->can_truncate) |
427 | 0 | can_truncate = class->can_truncate (stream); |
428 | 0 | } |
429 | |
|
430 | 0 | return can_truncate; |
431 | 0 | } |
432 | | |
433 | | static gboolean |
434 | | g_file_io_stream_seekable_can_truncate (GSeekable *seekable) |
435 | 0 | { |
436 | 0 | return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable)); |
437 | 0 | } |
438 | | |
439 | | static gboolean |
440 | | g_file_io_stream_truncate (GFileIOStream *stream, |
441 | | goffset size, |
442 | | GCancellable *cancellable, |
443 | | GError **error) |
444 | 0 | { |
445 | 0 | GFileIOStreamClass *class; |
446 | 0 | GIOStream *io_stream; |
447 | 0 | gboolean res; |
448 | |
|
449 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
450 | | |
451 | 0 | io_stream = G_IO_STREAM (stream); |
452 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
453 | |
|
454 | 0 | if (!class->truncate_fn) |
455 | 0 | { |
456 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
457 | 0 | _("Truncate not supported on stream")); |
458 | 0 | return FALSE; |
459 | 0 | } |
460 | | |
461 | 0 | if (!g_io_stream_set_pending (io_stream, error)) |
462 | 0 | return FALSE; |
463 | | |
464 | 0 | if (cancellable) |
465 | 0 | g_cancellable_push_current (cancellable); |
466 | |
|
467 | 0 | res = class->truncate_fn (stream, size, cancellable, error); |
468 | |
|
469 | 0 | if (cancellable) |
470 | 0 | g_cancellable_pop_current (cancellable); |
471 | |
|
472 | 0 | g_io_stream_clear_pending (io_stream); |
473 | |
|
474 | 0 | return res; |
475 | 0 | } |
476 | | |
477 | | static gboolean |
478 | | g_file_io_stream_seekable_truncate (GSeekable *seekable, |
479 | | goffset size, |
480 | | GCancellable *cancellable, |
481 | | GError **error) |
482 | 0 | { |
483 | 0 | return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable), |
484 | 0 | size, cancellable, error); |
485 | 0 | } |
486 | | /***************************************************** |
487 | | * Default implementations based on output stream * |
488 | | *****************************************************/ |
489 | | |
490 | | static goffset |
491 | | g_file_io_stream_real_tell (GFileIOStream *stream) |
492 | 0 | { |
493 | 0 | GOutputStream *out; |
494 | 0 | GSeekable *seekable; |
495 | |
|
496 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
497 | 0 | seekable = G_SEEKABLE (out); |
498 | |
|
499 | 0 | return g_seekable_tell (seekable); |
500 | 0 | } |
501 | | |
502 | | static gboolean |
503 | | g_file_io_stream_real_can_seek (GFileIOStream *stream) |
504 | 0 | { |
505 | 0 | GOutputStream *out; |
506 | 0 | GSeekable *seekable; |
507 | |
|
508 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
509 | 0 | seekable = G_SEEKABLE (out); |
510 | |
|
511 | 0 | return g_seekable_can_seek (seekable); |
512 | 0 | } |
513 | | |
514 | | static gboolean |
515 | | g_file_io_stream_real_seek (GFileIOStream *stream, |
516 | | goffset offset, |
517 | | GSeekType type, |
518 | | GCancellable *cancellable, |
519 | | GError **error) |
520 | 0 | { |
521 | 0 | GOutputStream *out; |
522 | 0 | GSeekable *seekable; |
523 | |
|
524 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
525 | 0 | seekable = G_SEEKABLE (out); |
526 | |
|
527 | 0 | return g_seekable_seek (seekable, offset, type, cancellable, error); |
528 | 0 | } |
529 | | |
530 | | static gboolean |
531 | | g_file_io_stream_real_can_truncate (GFileIOStream *stream) |
532 | 0 | { |
533 | 0 | GOutputStream *out; |
534 | 0 | GSeekable *seekable; |
535 | |
|
536 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
537 | 0 | seekable = G_SEEKABLE (out); |
538 | |
|
539 | 0 | return g_seekable_can_truncate (seekable); |
540 | 0 | } |
541 | | |
542 | | static gboolean |
543 | | g_file_io_stream_real_truncate_fn (GFileIOStream *stream, |
544 | | goffset size, |
545 | | GCancellable *cancellable, |
546 | | GError **error) |
547 | 0 | { |
548 | 0 | GOutputStream *out; |
549 | 0 | GSeekable *seekable; |
550 | |
|
551 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
552 | 0 | seekable = G_SEEKABLE (out); |
553 | |
|
554 | 0 | return g_seekable_truncate (seekable, size, cancellable, error); |
555 | 0 | } |
556 | | |
557 | | static char * |
558 | | g_file_io_stream_real_get_etag (GFileIOStream *stream) |
559 | 0 | { |
560 | 0 | GOutputStream *out; |
561 | 0 | GFileOutputStream *file_out; |
562 | |
|
563 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
564 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
565 | |
|
566 | 0 | return g_file_output_stream_get_etag (file_out); |
567 | 0 | } |
568 | | |
569 | | static GFileInfo * |
570 | | g_file_io_stream_real_query_info (GFileIOStream *stream, |
571 | | const char *attributes, |
572 | | GCancellable *cancellable, |
573 | | GError **error) |
574 | 0 | { |
575 | 0 | GOutputStream *out; |
576 | 0 | GFileOutputStream *file_out; |
577 | |
|
578 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
579 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
580 | |
|
581 | 0 | return g_file_output_stream_query_info (file_out, |
582 | 0 | attributes, cancellable, error); |
583 | 0 | } |
584 | | |
585 | | typedef struct { |
586 | | GObject *object; |
587 | | GAsyncReadyCallback callback; |
588 | | gpointer user_data; |
589 | | } AsyncOpWrapper; |
590 | | |
591 | | static AsyncOpWrapper * |
592 | | async_op_wrapper_new (gpointer object, |
593 | | GAsyncReadyCallback callback, |
594 | | gpointer user_data) |
595 | 0 | { |
596 | 0 | AsyncOpWrapper *data; |
597 | |
|
598 | 0 | data = g_new0 (AsyncOpWrapper, 1); |
599 | 0 | data->object = g_object_ref (object); |
600 | 0 | data->callback = callback; |
601 | 0 | data->user_data = user_data; |
602 | |
|
603 | 0 | return data; |
604 | 0 | } |
605 | | |
606 | | static void |
607 | | async_op_wrapper_callback (GObject *source_object, |
608 | | GAsyncResult *res, |
609 | | gpointer user_data) |
610 | 0 | { |
611 | 0 | AsyncOpWrapper *data = user_data; |
612 | 0 | data->callback (data->object, res, data->user_data); |
613 | 0 | g_object_unref (data->object); |
614 | 0 | g_free (data); |
615 | 0 | } |
616 | | |
617 | | static void |
618 | | g_file_io_stream_real_query_info_async (GFileIOStream *stream, |
619 | | const char *attributes, |
620 | | int io_priority, |
621 | | GCancellable *cancellable, |
622 | | GAsyncReadyCallback callback, |
623 | | gpointer user_data) |
624 | 0 | { |
625 | 0 | GOutputStream *out; |
626 | 0 | GFileOutputStream *file_out; |
627 | 0 | AsyncOpWrapper *data; |
628 | |
|
629 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
630 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
631 | |
|
632 | 0 | data = async_op_wrapper_new (stream, callback, user_data); |
633 | 0 | g_file_output_stream_query_info_async (file_out, |
634 | 0 | attributes, io_priority, |
635 | 0 | cancellable, async_op_wrapper_callback, data); |
636 | 0 | } |
637 | | |
638 | | static GFileInfo * |
639 | | g_file_io_stream_real_query_info_finish (GFileIOStream *stream, |
640 | | GAsyncResult *res, |
641 | | GError **error) |
642 | 0 | { |
643 | 0 | GOutputStream *out; |
644 | 0 | GFileOutputStream *file_out; |
645 | |
|
646 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
647 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
648 | |
|
649 | 0 | return g_file_output_stream_query_info_finish (file_out, res, error); |
650 | 0 | } |
651 | | |
652 | | static void |
653 | | g_file_io_stream_class_init (GFileIOStreamClass *klass) |
654 | 0 | { |
655 | 0 | klass->tell = g_file_io_stream_real_tell; |
656 | 0 | klass->can_seek = g_file_io_stream_real_can_seek; |
657 | 0 | klass->seek = g_file_io_stream_real_seek; |
658 | 0 | klass->can_truncate = g_file_io_stream_real_can_truncate; |
659 | 0 | klass->truncate_fn = g_file_io_stream_real_truncate_fn; |
660 | 0 | klass->query_info = g_file_io_stream_real_query_info; |
661 | 0 | klass->query_info_async = g_file_io_stream_real_query_info_async; |
662 | 0 | klass->query_info_finish = g_file_io_stream_real_query_info_finish; |
663 | 0 | klass->get_etag = g_file_io_stream_real_get_etag; |
664 | 0 | } |