Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-stream-gio.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
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 License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  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 Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #include "gmime-stream-gio.h"
32
33
34 /**
35  * SECTION: gmime-stream-gio
36  * @title: GMimeStreamGIO
37  * @short_description: A wrapper for GLib's GIO streams
38  * @see_also: #GMimeStream
39  *
40  * A simple #GMimeStream implementation that sits on top of GLib's GIO
41  * input and output streams.
42  **/
43
44
45 static void g_mime_stream_gio_class_init (GMimeStreamGIOClass *klass);
46 static void g_mime_stream_gio_init (GMimeStreamGIO *stream, GMimeStreamGIOClass *klass);
47 static void g_mime_stream_gio_finalize (GObject *object);
48
49 static ssize_t stream_read (GMimeStream *stream, char *buf, size_t len);
50 static ssize_t stream_write (GMimeStream *stream, const char *buf, size_t len);
51 static int stream_flush (GMimeStream *stream);
52 static int stream_close (GMimeStream *stream);
53 static gboolean stream_eos (GMimeStream *stream);
54 static int stream_reset (GMimeStream *stream);
55 static gint64 stream_seek (GMimeStream *stream, gint64 ofgioet, GMimeSeekWhence whence);
56 static gint64 stream_tell (GMimeStream *stream);
57 static gint64 stream_length (GMimeStream *stream);
58 static GMimeStream *stream_substream (GMimeStream *stream, gint64 start, gint64 end);
59
60
61 static GMimeStreamClass *parent_class = NULL;
62
63
64 GType
65 g_mime_stream_gio_get_type (void)
66 {
67         static GType type = 0;
68         
69         if (!type) {
70                 static const GTypeInfo info = {
71                         sizeof (GMimeStreamGIOClass),
72                         NULL, /* base_class_init */
73                         NULL, /* base_class_finalize */
74                         (GClassInitFunc) g_mime_stream_gio_class_init,
75                         NULL, /* class_finalize */
76                         NULL, /* class_data */
77                         sizeof (GMimeStreamGIO),
78                         0,    /* n_preallocs */
79                         (GInstanceInitFunc) g_mime_stream_gio_init,
80                 };
81                 
82                 type = g_type_register_static (GMIME_TYPE_STREAM, "GMimeStreamGIO", &info, 0);
83         }
84         
85         return type;
86 }
87
88
89 static void
90 g_mime_stream_gio_class_init (GMimeStreamGIOClass *klass)
91 {
92         GMimeStreamClass *stream_class = GMIME_STREAM_CLASS (klass);
93         GObjectClass *object_class = G_OBJECT_CLASS (klass);
94         
95         parent_class = g_type_class_ref (GMIME_TYPE_STREAM);
96         
97         object_class->finalize = g_mime_stream_gio_finalize;
98         
99         stream_class->read = stream_read;
100         stream_class->write = stream_write;
101         stream_class->flush = stream_flush;
102         stream_class->close = stream_close;
103         stream_class->eos = stream_eos;
104         stream_class->reset = stream_reset;
105         stream_class->seek = stream_seek;
106         stream_class->tell = stream_tell;
107         stream_class->length = stream_length;
108         stream_class->substream = stream_substream;
109 }
110
111 static void
112 g_mime_stream_gio_init (GMimeStreamGIO *stream, GMimeStreamGIOClass *klass)
113 {
114         stream->ostream = NULL;
115         stream->istream = NULL;
116         stream->file = NULL;
117         
118         stream->owner = TRUE;
119         stream->eos = FALSE;
120 }
121
122 static void
123 g_mime_stream_gio_finalize (GObject *object)
124 {
125         GMimeStreamGIO *gio = (GMimeStreamGIO *) object;
126         
127         if (gio->istream) {
128                 g_input_stream_close (gio->istream, NULL, NULL);
129                 g_object_unref (gio->istream);
130         }
131         
132         if (gio->ostream) {
133                 g_output_stream_close (gio->ostream, NULL, NULL);
134                 g_object_unref (gio->ostream);
135         }
136         
137         if (gio->owner && gio->file)
138                 g_object_unref (gio->file);
139         
140         G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 static void
144 set_errno (GError *err)
145 {
146         if (!err) {
147                 errno = 0;
148                 return;
149         }
150         
151         switch (err->code) {
152         case G_IO_ERROR_NOT_FOUND: errno = ENOENT; break;
153         case G_IO_ERROR_EXISTS: errno = EEXIST; break;
154         case G_IO_ERROR_IS_DIRECTORY: errno = EISDIR; break;
155         case G_IO_ERROR_NOT_DIRECTORY: errno = ENOTDIR; break;
156         case G_IO_ERROR_NOT_EMPTY: errno = ENOTEMPTY; break;
157         case G_IO_ERROR_FILENAME_TOO_LONG: errno = ENAMETOOLONG; break;
158         case G_IO_ERROR_TOO_MANY_LINKS: errno = EMLINK; break;
159         case G_IO_ERROR_NO_SPACE: errno = ENOSPC; break; // or ENOMEM
160         case G_IO_ERROR_INVALID_ARGUMENT: errno = EINVAL; break;
161         case G_IO_ERROR_PERMISSION_DENIED: errno = EACCES; break; // or EPERM
162 #ifdef ENOTSUP
163         case G_IO_ERROR_NOT_SUPPORTED: errno = ENOTSUP; break;
164 #endif
165 #ifdef ECANCELED
166         case G_IO_ERROR_CANCELLED: errno = ECANCELED; break;
167 #endif
168         case G_IO_ERROR_READ_ONLY: errno = EROFS; break;
169 #ifdef ETIMEDOUT
170         case G_IO_ERROR_TIMED_OUT: errno = ETIMEDOUT; break;
171 #endif
172         case G_IO_ERROR_BUSY: errno = EBUSY; break;
173         case G_IO_ERROR_WOULD_BLOCK: errno = EAGAIN; break;
174         case G_IO_ERROR_FAILED:
175         default:
176                 errno = EIO;
177                 break;
178         }
179         
180         g_error_free (err);
181 }
182
183 static ssize_t
184 stream_read (GMimeStream *stream, char *buf, size_t len)
185 {
186         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
187         GError *err = NULL;
188         ssize_t nread;
189         
190         if (gio->file == NULL) {
191                 errno = EBADF;
192                 return -1;
193         }
194         
195         if (gio->istream == NULL) {
196                 /* try opening an input stream */
197                 if (!(gio->istream = (GInputStream *) g_file_read (gio->file, NULL, &err))) {
198                         set_errno (err);
199                         return -1;
200                 }
201         }
202         
203         if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
204                 errno = EINVAL;
205                 return -1;
206         }
207         
208         if (stream->bound_end != -1)
209                 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
210         
211         /* make sure we are at the right position */
212         if (G_IS_SEEKABLE (gio->istream)) {
213                 GSeekable *seekable = (GSeekable *) gio->istream;
214                 
215                 if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
216                         set_errno (err);
217                         return -1;
218                 }
219         }
220         
221         if ((nread = g_input_stream_read (gio->istream, buf, len, NULL, &err)) < 0) {
222                 set_errno (err);
223                 return -1;
224         }
225         
226         if (nread > 0)
227                 stream->position += nread;
228         else if (nread == 0)
229                 gio->eos = TRUE;
230         
231         return nread;
232 }
233
234 static ssize_t
235 stream_write (GMimeStream *stream, const char *buf, size_t len)
236 {
237         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
238         size_t nwritten = 0;
239         GError *err = NULL;
240         
241         if (gio->file == NULL) {
242                 errno = EBADF;
243                 return -1;
244         }
245         
246         if (gio->ostream == NULL) {
247                 /* try opening an output stream */
248                 if (!(gio->ostream = (GOutputStream *) g_file_append_to (gio->file, G_FILE_CREATE_NONE, NULL, &err))) {
249                         set_errno (err);
250                         return -1;
251                 }
252         }
253         
254         if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
255                 errno = EINVAL;
256                 return -1;
257         }
258         
259         if (stream->bound_end != -1)
260                 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
261         
262         /* make sure we are at the right position */
263         if (G_IS_SEEKABLE (gio->ostream)) {
264                 GSeekable *seekable = (GSeekable *) gio->ostream;
265                 
266                 if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
267                         set_errno (err);
268                         return -1;
269                 }
270         }
271         
272         if (!g_output_stream_write_all (gio->ostream, buf, len, &nwritten, NULL, &err)) {
273                 set_errno (err);
274                 gio->eos = TRUE;
275                 
276                 if (nwritten == 0) {
277                         /* nothing was written, return error */
278                         return -1;
279                 }
280                 
281                 errno = 0;
282         }
283         
284         if (nwritten > 0)
285                 stream->position += nwritten;
286         
287         return nwritten;
288 }
289
290 static int
291 stream_flush (GMimeStream *stream)
292 {
293         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
294         GError *err = NULL;
295         
296         if (gio->file == NULL) {
297                 errno = EBADF;
298                 return -1;
299         }
300         
301         if (gio->ostream && !g_output_stream_flush (gio->ostream, NULL, &err)) {
302                 set_errno (err);
303                 return -1;
304         }
305         
306         return 0;
307 }
308
309 static int
310 stream_close (GMimeStream *stream)
311 {
312         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
313         
314         if (gio->istream) {
315                 g_input_stream_close (gio->istream, NULL, NULL);
316                 g_object_unref (gio->istream);
317                 gio->istream = NULL;
318         }
319         
320         if (gio->ostream) {
321                 g_output_stream_close (gio->ostream, NULL, NULL);
322                 g_object_unref (gio->ostream);
323                 gio->ostream = NULL;
324         }
325         
326         if (gio->owner && gio->file)
327                 g_object_unref (gio->file);
328         
329         gio->file = NULL;
330         
331         return 0;
332 }
333
334 static gboolean
335 stream_eos (GMimeStream *stream)
336 {
337         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
338         
339         if (gio->file == NULL)
340                 return TRUE;
341         
342         return gio->eos;
343 }
344
345 static int
346 stream_reset (GMimeStream *stream)
347 {
348         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
349         GError *err = NULL;
350         
351         if (gio->file == NULL) {
352                 errno = EBADF;
353                 return -1;
354         }
355         
356         if (stream->position == stream->bound_start) {
357                 gio->eos = FALSE;
358                 return 0;
359         }
360         
361         if (gio->istream != NULL) {
362                 /* reset the input stream */
363                 if (!G_IS_SEEKABLE (gio->istream)) {
364                         errno = EINVAL;
365                         return -1;
366                 }
367                 
368                 if (!g_seekable_seek ((GSeekable *) gio->istream, stream->bound_start, G_SEEK_SET, NULL, &err)) {
369                         set_errno (err);
370                         return -1;
371                 }
372         }
373         
374         if (gio->ostream != NULL) {
375                 /* reset the output stream */
376                 if (!G_IS_SEEKABLE (gio->ostream)) {
377                         errno = EINVAL;
378                         return -1;
379                 }
380                 
381                 if (!g_seekable_seek ((GSeekable *) gio->ostream, stream->bound_start, G_SEEK_SET, NULL, &err)) {
382                         set_errno (err);
383                         return -1;
384                 }
385         }
386         
387         gio->eos = FALSE;
388         
389         return 0;
390 }
391
392 static gint64
393 gio_seekable_seek (GMimeStream *stream, GSeekable *seekable, gint64 offset, GMimeSeekWhence whence)
394 {
395         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
396         gboolean need_seek = TRUE;
397         GError *err = NULL;
398         gint64 real;
399         
400         switch (whence) {
401         case GMIME_STREAM_SEEK_SET:
402                 real = offset;
403                 break;
404         case GMIME_STREAM_SEEK_CUR:
405                 real = stream->position + offset;
406                 break;
407         case GMIME_STREAM_SEEK_END:
408                 if (offset > 0 || (stream->bound_end == -1 && !gio->eos)) {
409                         /* need to do an actual lseek() here because
410                          * we either don't know the offset of the end
411                          * of the stream and/or don't know if we can
412                          * seek past the end */
413                         if (!g_seekable_seek (seekable, offset, G_SEEK_END, NULL, &err)) {
414                                 set_errno (err);
415                                 return -1;
416                         }
417                         
418                         need_seek = FALSE;
419                         real = offset;
420                 } else if (gio->eos && stream->bound_end == -1) {
421                         /* seeking backwards from eos (which happens
422                          * to be our current position) */
423                         real = stream->position + offset;
424                 } else {
425                         /* seeking backwards from a known position */
426                         real = stream->bound_end + offset;
427                 }
428                 
429                 break;
430         default:
431                 g_assert_not_reached ();
432                 return -1;
433         }
434         
435         /* sanity check the resultant offset */
436         if (real < stream->bound_start) {
437                 errno = EINVAL;
438                 return -1;
439         }
440         
441         /* short-cut if we are seeking to our current position */
442         if (real == stream->position)
443                 return real;
444         
445         if (stream->bound_end != -1 && real > stream->bound_end) {
446                 errno = EINVAL;
447                 return -1;
448         }
449         
450         if (need_seek && !g_seekable_seek (seekable, real, G_SEEK_SET, NULL, &err)) {
451                 set_errno (err);
452                 return -1;
453         }
454         
455         return real;
456 }
457
458 static gint64
459 stream_seek (GMimeStream *stream, gint64 offset, GMimeSeekWhence whence)
460 {
461         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
462         gint64 real;
463         
464         if (gio->file == NULL) {
465                 errno = EBADF;
466                 return -1;
467         }
468         
469         /* if either of our streams are unseekable, fail */
470         if ((gio->istream != NULL && !G_IS_SEEKABLE (gio->istream)) ||
471             (gio->ostream != NULL && !G_IS_SEEKABLE (gio->ostream))) {
472                 errno = EINVAL;
473                 return -1;
474         }
475         
476         if (gio->istream || gio->ostream) {
477                 gint64 outreal = -1;
478                 gint64 inreal = -1;
479                 
480                 if (gio->istream) {
481                         /* seek on our input stream */
482                         if ((inreal = gio_seekable_seek (stream, (GSeekable *) gio->istream, offset, whence)) == -1)
483                                 return -1;
484                         
485                         if (gio->ostream == NULL)
486                                 outreal = inreal;
487                 }
488                 
489                 if (gio->ostream) {
490                         /* seek on our output stream */
491                         if ((outreal = gio_seekable_seek (stream, (GSeekable *) gio->istream, offset, whence)) == -1)
492                                 return -1;
493                         
494                         if (gio->istream == NULL)
495                                 inreal = outreal;
496                 }
497                 
498                 if (outreal != inreal) {
499                 }
500                 
501                 real = outreal;
502         } else {
503                 /* no streams yet opened... */
504                 switch (whence) {
505                 case GMIME_STREAM_SEEK_SET:
506                         real = offset;
507                         break;
508                 case GMIME_STREAM_SEEK_CUR:
509                         real = stream->position + offset;
510                         break;
511                 case GMIME_STREAM_SEEK_END:
512                         real = stream->bound_end + offset;
513                         break;
514                 default:
515                         g_assert_not_reached ();
516                         return -1;
517                 }
518                 
519                 /* check that we haven't seekend beyond bound_end */
520                 if (stream->bound_end != -1 && real > stream->bound_end) {
521                         errno = EINVAL;
522                         return -1;
523                 }
524                 
525                 /* check that we are within the starting bounds */
526                 if (real < stream->bound_start) {
527                         errno = EINVAL;
528                         return -1;
529                 }
530         }
531         
532         /* reset eos if appropriate */
533         if ((stream->bound_end != -1 && real < stream->bound_end) ||
534             (gio->eos && real < stream->position))
535                 gio->eos = FALSE;
536         
537         stream->position = real;
538         
539         return real;
540 }
541
542 static gint64
543 stream_tell (GMimeStream *stream)
544 {
545         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
546         
547         if (gio->file == NULL) {
548                 errno = EBADF;
549                 return -1;
550         }
551         
552         return stream->position;
553 }
554
555 static gint64
556 gio_seekable_bound_end (GMimeStream *stream, GSeekable *seekable)
557 {
558         GError *err = NULL;
559         gint64 bound_end;
560         
561         if (!g_seekable_seek (seekable, 0, G_SEEK_END, NULL, &err)) {
562                 set_errno (err);
563                 return -1;
564         }
565         
566         bound_end = g_seekable_tell (seekable);
567         if (bound_end < stream->bound_start) {
568                 errno = EINVAL;
569                 return -1;
570         }
571         
572         if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
573                 set_errno (err);
574                 return -1;
575         }
576         
577         return bound_end;
578 }
579
580 static gint64
581 stream_length (GMimeStream *stream)
582 {
583         GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
584         gint64 bound_end;
585         
586         if (gio->file == NULL) {
587                 errno = EBADF;
588                 return -1;
589         }
590         
591         if (stream->bound_end != -1)
592                 return stream->bound_end - stream->bound_start;
593         
594         if (gio->istream && G_IS_SEEKABLE (gio->istream)) {
595                 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->istream)) == -1)
596                         return -1;
597         } else if (gio->ostream && G_IS_SEEKABLE (gio->ostream)) {
598                 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->ostream)) == -1)
599                         return -1;
600         } else if (!gio->istream && !gio->ostream) {
601                 /* try opening an input stream to get the length */
602                 if (!(gio->istream = (GInputStream *) g_file_read (gio->file, NULL, NULL))) {
603                         errno = EINVAL;
604                         return -1;
605                 }
606                 
607                 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->istream)) == -1)
608                         return -1;
609         } else {
610                 /* neither of our streams is seekable, can't get the length */
611                 errno = EINVAL;
612                 return -1;
613         }
614         
615         return bound_end - stream->bound_start;
616 }
617
618 static GMimeStream *
619 stream_substream (GMimeStream *stream, gint64 start, gint64 end)
620 {
621         GMimeStreamGIO *gio;
622         
623         gio = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
624         g_mime_stream_construct (GMIME_STREAM (gio), start, end);
625         gio->file = GMIME_STREAM_GIO (stream)->file;
626         gio->owner = FALSE;
627         gio->eos = FALSE;
628         
629         return (GMimeStream *) gio;
630 }
631
632
633 /**
634  * g_mime_stream_gio_new:
635  * @file: a #GFile
636  *
637  * Creates a new #GMimeStreamGIO wrapper around a #GFile object.
638  *
639  * Returns: a stream using @file.
640  **/
641 GMimeStream *
642 g_mime_stream_gio_new (GFile *file)
643 {
644         GMimeStreamGIO *gio;
645         
646         g_return_val_if_fail (G_IS_FILE (file), NULL);
647         
648         gio = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
649         g_mime_stream_construct (GMIME_STREAM (gio), 0, -1);
650         gio->file = file;
651         gio->owner = TRUE;
652         gio->eos = FALSE;
653         
654         return (GMimeStream *) gio;
655 }
656
657
658 /**
659  * g_mime_stream_gio_new_with_bounds:
660  * @file: a #GFile
661  * @start: start boundary
662  * @end: end boundary
663  *
664  * Creates a new #GMimeStreamGIO stream around a #GFile with bounds
665  * @start and @end.
666  *
667  * Returns: a stream using @file with bounds @start and @end.
668  **/
669 GMimeStream *
670 g_mime_stream_gio_new_with_bounds (GFile *file, gint64 start, gint64 end)
671 {
672         GMimeStreamGIO *gio;
673         
674         g_return_val_if_fail (G_IS_FILE (file), NULL);
675         
676         gio = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
677         g_mime_stream_construct (GMIME_STREAM (gio), start, end);
678         gio->file = file;
679         gio->owner = TRUE;
680         gio->eos = FALSE;
681         
682         return (GMimeStream *) gio;
683 }
684
685
686 /**
687  * g_mime_stream_gio_get_owner:
688  * @stream: a #GMimeStreamGIO stream
689  *
690  * Gets whether or not @stream owns the backend #GFile.
691  *
692  * Returns: %TRUE if @stream owns the backend #GFile or %FALSE
693  * otherwise.
694  **/
695 gboolean
696 g_mime_stream_gio_get_owner (GMimeStreamGIO *stream)
697 {
698         g_return_val_if_fail (GMIME_IS_STREAM_GIO (stream), FALSE);
699         
700         return stream->owner;
701 }
702
703
704 /**
705  * g_mime_stream_gio_set_owner:
706  * @stream: a #GMimeStreamGIO stream
707  * @owner: %TRUE if this stream should own the #GFile or %FALSE otherwise
708  *
709  * Sets whether or not @stream owns the backend GIO pointer.
710  *
711  * Note: @owner should be %TRUE if the stream should close() the
712  * backend file descriptor when destroyed or %FALSE otherwise.
713  **/
714 void
715 g_mime_stream_gio_set_owner (GMimeStreamGIO *stream, gboolean owner)
716 {
717         g_return_if_fail (GMIME_IS_STREAM_GIO (stream));
718         
719         stream->owner = owner;
720 }