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