gkdbus: Fix underflow and unreachable code bug
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
74                                                         GCancellable      *cancellable,
75                                                         GError           **error);
76 static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
77 static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
78 static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
79                                                         goffset            offset,
80                                                         GSeekType          type,
81                                                         GCancellable      *cancellable,
82                                                         GError           **error);
83 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
84                                                         const char        *attributes,
85                                                         GCancellable      *cancellable,
86                                                         GError           **error);
87 #ifdef G_OS_UNIX
88 static int        g_local_file_input_stream_get_fd     (GFileDescriptorBased *stream);
89 #endif
90
91 void
92 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
93                                           gboolean do_close)
94 {
95   in->priv->do_close = do_close;
96 }
97
98 static void
99 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
100 {
101   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
102   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
103
104   stream_class->read_fn = g_local_file_input_stream_read;
105   stream_class->close_fn = g_local_file_input_stream_close;
106   file_stream_class->tell = g_local_file_input_stream_tell;
107   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
108   file_stream_class->seek = g_local_file_input_stream_seek;
109   file_stream_class->query_info = g_local_file_input_stream_query_info;
110 }
111
112 #ifdef G_OS_UNIX
113 static void
114 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
115 {
116   iface->get_fd = g_local_file_input_stream_get_fd;
117 }
118 #endif
119
120 static void
121 g_local_file_input_stream_init (GLocalFileInputStream *info)
122 {
123   info->priv = g_local_file_input_stream_get_instance_private (info);
124   info->priv->do_close = TRUE;
125 }
126
127 GFileInputStream *
128 _g_local_file_input_stream_new (int fd)
129 {
130   GLocalFileInputStream *stream;
131
132   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
133   stream->priv->fd = fd;
134   
135   return G_FILE_INPUT_STREAM (stream);
136 }
137
138 static gssize
139 g_local_file_input_stream_read (GInputStream  *stream,
140                                 void          *buffer,
141                                 gsize          count,
142                                 GCancellable  *cancellable,
143                                 GError       **error)
144 {
145   GLocalFileInputStream *file;
146   gssize res;
147
148   file = G_LOCAL_FILE_INPUT_STREAM (stream);
149
150   res = -1;
151   while (1)
152     {
153       if (g_cancellable_set_error_if_cancelled (cancellable, error))
154         break;
155       res = read (file->priv->fd, buffer, count);
156       if (res == -1)
157         {
158           int errsv = errno;
159
160           if (errsv == EINTR)
161             continue;
162           
163           g_set_error (error, G_IO_ERROR,
164                        g_io_error_from_errno (errsv),
165                        _("Error reading from file: %s"),
166                        g_strerror (errsv));
167         }
168       
169       break;
170     }
171   
172   return res;
173 }
174
175 static gboolean
176 g_local_file_input_stream_close (GInputStream  *stream,
177                                  GCancellable  *cancellable,
178                                  GError       **error)
179 {
180   GLocalFileInputStream *file;
181
182   file = G_LOCAL_FILE_INPUT_STREAM (stream);
183
184   if (!file->priv->do_close)
185     return TRUE;
186
187   if (file->priv->fd == -1)
188     return TRUE;
189
190   if (!g_close (file->priv->fd, NULL))
191     {
192       int errsv = errno;
193       
194       g_set_error (error, G_IO_ERROR,
195                    g_io_error_from_errno (errsv),
196                    _("Error closing file: %s"),
197                    g_strerror (errsv));
198       return FALSE;
199     }
200
201   return TRUE;
202 }
203
204
205 static goffset
206 g_local_file_input_stream_tell (GFileInputStream *stream)
207 {
208   GLocalFileInputStream *file;
209   off_t pos;
210
211   file = G_LOCAL_FILE_INPUT_STREAM (stream);
212   
213   pos = lseek (file->priv->fd, 0, SEEK_CUR);
214
215   if (pos == (off_t)-1)
216     return 0;
217   
218   return pos;
219 }
220
221 static gboolean
222 g_local_file_input_stream_can_seek (GFileInputStream *stream)
223 {
224   GLocalFileInputStream *file;
225   off_t pos;
226
227   file = G_LOCAL_FILE_INPUT_STREAM (stream);
228   
229   pos = lseek (file->priv->fd, 0, SEEK_CUR);
230
231   if (pos == (off_t)-1 && errno == ESPIPE)
232     return FALSE;
233   
234   return TRUE;
235 }
236
237 static int
238 seek_type_to_lseek (GSeekType type)
239 {
240   switch (type)
241     {
242     default:
243     case G_SEEK_CUR:
244       return SEEK_CUR;
245       
246     case G_SEEK_SET:
247       return SEEK_SET;
248       
249     case G_SEEK_END:
250       return SEEK_END;
251     }
252 }
253
254 static gboolean
255 g_local_file_input_stream_seek (GFileInputStream  *stream,
256                                 goffset            offset,
257                                 GSeekType          type,
258                                 GCancellable      *cancellable,
259                                 GError           **error)
260 {
261   GLocalFileInputStream *file;
262   off_t pos;
263
264   file = G_LOCAL_FILE_INPUT_STREAM (stream);
265
266   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
267
268   if (pos == (off_t)-1)
269     {
270       int errsv = errno;
271
272       g_set_error (error, G_IO_ERROR,
273                    g_io_error_from_errno (errsv),
274                    _("Error seeking in file: %s"),
275                    g_strerror (errsv));
276       return FALSE;
277     }
278   
279   return TRUE;
280 }
281
282 static GFileInfo *
283 g_local_file_input_stream_query_info (GFileInputStream  *stream,
284                                       const char        *attributes,
285                                       GCancellable      *cancellable,
286                                       GError           **error)
287 {
288   GLocalFileInputStream *file;
289
290   file = G_LOCAL_FILE_INPUT_STREAM (stream);
291
292   if (g_cancellable_set_error_if_cancelled (cancellable, error))
293     return NULL;
294   
295   return _g_local_file_info_get_from_fd (file->priv->fd,
296                                          attributes,
297                                          error);
298 }
299
300 #ifdef G_OS_UNIX
301 static int
302 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
303 {
304   GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
305   return stream->priv->fd;
306 }
307 #endif