Formatting cleanup
[glib.git] / gio / glocalfileinputstream.c
blob5aa436d21f0b4e97a23f6980efc02bbde9fa0c52
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <errno.h>
33 #include <glib.h>
34 #include <glib/gstdio.h>
35 #include "gcancellable.h"
36 #include "gioerror.h"
37 #include "glocalfileinputstream.h"
38 #include "glocalfileinfo.h"
39 #include "glibintl.h"
41 #ifdef G_OS_UNIX
42 #include "gfiledescriptorbased.h"
43 #endif
45 #ifdef G_OS_WIN32
46 #include <io.h>
47 #endif
51 #ifdef G_OS_UNIX
52 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
53 #endif
55 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
56 #ifdef G_OS_UNIX
57 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,
58 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
59 g_file_descriptor_based_iface_init)
61 #else
62 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,);
63 #endif
65 struct _GLocalFileInputStreamPrivate {
66 int fd;
67 guint do_close : 1;
70 static gssize g_local_file_input_stream_read (GInputStream *stream,
71 void *buffer,
72 gsize count,
73 GCancellable *cancellable,
74 GError **error);
75 static gssize g_local_file_input_stream_skip (GInputStream *stream,
76 gsize count,
77 GCancellable *cancellable,
78 GError **error);
79 static gboolean g_local_file_input_stream_close (GInputStream *stream,
80 GCancellable *cancellable,
81 GError **error);
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,
85 goffset offset,
86 GSeekType type,
87 GCancellable *cancellable,
88 GError **error);
89 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream *stream,
90 const char *attributes,
91 GCancellable *cancellable,
92 GError **error);
93 #ifdef G_OS_UNIX
94 static int g_local_file_input_stream_get_fd (GFileDescriptorBased *stream);
95 #endif
97 static void
98 g_local_file_input_stream_finalize (GObject *object)
100 G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
103 void
104 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
105 gboolean do_close)
107 in->priv->do_close = do_close;
110 static void
111 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
117 g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
119 gobject_class->finalize = g_local_file_input_stream_finalize;
121 stream_class->read_fn = g_local_file_input_stream_read;
122 stream_class->skip = g_local_file_input_stream_skip;
123 stream_class->close_fn = g_local_file_input_stream_close;
124 file_stream_class->tell = g_local_file_input_stream_tell;
125 file_stream_class->can_seek = g_local_file_input_stream_can_seek;
126 file_stream_class->seek = g_local_file_input_stream_seek;
127 file_stream_class->query_info = g_local_file_input_stream_query_info;
130 #ifdef G_OS_UNIX
131 static void
132 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
134 iface->get_fd = g_local_file_input_stream_get_fd;
136 #endif
138 static void
139 g_local_file_input_stream_init (GLocalFileInputStream *info)
141 info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
142 G_TYPE_LOCAL_FILE_INPUT_STREAM,
143 GLocalFileInputStreamPrivate);
144 info->priv->do_close = TRUE;
147 GFileInputStream *
148 _g_local_file_input_stream_new (int fd)
150 GLocalFileInputStream *stream;
152 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
153 stream->priv->fd = fd;
155 return G_FILE_INPUT_STREAM (stream);
158 static gssize
159 g_local_file_input_stream_read (GInputStream *stream,
160 void *buffer,
161 gsize count,
162 GCancellable *cancellable,
163 GError **error)
165 GLocalFileInputStream *file;
166 gssize res;
168 file = G_LOCAL_FILE_INPUT_STREAM (stream);
170 res = -1;
171 while (1)
173 if (g_cancellable_set_error_if_cancelled (cancellable, error))
174 break;
175 res = read (file->priv->fd, buffer, count);
176 if (res == -1)
178 int errsv = errno;
180 if (errsv == EINTR)
181 continue;
183 g_set_error (error, G_IO_ERROR,
184 g_io_error_from_errno (errsv),
185 _("Error reading from file: %s"),
186 g_strerror (errsv));
189 break;
192 return res;
195 static gssize
196 g_local_file_input_stream_skip (GInputStream *stream,
197 gsize count,
198 GCancellable *cancellable,
199 GError **error)
201 off_t res, start;
202 GLocalFileInputStream *file;
204 file = G_LOCAL_FILE_INPUT_STREAM (stream);
206 if (g_cancellable_set_error_if_cancelled (cancellable, error))
207 return -1;
209 start = lseek (file->priv->fd, 0, SEEK_CUR);
210 if (start == -1)
212 int errsv = errno;
214 g_set_error (error, G_IO_ERROR,
215 g_io_error_from_errno (errsv),
216 _("Error seeking in file: %s"),
217 g_strerror (errsv));
218 return -1;
221 res = lseek (file->priv->fd, count, SEEK_CUR);
222 if (res == -1)
224 int errsv = errno;
226 g_set_error (error, G_IO_ERROR,
227 g_io_error_from_errno (errsv),
228 _("Error seeking in file: %s"),
229 g_strerror (errsv));
230 return -1;
233 return res - start;
236 static gboolean
237 g_local_file_input_stream_close (GInputStream *stream,
238 GCancellable *cancellable,
239 GError **error)
241 GLocalFileInputStream *file;
242 int res;
244 file = G_LOCAL_FILE_INPUT_STREAM (stream);
246 if (!file->priv->do_close)
247 return TRUE;
249 if (file->priv->fd == -1)
250 return TRUE;
252 while (1)
254 res = close (file->priv->fd);
255 if (res == -1)
257 int errsv = errno;
259 g_set_error (error, G_IO_ERROR,
260 g_io_error_from_errno (errsv),
261 _("Error closing file: %s"),
262 g_strerror (errsv));
264 break;
267 return res != -1;
271 static goffset
272 g_local_file_input_stream_tell (GFileInputStream *stream)
274 GLocalFileInputStream *file;
275 off_t pos;
277 file = G_LOCAL_FILE_INPUT_STREAM (stream);
279 pos = lseek (file->priv->fd, 0, SEEK_CUR);
281 if (pos == (off_t)-1)
282 return 0;
284 return pos;
287 static gboolean
288 g_local_file_input_stream_can_seek (GFileInputStream *stream)
290 GLocalFileInputStream *file;
291 off_t pos;
293 file = G_LOCAL_FILE_INPUT_STREAM (stream);
295 pos = lseek (file->priv->fd, 0, SEEK_CUR);
297 if (pos == (off_t)-1 && errno == ESPIPE)
298 return FALSE;
300 return TRUE;
303 static int
304 seek_type_to_lseek (GSeekType type)
306 switch (type)
308 default:
309 case G_SEEK_CUR:
310 return SEEK_CUR;
312 case G_SEEK_SET:
313 return SEEK_SET;
315 case G_SEEK_END:
316 return SEEK_END;
320 static gboolean
321 g_local_file_input_stream_seek (GFileInputStream *stream,
322 goffset offset,
323 GSeekType type,
324 GCancellable *cancellable,
325 GError **error)
327 GLocalFileInputStream *file;
328 off_t pos;
330 file = G_LOCAL_FILE_INPUT_STREAM (stream);
332 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
334 if (pos == (off_t)-1)
336 int errsv = errno;
338 g_set_error (error, G_IO_ERROR,
339 g_io_error_from_errno (errsv),
340 _("Error seeking in file: %s"),
341 g_strerror (errsv));
342 return FALSE;
345 return TRUE;
348 static GFileInfo *
349 g_local_file_input_stream_query_info (GFileInputStream *stream,
350 const char *attributes,
351 GCancellable *cancellable,
352 GError **error)
354 GLocalFileInputStream *file;
356 file = G_LOCAL_FILE_INPUT_STREAM (stream);
358 if (g_cancellable_set_error_if_cancelled (cancellable, error))
359 return NULL;
361 return _g_local_file_info_get_from_fd (file->priv->fd,
362 attributes,
363 error);
366 #ifdef G_OS_UNIX
367 static int
368 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
370 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
371 return stream->priv->fd;
373 #endif