/src/glib/gio/gfileiostream.c
Line | Count | Source (jump to first uncovered line) |
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 | | G_DEFINE_TYPE_WITH_CODE (GFileIOStream, g_file_io_stream, G_TYPE_IO_STREAM, |
93 | | G_ADD_PRIVATE (GFileIOStream) |
94 | | G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE, |
95 | | g_file_io_stream_seekable_iface_init)) |
96 | | |
97 | | static void |
98 | | 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): a #GAsyncReadyCallback |
199 | | * to call when the request is satisfied |
200 | | * @user_data: the data to pass to callback function |
201 | | * |
202 | | * Asynchronously queries the @stream for a #GFileInfo. When completed, |
203 | | * @callback will be called with a #GAsyncResult which can be used to |
204 | | * finish the operation with g_file_io_stream_query_info_finish(). |
205 | | * |
206 | | * For the synchronous version of this function, see |
207 | | * g_file_io_stream_query_info(). |
208 | | * |
209 | | * Since: 2.22 |
210 | | **/ |
211 | | void |
212 | | g_file_io_stream_query_info_async (GFileIOStream *stream, |
213 | | const char *attributes, |
214 | | int io_priority, |
215 | | GCancellable *cancellable, |
216 | | GAsyncReadyCallback callback, |
217 | | gpointer user_data) |
218 | 0 | { |
219 | 0 | GFileIOStreamClass *klass; |
220 | 0 | GIOStream *io_stream; |
221 | 0 | GError *error = NULL; |
222 | |
|
223 | 0 | g_return_if_fail (G_IS_FILE_IO_STREAM (stream)); |
224 | | |
225 | 0 | io_stream = G_IO_STREAM (stream); |
226 | |
|
227 | 0 | if (!g_io_stream_set_pending (io_stream, &error)) |
228 | 0 | { |
229 | 0 | g_task_report_error (stream, callback, user_data, |
230 | 0 | g_file_io_stream_query_info_async, |
231 | 0 | error); |
232 | 0 | return; |
233 | 0 | } |
234 | | |
235 | 0 | klass = G_FILE_IO_STREAM_GET_CLASS (stream); |
236 | |
|
237 | 0 | stream->priv->outstanding_callback = callback; |
238 | 0 | g_object_ref (stream); |
239 | 0 | klass->query_info_async (stream, attributes, io_priority, cancellable, |
240 | 0 | async_ready_callback_wrapper, user_data); |
241 | 0 | } |
242 | | |
243 | | /** |
244 | | * g_file_io_stream_query_info_finish: |
245 | | * @stream: a #GFileIOStream. |
246 | | * @result: a #GAsyncResult. |
247 | | * @error: a #GError, %NULL to ignore. |
248 | | * |
249 | | * Finalizes the asynchronous query started |
250 | | * by g_file_io_stream_query_info_async(). |
251 | | * |
252 | | * Returns: (transfer full): A #GFileInfo for the finished query. |
253 | | * |
254 | | * Since: 2.22 |
255 | | **/ |
256 | | GFileInfo * |
257 | | g_file_io_stream_query_info_finish (GFileIOStream *stream, |
258 | | GAsyncResult *result, |
259 | | GError **error) |
260 | 0 | { |
261 | 0 | GFileIOStreamClass *class; |
262 | |
|
263 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL); |
264 | 0 | g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL); |
265 | | |
266 | 0 | if (g_async_result_legacy_propagate_error (result, error)) |
267 | 0 | return NULL; |
268 | 0 | else if (g_async_result_is_tagged (result, g_file_io_stream_query_info_async)) |
269 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
270 | | |
271 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
272 | 0 | return class->query_info_finish (stream, result, error); |
273 | 0 | } |
274 | | |
275 | | /** |
276 | | * g_file_io_stream_get_etag: |
277 | | * @stream: a #GFileIOStream. |
278 | | * |
279 | | * Gets the entity tag for the file when it has been written. |
280 | | * This must be called after the stream has been written |
281 | | * and closed, as the etag can change while writing. |
282 | | * |
283 | | * Returns: (nullable) (transfer full): the entity tag for the stream. |
284 | | * |
285 | | * Since: 2.22 |
286 | | **/ |
287 | | char * |
288 | | g_file_io_stream_get_etag (GFileIOStream *stream) |
289 | 0 | { |
290 | 0 | GFileIOStreamClass *class; |
291 | 0 | GIOStream *io_stream; |
292 | 0 | char *etag; |
293 | |
|
294 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), NULL); |
295 | | |
296 | 0 | io_stream = G_IO_STREAM (stream); |
297 | |
|
298 | 0 | if (!g_io_stream_is_closed (io_stream)) |
299 | 0 | { |
300 | 0 | g_warning ("stream is not closed yet, can't get etag"); |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | 0 | etag = NULL; |
305 | |
|
306 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
307 | 0 | if (class->get_etag) |
308 | 0 | etag = class->get_etag (stream); |
309 | |
|
310 | 0 | return etag; |
311 | 0 | } |
312 | | |
313 | | static goffset |
314 | | g_file_io_stream_tell (GFileIOStream *stream) |
315 | 0 | { |
316 | 0 | GFileIOStreamClass *class; |
317 | 0 | goffset offset; |
318 | |
|
319 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), 0); |
320 | | |
321 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
322 | |
|
323 | 0 | offset = 0; |
324 | 0 | if (class->tell) |
325 | 0 | offset = class->tell (stream); |
326 | |
|
327 | 0 | return offset; |
328 | 0 | } |
329 | | |
330 | | static goffset |
331 | | g_file_io_stream_seekable_tell (GSeekable *seekable) |
332 | 0 | { |
333 | 0 | return g_file_io_stream_tell (G_FILE_IO_STREAM (seekable)); |
334 | 0 | } |
335 | | |
336 | | static gboolean |
337 | | g_file_io_stream_can_seek (GFileIOStream *stream) |
338 | 0 | { |
339 | 0 | GFileIOStreamClass *class; |
340 | 0 | gboolean can_seek; |
341 | |
|
342 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
343 | | |
344 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
345 | |
|
346 | 0 | can_seek = FALSE; |
347 | 0 | if (class->seek) |
348 | 0 | { |
349 | 0 | can_seek = TRUE; |
350 | 0 | if (class->can_seek) |
351 | 0 | can_seek = class->can_seek (stream); |
352 | 0 | } |
353 | |
|
354 | 0 | return can_seek; |
355 | 0 | } |
356 | | |
357 | | static gboolean |
358 | | g_file_io_stream_seekable_can_seek (GSeekable *seekable) |
359 | 0 | { |
360 | 0 | return g_file_io_stream_can_seek (G_FILE_IO_STREAM (seekable)); |
361 | 0 | } |
362 | | |
363 | | static gboolean |
364 | | g_file_io_stream_seek (GFileIOStream *stream, |
365 | | goffset offset, |
366 | | GSeekType type, |
367 | | GCancellable *cancellable, |
368 | | GError **error) |
369 | 0 | { |
370 | 0 | GFileIOStreamClass *class; |
371 | 0 | GIOStream *io_stream; |
372 | 0 | gboolean res; |
373 | |
|
374 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
375 | | |
376 | 0 | io_stream = G_IO_STREAM (stream); |
377 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
378 | |
|
379 | 0 | if (!class->seek) |
380 | 0 | { |
381 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
382 | 0 | _("Seek not supported on stream")); |
383 | 0 | return FALSE; |
384 | 0 | } |
385 | | |
386 | 0 | if (!g_io_stream_set_pending (io_stream, error)) |
387 | 0 | return FALSE; |
388 | | |
389 | 0 | if (cancellable) |
390 | 0 | g_cancellable_push_current (cancellable); |
391 | |
|
392 | 0 | res = class->seek (stream, offset, type, cancellable, error); |
393 | |
|
394 | 0 | if (cancellable) |
395 | 0 | g_cancellable_pop_current (cancellable); |
396 | |
|
397 | 0 | g_io_stream_clear_pending (io_stream); |
398 | |
|
399 | 0 | return res; |
400 | 0 | } |
401 | | |
402 | | static gboolean |
403 | | g_file_io_stream_seekable_seek (GSeekable *seekable, |
404 | | goffset offset, |
405 | | GSeekType type, |
406 | | GCancellable *cancellable, |
407 | | GError **error) |
408 | 0 | { |
409 | 0 | return g_file_io_stream_seek (G_FILE_IO_STREAM (seekable), |
410 | 0 | offset, type, cancellable, error); |
411 | 0 | } |
412 | | |
413 | | static gboolean |
414 | | g_file_io_stream_can_truncate (GFileIOStream *stream) |
415 | 0 | { |
416 | 0 | GFileIOStreamClass *class; |
417 | 0 | gboolean can_truncate; |
418 | |
|
419 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
420 | | |
421 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
422 | |
|
423 | 0 | can_truncate = FALSE; |
424 | 0 | if (class->truncate_fn) |
425 | 0 | { |
426 | 0 | can_truncate = TRUE; |
427 | 0 | if (class->can_truncate) |
428 | 0 | can_truncate = class->can_truncate (stream); |
429 | 0 | } |
430 | |
|
431 | 0 | return can_truncate; |
432 | 0 | } |
433 | | |
434 | | static gboolean |
435 | | g_file_io_stream_seekable_can_truncate (GSeekable *seekable) |
436 | 0 | { |
437 | 0 | return g_file_io_stream_can_truncate (G_FILE_IO_STREAM (seekable)); |
438 | 0 | } |
439 | | |
440 | | static gboolean |
441 | | g_file_io_stream_truncate (GFileIOStream *stream, |
442 | | goffset size, |
443 | | GCancellable *cancellable, |
444 | | GError **error) |
445 | 0 | { |
446 | 0 | GFileIOStreamClass *class; |
447 | 0 | GIOStream *io_stream; |
448 | 0 | gboolean res; |
449 | |
|
450 | 0 | g_return_val_if_fail (G_IS_FILE_IO_STREAM (stream), FALSE); |
451 | | |
452 | 0 | io_stream = G_IO_STREAM (stream); |
453 | 0 | class = G_FILE_IO_STREAM_GET_CLASS (stream); |
454 | |
|
455 | 0 | if (!class->truncate_fn) |
456 | 0 | { |
457 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
458 | 0 | _("Truncate not supported on stream")); |
459 | 0 | return FALSE; |
460 | 0 | } |
461 | | |
462 | 0 | if (!g_io_stream_set_pending (io_stream, error)) |
463 | 0 | return FALSE; |
464 | | |
465 | 0 | if (cancellable) |
466 | 0 | g_cancellable_push_current (cancellable); |
467 | |
|
468 | 0 | res = class->truncate_fn (stream, size, cancellable, error); |
469 | |
|
470 | 0 | if (cancellable) |
471 | 0 | g_cancellable_pop_current (cancellable); |
472 | |
|
473 | 0 | g_io_stream_clear_pending (io_stream); |
474 | |
|
475 | 0 | return res; |
476 | 0 | } |
477 | | |
478 | | static gboolean |
479 | | g_file_io_stream_seekable_truncate (GSeekable *seekable, |
480 | | goffset size, |
481 | | GCancellable *cancellable, |
482 | | GError **error) |
483 | 0 | { |
484 | 0 | return g_file_io_stream_truncate (G_FILE_IO_STREAM (seekable), |
485 | 0 | size, cancellable, error); |
486 | 0 | } |
487 | | /***************************************************** |
488 | | * Default implementations based on output stream * |
489 | | *****************************************************/ |
490 | | |
491 | | static goffset |
492 | | g_file_io_stream_real_tell (GFileIOStream *stream) |
493 | 0 | { |
494 | 0 | GOutputStream *out; |
495 | 0 | GSeekable *seekable; |
496 | |
|
497 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
498 | 0 | seekable = G_SEEKABLE (out); |
499 | |
|
500 | 0 | return g_seekable_tell (seekable); |
501 | 0 | } |
502 | | |
503 | | static gboolean |
504 | | g_file_io_stream_real_can_seek (GFileIOStream *stream) |
505 | 0 | { |
506 | 0 | GOutputStream *out; |
507 | 0 | GSeekable *seekable; |
508 | |
|
509 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
510 | 0 | seekable = G_SEEKABLE (out); |
511 | |
|
512 | 0 | return g_seekable_can_seek (seekable); |
513 | 0 | } |
514 | | |
515 | | static gboolean |
516 | | g_file_io_stream_real_seek (GFileIOStream *stream, |
517 | | goffset offset, |
518 | | GSeekType type, |
519 | | GCancellable *cancellable, |
520 | | GError **error) |
521 | 0 | { |
522 | 0 | GOutputStream *out; |
523 | 0 | GSeekable *seekable; |
524 | |
|
525 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
526 | 0 | seekable = G_SEEKABLE (out); |
527 | |
|
528 | 0 | return g_seekable_seek (seekable, offset, type, cancellable, error); |
529 | 0 | } |
530 | | |
531 | | static gboolean |
532 | | g_file_io_stream_real_can_truncate (GFileIOStream *stream) |
533 | 0 | { |
534 | 0 | GOutputStream *out; |
535 | 0 | GSeekable *seekable; |
536 | |
|
537 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
538 | 0 | seekable = G_SEEKABLE (out); |
539 | |
|
540 | 0 | return g_seekable_can_truncate (seekable); |
541 | 0 | } |
542 | | |
543 | | static gboolean |
544 | | g_file_io_stream_real_truncate_fn (GFileIOStream *stream, |
545 | | goffset size, |
546 | | GCancellable *cancellable, |
547 | | GError **error) |
548 | 0 | { |
549 | 0 | GOutputStream *out; |
550 | 0 | GSeekable *seekable; |
551 | |
|
552 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
553 | 0 | seekable = G_SEEKABLE (out); |
554 | |
|
555 | 0 | return g_seekable_truncate (seekable, size, cancellable, error); |
556 | 0 | } |
557 | | |
558 | | static char * |
559 | | g_file_io_stream_real_get_etag (GFileIOStream *stream) |
560 | 0 | { |
561 | 0 | GOutputStream *out; |
562 | 0 | GFileOutputStream *file_out; |
563 | |
|
564 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
565 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
566 | |
|
567 | 0 | return g_file_output_stream_get_etag (file_out); |
568 | 0 | } |
569 | | |
570 | | static GFileInfo * |
571 | | g_file_io_stream_real_query_info (GFileIOStream *stream, |
572 | | const char *attributes, |
573 | | GCancellable *cancellable, |
574 | | GError **error) |
575 | 0 | { |
576 | 0 | GOutputStream *out; |
577 | 0 | GFileOutputStream *file_out; |
578 | |
|
579 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
580 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
581 | |
|
582 | 0 | return g_file_output_stream_query_info (file_out, |
583 | 0 | attributes, cancellable, error); |
584 | 0 | } |
585 | | |
586 | | typedef struct { |
587 | | GObject *object; |
588 | | GAsyncReadyCallback callback; |
589 | | gpointer user_data; |
590 | | } AsyncOpWrapper; |
591 | | |
592 | | static AsyncOpWrapper * |
593 | | async_op_wrapper_new (gpointer object, |
594 | | GAsyncReadyCallback callback, |
595 | | gpointer user_data) |
596 | 0 | { |
597 | 0 | AsyncOpWrapper *data; |
598 | |
|
599 | 0 | data = g_new0 (AsyncOpWrapper, 1); |
600 | 0 | data->object = g_object_ref (object); |
601 | 0 | data->callback = callback; |
602 | 0 | data->user_data = user_data; |
603 | |
|
604 | 0 | return data; |
605 | 0 | } |
606 | | |
607 | | static void |
608 | | async_op_wrapper_callback (GObject *source_object, |
609 | | GAsyncResult *res, |
610 | | gpointer user_data) |
611 | 0 | { |
612 | 0 | AsyncOpWrapper *data = user_data; |
613 | 0 | data->callback (data->object, res, data->user_data); |
614 | 0 | g_object_unref (data->object); |
615 | 0 | g_free (data); |
616 | 0 | } |
617 | | |
618 | | static void |
619 | | g_file_io_stream_real_query_info_async (GFileIOStream *stream, |
620 | | const char *attributes, |
621 | | int io_priority, |
622 | | GCancellable *cancellable, |
623 | | GAsyncReadyCallback callback, |
624 | | gpointer user_data) |
625 | 0 | { |
626 | 0 | GOutputStream *out; |
627 | 0 | GFileOutputStream *file_out; |
628 | 0 | AsyncOpWrapper *data; |
629 | |
|
630 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
631 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
632 | |
|
633 | 0 | data = async_op_wrapper_new (stream, callback, user_data); |
634 | 0 | g_file_output_stream_query_info_async (file_out, |
635 | 0 | attributes, io_priority, |
636 | 0 | cancellable, async_op_wrapper_callback, data); |
637 | 0 | } |
638 | | |
639 | | static GFileInfo * |
640 | | g_file_io_stream_real_query_info_finish (GFileIOStream *stream, |
641 | | GAsyncResult *res, |
642 | | GError **error) |
643 | 0 | { |
644 | 0 | GOutputStream *out; |
645 | 0 | GFileOutputStream *file_out; |
646 | |
|
647 | 0 | out = g_io_stream_get_output_stream (G_IO_STREAM (stream)); |
648 | 0 | file_out = G_FILE_OUTPUT_STREAM (out); |
649 | |
|
650 | 0 | return g_file_output_stream_query_info_finish (file_out, res, error); |
651 | 0 | } |
652 | | |
653 | | static void |
654 | | g_file_io_stream_class_init (GFileIOStreamClass *klass) |
655 | 0 | { |
656 | 0 | klass->tell = g_file_io_stream_real_tell; |
657 | 0 | klass->can_seek = g_file_io_stream_real_can_seek; |
658 | 0 | klass->seek = g_file_io_stream_real_seek; |
659 | 0 | klass->can_truncate = g_file_io_stream_real_can_truncate; |
660 | 0 | klass->truncate_fn = g_file_io_stream_real_truncate_fn; |
661 | 0 | klass->query_info = g_file_io_stream_real_query_info; |
662 | 0 | klass->query_info_async = g_file_io_stream_real_query_info_async; |
663 | 0 | klass->query_info_finish = g_file_io_stream_real_query_info_finish; |
664 | 0 | klass->get_etag = g_file_io_stream_real_get_etag; |
665 | 0 | } |