Codebase list cyrus-imapd / lintian-fixes/main backup / lcb.c
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

lcb.c @lintian-fixes/mainraw · history · 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/* lcb.c -- replication-based backup api
 *
 * Copyright (c) 1994-2015 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <config.h>

#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <syslog.h>
#include <sysexits.h>
#include <zlib.h>

#include "lib/cyrusdb.h"
#include "lib/cyr_lock.h"
#include "lib/gzuncat.h"
#include "lib/map.h"
#include "lib/sqldb.h"
#include "lib/util.h"
#include "lib/xmalloc.h"
#include "lib/xsha1.h"
#include "lib/xstrlcat.h"
#include "lib/xstrlcpy.h"

#include "imap/dlist.h"
#include "imap/global.h"
#include "imap/imap_err.h"

#include "backup/backup.h"

#define LIBCYRUS_BACKUP_SOURCE /* this file is part of libcyrus_backup */
#include "backup/lcb_internal.h"
#include "backup/lcb_sqlconsts.h"

static const char *NOUSERID = "\%SHARED";

/* remove this process's staging directory.
 * will warn about and clean up files that are hanging around - these should
 * be removed by dlist_unlink_files but may be missed if we're shutdown by a
 * signal.
 */
EXPORTED void backup_cleanup_staging_path(void)
{
    char name[MAX_MAILBOX_PATH];
    const char *base = config_backupstagingpath();
    DIR *dirp;
    int r;

    r = snprintf(name, MAX_MAILBOX_PATH, "%s/sync./%lu",
                 base, (unsigned long) getpid());
    if (r >= MAX_MAILBOX_PATH) {
        /* path was truncated, don't try to delete it */
        return;
    }

    /* make sure it's empty */
    if ((dirp = opendir(name))) {
        struct dirent *d;
        while ((d = readdir(dirp))) {
            if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                continue;

            char *tmp = strconcat(name, "/", d->d_name, NULL);
            syslog(LOG_INFO, "%s: unlinking leftover stage file: %s", __func__, tmp);
            unlink(tmp);
            free(tmp);
        }
        closedir(dirp);
    }

    r = rmdir(name);
    if (r && errno != ENOENT)
        syslog(LOG_WARNING, "%s rmdir %s: %m", __func__, name);
}

/*
 * use cases:
 *  - backupd needs to be able to append to data stream and update index (exclusive)
 *  - backupd maybe needs to create a new backup from scratch (exclusive)
 *  - reindex needs to gzuc data stream and rewrite index (exclusive)
 *  - compact needs to rewrite data stream and index (exclusive)
 *  - restore needs to read data stream and index (shared)
 *
 * with only one shared case, might as well always lock exclusively...
 */
HIDDEN int backup_real_open(struct backup **backupp,
                            const char *data_fname, const char *index_fname,
                            enum backup_open_reindex reindex,
                            enum backup_open_nonblock nonblock,
                            enum backup_open_create create)
{
    struct backup *backup = xzmalloc(sizeof *backup);
    int r;

    backup->fd = -1;

    backup->data_fname = xstrdup(data_fname);
    backup->index_fname = xstrdup(index_fname);

    int open_flags = O_RDWR | O_APPEND;

    switch (create) {
    case BACKUP_OPEN_CREATE_EXCL:   open_flags |= O_EXCL;  /* fall thru */
    case BACKUP_OPEN_CREATE:        open_flags |= O_CREAT; /*           */
    case BACKUP_OPEN_NOCREATE:      break;
    }

    while (backup->fd == -1) {
        struct stat sbuf1, sbuf2;

        int fd = open(backup->data_fname,
                      open_flags,
                      S_IRUSR | S_IWUSR);
        if (fd < 0) {
            switch (errno) {
            case EEXIST:
                r = IMAP_MAILBOX_EXISTS;
                break;
            case ENOENT:
                r = IMAP_MAILBOX_NONEXISTENT;
                break;
            default:
                xsyslog(LOG_ERR, "IOERROR: open failed",
                                 "filename=<%s>", backup->data_fname);
                r = IMAP_IOERROR;
                break;
            }

            goto error;
        }

        r = lock_setlock(fd, /*excl*/ 1, nonblock, backup->data_fname);
        if (r) {
            if (errno == EWOULDBLOCK || errno == EAGAIN) {
                r = IMAP_MAILBOX_LOCKED;
            }
            else {
                xsyslog(LOG_ERR, "IOERROR: lock_setlock failed",
                                 "filename=<%s>", backup->data_fname);
                r = IMAP_IOERROR;
            }
            goto error;
        }

        r = fstat(fd, &sbuf1);
        if (!r) r = stat(backup->data_fname, &sbuf2);
        if (r) {
            xsyslog(LOG_ERR, "IOERROR: stat failed",
                             "filename=<%s>", backup->data_fname);
            r = IMAP_IOERROR;
            close(fd);
            goto error;
        }

        if (sbuf1.st_ino == sbuf2.st_ino) {
            backup->fd = fd;
            break;
        }

        close(fd);
    }

    if (reindex) {
        // when reindexing, we want to move the old index out of the way
        // and create a new, empty one -- while holding the lock
        char oldindex_fname[PATH_MAX];
        snprintf(oldindex_fname, sizeof(oldindex_fname), "%s.%" PRId64,
                 backup->index_fname, (int64_t) time(NULL));

        r = rename(backup->index_fname, oldindex_fname);
        if (r && errno != ENOENT) {
            xsyslog(LOG_ERR, "IOERROR: rename failed",
                             "source=<%s> dest=<%s>",
                             backup->index_fname, oldindex_fname);
            r = IMAP_IOERROR;
            goto error;
        }

        backup->oldindex_fname = xstrdup(oldindex_fname);
    }
    else {
        // if there's data in the data file but the index file is empty
        // or doesn't exist, insist on a reindex before opening
        struct stat data_statbuf;
        r = fstat(backup->fd, &data_statbuf);
        if (r) {
            xsyslog(LOG_ERR, "IOERROR: fstat failed",
                             "filename=<%s>", backup->data_fname);
            r = IMAP_IOERROR;
            goto error;
        }
        if (data_statbuf.st_size > 0) {
            struct stat index_statbuf;
            r = stat(backup->index_fname, &index_statbuf);
            if (r && errno != ENOENT) {
                xsyslog(LOG_ERR, "IOERROR: stat failed",
                                 "filename=<%s>", backup->index_fname);
                r = IMAP_IOERROR;
                goto error;
            }

            if ((r && errno == ENOENT) || index_statbuf.st_size == 0) {
                xsyslog(LOG_ERR, "IOERROR: reindex needed",
                                 "filename=<%s>", backup->index_fname);
                r = IMAP_MAILBOX_BADFORMAT;
                goto error;
            }
        }
    }

    backup->db = sqldb_open(backup->index_fname, backup_index_initsql,
                            backup_index_version, backup_index_upgrade,
                            SQLDB_DEFAULT_TIMEOUT);
    if (!backup->db) {
        r = IMAP_INTERNAL; // FIXME what does it mean to error here?
        goto error;
    }

    *backupp = backup;
    return 0;

error:
    backup_close(&backup);
    if (!r) r = IMAP_INTERNAL;
    return r;
}

EXPORTED int backup_open(struct backup **backupp,
                         const mbname_t *mbname,
                         enum backup_open_nonblock nonblock,
                         enum backup_open_create create)
{
    struct buf data_fname = BUF_INITIALIZER;
    struct buf index_fname = BUF_INITIALIZER;

    int r = backup_get_paths(mbname, &data_fname, &index_fname, create);
    /* XXX convert CYRUSDB return code to IMAP */
    if (r) goto done;

    r = backup_real_open(backupp,
                         buf_cstring(&data_fname), buf_cstring(&index_fname),
                         BACKUP_OPEN_NOREINDEX, nonblock, create);
    if (r) goto done;

done:
    buf_free(&data_fname);
    buf_free(&index_fname);

    return r;
}

/* Uses mkstemp() to create a new, unique, backup path for the given user.
 *
 * On success, the file is not unlinked, presuming that it will shortly be
 * used for storing backup data.  This also ensures its uniqueness remains:
 * this function won't generate the same value again as long as the previous
 * file is intact, so there's no user-rename race.
 *
 * If out_fd is non-NULL, on successful return it will contain an open, locked
 * file descriptor for the new file.  In this case the caller must unlock
 * and close the fd.
 *
 * On error, returns NULL and logs to syslog, without touching out_fd.
 */
static const char *_make_path(const mbname_t *mbname, int *out_fd)
{
    static char pathresult[PATH_MAX];

    const char *userid = mbname_userid(mbname);
    const char *partition = partlist_backup_select();
    const char *ret = NULL;

    if (!userid) userid = NOUSERID;

    if (!partition) {
        syslog(LOG_ERR,
               "unable to make backup path for %s: "
               "couldn't select partition",
               userid);
        return NULL;
    }

    char hash_buf[2];
    char *template = strconcat(partition,
                               "/", dir_hash_b(userid, config_fulldirhash, hash_buf),
                               "/", userid, "_XXXXXX",
                               NULL);

    /* make sure the destination directory exists */
    cyrus_mkdir(template, 0755);

    int fd = mkstemp(template);
    if (fd < 0) {
        syslog(LOG_ERR, "unable to make backup path for %s: %m", userid);
        goto error;
    }

    /* lock it -- even if we're just going to immediately unlock it */
    int r = lock_setlock(fd, /*excl*/ 1, /*nb*/ 0, template);
    if (r) {
        syslog(LOG_ERR,
               "unable to obtain exclusive lock on just-created file %s: %m",
               template);
        /* don't unlink it, we don't know what's in it */
        goto error;
    }

    /* save the path */
    if (strlcpy(pathresult, template, sizeof(pathresult)) >= sizeof(pathresult)) {
        syslog(LOG_ERR,
               "unable to make backup path for %s: path too long",
               userid);
        unlink(template);
        goto error;
    }
    ret = pathresult;

    /* save or close the fd */
    if (out_fd)
        *out_fd = fd;
    else
        close(fd);

    free(template);
    return ret;

error:
    if (fd >= 0) close(fd);
    free(template);
    return NULL;
}

EXPORTED int backup_get_paths(const mbname_t *mbname,
                              struct buf *data_fname, struct buf *index_fname,
                              enum backup_open_create create)
{
    struct db *backups_db = NULL;
    struct txn *tid = NULL;

    int r = backupdb_open(&backups_db, &tid);
    if (r) return r;

    const char *userid = mbname_userid(mbname);
    const char *backup_path = NULL;
    size_t path_len = 0;

    if (!userid) userid = NOUSERID;

    r = cyrusdb_fetch(backups_db,
                      userid, strlen(userid),
                      &backup_path, &path_len,
                      &tid);

    if (r == CYRUSDB_NOTFOUND && create) {
        syslog(LOG_DEBUG, "%s not found in backups.db, creating new record", userid);
        backup_path = _make_path(mbname, NULL);
        if (!backup_path) {
            r = CYRUSDB_INTERNAL;
            goto done;
        }
        path_len = strlen(backup_path);

        r = cyrusdb_create(backups_db,
                           userid, strlen(userid),
                           backup_path, path_len,
                           &tid);
        if (r) cyrusdb_abort(backups_db, tid);
        else r = cyrusdb_commit(backups_db, tid);

        tid = NULL;

        /* if we didn't store it in the database successfully, trash the file,
         * it won't be used */
        if (r) unlink(backup_path);
    }

    if (r) goto done;

    if (path_len == 0) {
        syslog(LOG_DEBUG,
               "unexpectedly got zero length backup path for user %s",
               userid);
        r = CYRUSDB_INTERNAL;
        goto done;
    }

    if (data_fname)
        buf_setmap(data_fname, backup_path, path_len);

    if (index_fname) {
        buf_setmap(index_fname, backup_path, path_len);
        buf_appendcstr(index_fname, ".index");
    }

done:
    if (backups_db) {
        if (tid) cyrusdb_abort(backups_db, tid);
        cyrusdb_close(backups_db);
    }
    return r;
}

/*
 * If index_fname is NULL, it will be automatically derived from data_fname
 */
EXPORTED int backup_open_paths(struct backup **backupp,
                               const char *data_fname,
                               const char *index_fname,
                               enum backup_open_nonblock nonblock,
                               enum backup_open_create create)
{
    if (index_fname)
        return backup_real_open(backupp, data_fname, index_fname,
                                BACKUP_OPEN_NOREINDEX, nonblock, create);

    char *tmp = strconcat(data_fname, ".index", NULL);
    int r = backup_real_open(backupp, data_fname, tmp,
                             BACKUP_OPEN_NOREINDEX, nonblock, create);
    free(tmp);

    return r;
}

EXPORTED int backup_close(struct backup **backupp)
{
    struct backup *backup = *backupp;
    *backupp = NULL;

    gzFile gzfile = NULL;
    int r1 = 0, r2 = 0;

    if (!backup) return 0;

    if (backup->append_state) {
        if (backup->append_state->mode != BACKUP_APPEND_INACTIVE)
            r1 = backup_append_end(backup, NULL);

        gzfile = backup->append_state->gzfile;

        free(backup->append_state);
        backup->append_state = NULL;
    }

    if (backup->db) r2 = sqldb_close(&backup->db);

    if (backup->oldindex_fname) {
        if (r2) {
            /* something went wrong closing the new index, put the old one back */
            rename(backup->oldindex_fname, backup->index_fname);
        }
        else {
            if (!config_getswitch(IMAPOPT_BACKUP_KEEP_PREVIOUS)) {
                unlink(backup->oldindex_fname);
            }
        }
    }

    if (backup->fd >= 0) {
        /* closing the file will also release the lock on the fd */
        if (gzfile)
            gzclose_w(gzfile);
        else
            close(backup->fd);
    }

    if (backup->index_fname) free(backup->index_fname);
    if (backup->data_fname) free(backup->data_fname);
    if (backup->oldindex_fname) free(backup->oldindex_fname);

    free(backup);
    return r1 ? r1 : r2;
}

EXPORTED int backup_unlink(struct backup **backupp)
{
    struct backup *backup = *backupp;

    unlink(backup->index_fname);
    unlink(backup->data_fname);

    return backup_close(backupp);
}

EXPORTED const char *backup_get_data_fname(const struct backup *backup)
{
    return backup->data_fname;
}

EXPORTED const char *backup_get_index_fname(const struct backup *backup)
{
    return backup->index_fname;
}

EXPORTED int backup_stat(const struct backup *backup,
                         struct stat *data_statp,
                         struct stat *index_statp)
{
    struct stat data_statbuf, index_statbuf;
    int r;

    r = fstat(backup->fd, &data_statbuf);
    if (r) {
        xsyslog(LOG_ERR, "IOERROR: fstat failed",
                         "filename=<%s>", backup->data_fname);
        return IMAP_IOERROR;
    }

    r = stat(backup->index_fname, &index_statbuf);
    if (r) {
        xsyslog(LOG_ERR, "IOERROR: stat failed",
                         "filename=<%s>", backup->index_fname);
        return IMAP_IOERROR;
    }

    if (data_statp)
        memcpy(data_statp, &data_statbuf, sizeof data_statbuf);
    if (index_statp)
        memcpy(index_statp, &index_statbuf, sizeof index_statbuf);

    return 0;
}

static ssize_t _prot_fill_cb(unsigned char *buf, size_t len, void *rock)
{
    struct gzuncat *gzuc = (struct gzuncat *) rock;
    int r = gzuc_read(gzuc, buf, len);

    if (r < 0)
        xsyslog(LOG_ERR, "IOERROR: gzuc_read failed",
                         "return=<%d>", r);
    if (r < -1)
        errno = EIO;

    return r;
}

EXPORTED int backup_reindex(const char *name,
                            enum backup_open_nonblock nonblock,
                            int verbose, FILE *out)
{
    struct buf data_fname = BUF_INITIALIZER;
    struct buf index_fname = BUF_INITIALIZER;
    struct backup *backup = NULL;
    int r;

    buf_printf(&data_fname, "%s", name);
    buf_printf(&index_fname, "%s.index", name);

    r = backup_real_open(&backup,
                         buf_cstring(&data_fname), buf_cstring(&index_fname),
                         BACKUP_OPEN_REINDEX, nonblock,
                         BACKUP_OPEN_NOCREATE);
    buf_free(&index_fname);
    buf_free(&data_fname);
    if (r) return r;

    struct gzuncat *gzuc = gzuc_new(backup->fd);

    time_t prev_member_ts = -1;

    struct buf cmd = BUF_INITIALIZER;
    while (gzuc && !gzuc_eof(gzuc)) {
        gzuc_member_start(gzuc);
        off_t member_offset = gzuc_member_offset(gzuc);

        if (verbose)
            fprintf(out, "\nfound chunk at offset " OFF_T_FMT "\n\n", member_offset);

        struct protstream *member = prot_readcb(_prot_fill_cb, gzuc);
        prot_setisclient(member, 1); /* don't sync literals */

        // FIXME stricter timestamp sequence checks
        time_t member_start_ts = -1;
        time_t member_end_ts = -1;
        time_t ts = -1;

        while (1) {
            struct dlist *dl = NULL;

            int c = parse_backup_line(member, &ts, &cmd, &dl);
            if (c == EOF) {
                const char *error = prot_error(member);
                if (error && 0 != strcmp(error, PROT_EOF_STRING)) {
                    syslog(LOG_ERR,
                           "IOERROR: %s: error reading chunk at offset " OFF_T_FMT ", byte %i: %s",
                           name, member_offset, prot_bytes_in(member), error);

                    if (out)
                        fprintf(out, "error reading chunk at offset " OFF_T_FMT ", byte %i: %s\n",
                                member_offset, prot_bytes_in(member), error);

                    r = IMAP_IOERROR;
                }
                member_end_ts = ts;
                break;
            }

            if (member_start_ts == -1) {
                if (prev_member_ts != -1 && prev_member_ts > ts) {
                    fatal("member timestamp older than previous", EX_DATAERR);
                }
                member_start_ts = ts;
                char file_sha1[2 * SHA1_DIGEST_LENGTH + 1];
                sha1_file(backup->fd, backup->data_fname, member_offset, file_sha1);
                backup_real_append_start(backup, member_start_ts,
                                         member_offset, file_sha1, 1, 0);
            }
            else if (member_start_ts > ts)
                fatal("line timestamp older than previous", EX_DATAERR);

            if (strcmp(buf_cstring(&cmd), "APPLY") != 0) {
                dlist_unlink_files(dl);
                dlist_free(&dl);
                continue;
            }

            ucase(dl->name);

            r = backup_append(backup, dl, &ts, BACKUP_APPEND_NOFLUSH);
            if (r) {
                // FIXME do something
                syslog(LOG_ERR, "backup_append returned %d", r);
                fprintf(out, "backup_append returned %d\n", r);
            }

            dlist_unlink_files(dl);
            dlist_free(&dl);
        }

        if (backup->append_state && backup->append_state->mode)
            backup_real_append_end(backup, member_end_ts);
        prot_free(member);
        gzuc_member_end(gzuc, NULL);

        prev_member_ts = member_start_ts;
    }
    buf_free(&cmd);

    if (verbose)
        fprintf(out, "reached end of file\n");

    gzuc_free(&gzuc);
    backup_close(&backup);

    return r;
}

struct _rename_meta {
    const char *userid;
    char *fname;
    char *ext_ptr;
    int fd;
};
#define RENAME_META_INITIALIZER { NULL, NULL, NULL, -1 }

static void _rename_meta_set_fname(struct _rename_meta *meta, const char *data_fname)
{
    size_t len = strlen(data_fname) + strlen(".index") + 1;
    meta->fname = xmalloc(len);
    snprintf(meta->fname, len, "%s.index", data_fname);
    meta->ext_ptr = strrchr(meta->fname, '.');
    *meta->ext_ptr = '\0';
}

static void _rename_meta_fini(struct _rename_meta *meta)
{
    if (meta->fname) free(meta->fname);
    memset(meta, 0, sizeof(*meta));
    meta->fd = -1;
}

EXPORTED int backup_rename(const mbname_t *old_mbname, const mbname_t *new_mbname)
{
    struct db *backups_db = NULL;
    struct txn *tid = NULL;
    struct _rename_meta old = RENAME_META_INITIALIZER;
    struct _rename_meta new = RENAME_META_INITIALIZER;
    old.userid = mbname_userid(old_mbname);
    new.userid = mbname_userid(new_mbname);
    const char *path;
    size_t path_len;
    int r;

    if (!old.userid) old.userid = NOUSERID;
    if (!new.userid) new.userid = NOUSERID;

    /* bail out if the names are the same */
    if (strcmp(old.userid, new.userid) == 0)
        return 0;

    /* exclusively open backups database */
    r = backupdb_open(&backups_db, &tid);
    if (r) goto error; // FIXME log

    /* make sure new_mbname isn't already in use */
    r = cyrusdb_fetch(backups_db,
                      new.userid, strlen(new.userid),
                      &path, &path_len,
                      &tid);
    if (!r) r = CYRUSDB_EXISTS;
    if (r) goto error;  // FIXME log

    /* locate (but not create) backup for old_mbname, open and lock it */
    r = cyrusdb_fetch(backups_db,
                      old.userid, strlen(old.userid),
                      &path, &path_len,
                      &tid);
    if (r) goto error;  // FIXME log

    _rename_meta_set_fname(&old, path);

    old.fd = open(old.fname,
                  O_RDWR | O_APPEND, /* no O_CREAT */
                  S_IRUSR | S_IWUSR);
    if (old.fd < 0) {
        xsyslog(LOG_ERR, "IOERROR: open failed",
                         "filename=<%s>", old.fname);
        r = -1;
        goto error;
    }

    /* non-blocking, to avoid deadlock */
    r = lock_setlock(old.fd, /*excl*/ 1, /*nb*/ 1, old.fname);
    if (r) {
        xsyslog(LOG_ERR, "IOERROR: lock_setlock failed",
                         "filename=<%s>", old.fname);
        goto error;
    }

    /* make a path for new_mbname, open and lock it */
    path = _make_path(new_mbname, &new.fd);
    if (!path) goto error; // FIXME log
    _rename_meta_set_fname(&new, path);

    /* copy old data and index files to new paths */
    r = cyrus_copyfile(old.fname, new.fname, 0);
    if (r) goto error; // FIXME log
    *old.ext_ptr = *new.ext_ptr = '.';
    r = cyrus_copyfile(old.fname, new.fname, 0);
    *old.ext_ptr = *new.ext_ptr = '\0';
    if (r) goto error; // FIXME log

    /* files exist under both names now. try to update the database */
    r = cyrusdb_create(backups_db,
                       new.userid, strlen(new.userid),
                       new.fname, strlen(new.fname),
                       &tid);
    if (r) goto error; // FIXME log

    r = cyrusdb_delete(backups_db,
                       old.userid, strlen(old.userid),
                       &tid, 0);
    if (r) goto error; // FIXME log

    r = cyrusdb_commit(backups_db, tid);
    tid = NULL;
    if (r) goto error; // FIXME log

    /* database update succeeded. unlink old names */
    unlink(old.fname);
    *old.ext_ptr = '.';
    unlink(old.fname);
    *old.ext_ptr = '\0';

    /* unlock and close backup files */
    lock_unlock(new.fd, new.fname);
    close(new.fd);
    lock_unlock(old.fd, old.fname);
    close(old.fd);

    /* close backups database */
    cyrusdb_close(backups_db);

    /* clean up and exit */
    _rename_meta_fini(&old);
    _rename_meta_fini(&new);
    return 0;

error:
    /* we didn't finish, so unlink the new filenames if we got that far */
    if (new.fname) {
        unlink(new.fname);
        *new.ext_ptr = '.';
        unlink(new.fname);
        *new.ext_ptr = '\0';
    }

    /* close the files if we got that far (also unlocks) */
    if (new.fd != -1)
        close(new.fd);
    if (old.fd != -1)
        close(old.fd);

    /* abort any transaction and close the database */
    if (backups_db) {
        if (tid) cyrusdb_abort(backups_db, tid);
        cyrusdb_close(backups_db);
    }

    /* clean up and exit */
    _rename_meta_fini(&old);
    _rename_meta_fini(&new);
    return r;
}