diff options
| author | Sam Varshavchik | 2013-08-19 16:39:41 -0400 | 
|---|---|---|
| committer | Sam Varshavchik | 2013-08-25 14:43:51 -0400 | 
| commit | 9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch) | |
| tree | 7a81a04cb51efb078ee350859a64be2ebc6b8813 /maildir/maildirlock.c | |
| parent | a9520698b770168d1f33d6301463bb70a19655ec (diff) | |
| download | courier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2 | |
Initial checkin
Imported from subversion report, converted to git. Updated all paths in
scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'maildir/maildirlock.c')
| -rw-r--r-- | maildir/maildirlock.c | 80 | 
1 files changed, 80 insertions, 0 deletions
| diff --git a/maildir/maildirlock.c b/maildir/maildirlock.c new file mode 100644 index 0000000..5531f29 --- /dev/null +++ b/maildir/maildirlock.c @@ -0,0 +1,80 @@ +/* +** Copyright 2003 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#include "config.h" +#include "maildirwatch.h" +#include "maildircreate.h" +#include "liblock/config.h" +#include "liblock/liblock.h" +#include "liblock/mail.h" +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> + + +/* +** Courier-IMAP compatible maildir lock. +*/ + +char *maildir_lock(const char *dir, struct maildirwatch *w, +		   int *tryAnyway) +{ +	struct maildir_tmpcreate_info createInfo; +	char *tmpname; +	char *newname; +	int rc; + +	*tryAnyway=0; + +	maildir_tmpcreate_init(&createInfo); + +	createInfo.maildir=dir; +	createInfo.uniq="courierlock"; +	createInfo.hostname=getenv("HOSTNAME"); +	createInfo.doordie=1; + +	if ((rc=maildir_tmpcreate_fd(&createInfo)) < 0) +		return NULL; +	close(rc); + +	tmpname=createInfo.tmpname; +	newname=createInfo.newname; + +	createInfo.tmpname=NULL; +	createInfo.newname=NULL; +	maildir_tmpcreate_free(&createInfo); + +	/* HACK: newname now contains: ".../new/filename" */ + +	strcpy(strrchr(newname, '/')-3, WATCHDOTLOCK); + +	while (ll_dotlock(newname, tmpname, 120) < 0) +	{ +		if (errno == EEXIST) +		{ +			if (w == NULL || maildirwatch_unlock(w, 120) < 0) +				sleep(1); +			continue; +		} + +		if (errno == EAGAIN) +		{ +			unlink(newname); +			sleep(5); +			continue; +		} + +		free(newname); +		newname=NULL; +		*tryAnyway=1; +		break; +	} + +	free(tmpname); + +	return newname; +} | 
