aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-movemail.c
blob: e15e26e7aeedcae81f16ab05294aa468a5db3d06 (plain) (blame)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-movemail.c: mbox copying function */

/*
 * Author:
 *  Dan Winship <danw@helixcode.com>
 *
 * Copyright 2000 Helix Code, Inc. (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
 */

#include <config.h>

#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

#include "camel-movemail.h"
#include "camel-exception.h"

/**
 * camel_movemail: Copy an mbox file from a shared spool directory to a
 * new folder in a Camel store
 * @source: source file
 * @dest: destination file
 * @ex: a CamelException
 *
 * This copies an mbox file from a shared directory with multiple
 * readers and writers into a private (presumably Camel-controlled)
 * directory. Dot locking is used on the source file (but not the
 * destination).
 *
 * Return value: 1 if mail was copied, 0 if the source file contained
 * no mail, -1 if an error occurred.
 **/
int
camel_movemail (const char *source, const char *dest, CamelException *ex)
{
    gboolean locked, error;
    int sfd, dfd, tmpfd;
    char *locktmpfile, *lockfile;
    struct stat st;
    time_t now, timeout;
    int nread, nwrote;
    char buf[BUFSIZ];

    camel_exception_clear (ex);

    /* Stat and then open the spool file. If it doesn't exist or
     * is empty, the user has no mail. (There's technically a race
     * condition here in that an MDA might have just now locked it
     * to deliver a message, but we don't care. In that case,
     * assuming it's unlocked is equivalent to pretending we were
     * called a fraction earlier.)
     */
    if (stat (source, &st) == -1) {
        if (errno == ENOENT)
            return 0;

        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "Could not check mail file %s: %s",
                      source, g_strerror (errno));
        return -1;
    }
    if (st.st_size == 0)
        return 0;

    sfd = open (source, O_RDWR);
    if (sfd == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "Could not open mail file %s: %s",
                      source, g_strerror (errno));
        return -1;
    }

    dfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (dfd == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "Could not open temporary mail "
                      "file %s: %s", dest, g_strerror (errno));
        close (sfd);
        return -1;
    }

    /* Create the unique lock file. */
    locktmpfile = g_strdup_printf ("%s.lock.XXXXXX", source);
#ifdef HAVE_MKSTEMP
    tmpfd = mkstemp (locktmpfile);
#else
    if (mktemp (locktmpfile)) {
        tmpfd = open (locktmpfile, O_RDWR | O_CREAT | O_EXCL,
                  S_IRUSR | S_IWUSR);
    } else
        tmpfd = -1;
#endif
    if (tmpfd == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "Could not create lock file "
                      "for %s: %s", source, g_strerror (errno));
        close (sfd);
        close (dfd);
        unlink (dest);
        return -1;
    }
    close (tmpfd);

    lockfile = g_strdup_printf ("%s.lock", source);
    locked = FALSE;
    time (&timeout);
    timeout += 30;

    /* Loop trying to lock the file for 30 seconds. */
    while (time (&now) < timeout) {
        /* Try to make the lock. */
        if (symlink (locktmpfile, lockfile) == 0) {
            locked = TRUE;
            break;
        }

        /* If we fail for a reason other than that someone
         * else has the lock, then abort.
         */
        if (errno != EEXIST) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Could not create lock "
                          "file for %s: %s", source,
                          g_strerror (errno));
            break;
        }

        /* Check the modtime on the lock file. */
        if (stat (lockfile, &st) == -1) {
            /* If the lockfile disappeared, try again. */
            if (errno == ENOENT)
                continue;

            /* Some other error. Abort. */
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Could not test lock "
                          "file for %s: %s", source,
                          g_strerror (errno));
            break;
        }

        /* If the lock file is stale, remove it and try again. */
        if (st.st_mtime < now - 60) {
            unlink (lockfile);
            continue;
        }

        /* Otherwise, sleep and try again. */
        sleep (5);
    }

    if (!locked) {
        /* Something has gone awry. */
        if (now >= timeout) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Timed out trying to get "
                          "lock file on %s. Try again "
                          "later.", source);
        }
        g_free (lockfile);
        unlink (locktmpfile);
        g_free (locktmpfile);
        close (sfd);
        close (dfd);
        unlink (dest);
        return -1;
    }

    /* OK. We have the file locked now. */

    /* FIXME: Set a timer to keep the file locked. */

    error = FALSE;
    while (1) {
        int written = 0;

        nread = read (sfd, buf, sizeof (buf));
        if (nread == 0)
            break;
        else if (nread == -1) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Error reading mail file: %s",
                          g_strerror (errno));
            error = TRUE;
            break;
        }

        while (nread) {
            nwrote = write (dfd, buf + written, nread);
            if (nwrote == -1) {
                camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                              "Error writing "
                              "mail temp file: %s",
                              g_strerror (errno));
                error = TRUE;
                break;
            }
            written += nwrote;
            nread -= nwrote;
        }
    }

    /* If no errors occurred copying the data, and we successfully
     * close the destination file, then truncate the source file.
     * If there is some sort of error, delete the destination file.
     */
    if (!error) {
        if (close (dfd) == 0)
            ftruncate (sfd, 0);
        else {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Failed to store mail in "
                          "temp file %s: %s", dest,
                          g_strerror (errno));
            unlink (dest);
            error = TRUE;
        }
    } else {
        close (dfd);
        unlink (dest);
    }
    close (sfd);

    /* Clean up lock files. */
    unlink (lockfile);
    g_free (lockfile);
    unlink (locktmpfile);
    g_free (locktmpfile);

    return error ? -1 : 1;
}