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