Updated for string-utils namespace changes.
[platform/upstream/evolution-data-server.git] / camel / providers / local / camel-spool-folder.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
2  * 
3  * Authors: Michael Zucchi <notzed@ximian.com>
4  *
5  * Copyright (C) 2001-2003 Ximian, Inc. (www.ximian.com)
6  *
7  * This program is free software; you can redistribute it and/or 
8  * modify it under the terms of version 2 of the GNU General Public 
9  * License as published by the Free Software Foundation.
10  *
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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34
35 #include "camel-spool-folder.h"
36 #include "camel-spool-store.h"
37 #include "camel-stream-fs.h"
38 #include "camel-spool-summary.h"
39 #include "camel-data-wrapper.h"
40 #include "camel-mime-message.h"
41 #include "camel-stream-filter.h"
42 #include "camel-mime-filter-from.h"
43 #include "camel-exception.h"
44
45 #include "camel-lock-client.h"
46
47 #include "camel-local-private.h"
48
49 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
50
51 static CamelFolderClass *parent_class = NULL;
52
53 /* Returns the class for a CamelSpoolFolder */
54 #define CSPOOLF_CLASS(so) CAMEL_SPOOL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
55 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
56 #define CSPOOLS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
57
58 static CamelLocalSummary *spool_create_summary(const char *path, const char *folder, CamelIndex *index);
59
60 static int spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
61 static void spool_unlock(CamelLocalFolder *lf);
62
63 static void spool_finalize(CamelObject * object);
64
65 static void
66 camel_spool_folder_class_init(CamelSpoolFolderClass *klass)
67 {
68         CamelLocalFolderClass *lklass = (CamelLocalFolderClass *)klass;
69
70         parent_class = (CamelFolderClass *)camel_mbox_folder_get_type();
71
72         /* virtual method overload */
73         lklass->create_summary = spool_create_summary;
74         lklass->lock = spool_lock;
75         lklass->unlock = spool_unlock;
76 }
77
78 static void
79 spool_init(gpointer object, gpointer klass)
80 {
81         CamelSpoolFolder *spool_folder = object;
82
83         spool_folder->lockid = -1;
84 }
85
86 static void
87 spool_finalize(CamelObject * object)
88 {
89         /*CamelSpoolFolder *spool_folder = CAMEL_SPOOL_FOLDER(object);*/
90 }
91
92 CamelType camel_spool_folder_get_type(void)
93 {
94         static CamelType camel_spool_folder_type = CAMEL_INVALID_TYPE;
95
96         if (camel_spool_folder_type == CAMEL_INVALID_TYPE) {
97                 camel_spool_folder_type = camel_type_register(camel_mbox_folder_get_type(), "CamelSpoolFolder",
98                                                              sizeof(CamelSpoolFolder),
99                                                              sizeof(CamelSpoolFolderClass),
100                                                              (CamelObjectClassInitFunc) camel_spool_folder_class_init,
101                                                              NULL,
102                                                              (CamelObjectInitFunc) spool_init,
103                                                              (CamelObjectFinalizeFunc) spool_finalize);
104         }
105
106         return camel_spool_folder_type;
107 }
108
109 CamelFolder *
110 camel_spool_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
111 {
112         CamelFolder *folder;
113
114         d(printf("Creating spool folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
115
116         folder = (CamelFolder *)camel_object_new(CAMEL_SPOOL_FOLDER_TYPE);
117
118         if (parent_store->flags & CAMEL_STORE_FILTER_INBOX
119             && strcmp(full_name, "INBOX") == 0)
120                 folder->folder_flags |= CAMEL_FOLDER_FILTER_RECENT;
121         flags &= CAMEL_STORE_FOLDER_BODY_INDEX;
122
123         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder, parent_store, full_name, flags, ex);
124         if (folder) {
125                 if (camel_url_get_param(((CamelService *)parent_store)->url, "xstatus"))
126                         camel_mbox_summary_xstatus((CamelMboxSummary *)folder->summary, TRUE);
127         }
128
129         return folder;
130 }
131
132 static CamelLocalSummary *
133 spool_create_summary(const char *path, const char *folder, CamelIndex *index)
134 {
135         return (CamelLocalSummary *)camel_spool_summary_new(folder);
136 }
137
138 static int
139 spool_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
140 {
141         int retry = 0;
142         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
143         CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
144
145         mf->lockfd = open(lf->folder_path, O_RDWR, 0);
146         if (mf->lockfd == -1) {
147                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
148                                       _("Cannot create folder lock on %s: %s"),
149                                       lf->folder_path, g_strerror (errno));
150                 return -1;
151         }
152
153         while (retry < CAMEL_LOCK_RETRY) {
154                 if (retry > 0)
155                         sleep(CAMEL_LOCK_DELAY);
156
157                 camel_exception_clear(ex);
158
159                 if (camel_lock_fcntl(mf->lockfd, type, ex) == 0) {
160                         if (camel_lock_flock(mf->lockfd, type, ex) == 0) {
161                                 if ((sf->lockid = camel_lock_helper_lock(lf->folder_path, ex)) != -1)
162                                         return 0;
163                                 camel_unlock_flock(mf->lockfd);
164                         }
165                         camel_unlock_fcntl(mf->lockfd);
166                 }
167                 retry++;
168         }
169
170         return -1;
171 }
172
173 static void
174 spool_unlock(CamelLocalFolder *lf)
175 {
176         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
177         CamelSpoolFolder *sf = (CamelSpoolFolder *)lf;
178
179         camel_lock_helper_unlock(sf->lockid);
180         sf->lockid = -1;
181         camel_unlock_flock(mf->lockfd);
182         camel_unlock_fcntl(mf->lockfd);
183
184         close(mf->lockfd);
185         mf->lockfd = -1;
186 }