Only create a missing uid if we have indexing turned on.

2001-06-18  Not Zed  <NotZed@Ximian.com>

        * camel-folder-summary.c
        (camel_folder_summary_info_new_from_parser): Only create a missing
        uid if we have indexing turned on.

        * camel-lock-helper.c (setup_process): Function to setup
        process/sanity/security checks.  Change to the real uid as soon as
        we can.
        (lock_path): First try to lock as the real uid, if that fails, try
        the root uid.
        (unlock_id): Unlock as the uid we created the lock as.

        * Makefile.am (INCLUDES): Added -DCAMEL_SBINDIR for lock helper
        location.

        * providers/local/camel-spool-folder.c (spool_lock): Implemented,
        using lock helper locking.  Need to work out if the locking
        requires a root created lock?
        (spool_unlock): Likewise.

2001-06-15  Not Zed  <NotZed@Ximian.com>

        * camel-lock-helper.c: Setuid Lock helper process.  Creates and manages
        .locks, keeping them active, removing them, etc.  What real perms it
        needs is a little system dependent.

2001-06-14  Not Zed  <NotZed@Ximian.com>

        * providers/local/camel-maildir-store.c (get_folder_info): Implement.
        (scan_dir): Does the work of scanning for maildir directories.

2001-06-13  Not Zed  <NotZed@Ximian.com>

        * providers/local/camel-spool-store.c (get_folder_info):
        Implemented, just returns a hardcoded INBOX folder.
        (free_folder_info): implemented, free's the 1 possible level of
        folder info.

        * providers/local/camel-spool-folder.c
        (camel_spool_folder_construct): Set the real unread message
        count on the folder_created thing.

svn path=/trunk/; revision=10261
This commit is contained in:
Not Zed
2001-06-18 14:36:44 +00:00
committed by Michael Zucci
parent 5255cb6d49
commit e905364bee
15 changed files with 1145 additions and 53 deletions

View File

@ -1,3 +1,46 @@
2001-06-18 Not Zed <NotZed@Ximian.com>
* camel-folder-summary.c
(camel_folder_summary_info_new_from_parser): Only create a missing
uid if we have indexing turned on.
* camel-lock-helper.c (setup_process): Function to setup
process/sanity/security checks. Change to the real uid as soon as
we can.
(lock_path): First try to lock as the real uid, if that fails, try
the root uid.
(unlock_id): Unlock as the uid we created the lock as.
* Makefile.am (INCLUDES): Added -DCAMEL_SBINDIR for lock helper
location.
* providers/local/camel-spool-folder.c (spool_lock): Implemented,
using lock helper locking. Need to work out if the locking
requires a root created lock?
(spool_unlock): Likewise.
2001-06-15 Not Zed <NotZed@Ximian.com>
* camel-lock-helper.c: Setuid Lock helper process. Creates and manages
.locks, keeping them active, removing them, etc. What real perms it
needs is a little system dependent.
2001-06-14 Not Zed <NotZed@Ximian.com>
* providers/local/camel-maildir-store.c (get_folder_info): Implement.
(scan_dir): Does the work of scanning for maildir directories.
2001-06-13 Not Zed <NotZed@Ximian.com>
* providers/local/camel-spool-store.c (get_folder_info):
Implemented, just returns a hardcoded INBOX folder.
(free_folder_info): implemented, free's the 1 possible level of
folder info.
* providers/local/camel-spool-folder.c
(camel_spool_folder_construct): Set the real unread message
count on the folder_created thing.
2001-06-15 Dan Winship <danw@ximian.com>
* providers/imap/camel-imap-message-cache.c

View File

@ -5,6 +5,8 @@ SUBDIRS = providers tests
libcamelincludedir = $(includedir)/camel
providerdir = $(libdir)/evolution/camel-providers/$(VERSION)
sbin_PROGRAMS = camel-lock-helper
lib_LTLIBRARIES = libcamel.la
INCLUDES = -I.. -I$(srcdir)/.. \
@ -15,6 +17,7 @@ INCLUDES = -I.. -I$(srcdir)/.. \
$(NSPR_CFLAGS) \
$(NSS_CFLAGS) \
$(OPENSSL_CFLAGS) \
-DCAMEL_SBINDIR=\""$(sbindir)"\" \
-DCAMEL_PROVIDERDIR=\""$(providerdir)"\" \
-DG_LOG_DOMAIN=\"camel\"
@ -38,6 +41,7 @@ libcamel_la_SOURCES = \
camel-folder.c \
camel-internet-address.c \
camel-lock.c \
camel-lock-client.c \
camel-medium.c \
camel-mime-filter-bestenc.c \
camel-mime-filter-basic.c \
@ -121,6 +125,7 @@ libcamelinclude_HEADERS = \
camel-folder.h \
camel-internet-address.h \
camel-lock.h \
camel-lock-client.h \
camel-medium.h \
camel-mime-filter-bestenc.h \
camel-mime-filter-basic.h \
@ -191,13 +196,23 @@ libcamel_la_LIBADD = $(top_builddir)/e-util/libeutil.la \
$(OPENSSL_LDFLAGS)
camel-lock-helper: camel-lock-helper.o camel-lock.o
$(CC) -o $@ $^ -I$(srcdir)/.. -I$(srcdir)
install-exec-local:
-file=$(DESTDIR)$(sbindir)/camel-lock-helper; \
chown root $$file && chgrp root $$file && chmod u+s $$file
noinst_HEADERS = \
camel-charset-map-private.h \
camel-private.h \
camel-search-private.h
camel-search-private.h \
camel-lock-helper.h
# manually include camel-lock-helper.c since we build it manually
EXTRA_DIST = \
README
README \
camel-lock-helper.c
#noinst_PROGRAMS = \
# camel-mime-filter-from

View File

@ -801,7 +801,10 @@ CamelMessageInfo *camel_folder_summary_info_new_from_header(CamelFolderSummary *
* @mp:
*
* Create a new info record from a parser. If the parser cannot
* determine a uid, then a new one is automatically assigned.
* determine a uid, then none will be assigned.
* If indexing is enabled, and the parser cannot determine a new uid, then
* one is automatically assigned.
*
* If indexing is enabled, then the content will be indexed based
* on this new uid. In this case, the message info MUST be
@ -831,7 +834,8 @@ CamelMessageInfo *camel_folder_summary_info_new_from_parser(CamelFolderSummary *
/* assign a unique uid, this is slightly 'wrong' as we do not really
* know if we are going to store this in the summary, but no matter */
summary_assign_uid(s, info);
if (p->index)
summary_assign_uid(s, info);
CAMEL_SUMMARY_LOCK(s, filter_lock);

329
camel/camel-lock-client.c Normal file
View File

@ -0,0 +1,329 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2001 Ximian Inc.
*
* Authors: Michael Zucchi <notzed@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef ENABLE_THREADS
#include <pthread.h>
#endif
#include "camel-exception.h"
#include "camel-lock-helper.h"
#include "camel-lock-client.h"
#define d(x)
/* dunno where this fucking thing is got from */
/* see also camel-lock.c */
#define _(x) (x)
#ifdef ENABLE_THREADS
static pthread_mutex_t lock_lock = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() pthread_mutex_lock(&lock_lock)
#define UNLOCK() pthread_mutex_unlock(&lock_lock)
#else
#define LOCK()
#define UNLOCK()
#endif
static int lock_sequence;
static int lock_helper_pid = -1;
static int lock_stdin_pipe[2], lock_stdout_pipe[2];
static int read_n(int fd, void *buffer, int inlen)
{
char *p = buffer;
int len, left = inlen;
do {
len = read(fd, p, left);
if (len == -1) {
if (errno != EINTR)
return -1;
} else {
left -= len;
p += len;
}
} while (left > 0 && len != 0);
return inlen - left;
}
static int write_n(int fd, void *buffer, int inlen)
{
char *p = buffer;
int len, left = inlen;
do {
len = write(fd, p, left);
if (len == -1) {
if (errno != EINTR)
return -1;
} else {
left -= len;
p += len;
}
} while (left > 0);
return inlen;
}
static int camel_lock_helper_init(CamelException *ex)
{
/* FIXME: check returns */
if (pipe(lock_stdin_pipe) == -1
|| pipe(lock_stdout_pipe) == -1) {
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Cannot build locking helper pipe: %s"),
strerror(errno));
}
lock_helper_pid = fork();
switch(lock_helper_pid) {
case -1:
close(lock_stdin_pipe[0]);
close(lock_stdin_pipe[1]);
close(lock_stdout_pipe[0]);
close(lock_stdout_pipe[1]);
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Cannot fork locking helper: %s"),
strerror(errno));
return -1;
case 0:
close(STDIN_FILENO);
dup(lock_stdin_pipe[0]);
close(STDOUT_FILENO);
dup(lock_stdout_pipe[1]);
close(lock_stdin_pipe[0]);
close(lock_stdin_pipe[1]);
close(lock_stdout_pipe[0]);
close(lock_stdout_pipe[1]);
execl(CAMEL_SBINDIR "/camel-lock-helper", "camel-lock-helper", NULL);
d(fprintf(stderr, "shit, couldn't exec lock helper!\n"));
/* it'll pick this up when it tries to use us */
exit(255);
default:
close(lock_stdin_pipe[0]);
close(lock_stdout_pipe[1]);
/* so the child knows when we vanish */
fcntl(lock_stdin_pipe[1], F_SETFD, FD_CLOEXEC);
fcntl(lock_stdout_pipe[0], F_SETFD, FD_CLOEXEC);
}
return 0;
}
int camel_lock_helper_lock(const char *path, CamelException *ex)
{
struct _CamelLockHelperMsg *msg;
int len = strlen(path);
int res = -1;
int retry = 3;
LOCK();
if (lock_helper_pid == -1) {
if (camel_lock_helper_init(ex) == -1) {
UNLOCK();
return -1;
}
}
msg = alloca(len + sizeof(*msg));
again:
msg->magic = CAMEL_LOCK_HELPER_MAGIC;
msg->seq = lock_sequence;
msg->id = CAMEL_LOCK_HELPER_LOCK;
msg->data = len;
memcpy(msg+1, path, len);
write_n(lock_stdin_pipe[1], msg, len+sizeof(*msg));
do {
/* should also have a timeout here? cancellation? */
len = read_n(lock_stdout_pipe[0], msg, sizeof(*msg));
if (len == 0) {
/* child quit, do we try ressurect it? */
res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
/* if the child exited, this should get it, waidpid returns 0 if the child hasn't */
if (waitpid(lock_helper_pid, NULL, WNOHANG) > 0) {
lock_helper_pid = -1;
close(lock_stdout_pipe[0]);
close(lock_stdin_pipe[1]);
lock_stdout_pipe[0] = -1;
lock_stdin_pipe[1] = -1;
}
goto fail;
}
if (msg->magic != CAMEL_LOCK_HELPER_RETURN_MAGIC
|| msg->seq > lock_sequence) {
res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
d(printf("lock child protocol error\n"));
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Could not lock '%s': protocol error with lock-helper"), path);
goto fail;
}
} while (msg->seq < lock_sequence);
if (msg->seq == lock_sequence) {
switch(msg->id) {
case CAMEL_LOCK_HELPER_STATUS_OK:
d(printf("lock child locked ok, id is %d\n", msg->data));
res = msg->data;
break;
default:
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Could not lock '%s'"), path);
d(printf("locking failed ! status = %d\n", msg->id));
break;
}
} else if (retry > 0) {
d(printf("sequence failure, lost message? retry?\n"));
retry--;
goto again;
} else {
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Could not lock '%s': protocol error with lock-helper"), path);
}
fail:
lock_sequence++;
UNLOCK();
return res;
}
int camel_lock_helper_unlock(int lockid)
{
struct _CamelLockHelperMsg *msg;
int res = -1;
int retry = 3;
int len;
d(printf("unlocking lock id %d\n", lockid));
LOCK();
/* impossible to unlock if we haven't locked yet */
if (lock_helper_pid == -1) {
UNLOCK();
return -1;
}
msg = alloca(sizeof(*msg));
again:
msg->magic = CAMEL_LOCK_HELPER_MAGIC;
msg->seq = lock_sequence;
msg->id = CAMEL_LOCK_HELPER_UNLOCK;
msg->data = lockid;
write_n(lock_stdin_pipe[1], msg, sizeof(*msg));
do {
/* should also have a timeout here? cancellation? */
len = read_n(lock_stdout_pipe[0], msg, sizeof(*msg));
if (len == 0) {
/* child quit, do we try ressurect it? */
res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
if (waitpid(lock_helper_pid, NULL, WNOHANG) > 0) {
lock_helper_pid = -1;
close(lock_stdout_pipe[0]);
close(lock_stdin_pipe[1]);
lock_stdout_pipe[0] = -1;
lock_stdin_pipe[1] = -1;
}
goto fail;
}
if (msg->magic != CAMEL_LOCK_HELPER_RETURN_MAGIC
|| msg->seq > lock_sequence) {
goto fail;
}
} while (msg->seq < lock_sequence);
if (msg->seq == lock_sequence) {
switch(msg->id) {
case CAMEL_LOCK_HELPER_STATUS_OK:
d(printf("lock child unlocked ok\n"));
res = 0;
break;
default:
d(printf("locking failed ! \n"));
break;
}
} else if (retry > 0) {
d(printf("sequence failure, lost message? retry?\n"));
lock_sequence++;
retry--;
goto again;
}
fail:
lock_sequence++;
UNLOCK();
return res;
}
#if 0
int main(int argc, char **argv)
{
int id1, id2;
d(printf("locking started\n"));
camel_lock_helper_init();
id1 = camel_lock_helper_lock("1 path 1");
if (id1 != -1) {
d(printf("lock ok, unlock\n"));
camel_lock_helper_unlock(id1);
}
id1 = camel_lock_helper_lock("2 path 1");
id2 = camel_lock_helper_lock("2 path 2");
camel_lock_helper_unlock(id2);
camel_lock_helper_unlock(id1);
id1 = camel_lock_helper_lock("3 path 1");
id2 = camel_lock_helper_lock("3 path 2");
camel_lock_helper_unlock(id1);
}
#endif

42
camel/camel-lock-client.h Normal file
View File

@ -0,0 +1,42 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Author: Michael Zucchi <notzed@ximian.com>
*
* Copyright (C) 2001 Ximian Inc (http://www.ximian.com/)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/* defines protocol for lock helper process ipc */
#ifndef _CAMEL_LOCK_CLIENT_H
#define _CAMEL_LOCK_CLIENT_H
#ifdef __cplusplus
extern "C" {
#pragma }
#endif /* __cplusplus }*/
#include <camel/camel-exception.h>
int camel_lock_helper_lock(const char *path , CamelException *ex);
int camel_lock_helper_unlock(int lockid);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* !_CAMEL_LOCK_HELPER_H */

389
camel/camel-lock-helper.c Normal file
View File

@ -0,0 +1,389 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2001 Ximian Inc.
*
* Authors: Michael Zucchi <notzed@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/* lock helper process */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <utime.h>
#include <time.h>
#define SETEUID_SAVES (1)
/* we try and include as little as possible */
#include "camel-lock-helper.h"
#include "camel-lock.h"
#define d(x)
/* keeps track of open locks */
struct _lock_info {
struct _lock_info *next;
uid_t uid;
int id;
int depth;
time_t stamp; /* when last updated */
char path[1];
};
static int lock_id = 0;
static struct _lock_info *lock_info_list;
static uid_t lock_root_uid = -1;
static uid_t lock_real_uid = -1;
/* utility functions */
static int read_n(int fd, void *buffer, int inlen)
{
char *p = buffer;
int len, left = inlen;
do {
len = read(fd, p, left);
if (len == -1) {
if (errno != EINTR)
return -1;
} else {
left -= len;
p += len;
}
} while (left > 0 && len != 0);
return inlen - left;
}
static int write_n(int fd, void *buffer, int inlen)
{
char *p = buffer;
int len, left = inlen;
do {
len = write(fd, p, left);
if (len == -1) {
if (errno != EINTR)
return -1;
} else {
left -= len;
p += len;
}
} while (left > 0);
return inlen;
}
void
camel_exception_setv (CamelException *ex, ExceptionId id, const char *format, ...)
{
;
}
void
camel_exception_clear (CamelException *exception)
{
;
}
char *gettext (const char *msgid);
char *
gettext (const char *msgid)
{
return NULL;
}
static int lock_path(const char *path, guint32 *lockid)
{
struct _lock_info *info = NULL;
int res = CAMEL_LOCK_HELPER_STATUS_OK;
struct stat st;
d(fprintf(stderr, "locking path '%s' id = %d\n", path, lock_id));
/* check to see if we have it locked already, make the lock 'recursive' */
/* we could also error i suppose, but why bother */
info = lock_info_list;
while (info) {
if (!strcmp(info->path, path)) {
info->depth++;
return CAMEL_LOCK_HELPER_STATUS_OK;
}
info = info->next;
}
/* check we are allowed to lock it, we must own it, be able to write to it, and it has to exist */
if (stat(path, &st) == -1
|| st.st_uid != getuid()
|| !S_ISREG(st.st_mode)
|| (st.st_mode & 0400) == 0) {
return CAMEL_LOCK_HELPER_STATUS_INVALID;
}
info = malloc(sizeof(*info) + strlen(path));
if (info == NULL) {
res = CAMEL_LOCK_HELPER_STATUS_NOMEM;
goto fail;
}
/* we try the real uid first, and if that fails, try the 'root id' */
if (camel_lock_dot(path, NULL) == -1) {
#ifdef SETEUID_SAVES
if (lock_real_uid != lock_root_uid) {
if (seteuid(lock_root_uid) != -1) {
if (camel_lock_dot(path, NULL) == -1) {
seteuid(lock_real_uid);
res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
goto fail;
}
seteuid(lock_real_uid);
} else {
res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
goto fail;
}
} else {
res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
goto fail;
}
#else
res = CAMEL_LOCK_HELPER_STATUS_SYSTEM;
goto fail;
#endif
} else {
info->uid = lock_real_uid;
}
strcpy(info->path, path);
info->id = lock_id;
info->depth = 1;
info->next = lock_info_list;
info->stamp = time(0);
lock_info_list = info;
if (lockid)
*lockid = lock_id;
lock_id++;
d(fprintf(stderr, "lock ok\n"));
return res;
fail:
d(fprintf(stderr, "lock failed\n"));
if (info)
free(info);
return res;
}
static int unlock_id(guint32 lockid)
{
struct _lock_info *info, *p;
d(fprintf(stderr, "unlocking id '%d'\n", lockid));
p = (struct _lock_info *)&lock_info_list;
info = p->next;
while (info) {
if (info->id == lockid) {
d(fprintf(stderr, "found id %d path '%s'\n", lockid, info->path));
info->depth--;
if (info->depth <= 0) {
#ifdef SETEUID_SAVES
if (info->uid != lock_real_uid) {
seteuid(lock_root_uid);
camel_unlock_dot(info->path);
seteuid(lock_real_uid);
}
#else
camel_unlock_dot(info->path);
#endif
p->next = info->next;
free(info);
}
return CAMEL_LOCK_HELPER_STATUS_OK;
}
p = info;
info = info->next;
}
d(fprintf(stderr, "unknown id asked to be unlocked %d\n", lockid));
return CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
}
static void lock_touch(const char *path)
{
char *name;
/* we could also check that we haven't had our lock stolen from us here */
name = alloca(strlen(path) + 10);
sprintf(name, "%s.lock", path);
d(fprintf(stderr, "Updating lock %s\n", name));
utime(name, NULL);
}
static void setup_process(void)
{
struct sigaction sa;
sigset_t sigset;
/* ignore sigint/sigio */
sa.sa_handler = SIG_IGN;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigemptyset(&sigset);
sigaddset(&sigset, SIGIO);
sigaddset(&sigset, SIGINT);
sigprocmask(SIG_UNBLOCK, &sigset, NULL);
sigaction (SIGIO, &sa, NULL);
sigaction (SIGINT, &sa, NULL);
/* FIXME: add more sanity checks/setup here */
#ifdef SETEUID_SAVES
/* here we change to the real user id, this is probably not particularly
portable so may need configure checks */
lock_real_uid = getuid();
lock_root_uid = geteuid();
if (lock_real_uid != lock_root_uid)
seteuid(lock_real_uid);
#endif
}
int main(int argc, char **argv)
{
struct _CamelLockHelperMsg msg;
int len;
int res;
char *path;
fd_set rset;
struct timeval tv;
struct _lock_info *info;
setup_process();
do {
/* do a poll/etc, so we can refresh the .locks as required ... */
FD_ZERO(&rset);
FD_SET(STDIN_FILENO, &rset);
/* check the minimum timeout we need to refresh the next oldest lock */
if (lock_info_list) {
time_t now = time(0);
time_t left;
time_t delay = CAMEL_DOT_LOCK_REFRESH;
info = lock_info_list;
while (info) {
left = CAMEL_DOT_LOCK_REFRESH - (now - info->stamp);
left = MAX(left, 0);
delay = MIN(left, delay);
info = info->next;
}
tv.tv_sec = delay;
tv.tv_usec = 0;
}
d(fprintf(stderr, "lock helper waiting for input\n"));
if (select(STDIN_FILENO+1, &rset, NULL, NULL, lock_info_list?&tv:NULL) == -1) {
if (errno == EINTR)
break;
continue;
}
/* did we get a timeout? scan for any locks that need updating */
if (!FD_ISSET(STDIN_FILENO, &rset)) {
time_t now = time(0);
time_t left;
d(fprintf(stderr, "Got a timeout, checking locks\n"));
info = lock_info_list;
while (info) {
left = (now - info->stamp);
if (left >= CAMEL_DOT_LOCK_REFRESH) {
lock_touch(info->path);
info->stamp = now;
}
info = info->next;
}
continue;
}
len = read_n(STDIN_FILENO, &msg, sizeof(msg));
if (len == 0)
break;
res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
if (len == sizeof(msg) && msg.magic == CAMEL_LOCK_HELPER_MAGIC) {
switch(msg.id) {
case CAMEL_LOCK_HELPER_LOCK:
res = CAMEL_LOCK_HELPER_STATUS_NOMEM;
path = malloc(msg.data+1);
if (path != NULL) {
res = CAMEL_LOCK_HELPER_STATUS_PROTOCOL;
len = read_n(STDIN_FILENO, path, msg.data);
if (len == msg.data) {
path[len] = 0;
res = lock_path(path, &msg.data);
}
free(path);
}
break;
case CAMEL_LOCK_HELPER_UNLOCK:
res = unlock_id(msg.data);
break;
}
}
d(fprintf(stderr, "returning result %d\n", res));
msg.id = res;
msg.magic = CAMEL_LOCK_HELPER_RETURN_MAGIC;
write_n(STDOUT_FILENO, &msg, sizeof(msg));
} while (1);
d(fprintf(stderr, "parent exited, clsoing down remaining id's\n"));
while (lock_info_list)
unlock_id(lock_info_list->id);
return 0;
}

70
camel/camel-lock-helper.h Normal file
View File

@ -0,0 +1,70 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Author: Michael Zucchi <notzed@helixcode.com>
*
* Copyright (C) 1999 Helix Code (http://www.helixcode.com/).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/* defines protocol for lock helper process ipc */
#ifndef _CAMEL_LOCK_HELPER_H
#define _CAMEL_LOCK_HELPER_H
#ifdef __cplusplus
extern "C" {
#pragma }
#endif /* __cplusplus }*/
#include <glib.h>
struct _CamelLockHelperMsg {
guint32 magic;
guint32 seq;
guint32 id;
guint32 data;
};
/* magic values */
enum {
CAMEL_LOCK_HELPER_MAGIC = 0xABADF00D,
CAMEL_LOCK_HELPER_RETURN_MAGIC = 0xDEADBEEF
};
/* return status */
enum {
CAMEL_LOCK_HELPER_STATUS_OK = 0,
CAMEL_LOCK_HELPER_STATUS_PROTOCOL,
CAMEL_LOCK_HELPER_STATUS_NOMEM,
CAMEL_LOCK_HELPER_STATUS_SYSTEM,
CAMEL_LOCK_HELPER_STATUS_INVALID, /* not allowed to lock/doesn't exist etc */
};
/* commands */
enum {
CAMEL_LOCK_HELPER_LOCK = 0xf0f,
CAMEL_LOCK_HELPER_UNLOCK = 0xf0f0,
};
/* seconds between lock refreshes */
#define CAMEL_DOT_LOCK_REFRESH (30)
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* !_CAMEL_LOCK_HELPER_H */

View File

@ -46,6 +46,8 @@ static CamelLocalStoreClass *parent_class = NULL;
static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex);
static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex);
static CamelFolderInfo * get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
static void camel_maildir_store_class_init(CamelObjectClass * camel_maildir_store_class)
{
CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_maildir_store_class);
@ -56,6 +58,9 @@ static void camel_maildir_store_class_init(CamelObjectClass * camel_maildir_stor
/* virtual method overload, use defaults for most */
camel_store_class->get_folder = get_folder;
camel_store_class->delete_folder = delete_folder;
camel_store_class->get_folder_info = get_folder_info;
camel_store_class->free_folder_info = camel_store_free_folder_info_full;
}
CamelType camel_maildir_store_get_type(void)
@ -198,3 +203,112 @@ static void delete_folder(CamelStore * store, const char *folder_name, CamelExce
g_free(cur);
g_free(new);
}
static CamelFolderInfo *camel_folder_info_new(const char *url, const char *full, const char *name, int unread)
{
CamelFolderInfo *fi;
fi = g_malloc0(sizeof(*fi));
fi->url = g_strdup(url);
fi->full_name = g_strdup(full);
fi->name = g_strdup(name);
fi->unread_message_count = unread;
return fi;
}
static int scan_dir(char *root, const char *path, guint32 flags, CamelFolderInfo *parent, CamelFolderInfo **fip, CamelException *ex)
{
DIR *dir;
struct dirent *d;
char *name, *uri, *tmp, *cur, *new;
const char *base;
CamelFolderInfo *fi = NULL;
struct stat st;
/* look for folders matching the right structure, recursively */
name = g_strdup_printf("%s/%s", root, path);
printf("checking dir '%s' part '%s' for maildir content\n", root, path);
tmp = g_strdup_printf("%s/tmp", name);
cur = g_strdup_printf("%s/cur", name);
new = g_strdup_printf("%s/new", name);
if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)
&& stat(cur, &st) == 0 && S_ISDIR(st.st_mode)
&& stat(new, &st) == 0 && S_ISDIR(st.st_mode)) {
uri = g_strdup_printf("maildir://%s#%s", root, path);
base = strrchr(path, '/');
if (base)
base++;
else
base = path;
fi = camel_folder_info_new(uri, path, base, -1);
printf("found! uri = %s\n", fi->url);
printf(" full_name = %s\n name = '%s'\n", fi->full_name, fi->name);
fi->parent = parent;
fi->sibling = *fip;
*fip = fi;
g_free(uri);
}
g_free(tmp);
g_free(cur);
g_free(new);
/* we only look further if we found one at this level */
if (fi && ((flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) || parent == NULL)) {
dir = opendir(name);
if (dir == NULL) {
camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
_("Could not scan folder `%s': %s"),
root, strerror(errno));
g_free(name);
return -1;
}
while ( (d = readdir(dir)) ) {
if (strcmp(d->d_name, "tmp") == 0
|| strcmp(d->d_name, "cur") == 0
|| strcmp(d->d_name, "new") == 0
|| strcmp(d->d_name, ".") == 0
|| strcmp(d->d_name, "..") == 0)
continue;
tmp = g_strdup_printf("%s/%s", name, d->d_name);
if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)) {
new = g_strdup_printf("%s/%s", path, d->d_name);
if (scan_dir(root, new, flags, fi, &fi->child, ex) == -1) {
g_free(tmp);
g_free(new);
closedir(dir);
return -1;
}
g_free(new);
}
g_free(tmp);
}
closedir(dir);
}
g_free(name);
return 0;
}
static CamelFolderInfo *
get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex)
{
CamelFolderInfo *fi = NULL;
CamelService *service = (CamelService *)store;
if (scan_dir(service->url->path, top?top:".", flags, NULL, &fi, ex) == -1 && fi != NULL) {
camel_store_free_folder_info_full(store, fi);
fi = NULL;
}
return fi;
}

View File

@ -44,6 +44,8 @@
#include "camel-mime-filter-from.h"
#include "camel-exception.h"
#include "camel-lock-client.h"
#include "camel-local-private.h"
#define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
@ -187,22 +189,25 @@ camel_spool_folder_construct(CamelSpoolFolder *lf, CamelStore *parent_store, con
root_dir_path = camel_spool_store_get_toplevel_dir(CAMEL_SPOOL_STORE(folder->parent_store));
lf->base_path = g_strdup(root_dir_path);
lf->folder_path = g_strdup_printf("%s/%s", root_dir_path, full_name);
lf->folder_path = g_strdup(root_dir_path);
lf->changes = camel_folder_change_info_new();
lf->flags = flags;
folder->summary = (CamelFolderSummary *)camel_spool_summary_new(lf->folder_path);
if (camel_spool_folder_lock(lf, CAMEL_LOCK_WRITE, ex) != -1) {
camel_spool_summary_check((CamelSpoolSummary *)folder->summary, NULL, ex);
camel_spool_folder_unlock(lf);
if (camel_spool_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1) {
camel_object_unref((CamelObject *)lf);
return NULL;
}
camel_spool_summary_check((CamelSpoolSummary *)folder->summary, NULL, ex);
camel_spool_folder_unlock(lf);
fi = g_malloc0(sizeof(*fi));
fi->full_name = g_strdup(full_name);
fi->name = g_strdup(name);
fi->url = g_strdup(lf->folder_path);
fi->unread_message_count = -1;
fi->unread_message_count = camel_folder_get_unread_message_count(folder);
camel_object_trigger_event(CAMEL_OBJECT(parent_store), "folder_created", fi);
camel_folder_info_free (fi);
@ -256,13 +261,44 @@ int camel_spool_folder_unlock(CamelSpoolFolder *lf)
static int
spool_lock(CamelSpoolFolder *lf, CamelLockType type, CamelException *ex)
{
return 0;
int retry = 0;
lf->lockfd = open(lf->folder_path, O_RDWR, 0);
if (lf->lockfd == -1) {
camel_exception_setv(ex, 1, _("Cannot create folder lock on %s: %s"), lf->folder_path, strerror(errno));
return -1;
}
while (retry < CAMEL_LOCK_RETRY) {
if (retry > 0)
sleep(CAMEL_LOCK_DELAY);
camel_exception_clear(ex);
if (camel_lock_fcntl(lf->lockfd, type, ex) == 0) {
if (camel_lock_flock(lf->lockfd, type, ex) == 0) {
if ((lf->lockid = camel_lock_helper_lock(lf->folder_path, ex)) != -1)
return 0;
camel_unlock_flock(lf->lockfd);
}
camel_unlock_fcntl(lf->lockfd);
}
retry++;
}
return -1;
}
static void
spool_unlock(CamelSpoolFolder *lf)
{
/* nothing */
camel_lock_helper_unlock(lf->lockid);
lf->lockid = -1;
camel_unlock_flock(lf->lockid);
camel_unlock_fcntl(lf->lockid);
close(lf->lockfd);
lf->lockfd = -1;
}
static void
@ -479,10 +515,14 @@ spool_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
d(printf("Getting message %s\n", uid));
/* lock the folder first, burn if we can't */
if (camel_spool_folder_lock(lf, CAMEL_LOCK_READ, ex) == -1)
/* lock the folder first, burn if we can't, need write lock for summary check */
if (camel_spool_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
return NULL;
/* check for new messages, this may renumber uid's though */
if (camel_spool_summary_check((CamelSpoolSummary *)folder->summary, lf->changes, ex) == -1)
return NULL;
retry:
/* get the message summary info */
info = (CamelSpoolMessageInfo *) camel_folder_summary_uid(folder->summary, uid);

View File

@ -49,6 +49,8 @@ typedef struct {
int locked; /* lock counter */
CamelLockType locktype; /* what type of lock we have */
int lockfd; /* lock fd used for fcntl/etc locking */
int lockid; /* lock id for dot locking */
char *base_path; /* base path of the spool folder */
char *folder_path; /* the path to the folder itself */

View File

@ -46,8 +46,9 @@ static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guin
static char *get_name(CamelService *service, gboolean brief);
static CamelFolder *get_inbox (CamelStore *store, CamelException *ex);
static void rename_folder(CamelStore *store, const char *old_name, const char *new_name, CamelException *ex);
static CamelFolderInfo *get_folder_info (CamelStore *store, const char *top,
guint32 flags, CamelException *ex);
static CamelFolderInfo *get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
static void free_folder_info (CamelStore *store, CamelFolderInfo *fi);
static void delete_folder(CamelStore *store, const char *folder_name, CamelException *ex);
static void rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex);
@ -67,7 +68,7 @@ camel_spool_store_class_init (CamelSpoolStoreClass *camel_spool_store_class)
camel_store_class->get_folder = get_folder;
camel_store_class->get_inbox = get_inbox;
camel_store_class->get_folder_info = get_folder_info;
camel_store_class->free_folder_info = camel_store_free_folder_info_full;
camel_store_class->free_folder_info = free_folder_info;
camel_store_class->delete_folder = delete_folder;
camel_store_class->rename_folder = rename_folder;
@ -95,15 +96,42 @@ static void
construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
{
int len;
char *path, *name;
struct stat st;
printf("constructing store of type %s '%s:%s'\n",
camel_type_to_name(((CamelObject *)service)->s.type), url->protocol, url->path);
CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
if (camel_exception_is_set (ex))
return;
if (service->url->path[0] != '/') {
camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
_("Store root %s is not an absolute path"), service->url->path);
return;
}
len = strlen (service->url->path);
if (service->url->path[len - 1] != '/') {
if (len > 0 && service->url->path[len - 1] == '/') {
service->url->path = g_realloc (service->url->path, len + 2);
strcpy (service->url->path + len, "/");
service->url->path[len-1] = 0;
}
path = service->url->path;
len = strlen(path);
name = path;
#if 0
name = alloca(len+1);
strcpy(name, path);
name[len-1] = 0;
#endif
if (stat(name, &st) == -1 || !S_ISREG(st.st_mode)) {
camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
_("Spool `%s' does not exist or is not a regular file"),
path);
return;
}
}
@ -119,35 +147,26 @@ camel_spool_store_get_toplevel_dir (CamelSpoolStore *store)
static CamelFolder *
get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex)
{
struct stat st;
char *path = ((CamelService *)store)->url->path;
char *name;
int len;
if (path[0] != '/') {
camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
_("Store root %s is not an absolute path"), path);
return NULL;
}
printf("opening folder %s on path %s\n", folder_name, path);
name = g_strdup_printf("%s%s", CAMEL_SERVICE(store)->url->path, folder_name);
if (stat(name, &st) == -1) {
/* we only support an 'INBOX' */
if (strcmp(folder_name, "INBOX") != 0) {
camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
_("Folder `%s/%s' does not exist."),
path, folder_name);
g_free(name);
return NULL;
} else if (!S_ISREG(st.st_mode)) {
camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
_("`%s' is not a regular file."),
name);
g_free(name);
return NULL;
}
g_free(name);
len = strlen(path);
name = alloca(len+1);
strcpy(name, path);
name[len-1] = 0;
return camel_spool_folder_new(store, folder_name, flags, ex);
return camel_spool_folder_new(store, name, flags, ex);
}
static CamelFolder *
@ -171,12 +190,30 @@ static CamelFolderInfo *
get_folder_info (CamelStore *store, const char *top,
guint32 flags, CamelException *ex)
{
/* FIXME: This is broken, but it corresponds to what was
* there before.
*/
return NULL;
CamelFolderInfo *fi = NULL;
CamelService *service = (CamelService *)store;
if (top == NULL || strcmp(top, "/INBOX") == 0) {
/* FIXME: if the folder is opened we could look it up? */
fi = g_malloc0(sizeof(*fi));
fi->full_name = "/INBOX";
fi->name = "INBOX";
fi->url = g_strdup_printf("spool://%s#%s", service->url->path, fi->name);
fi->unread_message_count = -1;
}
return fi;
}
static void free_folder_info (CamelStore *store, CamelFolderInfo *fi)
{
if (fi) {
g_free(fi->url);
g_free(fi);
}
}
/* default implementation, rename all */
static void
rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex)

View File

@ -40,7 +40,7 @@ extern "C" {
typedef struct {
CamelStore parent_object;
} CamelSpoolStore;

View File

@ -513,7 +513,7 @@ spool_summary_check(CamelSpoolSummary *cls, CamelFolderChangeInfo *changeinfo, C
if (st.st_size != cls->folder_size || st.st_mtime != s->time) {
if (cls->folder_size < st.st_size) {
/* this will automatically rescan from 0 if there is a problem */
d(printf("folder grew, attempting to rebuild from %d\n", cls>folder_size));
d(printf("folder grew, attempting to rebuild from %d\n", cls->folder_size));
ret = summary_update(cls, cls->folder_size, changeinfo, ex);
} else {
d(printf("folder shrank! rebuilding from start\n"));
@ -538,6 +538,7 @@ spool_summary_check(CamelSpoolSummary *cls, CamelFolderChangeInfo *changeinfo, C
/* if we do, then write out the headers using sync_full, etc */
if (work) {
d(printf("Have to add new headers, re-syncing from the start to accomplish this\n"));
ret = spool_summary_sync_full(cls, FALSE, changeinfo, ex);
if (stat(cls->folder_path, &st) == -1) {
@ -683,7 +684,7 @@ spool_summary_sync_full(CamelSpoolSummary *cls, gboolean expunge, CamelFolderCha
g_assert(info);
d(printf("Looking at message %s\n", info->info.uid));
d(printf("Looking at message %s\n", camel_message_info_uid(info)));
/* only need to seek past deleted messages, otherwise we should be at the right spot/state already */
if (lastdel) {
@ -735,7 +736,7 @@ spool_summary_sync_full(CamelSpoolSummary *cls, gboolean expunge, CamelFolderCha
}
if (info && info->info.flags & (CAMEL_MESSAGE_FOLDER_NOXEV | CAMEL_MESSAGE_FOLDER_FLAGGED)) {
d(printf("Updating header for %s flags = %08x\n", info->info.uid, info->info.flags));
d(printf("Updating header for %s flags = %08x\n", camel_message_info_uid(info), info->info.flags));
if (camel_mime_parser_step(mp, &buffer, &len) == HSCAN_FROM_END) {
g_warning("camel_mime_parser_step failed (2)");
@ -955,7 +956,7 @@ spool_summary_sync_quick(CamelSpoolSummary *cls, gboolean expunge, CamelFolderCh
g_assert(info);
d(printf("Checking message %s %08x\n", info->info.uid, info->info.flags));
d(printf("Checking message %s %08x\n", camel_message_info_uid(info), info->info.flags));
if ((info->info.flags & CAMEL_MESSAGE_FOLDER_FLAGGED) == 0) {
camel_folder_summary_info_free(s, (CamelMessageInfo *)info);
@ -963,7 +964,7 @@ spool_summary_sync_quick(CamelSpoolSummary *cls, gboolean expunge, CamelFolderCh
continue;
}
d(printf("Updating message %s\n", info->info.uid));
d(printf("Updating message %s\n", camel_message_info_uid(info)));
camel_mime_parser_seek(mp, info->frompos, SEEK_SET);
@ -1125,7 +1126,7 @@ spool_summary_add(CamelSpoolSummary *cls, CamelMimeMessage *msg, const CamelMess
mi = camel_folder_summary_add_from_message((CamelFolderSummary *)cls, msg);
if (mi) {
d(printf("Added, uid = %s\n", mi->uid));
d(printf("Added, uid = %s\n", camel_message_info_uid(mi)));
if (info) {
CamelTag *tag = info->user_tags;
CamelFlag *flag = info->user_flags;

View File

@ -49,7 +49,7 @@ int main(int argc, char **argv)
/* create a pseudo-spool file, and check that */
creat("/tmp/camel-test/testbox", 0600);
test_folder_message_ops(session, "spool:///tmp/camel-test", FALSE, TRUE);
test_folder_message_ops(session, "spool:///tmp/camel-test/testbox", FALSE, TRUE);
check_unref(session, 1);
camel_exception_free(ex);

View File

@ -308,6 +308,12 @@ test_folder_message_ops(CamelSession *session, const char *name, int local, int
int indexed, max;
GPtrArray *uids;
CamelMessageInfo *info;
char *mailbox;
if (spool)
mailbox = "INBOX";
else
mailbox = "testbox";
max=local?2:1;
@ -329,7 +335,7 @@ test_folder_message_ops(CamelSession *session, const char *name, int local, int
flags = CAMEL_STORE_FOLDER_CREATE|CAMEL_STORE_FOLDER_BODY_INDEX;
else
flags = CAMEL_STORE_FOLDER_CREATE;
folder = camel_store_get_folder(store, "testbox", flags, ex);
folder = camel_store_get_folder(store, mailbox, flags, ex);
check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
check(folder != NULL);
@ -396,13 +402,13 @@ test_folder_message_ops(CamelSession *session, const char *name, int local, int
pull();
push("deleting test folder, with messages in it");
camel_store_delete_folder(store, "testbox", ex);
camel_store_delete_folder(store, mailbox, ex);
check(camel_exception_is_set(ex));
camel_exception_clear(ex);
pull();
push("re-opening folder");
folder = camel_store_get_folder(store, "testbox", flags, ex);
folder = camel_store_get_folder(store, mailbox, flags, ex);
check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
check(folder != NULL);
@ -505,7 +511,7 @@ test_folder_message_ops(CamelSession *session, const char *name, int local, int
if (!spool) {
push("deleting test folder, with no messages in it");
camel_store_delete_folder(store, "testbox", ex);
camel_store_delete_folder(store, mailbox, ex);
check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
pull();
}