Rename the generated private data getter function
[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 res, start;
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   res = lseek (file->priv->fd, count, SEEK_CUR);
209   if (res == -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   return res - start;
221 }
222
223 static gboolean
224 g_local_file_input_stream_close (GInputStream  *stream,
225                                  GCancellable  *cancellable,
226                                  GError       **error)
227 {
228   GLocalFileInputStream *file;
229
230   file = G_LOCAL_FILE_INPUT_STREAM (stream);
231
232   if (!file->priv->do_close)
233     return TRUE;
234
235   if (file->priv->fd == -1)
236     return TRUE;
237
238   if (!g_close (file->priv->fd, NULL))
239     {
240       int errsv = errno;
241       
242       g_set_error (error, G_IO_ERROR,
243                    g_io_error_from_errno (errsv),
244                    _("Error closing file: %s"),
245                    g_strerror (errsv));
246       return FALSE;
247     }
248
249   return TRUE;
250 }
251
252
253 static goffset
254 g_local_file_input_stream_tell (GFileInputStream *stream)
255 {
256   GLocalFileInputStream *file;
257   off_t pos;
258
259   file = G_LOCAL_FILE_INPUT_STREAM (stream);
260   
261   pos = lseek (file->priv->fd, 0, SEEK_CUR);
262
263   if (pos == (off_t)-1)
264     return 0;
265   
266   return pos;
267 }
268
269 static gboolean
270 g_local_file_input_stream_can_seek (GFileInputStream *stream)
271 {
272   GLocalFileInputStream *file;
273   off_t pos;
274
275   file = G_LOCAL_FILE_INPUT_STREAM (stream);
276   
277   pos = lseek (file->priv->fd, 0, SEEK_CUR);
278
279   if (pos == (off_t)-1 && errno == ESPIPE)
280     return FALSE;
281   
282   return TRUE;
283 }
284
285 static int
286 seek_type_to_lseek (GSeekType type)
287 {
288   switch (type)
289     {
290     default:
291     case G_SEEK_CUR:
292       return SEEK_CUR;
293       
294     case G_SEEK_SET:
295       return SEEK_SET;
296       
297     case G_SEEK_END:
298       return SEEK_END;
299     }
300 }
301
302 static gboolean
303 g_local_file_input_stream_seek (GFileInputStream  *stream,
304                                 goffset            offset,
305                                 GSeekType          type,
306                                 GCancellable      *cancellable,
307                                 GError           **error)
308 {
309   GLocalFileInputStream *file;
310   off_t pos;
311
312   file = G_LOCAL_FILE_INPUT_STREAM (stream);
313
314   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
315
316   if (pos == (off_t)-1)
317     {
318       int errsv = errno;
319
320       g_set_error (error, G_IO_ERROR,
321                    g_io_error_from_errno (errsv),
322                    _("Error seeking in file: %s"),
323                    g_strerror (errsv));
324       return FALSE;
325     }
326   
327   return TRUE;
328 }
329
330 static GFileInfo *
331 g_local_file_input_stream_query_info (GFileInputStream  *stream,
332                                       const char        *attributes,
333                                       GCancellable      *cancellable,
334                                       GError           **error)
335 {
336   GLocalFileInputStream *file;
337
338   file = G_LOCAL_FILE_INPUT_STREAM (stream);
339
340   if (g_cancellable_set_error_if_cancelled (cancellable, error))
341     return NULL;
342   
343   return _g_local_file_info_get_from_fd (file->priv->fd,
344                                          attributes,
345                                          error);
346 }
347
348 #ifdef G_OS_UNIX
349 static int
350 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
351 {
352   GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
353   return stream->priv->fd;
354 }
355 #endif