1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
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.
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 Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <sys/types.h>
31 #include "gmime-stream-gio.h"
35 * SECTION: gmime-stream-gio
36 * @title: GMimeStreamGIO
37 * @short_description: A wrapper for GLib's GIO streams
38 * @see_also: #GMimeStream
40 * A simple #GMimeStream implementation that sits on top of GLib's GIO
41 * input and output streams.
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);
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);
61 static GMimeStreamClass *parent_class = NULL;
65 g_mime_stream_gio_get_type (void)
67 static GType type = 0;
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),
79 (GInstanceInitFunc) g_mime_stream_gio_init,
82 type = g_type_register_static (GMIME_TYPE_STREAM, "GMimeStreamGIO", &info, 0);
90 g_mime_stream_gio_class_init (GMimeStreamGIOClass *klass)
92 GMimeStreamClass *stream_class = GMIME_STREAM_CLASS (klass);
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 parent_class = g_type_class_ref (GMIME_TYPE_STREAM);
97 object_class->finalize = g_mime_stream_gio_finalize;
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;
112 g_mime_stream_gio_init (GMimeStreamGIO *stream, GMimeStreamGIOClass *klass)
114 stream->ostream = NULL;
115 stream->istream = NULL;
118 stream->owner = TRUE;
123 g_mime_stream_gio_finalize (GObject *object)
125 GMimeStreamGIO *gio = (GMimeStreamGIO *) object;
128 g_input_stream_close (gio->istream, NULL, NULL);
129 g_object_unref (gio->istream);
133 g_output_stream_close (gio->ostream, NULL, NULL);
134 g_object_unref (gio->ostream);
137 if (gio->owner && gio->file)
138 g_object_unref (gio->file);
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 set_errno (GError *err)
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
163 case G_IO_ERROR_NOT_SUPPORTED: errno = ENOTSUP; break;
166 case G_IO_ERROR_CANCELLED: errno = ECANCELED; break;
168 case G_IO_ERROR_READ_ONLY: errno = EROFS; break;
170 case G_IO_ERROR_TIMED_OUT: errno = ETIMEDOUT; break;
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:
184 stream_read (GMimeStream *stream, char *buf, size_t len)
186 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
190 if (gio->file == NULL) {
195 if (gio->istream == NULL) {
196 /* try opening an input stream */
197 if (!(gio->istream = (GInputStream *) g_file_read (gio->file, NULL, &err))) {
203 if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
208 if (stream->bound_end != -1)
209 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
211 /* make sure we are at the right position */
212 if (G_IS_SEEKABLE (gio->istream)) {
213 GSeekable *seekable = (GSeekable *) gio->istream;
215 if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
221 if ((nread = g_input_stream_read (gio->istream, buf, len, NULL, &err)) < 0) {
227 stream->position += nread;
235 stream_write (GMimeStream *stream, const char *buf, size_t len)
237 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
241 if (gio->file == NULL) {
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))) {
254 if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
259 if (stream->bound_end != -1)
260 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
262 /* make sure we are at the right position */
263 if (G_IS_SEEKABLE (gio->ostream)) {
264 GSeekable *seekable = (GSeekable *) gio->ostream;
266 if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
272 if (!g_output_stream_write_all (gio->ostream, buf, len, &nwritten, NULL, &err)) {
277 /* nothing was written, return error */
285 stream->position += nwritten;
291 stream_flush (GMimeStream *stream)
293 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
296 if (gio->file == NULL) {
301 if (gio->ostream && !g_output_stream_flush (gio->ostream, NULL, &err)) {
310 stream_close (GMimeStream *stream)
312 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
315 g_input_stream_close (gio->istream, NULL, NULL);
316 g_object_unref (gio->istream);
321 g_output_stream_close (gio->ostream, NULL, NULL);
322 g_object_unref (gio->ostream);
326 if (gio->owner && gio->file)
327 g_object_unref (gio->file);
335 stream_eos (GMimeStream *stream)
337 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
339 if (gio->file == NULL)
346 stream_reset (GMimeStream *stream)
348 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
351 if (gio->file == NULL) {
356 if (stream->position == stream->bound_start) {
361 if (gio->istream != NULL) {
362 /* reset the input stream */
363 if (!G_IS_SEEKABLE (gio->istream)) {
368 if (!g_seekable_seek ((GSeekable *) gio->istream, stream->bound_start, G_SEEK_SET, NULL, &err)) {
374 if (gio->ostream != NULL) {
375 /* reset the output stream */
376 if (!G_IS_SEEKABLE (gio->ostream)) {
381 if (!g_seekable_seek ((GSeekable *) gio->ostream, stream->bound_start, G_SEEK_SET, NULL, &err)) {
393 gio_seekable_seek (GMimeStream *stream, GSeekable *seekable, gint64 offset, GMimeSeekWhence whence)
395 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
396 gboolean need_seek = TRUE;
401 case GMIME_STREAM_SEEK_SET:
404 case GMIME_STREAM_SEEK_CUR:
405 real = stream->position + offset;
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)) {
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;
425 /* seeking backwards from a known position */
426 real = stream->bound_end + offset;
431 g_assert_not_reached ();
435 /* sanity check the resultant offset */
436 if (real < stream->bound_start) {
441 /* short-cut if we are seeking to our current position */
442 if (real == stream->position)
445 if (stream->bound_end != -1 && real > stream->bound_end) {
450 if (need_seek && !g_seekable_seek (seekable, real, G_SEEK_SET, NULL, &err)) {
459 stream_seek (GMimeStream *stream, gint64 offset, GMimeSeekWhence whence)
461 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
464 if (gio->file == NULL) {
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))) {
476 if (gio->istream || gio->ostream) {
481 /* seek on our input stream */
482 if ((inreal = gio_seekable_seek (stream, (GSeekable *) gio->istream, offset, whence)) == -1)
485 if (gio->ostream == NULL)
490 /* seek on our output stream */
491 if ((outreal = gio_seekable_seek (stream, (GSeekable *) gio->istream, offset, whence)) == -1)
494 if (gio->istream == NULL)
498 if (outreal != inreal) {
503 /* no streams yet opened... */
505 case GMIME_STREAM_SEEK_SET:
508 case GMIME_STREAM_SEEK_CUR:
509 real = stream->position + offset;
511 case GMIME_STREAM_SEEK_END:
512 real = stream->bound_end + offset;
515 g_assert_not_reached ();
519 /* check that we haven't seekend beyond bound_end */
520 if (stream->bound_end != -1 && real > stream->bound_end) {
525 /* check that we are within the starting bounds */
526 if (real < stream->bound_start) {
532 /* reset eos if appropriate */
533 if ((stream->bound_end != -1 && real < stream->bound_end) ||
534 (gio->eos && real < stream->position))
537 stream->position = real;
543 stream_tell (GMimeStream *stream)
545 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
547 if (gio->file == NULL) {
552 return stream->position;
556 gio_seekable_bound_end (GMimeStream *stream, GSeekable *seekable)
561 if (!g_seekable_seek (seekable, 0, G_SEEK_END, NULL, &err)) {
566 bound_end = g_seekable_tell (seekable);
567 if (bound_end < stream->bound_start) {
572 if (!g_seekable_seek (seekable, stream->position, G_SEEK_SET, NULL, &err)) {
581 stream_length (GMimeStream *stream)
583 GMimeStreamGIO *gio = (GMimeStreamGIO *) stream;
586 if (gio->file == NULL) {
591 if (stream->bound_end != -1)
592 return stream->bound_end - stream->bound_start;
594 if (gio->istream && G_IS_SEEKABLE (gio->istream)) {
595 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->istream)) == -1)
597 } else if (gio->ostream && G_IS_SEEKABLE (gio->ostream)) {
598 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->ostream)) == -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))) {
607 if ((bound_end = gio_seekable_bound_end (stream, (GSeekable *) gio->istream)) == -1)
610 /* neither of our streams is seekable, can't get the length */
615 return bound_end - stream->bound_start;
619 stream_substream (GMimeStream *stream, gint64 start, gint64 end)
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;
629 return (GMimeStream *) gio;
634 * g_mime_stream_gio_new:
637 * Creates a new #GMimeStreamGIO wrapper around a #GFile object.
639 * Returns: a stream using @file.
642 g_mime_stream_gio_new (GFile *file)
646 g_return_val_if_fail (G_IS_FILE (file), NULL);
648 gio = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
649 g_mime_stream_construct (GMIME_STREAM (gio), 0, -1);
654 return (GMimeStream *) gio;
659 * g_mime_stream_gio_new_with_bounds:
661 * @start: start boundary
664 * Creates a new #GMimeStreamGIO stream around a #GFile with bounds
667 * Returns: a stream using @file with bounds @start and @end.
670 g_mime_stream_gio_new_with_bounds (GFile *file, gint64 start, gint64 end)
674 g_return_val_if_fail (G_IS_FILE (file), NULL);
676 gio = g_object_newv (GMIME_TYPE_STREAM_GIO, 0, NULL);
677 g_mime_stream_construct (GMIME_STREAM (gio), start, end);
682 return (GMimeStream *) gio;
687 * g_mime_stream_gio_get_owner:
688 * @stream: a #GMimeStreamGIO stream
690 * Gets whether or not @stream owns the backend #GFile.
692 * Returns: %TRUE if @stream owns the backend #GFile or %FALSE
696 g_mime_stream_gio_get_owner (GMimeStreamGIO *stream)
698 g_return_val_if_fail (GMIME_IS_STREAM_GIO (stream), FALSE);
700 return stream->owner;
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
709 * Sets whether or not @stream owns the backend GIO pointer.
711 * Note: @owner should be %TRUE if the stream should close() the
712 * backend file descriptor when destroyed or %FALSE otherwise.
715 g_mime_stream_gio_set_owner (GMimeStreamGIO *stream, gboolean owner)
717 g_return_if_fail (GMIME_IS_STREAM_GIO (stream));
719 stream->owner = owner;