Add some more cases to the app-id unit tests
[glib.git] / gio / glocalfileinputstream.c
blob2b7944462869ca077f5e83149ad2a1f27f1d8999
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
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"
36 #ifdef G_OS_UNIX
37 #include <unistd.h>
38 #include "glib-unix.h"
39 #include "gfiledescriptorbased.h"
40 #endif
42 #ifdef G_OS_WIN32
43 #include <io.h>
44 #endif
46 struct _GLocalFileInputStreamPrivate {
47 int fd;
48 guint do_close : 1;
51 #ifdef G_OS_UNIX
52 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
53 #endif
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
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
93 void
94 _g_local_file_input_stream_set_do_close (GLocalFileInputStream *in,
95 gboolean do_close)
97 in->priv->do_close = do_close;
100 static void
101 g_local_file_input_stream_class_init (GLocalFileInputStreamClass *klass)
103 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
104 GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass);
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;
115 #ifdef G_OS_UNIX
116 static void
117 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
119 iface->get_fd = g_local_file_input_stream_get_fd;
121 #endif
123 static void
124 g_local_file_input_stream_init (GLocalFileInputStream *info)
126 info->priv = g_local_file_input_stream_get_instance_private (info);
127 info->priv->do_close = TRUE;
130 GFileInputStream *
131 _g_local_file_input_stream_new (int fd)
133 GLocalFileInputStream *stream;
135 stream = g_object_new (G_TYPE_LOCAL_FILE_INPUT_STREAM, NULL);
136 stream->priv->fd = fd;
138 return G_FILE_INPUT_STREAM (stream);
141 static gssize
142 g_local_file_input_stream_read (GInputStream *stream,
143 void *buffer,
144 gsize count,
145 GCancellable *cancellable,
146 GError **error)
148 GLocalFileInputStream *file;
149 gssize res;
151 file = G_LOCAL_FILE_INPUT_STREAM (stream);
153 res = -1;
154 while (1)
156 if (g_cancellable_set_error_if_cancelled (cancellable, error))
157 break;
158 res = read (file->priv->fd, buffer, count);
159 if (res == -1)
161 int errsv = errno;
163 if (errsv == EINTR)
164 continue;
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));
172 break;
175 return res;
178 static gssize
179 g_local_file_input_stream_skip (GInputStream *stream,
180 gsize count,
181 GCancellable *cancellable,
182 GError **error)
184 off_t start, end;
185 GLocalFileInputStream *file;
187 file = G_LOCAL_FILE_INPUT_STREAM (stream);
189 if (g_cancellable_set_error_if_cancelled (cancellable, error))
190 return -1;
192 start = lseek (file->priv->fd, 0, SEEK_CUR);
193 if (start == -1)
195 int errsv = errno;
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;
204 end = lseek (file->priv->fd, 0, SEEK_END);
205 if (end == -1)
207 int errsv = errno;
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;
216 if (end - start > count)
218 end = lseek (file->priv->fd, count - (end - start), SEEK_CUR);
219 if (end == -1)
221 int errsv = errno;
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;
231 return end - start;
234 static gboolean
235 g_local_file_input_stream_close (GInputStream *stream,
236 GCancellable *cancellable,
237 GError **error)
239 GLocalFileInputStream *file;
241 file = G_LOCAL_FILE_INPUT_STREAM (stream);
243 if (!file->priv->do_close)
244 return TRUE;
246 if (file->priv->fd == -1)
247 return TRUE;
249 if (!g_close (file->priv->fd, NULL))
251 int errsv = errno;
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;
260 return TRUE;
264 static goffset
265 g_local_file_input_stream_tell (GFileInputStream *stream)
267 GLocalFileInputStream *file;
268 off_t pos;
270 file = G_LOCAL_FILE_INPUT_STREAM (stream);
272 pos = lseek (file->priv->fd, 0, SEEK_CUR);
274 if (pos == (off_t)-1)
275 return 0;
277 return pos;
280 static gboolean
281 g_local_file_input_stream_can_seek (GFileInputStream *stream)
283 GLocalFileInputStream *file;
284 off_t pos;
286 file = G_LOCAL_FILE_INPUT_STREAM (stream);
288 pos = lseek (file->priv->fd, 0, SEEK_CUR);
290 if (pos == (off_t)-1 && errno == ESPIPE)
291 return FALSE;
293 return TRUE;
296 static int
297 seek_type_to_lseek (GSeekType type)
299 switch (type)
301 default:
302 case G_SEEK_CUR:
303 return SEEK_CUR;
305 case G_SEEK_SET:
306 return SEEK_SET;
308 case G_SEEK_END:
309 return SEEK_END;
313 static gboolean
314 g_local_file_input_stream_seek (GFileInputStream *stream,
315 goffset offset,
316 GSeekType type,
317 GCancellable *cancellable,
318 GError **error)
320 GLocalFileInputStream *file;
321 off_t pos;
323 file = G_LOCAL_FILE_INPUT_STREAM (stream);
325 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
327 if (pos == (off_t)-1)
329 int errsv = errno;
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;
338 return TRUE;
341 static GFileInfo *
342 g_local_file_input_stream_query_info (GFileInputStream *stream,
343 const char *attributes,
344 GCancellable *cancellable,
345 GError **error)
347 GLocalFileInputStream *file;
349 file = G_LOCAL_FILE_INPUT_STREAM (stream);
351 if (g_cancellable_set_error_if_cancelled (cancellable, error))
352 return NULL;
354 return _g_local_file_info_get_from_fd (file->priv->fd,
355 attributes,
356 error);
359 #ifdef G_OS_UNIX
360 static int
361 g_local_file_input_stream_get_fd (GFileDescriptorBased *fd_based)
363 GLocalFileInputStream *stream = G_LOCAL_FILE_INPUT_STREAM (fd_based);
364 return stream->priv->fd;
366 #endif