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