Commit 1c94c0a9 1c94c0a932d228a27b7f08c728c807faa97042ee by Alain Magloire

attachment.c file_stream.c header.c io.c mailbox.c mbx_pop.c

 	mbx_unix.c mbx_unixscan.c message.c mime.c trans_stream.c
 	include/private/io0.h include/public/attribute.h
 	include/public/auth.h include/public/body.h
 	include/public/header.h include/public/io.h
 	include/public/locker.h include/public/mailbox.h
 	include/public/message.h

expunge can be call safely, whithout having to close/destroy the mailbox
it will readjust the message_t pointer.

mailbox_t uses stream to now.
1 parent 8e968a03
......@@ -61,7 +61,8 @@ int message_create_attachment(const char *content_type, const char *encoding, co
sprintf(header, MSG_HDR, content_type, encoding);
if ( ( ret = header_create( &hdr, header, strlen(header), *newmsg ) ) == 0 ) {
message_get_body(*newmsg, &body);
if ( ( ret = file_stream_create(&fstream, filename, MU_STREAM_READ) ) == 0 ) {
if ( ( ret = file_stream_create(&fstream) ) == 0 ) {
if ( ( ret = stream_open(fstream, filename, 0, MU_STREAM_READ) ) == 0 ) {
if ( ( ret = encoder_stream_create(&tstream, fstream, encoding) ) == 0 ) {
body_set_stream(body, tstream, *newmsg);
message_set_header(*newmsg, hdr, NULL);
......@@ -70,6 +71,7 @@ int message_create_attachment(const char *content_type, const char *encoding, co
}
}
}
}
if ( ret ) {
if ( *newmsg )
message_destroy(newmsg, NULL);
......
......@@ -21,6 +21,11 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <io0.h>
struct _file_stream
......@@ -44,35 +49,76 @@ _file_read (stream_t stream, char *optr, size_t osize,
off_t offset, size_t *nbytes)
{
struct _file_stream *fs = stream->owner;
size_t n;
if (fs->offset != offset)
{
fseek (fs->file, offset, SEEK_SET);
fs->offset = offset;
}
*nbytes = fread (optr, osize, 1, fs->file);
if (*nbytes == 0)
n = fread (optr, sizeof(char), osize, fs->file);
if (n == 0)
{
if (ferror(fs->file))
return errno;
}
else
fs->offset += *nbytes;
fs->offset += n;
if (nbytes)
*nbytes = n;
return 0;
}
static int
_file_readline (stream_t stream, char *optr, size_t osize,
off_t offset, size_t *nbytes)
{
struct _file_stream *fs = stream->owner;
size_t n = 0;
int err = 0;
if (fs->offset != offset)
{
fseek (fs->file, offset, SEEK_SET);
fs->offset = offset;
}
if (fgets (optr, osize, fs->file) != NULL)
{
char *tmp = optr;
while (*tmp) tmp++; /* strlen(optr) */
n = tmp - optr;
fs->offset += n;
}
else
{
if (ferror (fs->file))
err = errno;
}
if (nbytes)
*nbytes = n;
return err;
}
static int
_file_write (stream_t stream, const char *iptr, size_t isize,
off_t offset, size_t *nbytes)
{
struct _file_stream *fs = stream->owner;
size_t n;
if (fs->offset != offset)
{
fseek (fs->file, offset, SEEK_SET);
fs->offset = offset;
}
*nbytes = fwrite (iptr, isize, 1, fs->file);
n = fwrite (iptr, sizeof(char), isize, fs->file);
if (*nbytes == 0)
{
if (ferror (fs->file))
......@@ -80,47 +126,181 @@ _file_write (stream_t stream, const char *iptr, size_t isize,
}
else
fs->offset += *nbytes;
if (nbytes)
*nbytes = n;
return 0;
}
int
file_stream_create (stream_t *stream, const char *filename, int flags)
static int
_file_truncate (stream_t stream, off_t len)
{
struct _file_stream *fs;
struct _file_stream *fs = stream->owner;
if (ftruncate (fileno(fs->file), len) == -1)
return errno;
return 0;
}
static int
_file_size (stream_t stream, off_t *psize)
{
struct _file_stream *fs = stream->owner;
struct stat stbuf;
if (fstat(fileno(fs->file), &stbuf) == -1)
return errno;
if (psize)
*psize = stbuf.st_size;
return 0;
}
static int
_file_flush (stream_t stream)
{
struct _file_stream *fs = stream->owner;
return fflush (fs->file);
}
static int
_file_get_fd (stream_t stream, int *pfd)
{
struct _file_stream *fs = stream->owner;
if (pfd)
*pfd = fileno (fs->file);
return 0;
}
static int
_file_open (stream_t stream, const char *filename, int port, int flags)
{
struct _file_stream *fs = stream->owner;
int flg;
int fd;
const char *mode;
int ret;
if (stream == NULL || filename == NULL)
return EINVAL;
(void)port; /* shutup gcc */
/* map the flags to the system equivalent */
if (flags & MU_STREAM_WRITE)
flg = O_WRONLY;
else if (flags & MU_STREAM_RDWR)
flg = O_RDWR;
else /* default */
flg = O_RDONLY;
if ((fs = calloc(sizeof(struct _file_stream), 1)) == NULL)
return ENOMEM;
/* local folders should not block it is local disk ???
* We simply ignore the O_NONBLOCK flag
* But take care of the APPEND.
*/
if (flags & MU_STREAM_APPEND)
flg |= O_APPEND;
if (flags & MU_STREAM_RDWR)
/* handle CREAT with care, not to follow symlinks */
if (flags & MU_STREAM_CREAT)
{
/* first see if the file already exists */
fd = open(filename, flg);
if (fd == -1)
{
/* oops bail out */
if (errno != ENOENT)
return errno;
/* Race condition here when creating the file ?? */
fd = open(filename, flg|O_CREAT|O_EXCL, 0600);
if (fd < 0)
return errno;
}
}
else
{
fd = open (filename, flg);
if (fd < 0)
return errno;
}
/* we have to make sure that We did not open
* a symlink. From Casper D. in bugtraq.
*/
if ((flg & MU_STREAM_CREAT) ||
(flg & MU_STREAM_RDWR) ||
(flg & MU_STREAM_WRITE))
{
struct stat fdbuf, filebuf;
/* the next two stats should never fail */
if (fstat(fd, &fdbuf) == -1)
return errno;
if (lstat(filename, &filebuf) == -1)
return errno;
/* Now check that: file and fd reference the same file,
file only has one link, file is plain file */
if (fdbuf.st_dev != filebuf.st_dev ||
fdbuf.st_ino != filebuf.st_ino ||
fdbuf.st_nlink != 1 ||
filebuf.st_nlink != 1 ||
(fdbuf.st_mode & S_IFMT) != S_IFREG) {
fprintf(stderr,"%s must be a plain file with one link\n", filename);
return EINVAL;
}
}
/* we use FILE * object */
if (flags & MU_STREAM_APPEND)
mode = "a";
else if (flags & MU_STREAM_RDWR)
mode = "r+b";
else if (flags & MU_STREAM_READ)
mode = "rb";
else if (flags & MU_STREAM_WRITE)
mode = "wb";
else
return EINVAL;
else /* default readonly*/
mode = "rb";
if ((fs->file = fopen (filename, mode)) == NULL)
fs->file = fopen (filename, mode);
if (fs->file == NULL)
{
ret = errno;
int ret = errno;
free (fs);
return ret;
}
if ((ret = stream_create (stream, flags, fs)) != 0)
#if BUFSIZ <= 1024
{
char *iobuffer;
iobuffer = malloc (8192);
if (iobuffer != NULL)
if (setvbuf (file, iobuffer, _IOFBF, 8192) != 0)
free (iobuffer);
}
#endif
stream_set_flags (stream, flags |MU_STREAM_NO_CHECK, fs);
return 0;
}
int
file_stream_create (stream_t *stream)
{
struct _file_stream *fs;
int ret;
if (stream == NULL)
return EINVAL;
fs = calloc (1, sizeof (struct _file_stream));
if (fs == NULL)
return ENOMEM;
ret = stream_create (stream, MU_STREAM_NO_CHECK, fs);
if (ret != 0)
{
fclose (fs->file);
free (fs);
return ret;
}
stream_set_read(*stream, _file_read, fs );
stream_set_write(*stream, _file_write, fs );
stream_set_destroy(*stream, _file_destroy, fs );
stream_set_open (*stream, _file_open, fs);
stream_set_fd (*stream, _file_get_fd, fs);
stream_set_read (*stream, _file_read, fs);
stream_set_readline (*stream, _file_readline, fs);
stream_set_write (*stream, _file_write, fs);
stream_set_truncate (*stream, _file_truncate, fs);
stream_set_size (*stream, _file_size, fs);
stream_set_flush (*stream, _file_flush, fs);
stream_set_destroy (*stream, _file_destroy, fs);
return 0;
}
......
......@@ -273,6 +273,8 @@ header_get_value (header_t header, const char *name, char *buffer,
*buffer = '\0'; /* null terminated */
if (pn)
*pn = total;
if (total == 0)
return ENOENT;
return 0;
}
......@@ -386,13 +388,13 @@ header_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread)
{
header_t header;
ssize_t len;
int len;
if (is == NULL || (header = (header_t)is->owner) == NULL)
return EINVAL;
len = header->blurb_len - off;
if ((header->blurb_len - off) > 0)
if (len > 0)
{
if (buf)
{
......
......@@ -41,7 +41,11 @@ struct _stream
int (*_close) __P ((stream_t));
int (*_get_fd) __P ((stream_t, int *));
int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *));
int (*_readline) __P ((stream_t, char *, size_t, off_t, size_t *));
int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *));
int (*_truncate) __P ((stream_t, off_t));
int (*_size) __P ((stream_t, off_t *));
int (*_flush) __P ((stream_t));
};
#ifdef __cplusplus
......
......@@ -47,13 +47,12 @@ typedef struct _stream *stream_t;
extern int stream_create __P ((stream_t *, int flags, void *owner));
extern void stream_destroy __P ((stream_t *, void *owner));
extern int stream_set_destroy __P ((stream_t, void (*_destroy) __P ((stream_t)),
void *owner));
extern int stream_set_destroy __P ((stream_t, void (*_destroy)
__P ((stream_t)), void *owner));
extern int stream_open __P ((stream_t, const char *, int, int));
extern int stream_set_open __P ((stream_t,
int (*_open) __P ((stream_t, const char *,
int, int)),
extern int stream_set_open __P ((stream_t, int (*_open)
__P ((stream_t, const char *, int, int)),
void *owner));
extern int stream_close __P ((stream_t));
......@@ -61,30 +60,47 @@ extern int stream_set_close __P ((stream_t, int (*_close) __P ((stream_t)),
void *owner));
extern int stream_get_fd __P ((stream_t , int *));
extern int stream_set_fd __P ((stream_t,
int (*_get_fd)(stream_t, int *),
extern int stream_set_fd __P ((stream_t, int (*_get_fd)(stream_t, int *),
void *owner));
extern int stream_read __P ((stream_t, char *, size_t, off_t, size_t *));
extern int stream_set_read __P ((stream_t,
int (*_read) __P ((stream_t, char *,
size_t, off_t, size_t *)),
extern int stream_read __P ((stream_t, char *, size_t,
off_t, size_t *));
extern int stream_set_read __P ((stream_t, int (*_read)
__P ((stream_t, char *, size_t,
off_t, size_t *)),
void *owner));
extern int stream_readline __P ((stream_t, char *, size_t,
off_t, size_t *));
extern int stream_set_readline __P ((stream_t, int (*_readline)
__P ((stream_t, char *, size_t,
off_t, size_t *)),
void *owner));
extern int stream_size __P ((stream_t, off_t *));
extern int stream_set_size __P ((stream_t, int (*_size)
__P ((stream_t, off_t *)), void *owner));
extern int stream_truncate __P ((stream_t, off_t));
extern int stream_set_truncate __P ((stream_t, int (*_truncate)
__P ((stream_t, off_t)), void *owner));
extern int stream_write __P ((stream_t, const char *, size_t,
off_t, size_t *));
extern int stream_set_write __P ((stream_t,
int (*_write) __P ((stream_t, const char *,
size_t, off_t,
size_t *)),
extern int stream_set_write __P ((stream_t, int (*_write)
__P ((stream_t, const char *,
size_t, off_t, size_t *)),
void *owner));
extern int stream_flush __P ((stream_t));
extern int stream_set_flush __P ((stream_t, int (*_flush)
__P ((stream_t)), void *owner));
extern int stream_get_flags __P ((stream_t, int *pflags));
extern int stream_set_flags __P ((stream_t, int flags, void *owner));
/* misc */
extern int file_stream_create __P ((stream_t *stream, const char *filename,
int flags));
extern int file_stream_create __P ((stream_t *stream));
extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream,
const char *encoding));
extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream,
......
......@@ -47,6 +47,8 @@ typedef struct _mailbox *mailbox_t;
extern int mailbox_create __P ((mailbox_t *, const char *, int id));
extern void mailbox_destroy __P ((mailbox_t *));
extern int mailbox_create_default __P ((mailbox_t *, const char *));
/* flags for mailbox_open () */
#define MU_MAILBOX_RDONLY MU_STREAM_READ
#define MU_MAILBOX_WRONLY MU_STREAM_WRITE
......@@ -96,6 +98,7 @@ extern int mailbox_register __P ((mailbox_t mbox, size_t type,
void *arg));
extern int mailbox_deregister __P ((mailbox_t mbox, void *action));
#ifdef __cplusplus
}
#endif
......
......@@ -43,10 +43,6 @@ typedef struct _message *message_t;
/* A message is considered to be a container for:
* header_t, body_t, and its attribute_t.
* The notion of body_t is not visible/exported, since
* they are alway tied to a floating message, there was no
* need to create yet another object, getting the {i,o}stream_t
* was enough.
*/
extern int message_create __P ((message_t *, void *owner));
......@@ -71,20 +67,26 @@ extern int message_set_from __P ((message_t,
size_t, size_t *)),
void *owner));
extern int message_received __P ((message_t, char *, size_t, size_t *));
extern int message_set_received __P ((message_t,
int (*_received) __P ((message_t,
char *, size_t,
size_t *)),
void *owner));
extern int message_set_received __P ((message_t, int (*_received)
__P ((message_t, char *, size_t,
size_t *)), void *owner));
extern int message_get_attribute __P ((message_t, attribute_t *));
extern int message_set_attribute __P ((message_t, attribute_t, void *owner));
extern int message_get_num_parts __P ((message_t, size_t *nparts));
extern int message_set_get_num_parts __P ((message_t, size_t *nparts));
extern int message_get_part __P ((message_t, size_t part, message_t *msg));
extern int message_set_get_part __P ((message_t, size_t part, message_t *msg));
extern int message_add_part __P ((message_t, message_t msg));
extern int message_set_add_part __P ((message_t, message_t msg));
/* events */
#define MU_EVT_MSG_DESTROY 32
extern int message_register __P ((message_t msg, size_t type,
int (*action) (size_t typ, void *arg),
void *arg));
extern int message_register __P ((message_t msg, size_t type, int (*action)
__P ((size_t typ, void *arg)), void *arg));
extern int message_deregister __P ((message_t msg, void *action));
/* misc functions */
......
......@@ -132,9 +132,7 @@ stream_set_read (stream_t stream, int (*_read)
{
if (stream == NULL)
return EINVAL;
if (owner == stream->owner &&
((stream->flags & MU_STREAM_READ) ||
(stream->flags & MU_STREAM_RDWR)))
if (owner == stream->owner)
{
stream->_read = _read;
return 0;
......@@ -143,16 +141,28 @@ stream_set_read (stream_t stream, int (*_read)
}
int
stream_set_readline (stream_t stream, int (*_readline)
(stream_t, char *, size_t, off_t, size_t *),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (owner == stream->owner)
{
stream->_readline = _readline;
return 0;
}
return EACCES;
}
int
stream_set_write (stream_t stream, int (*_write)
__P ((stream_t, const char *, size_t, off_t, size_t *)),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner == owner &&
((stream->flags & MU_STREAM_WRITE) ||
(stream->flags & MU_STREAM_RDWR) ||
(stream->flags & MU_STREAM_APPEND)))
if (stream->owner == owner)
{
stream->_write = _write;
return 0;
......@@ -170,6 +180,46 @@ stream_read (stream_t is, char *buf, size_t count,
}
int
stream_readline (stream_t is, char *buf, size_t count,
off_t offset, size_t *pnread)
{
size_t n, nr = 0;
char c;
int status;
if (is == NULL)
return EINVAL;
if (is->_readline != NULL)
return is->_readline (is, buf, count, offset, pnread);
/* grossly inefficient hopefully they override this */
for (n = 1; n < count; n++)
{
status = stream_read (is, &c, 1, offset, &nr);
if (status != 0) /* error */
return status;
else if (nr == 1)
{
*buf++ = c;
offset++;
if (c == '\n') /* newline is stored like fgets() */
break;
}
else if (nr == 0)
{
if (n == 1) /* EOF, no data read */
n = 0;
break; /* EOF, some data was read */
}
}
*buf = '\0';
if (pnread)
*pnread = n;
return 0;
}
int
stream_write (stream_t os, const char *buf, size_t count,
off_t offset, size_t *pnwrite)
{
......@@ -205,3 +255,63 @@ stream_set_flags (stream_t stream, int fl, void *owner)
stream->flags = fl;
return 0;
}
int
stream_size (stream_t stream, off_t *psize)
{
if (stream == NULL || stream->_size == NULL)
return EINVAL;
return stream->_size (stream, psize);
}
int
stream_set_size (stream_t stream, int (*_size)(stream_t, off_t *), void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_size = _size;
return 0;
}
int
stream_truncate (stream_t stream, off_t len)
{
if (stream == NULL || stream->_truncate == NULL )
return EINVAL;
return stream->_truncate (stream, len);
}
int
stream_set_truncate (stream_t stream, int (*_truncate) (stream_t, off_t),
void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_truncate = _truncate;
return 0;
}
int
stream_flush (stream_t stream)
{
if (stream == NULL || stream->_flush == NULL)
return EINVAL;
return stream->_flush (stream);
}
int
stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner)
{
if (stream == NULL)
return EINVAL;
if (stream->owner != owner)
return EACCES;
stream->_flush = _flush;
return 0;
}
......
......@@ -256,7 +256,7 @@ mailbox_deregister (mailbox_t mbox, void *action)
for (i = 0; i < mbox->event_num; i++)
{
event = &(mbox->event[i]);
if (event->_action == action)
if ((int)event->_action == (int)action)
{
event->type = 0;
event->_action = NULL;
......
......@@ -322,7 +322,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
mailbox_pop_data_t mpd;
int status;
bio_t bio;
void *func = mailbox_pop_open;
void *func = (void *)mailbox_pop_open;
int fd;
char host[256] ;
long port;
......@@ -395,9 +395,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
}
/* Dealing whith Authentication */
/* so far only normal user/pass supported */
if (mbox->auth)
auth_authenticate (mbox->auth, &mpd->user, &mpd->passwd);
else
if (mbox->auth == NULL)
{
status = auth_create (&(mbox->auth), mbox);
if (status != 0)
......@@ -494,7 +492,7 @@ static int
mailbox_pop_close (mailbox_t mbox)
{
mailbox_pop_data_t mpd;
void *func = mailbox_pop_close;
void *func = (void *)mailbox_pop_close;
int status;
bio_t bio;
......@@ -543,7 +541,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
bio_t bio;
int status;
size_t i;
void *func = mailbox_pop_get_message;
void *func = (void *)mailbox_pop_get_message;
/* sanity */
if (mbox == NULL || pmsg == NULL || (mpd = mbox->data) == NULL)
......@@ -829,7 +827,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount)
{
mailbox_pop_data_t mpd;
int status;
void *func = mailbox_pop_messages_count;
void *func = (void *)mailbox_pop_messages_count;
bio_t bio;
if (mbox == NULL || (mpd = (mailbox_pop_data_t)mbox->data) == NULL)
......@@ -939,7 +937,7 @@ mailbox_pop_expunge (mailbox_t mbox)
attribute_t attr;
bio_t bio;
int status;
void *func = mailbox_pop_expunge;
void *func = (void *)mailbox_pop_expunge;
if (mbox == NULL ||
(mpd = (mailbox_pop_data_t) mbox->data) == NULL)
......@@ -1063,7 +1061,7 @@ mailbox_pop_readstream (stream_t is, char *buffer, size_t buflen,
size_t nread = 0;
bio_t bio;
int status = 0;
void *func = mailbox_pop_readstream;
void *func = (void *)mailbox_pop_readstream;
(void)offset;
if (is == NULL || (mpm = is->owner) == NULL)
......
......@@ -202,7 +202,7 @@ do \
} while (0)
/* notifications ADD_MESG */
#define DISPATCH_ADD_MSG(mbox,mud,file) \
#define DISPATCH_ADD_MSG(mbox,mud) \
do \
{ \
int bailing = 0; \
......@@ -212,7 +212,6 @@ do \
{ \
if (pcount) \
*pcount = (mud)->messages_count; \
fclose (file); \
mailbox_unix_unlock (mbox); \
return EINTR; \
} \
......@@ -230,7 +229,7 @@ do \
* struct incomplete. So we only tell them about
* the complete messages.
*/
#define DISPATCH_PROGRESS(mbox,mud,file) \
#define DISPATCH_PROGRESS(mbox,mud) \
do \
{ \
{ \
......@@ -243,7 +242,6 @@ do \
{ \
if (pcount) \
*pcount = (mud)->messages_count; \
fclose (file); \
mailbox_unix_unlock (mbox); \
return EINTR; \
} \
......@@ -260,7 +258,6 @@ do \
attr = calloc (1, sizeof(*(attr))); \
if ((attr) == NULL) \
{ \
fclose (file); \
mailbox_unix_iunlock (mbox); \
mailbox_unix_unlock (mbox); \
return ENOMEM; \
......@@ -269,7 +266,7 @@ do \
// size_t num = 2 * ((mud)->messages_count) + 10;
/* allocate slots for the new messages */
#define ALLOCATE_MSGS(mbox,mud,file) \
#define ALLOCATE_MSGS(mbox,mud) \
do \
{ \
if ((mud)->messages_count >= (mud)->umessages_count) \
......@@ -279,7 +276,6 @@ do \
m = realloc ((mud)->umessages, num * sizeof (*m)); \
if (m == NULL) \
{ \
fclose (file); \
mailbox_unix_iunlock (mbox); \
mailbox_unix_unlock (mbox); \
return ENOMEM; \
......@@ -288,7 +284,6 @@ do \
(mud)->umessages[num - 1] = calloc (1, sizeof (*(mum))); \
if ((mud)->umessages[num - 1] == NULL) \
{ \
fclose (file); \
mailbox_unix_iunlock (mbox); \
mailbox_unix_unlock (mbox); \
return ENOMEM; \
......@@ -299,19 +294,19 @@ do \
} while (0)
static int
mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
mailbox_unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
{
#define MSGLINELEN 1024
char buf[MSGLINELEN];
int inheader;
int inbody;
off_t total;
off_t total = 0;
mailbox_unix_data_t mud;
mailbox_unix_message_t mum = NULL;
int status = 0;
size_t lines;
int newline;
FILE *file;
size_t n = 0;
int zn, isfrom = 0;
char *temp;
......@@ -321,69 +316,36 @@ mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
(mud = (mailbox_unix_data_t)mbox->data) == NULL)
return EINVAL;
/* the simplest way to deal with reentrancy issues is to
* duplicate the FILE * pointer instead of the orignal.
*
* QnX4(and earlier ?) has a BUFSIZ of about 512 ???
* we use setvbuf () to reset to something sane.
* Really something smaller would be counter productive.
*/
{
file = fopen (mbox->name, "r");
if (file == NULL)
return errno;
#if BUFSIZ <= 1024
{
char *iobuffer;
iobuffer = malloc (8192);
if (iobuffer != NULL)
if (setvbuf (file, iobuffer, _IOFBF, 8192) != 0)
free (iobuffer);
}
#endif
}
/* save the timestamp and size */
status = stream_size (mbox->stream, &(mud->size));
if (status != 0)
return status;
/* grab the locks */
mailbox_unix_ilock (mbox, MU_LOCKER_WRLOCK);
mailbox_unix_lock (mbox, MU_LOCKER_RDLOCK);
/* save the timestamp and size */
{
struct stat st;
if (fstat (fileno (file), &st) != 0)
{
status = errno;
fclose (file);
mailbox_unix_iunlock (mbox);
mailbox_unix_unlock (mbox);
return status;
}
mud->mtime = st.st_mtime;
mud->size = st.st_size;
}
/* seek to the starting point */
if (mud->umessages)
{
if (mud->messages_count > 0 &&
msgno != 0 &&
msgno <= mud->messages_count)
if (mud->umessages && msgno > 0 && mud->messages_count > 0
&& msgno <= mud->messages_count)
{
mum = mud->umessages[msgno - 1];
if (mum)
fseek (file, mum->body_end, SEEK_SET);
mud->messages_count = msgno;
}
total = mum->header_from;
mud->messages_count = msgno - 1;
}
else
mud->messages_count = 0;
newline = 1;
errno = total = lines = inheader = inbody = 0;
errno = lines = inheader = inbody = 0;
while (fgets (buf, sizeof (buf), file))
while ((status = stream_readline (mbox->stream, buf, sizeof (buf),
total, &n)) == 0 && n != 0)
{
int over, nl;
STRLEN (buf, over);
total += over;
int nl;
STRLEN(buf, n);
total += n;
nl = (*buf == '\n') ? 1 : 0;
VALID(buf, temp, isfrom, zn);
......@@ -403,22 +365,23 @@ mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
/* signal the end of the body */
if (mum && !mum->body_end)
{
mum->body_end = total - over - newline;
mum->body_end = total - n - newline;
mum->body_lines = --lines - newline;
DISPATCH_ADD_MSG(mbox, mud, file);
if (do_notif)
DISPATCH_ADD_MSG(mbox, mud);
}
/* allocate_msgs will initialize mum */
ALLOCATE_MSGS(mbox, mud, file);
ALLOCATE_MSGS(mbox, mud);
mud->messages_count++;
mum = mud->umessages[mud->messages_count - 1];
mum->file = mud->file;
mum->header_from = total - over;
mum->stream = mbox->stream;
mum->header_from = total - n;
mum->header_from_end = total;
lines = 0;
}
else if ((over > 7) && ISSTATUS(buf))
else if ((n > 7) && ISSTATUS(buf))
{
mum->header_status = total - over;
mum->header_status = total - n;
mum->header_status_end = total;
ATTRIBUTE_SET(buf, mum, 'r', 'R', MU_ATTRIBUTE_READ);
ATTRIBUTE_SET(buf, mum, 'o', 'O', MU_ATTRIBUTE_SEEN);
......@@ -433,7 +396,7 @@ mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
/* set the body position */
if (mum && !mum->body)
{
mum->body = total - over + nl;
mum->body = total - n + nl;
mum->header_lines = lines;
lines = 0;
}
......@@ -446,25 +409,19 @@ mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
mailbox_unix_touchlock (mbox);
/* ping them every 1000 lines */
if (do_notif)
if (((lines +1) % 1000) == 0)
DISPATCH_PROGRESS(mbox, mud, file);
DISPATCH_PROGRESS(mbox, mud);
} /* while */
status = errno;
/* not an error if we reach EOF */
if (feof (file))
status = 0;
clearerr (file);
if (mum)
{
mum->body_end = total - newline;
mum->body_lines = lines - newline;
DISPATCH_ADD_MSG(mbox, mud, file);
if (do_notif)
DISPATCH_ADD_MSG(mbox, mud);
}
fclose (file);
mailbox_unix_iunlock (mbox);
mailbox_unix_unlock (mbox);
if (pcount)
......
......@@ -389,7 +389,7 @@ message_deregister (message_t msg, void *action)
for (i = 0; i < msg->event_num; i++)
{
event = &(msg->event[i]);
if (event->_action == action)
if ((int)event->_action == (int)action)
{
event->type = 0;
event->_action = NULL;
......
......@@ -328,15 +328,6 @@ static int _mime_message_fd(stream_t stream, int *fd)
return stream_get_fd(mime_part->mime->stream, fd);
}
static int _mime_new_message_read(stream_t stream, char *buf, size_t buflen, off_t off, size_t *nbytes)
{
(void)stream; (void)buf; (void)buflen; (void)off;
if ( nbytes == NULL )
return(EINVAL);
return 0;
}
static int _mime_body_size (body_t body, size_t *psize)
{
struct _mime_part *mime_part = body->owner;
......
......@@ -74,8 +74,6 @@ static int _trans_read(stream_t stream, char *optr, size_t osize, off_t offset,
*nbytes = 0;
if ( offset && ts->offset != offset )
return ESPIPE;
if ( offset == 0 )
ts->cur_offset = 0;
if ( ( iptr = alloca(isize) ) == NULL )
......@@ -89,13 +87,13 @@ static int _trans_read(stream_t stream, char *optr, size_t osize, off_t offset,
if ( ( ret = stream_read(ts->stream, iptr + ts->llen, isize - ts->llen, ts->cur_offset, &osize) ) != 0 )
return ret;
ts->cur_offset += osize;
consumed = ts->transcoder(iptr, isize, optr, nbytes);
if ( ts->llen = (isize - consumed ) ) {
consumed = ts->transcoder(iptr, osize + ts->llen, optr, nbytes);
if ( ( ts->llen = ((osize + ts->llen) - consumed ) ) )
{
if ( ( ts->leftover = malloc(ts->llen) ) == NULL )
return ENOMEM;
memcpy(ts->leftover, iptr + consumed, ts->llen);
}
ts->offset = offset;
return 0;
}
......@@ -112,7 +110,7 @@ static int _trans_write(stream_t stream, const char *iptr, size_t isize, off_t o
*nbytes = 0;
if ( offset && ts->offset != offset )
if ( offset && ts->cur_offset != offset )
return ESPIPE;
if ( offset == 0 )
ts->cur_offset = 0;
......@@ -197,19 +195,19 @@ static int _b64_input(char c)
return -1;
}
static int _b64_output(int index)
static int _b64_output(int idx)
{
const char table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (index < 64)
return table[index];
if (idx < 64)
return table[idx];
return -1;
}
int _base64_decode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
{
int i, tmp = 0;
int consumed = 0;
int i = 0, tmp = 0;
size_t consumed = 0;
char data[4];
while ( consumed < isize ) {
......@@ -227,7 +225,10 @@ int _base64_decode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
(*nbytes) += 3;
}
else // I did not get all the data
{
consumed -= i;
return consumed;
}
i = 0;
}
return consumed;
......@@ -235,7 +236,7 @@ int _base64_decode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
int _base64_encode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
{
int consumed = 0;
size_t consumed = 0;
while (consumed < (isize - 3) && (*nbytes + 4) < isize) {
*optr++ = _b64_output(*iptr >> 2);
......@@ -270,7 +271,8 @@ static int _ishex(int c)
int _qp_decode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
{
char c;
int last_char = 0, consumed = 0;
int last_char = 0;
size_t consumed = 0;
while (consumed < isize) {
c = *iptr++;
......@@ -332,8 +334,8 @@ int _qp_decode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
#define QP_LINE_MAX 76
int _qp_encode(const char *iptr, size_t isize, char *optr, size_t *nbytes)
{
int count = 0;
int consumed = 0, c;
int count = 0, c;
size_t consumed = 0;
while (consumed < isize && (*nbytes + 4) < isize) {
if (count == QP_LINE_MAX) {
......