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>
23 #include <sys/types.h>
29 #include <glib/gstdio.h>
30 #include "gcancellable.h"
32 #include "glocalfileinputstream.h"
33 #include "glocalfileinfo.h"
38 #include "glib-unix.h"
39 #include "gfiledescriptorbased.h"
46 struct _GLocalFileInputStreamPrivate {
52 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
55 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
57 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
58 G_ADD_PRIVATE (GLocalFileInputStream)
59 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
60 g_file_descriptor_based_iface_init))
62 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
63 G_ADD_PRIVATE (GLocalFileInputStream))
66 static gssize g_local_file_input_stream_read (GInputStream *stream,
69 GCancellable *cancellable,
71 static gssize g_local_file_input_stream_skip (GInputStream *stream,
73 GCancellable *cancellable,
75 static gboolean g_local_file_input_stream_close (GInputStream *stream,
76 GCancellable *cancellable,
78 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
79 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
80 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
83 GCancellable *cancellable,
85 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
86 const char *attributes,
87 GCancellable *cancellable,
90 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
94 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
97 in->priv->do_close = do_close;
101 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
103 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
104 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
106 stream_class->read_fn = g_local_file_input_stream_read;
107 stream_class->skip = g_local_file_input_stream_skip;
108 stream_class->close_fn = g_local_file_input_stream_close;
109 file_stream_class->tell = g_local_file_input_stream_tell;
110 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
111 file_stream_class->seek = g_local_file_input_stream_seek;
112 file_stream_class->query_info = g_local_file_input_stream_query_info;
117 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
119 iface->get_fd = g_local_file_input_stream_get_fd;
124 g_local_file_input_stream_init (GLocalFileInputStream *info)
126 info->priv = g_local_file_input_stream_get_instance_private (info);
127 info->priv->do_close = TRUE;
131 _g_local_file_input_stream_new (int fd)
133 GLocalFileInputStream *stream;
135 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
136 stream->priv->fd = fd;
138 return G_FILE_INPUT_STREAM (stream);
142 g_local_file_input_stream_read (GInputStream *stream,
145 GCancellable *cancellable,
148 GLocalFileInputStream *file;
151 file = G_LOCAL_FILE_INPUT_STREAM (stream);
156 if (g_cancellable_set_error_if_cancelled (cancellable, error))
158 res = read (file->priv->fd, buffer, count);
166 g_set_error (error, G_IO_ERROR,
167 g_io_error_from_errno (errsv),
168 _("Error reading from file: %s"),
179 g_local_file_input_stream_skip (GInputStream *stream,
181 GCancellable *cancellable,
185 GLocalFileInputStream *file;
187 file = G_LOCAL_FILE_INPUT_STREAM (stream);
189 if (g_cancellable_set_error_if_cancelled (cancellable, error))
192 start = lseek (file->priv->fd, 0, SEEK_CUR);
197 g_set_error (error, G_IO_ERROR,
198 g_io_error_from_errno (errsv),
199 _("Error seeking in file: %s"),
204 end = lseek (file->priv->fd, 0, SEEK_END);
209 g_set_error (error, G_IO_ERROR,
210 g_io_error_from_errno (errsv),
211 _("Error seeking in file: %s"),
216 if (end - start > count)
218 end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
223 g_set_error (error, G_IO_ERROR,
224 g_io_error_from_errno (errsv),
225 _("Error seeking in file: %s"),
235 g_local_file_input_stream_close (GInputStream *stream,
236 GCancellable *cancellable,
239 GLocalFileInputStream *file;
241 file = G_LOCAL_FILE_INPUT_STREAM (stream);
243 if (!file->priv->do_close)
246 if (file->priv->fd == -1)
249 if (!g_close (file->priv->fd, NULL))
253 g_set_error (error, G_IO_ERROR,
254 g_io_error_from_errno (errsv),
255 _("Error closing file: %s"),
265 g_local_file_input_stream_tell (GFileInputStream *stream)
267 GLocalFileInputStream *file;
270 file = G_LOCAL_FILE_INPUT_STREAM (stream);
272 pos = lseek (file->priv->fd, 0, SEEK_CUR);
274 if (pos == (off_t)-1)
281 g_local_file_input_stream_can_seek (GFileInputStream *stream)
283 GLocalFileInputStream *file;
286 file = G_LOCAL_FILE_INPUT_STREAM (stream);
288 pos = lseek (file->priv->fd, 0, SEEK_CUR);
290 if (pos == (off_t)-1 && errno == ESPIPE)
297 seek_type_to_lseek (GSeekType type)
314 g_local_file_input_stream_seek (GFileInputStream *stream,
317 GCancellable *cancellable,
320 GLocalFileInputStream *file;
323 file = G_LOCAL_FILE_INPUT_STREAM (stream);
325 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
327 if (pos == (off_t)-1)
331 g_set_error (error, G_IO_ERROR,
332 g_io_error_from_errno (errsv),
333 _("Error seeking in file: %s"),
342 g_local_file_input_stream_query_info (GFileInputStream *stream,
343 const char *attributes,
344 GCancellable *cancellable,
347 GLocalFileInputStream *file;
349 file = G_LOCAL_FILE_INPUT_STREAM (stream);
351 if (g_cancellable_set_error_if_cancelled (cancellable, error))
354 return _g_local_file_info_get_from_fd (file->priv->fd,
361 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
363 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
364 return stream->priv->fd;