-
Notifications
You must be signed in to change notification settings - Fork 154
/
ctl_backups.c
1119 lines (957 loc) · 35.1 KB
/
ctl_backups.c
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ctl_backups.c -- tool for managing replication-based backup files
*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <jansson.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>
#include <unistd.h>
#include "lib/cyrusdb.h"
#include "lib/util.h"
#include "lib/xmalloc.h"
#include "imap/global.h"
#include "imap/imap_err.h"
#include "backup/backup.h"
static struct namespace ctl_backups_namespace;
EXPORTED void fatal(const char *error, int code)
{
fprintf(stderr, "fatal error: %s\n", error);
cyrus_done();
exit(code);
}
static const char *argv0 = NULL;
static void usage(void)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s [options] compact [mode] backup...\n", argv0);
fprintf(stderr, " %s [options] list [list_opts] [[mode] backup...]\n", argv0);
fprintf(stderr, " %s [options] lock [lock_opts] [mode] backup\n", argv0);
fprintf(stderr, " %s [options] reindex [mode] backup...\n", argv0);
fprintf(stderr, " %s [options] stat [mode] backup...\n", argv0);
fprintf(stderr, " %s [options] verify [mode] backup...\n", argv0);
fprintf(stderr, "\n%s\n",
"Commands:\n"
" compact # compact specified backups\n"
" list [list_opts] # list backups (all if none specified)\n"
" lock [lock_opts] # lock specified backup\n"
" reindex # reindex specified backups\n"
" stat # show stats of specified backups\n"
" verify # verify specified backups\n"
);
fprintf(stderr, "%s\n",
"Options:\n"
" -C alt_config # alternate config file\n"
" -F # force (run command even if not needed)\n"
" -S # stop on error\n"
" -V # don't verify checksums (faster read-only ops)\n"
" -j # output in JSON format\n"
" -v # verbose (repeat for more verbosity)\n"
" -w # wait for locks (don't skip locked backups)\n"
);
fprintf(stderr, "%s\n",
"List options:\n"
" -t [hours] # stale (no update in hours) backups only (default: 24)\n"
);
fprintf(stderr, "%s\n",
"Lock options:\n"
" -c # exclusively create backup\n"
" -p # lock backup and wait for eof on stdin (default)\n"
" -s # lock backup and open index in sqlite3\n"
" -x command # lock backup and execute command\n"
);
fprintf(stderr, "%s\n",
"Modes:\n"
" -A # all known backups\n"
" -D # specified backups interpreted as domains\n"
" -P # specified backups interpreted as userid prefixes\n"
" -f # specified backups interpreted as filenames\n"
" -m # specified backups interpreted as mboxnames\n"
" -u # specified backups interpreted as userids (default)\n"
"\n"
" Modes -A, -D, -P not available for all commands\n" /* FIXME which */
);
exit(EX_USAGE);
}
enum ctlbu_mode {
CTLBU_MODE_UNSPECIFIED = 0,
CTLBU_MODE_FILENAME,
CTLBU_MODE_MBOXNAME,
CTLBU_MODE_USERNAME,
CTLBU_MODE_DOMAIN,
CTLBU_MODE_PREFIX,
CTLBU_MODE_ALL,
};
enum ctlbu_lock_mode {
CTLBU_LOCK_MODE_UNSPECIFIED = 0,
CTLBU_LOCK_MODE_PIPE,
CTLBU_LOCK_MODE_SQL,
CTLBU_LOCK_MODE_EXEC,
};
struct ctlbu_cmd_options {
enum ctlbu_mode mode;
enum ctlbu_lock_mode lock_mode;
enum backup_open_nonblock wait;
enum backup_open_create create;
int verbose;
int stop_on_error;
int list_stale;
int force;
int noverify;
int jsonout;
const char *lock_exec_cmd;
const char *domain;
};
enum ctlbu_cmd {
CTLBU_CMD_UNSPECIFIED = 0,
CTLBU_CMD_COMPACT,
CTLBU_CMD_DELETE,
CTLBU_CMD_LIST,
CTLBU_CMD_LOCK,
CTLBU_CMD_MOVE,
CTLBU_CMD_RECONSTRUCT,
CTLBU_CMD_REINDEX,
CTLBU_CMD_STAT,
CTLBU_CMD_VERIFY,
};
static int ctlbu_skips_fails = 0;
/* same signature as foreach_cb */
static int cmd_compact_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_delete_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_list_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_lock_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_move_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_reindex_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_stat_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static int cmd_verify_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len);
static foreach_cb *const cmd_func[] = {
NULL,
cmd_compact_one,
cmd_delete_one,
cmd_list_one,
cmd_lock_one,
cmd_move_one,
NULL, /* reconstruct one doesn't make sense */
cmd_reindex_one,
cmd_stat_one,
cmd_verify_one,
};
static int lock_run_pipe(const char *userid, const char *fname,
enum backup_open_nonblock nonblock,
enum backup_open_create create);
static int lock_run_sqlite(const char *userid, const char *fname,
enum backup_open_nonblock nonblock,
enum backup_open_create create);
static int lock_run_exec(const char *userid, const char *fname,
const char *cmd,
enum backup_open_nonblock nonblock,
enum backup_open_create create);
static enum ctlbu_cmd parse_cmd_string(const char *cmd)
{
assert(cmd != NULL);
switch(cmd[0]) {
case 'c':
if (strcmp(cmd, "compact") == 0) return CTLBU_CMD_COMPACT;
break;
case 'd':
if (strcmp(cmd, "delete") == 0) return CTLBU_CMD_DELETE;
break;
case 'l':
if (strcmp(cmd, "list") == 0) return CTLBU_CMD_LIST;
if (strcmp(cmd, "lock") == 0) return CTLBU_CMD_LOCK;
break;
case 'm':
if (strcmp(cmd, "move") == 0) return CTLBU_CMD_MOVE;
break;
case 'r':
if (strcmp(cmd, "reconstruct") == 0) return CTLBU_CMD_RECONSTRUCT;
if (strcmp(cmd, "reindex") == 0) return CTLBU_CMD_REINDEX;
break;
case 's':
if (strcmp(cmd, "stat") == 0) return CTLBU_CMD_STAT;
break;
case 'v':
if (strcmp(cmd, "verify") == 0) return CTLBU_CMD_VERIFY;
break;
};
return CTLBU_CMD_UNSPECIFIED;
}
static void print_status(const char *cmd,
const char *userid, const char *fname,
const struct ctlbu_cmd_options *options,
int r)
{
char *status = NULL;
switch (r) {
case 1:
status = xstrdup("skipped");
break;
case 0:
status = xstrdup("ok");
break;
case IMAP_MAILBOX_LOCKED:
status = xstrdup("locked");
break;
default:
status = strconcat("failed (", error_message(r), ")", NULL);
break;
}
if (options->jsonout) {
json_t *out = json_object();
json_object_set_new(out, "command", json_string(cmd));
json_object_set_new(out, "userid", json_string(userid));
json_object_set_new(out, "fname", json_string(fname));
json_object_set_new(out, "status", json_string(status));
const size_t flags = JSON_INDENT(2) | JSON_PRESERVE_ORDER;
json_dumpf(out, stdout, flags);
puts("");
json_decref(out);
}
else {
printf("%s %s: %s\n", cmd, userid ? userid : fname, status);
}
free(status);
}
static int domain_filter(void *rock,
const char *key, size_t key_len,
const char *data __attribute__((unused)),
size_t data_len __attribute__((unused)))
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
char *userid = NULL;
mbname_t *mbname = NULL;
const char *domain = NULL;
int doit = 0;
/* input args might not be 0-terminated, so make a safe copy */
if (!key_len) return 0;
userid = xstrndup(key, key_len);
mbname = mbname_from_userid(userid);
domain = mbname_domain(mbname);
if (!domain)
domain = config_defdomain;
if (domain)
doit = !strcmp(domain, options->domain);
mbname_free(&mbname);
free(userid);
return doit;
}
static void save_argv0(const char *s)
{
const char *slash = strrchr(s, '/');
if (slash)
argv0 = slash + 1;
else
argv0 = s;
}
int main(int argc, char **argv)
{
save_argv0(argv[0]);
int opt, r = 0;
const char *alt_config = NULL;
enum ctlbu_cmd cmd = CTLBU_CMD_UNSPECIFIED;
struct ctlbu_cmd_options options = {0};
options.wait = BACKUP_OPEN_NONBLOCK;
/* keep in alphabetical order */
static const char short_options[] = ":AC:DFPSVcfjmpst:uvwx:";
static const struct option long_options[] = {
{ "all", no_argument, NULL, 'A' },
/* n.b. no long-option for -C */
{ "domains", no_argument, NULL, 'D' },
{ "force", no_argument, NULL, 'F' },
{ "prefixes", no_argument, NULL, 'P' },
{ "stop-on-error", no_argument, NULL, 'S' },
{ "no-verify", no_argument, NULL, 'V' },
{ "create", no_argument, NULL, 'c' },
{ "filenames", no_argument, NULL, 'f' },
{ "json", no_argument, NULL, 'j' },
{ "mailboxes", no_argument, NULL, 'm' },
{ "pause", no_argument, NULL, 'p' },
{ "sqlite3", no_argument, NULL, 's' },
{ "stale", optional_argument, NULL, 't' },
{ "userids", no_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ "wait-for-locks", no_argument, NULL, 'w' },
{ "execute", required_argument, NULL, 'x' },
{ 0, 0, 0, 0 },
};
while (-1 != (opt = getopt_long(argc, argv,
short_options, long_options, NULL)))
{
switch (opt) {
case 'A':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_ALL;
break;
case 'C':
alt_config = optarg;
break;
case 'D':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_DOMAIN;
break;
case 'F':
options.force = 1;
break;
case 'P':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_PREFIX;
break;
case 'S':
options.stop_on_error = 1;
break;
case 'V':
options.noverify = 1;
break;
case 'c':
options.create = BACKUP_OPEN_CREATE_EXCL;
break;
case 'j':
if (options.verbose) usage();
options.jsonout = 1;
break;
case 'f':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_FILENAME;
break;
case 'm':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_MBOXNAME;
break;
case 'p':
if (options.lock_mode != CTLBU_LOCK_MODE_UNSPECIFIED) usage();
options.lock_mode = CTLBU_LOCK_MODE_PIPE;
break;
case 's':
if (options.lock_mode != CTLBU_LOCK_MODE_UNSPECIFIED) usage();
options.lock_mode = CTLBU_LOCK_MODE_SQL;
break;
case 't':
options.list_stale = atoi(optarg);
if (!options.list_stale) usage();
break;
case 'u':
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
options.mode = CTLBU_MODE_USERNAME;
break;
case 'v':
if (options.jsonout) usage();
options.verbose ++;
break;
case 'x':
if (options.lock_mode != CTLBU_LOCK_MODE_UNSPECIFIED) usage();
options.lock_mode = CTLBU_LOCK_MODE_EXEC;
options.lock_exec_cmd = optarg;
break;
case 'w':
options.wait = BACKUP_OPEN_BLOCK;
break;
case ':':
if (optopt == 't') options.list_stale = 24;
else usage();
break;
default:
usage();
break;
}
}
/* get the command */
if (optind == argc) usage();
cmd = parse_cmd_string(argv[optind++]);
if (cmd == CTLBU_CMD_UNSPECIFIED) usage();
if (options.lock_mode != CTLBU_LOCK_MODE_UNSPECIFIED
&& cmd != CTLBU_CMD_LOCK)
usage();
switch (cmd) {
/* list defaults to all */
case CTLBU_CMD_LIST:
if (options.mode == CTLBU_MODE_UNSPECIFIED && argc - optind == 0)
options.mode = CTLBU_MODE_ALL;
break;
/* some commands only accept one backup at a time */
case CTLBU_CMD_LOCK:
case CTLBU_CMD_MOVE:
case CTLBU_CMD_DELETE:
if (options.mode == CTLBU_MODE_ALL) usage();
if (options.mode == CTLBU_MODE_DOMAIN) usage();
if (options.mode == CTLBU_MODE_PREFIX) usage();
if (argc - optind > 1) usage();
break;
/* reconstruct doesn't accept named backups */
case CTLBU_CMD_RECONSTRUCT:
if (options.mode != CTLBU_MODE_UNSPECIFIED) usage();
if (optind != argc) usage();
break;
default:
break;
}
/* default mode is username */
if (options.mode == CTLBU_MODE_UNSPECIFIED)
options.mode = CTLBU_MODE_USERNAME;
/* mode all doesn't want any named backups */
if (options.mode == CTLBU_MODE_ALL && optind != argc) usage();
cyrus_init(alt_config, "ctl_backups", 0, 0);
if ((r = mboxname_init_namespace(&ctl_backups_namespace, NAMESPACE_OPTION_ADMIN))) {
fatal(error_message(r), EX_CONFIG);
}
mboxevent_setnamespace(&ctl_backups_namespace);
if (cmd == CTLBU_CMD_RECONSTRUCT) {
/* special handling for reconstruct */
// FIXME
}
else if (options.mode == CTLBU_MODE_ALL) {
/* loop over entire backups.db */
struct db *backups_db = NULL;
r = backupdb_open(&backups_db, NULL);
if (!r)
r = cyrusdb_foreach(backups_db, NULL, 0, NULL,
cmd_func[cmd], &options,
NULL);
if (backups_db)
cyrusdb_close(backups_db);
}
else if (options.mode == CTLBU_MODE_DOMAIN) {
/* loop over domains named on command line */
struct db *backups_db = NULL;
int i;
r = backupdb_open(&backups_db, NULL);
for (i = optind; i < argc && !r; i++) {
options.domain = argv[i];
r = cyrusdb_foreach(backups_db, NULL, 0,
domain_filter,
cmd_func[cmd], &options,
NULL);
}
if (backups_db)
cyrusdb_close(backups_db);
}
else if (options.mode == CTLBU_MODE_PREFIX) {
/* loop over prefixes named on command line */
struct db *backups_db = NULL;
int i;
r = backupdb_open(&backups_db, NULL);
for (i = optind; i < argc && !r; i++) {
r = cyrusdb_foreach(backups_db,
argv[i], strlen(argv[i]),
NULL,
cmd_func[cmd], &options,
NULL);
}
if (backups_db)
cyrusdb_close(backups_db);
}
else {
/* loop over backups named on command line */
struct buf userid = BUF_INITIALIZER;
struct buf fname = BUF_INITIALIZER;
int i;
for (i = optind; i < argc; i++) {
buf_reset(&userid);
buf_reset(&fname);
mbname_t *mbname = NULL;
if (options.mode == CTLBU_MODE_USERNAME)
mbname = mbname_from_userid(argv[i]);
else if (options.mode == CTLBU_MODE_MBOXNAME)
mbname = mbname_from_extname(argv[i], &ctl_backups_namespace, NULL);
if (mbname) {
r = backup_get_paths(mbname, &fname, NULL, BACKUP_OPEN_NOCREATE);
if (r) {
/* XXX this should print cyrusdb_strerror(r), except that
* just returns "cyrusdb error", unhelpfully */
fprintf(stderr, "%s: not found in backups.db\n", argv[i]);
syslog(LOG_DEBUG, "ctl_backups '%s': %s (%d)", argv[i], cyrusdb_strerror(r), r);
mbname_free(&mbname);
continue;
}
if (mbname_userid(mbname)) {
buf_setcstr(&userid, mbname_userid(mbname));
}
}
else
buf_setcstr(&fname, argv[i]);
if (!r && cmd_func[cmd])
r = cmd_func[cmd](&options,
buf_cstring(&userid),
buf_len(&userid),
buf_cstring(&fname),
buf_len(&fname));
if (mbname) mbname_free(&mbname);
if (r) break;
}
buf_free(&userid);
buf_free(&fname);
}
backup_cleanup_staging_path();
cyrus_done();
exit(r || ctlbu_skips_fails ? EX_TEMPFAIL : EX_OK);
}
static int cmd_compact_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
char *userid = NULL;
char *fname = NULL;
int r = 0;
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
r = backup_compact(fname, options->wait, options->force,
options->verbose, stdout);
print_status("compact", userid, fname, options, r);
if (userid) free(userid);
if (fname) free(fname);
if (r) ++ctlbu_skips_fails;
if (r == IMAP_MAILBOX_LOCKED) r = 0;
return options->stop_on_error ? r : 0;
}
static int cmd_delete_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
(void) options;
fprintf(stderr, "unimplemented: %s %s[%zu] %s[%zu]\n", __func__,
userid, userid_len, fname, fname_len);
return -1;
}
static int cmd_list_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
struct backup *backup = NULL;
struct backup_chunk *latest_chunk = NULL;
struct stat data_stat_buf = {0};
char *userid = NULL;
char *fname = NULL;
char timestamp[32] = "[unknown]";
int r = 0;
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
r = backup_open_paths(&backup, fname, NULL,
options->wait, BACKUP_OPEN_NOCREATE);
if (!r && !options->noverify)
r = backup_verify(backup, BACKUP_VERIFY_QUICK, 0, NULL);
if (r) {
fprintf(stderr, "%s: %s\n", userid ? userid : fname, error_message(r));
goto done;
}
latest_chunk = backup_get_latest_chunk(backup);
if (latest_chunk) {
if (options->list_stale) {
/* skip out early if it's not stale */
if (time(NULL) - latest_chunk->ts_end < 3600 * options->list_stale)
goto done;
}
strftime(timestamp, sizeof(timestamp), "%F %T",
localtime(&latest_chunk->ts_end));
}
r = backup_stat(backup, &data_stat_buf, NULL);
if (r) {
fprintf(stderr, "fstat %s: %s\n", userid ? userid : fname, strerror(errno));
data_stat_buf.st_size = -1;
}
if (options->jsonout) {
json_t *out = json_object();
json_object_set_new(out, "timestamp", json_string(timestamp));
json_object_set_new(out, "compressed", json_integer(data_stat_buf.st_size));
json_object_set_new(out, "userid", json_string(userid));
json_object_set_new(out, "fname", json_string(fname));
const size_t flags = JSON_INDENT(2) | JSON_PRESERVE_ORDER;
json_dumpf(out, stdout, flags);
puts("");
json_decref(out);
}
else {
printf("%s\t" OFF_T_FMT "\t%s\t%s\n",
timestamp,
data_stat_buf.st_size,
userid ? userid : "[unknown]",
fname);
}
done:
if (latest_chunk) backup_chunk_free(&latest_chunk);
if (backup) backup_close(&backup);
if (userid) free(userid);
if (fname) free(fname);
if (r) ++ctlbu_skips_fails;
if (r == IMAP_MAILBOX_LOCKED) r = 0;
return options->stop_on_error ? r : 0;
}
static int cmd_lock_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
char *userid = NULL;
char *fname = NULL;
int r = 0;
assert(data != NULL && data_len > 0);
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
switch (options->lock_mode) {
case CTLBU_LOCK_MODE_UNSPECIFIED:
case CTLBU_LOCK_MODE_PIPE:
r = lock_run_pipe(userid, fname, options->wait, options->create);
break;
case CTLBU_LOCK_MODE_SQL:
r = lock_run_sqlite(userid, fname, options->wait, options->create);
break;
case CTLBU_LOCK_MODE_EXEC:
r = lock_run_exec(userid, fname, options->lock_exec_cmd,
options->wait, options->create);
break;
}
if (userid) free(userid);
if (fname) free(fname);
/* don't care about stop_on_error: lock command only accepts one backup */
if (r) ++ctlbu_skips_fails;
return r;
}
static int cmd_move_one(void *rock,
const char *userid, size_t userid_len,
const char *fname, size_t fname_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
(void) options;
fprintf(stderr, "unimplemented: %s %s[%zu] %s[%zu]\n", __func__,
userid, userid_len, fname, fname_len);
return -1;
}
static int cmd_reindex_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
char *userid = NULL;
char *fname = NULL;
int r;
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
r = backup_reindex(fname, options->wait, options->verbose, stdout);
print_status("reindex", userid, fname, options, r);
if (userid) free(userid);
if (fname) free(fname);
if (r) ++ctlbu_skips_fails;
if (r == IMAP_MAILBOX_LOCKED) r = 0;
return options->stop_on_error ? r : 0;
}
static int _sum_message_lengths_cb(const struct backup_message *message,
void *rock)
{
size_t *sum = (size_t *) rock;
*sum += message->length;
return 0;
}
static int cmd_stat_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
struct backup *backup = NULL;
char *userid = NULL;
char *fname = NULL;
struct backup_chunk_list *all_chunks = NULL, *keep_chunks = NULL;
struct backup_chunk *latest_chunk = NULL;
struct backup_chunk *chunk;
time_t since;
size_t compactable = 0, uncompressed = 0;
struct stat data_stat;
double cmp_ratio, utl_ratio;
char start_time[32] = "[unknown]";
char end_time[32] = "[unknown]";
int r;
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
r = backup_open_paths(&backup, fname, NULL,
options->wait, BACKUP_OPEN_NOCREATE);
if (r) goto done;
if (!options->noverify) {
r = backup_verify(backup, BACKUP_VERIFY_QUICK, 0, NULL);
if (r) goto done;
}
const int retention = config_getduration(IMAPOPT_BACKUP_RETENTION, 'd');
if (retention > 0) {
since = time(0) - retention;
}
else {
/* zero or negative retention means "keep forever" */
since = -1;
}
all_chunks = backup_get_chunks(backup);
keep_chunks = backup_get_live_chunks(backup, since);
if (!all_chunks || !keep_chunks) {
r = IMAP_INTERNAL;
goto done;
}
/* uncompressed length is sum of lengths of all chunks */
for (chunk = all_chunks->head; chunk; chunk = chunk->next) {
uncompressed += chunk->length;
}
/* compactable length is sum of live message lengths in keep chunks */
for (chunk = keep_chunks->head; chunk; chunk = chunk->next) {
backup_message_foreach(backup, chunk->id, &since,
_sum_message_lengths_cb, &compactable);
}
/* compression ratio is compressed length / uncompressed length */
r = backup_stat(backup, &data_stat, NULL);
if (r) goto done;
cmp_ratio = 100.0 * data_stat.st_size / uncompressed;
/* utilisation ratio is compactable length / uncompressed length */
utl_ratio = 100.0 * compactable / uncompressed;
/* start/end time are from latest chunk */
latest_chunk = backup_get_latest_chunk(backup);
if (latest_chunk) {
strftime(start_time, sizeof(start_time), "%F %T",
localtime(&latest_chunk->ts_start));
strftime(end_time, sizeof(end_time), "%F %T",
localtime(&latest_chunk->ts_end));
}
if (options->jsonout) {
json_t *out = json_object();
json_object_set_new(out, "userid", json_string(userid));
json_object_set_new(out, "fname", json_string(fname));
json_object_set_new(out, "compressed", json_integer(data_stat.st_size));
json_object_set_new(out, "uncompressed", json_integer(uncompressed));
json_object_set_new(out, "compactable", json_integer(compactable));
json_object_set_new(out, "compression_ratio", json_real(cmp_ratio));
json_object_set_new(out, "utilisation_ratio", json_real(utl_ratio));
json_object_set_new(out, "last_start_time", json_string(start_time));
json_object_set_new(out, "last_end_time", json_string(end_time));
const size_t flags = JSON_INDENT(2) | JSON_PRESERVE_ORDER;
json_dumpf(out, stdout, flags);
puts("");
json_decref(out);
}
else {
printf("%s\t" OFF_T_FMT "\t" SIZE_T_FMT "\t" SIZE_T_FMT "\t%6.1f%%\t%6.1f%%\t%21s\t%s\n",
userid ? userid : fname,
data_stat.st_size,
uncompressed,
compactable,
cmp_ratio,
utl_ratio,
start_time,
end_time);
}
done:
if (r) {
fprintf(stderr, "%s: %s\n", userid ? userid : fname, error_message(r));
}
if (latest_chunk) backup_chunk_free(&latest_chunk);
if (all_chunks) backup_chunk_list_free(&all_chunks);
if (keep_chunks) backup_chunk_list_free(&keep_chunks);
if (backup) backup_close(&backup);
if (fname) free(fname);
if (userid) free(userid);
return options->stop_on_error ? r : 0;
}
static int cmd_verify_one(void *rock,
const char *key, size_t key_len,
const char *data, size_t data_len)
{
struct ctlbu_cmd_options *options = (struct ctlbu_cmd_options *) rock;
struct backup *backup = NULL;
char *userid = NULL;
char *fname = NULL;
int r;
/* input args might not be 0-terminated, so make a safe copy */
if (key_len)
userid = xstrndup(key, key_len);
if (data_len)
fname = xstrndup(data, data_len);
r = backup_open_paths(&backup, fname, NULL,
options->wait, BACKUP_OPEN_NOCREATE);
/* n.b. deliberately ignoring nonsensical noverify option here */
if (!r) r = backup_verify(backup, BACKUP_VERIFY_FULL, options->verbose, stdout);
print_status("verify", userid, fname, options, r);
if (backup) backup_close(&backup);
if (userid) free(userid);
if (fname) free(fname);
if (r) ++ctlbu_skips_fails;
if (r == IMAP_MAILBOX_LOCKED) r = 0;
return options->stop_on_error ? r : 0;
}
static int lock_run_pipe(const char *userid, const char *fname,
enum backup_open_nonblock nonblock,
enum backup_open_create create)
{
printf("* Trying to obtain lock on %s...\n", userid ? userid : fname);
struct backup *backup = NULL;
int r;
r = backup_open_paths(&backup, fname, NULL, nonblock, create);
if (!r)
r = backup_verify(backup, BACKUP_VERIFY_QUICK, 0, NULL);
if (r) {
printf("NO failed (%s)\n", error_message(r));
r = backup_close(&backup);
return EX_SOFTWARE; // FIXME would something else be more appropriate?
}