1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include <gfileinputstream.h>
25 #include <gseekable.h>
26 #include "gcancellable.h"
27 #include "gasyncresult.h"
34 * SECTION:gfileinputstream
35 * @short_description: File input streaming operations
37 * @see_also: #GInputStream, #GDataInputStream, #GSeekable
39 * GFileInputStream provides input streams that take their
40 * content from a file.
42 * GFileInputStream implements #GSeekable, which allows the input
43 * stream to jump to arbitrary positions in the file, provided the
44 * filesystem of the file allows it. To find the position of a file
45 * input stream, use g_seekable_tell(). To find out if a file input
46 * stream supports seeking, use g_seekable_can_seek().
47 * To position a file input stream, use g_seekable_seek().
50 static void g_file_input_stream_seekable_iface_init (GSeekableIface
*iface
);
51 static goffset
g_file_input_stream_seekable_tell (GSeekable
*seekable
);
52 static gboolean
g_file_input_stream_seekable_can_seek (GSeekable
*seekable
);
53 static gboolean
g_file_input_stream_seekable_seek (GSeekable
*seekable
,
56 GCancellable
*cancellable
,
58 static gboolean
g_file_input_stream_seekable_can_truncate (GSeekable
*seekable
);
59 static gboolean
g_file_input_stream_seekable_truncate (GSeekable
*seekable
,
61 GCancellable
*cancellable
,
63 static void g_file_input_stream_real_query_info_async (GFileInputStream
*stream
,
64 const char *attributes
,
66 GCancellable
*cancellable
,
67 GAsyncReadyCallback callback
,
69 static GFileInfo
*g_file_input_stream_real_query_info_finish (GFileInputStream
*stream
,
74 struct _GFileInputStreamPrivate
{
75 GAsyncReadyCallback outstanding_callback
;
78 G_DEFINE_TYPE_WITH_CODE (GFileInputStream
, g_file_input_stream
, G_TYPE_INPUT_STREAM
,
79 G_ADD_PRIVATE (GFileInputStream
)
80 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE
,
81 g_file_input_stream_seekable_iface_init
))
84 g_file_input_stream_class_init (GFileInputStreamClass
*klass
)
86 klass
->query_info_async
= g_file_input_stream_real_query_info_async
;
87 klass
->query_info_finish
= g_file_input_stream_real_query_info_finish
;
91 g_file_input_stream_seekable_iface_init (GSeekableIface
*iface
)
93 iface
->tell
= g_file_input_stream_seekable_tell
;
94 iface
->can_seek
= g_file_input_stream_seekable_can_seek
;
95 iface
->seek
= g_file_input_stream_seekable_seek
;
96 iface
->can_truncate
= g_file_input_stream_seekable_can_truncate
;
97 iface
->truncate_fn
= g_file_input_stream_seekable_truncate
;
101 g_file_input_stream_init (GFileInputStream
*stream
)
103 stream
->priv
= g_file_input_stream_get_instance_private (stream
);
107 * g_file_input_stream_query_info:
108 * @stream: a #GFileInputStream.
109 * @attributes: a file attribute query string.
110 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
111 * @error: a #GError location to store the error occurring, or %NULL to
114 * Queries a file input stream the given @attributes. This function blocks
115 * while querying the stream. For the asynchronous (non-blocking) version
116 * of this function, see g_file_input_stream_query_info_async(). While the
117 * stream is blocked, the stream will set the pending flag internally, and
118 * any other operations on the stream will fail with %G_IO_ERROR_PENDING.
120 * Returns: (transfer full): a #GFileInfo, or %NULL on error.
123 g_file_input_stream_query_info (GFileInputStream
*stream
,
124 const char *attributes
,
125 GCancellable
*cancellable
,
128 GFileInputStreamClass
*class;
129 GInputStream
*input_stream
;
132 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream
), NULL
);
134 input_stream
= G_INPUT_STREAM (stream
);
136 if (!g_input_stream_set_pending (input_stream
, error
))
142 g_cancellable_push_current (cancellable
);
144 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
145 if (class->query_info
)
146 info
= class->query_info (stream
, attributes
, cancellable
, error
);
148 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
149 _("Stream doesn’t support query_info"));
152 g_cancellable_pop_current (cancellable
);
154 g_input_stream_clear_pending (input_stream
);
160 async_ready_callback_wrapper (GObject
*source_object
,
164 GFileInputStream
*stream
= G_FILE_INPUT_STREAM (source_object
);
166 g_input_stream_clear_pending (G_INPUT_STREAM (stream
));
167 if (stream
->priv
->outstanding_callback
)
168 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
169 g_object_unref (stream
);
173 * g_file_input_stream_query_info_async:
174 * @stream: a #GFileInputStream.
175 * @attributes: a file attribute query string.
176 * @io_priority: the [I/O priority][io-priority] of the request
177 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
178 * @callback: (scope async): callback to call when the request is satisfied
179 * @user_data: (closure): the data to pass to callback function
181 * Queries the stream information asynchronously.
182 * When the operation is finished @callback will be called.
183 * You can then call g_file_input_stream_query_info_finish()
184 * to get the result of the operation.
186 * For the synchronous version of this function,
187 * see g_file_input_stream_query_info().
189 * If @cancellable is not %NULL, then the operation can be cancelled by
190 * triggering the cancellable object from another thread. If the operation
191 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set
195 g_file_input_stream_query_info_async (GFileInputStream
*stream
,
196 const char *attributes
,
198 GCancellable
*cancellable
,
199 GAsyncReadyCallback callback
,
202 GFileInputStreamClass
*klass
;
203 GInputStream
*input_stream
;
204 GError
*error
= NULL
;
206 g_return_if_fail (G_IS_FILE_INPUT_STREAM (stream
));
208 input_stream
= G_INPUT_STREAM (stream
);
210 if (!g_input_stream_set_pending (input_stream
, &error
))
212 g_task_report_error (stream
, callback
, user_data
,
213 g_file_input_stream_query_info_async
,
218 klass
= G_FILE_INPUT_STREAM_GET_CLASS (stream
);
220 stream
->priv
->outstanding_callback
= callback
;
221 g_object_ref (stream
);
222 klass
->query_info_async (stream
, attributes
, io_priority
, cancellable
,
223 async_ready_callback_wrapper
, user_data
);
227 * g_file_input_stream_query_info_finish:
228 * @stream: a #GFileInputStream.
229 * @result: a #GAsyncResult.
230 * @error: a #GError location to store the error occurring,
231 * or %NULL to ignore.
233 * Finishes an asynchronous info query operation.
235 * Returns: (transfer full): #GFileInfo.
238 g_file_input_stream_query_info_finish (GFileInputStream
*stream
,
239 GAsyncResult
*result
,
242 GFileInputStreamClass
*class;
244 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream
), NULL
);
245 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), NULL
);
247 if (g_async_result_legacy_propagate_error (result
, error
))
249 else if (g_async_result_is_tagged (result
, g_file_input_stream_query_info_async
))
250 return g_task_propagate_pointer (G_TASK (result
), error
);
252 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
253 return class->query_info_finish (stream
, result
, error
);
257 g_file_input_stream_tell (GFileInputStream
*stream
)
259 GFileInputStreamClass
*class;
262 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream
), 0);
264 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
268 offset
= class->tell (stream
);
274 g_file_input_stream_seekable_tell (GSeekable
*seekable
)
276 return g_file_input_stream_tell (G_FILE_INPUT_STREAM (seekable
));
280 g_file_input_stream_can_seek (GFileInputStream
*stream
)
282 GFileInputStreamClass
*class;
285 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream
), FALSE
);
287 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
294 can_seek
= class->can_seek (stream
);
301 g_file_input_stream_seekable_can_seek (GSeekable
*seekable
)
303 return g_file_input_stream_can_seek (G_FILE_INPUT_STREAM (seekable
));
307 g_file_input_stream_seek (GFileInputStream
*stream
,
310 GCancellable
*cancellable
,
313 GFileInputStreamClass
*class;
314 GInputStream
*input_stream
;
317 g_return_val_if_fail (G_IS_FILE_INPUT_STREAM (stream
), FALSE
);
319 input_stream
= G_INPUT_STREAM (stream
);
320 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
324 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
325 _("Seek not supported on stream"));
329 if (!g_input_stream_set_pending (input_stream
, error
))
333 g_cancellable_push_current (cancellable
);
335 res
= class->seek (stream
, offset
, type
, cancellable
, error
);
338 g_cancellable_pop_current (cancellable
);
340 g_input_stream_clear_pending (input_stream
);
346 g_file_input_stream_seekable_seek (GSeekable
*seekable
,
349 GCancellable
*cancellable
,
352 return g_file_input_stream_seek (G_FILE_INPUT_STREAM (seekable
),
353 offset
, type
, cancellable
, error
);
357 g_file_input_stream_seekable_can_truncate (GSeekable
*seekable
)
363 g_file_input_stream_seekable_truncate (GSeekable
*seekable
,
365 GCancellable
*cancellable
,
368 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
369 _("Truncate not allowed on input stream"));
373 /********************************************
374 * Default implementation of async ops *
375 ********************************************/
378 query_info_async_thread (GTask
*task
,
379 gpointer source_object
,
381 GCancellable
*cancellable
)
383 GFileInputStream
*stream
= source_object
;
384 const char *attributes
= task_data
;
385 GFileInputStreamClass
*class;
386 GError
*error
= NULL
;
387 GFileInfo
*info
= NULL
;
389 class = G_FILE_INPUT_STREAM_GET_CLASS (stream
);
390 if (class->query_info
)
391 info
= class->query_info (stream
, attributes
, cancellable
, &error
);
393 g_set_error_literal (&error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
394 _("Stream doesn’t support query_info"));
397 g_task_return_error (task
, error
);
399 g_task_return_pointer (task
, info
, g_object_unref
);
403 g_file_input_stream_real_query_info_async (GFileInputStream
*stream
,
404 const char *attributes
,
406 GCancellable
*cancellable
,
407 GAsyncReadyCallback callback
,
412 task
= g_task_new (stream
, cancellable
, callback
, user_data
);
413 g_task_set_source_tag (task
, g_file_input_stream_real_query_info_async
);
414 g_task_set_task_data (task
, g_strdup (attributes
), g_free
);
415 g_task_set_priority (task
, io_priority
);
417 g_task_run_in_thread (task
, query_info_async_thread
);
418 g_object_unref (task
);
422 g_file_input_stream_real_query_info_finish (GFileInputStream
*stream
,
426 g_return_val_if_fail (g_task_is_valid (res
, stream
), NULL
);
428 return g_task_propagate_pointer (G_TASK (res
), error
);