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>
32 #include <glib/gstdio.h>
34 #include "glocalfileinputstream.h"
35 #include "glocalfileinfo.h"
40 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
41 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
43 struct _GLocalFileInputStreamPrivate {
47 static gssize g_local_file_input_stream_read (GInputStream *stream,
50 GCancellable *cancellable,
52 static gssize g_local_file_input_stream_skip (GInputStream *stream,
54 GCancellable *cancellable,
56 static gboolean g_local_file_input_stream_close (GInputStream *stream,
57 GCancellable *cancellable,
59 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
60 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
61 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
64 GCancellable *cancellable,
66 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
68 GCancellable *cancellable,
72 g_local_file_input_stream_finalize (GObject *object)
74 GLocalFileInputStream *file;
76 file = G_LOCAL_FILE_INPUT_STREAM (object);
78 if (G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize)
79 (*G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize) (object);
83 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
85 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
87 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
89 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
91 gobject_class->finalize = g_local_file_input_stream_finalize;
93 stream_class->read = g_local_file_input_stream_read;
94 stream_class->skip = g_local_file_input_stream_skip;
95 stream_class->close = g_local_file_input_stream_close;
96 file_stream_class->tell = g_local_file_input_stream_tell;
97 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
98 file_stream_class->seek = g_local_file_input_stream_seek;
99 file_stream_class->query_info = g_local_file_input_stream_query_info;
103 g_local_file_input_stream_init (GLocalFileInputStream *info)
105 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
106 G_TYPE_LOCAL_FILE_INPUT_STREAM,
107 GLocalFileInputStreamPrivate);
111 * g_local_file_input_stream_new:
112 * @fd: File Descriptor.
114 * Returns: #GFileInputStream for the given file descriptor.
117 _g_local_file_input_stream_new (int fd)
119 GLocalFileInputStream *stream;
121 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
122 stream->priv->fd = fd;
124 return G_FILE_INPUT_STREAM (stream);
128 g_local_file_input_stream_read (GInputStream *stream,
131 GCancellable *cancellable,
134 GLocalFileInputStream *file;
137 file = G_LOCAL_FILE_INPUT_STREAM (stream);
142 if (g_cancellable_set_error_if_cancelled (cancellable, error))
144 res = read (file->priv->fd, buffer, count);
150 g_set_error (error, G_IO_ERROR,
151 g_io_error_from_errno (errno),
152 _("Error reading from file: %s"),
163 g_local_file_input_stream_skip (GInputStream *stream,
165 GCancellable *cancellable,
169 GLocalFileInputStream *file;
171 file = G_LOCAL_FILE_INPUT_STREAM (stream);
173 if (g_cancellable_set_error_if_cancelled (cancellable, error))
176 start = lseek (file->priv->fd, 0, SEEK_CUR);
179 g_set_error (error, G_IO_ERROR,
180 g_io_error_from_errno (errno),
181 _("Error seeking in file: %s"),
186 res = lseek (file->priv->fd, count, SEEK_CUR);
189 g_set_error (error, G_IO_ERROR,
190 g_io_error_from_errno (errno),
191 _("Error seeking in file: %s"),
200 g_local_file_input_stream_close (GInputStream *stream,
201 GCancellable *cancellable,
204 GLocalFileInputStream *file;
207 file = G_LOCAL_FILE_INPUT_STREAM (stream);
209 if (file->priv->fd == -1)
214 res = close (file->priv->fd);
217 g_set_error (error, G_IO_ERROR,
218 g_io_error_from_errno (errno),
219 _("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,