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