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"
36 #include "gfiledescriptorbased.h"
38 #include "glocalfileinputstream.h"
39 #include "glocalfileinfo.h"
49 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
50 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
51 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
52 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
53 g_file_descriptor_based_iface_init));
55 struct _GLocalFileInputStreamPrivate {
60 static gssize g_local_file_input_stream_read (GInputStream *stream,
63 GCancellable *cancellable,
65 static gssize g_local_file_input_stream_skip (GInputStream *stream,
67 GCancellable *cancellable,
69 static gboolean g_local_file_input_stream_close (GInputStream *stream,
70 GCancellable *cancellable,
72 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
73 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
74 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
77 GCancellable *cancellable,
79 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
80 const char *attributes,
81 GCancellable *cancellable,
83 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
86 g_local_file_input_stream_finalize (GObject *object)
88 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
92 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
95 in->priv->do_close = do_close;
99 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
101 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
103 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
105 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
107 gobject_class->finalize = g_local_file_input_stream_finalize;
109 stream_class->read_fn = g_local_file_input_stream_read;
110 stream_class->skip = g_local_file_input_stream_skip;
111 stream_class->close_fn = g_local_file_input_stream_close;
112 file_stream_class->tell = g_local_file_input_stream_tell;
113 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
114 file_stream_class->seek = g_local_file_input_stream_seek;
115 file_stream_class->query_info = g_local_file_input_stream_query_info;
119 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
121 iface->get_fd = g_local_file_input_stream_get_fd;
125 g_local_file_input_stream_init (GLocalFileInputStream *info)
127 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
128 G_TYPE_LOCAL_FILE_INPUT_STREAM,
129 GLocalFileInputStreamPrivate);
130 info->priv->do_close = TRUE;
134 _g_local_file_input_stream_new (int fd)
136 GLocalFileInputStream *stream;
138 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
139 stream->priv->fd = fd;
141 return G_FILE_INPUT_STREAM (stream);
145 g_local_file_input_stream_read (GInputStream *stream,
148 GCancellable *cancellable,
151 GLocalFileInputStream *file;
154 file = G_LOCAL_FILE_INPUT_STREAM (stream);
159 if (g_cancellable_set_error_if_cancelled (cancellable, error))
161 res = read (file->priv->fd, buffer, count);
169 g_set_error (error, G_IO_ERROR,
170 g_io_error_from_errno (errsv),
171 _("Error reading from file: %s"),
182 g_local_file_input_stream_skip (GInputStream *stream,
184 GCancellable *cancellable,
188 GLocalFileInputStream *file;
190 file = G_LOCAL_FILE_INPUT_STREAM (stream);
192 if (g_cancellable_set_error_if_cancelled (cancellable, error))
195 start = lseek (file->priv->fd, 0, SEEK_CUR);
200 g_set_error (error, G_IO_ERROR,
201 g_io_error_from_errno (errsv),
202 _("Error seeking in file: %s"),
207 res = lseek (file->priv->fd, count, SEEK_CUR);
212 g_set_error (error, G_IO_ERROR,
213 g_io_error_from_errno (errsv),
214 _("Error seeking in file: %s"),
223 g_local_file_input_stream_close (GInputStream *stream,
224 GCancellable *cancellable,
227 GLocalFileInputStream *file;
230 file = G_LOCAL_FILE_INPUT_STREAM (stream);
232 if (!file->priv->do_close)
235 if (file->priv->fd == -1)
240 res = close (file->priv->fd);
245 g_set_error (error, G_IO_ERROR,
246 g_io_error_from_errno (errsv),
247 _("Error closing file: %s"),
258 g_local_file_input_stream_tell (GFileInputStream *stream)
260 GLocalFileInputStream *file;
263 file = G_LOCAL_FILE_INPUT_STREAM (stream);
265 pos = lseek (file->priv->fd, 0, SEEK_CUR);
267 if (pos == (off_t)-1)
274 g_local_file_input_stream_can_seek (GFileInputStream *stream)
276 GLocalFileInputStream *file;
279 file = G_LOCAL_FILE_INPUT_STREAM (stream);
281 pos = lseek (file->priv->fd, 0, SEEK_CUR);
283 if (pos == (off_t)-1 && errno == ESPIPE)
290 seek_type_to_lseek (GSeekType type)
307 g_local_file_input_stream_seek (GFileInputStream *stream,
310 GCancellable *cancellable,
313 GLocalFileInputStream *file;
316 file = G_LOCAL_FILE_INPUT_STREAM (stream);
318 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
320 if (pos == (off_t)-1)
324 g_set_error (error, G_IO_ERROR,
325 g_io_error_from_errno (errsv),
326 _("Error seeking in file: %s"),
335 g_local_file_input_stream_query_info (GFileInputStream *stream,
336 const char *attributes,
337 GCancellable *cancellable,
340 GLocalFileInputStream *file;
342 file = G_LOCAL_FILE_INPUT_STREAM (stream);
344 if (g_cancellable_set_error_if_cancelled (cancellable, error))
347 return _g_local_file_info_get_from_fd (file->priv->fd,
353 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
355 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
356 return stream->priv->fd;