2 * Totally braindamaged mbox splitter program.
4 * It just splits a mbox into a list of files: "0001" "0002" ..
5 * so you can process them further from there.
9 #include "string-list.h"
12 static const char git_mailsplit_usage[] =
13 "git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
15 static int is_from_line(const char *line, int len)
19 if (len < 20 || memcmp("From ", line, 5))
22 colon = line + len - 2;
31 if (!isdigit(colon[-4]) ||
32 !isdigit(colon[-2]) ||
33 !isdigit(colon[-1]) ||
34 !isdigit(colon[ 1]) ||
39 if (strtol(colon+3, NULL, 10) <= 90)
42 /* Ok, close enough */
46 static struct strbuf buf = STRBUF_INIT;
49 /* Called with the first line (potentially partial)
50 * already in buf[] -- normally that should begin with
51 * the Unix "From " line. Write it into the specified
54 static int split_one(FILE *mbox, const char *name, int allow_bare)
59 int is_bare = !is_from_line(buf.buf, buf.len);
61 if (is_bare && !allow_bare) {
62 fprintf(stderr, "corrupt mailbox\n");
65 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
67 die_errno("cannot open output file '%s'", name);
68 output = xfdopen(fd, "w");
70 /* Copy it out, while searching for a line that begins with
71 * "From " and having something that looks like a date format.
74 if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
75 buf.buf[buf.len-2] == '\r') {
76 strbuf_setlen(&buf, buf.len-2);
77 strbuf_addch(&buf, '\n');
80 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
81 die_errno("cannot write output");
83 if (strbuf_getwholeline(&buf, mbox, '\n')) {
88 die_errno("cannot read mbox");
90 if (!is_bare && is_from_line(buf.buf, buf.len))
91 break; /* done with one message */
97 static int populate_maildir_list(struct string_list *list, const char *path)
102 char *subs[] = { "cur", "new", NULL };
105 for (sub = subs; *sub; ++sub) {
106 snprintf(name, sizeof(name), "%s/%s", path, *sub);
107 if ((dir = opendir(name)) == NULL) {
110 error("cannot opendir %s (%s)", name, strerror(errno));
114 while ((dent = readdir(dir)) != NULL) {
115 if (dent->d_name[0] == '.')
117 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
118 string_list_insert(list, name);
127 static int maildir_filename_cmp(const char *a, const char *b)
130 if (isdigit(*a) && isdigit(*b)) {
132 na = strtol(a, (char **)&a, 10);
133 nb = strtol(b, (char **)&b, 10);
136 /* strtol advanced our pointers */
140 return (unsigned char)*a - (unsigned char)*b;
145 return (unsigned char)*a - (unsigned char)*b;
148 static int split_maildir(const char *maildir, const char *dir,
149 int nr_prec, int skip)
155 struct string_list list = STRING_LIST_INIT_DUP;
157 list.cmp = maildir_filename_cmp;
159 if (populate_maildir_list(&list, maildir) < 0)
162 for (i = 0; i < list.nr; i++) {
164 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
165 f = fopen(file, "r");
167 error("cannot open mail %s (%s)", file, strerror(errno));
171 if (strbuf_getwholeline(&buf, f, '\n')) {
172 error("cannot read mail %s (%s)", file, strerror(errno));
176 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
177 split_one(f, name, 1);
184 string_list_clear(&list, 1);
188 static int split_mbox(const char *file, const char *dir, int allow_bare,
189 int nr_prec, int skip)
195 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
199 error("cannot open mbox %s", file);
205 } while (isspace(peek));
208 if (strbuf_getwholeline(&buf, f, '\n')) {
209 /* empty stdin is OK */
211 error("cannot read mbox %s", file);
218 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
219 file_done = split_one(f, name, allow_bare);
230 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
232 int nr = 0, nr_prec = 4, num = 0;
234 const char *dir = NULL;
236 static const char *stdin_only[] = { "-", NULL };
238 for (argp = argv+1; *argp; argp++) {
239 const char *arg = *argp;
244 if ( arg[1] == 'd' ) {
245 nr_prec = strtol(arg+2, NULL, 10);
246 if (nr_prec < 3 || 10 <= nr_prec)
247 usage(git_mailsplit_usage);
249 } else if ( arg[1] == 'f' ) {
250 nr = strtol(arg+2, NULL, 10);
251 } else if ( arg[1] == 'h' ) {
252 usage(git_mailsplit_usage);
253 } else if ( arg[1] == 'b' && !arg[2] ) {
255 } else if (!strcmp(arg, "--keep-cr")) {
257 } else if ( arg[1] == 'o' && arg[2] ) {
259 } else if ( arg[1] == '-' && !arg[2] ) {
260 argp++; /* -- marks end of options */
263 die("unknown option: %s", arg);
268 /* Backwards compatibility: if no -o specified, accept
269 <mbox> <dir> or just <dir> */
270 switch (argc - (argp-argv)) {
276 stdin_only[0] = argp[0];
281 usage(git_mailsplit_usage);
284 /* New usage: if no more argument, parse stdin */
290 const char *arg = *argp++;
294 if (arg[0] == '-' && arg[1] == 0) {
295 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
297 error("cannot split patches from stdin");
305 if (stat(arg, &argstat) == -1) {
306 error("cannot stat %s (%s)", arg, strerror(errno));
310 if (S_ISDIR(argstat.st_mode))
311 ret = split_maildir(arg, dir, nr_prec, nr);
313 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
316 error("cannot split patches from %s", arg);