Commit 4cd95bc0 4cd95bc05a695c35d4c74e93631b49d13b6bbf8c by Alain Magloire

Corrected my oversights and bugs introduce last time. Keyboard quotas expired.

1 parent 24d3722e
......@@ -2,8 +2,7 @@
AUTOMAKE_OPTIONS = ../lib/ansi2knr
INCLUDES = -I${srcdir}/include/public \
-I${srcdir}/include/private
INCLUDES = -I${srcdir}/include -I${srcdir}/include -I${srcdir}/lib
CFLAGS = -Wall -pedantic -g -DTESTING
SUBDIRS = include
......
......@@ -111,8 +111,8 @@ attribute_set_seen (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_SEEN);
attr->flags |= MU_ATTRIBUTE_SEEN;
return 0;
}
......@@ -123,8 +123,8 @@ attribute_set_answered (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_ANSWERED);
attr->flags |= MU_ATTRIBUTE_ANSWERED;
return 0;
}
......@@ -135,8 +135,8 @@ attribute_set_flagged (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_FLAGGED);
attr->flags |= MU_ATTRIBUTE_FLAGGED;
return 0;
}
......@@ -147,8 +147,8 @@ attribute_set_read (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_READ);
attr->flags |= MU_ATTRIBUTE_READ;
return 0;
}
......@@ -159,8 +159,8 @@ attribute_set_deleted (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_DELETED);
attr->flags |= MU_ATTRIBUTE_DELETED;
return 0;
}
......@@ -171,8 +171,8 @@ attribute_set_draft (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_set_flags)
status = attr->_set_flags (attr, MU_ATTRIBUTE_DRAFT);
attr->flags |= MU_ATTRIBUTE_DRAFT;
return 0;
}
......@@ -183,15 +183,17 @@ attribute_set_recent (attribute_t attr)
int status = 0;
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
status = attr->_get_flags (attr, &(attr->flags));
if (attr->_unset_flags)
{
status = attr->_unset_flags (attr, MU_ATTRIBUTE_READ);
status = attr->_unset_flags (attr, MU_ATTRIBUTE_SEEN);
}
if (attr == NULL)
{
attr->flags &= ~MU_ATTRIBUTE_READ;
attr->flags &= ~MU_ATTRIBUTE_SEEN;
return 0;
}
return EACCES;
return 0;
}
int
......
......@@ -32,21 +32,23 @@ static int header_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread);
static int header_write (stream_t os, const char *buf, size_t buflen,
off_t off, size_t *pnwrite);
static int fill_blurb (header_t header);
int
header_create (header_t *ph, const char *blurb, size_t len, void *owner)
{
header_t h;
int status = 0;
h = calloc (1, sizeof (*h));
if (h == NULL)
return ENOMEM;
h->owner = owner;
/* Ignore the return value. */
header_parse (h, blurb, len);
status = header_parse (h, blurb, len);
*ph = h;
return 0;
return status;
}
void
......@@ -196,10 +198,17 @@ header_set_value (header_t header, const char *fn, const char *fv, int replace)
if (header == NULL || fn == NULL || fv == NULL)
return EINVAL;
/* Try to fill out the buffer, if we know how. */
if (header->_set_value != NULL)
return header->_set_value (header, fn, fv, replace);
/* Try to fill out the buffer, if we know how. */
if (header->blurb == NULL)
{
int err = fill_blurb (header);
if (err != 0)
return err;
}
/* Easy approach: if replace, overwrite the field-{name,value} and readjust
the pointers by calling header_parse () this is wastefull, we're just
fragmenting the memory it can be done better. But that may imply a
......@@ -265,39 +274,9 @@ header_get_value (header_t header, const char *name, char *buffer,
/* Try to fill out the buffer, if we know how. */
if (header->blurb == NULL)
{
stream_t is;
err = header_get_stream (header, &is);
err = fill_blurb (header);
if (err != 0)
return err;
else
{
char buf[1024];
char *tbuf;
size_t nread = 0;
do
{
err = stream_read (is, buf, sizeof (buf), header->temp_blurb_len,
&nread);
if (err != 0
|| (tbuf = realloc (header->temp_blurb,
header->temp_blurb_len + nread)) == NULL)
{
free (header->temp_blurb);
header->temp_blurb = NULL;
header->temp_blurb_len = 0;
return err;
}
else
header->temp_blurb = tbuf;
memcpy (header->temp_blurb + header->temp_blurb_len, buf, nread);
header->temp_blurb_len += nread;
} while (nread != 0);
/* parse it. */
header_parse (header, header->temp_blurb, header->temp_blurb_len);
free (header->temp_blurb);
header->temp_blurb = NULL;
header->temp_blurb_len = 0;
}
}
/* We set the threshold to be 1 less for the null. */
......@@ -362,6 +341,14 @@ header_lines (header_t header, size_t *plines)
if (header == NULL)
return EINVAL;
/* Try to fill out the buffer, if we know how. */
if (header->blurb == NULL)
{
int err = fill_blurb (header);
if (err != 0)
return err;
}
for (n = header->blurb_len - 1; n >= 0; n--)
{
if (header->blurb[n] == '\n')
......@@ -378,6 +365,14 @@ header_size (header_t header, size_t *pnum)
if (header == NULL)
return EINVAL;
/* Try to fill out the buffer, if we know how. */
if (header->blurb == NULL)
{
int err = fill_blurb (header);
if (err != 0)
return err;
}
if (pnum)
*pnum = header->blurb_len;
return 0;
......@@ -420,6 +415,54 @@ header_set_stream (header_t header, stream_t stream, void *owner)
return 0;
}
static int
fill_blurb (header_t header)
{
stream_t is;
int status;
char buf[1024];
char *tbuf;
size_t nread = 0;
status = header_get_stream (header, &is);
if (status != 0)
return status;
do
{
status = stream_read (is, buf, sizeof (buf), header->temp_blurb_len,
&nread);
if (status != 0)
{
if (status != EAGAIN || status != EINTR)
{
free (header->temp_blurb);
header->temp_blurb = NULL;
header->temp_blurb_len = 0;
}
return status;
}
tbuf = realloc (header->temp_blurb, header->temp_blurb_len + nread);
if (tbuf == NULL)
{
free (header->temp_blurb);
header->temp_blurb = NULL;
header->temp_blurb_len = 0;
return ENOMEM;
}
header->temp_blurb = tbuf;
memcpy (header->temp_blurb + header->temp_blurb_len, buf, nread);
header->temp_blurb_len += nread;
}
while (nread != 0);
/* parse it. */
status = header_parse (header, header->temp_blurb, header->temp_blurb_len);
free (header->temp_blurb);
header->temp_blurb = NULL;
header->temp_blurb_len = 0;
return status;
}
static int
header_write (stream_t os, const char *buf, size_t buflen,
......
......@@ -347,8 +347,8 @@ unix_open (mailbox_t mbox, int flags)
{
/* FIXME: for small mbox we shout try to mmap (). */
int trymap = (flags & MU_STREAM_CREAT) || (flags & MU_STREAM_APPEND);
if (trymap == 0)
status = (flags & MU_STREAM_CREAT) || (flags & MU_STREAM_APPEND);
if (status == 0)
status = mapfile_stream_create (&(mbox->stream));
if (status != 0)
{
......@@ -1154,11 +1154,6 @@ unix_append_message (mailbox_t mbox, message_t msg)
unix_lock (mbox, MU_LOCKER_WRLOCK);
{
off_t size;
char buffer[BUFSIZ];
size_t nread = 0;
off_t off = 0;
stream_t is;
header_t hdr;
int status;
size_t n = 0;
char nl = '\n';
......@@ -1170,8 +1165,7 @@ unix_append_message (mailbox_t mbox, message_t msg)
unix_unlock (mbox);
return status;
}
/* Header. */
message_get_header (msg, &hdr);
/* Generate a "From " separator. */
{
char from[128];
......@@ -1180,14 +1174,14 @@ unix_append_message (mailbox_t mbox, message_t msg)
size_t f = 0, d = 0;
*date = *from = '\0';
message_from (msg, from, sizeof (from), &f);
s = memchr (from, '\n', f);
s = memchr (from, nl, f);
if (s)
{
*s = '\0';
f--;
}
message_received (msg, date, sizeof (date), &d);
s = memchr (date, '\n', d);
s = memchr (date, nl, d);
if (s)
{
*s = '\0';
......@@ -1200,31 +1194,23 @@ unix_append_message (mailbox_t mbox, message_t msg)
stream_write (mbox->stream, &nl , 1, size, &n); size += n;
}
header_get_stream (hdr, &is);
do {
status = stream_read (is, buffer, sizeof (buffer), off, &nread);
if (status != 0)
return status;
if (nread == 0)
break;
stream_write (mbox->stream, buffer, nread, size, &n);
off += nread;
size += n;
} while (nread > 0);
*buffer = '\0';
/* Separator. */
/*fputc ('\n', mud->file);*/
/* Body. */
message_get_stream (msg, &is);
do {
stream_read (is, buffer, sizeof (buffer), off, &nread);
stream_write (mbox->stream, buffer, nread, size, &n);
off += nread;
size += n;
} while (nread > 0);
stream_write (mbox->stream, &nl, 1, size, &n);
/* Append the Message. */
{
char buffer[BUFSIZ];
size_t nread = 0;
off_t off = 0;
stream_t is;
message_get_stream (msg, &is);
do
{
stream_read (is, buffer, sizeof (buffer), off, &nread);
stream_write (mbox->stream, buffer, nread, size, &n);
off += nread;
size += n;
}
while (nread > 0);
stream_write (mbox->stream, &nl, 1, size, &n);
}
}
stream_flush (mbox->stream);
unix_unlock (mbox);
......
......@@ -533,8 +533,8 @@ message_read (stream_t is, char *buf, size_t buflen,
body_size (msg->body, &bsize);
/* On some remote sever (POP) the size of the header and body is not known
until you start reading them. So by checking hsize == bsize == 0, we
This kludge of a way of detecting the anomalie and start by the
until you start reading them. So by checking hsize == bsize == 0,
this kludge is a way of detecting the anomalie and start by the
header. */
if ((size_t)off <= hsize || (hsize == 0 && bsize == 0))
{
......