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"
47 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
48 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
50 struct _GLocalFileInputStreamPrivate {
54 static gssize g_local_file_input_stream_read (GInputStream *stream,
57 GCancellable *cancellable,
59 static gssize g_local_file_input_stream_skip (GInputStream *stream,
61 GCancellable *cancellable,
63 static gboolean g_local_file_input_stream_close (GInputStream *stream,
64 GCancellable *cancellable,
66 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
67 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
68 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
71 GCancellable *cancellable,
73 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
74 const char *attributes,
75 GCancellable *cancellable,
79 g_local_file_input_stream_finalize (GObject *object)
81 GLocalFileInputStream *file;
83 file = G_LOCAL_FILE_INPUT_STREAM (object);
85 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
89 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
93 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
95 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
97 gobject_class->finalize = g_local_file_input_stream_finalize;
99 stream_class->read_fn = g_local_file_input_stream_read;
100 stream_class->skip = g_local_file_input_stream_skip;
101 stream_class->close_fn = g_local_file_input_stream_close;
102 file_stream_class->tell = g_local_file_input_stream_tell;
103 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
104 file_stream_class->seek = g_local_file_input_stream_seek;
105 file_stream_class->query_info = g_local_file_input_stream_query_info;
109 g_local_file_input_stream_init (GLocalFileInputStream *info)
111 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
112 G_TYPE_LOCAL_FILE_INPUT_STREAM,
113 GLocalFileInputStreamPrivate);
117 * g_local_file_input_stream_new:
118 * @fd: File Descriptor.
120 * Returns: #GFileInputStream for the given file descriptor.
123 _g_local_file_input_stream_new (int fd)
125 GLocalFileInputStream *stream;
127 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
128 stream->priv->fd = fd;
130 return G_FILE_INPUT_STREAM (stream);
134 g_local_file_input_stream_read (GInputStream *stream,
137 GCancellable *cancellable,
140 GLocalFileInputStream *file;
143 file = G_LOCAL_FILE_INPUT_STREAM (stream);
148 if (g_cancellable_set_error_if_cancelled (cancellable, error))
150 res = read (file->priv->fd, buffer, count);
158 g_set_error (error, G_IO_ERROR,
159 g_io_error_from_errno (errsv),
160 _("Error reading from file: %s"),
171 g_local_file_input_stream_skip (GInputStream *stream,
173 GCancellable *cancellable,
177 GLocalFileInputStream *file;
179 file = G_LOCAL_FILE_INPUT_STREAM (stream);
181 if (g_cancellable_set_error_if_cancelled (cancellable, error))
184 start = lseek (file->priv->fd, 0, SEEK_CUR);
189 g_set_error (error, G_IO_ERROR,
190 g_io_error_from_errno (errsv),
191 _("Error seeking in file: %s"),
196 res = lseek (file->priv->fd, count, SEEK_CUR);
201 g_set_error (error, G_IO_ERROR,
202 g_io_error_from_errno (errsv),
203 _("Error seeking in file: %s"),
212 g_local_file_input_stream_close (GInputStream *stream,
213 GCancellable *cancellable,
216 GLocalFileInputStream *file;
219 file = G_LOCAL_FILE_INPUT_STREAM (stream);
221 if (file->priv->fd == -1)
226 res = close (file->priv->fd);
231 g_set_error (error, G_IO_ERROR,
232 g_io_error_from_errno (errsv),
233 _("Error closing file: %s"),
244 g_local_file_input_stream_tell (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)
260 g_local_file_input_stream_can_seek (GFileInputStream *stream)
262 GLocalFileInputStream *file;
265 file = G_LOCAL_FILE_INPUT_STREAM (stream);
267 pos = lseek (file->priv->fd, 0, SEEK_CUR);
269 if (pos == (off_t)-1 && errno == ESPIPE)
276 seek_type_to_lseek (GSeekType type)
293 g_local_file_input_stream_seek (GFileInputStream *stream,
296 GCancellable *cancellable,
299 GLocalFileInputStream *file;
302 file = G_LOCAL_FILE_INPUT_STREAM (stream);
304 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
306 if (pos == (off_t)-1)
310 g_set_error (error, G_IO_ERROR,
311 g_io_error_from_errno (errsv),
312 _("Error seeking in file: %s"),
321 g_local_file_input_stream_query_info (GFileInputStream *stream,
322 const char *attributes,
323 GCancellable *cancellable,
326 GLocalFileInputStream *file;
328 file = G_LOCAL_FILE_INPUT_STREAM (stream);
330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
333 return _g_local_file_info_get_from_fd (file->priv->fd,