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>
36 #include "glocalfileinputstream.h"
37 #include "glocalfileinfo.h"
42 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
43 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
45 struct _GLocalFileInputStreamPrivate {
49 static gssize g_local_file_input_stream_read (GInputStream *stream,
52 GCancellable *cancellable,
54 static gssize g_local_file_input_stream_skip (GInputStream *stream,
56 GCancellable *cancellable,
58 static gboolean g_local_file_input_stream_close (GInputStream *stream,
59 GCancellable *cancellable,
61 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
62 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
63 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
66 GCancellable *cancellable,
68 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
70 GCancellable *cancellable,
74 g_local_file_input_stream_finalize (GObject *object)
76 GLocalFileInputStream *file;
78 file = G_LOCAL_FILE_INPUT_STREAM (object);
80 if (G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize)
81 (*G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize) (object);
85 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
89 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
91 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
93 gobject_class->finalize = g_local_file_input_stream_finalize;
95 stream_class->read = g_local_file_input_stream_read;
96 stream_class->skip = g_local_file_input_stream_skip;
97 stream_class->close = g_local_file_input_stream_close;
98 file_stream_class->tell = g_local_file_input_stream_tell;
99 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
100 file_stream_class->seek = g_local_file_input_stream_seek;
101 file_stream_class->query_info = g_local_file_input_stream_query_info;
105 g_local_file_input_stream_init (GLocalFileInputStream *info)
107 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
108 G_TYPE_LOCAL_FILE_INPUT_STREAM,
109 GLocalFileInputStreamPrivate);
113 * g_local_file_input_stream_new:
114 * @fd: File Descriptor.
116 * Returns: #GFileInputStream for the given file descriptor.
119 _g_local_file_input_stream_new (int fd)
121 GLocalFileInputStream *stream;
123 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
124 stream->priv->fd = fd;
126 return G_FILE_INPUT_STREAM (stream);
130 g_local_file_input_stream_read (GInputStream *stream,
133 GCancellable *cancellable,
136 GLocalFileInputStream *file;
139 file = G_LOCAL_FILE_INPUT_STREAM (stream);
144 if (g_cancellable_set_error_if_cancelled (cancellable, error))
146 res = read (file->priv->fd, buffer, count);
152 g_set_error (error, G_IO_ERROR,
153 g_io_error_from_errno (errno),
154 _("Error reading from file: %s"),
165 g_local_file_input_stream_skip (GInputStream *stream,
167 GCancellable *cancellable,
171 GLocalFileInputStream *file;
173 file = G_LOCAL_FILE_INPUT_STREAM (stream);
175 if (g_cancellable_set_error_if_cancelled (cancellable, error))
178 start = lseek (file->priv->fd, 0, SEEK_CUR);
181 g_set_error (error, G_IO_ERROR,
182 g_io_error_from_errno (errno),
183 _("Error seeking in file: %s"),
188 res = lseek (file->priv->fd, count, SEEK_CUR);
191 g_set_error (error, G_IO_ERROR,
192 g_io_error_from_errno (errno),
193 _("Error seeking in file: %s"),
202 g_local_file_input_stream_close (GInputStream *stream,
203 GCancellable *cancellable,
206 GLocalFileInputStream *file;
209 file = G_LOCAL_FILE_INPUT_STREAM (stream);
211 if (file->priv->fd == -1)
216 res = close (file->priv->fd);
218 g_set_error (error, G_IO_ERROR,
219 g_io_error_from_errno (errno),
220 _("Error closing file: %s"),
230 g_local_file_input_stream_tell (GFileInputStream *stream)
232 GLocalFileInputStream *file;
235 file = G_LOCAL_FILE_INPUT_STREAM (stream);
237 pos = lseek (file->priv->fd, 0, SEEK_CUR);
239 if (pos == (off_t)-1)
246 g_local_file_input_stream_can_seek (GFileInputStream *stream)
248 GLocalFileInputStream *file;
251 file = G_LOCAL_FILE_INPUT_STREAM (stream);
253 pos = lseek (file->priv->fd, 0, SEEK_CUR);
255 if (pos == (off_t)-1 &&
263 seek_type_to_lseek (GSeekType type)
280 g_local_file_input_stream_seek (GFileInputStream *stream,
283 GCancellable *cancellable,
286 GLocalFileInputStream *file;
289 file = G_LOCAL_FILE_INPUT_STREAM (stream);
291 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
293 if (pos == (off_t)-1)
295 g_set_error (error, G_IO_ERROR,
296 g_io_error_from_errno (errno),
297 _("Error seeking in file: %s"),
306 g_local_file_input_stream_query_info (GFileInputStream *stream,
308 GCancellable *cancellable,
311 GLocalFileInputStream *file;
313 file = G_LOCAL_FILE_INPUT_STREAM (stream);
315 if (g_cancellable_set_error_if_cancelled (cancellable, error))
318 return _g_local_file_info_get_from_fd (file->priv->fd,