File: gmime-stream-gio.c

package info (click to toggle)
balsa 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 21,456 kB
  • ctags: 8,303
  • sloc: ansic: 93,724; xml: 13,662; sh: 11,146; makefile: 615; awk: 60
file content (489 lines) | stat: -rw-r--r-- 13,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/*  GMime GIO stream module
 *  Written/Copyright (c) by Albrecht Dre <albrecht.dress@arcor.de>
 *  The basic structure of this file has been shamelessly stolen from the
 *  gmime-stream-fs module, written by Jeffrey Stedfast.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "gmime-stream-gio.h"

/* note: this module will be compiled only if GIO is available */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

static void g_mime_stream_gio_class_init(GMimeStreamGioClass *klass);
static void g_mime_stream_gio_init(GMimeStreamGio *stream,
				   GMimeStreamGioClass *klass);
static void g_mime_stream_gio_finalize(GObject *object);

static ssize_t stream_read(GMimeStream *stream,
			   char *buf,
			   size_t len);
static ssize_t stream_write(GMimeStream *stream,
			    const char *buf,
			    size_t len);
static int stream_flush(GMimeStream *stream);
static int stream_close(GMimeStream *stream);
static gboolean stream_eos(GMimeStream *stream);
static int stream_reset(GMimeStream *stream);
static gint64 stream_seek(GMimeStream *stream,
			  gint64 offset,
			  GMimeSeekWhence whence);
static gint64 stream_tell(GMimeStream *stream);
#if defined(HAVE_GMIME_2_6)
static gint64 stream_length(GMimeStream *stream);
#else                           /* HAVE_GMIME_2_6 */
static ssize_t stream_length(GMimeStream *stream);
#endif                          /* HAVE_GMIME_2_6 */
static GMimeStream *stream_substream(GMimeStream *stream,
				     gint64 start,
				     gint64 end);


static GMimeStreamClass *parent_class = NULL;

#define GIO_DEBUG  1

#ifdef GIO_DEBUG
#define GIO_DEBUG_INIT   GError * gioerr = NULL
#define GIO_DEBUG_OUT    &gioerr
#define GIO_DEBUG_MSG(textmsg)						\
    do {								\
	if (gioerr) {							\
	    g_debug("%s::%s::%d " textmsg ": err %d: %s\n",		\
		    __FILE__, __FUNCTION__, __LINE__,			\
		    gioerr->code, gioerr->message);			\
	    g_clear_error(&gioerr);					\
	}								\
    } while(0)
#else
#define GIO_DEBUG_INIT
#define GIO_DEBUG_OUT    NULL
#define GIO_DEBUG_MSG    
#endif


GType
g_mime_stream_gio_get_type(void)
{
    static GType type = 0;
	
    if (!type) {
	static const GTypeInfo info = {
	    sizeof (GMimeStreamGioClass),
	    NULL, /* base_class_init */
	    NULL, /* base_class_finalize */
	    (GClassInitFunc) g_mime_stream_gio_class_init,
	    NULL, /* class_finalize */
	    NULL, /* class_data */
	    sizeof (GMimeStreamGio),
	    0,    /* n_preallocs */
	    (GInstanceInitFunc) g_mime_stream_gio_init,
	};
		
	type = g_type_register_static(GMIME_TYPE_STREAM, "GMimeStreamGio", &info, 0);
    }
	
    return type;
}


static void
g_mime_stream_gio_class_init(GMimeStreamGioClass *klass)
{
    GMimeStreamClass *stream_class = GMIME_STREAM_CLASS(klass);
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
	
    parent_class = g_type_class_ref(GMIME_TYPE_STREAM);
	
    object_class->finalize = g_mime_stream_gio_finalize;
	
    stream_class->read = stream_read;
    stream_class->write = stream_write;
    stream_class->flush = stream_flush;
    stream_class->close = stream_close;
    stream_class->eos = stream_eos;
    stream_class->reset = stream_reset;
    stream_class->seek = stream_seek;
    stream_class->tell = stream_tell;
    stream_class->length = stream_length;
    stream_class->substream = stream_substream;
}

static void
g_mime_stream_gio_init(GMimeStreamGio *stream,
		       GMimeStreamGioClass *klass)
{
    stream->eos = FALSE;
    stream->gfile = NULL;
    stream->stream = NULL;
}

static void
g_mime_stream_gio_finalize(GObject *object)
{
    GMimeStreamGio *stream = (GMimeStreamGio *) object;
    
    g_return_if_fail(stream);
    if (stream->stream) {
	g_object_unref(stream->stream);  // will also call g_(input|output)_stream_close
	stream->stream = NULL;
    }
    
    if (stream->gfile)
	g_object_unref(G_OBJECT(stream->gfile));

    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static ssize_t
stream_read(GMimeStream *stream, char *buf, size_t len)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
    gsize nread;
    gboolean result;
    GIO_DEBUG_INIT;
	
    g_return_val_if_fail(stream, -1);
    if (stream->bound_end != -1 && stream->position >= stream->bound_end)
	return -1;
	
    if (stream->bound_end != -1)
	len = MIN (stream->bound_end - stream->position, (gint64) len);
    
    /* try to create the stream if necessary */
    if (!gios->stream) {
	gios->stream = G_OBJECT(g_file_read(gios->gfile, NULL, GIO_DEBUG_OUT));
	GIO_DEBUG_MSG("g_file_read");
	if (!gios->stream)
	    return -1;
    }

    /* make sure we are at the right position */
    g_seekable_seek(G_SEEKABLE(gios->stream), (goffset) stream->position,
		    G_SEEK_SET, NULL, NULL);

    result = g_input_stream_read_all(G_INPUT_STREAM(gios->stream), buf, len, 
				     &nread, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_input_stream_read_all");
	
    if (result)
	stream->position += nread;
    if (nread == 0)
	gios->eos = TRUE;
	
    return nread;
}

static ssize_t
stream_write(GMimeStream *stream, const char *buf, size_t len)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
    gsize nwritten;
    gboolean result;
    GIO_DEBUG_INIT;
	
    g_return_val_if_fail(stream, -1);
    if (stream->bound_end != -1 && stream->position >= stream->bound_end)
	return -1;
	
    if (stream->bound_end != -1)
	len = MIN (stream->bound_end - stream->position, (gint64) len);
    
    /* try to create the stream if necessary */
    if (!gios->stream) {
	gios->stream = G_OBJECT(g_file_append_to(gios->gfile, G_FILE_CREATE_NONE, NULL, GIO_DEBUG_OUT));
	GIO_DEBUG_MSG("g_file_append_to");
	if (!gios->stream)
	    return -1;
    }
	
    /* make sure we are at the right position */
    g_seekable_seek(G_SEEKABLE(gios->stream), (goffset) stream->position,
		    G_SEEK_SET, NULL, NULL);
    
    result = g_output_stream_write_all(G_OUTPUT_STREAM(gios->stream), buf, len,
				       &nwritten, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_output_stream_write_all");
	
    if (result)
	stream->position += nwritten;
    else 
	return -1;
	
    return nwritten;
}

static int
stream_flush(GMimeStream *stream)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
    gboolean result;
    GIO_DEBUG_INIT;
    
    g_return_val_if_fail(stream, -1);
    g_return_val_if_fail(G_IS_OUTPUT_STREAM(gios->stream), -1);
    result = g_output_stream_flush(G_OUTPUT_STREAM(gios->stream), NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_output_stream_flush");
    return result ? 0 : -1;
}

static int
stream_close(GMimeStream *stream)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;

    g_return_val_if_fail(stream, -1);
    if (gios->stream) {
	g_object_unref(gios->stream);
	gios->stream = NULL;
    }
    
    return 0;
}

static gboolean
stream_eos(GMimeStream *stream)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
	
    g_return_val_if_fail(stream, TRUE);
    if (gios->stream)
        g_return_val_if_fail(G_IS_INPUT_STREAM(gios->stream), TRUE);
	
    return gios->eos;
}

static int
stream_reset(GMimeStream *stream)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
    gboolean result;
    GIO_DEBUG_INIT;

    g_return_val_if_fail(stream, -1);
    if (!gios->stream)
	return -1;
	
    if (stream->position == stream->bound_start) {
	gios->eos = FALSE;
	return 0;
    }
    
    result = g_seekable_seek(G_SEEKABLE(gios->stream), (goffset)stream->bound_start,
			     G_SEEK_SET, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_seekable_seek");

    if (!result)
	return -1;
	
    gios->eos = FALSE;
    return 0;
}

static gint64
stream_seek(GMimeStream *stream, gint64 offset, GMimeSeekWhence whence)
{
    GMimeStreamGio *gios = (GMimeStreamGio *) stream;
    goffset real;
    gboolean result;
    GIO_DEBUG_INIT;
	
    g_return_val_if_fail(stream, -1);
    g_return_val_if_fail(gios->stream, -1);
	
    switch (whence) {
    case GMIME_STREAM_SEEK_SET:
	real = offset;
	break;
    case GMIME_STREAM_SEEK_CUR:
	real = stream->position + offset;
	break;
    case GMIME_STREAM_SEEK_END:
	if (offset > 0 || (stream->bound_end == -1 && !gios->eos)) {
	    /* need to do an actual lseek() here because
	     * we either don't know the offset of the end
	     * of the stream and/or don't know if we can
	     * seek past the end */
	    result = g_seekable_seek(G_SEEKABLE(gios->stream), (goffset)offset,
				     G_SEEK_END, NULL, GIO_DEBUG_OUT);
	    GIO_DEBUG_MSG("g_seekable_seek");
	    if (!result)
		return -1;
	    else
		real = g_seekable_tell(G_SEEKABLE(gios->stream));
	} else if (gios->eos && stream->bound_end == -1) {
	    /* seeking backwards from eos (which happens
	     * to be our current position) */
	    real = stream->position + offset;
	} else {
	    /* seeking backwards from a known position */
	    real = stream->bound_end + offset;
	}
		
	break;
    default:
	g_assert_not_reached ();
	return -1;
    }
	
    /* sanity check the resultant offset */
    if (real < stream->bound_start)
	return -1;
	
    /* short-cut if we are seeking to our current position */
    if (real == stream->position)
	return real;
	
    if (stream->bound_end != -1 && real > stream->bound_end)
	return -1;
    
    result = g_seekable_seek(G_SEEKABLE(gios->stream), (goffset) real,
			     G_SEEK_SET, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_seekable_seek");
    if (!result)
	return -1;
    real = g_seekable_tell(G_SEEKABLE(gios->stream));
	
    /* reset eos if appropriate */
    if ((stream->bound_end != -1 && real < stream->bound_end) ||
	(gios->eos && real < stream->position))
	gios->eos = FALSE;
	
    stream->position = real;
	
    return real;
}

static gint64
stream_tell (GMimeStream *stream)
{
    g_return_val_if_fail(stream, -1);
    return stream->position;
}

#if defined(HAVE_GMIME_2_6)
static gint64
#else                           /* HAVE_GMIME_2_6 */
static ssize_t
#endif                          /* HAVE_GMIME_2_6 */
stream_length(GMimeStream *stream)
{
    goffset bound_end;
    GFileInputStream *istream;
    gboolean sres;
    GIO_DEBUG_INIT;
	
    g_return_val_if_fail(stream, -1);
    if (stream->bound_end != -1)
	return stream->bound_end - stream->bound_start;
    
    istream = g_file_read(GMIME_STREAM_GIO(stream)->gfile, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_file_read");
    if (!istream)
	return -1;

    sres = g_seekable_seek(G_SEEKABLE(istream), (goffset) 0, G_SEEK_END, NULL, GIO_DEBUG_OUT);
    GIO_DEBUG_MSG("g_seekable_seek");
    if (!sres) {
	g_object_unref(istream);
	return -1;
    }
    bound_end = g_seekable_tell(G_SEEKABLE(istream));
    g_object_unref(istream);
	
    if (bound_end < stream->bound_start)
	return -1;
	
    return bound_end - stream->bound_start;
}

static GMimeStream *
stream_substream(GMimeStream *stream, gint64 start, gint64 end)
{
    GMimeStreamGio *gios;
	
    g_return_val_if_fail(stream, NULL);
    gios = g_object_newv(GMIME_TYPE_STREAM_GIO, 0, NULL);
    g_mime_stream_construct (GMIME_STREAM(gios), start, end);
    gios->gfile = GMIME_STREAM_GIO(stream)->gfile;
    g_object_ref(G_OBJECT(gios->gfile));
    gios->eos = FALSE;
    gios->stream = NULL;
	
    return (GMimeStream *) gios;
}


/**
 * g_mime_stream_gio_new:
 * @gfile: GIO File
 *
 * Creates a new #GMimeStreamGio object around @gfile.
 *
 * Returns: a stream using @fd.
 **/
GMimeStream *
g_mime_stream_gio_new(GFile * gfile)
{
    GMimeStreamGio *gios;
	
    g_return_val_if_fail(gfile, NULL);
    gios = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
    g_mime_stream_construct(GMIME_STREAM (gios), 0, -1);
    gios->eos = FALSE;
    gios->gfile = gfile;
    g_object_ref(G_OBJECT(gios->gfile));
    gios->stream = NULL;
	
    return (GMimeStream *) gios;
}


/**
 * g_mime_stream_gio_new_with_bounds:
 * @fd: file descriptor
 * @start: start boundary
 * @end: end boundary
 *
 * Creates a new #GMimeStreamGio object around @fd with bounds @start
 * and @end.
 *
 * Returns: a stream using @fd with bounds @start and @end.
 **/
GMimeStream *
g_mime_stream_gio_new_with_bounds(GFile * gfile, gint64 start, gint64 end)
{
    GMimeStreamGio *gios;
	
    g_return_val_if_fail(gfile, NULL);
    gios = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
    g_mime_stream_construct (GMIME_STREAM (gios), start, end);
    gios->eos = FALSE;
    gios->gfile = gfile;
    g_object_ref(G_OBJECT(gios->gfile));
    gios->stream = NULL;
	
    return (GMimeStream *) gios;
}