Export g_charset_converter_get_num_fallbacks in header
[platform/upstream/glib.git] / gio / glocalfileinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
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>
32
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"
40
41 #ifdef G_OS_WIN32
42 #include <io.h>
43 #endif
44
45 #include "gioalias.h"
46
47 #define g_local_file_input_stream_get_type _g_local_file_input_stream_get_type
48 G_DEFINE_TYPE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
49
50 struct _GLocalFileInputStreamPrivate {
51   int fd;
52   guint do_close : 1;
53 };
54
55 static gssize     g_local_file_input_stream_read       (GInputStream      *stream,
56                                                         void              *buffer,
57                                                         gsize              count,
58                                                         GCancellable      *cancellable,
59                                                         GError           **error);
60 static gssize     g_local_file_input_stream_skip       (GInputStream      *stream,
61                                                         gsize              count,
62                                                         GCancellable      *cancellable,
63                                                         GError           **error);
64 static gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
65                                                         GCancellable      *cancellable,
66                                                         GError           **error);
67 static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
68 static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
69 static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
70                                                         goffset            offset,
71                                                         GSeekType          type,
72                                                         GCancellable      *cancellable,
73                                                         GError           **error);
74 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
75                                                         const char        *attributes,
76                                                         GCancellable      *cancellable,
77                                                         GError           **error);
78
79 static void
80 g_local_file_input_stream_finalize (GObject *object)
81 {
82   G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
83 }
84
85 void
86 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
87                                           gboolean do_close)
88 {
89   in->priv->do_close = do_close;
90 }
91
92 static void
93 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
94 {
95   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
97   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
98   
99   g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
100   
101   gobject_class->finalize = g_local_file_input_stream_finalize;
102
103   stream_class->read_fn = g_local_file_input_stream_read;
104   stream_class->skip = g_local_file_input_stream_skip;
105   stream_class->close_fn = g_local_file_input_stream_close;
106   file_stream_class->tell = g_local_file_input_stream_tell;
107   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
108   file_stream_class->seek = g_local_file_input_stream_seek;
109   file_stream_class->query_info = g_local_file_input_stream_query_info;
110 }
111
112 static void
113 g_local_file_input_stream_init (GLocalFileInputStream *info)
114 {
115   info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
116                                             G_TYPE_LOCAL_FILE_INPUT_STREAM,
117                                             GLocalFileInputStreamPrivate);
118   info->priv->do_close = TRUE;
119 }
120
121 /**
122  * g_local_file_input_stream_new:
123  * @fd: File Descriptor.
124  * 
125  * Returns: #GFileInputStream for the given file descriptor.
126  **/
127 GFileInputStream *
128 _g_local_file_input_stream_new (int fd)
129 {
130   GLocalFileInputStream *stream;
131
132   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
133   stream->priv->fd = fd;
134   
135   return G_FILE_INPUT_STREAM (stream);
136 }
137
138 static gssize
139 g_local_file_input_stream_read (GInputStream  *stream,
140                                 void          *buffer,
141                                 gsize          count,
142                                 GCancellable  *cancellable,
143                                 GError       **error)
144 {
145   GLocalFileInputStream *file;
146   gssize res;
147
148   file = G_LOCAL_FILE_INPUT_STREAM (stream);
149
150   res = -1;
151   while (1)
152     {
153       if (g_cancellable_set_error_if_cancelled (cancellable, error))
154         break;
155       res = read (file->priv->fd, buffer, count);
156       if (res == -1)
157         {
158           int errsv = errno;
159
160           if (errsv == EINTR)
161             continue;
162           
163           g_set_error (error, G_IO_ERROR,
164                        g_io_error_from_errno (errsv),
165                        _("Error reading from file: %s"),
166                        g_strerror (errsv));
167         }
168       
169       break;
170     }
171   
172   return res;
173 }
174
175 static gssize
176 g_local_file_input_stream_skip (GInputStream  *stream,
177                                 gsize          count,
178                                 GCancellable  *cancellable,
179                                 GError       **error)
180 {
181   off_t res, start;
182   GLocalFileInputStream *file;
183
184   file = G_LOCAL_FILE_INPUT_STREAM (stream);
185   
186   if (g_cancellable_set_error_if_cancelled (cancellable, error))
187     return -1;
188   
189   start = lseek (file->priv->fd, 0, SEEK_CUR);
190   if (start == -1)
191     {
192       int errsv = errno;
193
194       g_set_error (error, G_IO_ERROR,
195                    g_io_error_from_errno (errsv),
196                    _("Error seeking in file: %s"),
197                    g_strerror (errsv));
198       return -1;
199     }
200   
201   res = lseek (file->priv->fd, count, SEEK_CUR);
202   if (res == -1)
203     {
204       int errsv = errno;
205
206       g_set_error (error, G_IO_ERROR,
207                    g_io_error_from_errno (errsv),
208                    _("Error seeking in file: %s"),
209                    g_strerror (errsv));
210       return -1;
211     }
212
213   return res - start;
214 }
215
216 static gboolean
217 g_local_file_input_stream_close (GInputStream  *stream,
218                                  GCancellable  *cancellable,
219                                  GError       **error)
220 {
221   GLocalFileInputStream *file;
222   int res;
223
224   file = G_LOCAL_FILE_INPUT_STREAM (stream);
225
226   if (!file->priv->do_close)
227     return TRUE;
228
229   if (file->priv->fd == -1)
230     return TRUE;
231
232   while (1)
233     {
234       res = close (file->priv->fd);
235       if (res == -1)
236         {
237           int errsv = errno;
238
239           g_set_error (error, G_IO_ERROR,
240                        g_io_error_from_errno (errsv),
241                        _("Error closing file: %s"),
242                        g_strerror (errsv));
243         }
244       break;
245     }
246
247   return res != -1;
248 }
249
250
251 static goffset
252 g_local_file_input_stream_tell (GFileInputStream *stream)
253 {
254   GLocalFileInputStream *file;
255   off_t pos;
256
257   file = G_LOCAL_FILE_INPUT_STREAM (stream);
258   
259   pos = lseek (file->priv->fd, 0, SEEK_CUR);
260
261   if (pos == (off_t)-1)
262     return 0;
263   
264   return pos;
265 }
266
267 static gboolean
268 g_local_file_input_stream_can_seek (GFileInputStream *stream)
269 {
270   GLocalFileInputStream *file;
271   off_t pos;
272
273   file = G_LOCAL_FILE_INPUT_STREAM (stream);
274   
275   pos = lseek (file->priv->fd, 0, SEEK_CUR);
276
277   if (pos == (off_t)-1 && errno == ESPIPE)
278     return FALSE;
279   
280   return TRUE;
281 }
282
283 static int
284 seek_type_to_lseek (GSeekType type)
285 {
286   switch (type)
287     {
288     default:
289     case G_SEEK_CUR:
290       return SEEK_CUR;
291       
292     case G_SEEK_SET:
293       return SEEK_SET;
294       
295     case G_SEEK_END:
296       return SEEK_END;
297     }
298 }
299
300 static gboolean
301 g_local_file_input_stream_seek (GFileInputStream  *stream,
302                                 goffset            offset,
303                                 GSeekType          type,
304                                 GCancellable      *cancellable,
305                                 GError           **error)
306 {
307   GLocalFileInputStream *file;
308   off_t pos;
309
310   file = G_LOCAL_FILE_INPUT_STREAM (stream);
311
312   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
313
314   if (pos == (off_t)-1)
315     {
316       int errsv = errno;
317
318       g_set_error (error, G_IO_ERROR,
319                    g_io_error_from_errno (errsv),
320                    _("Error seeking in file: %s"),
321                    g_strerror (errsv));
322       return FALSE;
323     }
324   
325   return TRUE;
326 }
327
328 static GFileInfo *
329 g_local_file_input_stream_query_info (GFileInputStream  *stream,
330                                       const char        *attributes,
331                                       GCancellable      *cancellable,
332                                       GError           **error)
333 {
334   GLocalFileInputStream *file;
335
336   file = G_LOCAL_FILE_INPUT_STREAM (stream);
337
338   if (g_cancellable_set_error_if_cancelled (cancellable, error))
339     return NULL;
340   
341   return _g_local_file_info_get_from_fd (file->priv->fd,
342                                          attributes,
343                                          error);
344 }