aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-migrate.c
blob: 603bf531f529af9b6a112fa8ad68fe70a1d9aa31 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * e-mail-migrate.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-mail-migrate.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <unistd.h>
#include <dirent.h>
#include <regex.h>
#include <errno.h>
#include <ctype.h>

#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include <gtk/gtk.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include <shell/e-shell.h>
#include <shell/e-shell-migrate.h>

#include <e-util/e-util.h>
#include <libevolution-utils/e-xml-utils.h>

#include <libevolution-utils/e-alert-dialog.h>
#include <e-util/e-util-private.h>
#include <e-util/e-plugin.h>

#include <libemail-engine/e-mail-folder-utils.h>

#include "e-mail-backend.h"
#include "em-utils.h"

#define d(x) x

/* 1.4 upgrade functions */

static GtkProgressBar *progress;

static void
em_migrate_set_progress (double percent)
{
    gchar text[5];

    snprintf (text, sizeof (text), "%d%%", (gint) (percent * 100.0f));

    gtk_progress_bar_set_fraction (progress, percent);
    gtk_progress_bar_set_text (progress, text);

    while (gtk_events_pending ())
        gtk_main_iteration ();
}

enum {
    CP_UNIQUE = 0,
    CP_OVERWRITE,
    CP_APPEND
};

static gint open_flags[3] = {
    O_WRONLY | O_CREAT | O_TRUNC,
    O_WRONLY | O_CREAT | O_TRUNC,
    O_WRONLY | O_CREAT | O_APPEND,
};

static gboolean
cp (const gchar *src,
    const gchar *dest,
    gboolean show_progress,
    gint mode)
{
    guchar readbuf[65536];
    gssize nread, nwritten;
    gint errnosav, readfd, writefd;
    gsize total = 0;
    struct stat st;
    struct utimbuf ut;

    /* if the dest file exists and has content, abort - we don't
     * want to corrupt their existing data */
    if (g_stat (dest, &st) == 0 && st.st_size > 0 && mode == CP_UNIQUE) {
        errno = EEXIST;
        return FALSE;
    }

    if (g_stat (src, &st) == -1
        || (readfd = g_open (src, O_RDONLY | O_BINARY, 0)) == -1)
        return FALSE;

    if ((writefd = g_open (dest, open_flags[mode] | O_BINARY, 0666)) == -1) {
        errnosav = errno;
        close (readfd);
        errno = errnosav;
        return FALSE;
    }

    do {
        do {
            nread = read (readfd, readbuf, sizeof (readbuf));
        } while (nread == -1 && errno == EINTR);

        if (nread == 0)
            break;
        else if (nread < 0)
            goto exception;

        do {
            nwritten = write (writefd, readbuf, nread);
        } while (nwritten == -1 && errno == EINTR);

        if (nwritten < nread)
            goto exception;

        total += nwritten;
        if (show_progress)
            em_migrate_set_progress (((gdouble) total) / ((gdouble) st.st_size));
    } while (total < st.st_size);

    if (fsync (writefd) == -1)
        goto exception;

    close (readfd);
    if (close (writefd) == -1)
        goto failclose;

    ut.actime = st.st_atime;
    ut.modtime = st.st_mtime;
    utime (dest, &ut);
    chmod (dest, st.st_mode);

    return TRUE;

 exception:

    errnosav = errno;
    close (readfd);
    close (writefd);
    errno = errnosav;

 failclose:

    errnosav = errno;
    unlink (dest);
    errno = errnosav;

    return FALSE;
}

static gboolean
emm_setup_initial (const gchar *data_dir)
{
    GDir *dir;
    const gchar *d;
    gchar *local = NULL, *base;
    const gchar * const *language_names;

    /* special-case - this means brand new install of evolution */
    /* FIXME: create default folders and stuff... */

    d (printf ("Setting up initial mail tree\n"));

    base = g_build_filename (data_dir, "local", NULL);
    if (g_mkdir_with_parents (base, 0700) == -1 && errno != EEXIST) {
        g_free (base);
        return FALSE;
    }

    /* e.g. try en-AU then en, etc */
    language_names = g_get_language_names ();
    while (*language_names != NULL) {
        local = g_build_filename (
            EVOLUTION_PRIVDATADIR, "default",
            *language_names, "mail", "local", NULL);
        if (g_file_test (local, G_FILE_TEST_EXISTS))
            break;
        g_free (local);
        language_names++;
    }

    /* Make sure we found one. */
    g_return_val_if_fail (*language_names != NULL, FALSE);

    dir = g_dir_open (local, 0, NULL);
    if (dir) {
        while ((d = g_dir_read_name (dir))) {
            gchar *src, *dest;

            src = g_build_filename (local, d, NULL);
            dest = g_build_filename (base, d, NULL);

            cp (src, dest, FALSE, CP_UNIQUE);
            g_free (dest);
            g_free (src);
        }
        g_dir_close (dir);
    }

    g_free (local);
    g_free (base);

    return TRUE;
}

static void
em_rename_view_in_folder (gpointer data,
                          gpointer user_data)
{
    const gchar *filename = data;
    const gchar *views_dir = user_data;
    gchar *folderpos, *dotpos;

    g_return_if_fail (filename != NULL);
    g_return_if_fail (views_dir != NULL);

    folderpos = strstr (filename, "-folder:__");
    if (!folderpos)
        folderpos = strstr (filename, "-folder___");
    if (!folderpos)
        return;

    /* points on 'f' from the "folder" word */
    folderpos++;
    dotpos = strrchr (filename, '.');
    if (folderpos < dotpos && g_str_equal (dotpos, ".xml")) {
        GChecksum *checksum;
        gchar *oldname, *newname, *newfile;
        const gchar *md5_string;

        *dotpos = 0;

        /* use MD5 checksum of the folder URI, to not depend on its length */
        checksum = g_checksum_new (G_CHECKSUM_MD5);
        g_checksum_update (checksum, (const guchar *) folderpos, -1);

        *folderpos = 0;
        md5_string = g_checksum_get_string (checksum);
        newfile = g_strconcat (filename, md5_string, ".xml", NULL);
        *folderpos = 'f';
        *dotpos = '.';

        oldname = g_build_filename (views_dir, filename, NULL);
        newname = g_build_filename (views_dir, newfile, NULL);

        g_rename (oldname, newname);

        g_checksum_free (checksum);
        g_free (oldname);
        g_free (newname);
        g_free (newfile);
    }
}

static void
em_rename_folder_views (EShellBackend *shell_backend)
{
    const gchar *config_dir;
    gchar *views_dir;
    GDir *dir;

    g_return_if_fail (shell_backend != NULL);

    config_dir = e_shell_backend_get_config_dir (shell_backend);
    views_dir = g_build_filename (config_dir, "views", NULL);

    dir = g_dir_open (views_dir, 0, NULL);
    if (dir) {
        GSList *to_rename = NULL;
        const gchar *filename;

        while (filename = g_dir_read_name (dir), filename) {
            if (strstr (filename, "-folder:__") ||
                strstr (filename, "-folder___"))
                to_rename = g_slist_prepend (to_rename, g_strdup (filename));
        }

        g_dir_close (dir);

        g_slist_foreach (to_rename, em_rename_view_in_folder, views_dir);
        g_slist_free_full (to_rename, g_free);
    }

    g_free (views_dir);
}

gboolean
e_mail_migrate (EShellBackend *shell_backend,
                gint major,
                gint minor,
                gint micro,
                GError **error)
{
    struct stat st;
    const gchar *data_dir;

    /* make sure ~/.evolution/mail exists */
    data_dir = e_shell_backend_get_data_dir (shell_backend);
    if (g_stat (data_dir, &st) == -1) {
        if (errno != ENOENT || g_mkdir_with_parents (data_dir, 0700) == -1) {
            g_set_error (
                error, E_SHELL_MIGRATE_ERROR,
                E_SHELL_MIGRATE_ERROR_FAILED,
                _("Unable to create local mail folders at "
                "'%s': %s"), data_dir, g_strerror (errno));
            return FALSE;
        }
    }

    if (major == 0)
        return emm_setup_initial (data_dir);

    if (major <= 2 || (major == 3 && minor < 4))
        em_rename_folder_views (shell_backend);

    return TRUE;
}