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