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 {
55 static gssize g_local_file_input_stream_read (GInputStream *stream,
58 GCancellable *cancellable,
60 static gssize g_local_file_input_stream_skip (GInputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_local_file_input_stream_close (GInputStream *stream,
65 GCancellable *cancellable,
67 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
68 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
69 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
72 GCancellable *cancellable,
74 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
75 const char *attributes,
76 GCancellable *cancellable,
80 g_local_file_input_stream_finalize (GObject *object)
82 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
86 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
89 in->priv->do_close = do_close;
93 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
97 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
99 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
101 gobject_class->finalize = g_local_file_input_stream_finalize;
103 stream_class->read_fn = g_local_file_input_stream_read;
104 stream_class->skip = g_local_file_input_stream_skip;
105 stream_class->close_fn = g_local_file_input_stream_close;
106 file_stream_class->tell = g_local_file_input_stream_tell;
107 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
108 file_stream_class->seek = g_local_file_input_stream_seek;
109 file_stream_class->query_info = g_local_file_input_stream_query_info;
113 g_local_file_input_stream_init (GLocalFileInputStream *info)
115 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
116 G_TYPE_LOCAL_FILE_INPUT_STREAM,
117 GLocalFileInputStreamPrivate);
118 info->priv->do_close = TRUE;
122 * g_local_file_input_stream_new:
123 * @fd: File Descriptor.
125 * Returns: #GFileInputStream for the given file descriptor.
128 _g_local_file_input_stream_new (int fd)
130 GLocalFileInputStream *stream;
132 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
133 stream->priv->fd = fd;
135 return G_FILE_INPUT_STREAM (stream);
139 g_local_file_input_stream_read (GInputStream *stream,
142 GCancellable *cancellable,
145 GLocalFileInputStream *file;
148 file = G_LOCAL_FILE_INPUT_STREAM (stream);
153 if (g_cancellable_set_error_if_cancelled (cancellable, error))
155 res = read (file->priv->fd, buffer, count);
163 g_set_error (error, G_IO_ERROR,
164 g_io_error_from_errno (errsv),
165 _("Error reading from file: %s"),
176 g_local_file_input_stream_skip (GInputStream *stream,
178 GCancellable *cancellable,
182 GLocalFileInputStream *file;
184 file = G_LOCAL_FILE_INPUT_STREAM (stream);
186 if (g_cancellable_set_error_if_cancelled (cancellable, error))
189 start = lseek (file->priv->fd, 0, SEEK_CUR);
194 g_set_error (error, G_IO_ERROR,
195 g_io_error_from_errno (errsv),
196 _("Error seeking in file: %s"),
201 res = lseek (file->priv->fd, count, SEEK_CUR);
206 g_set_error (error, G_IO_ERROR,
207 g_io_error_from_errno (errsv),
208 _("Error seeking in file: %s"),
217 g_local_file_input_stream_close (GInputStream *stream,
218 GCancellable *cancellable,
221 GLocalFileInputStream *file;
224 file = G_LOCAL_FILE_INPUT_STREAM (stream);
226 if (!file->priv->do_close)
229 if (file->priv->fd == -1)
234 res = close (file->priv->fd);
239 g_set_error (error, G_IO_ERROR,
240 g_io_error_from_errno (errsv),
241 _("Error closing file: %s"),
252 g_local_file_input_stream_tell (GFileInputStream *stream)
254 GLocalFileInputStream *file;
257 file = G_LOCAL_FILE_INPUT_STREAM (stream);
259 pos = lseek (file->priv->fd, 0, SEEK_CUR);
261 if (pos == (off_t)-1)
268 g_local_file_input_stream_can_seek (GFileInputStream *stream)
270 GLocalFileInputStream *file;
273 file = G_LOCAL_FILE_INPUT_STREAM (stream);
275 pos = lseek (file->priv->fd, 0, SEEK_CUR);
277 if (pos == (off_t)-1 && errno == ESPIPE)
284 seek_type_to_lseek (GSeekType type)
301 g_local_file_input_stream_seek (GFileInputStream *stream,
304 GCancellable *cancellable,
307 GLocalFileInputStream *file;
310 file = G_LOCAL_FILE_INPUT_STREAM (stream);
312 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
314 if (pos == (off_t)-1)
318 g_set_error (error, G_IO_ERROR,
319 g_io_error_from_errno (errsv),
320 _("Error seeking in file: %s"),
329 g_local_file_input_stream_query_info (GFileInputStream *stream,
330 const char *attributes,
331 GCancellable *cancellable,
334 GLocalFileInputStream *file;
336 file = G_LOCAL_FILE_INPUT_STREAM (stream);
338 if (g_cancellable_set_error_if_cancelled (cancellable, error))
341 return _g_local_file_info_get_from_fd (file->priv->fd,