From 65ec43cb553420890c8d14b6c76ce2606674d893 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 8 May 2000 22:27:59 +0000 Subject: Use CamelException to signal failure. (camel_stream_write_strings): * camel-stream.c (camel_stream_read, camel_stream_write, camel_stream_flush, camel_stream_reset, camel_stream_printf, camel_stream_write_to_stream): Use CamelException to signal failure. (camel_stream_write_strings): Remove. camel_stream_printf is more useful in most of the places that used this. (camel_stream_write_string): Change from macro to function to prevent problems with double-evaluation. * camel-seekable-stream.c (camel_seekable_stream_seek, camel_seekable_stream_set_bounds): Use CamelException. (reset): Update. * camel-seekable-substream.c, camel-stream-buffer.c, camel-stream-filter.c, camel-stream-fs.c, camel-stream-mem.c: Update. * camel-stream-fs.c: Remove the virtual init functions and move the code into the creator functions. Add CamelExceptions to creation functions that could fail. * camel-data-wrapper.c (camel_data_wrapper_write_to_stream): Use CamelException. * camel-mime-message.c, camel-mime-part.c, camel-multipart.c (write_to_stream): Update. * camel-mime-parser.c: add an exception to the mime parser private data and pass that to stream functions as needed. * gmime-content-field.c, md5-utils.c: Update (badly) for stream changes. * camel-exception.h (camel_exception_is_set): convenience macro. * providers/Makefile.am: disable SMTP for now * providers/mbox/camel-mbox-folder.c (mbox_append_message): Pass CamelException to the functions that now need it. Check the exception after calling camel_stream_flush, and fail if it fails. (mbox_get_message_by_uid): More updates. * providers/pop/camel-pop3-folder.c, providers/pop/camel-pop3-store.c, providers/sendmail/camel-sendmail/transport.c: Update. svn path=/trunk/; revision=2924 --- camel/camel-stream-fs.c | 344 +++++++++++++++++++++++------------------------- 1 file changed, 165 insertions(+), 179 deletions(-) (limited to 'camel/camel-stream-fs.c') diff --git a/camel/camel-stream-fs.c b/camel/camel-stream-fs.c index 4bd506b143..3f53b5907a 100644 --- a/camel/camel-stream-fs.c +++ b/camel/camel-stream-fs.c @@ -1,14 +1,14 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* camel-stream-fs.c : file system based stream */ -/* inspired by gnome-stream-fs.c in bonobo by Miguel de Icaza */ -/* + +/* * Authors: Bertrand Guiheneuf * Michael Zucchi * * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com) * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * @@ -22,8 +22,10 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ + #include #include "camel-stream-fs.h" +#include "camel-exception.h" #include #include #include @@ -31,39 +33,34 @@ #include #include -static CamelSeekableStreamClass *parent_class=NULL; +static CamelSeekableStreamClass *parent_class = NULL; /* Returns the class for a CamelStreamFS */ #define CSFS_CLASS(so) CAMEL_STREAM_FS_CLASS (GTK_OBJECT(so)->klass) -static gint stream_read (CamelStream *stream, gchar *buffer, gint n); -static gint stream_write (CamelStream *stream, const gchar *buffer, gint n); -static void stream_flush (CamelStream *stream); -static off_t stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy policy); +static int stream_read (CamelStream *stream, char *buffer, unsigned int n, + CamelException *ex); +static int stream_write (CamelStream *stream, const char *buffer, + unsigned int n, CamelException *ex); +static void stream_flush (CamelStream *stream, CamelException *ex); +static off_t stream_seek (CamelSeekableStream *stream, off_t offset, + CamelStreamSeekPolicy policy, + CamelException *ex); static void finalize (GtkObject *object); -static void init_with_fd (CamelStreamFs *stream_fs, int fd); -static void init_with_fd_and_bounds (CamelStreamFs *stream_fs, int fd, off_t start, off_t end); -static int init_with_name (CamelStreamFs *stream_fs, const gchar *name, int flags, int mode); -static int init_with_name_and_bounds (CamelStreamFs *stream_fs, const gchar *name, int flags, int mode, - off_t start, off_t end); - static void camel_stream_fs_class_init (CamelStreamFsClass *camel_stream_fs_class) { - CamelSeekableStreamClass *camel_seekable_stream_class = CAMEL_SEEKABLE_STREAM_CLASS (camel_stream_fs_class); - CamelStreamClass *camel_stream_class = CAMEL_STREAM_CLASS (camel_stream_fs_class); - GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS (camel_stream_fs_class); + CamelSeekableStreamClass *camel_seekable_stream_class = + CAMEL_SEEKABLE_STREAM_CLASS (camel_stream_fs_class); + CamelStreamClass *camel_stream_class = + CAMEL_STREAM_CLASS (camel_stream_fs_class); + GtkObjectClass *gtk_object_class = + GTK_OBJECT_CLASS (camel_stream_fs_class); parent_class = gtk_type_class (camel_seekable_stream_get_type ()); - - /* virtual method definition */ - camel_stream_fs_class->init_with_fd = init_with_fd; - camel_stream_fs_class->init_with_fd_and_bounds = init_with_fd_and_bounds; - camel_stream_fs_class->init_with_name = init_with_name; - camel_stream_fs_class->init_with_name_and_bounds = init_with_name_and_bounds; - + /* virtual method overload */ camel_stream_class->read = stream_read; camel_stream_class->write = stream_write; @@ -75,11 +72,10 @@ camel_stream_fs_class_init (CamelStreamFsClass *camel_stream_fs_class) } static void -camel_stream_fs_init (gpointer object, gpointer klass) +camel_stream_fs_init (gpointer object, gpointer klass) { CamelStreamFs *stream = CAMEL_STREAM_FS (object); - stream->name = NULL; stream->fd = -1; } @@ -87,9 +83,9 @@ GtkType camel_stream_fs_get_type (void) { static GtkType camel_stream_fs_type = 0; - - if (!camel_stream_fs_type) { - GtkTypeInfo camel_stream_fs_info = + + if (!camel_stream_fs_type) { + GtkTypeInfo camel_stream_fs_info = { "CamelStreamFs", sizeof (CamelStreamFs), @@ -100,225 +96,210 @@ camel_stream_fs_get_type (void) /* reserved_2 */ NULL, (GtkClassInitFunc) NULL, }; - + camel_stream_fs_type = gtk_type_unique (camel_seekable_stream_get_type (), &camel_stream_fs_info); } - + return camel_stream_fs_type; } -static void +static void finalize (GtkObject *object) { CamelStreamFs *stream_fs = CAMEL_STREAM_FS (object); - if (stream_fs->fd != -1) { - if (close (stream_fs->fd) == -1) - g_warning ("CamelStreamFs::finalise: Error closing file: %s", strerror (errno)); - } - g_free (stream_fs->name); + if (stream_fs->fd != -1) + close (stream_fs->fd); GTK_OBJECT_CLASS (parent_class)->finalize (object); } -static void -init_with_fd (CamelStreamFs *stream_fs, int fd) +/** + * camel_stream_fs_new_with_fd: + * @fd: a file descriptor + * + * Returns a stream associated with the given file descriptor. + * When the stream is destroyed, the file descriptor will be closed. + * + * Return value: the stream + **/ +CamelStream * +camel_stream_fs_new_with_fd (int fd) { + CamelStreamFs *stream_fs; off_t offset; + stream_fs = gtk_type_new (camel_stream_fs_get_type ()); stream_fs->fd = fd; - offset = lseek(fd, 0, SEEK_CUR); + offset = lseek (fd, 0, SEEK_CUR); if (offset == -1) offset = 0; - ((CamelSeekableStream *)stream_fs)->position = offset; -} + CAMEL_SEEKABLE_STREAM (stream_fs)->position = offset; -static void -init_with_fd_and_bounds (CamelStreamFs *stream_fs, int fd, off_t start, off_t end) -{ - - CSFS_CLASS (stream_fs)->init_with_fd (stream_fs, fd); - camel_seekable_stream_set_bounds((CamelSeekableStream *)stream_fs, start, end); + return CAMEL_STREAM (stream_fs); } -static int -init_with_name (CamelStreamFs *stream_fs, const gchar *name, int flags, int mode) +/** + * camel_stream_fs_new_with_fd_and_bounds: + * @fd: a file descriptor + * @start: the first valid position in the file + * @end: the first invalid position in the file, or CAMEL_STREAM_UNBOUND + * + * Returns a stream associated with the given file descriptor and bounds. + * When the stream is destroyed, the file descriptor will be closed. + * + * Return value: the stream + **/ +CamelStream * +camel_stream_fs_new_with_fd_and_bounds (int fd, off_t start, off_t end, + CamelException *ex) { - int fd; - - g_assert(name); - g_assert(stream_fs); - - fd = open (name, flags, mode); - if (fd==-1) { - g_warning ("CamelStreamFs::new_with_name cannot open file: %s: %s", name, strerror(errno)); - return -1; - } - - stream_fs->name = g_strdup (name); - CSFS_CLASS (stream_fs)->init_with_fd (stream_fs, fd); - return 0; -} + CamelStream *stream; -static int -init_with_name_and_bounds (CamelStreamFs *stream_fs, const gchar *name, int flags, int mode, - off_t start, off_t end) -{ - if (CSFS_CLASS (stream_fs)->init_with_name (stream_fs, name, flags, mode) == -1) - return -1; - camel_seekable_stream_set_bounds((CamelSeekableStream *)stream_fs, start, end); - return 0; + stream = camel_stream_fs_new_with_fd (fd); + camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream), + start, end, ex); + + return stream; } /** * camel_stream_fs_new_with_name: - * @name: - * @mode: - * - * - * - * Return value: + * @name: a local filename + * @flags: flags as in open(2) + * @mode: a file mode + * @ex: a CamelException. + * + * Creates a new CamelStream corresponding to the named file, flags, + * and mode. If an error occurs, @ex will be set. + * + * Return value: the stream **/ CamelStream * -camel_stream_fs_new_with_name (const gchar *name, int flags, int mode) +camel_stream_fs_new_with_name (const char *name, int flags, mode_t mode, + CamelException *ex) { - CamelStreamFs *stream_fs; - stream_fs = gtk_type_new (camel_stream_fs_get_type ()); - if (CSFS_CLASS (stream_fs)->init_with_name (stream_fs, name, flags, mode) == -1) { - gtk_object_unref (GTK_OBJECT (stream_fs)); + CamelStream *stream; + int fd; + + fd = open (name, flags, mode); + if (fd == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not open file %s: %s.", + name, g_strerror (errno)); return NULL; } - return (CamelStream *)stream_fs; + stream = camel_stream_fs_new_with_fd (fd); + + return stream; } /** * camel_stream_fs_new_with_name_and_bounds: - * @name: - * @mode: - * @inf_bound: - * @sup_bound: - * - * - * - * Return value: + * @name: a local filename + * @flags: flags as in open(2) + * @mode: a file mode + * @start: the first valid position in the file + * @end: the first invalid position in the file, or CAMEL_STREAM_UNBOUND + * @ex: a CamelException. + * + * Creates a new CamelStream corresponding to the given arguments. If + * an error occurs, @ex will be set. + * + * Return value: the stream **/ CamelStream * -camel_stream_fs_new_with_name_and_bounds (const gchar *name, int flags, int mode, - off_t start, off_t end) +camel_stream_fs_new_with_name_and_bounds (const char *name, int flags, + mode_t mode, off_t start, off_t end, + CamelException *ex) { - CamelStreamFs *stream_fs; - stream_fs = gtk_type_new (camel_stream_fs_get_type ()); - if (CSFS_CLASS (stream_fs)->init_with_name_and_bounds (stream_fs, name, flags, mode, start, end) == -1) { - gtk_object_unref (GTK_OBJECT (stream_fs)); + CamelStream *stream; + + stream = camel_stream_fs_new_with_name (name, flags, mode, ex); + if (camel_exception_is_set (ex)) + return NULL; + + camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream), + start, end, ex); + if (camel_exception_is_set (ex)) { + gtk_object_unref (GTK_OBJECT (stream)); return NULL; } - - return (CamelStream *)stream_fs; -} -/** - * camel_stream_fs_new_with_fd: - * @fd: - * - * - * - * Return value: - **/ -CamelStream * -camel_stream_fs_new_with_fd (int fd) -{ - CamelStreamFs *stream_fs; - - stream_fs = gtk_type_new (camel_stream_fs_get_type ()); - CSFS_CLASS (stream_fs)->init_with_fd (stream_fs, fd); - - return CAMEL_STREAM (stream_fs); + return stream; } -/** - * camel_stream_fs_new_with_fd_and_bounds: - * @fd: - * @inf_bound: - * @sup_bound: - * - * - * - * Return value: - **/ -CamelStream * -camel_stream_fs_new_with_fd_and_bounds (int fd, off_t start, off_t end) -{ - CamelStreamFs *stream_fs; - - stream_fs = gtk_type_new (camel_stream_fs_get_type ()); - CSFS_CLASS (stream_fs)->init_with_fd_and_bounds (stream_fs, fd, start, end); - - return CAMEL_STREAM (stream_fs); -} -static gint -stream_read (CamelStream *stream, gchar *buffer, gint n) +static int +stream_read (CamelStream *stream, char *buffer, unsigned int n, + CamelException *ex) { CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream); - CamelSeekableStream *seekable = (CamelSeekableStream *)stream; - gint v; - - g_assert (n); + CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream); + int nread; if (seekable->bound_end != CAMEL_STREAM_UNBOUND) n = MIN (seekable->bound_end - seekable->position, n); do { - v = read ( stream_fs->fd, buffer, n); - } while (v == -1 && errno == EINTR); - - if (v>0) - seekable->position += v; + nread = read (stream_fs->fd, buffer, n); + } while (nread == -1 && errno == EINTR); - if (v == 0) + if (nread > 0) + seekable->position += nread; + else if (nread == 0) stream->eos = TRUE; + else { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not read from stream: %s", + g_strerror (errno)); + } - return v; + return nread; } -static gint -stream_write (CamelStream *stream, const gchar *buffer, gint n) +static int +stream_write (CamelStream *stream, const char *buffer, unsigned int n, + CamelException *ex) { CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream); - CamelSeekableStream *seekable = (CamelSeekableStream *)stream; - int v; - gint written = 0; - - g_assert (stream); - g_assert (stream_fs->fd != -1); - - if (n <= 0) - return 0; + CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream); + int v, written = 0; if (seekable->bound_end != CAMEL_STREAM_UNBOUND) n = MIN (seekable->bound_end - seekable->position, n); do { - v = write ( stream_fs->fd, buffer, n); - if (v>0) + v = write (stream_fs->fd, buffer, n); + if (v > 0) written += v; } while (v == -1 && errno == EINTR); - - if (written>0) + + if (written > 0) seekable->position += written; + else if (v == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not write to stream: %s", + g_strerror (errno)); + } return written; } static void -stream_flush (CamelStream *stream) +stream_flush (CamelStream *stream, CamelException *ex) { - fsync ((CAMEL_STREAM_FS (stream))->fd); + if (fsync (CAMEL_STREAM_FS (stream)->fd) == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not flush stream data: %s", + g_strerror (errno)); + } } static off_t -stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy policy) +stream_seek (CamelSeekableStream *stream, off_t offset, + CamelStreamSeekPolicy policy, CamelException *ex) { CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream); off_t real = 0; @@ -335,29 +316,34 @@ stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy po real = lseek(stream_fs->fd, offset, SEEK_END); if (real != -1) stream->position = real; + else { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not seek to " + "given offset: %s", + g_strerror (errno)); + } return real; } real = stream->bound_end + offset; break; - default: - errno = EINVAL; - return -1; } - if (stream->bound_end != CAMEL_STREAM_UNBOUND) { + if (stream->bound_end != CAMEL_STREAM_UNBOUND) real = MIN (real, stream->bound_end); - } real = MAX (real, stream->bound_start); real = lseek(stream_fs->fd, real, SEEK_SET); - - if (real == -1) + if (real == -1) { + camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, + "Could not seek to given offset: %s", + g_strerror (errno)); return -1; + } if (real != stream->position && ((CamelStream *)stream)->eos) ((CamelStream *)stream)->eos = FALSE; stream->position = real; - + return real; } -- cgit v1.2.3