Added UG translation
[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 "gfiledescriptorbased.h"
43 #endif
44
45 #ifdef G_OS_WIN32
46 #include <io.h>
47 #endif
48
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_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
59                                                 g_file_descriptor_based_iface_init)
60 );
61 #else
62 G_DEFINE_TYPE_WITH_CODE (GLocalFileInputStream, g_local_file_input_stream, G_TYPE_FILE_INPUT_STREAM,);
63 #endif
64
65 struct _GLocalFileInputStreamPrivate {
66   int fd;
67   guint do_close : 1;
68 };
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 static void
98 g_local_file_input_stream_finalize (GObject *object)
99 {
100   G_OBJECT_CLASS (g_local_file_input_stream_parent_class)->finalize (object);
101 }
102
103 void
104 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
105                                           gboolean do_close)
106 {
107   in->priv->do_close = do_close;
108 }
109
110 static void
111 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
116   
117   g_type_class_add_private (klass, sizeof (GLocalFileInputStreamPrivate));
118   
119   gobject_class->finalize = g_local_file_input_stream_finalize;
120
121   stream_class->read_fn = g_local_file_input_stream_read;
122   stream_class->skip = g_local_file_input_stream_skip;
123   stream_class->close_fn = g_local_file_input_stream_close;
124   file_stream_class->tell = g_local_file_input_stream_tell;
125   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
126   file_stream_class->seek = g_local_file_input_stream_seek;
127   file_stream_class->query_info = g_local_file_input_stream_query_info;
128 }
129
130 #ifdef G_OS_UNIX
131 static void
132 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
133 {
134   iface->get_fd = g_local_file_input_stream_get_fd;
135 }
136 #endif
137
138 static void
139 g_local_file_input_stream_init (GLocalFileInputStream *info)
140 {
141   info->priv = G_TYPE_INSTANCE_GET_PRIVATE (info,
142                                             G_TYPE_LOCAL_FILE_INPUT_STREAM,
143                                             GLocalFileInputStreamPrivate);
144   info->priv->do_close = TRUE;
145 }
146
147 GFileInputStream *
148 _g_local_file_input_stream_new (int fd)
149 {
150   GLocalFileInputStream *stream;
151
152   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
153   stream->priv->fd = fd;
154   
155   return G_FILE_INPUT_STREAM (stream);
156 }
157
158 static gssize
159 g_local_file_input_stream_read (GInputStream  *stream,
160                                 void          *buffer,
161                                 gsize          count,
162                                 GCancellable  *cancellable,
163                                 GError       **error)
164 {
165   GLocalFileInputStream *file;
166   gssize res;
167
168   file = G_LOCAL_FILE_INPUT_STREAM (stream);
169
170   res = -1;
171   while (1)
172     {
173       if (g_cancellable_set_error_if_cancelled (cancellable, error))
174         break;
175       res = read (file->priv->fd, buffer, count);
176       if (res == -1)
177         {
178           int errsv = errno;
179
180           if (errsv == EINTR)
181             continue;
182           
183           g_set_error (error, G_IO_ERROR,
184                        g_io_error_from_errno (errsv),
185                        _("Error reading from file: %s"),
186                        g_strerror (errsv));
187         }
188       
189       break;
190     }
191   
192   return res;
193 }
194
195 static gssize
196 g_local_file_input_stream_skip (GInputStream  *stream,
197                                 gsize          count,
198                                 GCancellable  *cancellable,
199                                 GError       **error)
200 {
201   off_t res, start;
202   GLocalFileInputStream *file;
203
204   file = G_LOCAL_FILE_INPUT_STREAM (stream);
205   
206   if (g_cancellable_set_error_if_cancelled (cancellable, error))
207     return -1;
208   
209   start = lseek (file->priv->fd, 0, SEEK_CUR);
210   if (start == -1)
211     {
212       int errsv = errno;
213
214       g_set_error (error, G_IO_ERROR,
215                    g_io_error_from_errno (errsv),
216                    _("Error seeking in file: %s"),
217                    g_strerror (errsv));
218       return -1;
219     }
220   
221   res = lseek (file->priv->fd, count, SEEK_CUR);
222   if (res == -1)
223     {
224       int errsv = errno;
225
226       g_set_error (error, G_IO_ERROR,
227                    g_io_error_from_errno (errsv),
228                    _("Error seeking in file: %s"),
229                    g_strerror (errsv));
230       return -1;
231     }
232
233   return res - 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   int res;
243
244   file = G_LOCAL_FILE_INPUT_STREAM (stream);
245
246   if (!file->priv->do_close)
247     return TRUE;
248
249   if (file->priv->fd == -1)
250     return TRUE;
251
252   while (1)
253     {
254       res = close (file->priv->fd);
255       if (res == -1)
256         {
257           int errsv = errno;
258
259           g_set_error (error, G_IO_ERROR,
260                        g_io_error_from_errno (errsv),
261                        _("Error closing file: %s"),
262                        g_strerror (errsv));
263         }
264       break;
265     }
266
267   return res != -1;
268 }
269
270
271 static goffset
272 g_local_file_input_stream_tell (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)
282     return 0;
283   
284   return pos;
285 }
286
287 static gboolean
288 g_local_file_input_stream_can_seek (GFileInputStream *stream)
289 {
290   GLocalFileInputStream *file;
291   off_t pos;
292
293   file = G_LOCAL_FILE_INPUT_STREAM (stream);
294   
295   pos = lseek (file->priv->fd, 0, SEEK_CUR);
296
297   if (pos == (off_t)-1 && errno == ESPIPE)
298     return FALSE;
299   
300   return TRUE;
301 }
302
303 static int
304 seek_type_to_lseek (GSeekType type)
305 {
306   switch (type)
307     {
308     default:
309     case G_SEEK_CUR:
310       return SEEK_CUR;
311       
312     case G_SEEK_SET:
313       return SEEK_SET;
314       
315     case G_SEEK_END:
316       return SEEK_END;
317     }
318 }
319
320 static gboolean
321 g_local_file_input_stream_seek (GFileInputStream  *stream,
322                                 goffset            offset,
323                                 GSeekType          type,
324                                 GCancellable      *cancellable,
325                                 GError           **error)
326 {
327   GLocalFileInputStream *file;
328   off_t pos;
329
330   file = G_LOCAL_FILE_INPUT_STREAM (stream);
331
332   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
333
334   if (pos == (off_t)-1)
335     {
336       int errsv = errno;
337
338       g_set_error (error, G_IO_ERROR,
339                    g_io_error_from_errno (errsv),
340                    _("Error seeking in file: %s"),
341                    g_strerror (errsv));
342       return FALSE;
343     }
344   
345   return TRUE;
346 }
347
348 static GFileInfo *
349 g_local_file_input_stream_query_info (GFileInputStream  *stream,
350                                       const char        *attributes,
351                                       GCancellable      *cancellable,
352                                       GError           **error)
353 {
354   GLocalFileInputStream *file;
355
356   file = G_LOCAL_FILE_INPUT_STREAM (stream);
357
358   if (g_cancellable_set_error_if_cancelled (cancellable, error))
359     return NULL;
360   
361   return _g_local_file_info_get_from_fd (file->priv->fd,
362                                          attributes,
363                                          error);
364 }
365
366 #ifdef G_OS_UNIX
367 static int
368 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
369 {
370   GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
371   return stream->priv->fd;
372 }
373 #endif