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"
42 #include "gfiledescriptorbased.h"
53 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
56 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
57 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
59 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
60 g_file_descriptor_based_iface_init)
64 struct _GLocalFileInputStreamPrivate {
69 static gssize g_local_file_input_stream_read (GInputStream *stream,
72 GCancellable *cancellable,
74 static gssize g_local_file_input_stream_skip (GInputStream *stream,
76 GCancellable *cancellable,
78 static gboolean g_local_file_input_stream_close (GInputStream *stream,
79 GCancellable *cancellable,
81 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
82 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
83 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
86 GCancellable *cancellable,
88 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
89 const char *attributes,
90 GCancellable *cancellable,
93 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
97 g_local_file_input_stream_finalize (GObject *object)
99 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
103 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
106 in->priv->do_close = do_close;
110 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
114 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
116 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
118 gobject_class->finalize = g_local_file_input_stream_finalize;
120 stream_class->read_fn = g_local_file_input_stream_read;
121 stream_class->skip = g_local_file_input_stream_skip;
122 stream_class->close_fn = g_local_file_input_stream_close;
123 file_stream_class->tell = g_local_file_input_stream_tell;
124 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
125 file_stream_class->seek = g_local_file_input_stream_seek;
126 file_stream_class->query_info = g_local_file_input_stream_query_info;
131 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
133 iface->get_fd = g_local_file_input_stream_get_fd;
138 g_local_file_input_stream_init (GLocalFileInputStream *info)
140 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
141 G_TYPE_LOCAL_FILE_INPUT_STREAM,
142 GLocalFileInputStreamPrivate);
143 info->priv->do_close = TRUE;
147 _g_local_file_input_stream_new (int fd)
149 GLocalFileInputStream *stream;
151 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
152 stream->priv->fd = fd;
154 return G_FILE_INPUT_STREAM (stream);
158 g_local_file_input_stream_read (GInputStream *stream,
161 GCancellable *cancellable,
164 GLocalFileInputStream *file;
167 file = G_LOCAL_FILE_INPUT_STREAM (stream);
172 if (g_cancellable_set_error_if_cancelled (cancellable, error))
174 res = read (file->priv->fd, buffer, count);
182 g_set_error (error, G_IO_ERROR,
183 g_io_error_from_errno (errsv),
184 _("Error reading from file: %s"),
195 g_local_file_input_stream_skip (GInputStream *stream,
197 GCancellable *cancellable,
201 GLocalFileInputStream *file;
203 file = G_LOCAL_FILE_INPUT_STREAM (stream);
205 if (g_cancellable_set_error_if_cancelled (cancellable, error))
208 start = lseek (file->priv->fd, 0, SEEK_CUR);
213 g_set_error (error, G_IO_ERROR,
214 g_io_error_from_errno (errsv),
215 _("Error seeking in file: %s"),
220 res = lseek (file->priv->fd, count, SEEK_CUR);
225 g_set_error (error, G_IO_ERROR,
226 g_io_error_from_errno (errsv),
227 _("Error seeking in file: %s"),
236 g_local_file_input_stream_close (GInputStream *stream,
237 GCancellable *cancellable,
240 GLocalFileInputStream *file;
243 file = G_LOCAL_FILE_INPUT_STREAM (stream);
245 if (!file->priv->do_close)
248 if (file->priv->fd == -1)
253 res = close (file->priv->fd);
258 g_set_error (error, G_IO_ERROR,
259 g_io_error_from_errno (errsv),
260 _("Error closing file: %s"),
271 g_local_file_input_stream_tell (GFileInputStream *stream)
273 GLocalFileInputStream *file;
276 file = G_LOCAL_FILE_INPUT_STREAM (stream);
278 pos = lseek (file->priv->fd, 0, SEEK_CUR);
280 if (pos == (off_t)-1)
287 g_local_file_input_stream_can_seek (GFileInputStream *stream)
289 GLocalFileInputStream *file;
292 file = G_LOCAL_FILE_INPUT_STREAM (stream);
294 pos = lseek (file->priv->fd, 0, SEEK_CUR);
296 if (pos == (off_t)-1 && errno == ESPIPE)
303 seek_type_to_lseek (GSeekType type)
320 g_local_file_input_stream_seek (GFileInputStream *stream,
323 GCancellable *cancellable,
326 GLocalFileInputStream *file;
329 file = G_LOCAL_FILE_INPUT_STREAM (stream);
331 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
333 if (pos == (off_t)-1)
337 g_set_error (error, G_IO_ERROR,
338 g_io_error_from_errno (errsv),
339 _("Error seeking in file: %s"),
348 g_local_file_input_stream_query_info (GFileInputStream *stream,
349 const char *attributes,
350 GCancellable *cancellable,
353 GLocalFileInputStream *file;
355 file = G_LOCAL_FILE_INPUT_STREAM (stream);
357 if (g_cancellable_set_error_if_cancelled (cancellable, error))
360 return _g_local_file_info_get_from_fd (file->priv->fd,
367 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
369 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
370 return stream->priv->fd;