1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gseekable.h"
30 * @short_description: Stream seeking interface
32 * @see_also: #GInputStream, #GOutputStream
34 * #GSeekable is implemented by streams (implementations of
35 * #GInputStream or #GOutputStream) that support seeking.
37 * Seekable streams largely fall into two categories: resizable and
40 * #GSeekable on fixed-sized streams is approximately the same as POSIX
41 * lseek() on a block device (for example: attempting to seek past the
42 * end of the device is an error). Fixed streams typically cannot be
45 * #GSeekable on resizable streams is approximately the same as POSIX
46 * lseek() on a normal file. Seeking past the end and writing data will
47 * usually cause the stream to resize by introducing zero bytes.
50 typedef GSeekableIface GSeekableInterface;
51 G_DEFINE_INTERFACE (GSeekable, g_seekable, G_TYPE_OBJECT)
54 g_seekable_default_init (GSeekableInterface *iface)
60 * @seekable: a #GSeekable.
62 * Tells the current position within the stream.
64 * Returns: the (positive or zero) offset from the beginning of the
65 * buffer, zero if the target is not seekable.
68 g_seekable_tell (GSeekable *seekable)
70 GSeekableIface *iface;
72 g_return_val_if_fail (G_IS_SEEKABLE (seekable), 0);
74 iface = G_SEEKABLE_GET_IFACE (seekable);
76 return (* iface->tell) (seekable);
80 * g_seekable_can_seek:
81 * @seekable: a #GSeekable.
83 * Tests if the stream supports the #GSeekableIface.
85 * Returns: %TRUE if @seekable can be seeked. %FALSE otherwise.
88 g_seekable_can_seek (GSeekable *seekable)
90 GSeekableIface *iface;
92 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
94 iface = G_SEEKABLE_GET_IFACE (seekable);
96 return (* iface->can_seek) (seekable);
101 * @seekable: a #GSeekable.
102 * @offset: a #goffset.
103 * @type: a #GSeekType.
104 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
105 * @error: a #GError location to store the error occurring, or %NULL to
108 * Seeks in the stream by the given @offset, modified by @type.
110 * Attempting to seek past the end of the stream will have different
111 * results depending on if the stream is fixed-sized or resizable. If
112 * the stream is resizable then seeking past the end and then writing
113 * will result in zeros filling the empty space. Seeking past the end
114 * of a resizable stream and reading will result in EOF. Seeking past
115 * the end of a fixed-sized stream will fail.
117 * Any operation that would result in a negative offset will fail.
119 * If @cancellable is not %NULL, then the operation can be cancelled by
120 * triggering the cancellable object from another thread. If the operation
121 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
123 * Returns: %TRUE if successful. If an error
124 * has occurred, this function will return %FALSE and set @error
125 * appropriately if present.
128 g_seekable_seek (GSeekable *seekable,
131 GCancellable *cancellable,
134 GSeekableIface *iface;
136 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
138 iface = G_SEEKABLE_GET_IFACE (seekable);
140 return (* iface->seek) (seekable, offset, type, cancellable, error);
144 * g_seekable_can_truncate:
145 * @seekable: a #GSeekable.
147 * Tests if the length of the stream can be adjusted with
148 * g_seekable_truncate().
150 * Returns: %TRUE if the stream can be truncated, %FALSE otherwise.
153 g_seekable_can_truncate (GSeekable *seekable)
155 GSeekableIface *iface;
157 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
159 iface = G_SEEKABLE_GET_IFACE (seekable);
161 return (* iface->can_truncate) (seekable);
165 * g_seekable_truncate: (virtual truncate_fn)
166 * @seekable: a #GSeekable.
167 * @offset: new length for @seekable, in bytes.
168 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
169 * @error: a #GError location to store the error occurring, or %NULL to
172 * Sets the length of the stream to @offset. If the stream was previously
173 * larger than @offset, the extra data is discarded. If the stream was
174 * previously shorter than @offset, it is extended with NUL ('\0') bytes.
176 * If @cancellable is not %NULL, then the operation can be cancelled by
177 * triggering the cancellable object from another thread. If the operation
178 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
179 * operation was partially finished when the operation was cancelled the
180 * partial result will be returned, without an error.
182 * Returns: %TRUE if successful. If an error
183 * has occurred, this function will return %FALSE and set @error
184 * appropriately if present.
187 g_seekable_truncate (GSeekable *seekable,
189 GCancellable *cancellable,
192 GSeekableIface *iface;
194 g_return_val_if_fail (G_IS_SEEKABLE (seekable), FALSE);
196 iface = G_SEEKABLE_GET_IFACE (seekable);
198 return (* iface->truncate_fn) (seekable, offset, cancellable, error);