• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.1 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 gboolean   g_local_file_input_stream_close      (GInputStream      *stream,
72 							GCancellable      *cancellable,
73 							GError           **error);
74 static goffset    g_local_file_input_stream_tell       (GFileInputStream  *stream);
75 static gboolean   g_local_file_input_stream_can_seek   (GFileInputStream  *stream);
76 static gboolean   g_local_file_input_stream_seek       (GFileInputStream  *stream,
77 							goffset            offset,
78 							GSeekType          type,
79 							GCancellable      *cancellable,
80 							GError           **error);
81 static GFileInfo *g_local_file_input_stream_query_info (GFileInputStream  *stream,
82 							const char        *attributes,
83 							GCancellable      *cancellable,
84 							GError           **error);
85 #ifdef G_OS_UNIX
86 static int        g_local_file_input_stream_get_fd     (GFileDescriptorBased *stream);
87 #endif
88 
89 void
_g_local_file_input_stream_set_do_close(GLocalFileInputStream * in,gboolean do_close)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
g_local_file_input_stream_class_init(GLocalFileInputStreamClass * klass)97 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
98 {
99   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
100   GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
101 
102   stream_class->read_fn = g_local_file_input_stream_read;
103   stream_class->close_fn = g_local_file_input_stream_close;
104   file_stream_class->tell = g_local_file_input_stream_tell;
105   file_stream_class->can_seek = g_local_file_input_stream_can_seek;
106   file_stream_class->seek = g_local_file_input_stream_seek;
107   file_stream_class->query_info = g_local_file_input_stream_query_info;
108 }
109 
110 #ifdef G_OS_UNIX
111 static void
g_file_descriptor_based_iface_init(GFileDescriptorBasedIface * iface)112 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
113 {
114   iface->get_fd = g_local_file_input_stream_get_fd;
115 }
116 #endif
117 
118 static void
g_local_file_input_stream_init(GLocalFileInputStream * info)119 g_local_file_input_stream_init (GLocalFileInputStream *info)
120 {
121   info->priv = g_local_file_input_stream_get_instance_private (info);
122   info->priv->do_close = TRUE;
123 }
124 
125 GFileInputStream *
_g_local_file_input_stream_new(int fd)126 _g_local_file_input_stream_new (int fd)
127 {
128   GLocalFileInputStream *stream;
129 
130   stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
131   stream->priv->fd = fd;
132 
133   return G_FILE_INPUT_STREAM (stream);
134 }
135 
136 static gssize
g_local_file_input_stream_read(GInputStream * stream,void * buffer,gsize count,GCancellable * cancellable,GError ** error)137 g_local_file_input_stream_read (GInputStream  *stream,
138 				void          *buffer,
139 				gsize          count,
140 				GCancellable  *cancellable,
141 				GError       **error)
142 {
143   GLocalFileInputStream *file;
144   gssize res;
145 
146   file = G_LOCAL_FILE_INPUT_STREAM (stream);
147 
148   res = -1;
149   while (1)
150     {
151       if (g_cancellable_set_error_if_cancelled (cancellable, error))
152 	break;
153       res = read (file->priv->fd, buffer, count);
154       if (res == -1)
155 	{
156           int errsv = errno;
157 
158 	  if (errsv == EINTR)
159 	    continue;
160 
161 	  g_set_error (error, G_IO_ERROR,
162 		       g_io_error_from_errno (errsv),
163 		       _("Error reading from file: %s"),
164 		       g_strerror (errsv));
165 	}
166 
167       break;
168     }
169 
170   return res;
171 }
172 
173 static gboolean
g_local_file_input_stream_close(GInputStream * stream,GCancellable * cancellable,GError ** error)174 g_local_file_input_stream_close (GInputStream  *stream,
175 				 GCancellable  *cancellable,
176 				 GError       **error)
177 {
178   GLocalFileInputStream *file;
179 
180   file = G_LOCAL_FILE_INPUT_STREAM (stream);
181 
182   if (!file->priv->do_close)
183     return TRUE;
184 
185   if (file->priv->fd == -1)
186     return TRUE;
187 
188   if (!g_close (file->priv->fd, NULL))
189     {
190       int errsv = errno;
191 
192       g_set_error (error, G_IO_ERROR,
193                    g_io_error_from_errno (errsv),
194                    _("Error closing file: %s"),
195                    g_strerror (errsv));
196       return FALSE;
197     }
198 
199   return TRUE;
200 }
201 
202 
203 static goffset
g_local_file_input_stream_tell(GFileInputStream * stream)204 g_local_file_input_stream_tell (GFileInputStream *stream)
205 {
206   GLocalFileInputStream *file;
207   off_t pos;
208 
209   file = G_LOCAL_FILE_INPUT_STREAM (stream);
210 
211   pos = lseek (file->priv->fd, 0, SEEK_CUR);
212 
213   if (pos == (off_t)-1)
214     return 0;
215 
216   return pos;
217 }
218 
219 static gboolean
g_local_file_input_stream_can_seek(GFileInputStream * stream)220 g_local_file_input_stream_can_seek (GFileInputStream *stream)
221 {
222   GLocalFileInputStream *file;
223   off_t pos;
224 
225   file = G_LOCAL_FILE_INPUT_STREAM (stream);
226 
227   pos = lseek (file->priv->fd, 0, SEEK_CUR);
228 
229   if (pos == (off_t)-1 && errno == ESPIPE)
230     return FALSE;
231 
232   return TRUE;
233 }
234 
235 static int
seek_type_to_lseek(GSeekType type)236 seek_type_to_lseek (GSeekType type)
237 {
238   switch (type)
239     {
240     default:
241     case G_SEEK_CUR:
242       return SEEK_CUR;
243 
244     case G_SEEK_SET:
245       return SEEK_SET;
246 
247     case G_SEEK_END:
248       return SEEK_END;
249     }
250 }
251 
252 static gboolean
g_local_file_input_stream_seek(GFileInputStream * stream,goffset offset,GSeekType type,GCancellable * cancellable,GError ** error)253 g_local_file_input_stream_seek (GFileInputStream  *stream,
254 				goffset            offset,
255 				GSeekType          type,
256 				GCancellable      *cancellable,
257 				GError           **error)
258 {
259   GLocalFileInputStream *file;
260   off_t pos;
261 
262   file = G_LOCAL_FILE_INPUT_STREAM (stream);
263 
264   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
265 
266   if (pos == (off_t)-1)
267     {
268       int errsv = errno;
269 
270       g_set_error (error, G_IO_ERROR,
271 		   g_io_error_from_errno (errsv),
272 		   _("Error seeking in file: %s"),
273 		   g_strerror (errsv));
274       return FALSE;
275     }
276 
277   return TRUE;
278 }
279 
280 static GFileInfo *
g_local_file_input_stream_query_info(GFileInputStream * stream,const char * attributes,GCancellable * cancellable,GError ** error)281 g_local_file_input_stream_query_info (GFileInputStream  *stream,
282 				      const char        *attributes,
283 				      GCancellable      *cancellable,
284 				      GError           **error)
285 {
286   GLocalFileInputStream *file;
287 
288   file = G_LOCAL_FILE_INPUT_STREAM (stream);
289 
290   if (g_cancellable_set_error_if_cancelled (cancellable, error))
291     return NULL;
292 
293   return _g_local_file_info_get_from_fd (file->priv->fd,
294 					 attributes,
295 					 error);
296 }
297 
298 #ifdef G_OS_UNIX
299 static int
g_local_file_input_stream_get_fd(GFileDescriptorBased * fd_based)300 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
301 {
302   GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
303   return stream->priv->fd;
304 }
305 #endif
306