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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
34 #include <glib/gstdio.h>
35 #include "gcancellable.h"
37 #include "glocalfileinputstream.h"
38 #include "glocalfileinfo.h"
42 #include "gfiledescriptorbased.h"
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_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
59 g_file_descriptor_based_iface_init)
62 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,);
65 struct _GLocalFileInputStreamPrivate {
70 static gssize g_local_file_input_stream_read (GInputStream *stream,
73 GCancellable *cancellable,
75 static gssize g_local_file_input_stream_skip (GInputStream *stream,
77 GCancellable *cancellable,
79 static gboolean g_local_file_input_stream_close (GInputStream *stream,
80 GCancellable *cancellable,
82 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
83 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
84 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
87 GCancellable *cancellable,
89 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
90 const char *attributes,
91 GCancellable *cancellable,
94 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
98 g_local_file_input_stream_finalize (GObject *object)
100 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
104 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
107 in->priv->do_close = do_close;
111 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
117 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
119 gobject_class->finalize = g_local_file_input_stream_finalize;
121 stream_class->read_fn = g_local_file_input_stream_read;
122 stream_class->skip = g_local_file_input_stream_skip;
123 stream_class->close_fn = g_local_file_input_stream_close;
124 file_stream_class->tell = g_local_file_input_stream_tell;
125 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
126 file_stream_class->seek = g_local_file_input_stream_seek;
127 file_stream_class->query_info = g_local_file_input_stream_query_info;
132 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
134 iface->get_fd = g_local_file_input_stream_get_fd;
139 g_local_file_input_stream_init (GLocalFileInputStream *info)
141 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
142 G_TYPE_LOCAL_FILE_INPUT_STREAM,
143 GLocalFileInputStreamPrivate);
144 info->priv->do_close = TRUE;
148 _g_local_file_input_stream_new (int fd)
150 GLocalFileInputStream *stream;
152 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
153 stream->priv->fd = fd;
155 return G_FILE_INPUT_STREAM (stream);
159 g_local_file_input_stream_read (GInputStream *stream,
162 GCancellable *cancellable,
165 GLocalFileInputStream *file;
168 file = G_LOCAL_FILE_INPUT_STREAM (stream);
173 if (g_cancellable_set_error_if_cancelled (cancellable, error))
175 res = read (file->priv->fd, buffer, count);
183 g_set_error (error, G_IO_ERROR,
184 g_io_error_from_errno (errsv),
185 _("Error reading from file: %s"),
196 g_local_file_input_stream_skip (GInputStream *stream,
198 GCancellable *cancellable,
202 GLocalFileInputStream *file;
204 file = G_LOCAL_FILE_INPUT_STREAM (stream);
206 if (g_cancellable_set_error_if_cancelled (cancellable, error))
209 start = lseek (file->priv->fd, 0, SEEK_CUR);
214 g_set_error (error, G_IO_ERROR,
215 g_io_error_from_errno (errsv),
216 _("Error seeking in file: %s"),
221 res = lseek (file->priv->fd, count, SEEK_CUR);
226 g_set_error (error, G_IO_ERROR,
227 g_io_error_from_errno (errsv),
228 _("Error seeking in file: %s"),
237 g_local_file_input_stream_close (GInputStream *stream,
238 GCancellable *cancellable,
241 GLocalFileInputStream *file;
244 file = G_LOCAL_FILE_INPUT_STREAM (stream);
246 if (!file->priv->do_close)
249 if (file->priv->fd == -1)
254 res = close (file->priv->fd);
259 g_set_error (error, G_IO_ERROR,
260 g_io_error_from_errno (errsv),
261 _("Error closing file: %s"),
272 g_local_file_input_stream_tell (GFileInputStream *stream)
274 GLocalFileInputStream *file;
277 file = G_LOCAL_FILE_INPUT_STREAM (stream);
279 pos = lseek (file->priv->fd, 0, SEEK_CUR);
281 if (pos == (off_t)-1)
288 g_local_file_input_stream_can_seek (GFileInputStream *stream)
290 GLocalFileInputStream *file;
293 file = G_LOCAL_FILE_INPUT_STREAM (stream);
295 pos = lseek (file->priv->fd, 0, SEEK_CUR);
297 if (pos == (off_t)-1 && errno == ESPIPE)
304 seek_type_to_lseek (GSeekType type)
321 g_local_file_input_stream_seek (GFileInputStream *stream,
324 GCancellable *cancellable,
327 GLocalFileInputStream *file;
330 file = G_LOCAL_FILE_INPUT_STREAM (stream);
332 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
334 if (pos == (off_t)-1)
338 g_set_error (error, G_IO_ERROR,
339 g_io_error_from_errno (errsv),
340 _("Error seeking in file: %s"),
349 g_local_file_input_stream_query_info (GFileInputStream *stream,
350 const char *attributes,
351 GCancellable *cancellable,
354 GLocalFileInputStream *file;
356 file = G_LOCAL_FILE_INPUT_STREAM (stream);
358 if (g_cancellable_set_error_if_cancelled (cancellable, error))
361 return _g_local_file_info_get_from_fd (file->priv->fd,
368 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
370 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
371 return stream->priv->fd;