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"
39 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
41 struct _GLocalFileInputStreamPrivate {
45 static gssize g_local_file_input_stream_read (GInputStream *stream,
48 GCancellable *cancellable,
50 static gssize g_local_file_input_stream_skip (GInputStream *stream,
52 GCancellable *cancellable,
54 static gboolean g_local_file_input_stream_close (GInputStream *stream,
55 GCancellable *cancellable,
57 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
58 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
59 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
62 GCancellable *cancellable,
64 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
66 GCancellable *cancellable,
70 g_local_file_input_stream_finalize (GObject *object)
72 GLocalFileInputStream *file;
74 file = G_LOCAL_FILE_INPUT_STREAM (object);
76 if (G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize)
77 (*G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize) (object);
81 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
83 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
84 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
85 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
87 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
89 gobject_class->finalize = g_local_file_input_stream_finalize;
91 stream_class->read = g_local_file_input_stream_read;
92 stream_class->skip = g_local_file_input_stream_skip;
93 stream_class->close = g_local_file_input_stream_close;
94 file_stream_class->tell = g_local_file_input_stream_tell;
95 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
96 file_stream_class->seek = g_local_file_input_stream_seek;
97 file_stream_class->query_info = g_local_file_input_stream_query_info;
101 g_local_file_input_stream_init (GLocalFileInputStream *info)
103 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
104 G_TYPE_LOCAL_FILE_INPUT_STREAM,
105 GLocalFileInputStreamPrivate);
109 * g_local_file_input_stream_new:
110 * @fd: File Descriptor.
112 * Returns: #GFileInputStream for the given file descriptor.
115 g_local_file_input_stream_new (int fd)
117 GLocalFileInputStream *stream;
119 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
120 stream->priv->fd = fd;
122 return G_FILE_INPUT_STREAM (stream);
126 g_local_file_input_stream_read (GInputStream *stream,
129 GCancellable *cancellable,
132 GLocalFileInputStream *file;
135 file = G_LOCAL_FILE_INPUT_STREAM (stream);
140 if (g_cancellable_set_error_if_cancelled (cancellable, error))
142 res = read (file->priv->fd, buffer, count);
148 g_set_error (error, G_IO_ERROR,
149 g_io_error_from_errno (errno),
150 _("Error reading from file: %s"),
161 g_local_file_input_stream_skip (GInputStream *stream,
163 GCancellable *cancellable,
167 GLocalFileInputStream *file;
169 file = G_LOCAL_FILE_INPUT_STREAM (stream);
171 if (g_cancellable_set_error_if_cancelled (cancellable, error))
174 start = lseek (file->priv->fd, 0, SEEK_CUR);
177 g_set_error (error, G_IO_ERROR,
178 g_io_error_from_errno (errno),
179 _("Error seeking in file: %s"),
184 res = lseek (file->priv->fd, count, SEEK_CUR);
187 g_set_error (error, G_IO_ERROR,
188 g_io_error_from_errno (errno),
189 _("Error seeking in file: %s"),
198 g_local_file_input_stream_close (GInputStream *stream,
199 GCancellable *cancellable,
202 GLocalFileInputStream *file;
205 file = G_LOCAL_FILE_INPUT_STREAM (stream);
207 if (file->priv->fd == -1)
212 res = close (file->priv->fd);
215 g_set_error (error, G_IO_ERROR,
216 g_io_error_from_errno (errno),
217 _("Error closing file: %s"),
228 g_local_file_input_stream_tell (GFileInputStream *stream)
230 GLocalFileInputStream *file;
233 file = G_LOCAL_FILE_INPUT_STREAM (stream);
235 pos = lseek (file->priv->fd, 0, SEEK_CUR);
237 if (pos == (off_t)-1)
244 g_local_file_input_stream_can_seek (GFileInputStream *stream)
246 GLocalFileInputStream *file;
249 file = G_LOCAL_FILE_INPUT_STREAM (stream);
251 pos = lseek (file->priv->fd, 0, SEEK_CUR);
253 if (pos == (off_t)-1 &&
261 seek_type_to_lseek (GSeekType type)
278 g_local_file_input_stream_seek (GFileInputStream *stream,
281 GCancellable *cancellable,
284 GLocalFileInputStream *file;
287 file = G_LOCAL_FILE_INPUT_STREAM (stream);
289 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
291 if (pos == (off_t)-1)
293 g_set_error (error, G_IO_ERROR,
294 g_io_error_from_errno (errno),
295 _("Error seeking in file: %s"),
304 g_local_file_input_stream_query_info (GFileInputStream *stream,
306 GCancellable *cancellable,
309 GLocalFileInputStream *file;
311 file = G_LOCAL_FILE_INPUT_STREAM (stream);
313 if (g_cancellable_set_error_if_cancelled (cancellable, error))
316 return _g_local_file_info_get_from_fd (file->priv->fd,