1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Copyright (C) 2000 Ximian, Inc.
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
31 #include <sys/types.h>
33 #include <glib/gi18n-lib.h>
35 #include "camel-exception.h"
36 #include "camel-private.h"
37 #include "camel-url.h"
39 #include "camel-maildir-folder.h"
40 #include "camel-maildir-store.h"
41 #include "camel-maildir-summary.h"
45 static CamelLocalStoreClass *parent_class = NULL;
47 /* Returns the class for a CamelMaildirStore */
48 #define CMAILDIRS_CLASS(so) CAMEL_MAILDIR_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
49 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
50 #define CMAILDIRF_CLASS(so) CAMEL_MAILDIR_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
52 static CamelFolder *get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex);
53 static CamelFolder *get_inbox (CamelStore *store, CamelException *ex);
54 static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex);
55 static void maildir_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex);
57 static CamelFolderInfo * get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
59 static gboolean maildir_compare_folder_name(const void *a, const void *b);
60 static guint maildir_hash_folder_name(const void *a);
62 static void camel_maildir_store_class_init(CamelObjectClass * camel_maildir_store_class)
64 CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_maildir_store_class);
65 /*CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_maildir_store_class);*/
67 parent_class = (CamelLocalStoreClass *)camel_type_get_global_classfuncs(camel_local_store_get_type());
69 /* virtual method overload, use defaults for most */
70 camel_store_class->hash_folder_name = maildir_hash_folder_name;
71 camel_store_class->compare_folder_name = maildir_compare_folder_name;
72 camel_store_class->get_folder = get_folder;
73 camel_store_class->get_inbox = get_inbox;
74 camel_store_class->delete_folder = delete_folder;
75 camel_store_class->rename_folder = maildir_rename_folder;
77 camel_store_class->get_folder_info = get_folder_info;
78 camel_store_class->free_folder_info = camel_store_free_folder_info_full;
81 CamelType camel_maildir_store_get_type(void)
83 static CamelType camel_maildir_store_type = CAMEL_INVALID_TYPE;
85 if (camel_maildir_store_type == CAMEL_INVALID_TYPE) {
86 camel_maildir_store_type = camel_type_register(CAMEL_LOCAL_STORE_TYPE, "CamelMaildirStore",
87 sizeof(CamelMaildirStore),
88 sizeof(CamelMaildirStoreClass),
89 (CamelObjectClassInitFunc) camel_maildir_store_class_init,
95 return camel_maildir_store_type;
98 /* This fixes up some historical cruft of names starting with "./" */
100 md_canon_name(const char *a)
105 if (a[0] == '.' && a[1] == '/')
111 static guint maildir_hash_folder_name(const void *a)
113 return g_str_hash(md_canon_name(a));
116 static gboolean maildir_compare_folder_name(const void *a, const void *b)
118 return g_str_equal(md_canon_name(a), md_canon_name(b));
122 get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex)
124 char *name, *tmp, *cur, *new;
126 CamelFolder *folder = NULL;
128 folder_name = md_canon_name(folder_name);
130 if (!((CamelStoreClass *)parent_class)->get_folder(store, folder_name, flags, ex))
133 name = g_strdup_printf("%s%s", CAMEL_LOCAL_STORE(store)->toplevel_dir, folder_name);
134 tmp = g_strdup_printf("%s/tmp", name);
135 cur = g_strdup_printf("%s/cur", name);
136 new = g_strdup_printf("%s/new", name);
138 if (!strcmp(folder_name, ".")) {
139 /* special case "." (aka inbox), may need to be created */
140 if (stat(tmp, &st) != 0 || !S_ISDIR(st.st_mode)
141 || stat(cur, &st) != 0 || !S_ISDIR(st.st_mode)
142 || stat(new, &st) != 0 || !S_ISDIR(st.st_mode)) {
143 if (mkdir(tmp, 0700) != 0
144 || mkdir(cur, 0700) != 0
145 || mkdir(new, 0700) != 0) {
146 camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
147 _("Cannot create folder `%s': %s"),
148 folder_name, g_strerror(errno));
155 folder = camel_maildir_folder_new(store, folder_name, flags, ex);
156 } else if (stat(name, &st) == -1) {
157 /* folder doesn't exist, see if we should create it */
158 if (errno != ENOENT) {
159 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
160 _("Cannot get folder `%s': %s"),
161 folder_name, g_strerror (errno));
162 } else if ((flags & CAMEL_STORE_FOLDER_CREATE) == 0) {
163 camel_exception_setv (ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
164 _("Cannot get folder `%s': folder does not exist."),
167 if (mkdir(name, 0700) != 0
168 || mkdir(tmp, 0700) != 0
169 || mkdir(cur, 0700) != 0
170 || mkdir(new, 0700) != 0) {
171 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
172 _("Cannot create folder `%s': %s"),
173 folder_name, g_strerror (errno));
179 folder = camel_maildir_folder_new(store, folder_name, flags, ex);
182 } else if (!S_ISDIR(st.st_mode)
183 || stat(tmp, &st) != 0 || !S_ISDIR(st.st_mode)
184 || stat(cur, &st) != 0 || !S_ISDIR(st.st_mode)
185 || stat(new, &st) != 0 || !S_ISDIR(st.st_mode)) {
186 /* folder exists, but not maildir */
187 camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
188 _("Cannot get folder `%s': not a maildir directory."), name);
189 } else if (flags & CAMEL_STORE_FOLDER_EXCL) {
190 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
191 _("Cannot create folder `%s': folder exists."),
194 folder = camel_maildir_folder_new(store, folder_name, flags, ex);
206 get_inbox (CamelStore *store, CamelException *ex)
208 return camel_store_get_folder(store, ".", CAMEL_STORE_FOLDER_CREATE, ex);
211 static void delete_folder(CamelStore * store, const char *folder_name, CamelException * ex)
213 char *name, *tmp, *cur, *new;
216 if (strcmp(folder_name, ".") == 0) {
217 camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
218 _("Cannot delete folder: %s: Invalid operation"), _("Inbox"));
222 name = g_strdup_printf("%s%s", CAMEL_LOCAL_STORE(store)->toplevel_dir, folder_name);
224 tmp = g_strdup_printf("%s/tmp", name);
225 cur = g_strdup_printf("%s/cur", name);
226 new = g_strdup_printf("%s/new", name);
228 if (stat(name, &st) == -1 || !S_ISDIR(st.st_mode)
229 || stat(tmp, &st) == -1 || !S_ISDIR(st.st_mode)
230 || stat(cur, &st) == -1 || !S_ISDIR(st.st_mode)
231 || stat(new, &st) == -1 || !S_ISDIR(st.st_mode)) {
232 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
233 _("Could not delete folder `%s': %s"),
234 folder_name, errno ? g_strerror (errno) :
235 _("not a maildir directory"));
239 /* remove subdirs first - will fail if not empty */
240 if (rmdir(cur) == -1 || rmdir(new) == -1) {
246 /* for tmp (only), its contents is irrelevant */
249 while ( (d=readdir(dir)) ) {
250 char *name = d->d_name, *file;
252 if (!strcmp(name, ".") || !strcmp(name, ".."))
254 file = g_strdup_printf("%s/%s", tmp, name);
260 if (rmdir(tmp) == -1 || rmdir(name) == -1)
265 /* easier just to mkdir all (and let them fail), than remember what we got to */
270 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
271 _("Could not delete folder `%s': %s"),
272 folder_name, g_strerror (err));
274 /* and remove metadata */
275 ((CamelStoreClass *)parent_class)->delete_folder(store, folder_name, ex);
286 maildir_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex)
288 if (strcmp(old, ".") == 0) {
289 camel_exception_setv(ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
290 _("Cannot rename folder: %s: Invalid operation"), _("Inbox"));
294 ((CamelStoreClass *)parent_class)->rename_folder(store, old, new, ex);
298 fill_fi(CamelStore *store, CamelFolderInfo *fi, guint32 flags)
302 folder = camel_object_bag_get(store->folders, fi->full_name);
305 && (flags & CAMEL_STORE_FOLDER_INFO_FAST) == 0)
306 folder = camel_store_get_folder(store, fi->full_name, 0, NULL);
309 if ((flags & CAMEL_STORE_FOLDER_INFO_FAST) == 0)
310 camel_folder_refresh_info(folder, NULL);
311 fi->unread = camel_folder_get_unread_message_count(folder);
312 fi->total = camel_folder_get_message_count(folder);
313 camel_object_unref(folder);
315 char *path, *folderpath;
316 CamelFolderSummary *s;
319 /* This should be fast enough not to have to test for INFO_FAST */
320 root = camel_local_store_get_toplevel_dir((CamelLocalStore *)store);
321 path = g_strdup_printf("%s/%s.ev-summary", root, fi->full_name);
322 folderpath = g_strdup_printf("%s/%s", root, fi->full_name);
323 s = (CamelFolderSummary *)camel_maildir_summary_new(NULL, path, folderpath, NULL);
324 if (camel_folder_summary_header_load(s) != -1) {
325 fi->unread = s->unread_count;
326 fi->total = s->saved_count;
328 camel_object_unref(s);
335 struct _scan_node *next;
336 struct _scan_node *prev;
344 static guint scan_hash(const void *d)
346 const struct _scan_node *v = d;
348 return v->inode ^ v->dnode;
351 static gboolean scan_equal(const void *a, const void *b)
353 const struct _scan_node *v1 = a, *v2 = b;
355 return v1->inode == v2->inode && v1->dnode == v2->dnode;
358 static void scan_free(void *k, void *v, void *d)
363 static CamelFolderInfo *scan_fi(CamelStore *store, guint32 flags, CamelURL *url, const char *full, const char *name)
366 char *tmp, *cur, *new;
369 fi = g_malloc0(sizeof(*fi));
370 fi->full_name = g_strdup(full);
371 fi->name = g_strdup(name);
372 camel_url_set_fragment(url, fi->full_name);
373 fi->uri = camel_url_to_string(url, 0);
376 /* we only calculate nochildren properly if we're recursive */
377 if (((flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) != 0))
378 fi->flags = CAMEL_FOLDER_NOCHILDREN;
380 d(printf("Adding maildir info: '%s' '%s' '%s'\n", fi->name, fi->full_name, fi->uri));
382 tmp = g_build_filename(url->path, fi->full_name, "tmp", NULL);
383 cur = g_build_filename(url->path, fi->full_name, "cur", NULL);
384 new = g_build_filename(url->path, fi->full_name, "new", NULL);
386 if (!(stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)
387 && stat(cur, &st) == 0 && S_ISDIR(st.st_mode)
388 && stat(new, &st) == 0 && S_ISDIR(st.st_mode)))
389 fi->flags |= CAMEL_FOLDER_NOSELECT;
395 fill_fi(store, fi, flags);
401 scan_dirs(CamelStore *store, guint32 flags, CamelFolderInfo *topfi, CamelURL *url, CamelException *ex)
403 EDList queue = E_DLIST_INITIALISER(queue);
404 struct _scan_node *sn;
405 const char *root = ((CamelService *)store)->url->path;
411 visited = g_hash_table_new(scan_hash, scan_equal);
413 sn = g_malloc0(sizeof(*sn));
415 e_dlist_addtail(&queue, (EDListNode *)sn);
416 g_hash_table_insert(visited, sn, sn);
418 while (!e_dlist_empty(&queue)) {
422 CamelFolderInfo *last;
424 sn = (struct _scan_node *)e_dlist_remhead(&queue);
426 last = (CamelFolderInfo *)&sn->fi->child;
428 if (!strcmp(sn->fi->full_name, "."))
429 name = g_strdup(root);
431 name = g_build_filename(root, sn->fi->full_name, NULL);
436 camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM,
437 _("Could not scan folder `%s': %s"),
438 root, g_strerror(errno));
442 while ( (d = readdir(dir)) ) {
443 if (strcmp(d->d_name, "tmp") == 0
444 || strcmp(d->d_name, "cur") == 0
445 || strcmp(d->d_name, "new") == 0
446 || strcmp(d->d_name, ".") == 0
447 || strcmp(d->d_name, "..") == 0)
450 tmp = g_build_filename(name, d->d_name, NULL);
451 if (stat(tmp, &st) == 0 && S_ISDIR(st.st_mode)) {
452 struct _scan_node in;
454 in.dnode = st.st_dev;
455 in.inode = st.st_ino;
457 /* see if we've visited already */
458 if (g_hash_table_lookup(visited, &in) == NULL) {
459 struct _scan_node *snew = g_malloc(sizeof(*snew));
462 snew->dnode = in.dnode;
463 snew->inode = in.inode;
465 if (!strcmp(sn->fi->full_name, "."))
466 full = g_strdup(d->d_name);
468 full = g_strdup_printf("%s/%s", sn->fi->full_name, d->d_name);
469 snew->fi = scan_fi(store, flags, url, full, d->d_name);
472 last->next = snew->fi;
474 snew->fi->parent = sn->fi;
476 sn->fi->flags &= ~CAMEL_FOLDER_NOCHILDREN;
477 sn->fi->flags |= CAMEL_FOLDER_CHILDREN;
479 g_hash_table_insert(visited, snew, snew);
481 if (((flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) != 0))
482 e_dlist_addtail(&queue, (EDListNode *)snew);
492 g_hash_table_foreach(visited, scan_free, NULL);
493 g_hash_table_destroy(visited);
498 static CamelFolderInfo *
499 get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex)
501 CamelFolderInfo *fi = NULL;
502 CamelLocalStore *local_store = (CamelLocalStore *)store;
505 url = camel_url_new("maildir:", NULL);
506 camel_url_set_path(url, ((CamelService *)local_store)->url->path);
508 if (top == NULL || top[0] == 0) {
509 CamelFolderInfo *scan;
511 /* create a dummy "." parent inbox, use to scan, then put back at the top level */
512 fi = scan_fi(store, flags, url, ".", _("Inbox"));
513 if (scan_dirs(store, flags, fi, url, ex) == -1)
515 fi->next = fi->child;
522 fi->flags &= ~CAMEL_FOLDER_CHILDREN;
523 fi->flags |= CAMEL_FOLDER_SYSTEM|CAMEL_FOLDER_NOCHILDREN|CAMEL_FOLDER_NOINFERIORS|CAMEL_FOLDER_TYPE_INBOX;
524 } else if (!strcmp(top, ".")) {
525 fi = scan_fi(store, flags, url, ".", _("Inbox"));
526 fi->flags |= CAMEL_FOLDER_SYSTEM|CAMEL_FOLDER_NOCHILDREN|CAMEL_FOLDER_NOINFERIORS|CAMEL_FOLDER_TYPE_INBOX;
528 const char *name = strrchr(top, '/');
530 fi = scan_fi(store, flags, url, top, name?name+1:top);
531 if (scan_dirs(store, flags, fi, url, ex) == -1)
541 camel_store_free_folder_info_full(store, fi);