"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf / gsf-input-stdio.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-input-stdio.c: stdio based input
4  *
5  * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21
22 #include <gsf-config.h>
23 #include <gsf/gsf-input-stdio.h>
24 #include <gsf/gsf-input-impl.h>
25 #include <gsf/gsf-impl-utils.h>
26 #include <gsf/gsf-utils.h>
27 #include <gsf/gsf-input-memory.h>
28 #include <gsf/gsf-output-memory.h>
29 #include <glib/gstdio.h>
30
31 #include <stdio.h>
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 static GObjectClass *parent_class;
40
41 struct _GsfInputStdio {
42         GsfInput input;
43
44         FILE     *file;
45         char     *filename;
46         guint8   *buf;
47         size_t   buf_size;
48         gboolean keep_open;
49 };
50
51 typedef struct {
52         GsfInputClass input_class;
53 } GsfInputStdioClass;
54
55 static GsfInput *
56 make_local_copy (FILE *stream, const char *filename, GError **err)
57 {
58         GsfOutput *out;
59         GsfInput *copy = NULL;
60
61         out = gsf_output_memory_new ();
62
63         while (1) {
64                 guint8 buf[4096];
65                 gssize nread;
66
67                 nread = fread (buf, 1, sizeof(buf), stream);
68
69                 if (nread > 0) {
70                         if (!gsf_output_write (out, nread, buf))
71                                 goto error;
72                 } else if (nread == 0)
73                         break;
74                 else
75                         goto error;
76         }
77
78         copy = gsf_input_memory_new_clone
79                 (gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (out)),
80                  gsf_output_size (out));
81
82         gsf_output_close (out);
83         g_object_unref (out);
84
85         if (filename)
86                 gsf_input_set_name_from_filename (GSF_INPUT (copy), filename);
87
88         return copy;
89
90 error:
91         if (err) {
92                 char *utf8name = filename
93                         ? g_filename_display_name (filename)
94                         : g_strdup ("?");
95                 g_set_error (err, gsf_input_error_id (), 0,
96                              "%s: not a regular file",
97                              utf8name);
98                 g_free (utf8name);
99         }
100
101         gsf_output_close (out);
102         g_object_unref (out);
103
104         return NULL;
105 }
106
107 /**
108  * gsf_input_stdio_new :
109  * @filename : in utf8.
110  * @err      : optionally NULL.
111  *
112  * Returns: a new file or NULL.
113  **/
114 /* coverity[ -tainted_string_sink_content : arg-0 ] */
115 GsfInput *
116 gsf_input_stdio_new (char const *filename, GError **err)
117 {
118         GsfInputStdio *input;
119         struct stat st;
120         FILE *file;
121         gsf_off_t size;
122
123         g_return_val_if_fail (filename != NULL, NULL);
124
125         file = g_fopen (filename, "rb");
126         if (file == NULL) {
127                 if (err) {
128                         int save_errno = errno;
129                         char *utf8name = g_filename_display_name (filename);
130                         g_set_error (err,
131                                      G_FILE_ERROR,
132                                      g_file_error_from_errno (save_errno),
133                                      "%s: %s",
134                                      utf8name, g_strerror (save_errno));
135                         g_free (utf8name);
136                 }
137                 return NULL;
138         }
139
140         if (fstat (fileno (file), &st) < 0 || !S_ISREG (st.st_mode)) {
141                 GsfInput *res = make_local_copy (file, filename, err);
142                 fclose (file);
143                 return res;
144         }
145
146         size = st.st_size;
147         input = (GsfInputStdio *)g_object_new (GSF_INPUT_STDIO_TYPE, NULL);
148         if (G_UNLIKELY (NULL == input)) {
149                 fclose (file);
150                 return NULL;
151         }
152
153         input->file = file;
154         input->filename = g_strdup (filename);
155         input->buf  = NULL;
156         input->buf_size = 0;
157         input->keep_open = FALSE;
158         gsf_input_set_size (GSF_INPUT (input), size);
159         gsf_input_set_name_from_filename (GSF_INPUT (input), filename);
160
161         return GSF_INPUT (input);
162 }
163
164 /**
165  * gsf_input_stdio_new_FILE :
166  * @filename  : The filename corresponding to @file.
167  * @file      : an existing stdio FILE *
168  * @keep_open : Should @file be closed when the wrapper is closed
169  *
170  * Assumes ownership of @file when succeeding.  If @keep_open is true,
171  * ownership reverts to caller when the GsfObject is closed.
172  *
173  * Returns: a new GsfInput wrapper for @file.  Note that if the file is not
174  *      seekable, this function will make a local copy of the entire file.
175  **/
176 GsfInput *
177 gsf_input_stdio_new_FILE (char const *filename, FILE *file, gboolean keep_open)
178 {
179         GsfInputStdio *stdio;
180         struct stat st;
181         gsf_off_t size;
182
183         g_return_val_if_fail (filename != NULL, NULL);
184         g_return_val_if_fail (file != NULL, NULL);
185
186         if (fstat (fileno (file), &st) < 0 || !S_ISREG (st.st_mode)) {
187                 return make_local_copy (file, filename, NULL);
188         }
189
190         size = st.st_size;
191
192         stdio = g_object_new (GSF_INPUT_STDIO_TYPE, NULL);
193         if (G_UNLIKELY (NULL == stdio)) return NULL;
194         stdio->file = file;
195         stdio->keep_open = keep_open;
196         stdio->filename = g_strdup (filename);
197         gsf_input_set_size (GSF_INPUT (stdio), size);
198         gsf_input_set_name_from_filename (GSF_INPUT (stdio), filename);
199         return GSF_INPUT (stdio);
200 }
201
202 static void
203 gsf_input_stdio_finalize (GObject *obj)
204 {
205         GsfInputStdio *input = (GsfInputStdio *)obj;
206
207         if (input->file != NULL) {
208                 if (!input->keep_open)
209                         fclose (input->file);
210                 input->file = NULL;
211         }
212
213         g_free (input->buf);
214         input->buf = NULL;
215         input->buf_size = 0;
216
217         g_free (input->filename);
218
219         parent_class->finalize (obj);
220 }
221
222 static GsfInput *
223 gsf_input_stdio_dup (GsfInput *src_input, GError **err)
224 {
225         GsfInputStdio const *src = (GsfInputStdio *)src_input;
226         return gsf_input_stdio_new (src->filename, err);
227 }
228
229 static guint8 const *
230 gsf_input_stdio_read (GsfInput *input, size_t num_bytes,
231                       guint8 *buffer)
232 {
233         GsfInputStdio *stdio = GSF_INPUT_STDIO (input);
234         size_t nread = 0, total_read = 0;
235
236         g_return_val_if_fail (stdio != NULL, NULL);
237         g_return_val_if_fail (stdio->file != NULL, NULL);
238
239         if (buffer == NULL) {
240                 if (stdio->buf_size < num_bytes) {
241                         stdio->buf_size = num_bytes;
242                         g_free (stdio->buf);
243                         stdio->buf = g_new (guint8, stdio->buf_size);
244                 }
245                 buffer = stdio->buf;
246         }
247
248         while (total_read < num_bytes) {
249                 nread = fread (buffer + total_read, 1, 
250                                num_bytes - total_read, stdio->file);
251                 total_read += nread;
252                 if (total_read < num_bytes &&
253                     (ferror (stdio->file) || feof (stdio->file)))
254                         return NULL;
255         }
256
257         return buffer;
258 }
259
260 static gboolean
261 gsf_input_stdio_seek (GsfInput *input, gsf_off_t offset, GSeekType whence)
262 {
263         GsfInputStdio const *stdio = GSF_INPUT_STDIO (input);
264         int stdio_whence = SEEK_SET;
265
266 #ifndef HAVE_FSEEKO
267         long loffset;
268 #else
269         off_t loffset;
270 #endif
271
272         if (stdio->file == NULL)
273                 return TRUE;
274
275         loffset = offset;
276         if ((gsf_off_t) loffset != offset) { /* Check for overflow */
277 #ifdef HAVE_FSEEKO
278                 g_warning ("offset too large for fseeko");
279 #else
280                 g_warning ("offset too large for fseek");
281 #endif
282                 return TRUE;
283         }
284         switch (whence) {
285         case G_SEEK_CUR : stdio_whence = SEEK_CUR; break;
286         case G_SEEK_END : stdio_whence = SEEK_END; break;
287         case G_SEEK_SET:
288         default:
289                 break;
290         }
291
292         errno = 0;
293 #ifdef HAVE_FSEEKO
294         if (0 == fseeko (stdio->file, loffset, stdio_whence))
295                 return FALSE;
296 #else
297         if (0 == fseek (stdio->file, loffset, stdio_whence))
298                 return FALSE;
299 #endif
300
301         return TRUE;
302 }
303
304 static void
305 gsf_input_stdio_init (GObject *obj)
306 {
307         GsfInputStdio *stdio = GSF_INPUT_STDIO (obj);
308
309         stdio->file = NULL;
310         stdio->filename = NULL;
311         stdio->buf = NULL;
312         stdio->buf_size = 0;
313         stdio->keep_open = FALSE;
314 }
315
316 static void
317 gsf_input_stdio_class_init (GObjectClass *gobject_class)
318 {
319         GsfInputClass *input_class = GSF_INPUT_CLASS (gobject_class);
320
321         gobject_class->finalize = gsf_input_stdio_finalize;
322         input_class->Dup        = gsf_input_stdio_dup;
323         input_class->Read       = gsf_input_stdio_read;
324         input_class->Seek       = gsf_input_stdio_seek;
325
326         parent_class = g_type_class_peek_parent (gobject_class);
327 }
328
329 GSF_CLASS (GsfInputStdio, gsf_input_stdio,
330            gsf_input_stdio_class_init, gsf_input_stdio_init,
331            GSF_INPUT_TYPE)
332