9dc5676235f57273c1db79bd42ef253550e23113
[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 GFileInputStream *
122 _g_local_file_input_stream_new (int fd)
123 {
124   GLocalFileInputStream *stream;
125
126   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
127   stream->priv->fd = fd;
128   
129   return G_FILE_INPUT_STREAM (stream);
130 }
131
132 static gssize
133 g_local_file_input_stream_read (GInputStream  *stream,
134                                 void          *buffer,
135                                 gsize          count,
136                                 GCancellable  *cancellable,
137                                 GError       **error)
138 {
139   GLocalFileInputStream *file;
140   gssize res;
141
142   file = G_LOCAL_FILE_INPUT_STREAM (stream);
143
144   res = -1;
145   while (1)
146     {
147       if (g_cancellable_set_error_if_cancelled (cancellable, error))
148         break;
149       res = read (file->priv->fd, buffer, count);
150       if (res == -1)
151         {
152           int errsv = errno;
153
154           if (errsv == EINTR)
155             continue;
156           
157           g_set_error (error, G_IO_ERROR,
158                        g_io_error_from_errno (errsv),
159                        _("Error reading from file: %s"),
160                        g_strerror (errsv));
161         }
162       
163       break;
164     }
165   
166   return res;
167 }
168
169 static gssize
170 g_local_file_input_stream_skip (GInputStream  *stream,
171                                 gsize          count,
172                                 GCancellable  *cancellable,
173                                 GError       **error)
174 {
175   off_t res, start;
176   GLocalFileInputStream *file;
177
178   file = G_LOCAL_FILE_INPUT_STREAM (stream);
179   
180   if (g_cancellable_set_error_if_cancelled (cancellable, error))
181     return -1;
182   
183   start = lseek (file->priv->fd, 0, SEEK_CUR);
184   if (start == -1)
185     {
186       int errsv = errno;
187
188       g_set_error (error, G_IO_ERROR,
189                    g_io_error_from_errno (errsv),
190                    _("Error seeking in file: %s"),
191                    g_strerror (errsv));
192       return -1;
193     }
194   
195   res = lseek (file->priv->fd, count, SEEK_CUR);
196   if (res == -1)
197     {
198       int errsv = errno;
199
200       g_set_error (error, G_IO_ERROR,
201                    g_io_error_from_errno (errsv),
202                    _("Error seeking in file: %s"),
203                    g_strerror (errsv));
204       return -1;
205     }
206
207   return res - start;
208 }
209
210 static gboolean
211 g_local_file_input_stream_close (GInputStream  *stream,
212                                  GCancellable  *cancellable,
213                                  GError       **error)
214 {
215   GLocalFileInputStream *file;
216   int res;
217
218   file = G_LOCAL_FILE_INPUT_STREAM (stream);
219
220   if (!file->priv->do_close)
221     return TRUE;
222
223   if (file->priv->fd == -1)
224     return TRUE;
225
226   while (1)
227     {
228       res = close (file->priv->fd);
229       if (res == -1)
230         {
231           int errsv = errno;
232
233           g_set_error (error, G_IO_ERROR,
234                        g_io_error_from_errno (errsv),
235                        _("Error closing file: %s"),
236                        g_strerror (errsv));
237         }
238       break;
239     }
240
241   return res != -1;
242 }
243
244
245 static goffset
246 g_local_file_input_stream_tell (GFileInputStream *stream)
247 {
248   GLocalFileInputStream *file;
249   off_t pos;
250
251   file = G_LOCAL_FILE_INPUT_STREAM (stream);
252   
253   pos = lseek (file->priv->fd, 0, SEEK_CUR);
254
255   if (pos == (off_t)-1)
256     return 0;
257   
258   return pos;
259 }
260
261 static gboolean
262 g_local_file_input_stream_can_seek (GFileInputStream *stream)
263 {
264   GLocalFileInputStream *file;
265   off_t pos;
266
267   file = G_LOCAL_FILE_INPUT_STREAM (stream);
268   
269   pos = lseek (file->priv->fd, 0, SEEK_CUR);
270
271   if (pos == (off_t)-1 && errno == ESPIPE)
272     return FALSE;
273   
274   return TRUE;
275 }
276
277 static int
278 seek_type_to_lseek (GSeekType type)
279 {
280   switch (type)
281     {
282     default:
283     case G_SEEK_CUR:
284       return SEEK_CUR;
285       
286     case G_SEEK_SET:
287       return SEEK_SET;
288       
289     case G_SEEK_END:
290       return SEEK_END;
291     }
292 }
293
294 static gboolean
295 g_local_file_input_stream_seek (GFileInputStream  *stream,
296                                 goffset            offset,
297                                 GSeekType          type,
298                                 GCancellable      *cancellable,
299                                 GError           **error)
300 {
301   GLocalFileInputStream *file;
302   off_t pos;
303
304   file = G_LOCAL_FILE_INPUT_STREAM (stream);
305
306   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
307
308   if (pos == (off_t)-1)
309     {
310       int errsv = errno;
311
312       g_set_error (error, G_IO_ERROR,
313                    g_io_error_from_errno (errsv),
314                    _("Error seeking in file: %s"),
315                    g_strerror (errsv));
316       return FALSE;
317     }
318   
319   return TRUE;
320 }
321
322 static GFileInfo *
323 g_local_file_input_stream_query_info (GFileInputStream  *stream,
324                                       const char        *attributes,
325                                       GCancellable      *cancellable,
326                                       GError           **error)
327 {
328   GLocalFileInputStream *file;
329
330   file = G_LOCAL_FILE_INPUT_STREAM (stream);
331
332   if (g_cancellable_set_error_if_cancelled (cancellable, error))
333     return NULL;
334   
335   return _g_local_file_info_get_from_fd (file->priv->fd,
336                                          attributes,
337                                          error);
338 }