Commit 8be6084f 8be6084f1304aa64062065454dd2d138ebc6a9ca by Alain Magloire

Pff !!! And everything with one hand. Now I understand why people

use threads and things like aio_*() calls.  Pop in Nonblocking
mode should work properly, but the price to pay is big hairy code
Because we have to keep the state of the connection when return
EAGAIN.  There is lot of buffer copy all over the place .i.e when
we come back from a EAGAIN, we have to reset the same state.

We have two new files call bio.[ch] it stands for buffered I/O
Saves me from a lot of read()/write() calls by bufferring a bit
for me.

Attribute added 3 new functions, attribute_{set,get,unset,is}userflag()
to let someone else the library store certain attributes on a message.
They are keep in a different place the  the normal attributes.
1 parent 1152383e
......@@ -53,6 +53,8 @@ attribute_destroy (attribute_t *pattr, void *owner)
int
attribute_get_flags (attribute_t attr, int *pflags)
{
if (attr == NULL)
return EINVAL;
if (attr->_get_flags)
return attr->_get_flags (attr, pflags);
if (pflags)
......@@ -63,6 +65,8 @@ attribute_get_flags (attribute_t attr, int *pflags)
int
attribute_set_flags (attribute_t attr, int flags)
{
if (attr == NULL)
return EINVAL;
if (attr->_set_flags)
return attr->_set_flags (attr, flags);
attr->flags |= flags;
......@@ -105,6 +109,17 @@ attribute_set_unset_flags (attribute_t attr, int (*_unset_flags)
return 0;
}
/* We add support for "USER" flag, it is a way for external objects
Not being the owner to add custom flags. */
int
attribute_set_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return EINVAL;
attr->user_flags |= flag;
return 0;
}
int
attribute_set_seen (attribute_t attr)
{
......@@ -197,6 +212,14 @@ attribute_set_recent (attribute_t attr)
}
int
attribute_is_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return 0;
return attr->user_flags & flag;
}
int
attribute_is_seen (attribute_t attr)
{
int status = 0;
......@@ -277,6 +300,15 @@ attribute_is_recent (attribute_t attr)
}
int
attribute_unset_userflag (attribute_t attr, int flag)
{
if (attr == NULL)
return 0;
attr->user_flags &= ~flag;
return 0;
}
int
attribute_unset_seen (attribute_t attr)
{
int status = 0;
......@@ -371,6 +403,7 @@ attribute_is_equal (attribute_t attr, attribute_t attr2)
return attr->flags == attr2->flags;
}
/* Miscellaneous. */
int
attribute_copy (attribute_t dest, attribute_t src)
{
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "bio.h"
/* This is so annoying, we can not use the standard I/O(stdio) because a
socket may write/read less then requested and with the stdio functions
there is no way to know how much data was written/read, meaning also things
can get tricky in NONBLOCKING. */
struct _bio
{
char *read_buffer; /* Read Buffer. */
char *read_ptr;
size_t read_buflen;
char *write_buffer; /* Write Buffer. */
char *write_ptr;
size_t write_buflen;
stream_t stream;
};
int
bio_create (bio_t *pbio, stream_t stream)
{
bio_t bio;
/* 512 is the minimum for POP answer buffer. */
#define MIOBUF 512
if (pbio == NULL)
return EINVAL;
bio = calloc (1, sizeof (*bio));
if (bio == NULL)
return ENOMEM;
/* + 1 for the sentinel. */
bio->read_buffer = calloc (MIOBUF + 1, sizeof (char));
if (bio->read_buffer == NULL)
{
bio_destroy (&bio);
return ENOMEM;
}
bio->read_ptr = bio->read_buffer;
bio->read_buflen = MIOBUF;
/* + 1 for the sentinel. */
bio->write_buffer = calloc (MIOBUF/2 + 1, sizeof (char));
if (bio->write_buffer == NULL)
{
bio_destroy (&bio);
return ENOMEM;
}
bio->write_ptr = bio->write_buffer;
bio->write_buflen = MIOBUF;
bio->stream = stream;
*pbio = bio;
return 0;
}
void
bio_destroy (bio_t *pbio)
{
if (pbio && *pbio)
{
free ((*pbio)->read_buffer);
free ((*pbio)->write_buffer);
/* We don't close the file descriptor ? */
/* stream_close (stream); */
free ((*pbio));
*pbio = NULL;
}
}
/* buffered read */
int
bio_read (bio_t bio, char *ptr, size_t n, size_t *pnread)
{
int nleft;
size_t nread = 0;
size_t len = 0;
/* sanity check */
if (bio == NULL || pnread == NULL)
return EINVAL;
/* noop */
if (n == 0 || ptr == NULL)
{
*pnread = 0;
return 0;
}
/* fill up the global buffer */
if (bio->read_ptr == bio->read_buffer)
{
int err = stream_read (bio->stream, bio->read_buffer,
bio->read_buflen, 0, &len);
if (err != 0)
return err;
/* If len == read_buflen, read_ptr points to the sentinel. */
bio->read_ptr = bio->read_buffer + len;
}
else
len = bio->read_ptr - bio->read_buffer;
/* How much can wd copy out ? */
nleft = n - len;
/* We got more then requested. */
if (nleft < 0)
{
size_t sentinel;
nread = n;
/* where to move the sentinel to mark the end. */
sentinel = bio->read_ptr - (bio->read_buffer + nread);
memcpy (ptr, bio->read_buffer, nread);
memmove (bio->read_buffer, bio->read_buffer + nread, sentinel);
bio->read_ptr = bio->read_buffer + sentinel ;
}
else
{
/* Drain our buffer. */;
nread = len;
memcpy (ptr, bio->read_buffer, nread);
bio->read_ptr = bio->read_buffer;
}
if (pnread)
*pnread = nread;
return 0;
}
int
bio_write (bio_t bio, const char *ptr, size_t n, size_t *pnwriten)
{
int nleft;
int err;
size_t nwriten = 0;
size_t total = 0;
nleft = n;
/* First try to send it all. */
while (nleft > 0)
{
err = stream_write (bio->stream, ptr, nleft, 0, &nwriten);
if (err != 0)
break;
nleft -= nwriten;
total += nwriten;
ptr += nwriten;
}
/* Not recoverable. */
if (err != EAGAIN && err != EINTR)
bio->write_ptr = bio->write_buffer;
if (pnwriten)
*pnwriten = total;
return err;
}
/* Return the number of char read excluding the null char */
int
bio_readline (bio_t bio, char *ptr, size_t maxlen, size_t *pwriten)
{
int rc = 0;
size_t n = 0;
int err;
char c;
/* Noop. */
if (ptr == NULL || maxlen == 0)
{
if (pwriten)
*pwriten = 0;
return 0;
}
maxlen--;
while ((err = bio_read (bio, &c, 1, &rc)) == 0 && rc == 1)
{
*ptr++ = c;
n++;
if (c == '\n')
break; /* newline is stored, like fgets () */
if (n == maxlen)
break;
}
/* This is tricky, We may have data in the buffer, so we flush it
first and return success. On the next read if the error persist
it will be returned. This can happen for NONBLOCKING. */
if (n > 0)
err = 0;
*ptr = '\0';
if (pwriten)
*pwriten = n;
//fprintf (stderr, "\n%d %d BIO %s\n", n, *(ptr -n), (ptr -n ));
return err;
}
......@@ -461,7 +461,7 @@ fill_blurb (header_t header)
&nread);
if (status != 0)
{
if (status != EAGAIN || status != EINTR)
if (status != EAGAIN && status != EINTR)
{
free (header->temp_blurb);
header->temp_blurb = NULL;
......@@ -481,7 +481,7 @@ fill_blurb (header_t header)
memcpy (header->temp_blurb + header->temp_blurb_len, buf, nread);
header->temp_blurb_len += nread;
}
while (nread != 0);
while (nread > 0);
/* parse it. */
status = header_parse (header, header->temp_blurb, header->temp_blurb_len);
......
......@@ -37,6 +37,8 @@ struct _attribute
void *owner;
int flags;
int user_flags;
int (*_get_flags) __P ((attribute_t, int *));
int (*_set_flags) __P ((attribute_t, int));
int (*_unset_flags) __P ((attribute_t, int));
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _BIO_H
# define _BIO_H
#include <sys/types.h>
#include <mailutils/stream.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
typedef struct _bio * bio_t;
/* bufferred I/O */
extern ssize_t bio_create __P ((bio_t *pbio, stream_t));
extern void bio_destroy __P ((bio_t *pbio));
extern ssize_t bio_read __P ((bio_t bio, char *vptr,
size_t maxlen, size_t *pnread));
extern ssize_t bio_readline __P ((bio_t bio, char *vptr,
size_t maxlen, size_t *pnread));
extern ssize_t bio_write __P ((bio_t bio, const char *vptr,
size_t maxlen, size_t *pnwrite));
#ifdef __cplusplus
}
#endif
#endif /* _BIO_H */
......@@ -418,7 +418,7 @@ message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwriten)
if (msg == NULL || buffer == NULL || buflen == 0)
return EINVAL;
buffer[0] = '0';
buffer[0] = '\0';
if (msg->_get_uidl)
return msg->_get_uidl (msg, buffer, buflen, pwriten);
......@@ -430,18 +430,21 @@ message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwriten)
|| (status = header_get_value (header, "Message-ID", buffer,
buflen, &n)) == 0)
{
/* We need to collapse the header if it was mutiline. */
/* FIXME: Is header_get_value suppose to do this ? */
/* We need to collapse the header if it was mutiline. e points to the
last char meaning in a C string that's '\0', s to the start. We also
remove the spesky '<' '>' if they are around. */
char *s, *e;
for (s = buffer, e = buffer + n; s <= e; s++)
{
if (isspace (*s))
memmove (s, s + 1, e - s);
if (isspace (*s) || *s == '<' || *s == '>')
{
memmove (s, s + 1, e - (s + 1));
e -= 1;
*e = '\0';
}
}
return 0;
}
if (pwriten)
*pwriten = n;
return status;
}
......