Add support for abstract unix socket addresses
[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   GLocalFileInputStream *file;
83   
84   file = G_LOCAL_FILE_INPUT_STREAM (object);
85
86   G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
87 }
88
89 void
90 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
91                                           gboolean do_close)
92 {
93   in->priv->do_close = do_close;
94 }
95
96 static void
97 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
98 {
99   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
101   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
102   
103   g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
104   
105   gobject_class->finalize = g_local_file_input_stream_finalize;
106
107   stream_class->read_fn = g_local_file_input_stream_read;
108   stream_class->skip = g_local_file_input_stream_skip;
109   stream_class->close_fn = g_local_file_input_stream_close;
110   file_stream_class->tell = g_local_file_input_stream_tell;
111   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
112   file_stream_class->seek = g_local_file_input_stream_seek;
113   file_stream_class->query_info = g_local_file_input_stream_query_info;
114 }
115
116 static void
117 g_local_file_input_stream_init (GLocalFileInputStream *info)
118 {
119   info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
120                                             G_TYPE_LOCAL_FILE_INPUT_STREAM,
121                                             GLocalFileInputStreamPrivate);
122   info->priv->do_close = TRUE;
123 }
124
125 /**
126  * g_local_file_input_stream_new:
127  * @fd: File Descriptor.
128  * 
129  * Returns: #GFileInputStream for the given file descriptor.
130  **/
131 GFileInputStream *
132 _g_local_file_input_stream_new (int fd)
133 {
134   GLocalFileInputStream *stream;
135
136   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
137   stream->priv->fd = fd;
138   
139   return G_FILE_INPUT_STREAM (stream);
140 }
141
142 static gssize
143 g_local_file_input_stream_read (GInputStream  *stream,
144                                 void          *buffer,
145                                 gsize          count,
146                                 GCancellable  *cancellable,
147                                 GError       **error)
148 {
149   GLocalFileInputStream *file;
150   gssize res;
151
152   file = G_LOCAL_FILE_INPUT_STREAM (stream);
153
154   res = -1;
155   while (1)
156     {
157       if (g_cancellable_set_error_if_cancelled (cancellable, error))
158         break;
159       res = read (file->priv->fd, buffer, count);
160       if (res == -1)
161         {
162           int errsv = errno;
163
164           if (errsv == EINTR)
165             continue;
166           
167           g_set_error (error, G_IO_ERROR,
168                        g_io_error_from_errno (errsv),
169                        _("Error reading from file: %s"),
170                        g_strerror (errsv));
171         }
172       
173       break;
174     }
175   
176   return res;
177 }
178
179 static gssize
180 g_local_file_input_stream_skip (GInputStream  *stream,
181                                 gsize          count,
182                                 GCancellable  *cancellable,
183                                 GError       **error)
184 {
185   off_t res, start;
186   GLocalFileInputStream *file;
187
188   file = G_LOCAL_FILE_INPUT_STREAM (stream);
189   
190   if (g_cancellable_set_error_if_cancelled (cancellable, error))
191     return -1;
192   
193   start = lseek (file->priv->fd, 0, SEEK_CUR);
194   if (start == -1)
195     {
196       int errsv = errno;
197
198       g_set_error (error, G_IO_ERROR,
199                    g_io_error_from_errno (errsv),
200                    _("Error seeking in file: %s"),
201                    g_strerror (errsv));
202       return -1;
203     }
204   
205   res = lseek (file->priv->fd, count, SEEK_CUR);
206   if (res == -1)
207     {
208       int errsv = errno;
209
210       g_set_error (error, G_IO_ERROR,
211                    g_io_error_from_errno (errsv),
212                    _("Error seeking in file: %s"),
213                    g_strerror (errsv));
214       return -1;
215     }
216
217   return res - start;
218 }
219
220 static gboolean
221 g_local_file_input_stream_close (GInputStream  *stream,
222                                  GCancellable  *cancellable,
223                                  GError       **error)
224 {
225   GLocalFileInputStream *file;
226   int res;
227
228   file = G_LOCAL_FILE_INPUT_STREAM (stream);
229
230   if (!file->priv->do_close)
231     return TRUE;
232
233   if (file->priv->fd == -1)
234     return TRUE;
235
236   while (1)
237     {
238       res = close (file->priv->fd);
239       if (res == -1)
240         {
241           int errsv = errno;
242
243           g_set_error (error, G_IO_ERROR,
244                        g_io_error_from_errno (errsv),
245                        _("Error closing file: %s"),
246                        g_strerror (errsv));
247         }
248       break;
249     }
250
251   return res != -1;
252 }
253
254
255 static goffset
256 g_local_file_input_stream_tell (GFileInputStream *stream)
257 {
258   GLocalFileInputStream *file;
259   off_t pos;
260
261   file = G_LOCAL_FILE_INPUT_STREAM (stream);
262   
263   pos = lseek (file->priv->fd, 0, SEEK_CUR);
264
265   if (pos == (off_t)-1)
266     return 0;
267   
268   return pos;
269 }
270
271 static gboolean
272 g_local_file_input_stream_can_seek (GFileInputStream *stream)
273 {
274   GLocalFileInputStream *file;
275   off_t pos;
276
277   file = G_LOCAL_FILE_INPUT_STREAM (stream);
278   
279   pos = lseek (file->priv->fd, 0, SEEK_CUR);
280
281   if (pos == (off_t)-1 && errno == ESPIPE)
282     return FALSE;
283   
284   return TRUE;
285 }
286
287 static int
288 seek_type_to_lseek (GSeekType type)
289 {
290   switch (type)
291     {
292     default:
293     case G_SEEK_CUR:
294       return SEEK_CUR;
295       
296     case G_SEEK_SET:
297       return SEEK_SET;
298       
299     case G_SEEK_END:
300       return SEEK_END;
301     }
302 }
303
304 static gboolean
305 g_local_file_input_stream_seek (GFileInputStream  *stream,
306                                 goffset            offset,
307                                 GSeekType          type,
308                                 GCancellable      *cancellable,
309                                 GError           **error)
310 {
311   GLocalFileInputStream *file;
312   off_t pos;
313
314   file = G_LOCAL_FILE_INPUT_STREAM (stream);
315
316   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
317
318   if (pos == (off_t)-1)
319     {
320       int errsv = errno;
321
322       g_set_error (error, G_IO_ERROR,
323                    g_io_error_from_errno (errsv),
324                    _("Error seeking in file: %s"),
325                    g_strerror (errsv));
326       return FALSE;
327     }
328   
329   return TRUE;
330 }
331
332 static GFileInfo *
333 g_local_file_input_stream_query_info (GFileInputStream  *stream,
334                                       const char        *attributes,
335                                       GCancellable      *cancellable,
336                                       GError           **error)
337 {
338   GLocalFileInputStream *file;
339
340   file = G_LOCAL_FILE_INPUT_STREAM (stream);
341
342   if (g_cancellable_set_error_if_cancelled (cancellable, error))
343     return NULL;
344   
345   return _g_local_file_info_get_from_fd (file->priv->fd,
346                                          attributes,
347                                          error);
348 }