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 GLocalFileInputStream *file;
84 file = G_LOCAL_FILE_INPUT_STREAM (object);
86 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
90 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
93 in->priv->do_close = do_close;
97 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
99 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
101 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
103 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
105 gobject_class->finalize = g_local_file_input_stream_finalize;
107 stream_class->read_fn = g_local_file_input_stream_read;
108 stream_class->skip = g_local_file_input_stream_skip;
109 stream_class->close_fn = g_local_file_input_stream_close;
110 file_stream_class->tell = g_local_file_input_stream_tell;
111 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
112 file_stream_class->seek = g_local_file_input_stream_seek;
113 file_stream_class->query_info = g_local_file_input_stream_query_info;
117 g_local_file_input_stream_init (GLocalFileInputStream *info)
119 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
120 G_TYPE_LOCAL_FILE_INPUT_STREAM,
121 GLocalFileInputStreamPrivate);
122 info->priv->do_close = TRUE;
126 * g_local_file_input_stream_new:
127 * @fd: File Descriptor.
129 * Returns: #GFileInputStream for the given file descriptor.
132 _g_local_file_input_stream_new (int fd)
134 GLocalFileInputStream *stream;
136 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
137 stream->priv->fd = fd;
139 return G_FILE_INPUT_STREAM (stream);
143 g_local_file_input_stream_read (GInputStream *stream,
146 GCancellable *cancellable,
149 GLocalFileInputStream *file;
152 file = G_LOCAL_FILE_INPUT_STREAM (stream);
157 if (g_cancellable_set_error_if_cancelled (cancellable, error))
159 res = read (file->priv->fd, buffer, count);
167 g_set_error (error, G_IO_ERROR,
168 g_io_error_from_errno (errsv),
169 _("Error reading from file: %s"),
180 g_local_file_input_stream_skip (GInputStream *stream,
182 GCancellable *cancellable,
186 GLocalFileInputStream *file;
188 file = G_LOCAL_FILE_INPUT_STREAM (stream);
190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
193 start = lseek (file->priv->fd, 0, SEEK_CUR);
198 g_set_error (error, G_IO_ERROR,
199 g_io_error_from_errno (errsv),
200 _("Error seeking in file: %s"),
205 res = lseek (file->priv->fd, count, SEEK_CUR);
210 g_set_error (error, G_IO_ERROR,
211 g_io_error_from_errno (errsv),
212 _("Error seeking in file: %s"),
221 g_local_file_input_stream_close (GInputStream *stream,
222 GCancellable *cancellable,
225 GLocalFileInputStream *file;
228 file = G_LOCAL_FILE_INPUT_STREAM (stream);
230 if (!file->priv->do_close)
233 if (file->priv->fd == -1)
238 res = close (file->priv->fd);
243 g_set_error (error, G_IO_ERROR,
244 g_io_error_from_errno (errsv),
245 _("Error closing file: %s"),
256 g_local_file_input_stream_tell (GFileInputStream *stream)
258 GLocalFileInputStream *file;
261 file = G_LOCAL_FILE_INPUT_STREAM (stream);
263 pos = lseek (file->priv->fd, 0, SEEK_CUR);
265 if (pos == (off_t)-1)
272 g_local_file_input_stream_can_seek (GFileInputStream *stream)
274 GLocalFileInputStream *file;
277 file = G_LOCAL_FILE_INPUT_STREAM (stream);
279 pos = lseek (file->priv->fd, 0, SEEK_CUR);
281 if (pos == (off_t)-1 && errno == ESPIPE)
288 seek_type_to_lseek (GSeekType type)
305 g_local_file_input_stream_seek (GFileInputStream *stream,
308 GCancellable *cancellable,
311 GLocalFileInputStream *file;
314 file = G_LOCAL_FILE_INPUT_STREAM (stream);
316 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
318 if (pos == (off_t)-1)
322 g_set_error (error, G_IO_ERROR,
323 g_io_error_from_errno (errsv),
324 _("Error seeking in file: %s"),
333 g_local_file_input_stream_query_info (GFileInputStream *stream,
334 const char *attributes,
335 GCancellable *cancellable,
338 GLocalFileInputStream *file;
340 file = G_LOCAL_FILE_INPUT_STREAM (stream);
342 if (g_cancellable_set_error_if_cancelled (cancellable, error))
345 return _g_local_file_info_get_from_fd (file->priv->fd,