summaryrefslogtreecommitdiffstats
path: root/maildir/maildirlock.c
blob: 5531f2994dcf2e03526349d023d8fc6363ff560f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}