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>
31 #include <glib/gstdio.h>
32 #include "gcancellable.h"
34 #include "glocalfileinputstream.h"
35 #include "glocalfileinfo.h"
40 #include "glib-unix.h"
41 #include "gfiledescriptorbased.h"
48 struct _GLocalFileInputStreamPrivate {
54 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
57 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
59 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
60 G_ADD_PRIVATE (GLocalFileInputStream)
61 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
62 g_file_descriptor_based_iface_init))
64 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
65 G_ADD_PRIVATE (GLocalFileInputStream))
68 static gssize g_local_file_input_stream_read (GInputStream *stream,
71 GCancellable *cancellable,
73 static gssize g_local_file_input_stream_skip (GInputStream *stream,
75 GCancellable *cancellable,
77 static gboolean g_local_file_input_stream_close (GInputStream *stream,
78 GCancellable *cancellable,
80 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
81 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
82 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
85 GCancellable *cancellable,
87 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
88 const char *attributes,
89 GCancellable *cancellable,
92 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
96 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
99 in->priv->do_close = do_close;
103 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
105 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
106 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
108 stream_class->read_fn = g_local_file_input_stream_read;
109 stream_class->skip = g_local_file_input_stream_skip;
110 stream_class->close_fn = g_local_file_input_stream_close;
111 file_stream_class->tell = g_local_file_input_stream_tell;
112 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
113 file_stream_class->seek = g_local_file_input_stream_seek;
114 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;
126 g_local_file_input_stream_init (GLocalFileInputStream *info)
128 info->priv = g_local_file_input_stream_get_instance_private (info);
129 info->priv->do_close = TRUE;
133 _g_local_file_input_stream_new (int fd)
135 GLocalFileInputStream *stream;
137 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
138 stream->priv->fd = fd;
140 return G_FILE_INPUT_STREAM (stream);
144 g_local_file_input_stream_read (GInputStream *stream,
147 GCancellable *cancellable,
150 GLocalFileInputStream *file;
153 file = G_LOCAL_FILE_INPUT_STREAM (stream);
158 if (g_cancellable_set_error_if_cancelled (cancellable, error))
160 res = read (file->priv->fd, buffer, count);
168 g_set_error (error, G_IO_ERROR,
169 g_io_error_from_errno (errsv),
170 _("Error reading from file: %s"),
181 g_local_file_input_stream_skip (GInputStream *stream,
183 GCancellable *cancellable,
187 GLocalFileInputStream *file;
189 file = G_LOCAL_FILE_INPUT_STREAM (stream);
191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
194 start = lseek (file->priv->fd, 0, SEEK_CUR);
199 g_set_error (error, G_IO_ERROR,
200 g_io_error_from_errno (errsv),
201 _("Error seeking in file: %s"),
206 end = lseek (file->priv->fd, 0, SEEK_END);
211 g_set_error (error, G_IO_ERROR,
212 g_io_error_from_errno (errsv),
213 _("Error seeking in file: %s"),
218 if (end - start > count)
220 end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
225 g_set_error (error, G_IO_ERROR,
226 g_io_error_from_errno (errsv),
227 _("Error seeking in file: %s"),
237 g_local_file_input_stream_close (GInputStream *stream,
238 GCancellable *cancellable,
241 GLocalFileInputStream *file;
243 file = G_LOCAL_FILE_INPUT_STREAM (stream);
245 if (!file->priv->do_close)
248 if (file->priv->fd == -1)
251 if (!g_close (file->priv->fd, NULL))
255 g_set_error (error, G_IO_ERROR,
256 g_io_error_from_errno (errsv),
257 _("Error closing file: %s"),
267 g_local_file_input_stream_tell (GFileInputStream *stream)
269 GLocalFileInputStream *file;
272 file = G_LOCAL_FILE_INPUT_STREAM (stream);
274 pos = lseek (file->priv->fd, 0, SEEK_CUR);
276 if (pos == (off_t)-1)
283 g_local_file_input_stream_can_seek (GFileInputStream *stream)
285 GLocalFileInputStream *file;
288 file = G_LOCAL_FILE_INPUT_STREAM (stream);
290 pos = lseek (file->priv->fd, 0, SEEK_CUR);
292 if (pos == (off_t)-1 && errno == ESPIPE)
299 seek_type_to_lseek (GSeekType type)
316 g_local_file_input_stream_seek (GFileInputStream *stream,
319 GCancellable *cancellable,
322 GLocalFileInputStream *file;
325 file = G_LOCAL_FILE_INPUT_STREAM (stream);
327 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
329 if (pos == (off_t)-1)
333 g_set_error (error, G_IO_ERROR,
334 g_io_error_from_errno (errsv),
335 _("Error seeking in file: %s"),
344 g_local_file_input_stream_query_info (GFileInputStream *stream,
345 const char *attributes,
346 GCancellable *cancellable,
349 GLocalFileInputStream *file;
351 file = G_LOCAL_FILE_INPUT_STREAM (stream);
353 if (g_cancellable_set_error_if_cancelled (cancellable, error))
356 return _g_local_file_info_get_from_fd (file->priv->fd,
363 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
365 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
366 return stream->priv->fd;