File: mboxlock.C

package info (click to toggle)
cone 0.75-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 31,040 kB
  • ctags: 13,930
  • sloc: ansic: 90,648; cpp: 79,781; sh: 18,355; perl: 3,218; makefile: 1,611; yacc: 289; sed: 16
file content (175 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* $Id: mboxlock.C,v 1.2 2008/05/24 17:57:42 mrsam Exp $
**
** Copyright 2002, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "mboxlock.H"
#include "mboxopen.H"
#include "file.H"
#include <cstring>

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

//
// Stub callback used for the OpenTask that updates the internal folder
// index.  When OpenTask completes this object goes back on the queue.
//


mail::mbox::LockTask::reopenCallback::reopenCallback(mail::mbox::LockTask *t)
	: task(t)
{
}

mail::mbox::LockTask::reopenCallback::~reopenCallback()
{
}

void mail::mbox::LockTask::reopenCallback::success(string msg)
{
	try {
		task->mboxAccount.installTask(task);
	} catch (...) {
		delete task;
		LIBMAIL_THROW();
	}
}

void mail::mbox::LockTask::reopenCallback::fail(string msg)
{
	task->fail(msg);
}

////////////////////////////////////////////////////////////////////////

mail::mbox::LockTask::LockTask(mail::mbox &mboxAccount,
		   mail::callback &callbackArg, string filenameArg)
	: TimedTask(mboxAccount, callbackArg), reopen(this),
	  filename(filenameArg)
{
}

mail::mbox::LockTask::~LockTask()
{
}

bool mail::mbox::LockTask::doit()
{
	for (;;)
	{
		string pathStr=filename;
		bool ro=false;

		if (pathStr.size() == 0)
		{
			pathStr=mboxAccount.currentFolder;
			ro=mboxAccount.currentFolderReadOnly;
		}

		if (pathStr.size() == 0)
		{
			callback.success("Folder locked");
			done();
			return true;
		}

		struct stat stat_buf;

		if (pathStr == "INBOX")
			pathStr=mboxAccount.inboxMailboxPath;

		if (mboxAccount.multiLockLock &&
		    mboxAccount.multiLockFilename == pathStr)
		{
			return locked( *mboxAccount.multiLockLock, pathStr);
		}

		mail::mbox::lock mailbox_lock( pathStr );

		if (!mailbox_lock(ro))
		{
			// TODO: fail if EEXIST

			if (errno != EAGAIN && errno != EEXIST)
			{
				fail(pathStr + ": " + strerror(errno));
				return true;
			}
			return false;
		}

		if (filename.size() > 0)
			// Locked another folder, no need
			// to check.
		{
			try {
				return locked(mailbox_lock, pathStr);
			} catch (...) {
				fail("Exception caught while accessing folder.");
			}
			return true;
		}

		if (fstat(mailbox_lock.getfd(), &stat_buf) < 0)
		{
			fail(pathStr + ": " + strerror(errno));
			return true;
		}

		if (stat_buf.st_size != mboxAccount.folderSavedSize ||
		    stat_buf.st_mtime != mboxAccount.folderSavedTimestamp)
			break;

		try {
			return locked(mailbox_lock, pathStr);
		} catch (...) {
			fail("Exception caught while accessing folder.");
		}
		return true;
	}

	// Uh-oh, something happened to the folder.

	OpenTask *t=new OpenTask(mboxAccount, mboxAccount.currentFolder, reopen, NULL);

	if (!t)
	{
		fail(mboxAccount.currentFolder + ": " + strerror(errno));
		return true;
	}

	try {
		mboxAccount.tasks.pop();
	} catch (...) {
		delete t;
		LIBMAIL_THROW();
	}

	try {
		mboxAccount.installTask(t);
	} catch (...) {
		fail("Exception caught while accessing folder.");
	}

	return true;
}

bool mail::mbox::LockTask::locked(mail::mbox::lock &mlock, string path)
{
	mail::file mboxFp(mlock.getfd(),
			  mlock.readOnly() ? "r":"r+");

	if (!mboxFp)
	{
		fail(strerror(errno));
		return true;
	}

	return (locked(mboxFp));
}