contrib/ksmtpproxy: Fix typo
[navymail.git] / export.c
blob6e29d24a1099633005cbffd68f8dc5f26cf082cf
1 /* export mbox from navymail store
2 * Copyright (C) 2011 Kirill Smelkov <kirr@navytux.spb.ru>
4 * This program is free software: you can Use, Study, Modify and Redistribute it
5 * under the terms of the GNU General Public License version 2. This program is
6 * distributed WITHOUT ANY WARRANTY. See COPYING file for full License terms.
9 * NOTE by default we export messages in reverse chronological order, because it
10 * is faster, and because it is what people are usually interested in.
12 * messages can be exported in original order with --original-order.
15 #include "git-compat-util.h"
16 #include "parse-options.h"
17 #include "strbuf.h"
18 #include "navymail/navymail.h"
19 #include "navymail/gbox-walk.h"
21 /* XXX better naming for gbox */
22 static const char * const export_usage[] = {
23 "navymail export [--original-order] <gbox>",
24 NULL
27 static int original_order;
29 static const struct option export_options[] = {
30 OPT_BOOLEAN( 0, "original-order", &original_order, "export messages in original order"),
31 OPT_END()
35 static void cat_blob(const unsigned char *blob_sha1, const unsigned char *tree_sha1)
37 enum object_type type;
38 void *buf;
39 unsigned long size;
41 buf = read_sha1_file(blob_sha1, &type, &size);
42 if (!buf)
43 die("navymail export: corrupt tree %s", sha1_to_hex(tree_sha1));
44 if (type != OBJ_BLOB)
45 die("navymail export: corrupt tree %s (got %s instead of %s)",
46 sha1_to_hex(tree_sha1),
47 typename(type), typename(OBJ_BLOB));
49 write_or_die(1, buf, size);
50 free(buf);
53 static int export_gbox(const char *gbox)
55 struct gbox_walk gbox_walk = { .gbox = gbox, .original_order = original_order };
56 const unsigned char *sha1;
58 prepare_gbox_walk(&gbox_walk);
60 while ((sha1 = next_gbox_entry(&gbox_walk)))
61 cat_blob(sha1, gbox_walk.tree->object.sha1);
63 return 0;
67 int cmd_export(int argc, const char **argv, const char *prefix)
69 const char *gbox;
70 struct strbuf gbox_ref = STRBUF_INIT;
71 int ret;
73 argc = parse_options(argc, argv, NULL /*prefix?*/, export_options, export_usage, 0);
75 /* git_config(git_default_config, NULL); ? */
77 if (argc != 1)
78 usage_with_options(export_usage, export_options);
80 gbox = argv[0];
81 strbuf_addf(&gbox_ref, "refs/mail/%s", gbox); /* XXX don't hardcode mail/ here */
82 ret = export_gbox(gbox_ref.buf); /* XXX or pass strbuf directly? */
83 strbuf_release(&gbox_ref);
84 return ret;