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