summaryrefslogtreecommitdiff
path: root/mailbox2
diff options
context:
space:
mode:
authorAlain Magloire <alainm@gnu.org>2001-09-23 03:59:39 +0000
committerAlain Magloire <alainm@gnu.org>2001-09-23 03:59:39 +0000
commit3dea5890547c7ec7f941660d011fb82fed1b174a (patch)
treea344eb457818bde86ea95291234ebaba7833fe32 /mailbox2
parent1e6107e6b09161b7758e111d8d45301a996f9488 (diff)
downloadmailutils-3dea5890547c7ec7f941660d011fb82fed1b174a.tar.gz
mailutils-3dea5890547c7ec7f941660d011fb82fed1b174a.tar.bz2
In order to provide inheritance, it is necessary to make
the base functions/methods visible so the extended object could reuse some base methods if necessary. Inheritance is possible because we always add to the end of the vtable structure new functions. For example, mime_t object will be also a message_t. and it will be possible to do { mime_t mime; header_t header; ... message_get_header ((message_t)mime, header); } Another example is tcpstream(socket stream) extends fdstream(file description stream); Also the name of the functions have change to match better there purpose. * mailbox2/Makefile.am: lockfile.c added. * mailbox2/bstream.c: Reoganisation to make the functions visible. * mailbox2/dattribute.c: Likewise. * mailbox2/dotlock.c: Likewise. * mailbox2/fdstream.c: Likewise. * mailbox2/fstream.c: Likewise. * mailbox2/mapstream.c: Likewise. * mailbox2/memstream.c: Likewise. * mailbox2/pticket.c: Likewise. * mailbox2/sdebug.c: Likewise. * mailbox2/tcpstream.c: Likewise. * mailbox2/include/mailutils/Makefile.am: lockfile.h added. * mailbox2/include/mailutils/attribute.h: Add the new prototypes. * mailbox2/include/mailutils/mbox.h: Add the new prototypes. * mailbox2/include/mailutils/stream.h: Add the new prototypes. * mailbox2/include/mailutils/sys/Makefile.am: dattribute.h lockfile.h mapstream.h pticket.h added. * mailbox2/include/mailutils/sys/bstream.h: Add new prototypes. * mailbox2/include/mailutils/sys/fdstream.h: Likewise. * mailbox2/include/mailutils/sys/fstream.h: Likewise. * mailbox2/include/mailutils/sys/mbox.h: Likewise. * mailbox2/include/mailutils/sys/memstream.h: Likewise. * mailbox2/include/mailutils/sys/sdebug.h: Likewise. * mailbox2/include/mailutils/sys/tcpstream.h: Likewise.
Diffstat (limited to 'mailbox2')
-rw-r--r--mailbox2/Makefile.am2
-rw-r--r--mailbox2/bstream.c222
-rw-r--r--mailbox2/dattribute.c136
-rw-r--r--mailbox2/dotlock.c157
-rw-r--r--mailbox2/fdstream.c205
-rw-r--r--mailbox2/fstream.c179
-rw-r--r--mailbox2/include/mailutils/Makefile.am2
-rw-r--r--mailbox2/include/mailutils/attribute.h4
-rw-r--r--mailbox2/include/mailutils/lockfile.h (renamed from mailbox2/include/mailutils/locker.h)24
-rw-r--r--mailbox2/include/mailutils/mbox.h12
-rw-r--r--mailbox2/include/mailutils/stream.h4
-rw-r--r--mailbox2/include/mailutils/sys/Makefile.am6
-rw-r--r--mailbox2/include/mailutils/sys/bstream.h32
-rw-r--r--mailbox2/include/mailutils/sys/dattribute.h54
-rw-r--r--mailbox2/include/mailutils/sys/dotlock.h64
-rw-r--r--mailbox2/include/mailutils/sys/fdstream.h33
-rw-r--r--mailbox2/include/mailutils/sys/fstream.h32
-rw-r--r--mailbox2/include/mailutils/sys/lockfile.h (renamed from mailbox2/include/mailutils/sys/locker.h)24
-rw-r--r--mailbox2/include/mailutils/sys/mapstream.h71
-rw-r--r--mailbox2/include/mailutils/sys/mbox.h63
-rw-r--r--mailbox2/include/mailutils/sys/memstream.h41
-rw-r--r--mailbox2/include/mailutils/sys/pticket.h (renamed from mailbox2/include/mailutils/sys/mstream.h)37
-rw-r--r--mailbox2/include/mailutils/sys/sdebug.h18
-rw-r--r--mailbox2/include/mailutils/sys/tcpstream.h23
-rw-r--r--mailbox2/lockfile.c (renamed from mailbox2/locker.c)46
-rw-r--r--mailbox2/mapstream.c213
-rw-r--r--mailbox2/memstream.c253
-rw-r--r--mailbox2/pticket.c60
-rw-r--r--mailbox2/sdebug.c46
-rw-r--r--mailbox2/tcpstream.c371
30 files changed, 1380 insertions, 1054 deletions
diff --git a/mailbox2/Makefile.am b/mailbox2/Makefile.am
index 01fa481a7..7284d88d3 100644
--- a/mailbox2/Makefile.am
+++ b/mailbox2/Makefile.am
@@ -25,7 +25,7 @@ libmailbox_la_SOURCES = \
header.c \
iterator.c \
list.c \
- locker.c \
+ lockfile.c \
mailbox.c \
md5-rsa.c \
memstream.c \
diff --git a/mailbox2/bstream.c b/mailbox2/bstream.c
index ded659a5d..23ac72bd4 100644
--- a/mailbox2/bstream.c
+++ b/mailbox2/bstream.c
@@ -37,14 +37,14 @@
#include <mailutils/sys/bstream.h>
static void
-_bs_cleanup (void *arg)
+_stream_buffer_cleanup (void *arg)
{
- struct _bs *bs = arg;
+ struct _stream_buffer *bs = arg;
mu_refcount_unlock (bs->refcount);
}
static int
-refill (struct _bs *bs)
+refill (struct _stream_buffer *bs)
{
int status;
if (bs->rbuffer.base == NULL)
@@ -60,17 +60,17 @@ refill (struct _bs *bs)
return status;
}
-static int
-_bs_ref (stream_t stream)
+int
+_stream_buffer_ref (stream_t stream)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return mu_refcount_inc (bs->refcount);
}
-static void
-_bs_destroy (stream_t *pstream)
+void
+_stream_buffer_destroy (stream_t *pstream)
{
- struct _bs *bs = (struct _bs *)*pstream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)*pstream;
if (mu_refcount_dec (bs->refcount) == 0)
{
stream_destroy (&bs->stream);
@@ -79,17 +79,17 @@ _bs_destroy (stream_t *pstream)
}
}
-static int
-_bs_open (stream_t stream, const char *name, int port, int flags)
+int
+_stream_buffer_open (stream_t stream, const char *name, int port, int flags)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_open (bs->stream, name, port, flags);
}
-static int
-_bs_close (stream_t stream)
+int
+_stream_buffer_close (stream_t stream)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
mu_refcount_lock (bs->refcount);
/* Clear the buffer of any residue left. */
if (bs->rbuffer.base && bs->rbuffer.bufsize)
@@ -106,11 +106,11 @@ _bs_close (stream_t stream)
use as a fully fledge buffer mechanism. It is a simple mechanims for
networking. Lots of code between POP and IMAP can be share this way.
The buffering is on the read only, the writes fall through. */
-static int
-_bs_read (stream_t stream, void *buf, size_t count, size_t *pnread)
+int
+_stream_buffer_read (stream_t stream, void *buf, size_t count, size_t *pnread)
{
int status = 0;
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
/* Noop. */
if (count == 0)
@@ -131,7 +131,7 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread)
int r;
mu_refcount_lock (bs->refcount);
- monitor_cleanup_push (_bs_cleanup, bs);
+ monitor_cleanup_push (_stream_buffer_cleanup, bs);
/* If the amount requested is bigger then the buffer cache size
bypass it. Do no waste time and let it through. */
@@ -199,11 +199,11 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread)
* Read at most n-1 characters.
* Stop when a newline has been read, or the count runs out.
*/
-static int
-_bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
+int
+_stream_buffer_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
{
int status = 0;
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
/* Noop. */
if (count == 0)
@@ -224,7 +224,7 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
size_t total = 0;
mu_refcount_lock (bs->refcount);
- monitor_cleanup_push (_bs_cleanup, bs);
+ monitor_cleanup_push (_stream_buffer_cleanup, bs);
count--; /* Leave space for the null. */
@@ -277,91 +277,74 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
return status;
}
-static int
-_bs_write (stream_t stream, const void *buf, size_t count, size_t *pnwrite)
+int
+_stream_buffer_write (stream_t stream, const void *buf, size_t count,
+ size_t *pnwrite)
{
- struct _bs *bs = (struct _bs *)stream;
- int err = 0;
- size_t nwriten = 0;
- size_t total = 0;
- int nleft = count;
- const char *p = buf;
-
- /* First try to send it all. */
- while (nleft > 0)
- {
- err = stream_write (bs->stream, p, nleft, &nwriten);
- if (err != 0 || nwriten == 0)
- break;
- nleft -= nwriten;
- total += nwriten;
- p += nwriten;
- }
- if (pnwrite)
- *pnwrite = total;
- return err;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
+ return stream_write (bs->stream, buf, count, pnwrite);
}
-static int
-_bs_get_fd (stream_t stream, int *pfd)
+int
+_stream_buffer_get_fd (stream_t stream, int *pfd)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_get_fd (bs->stream, pfd);
}
-static int
-_bs_get_flags (stream_t stream, int *pfl)
+int
+_stream_buffer_get_flags (stream_t stream, int *pfl)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_get_flags (bs->stream, pfl);
}
-static int
-_bs_get_size (stream_t stream, off_t *psize)
+int
+_stream_buffer_get_size (stream_t stream, off_t *psize)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_get_size (bs->stream, psize);
}
-static int
-_bs_truncate (stream_t stream, off_t len)
+int
+_stream_buffer_truncate (stream_t stream, off_t len)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_truncate (bs->stream, len);
}
-static int
-_bs_flush (stream_t stream)
+int
+_stream_buffer_flush (stream_t stream)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_flush (bs->stream);
}
-static int
-_bs_get_state (stream_t stream, enum stream_state *pstate)
+int
+_stream_buffer_get_state (stream_t stream, enum stream_state *pstate)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_get_state (bs->stream, pstate);
}
-static int
-_bs_seek (stream_t stream, off_t off, enum stream_whence whence)
+int
+_stream_buffer_seek (stream_t stream, off_t off, enum stream_whence whence)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_seek (bs->stream, off, whence);
}
-static int
-_bs_tell (stream_t stream, off_t *off)
+int
+_stream_buffer_tell (stream_t stream, off_t *off)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_tell (bs->stream, off);
}
-static int
-_bs_is_readready (stream_t stream, int timeout)
+int
+_stream_buffer_is_readready (stream_t stream, int timeout)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
/* Drain our buffer first. */
mu_refcount_lock (bs->refcount);
if (bs->rbuffer.count > 0)
@@ -373,62 +356,84 @@ _bs_is_readready (stream_t stream, int timeout)
return stream_is_readready (bs->stream, timeout);
}
-static int
-_bs_is_writeready (stream_t stream, int timeout)
+int
+_stream_buffer_is_writeready (stream_t stream, int timeout)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_is_writeready (bs->stream, timeout);
}
-static int
-_bs_is_exceptionpending (stream_t stream, int timeout)
+int
+_stream_buffer_is_exceptionpending (stream_t stream, int timeout)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_is_exceptionpending (bs->stream, timeout);
}
-static int
-_bs_is_open (stream_t stream)
+int
+_stream_buffer_is_open (stream_t stream)
{
- struct _bs *bs = (struct _bs *)stream;
+ struct _stream_buffer *bs = (struct _stream_buffer *)stream;
return stream_is_open (bs->stream);
}
-static struct _stream_vtable _bs_vtable =
+static struct _stream_vtable _stream_buffer_vtable =
{
- _bs_ref,
- _bs_destroy,
+ _stream_buffer_ref,
+ _stream_buffer_destroy,
- _bs_open,
- _bs_close,
+ _stream_buffer_open,
+ _stream_buffer_close,
- _bs_read,
- _bs_readline,
- _bs_write,
+ _stream_buffer_read,
+ _stream_buffer_readline,
+ _stream_buffer_write,
- _bs_seek,
- _bs_tell,
+ _stream_buffer_seek,
+ _stream_buffer_tell,
- _bs_get_size,
- _bs_truncate,
- _bs_flush,
+ _stream_buffer_get_size,
+ _stream_buffer_truncate,
+ _stream_buffer_flush,
- _bs_get_fd,
- _bs_get_flags,
- _bs_get_state,
+ _stream_buffer_get_fd,
+ _stream_buffer_get_flags,
+ _stream_buffer_get_state,
- _bs_is_readready,
- _bs_is_writeready,
- _bs_is_exceptionpending,
+ _stream_buffer_is_readready,
+ _stream_buffer_is_writeready,
+ _stream_buffer_is_exceptionpending,
- _bs_is_open
+ _stream_buffer_is_open
};
int
+_stream_buffer_ctor (struct _stream_buffer *bs, stream_t stream,
+ size_t bufsize)
+{
+ mu_refcount_create (&(bs->refcount));
+ if (bs->refcount == NULL)
+ return MU_ERROR_NO_MEMORY;
+
+ bs->stream = stream;
+ bs->rbuffer.bufsize = bufsize;
+ bs->base.vtable = &_stream_buffer_vtable;
+ return 0;
+}
+
+void
+_stream_buffer_dtor (struct _stream_buffer *bs)
+{
+ stream_destroy (&bs->stream);
+ mu_refcount_destroy (&bs->refcount);
+}
+
+int
stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize)
{
- struct _bs *bs;
+ struct _stream_buffer *bs;
+ int status;
if (pstream == NULL || stream == NULL || bufsize == 0)
return MU_ERROR_INVALID_PARAMETER;
@@ -437,16 +442,9 @@ stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize)
if (bs == NULL)
return MU_ERROR_NO_MEMORY;
- mu_refcount_create (&(bs->refcount));
- if (bs->refcount == NULL)
- {
- free (bs);
- return MU_ERROR_NO_MEMORY;
- }
-
- bs->stream = stream;
- bs->rbuffer.bufsize = bufsize;
- bs->base.vtable = &_bs_vtable;
+ status = _stream_buffer_ctor (bs, stream, bufsize);
+ if (status != 0)
+ return status;
*pstream = &bs->base;
return 0;
}
diff --git a/mailbox2/dattribute.c b/mailbox2/dattribute.c
index 9e6796a92..b8b98cd9c 100644
--- a/mailbox2/dattribute.c
+++ b/mailbox2/dattribute.c
@@ -28,19 +28,19 @@
#endif
#include <mailutils/error.h>
-#include <mailutils/sys/attribute.h>
+#include <mailutils/sys/dattribute.h>
-static int
-_da_ref (attribute_t attribute)
+int
+_attribute_default_ref (attribute_t attribute)
{
- struct _da *da = (struct _da *)attribute;
+ struct _attribute_default *da = (struct _attribute_default *)attribute;
return mu_refcount_inc (da->refcount);
}
-static void
-_da_destroy (attribute_t *pattribute)
+void
+_attribute_default_destroy (attribute_t *pattribute)
{
- struct _da *da = (struct _da *)*pattribute;
+ struct _attribute_default *da = (struct _attribute_default *)*pattribute;
if (mu_refcount_dec (da->refcount) == 0)
{
mu_refcount_destroy (&da->refcount);
@@ -48,10 +48,10 @@ _da_destroy (attribute_t *pattribute)
}
}
-static int
-_da_get_flags (attribute_t attribute, int *pflags)
+int
+_attribute_default_get_flags (attribute_t attribute, int *pflags)
{
- struct _da *da = (struct _da *)attribute;
+ struct _attribute_default *da = (struct _attribute_default *)attribute;
mu_refcount_lock (da->refcount);
if (pflags)
*pflags = da->flags;
@@ -59,20 +59,20 @@ _da_get_flags (attribute_t attribute, int *pflags)
return 0;
}
-static int
-_da_set_flags (attribute_t attribute, int flags)
+int
+_attribute_default_set_flags (attribute_t attribute, int flags)
{
- struct _da *da = (struct _da *)attribute;
+ struct _attribute_default *da = (struct _attribute_default *)attribute;
mu_refcount_lock (da->refcount);
da->flags |= (flags | MU_ATTRIBUTE_MODIFIED);
mu_refcount_unlock (da->refcount);
return 0;
}
-static int
-_da_unset_flags (attribute_t attribute, int flags)
+int
+_attribute_default_unset_flags (attribute_t attribute, int flags)
{
- struct _da *da = (struct _da *)attribute;
+ struct _attribute_default *da = (struct _attribute_default *)attribute;
mu_refcount_lock (da->refcount);
da->flags &= ~flags;
/* If Modified was being unset do not reset it. */
@@ -82,45 +82,115 @@ _da_unset_flags (attribute_t attribute, int flags)
return 0;
}
-static int
-_da_clear_flags (attribute_t attribute)
+int
+_attribute_default_clear_flags (attribute_t attribute)
{
- struct _da *da = (struct _da *)attribute;
+ struct _attribute_default *da = (struct _attribute_default *)attribute;
mu_refcount_lock (da->refcount);
da->flags = 0;
mu_refcount_unlock (da->refcount);
return 0;
}
-static struct _attribute_vtable _da_vtable =
+static struct _attribute_vtable _attribute_default_vtable =
{
- _da_ref,
- _da_destroy,
+ _attribute_default_ref,
+ _attribute_default_destroy,
- _da_get_flags,
- _da_set_flags,
- _da_unset_flags,
- _da_clear_flags
+ _attribute_default_get_flags,
+ _attribute_default_set_flags,
+ _attribute_default_unset_flags,
+ _attribute_default_clear_flags
};
int
-attribute_create (attribute_t *pattribute)
+_attribute_default_ctor (struct _attribute_default *da)
{
- struct _da *da;
+ mu_refcount_create (&(da->refcount));
+ if (da->refcount == NULL)
+ return MU_ERROR_NO_MEMORY;
+ da->flags = 0;
+ da->base.vtable = &_attribute_default_vtable;
+ return 0;
+}
+
+void
+_attribute_default_dtor (struct _attribute_default *da)
+{
+ mu_refcount_destroy (&da->refcount);
+}
+
+int
+attribute_default_create (attribute_t *pattribute)
+{
+ struct _attribute_default *da;
+ int status;
+
if (pattribute == NULL)
return MU_ERROR_INVALID_PARAMETER;
+
da = calloc (1, sizeof *da);
if (da == NULL)
return MU_ERROR_NO_MEMORY;
- mu_refcount_create (&(da->refcount));
- if (da->refcount == NULL)
+ status = _attribute_default_ctor (da);
+ if (status != 0)
{
free (da);
- return MU_ERROR_NO_MEMORY;
+ return status;
}
- da->flags = 0;
- da->base.vtable = &_da_vtable;
*pattribute = &da->base;
return 0;
}
+
+int
+attribute_status_create (attribute_t *pattribute, const char *field)
+{
+ struct _attribute_default *da;
+ int status;
+ char *colon;
+
+ if (pattribute == NULL || field == NULL)
+ return MU_ERROR_INVALID_PARAMETER;
+
+ da = calloc (1, sizeof *da);
+ if (da == NULL)
+ return MU_ERROR_NO_MEMORY;
+
+ status = _attribute_default_ctor (da);
+ if (status != 0)
+ {
+ free (da);
+ return status;
+ }
+ *pattribute = &da->base;
+
+ colon = strchr (field, ':');
+ if (colon)
+ field = ++colon;
+
+ for (; *field; field++)
+ {
+ switch (*field)
+ {
+ case 'r':
+ case 'R':
+ attribute_set_read (*pattribute);
+ break;
+ case 'O':
+ case 'o':
+ attribute_set_seen (*pattribute);
+ break;
+ case 'a':
+ case 'A':
+ attribute_set_answered (*pattribute);
+ break;
+ case 'd':
+ case 'D':
+ attribute_set_deleted (*pattribute);
+ break;
+ }
+ }
+ attribute_unset_modified (*pattribute);
+ return 0;
+}
diff --git a/mailbox2/dotlock.c b/mailbox2/dotlock.c
index ae8fbe43f..1c261d343 100644
--- a/mailbox2/dotlock.c
+++ b/mailbox2/dotlock.c
@@ -33,53 +33,35 @@
#include <signal.h>
#include <mailutils/error.h>
-#include <mailutils/sys/locker.h>
+#include <mailutils/sys/dotlock.h>
#include <mailutils/refcount.h>
-/* locking flags */
-#define MU_DOTLOCK_PID 1
-#define MU_DOTLOCK_FCNTL 2
-#define MU_DOTLOCK_TIME 4
-
-#define MU_DOTLOCK_EXPIRE_TIME (5 * 60)
-
-#define LOCKFILE_ATTR 0444
-
/* First draft by Brian Edmond. */
-struct _dotlock
-{
- struct _locker base;
- mu_refcount_t refcount;
- int fd;
- int refcnt;
- char *fname;
- int flags;
-};
-
-static int
-_dotlock_ref (locker_t locker)
+int
+_lockfile_dotlock_ref (lockfile_t lockfile)
{
- struct _dotlock *dotlock = (struct _dotlock *)locker;
+ struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
return mu_refcount_inc (dotlock->refcount);
}
-static void
-_dotlock_destroy (locker_t *plocker)
+void
+_lockfile_dotlock_destroy (lockfile_t *plockfile)
{
- struct _dotlock *dotlock = (struct _dotlock *)*plocker;
+ struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)*plockfile;
if (mu_refcount_dec (dotlock->refcount) == 0)
{
mu_refcount_destroy (&dotlock->refcount);
- free (dotlock->fname);
+ if (dotlock->fname)
+ free (dotlock->fname);
free (dotlock);
}
}
-static int
-_dotlock_lock (locker_t locker)
+int
+_lockfile_dotlock_lock (lockfile_t lockfile)
{
- struct _dotlock *dotlock = (struct _dotlock *)locker;
+ struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
int fd = -1;
char buf[16];
pid_t pid;
@@ -104,31 +86,34 @@ _dotlock_lock (locker_t locker)
if (fd != -1)
{
/* Check to see if this process is still running. */
- if (dotlock->flags & MU_DOTLOCK_PID)
+ if (dotlock->flags & MU_LOCKFILE_DOTLOCK_PID)
{
int nread = read (fd, buf, sizeof (buf) - 1);
if (nread > 0)
{
buf[nread] = '\0';
- pid = strtol (buf, NULL, 10);
- if (pid > 0)
- {
- /* Process is gone so we try to remove the lock. */
- if (kill (pid, 0) == -1)
- removed = 1;
- }
- else
- removed = 1; /* Corrupted file, remove the lock. */
- }
+ switch (pid = strtol (buf, NULL, 10))
+ {
+ case LONG_MIN:
+ case LONG_MAX:
+ if (errno == ERANGE)
+ removed = 1;
+ break;
+ default:
+ /* Process is gone so we try to remove the lock. */
+ if (kill (pid, 0) == -1)
+ removed = 1;
+ }
+ }
}
/* Check to see if the lock expired. */
- if (dotlock->flags & MU_DOTLOCK_TIME)
+ if (dotlock->flags & MU_LOCKFILE_DOTLOCK_TIME)
{
struct stat stbuf;
fstat (fd, &stbuf);
/* The lock has expired. */
- if ((time (NULL) - stbuf.st_mtime) > MU_DOTLOCK_EXPIRE_TIME)
+ if ((time (NULL) - stbuf.st_mtime) > MU_LOCKFILE_DOTLOCK_EXPIRE_TIME)
removed = 1;
}
@@ -138,7 +123,7 @@ _dotlock_lock (locker_t locker)
}
/* Try to create the lockfile. */
- fd = open (dotlock->fname, O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR);
+ fd = open (dotlock->fname, O_WRONLY | O_CREAT | O_EXCL, MU_LOCKFILE_DOTLOCK_ATTR);
if (fd == -1)
return errno;
else
@@ -165,7 +150,7 @@ _dotlock_lock (locker_t locker)
write (fd, buf, strlen (buf));
/* Try to get a file lock. */
- if (dotlock->flags & MU_DOTLOCK_FCNTL)
+ if (dotlock->flags & MU_LOCKFILE_DOTLOCK_FCNTL)
{
struct flock fl;
@@ -186,26 +171,26 @@ _dotlock_lock (locker_t locker)
return 0;
}
-static int
-_dotlock_touchlock (locker_t locker)
+int
+_lockfile_dotlock_touchlock (lockfile_t lockfile)
{
- struct _dotlock *dotlock = (struct _dotlock *)locker;
+ struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
if (!dotlock || !dotlock->fname || dotlock->fd == -1)
return MU_ERROR_INVALID_PARAMETER;
return utime (dotlock->fname, NULL);
}
-static int
-_dotlock_unlock (locker_t locker)
+int
+_lockfile_dotlock_unlock (lockfile_t lockfile)
{
- struct _dotlock *dotlock = (struct _dotlock *)locker;
+ struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
if (!dotlock || !dotlock->fname || dotlock->fd == -1 || dotlock->refcnt <= 0)
return EINVAL;
if (--dotlock->refcnt > 0)
return 0;
- if (dotlock->flags & MU_DOTLOCK_FCNTL)
+ if (dotlock->flags & MU_LOCKFILE_DOTLOCK_FCNTL)
{
struct flock fl;
@@ -221,48 +206,68 @@ _dotlock_unlock (locker_t locker)
return 0;
}
-static struct _locker_vtable _dotlock_vtable =
+static struct _lockfile_vtable _lockfile_dotlock_vtable =
{
- _dotlock_ref,
- _dotlock_destroy,
+ _lockfile_dotlock_ref,
+ _lockfile_dotlock_destroy,
- _dotlock_lock,
- _dotlock_touchlock,
- _dotlock_unlock,
+ _lockfile_dotlock_lock,
+ _lockfile_dotlock_touchlock,
+ _lockfile_dotlock_unlock,
};
int
-locker_dotlock_create (locker_t *plocker, const char *filename)
+_lockfile_dotlock_ctor (struct _lockfile_dotlock *dotlock,
+ const char *filename)
{
- struct _dotlock *dotlock;
-
- if (plocker == NULL || filename == NULL)
- return MU_ERROR_INVALID_PARAMETER;
-
- dotlock = calloc (1, sizeof *dotlock);
- if (dotlock == NULL)
- return MU_ERROR_NO_MEMORY;
-
mu_refcount_create (&dotlock->refcount);
if (dotlock->refcount)
- {
- free (dotlock);
- return MU_ERROR_NO_MEMORY;
- }
+ return MU_ERROR_NO_MEMORY;
dotlock->fname = calloc (strlen (filename) + 5 /*strlen(".lock")*/ + 1, 1);
if (dotlock->fname == NULL)
{
- free (dotlock);
+ mu_refcount_destroy (&dotlock->refcount);
return MU_ERROR_NO_MEMORY;
}
strcpy (dotlock->fname, filename);
strcat (dotlock->fname, ".lock");
- dotlock->flags = MU_DOTLOCK_PID | MU_DOTLOCK_TIME | MU_DOTLOCK_FCNTL;
+ dotlock->flags = MU_LOCKFILE_DOTLOCK_PID | MU_LOCKFILE_DOTLOCK_TIME
+ | MU_LOCKFILE_DOTLOCK_FCNTL;
dotlock->fd = -1;
dotlock->refcnt = 0;
- dotlock->base.vtable = &_dotlock_vtable;
- *plocker = &dotlock->base;
+ dotlock->base.vtable = &_lockfile_dotlock_vtable;
+ return 0;
+}
+
+void
+_lockfile_dotlock_dtor (struct _lockfile_dotlock *dotlock)
+{
+ mu_refcount_destroy (&dotlock->refcount);
+ if (dotlock->fname)
+ free (dotlock->fname);
+}
+
+int
+lockfile_dotlock_create (lockfile_t *plockfile, const char *filename)
+{
+ struct _lockfile_dotlock *dotlock;
+ int status;
+
+ if (plockfile == NULL || filename == NULL)
+ return MU_ERROR_INVALID_PARAMETER;
+
+ dotlock = calloc (1, sizeof *dotlock);
+ if (dotlock == NULL)
+ return MU_ERROR_NO_MEMORY;
+
+ status = _lockfile_dotlock_ctor (dotlock, filename);
+ if (status != 0)
+ {
+ free (dotlock);
+ return status;
+ }
+ *plockfile = &dotlock->base;
return 0;
}
diff --git a/mailbox2/fdstream.c b/mailbox2/fdstream.c
index f0100dfb1..c82c83a5f 100644
--- a/mailbox2/fdstream.c
+++ b/mailbox2/fdstream.c
@@ -33,23 +33,23 @@
#include <mailutils/error.h>
static void
-_fds_cleanup (void *arg)
+_stream_fd_cleanup (void *arg)
{
- struct _fds *fds = arg;
+ struct _stream_fd *fds = arg;
mu_refcount_unlock (fds->refcount);
}
-static int
-_fds_ref (stream_t stream)
+int
+_stream_fd_ref (stream_t stream)
{
- struct _fds *fds = (struct _fds *)stream;
+ struct _stream_fd *fds = (struct _stream_fd *)stream;
return mu_refcount_inc (fds->refcount);
}
-static void
-_fds_destroy (stream_t *pstream)
+void