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 "glib-unix.h"
43 #include "gfiledescriptorbased.h"
50 struct _GLocalFileInputStreamPrivate {
56 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
59 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
61 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
62 G_ADD_PRIVATE (GLocalFileInputStream)
63 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
64 g_file_descriptor_based_iface_init))
66 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
67 G_ADD_PRIVATE (GLocalFileInputStream))
70 static gssize g_local_file_input_stream_read (GInputStream *stream,
73 GCancellable *cancellable,
75 static gssize g_local_file_input_stream_skip (GInputStream *stream,
77 GCancellable *cancellable,
79 static gboolean g_local_file_input_stream_close (GInputStream *stream,
80 GCancellable *cancellable,
82 static goffset g_local_file_input_stream_tell (GFileInputStream *stream);
83 static gboolean g_local_file_input_stream_can_seek (GFileInputStream *stream);
84 static gboolean g_local_file_input_stream_seek (GFileInputStream *stream,
87 GCancellable *cancellable,
89 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
90 const char *attributes,
91 GCancellable *cancellable,
94 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
98 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
101 in->priv->do_close = do_close;
105 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
107 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
108 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
110 stream_class->read_fn = g_local_file_input_stream_read;
111 stream_class->skip = g_local_file_input_stream_skip;
112 stream_class->close_fn = g_local_file_input_stream_close;
113 file_stream_class->tell = g_local_file_input_stream_tell;
114 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
115 file_stream_class->seek = g_local_file_input_stream_seek;
116 file_stream_class->query_info = g_local_file_input_stream_query_info;
121 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
123 iface->get_fd = g_local_file_input_stream_get_fd;
128 g_local_file_input_stream_init (GLocalFileInputStream *info)
130 info->priv = g_local_file_input_stream_get_instance_private (info);
131 info->priv->do_close = TRUE;
135 _g_local_file_input_stream_new (int fd)
137 GLocalFileInputStream *stream;
139 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
140 stream->priv->fd = fd;
142 return G_FILE_INPUT_STREAM (stream);
146 g_local_file_input_stream_read (GInputStream *stream,
149 GCancellable *cancellable,
152 GLocalFileInputStream *file;
155 file = G_LOCAL_FILE_INPUT_STREAM (stream);
160 if (g_cancellable_set_error_if_cancelled (cancellable, error))
162 res = read (file->priv->fd, buffer, count);
170 g_set_error (error, G_IO_ERROR,
171 g_io_error_from_errno (errsv),
172 _("Error reading from file: %s"),
183 g_local_file_input_stream_skip (GInputStream *stream,
185 GCancellable *cancellable,
189 GLocalFileInputStream *file;
191 file = G_LOCAL_FILE_INPUT_STREAM (stream);
193 if (g_cancellable_set_error_if_cancelled (cancellable, error))
196 start = lseek (file->priv->fd, 0, SEEK_CUR);
201 g_set_error (error, G_IO_ERROR,
202 g_io_error_from_errno (errsv),
203 _("Error seeking in file: %s"),
208 end = lseek (file->priv->fd, 0, SEEK_END);
213 g_set_error (error, G_IO_ERROR,
214 g_io_error_from_errno (errsv),
215 _("Error seeking in file: %s"),
220 if (end - start > count)
222 end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
227 g_set_error (error, G_IO_ERROR,
228 g_io_error_from_errno (errsv),
229 _("Error seeking in file: %s"),
239 g_local_file_input_stream_close (GInputStream *stream,
240 GCancellable *cancellable,
243 GLocalFileInputStream *file;
245 file = G_LOCAL_FILE_INPUT_STREAM (stream);
247 if (!file->priv->do_close)
250 if (file->priv->fd == -1)
253 if (!g_close (file->priv->fd, NULL))
257 g_set_error (error, G_IO_ERROR,
258 g_io_error_from_errno (errsv),
259 _("Error closing file: %s"),
269 g_local_file_input_stream_tell (GFileInputStream *stream)
271 GLocalFileInputStream *file;
274 file = G_LOCAL_FILE_INPUT_STREAM (stream);
276 pos = lseek (file->priv->fd, 0, SEEK_CUR);
278 if (pos == (off_t)-1)
285 g_local_file_input_stream_can_seek (GFileInputStream *stream)
287 GLocalFileInputStream *file;
290 file = G_LOCAL_FILE_INPUT_STREAM (stream);
292 pos = lseek (file->priv->fd, 0, SEEK_CUR);
294 if (pos == (off_t)-1 && errno == ESPIPE)
301 seek_type_to_lseek (GSeekType type)
318 g_local_file_input_stream_seek (GFileInputStream *stream,
321 GCancellable *cancellable,
324 GLocalFileInputStream *file;
327 file = G_LOCAL_FILE_INPUT_STREAM (stream);
329 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
331 if (pos == (off_t)-1)
335 g_set_error (error, G_IO_ERROR,
336 g_io_error_from_errno (errsv),
337 _("Error seeking in file: %s"),
346 g_local_file_input_stream_query_info (GFileInputStream *stream,
347 const char *attributes,
348 GCancellable *cancellable,
351 GLocalFileInputStream *file;
353 file = G_LOCAL_FILE_INPUT_STREAM (stream);
355 if (g_cancellable_set_error_if_cancelled (cancellable, error))
358 return _g_local_file_info_get_from_fd (file->priv->fd,
365 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
367 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
368 return stream->priv->fd;