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 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
42 struct _GLocalFileInputStreamPrivate {
46 static gssize g_local_file_input_stream_read (GInputStream *stream,
49 GCancellable *cancellable,
51 static gssize g_local_file_input_stream_skip (GInputStream *stream,
53 GCancellable *cancellable,
55 static gboolean g_local_file_input_stream_close (GInputStream *stream,
56 GCancellable *cancellable,
58 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
59 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
60 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
63 GCancellable *cancellable,
65 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
67 GCancellable *cancellable,
71 g_local_file_input_stream_finalize (GObject *object)
73 GLocalFileInputStream *file;
75 file = G_LOCAL_FILE_INPUT_STREAM (object);
77 if (G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize) (object);
82 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
86 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
88 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
90 gobject_class->finalize = g_local_file_input_stream_finalize;
92 stream_class->read = g_local_file_input_stream_read;
93 stream_class->skip = g_local_file_input_stream_skip;
94 stream_class->close = g_local_file_input_stream_close;
95 file_stream_class->tell = g_local_file_input_stream_tell;
96 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
97 file_stream_class->seek = g_local_file_input_stream_seek;
98 file_stream_class->query_info = g_local_file_input_stream_query_info;
102 g_local_file_input_stream_init (GLocalFileInputStream *info)
104 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
105 G_TYPE_LOCAL_FILE_INPUT_STREAM,
106 GLocalFileInputStreamPrivate);
110 * g_local_file_input_stream_new:
111 * @fd: File Descriptor.
113 * Returns: #GFileInputStream for the given file descriptor.
116 g_local_file_input_stream_new (int fd)
118 GLocalFileInputStream *stream;
120 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
121 stream->priv->fd = fd;
123 return G_FILE_INPUT_STREAM (stream);
127 g_local_file_input_stream_read (GInputStream *stream,
130 GCancellable *cancellable,
133 GLocalFileInputStream *file;
136 file = G_LOCAL_FILE_INPUT_STREAM (stream);
141 if (g_cancellable_set_error_if_cancelled (cancellable, error))
143 res = read (file->priv->fd, buffer, count);
149 g_set_error (error, G_IO_ERROR,
150 g_io_error_from_errno (errno),
151 _("Error reading from file: %s"),
162 g_local_file_input_stream_skip (GInputStream *stream,
164 GCancellable *cancellable,
168 GLocalFileInputStream *file;
170 file = G_LOCAL_FILE_INPUT_STREAM (stream);
172 if (g_cancellable_set_error_if_cancelled (cancellable, error))
175 start = lseek (file->priv->fd, 0, SEEK_CUR);
178 g_set_error (error, G_IO_ERROR,
179 g_io_error_from_errno (errno),
180 _("Error seeking in file: %s"),
185 res = lseek (file->priv->fd, count, SEEK_CUR);
188 g_set_error (error, G_IO_ERROR,
189 g_io_error_from_errno (errno),
190 _("Error seeking in file: %s"),
199 g_local_file_input_stream_close (GInputStream *stream,
200 GCancellable *cancellable,
203 GLocalFileInputStream *file;
206 file = G_LOCAL_FILE_INPUT_STREAM (stream);
208 if (file->priv->fd == -1)
213 res = close (file->priv->fd);
216 g_set_error (error, G_IO_ERROR,
217 g_io_error_from_errno (errno),
218 _("Error closing file: %s"),
229 g_local_file_input_stream_tell (GFileInputStream *stream)
231 GLocalFileInputStream *file;
234 file = G_LOCAL_FILE_INPUT_STREAM (stream);
236 pos = lseek (file->priv->fd, 0, SEEK_CUR);
238 if (pos == (off_t)-1)
245 g_local_file_input_stream_can_seek (GFileInputStream *stream)
247 GLocalFileInputStream *file;
250 file = G_LOCAL_FILE_INPUT_STREAM (stream);
252 pos = lseek (file->priv->fd, 0, SEEK_CUR);
254 if (pos == (off_t)-1 &&
262 seek_type_to_lseek (GSeekType type)
279 g_local_file_input_stream_seek (GFileInputStream *stream,
282 GCancellable *cancellable,
285 GLocalFileInputStream *file;
288 file = G_LOCAL_FILE_INPUT_STREAM (stream);
290 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
292 if (pos == (off_t)-1)
294 g_set_error (error, G_IO_ERROR,
295 g_io_error_from_errno (errno),
296 _("Error seeking in file: %s"),
305 g_local_file_input_stream_query_info (GFileInputStream *stream,
307 GCancellable *cancellable,
310 GLocalFileInputStream *file;
312 file = G_LOCAL_FILE_INPUT_STREAM (stream);
314 if (g_cancellable_set_error_if_cancelled (cancellable, error))
317 return _g_local_file_info_get_from_fd (file->priv->fd,