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>
25 #include <sys/types.h>
31 #include <glib/gstdio.h>
32 #include "gcancellable.h"
34 #include "glocalfileinputstream.h"
35 #include "glocalfileinfo.h"
40 #include "glib-unix.h"
41 #include "gfiledescriptorbased.h"
48 struct _GLocalFileInputStreamPrivate {
54 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
57 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
59 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
60 G_ADD_PRIVATE (GLocalFileInputStream)
61 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
62 g_file_descriptor_based_iface_init))
64 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
65 G_ADD_PRIVATE (GLocalFileInputStream))
68 static gssize g_local_file_input_stream_read (GInputStream *stream,
71 GCancellable *cancellable,
73 static gboolean g_local_file_input_stream_close (GInputStream *stream,
74 GCancellable *cancellable,
76 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
77 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
78 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
81 GCancellable *cancellable,
83 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
84 const char *attributes,
85 GCancellable *cancellable,
88 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
92 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
95 in->priv->do_close = do_close;
99 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
101 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
102 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
104 stream_class->read_fn = g_local_file_input_stream_read;
105 stream_class->close_fn = g_local_file_input_stream_close;
106 file_stream_class->tell = g_local_file_input_stream_tell;
107 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
108 file_stream_class->seek = g_local_file_input_stream_seek;
109 file_stream_class->query_info = g_local_file_input_stream_query_info;
114 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
116 iface->get_fd = g_local_file_input_stream_get_fd;
121 g_local_file_input_stream_init (GLocalFileInputStream *info)
123 info->priv = g_local_file_input_stream_get_instance_private (info);
124 info->priv->do_close = TRUE;
128 _g_local_file_input_stream_new (int fd)
130 GLocalFileInputStream *stream;
132 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
133 stream->priv->fd = fd;
135 return G_FILE_INPUT_STREAM (stream);
139 g_local_file_input_stream_read (GInputStream *stream,
142 GCancellable *cancellable,
145 GLocalFileInputStream *file;
148 file = G_LOCAL_FILE_INPUT_STREAM (stream);
153 if (g_cancellable_set_error_if_cancelled (cancellable, error))
155 res = read (file->priv->fd, buffer, count);
163 g_set_error (error, G_IO_ERROR,
164 g_io_error_from_errno (errsv),
165 _("Error reading from file: %s"),
176 g_local_file_input_stream_close (GInputStream *stream,
177 GCancellable *cancellable,
180 GLocalFileInputStream *file;
182 file = G_LOCAL_FILE_INPUT_STREAM (stream);
184 if (!file->priv->do_close)
187 if (file->priv->fd == -1)
190 if (!g_close (file->priv->fd, NULL))
194 g_set_error (error, G_IO_ERROR,
195 g_io_error_from_errno (errsv),
196 _("Error closing file: %s"),
206 g_local_file_input_stream_tell (GFileInputStream *stream)
208 GLocalFileInputStream *file;
211 file = G_LOCAL_FILE_INPUT_STREAM (stream);
213 pos = lseek (file->priv->fd, 0, SEEK_CUR);
215 if (pos == (off_t)-1)
222 g_local_file_input_stream_can_seek (GFileInputStream *stream)
224 GLocalFileInputStream *file;
227 file = G_LOCAL_FILE_INPUT_STREAM (stream);
229 pos = lseek (file->priv->fd, 0, SEEK_CUR);
231 if (pos == (off_t)-1 && errno == ESPIPE)
238 seek_type_to_lseek (GSeekType type)
255 g_local_file_input_stream_seek (GFileInputStream *stream,
258 GCancellable *cancellable,
261 GLocalFileInputStream *file;
264 file = G_LOCAL_FILE_INPUT_STREAM (stream);
266 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
268 if (pos == (off_t)-1)
272 g_set_error (error, G_IO_ERROR,
273 g_io_error_from_errno (errsv),
274 _("Error seeking in file: %s"),
283 g_local_file_input_stream_query_info (GFileInputStream *stream,
284 const char *attributes,
285 GCancellable *cancellable,
288 GLocalFileInputStream *file;
290 file = G_LOCAL_FILE_INPUT_STREAM (stream);
292 if (g_cancellable_set_error_if_cancelled (cancellable, error))
295 return _g_local_file_info_get_from_fd (file->priv->fd,
302 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
304 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
305 return stream->priv->fd;