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
28 #include <sys/types.h>
34 #include "gmime-stream-pipe.h"
38 * SECTION: gmime-stream-pipe
39 * @title: GMimeStreamPipe
40 * @short_description: A low-level pipe stream
41 * @see_also: #GMimeStream
43 * A simple #GMimeStream implementation that sits on top of low-level
48 static void g_mime_stream_pipe_class_init (GMimeStreamPipeClass *klass);
49 static void g_mime_stream_pipe_init (GMimeStreamPipe *stream, GMimeStreamPipeClass *klass);
50 static void g_mime_stream_pipe_finalize (GObject *object);
52 static ssize_t stream_read (GMimeStream *stream, char *buf, size_t len);
53 static ssize_t stream_write (GMimeStream *stream, const char *buf, size_t len);
54 static int stream_flush (GMimeStream *stream);
55 static int stream_close (GMimeStream *stream);
56 static gboolean stream_eos (GMimeStream *stream);
57 static int stream_reset (GMimeStream *stream);
58 static gint64 stream_seek (GMimeStream *stream, gint64 offset, GMimeSeekWhence whence);
59 static gint64 stream_tell (GMimeStream *stream);
60 static gint64 stream_length (GMimeStream *stream);
61 static GMimeStream *stream_substream (GMimeStream *stream, gint64 start, gint64 end);
64 static GMimeStreamClass *parent_class = NULL;
68 g_mime_stream_pipe_get_type (void)
70 static GType type = 0;
73 static const GTypeInfo info = {
74 sizeof (GMimeStreamPipeClass),
75 NULL, /* base_class_init */
76 NULL, /* base_class_finalize */
77 (GClassInitFunc) g_mime_stream_pipe_class_init,
78 NULL, /* class_finalize */
79 NULL, /* class_data */
80 sizeof (GMimeStreamPipe),
82 (GInstanceInitFunc) g_mime_stream_pipe_init,
85 type = g_type_register_static (GMIME_TYPE_STREAM, "GMimeStreamPipe", &info, 0);
93 g_mime_stream_pipe_class_init (GMimeStreamPipeClass *klass)
95 GMimeStreamClass *stream_class = GMIME_STREAM_CLASS (klass);
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 parent_class = g_type_class_ref (GMIME_TYPE_STREAM);
100 object_class->finalize = g_mime_stream_pipe_finalize;
102 stream_class->read = stream_read;
103 stream_class->write = stream_write;
104 stream_class->flush = stream_flush;
105 stream_class->close = stream_close;
106 stream_class->eos = stream_eos;
107 stream_class->reset = stream_reset;
108 stream_class->seek = stream_seek;
109 stream_class->tell = stream_tell;
110 stream_class->length = stream_length;
111 stream_class->substream = stream_substream;
115 g_mime_stream_pipe_init (GMimeStreamPipe *stream, GMimeStreamPipeClass *klass)
117 stream->owner = TRUE;
123 g_mime_stream_pipe_finalize (GObject *object)
125 GMimeStreamPipe *stream = (GMimeStreamPipe *) object;
127 if (stream->owner && stream->fd != -1)
130 G_OBJECT_CLASS (parent_class)->finalize (object);
134 stream_read (GMimeStream *stream, char *buf, size_t len)
136 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
139 if (pipes->fd == -1) {
144 if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
149 if (stream->bound_end != -1)
150 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
153 nread = read (pipes->fd, buf, len);
154 } while (nread == -1 && errno == EINTR);
157 stream->position += nread;
158 } else if (nread == 0) {
166 stream_write (GMimeStream *stream, const char *buf, size_t len)
168 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
172 if (pipes->fd == -1) {
177 if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
182 if (stream->bound_end != -1)
183 len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
187 n = write (pipes->fd, buf + nwritten, len - nwritten);
188 } while (n == -1 && (errno == EINTR || errno == EAGAIN));
192 } while (n != -1 && nwritten < len);
194 if (n == -1 && (errno == EFBIG || errno == ENOSPC))
198 stream->position += nwritten;
199 } else if (n == -1) {
200 /* error and nothing written */
208 stream_flush (GMimeStream *stream)
210 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
212 if (pipes->fd == -1) {
221 stream_close (GMimeStream *stream)
223 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
230 if ((rv = close (pipes->fd)) == 0)
232 } while (rv == -1 && errno == EINTR);
238 stream_eos (GMimeStream *stream)
240 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
249 stream_reset (GMimeStream *stream)
251 GMimeStreamPipe *pipes = (GMimeStreamPipe *) stream;
253 if (pipes->fd == -1) {
262 stream_seek (GMimeStream *stream, gint64 offset, GMimeSeekWhence whence)
268 stream_tell (GMimeStream *stream)
274 stream_length (GMimeStream *stream)
280 stream_substream (GMimeStream *stream, gint64 start, gint64 end)
287 * g_mime_stream_pipe_new:
288 * @fd: a pipe descriptor
290 * Creates a new #GMimeStreamPipe object around @fd.
292 * Returns: a stream using @fd.
295 g_mime_stream_pipe_new (int fd)
297 GMimeStreamPipe *pipes;
299 pipes = g_object_newv (GMIME_TYPE_STREAM_PIPE, 0, NULL);
300 g_mime_stream_construct (GMIME_STREAM (pipes), 0, -1);
305 return (GMimeStream *) pipes;
310 * g_mime_stream_pipe_get_owner:
311 * @stream: a #GMimeStreamPipe
313 * Gets whether or not @stream owns the backend pipe descriptor.
315 * Returns: %TRUE if @stream owns the backend pipe descriptor or %FALSE
319 g_mime_stream_pipe_get_owner (GMimeStreamPipe *stream)
321 g_return_val_if_fail (GMIME_IS_STREAM_PIPE (stream), FALSE);
323 return stream->owner;
328 * g_mime_stream_pipe_set_owner:
329 * @stream: a #GMimeStreamPipe
332 * Sets whether or not @stream owns the backend pipe descriptor.
334 * Note: @owner should be %TRUE if the stream should close() the
335 * backend pipe descriptor when destroyed or %FALSE otherwise.
338 g_mime_stream_pipe_set_owner (GMimeStreamPipe *stream, gboolean owner)
340 g_return_if_fail (GMIME_IS_STREAM_PIPE (stream));
342 stream->owner = owner;