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)
64 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
66 die_errno("cannot open output file '%s'", name);
67 output = xfdopen(fd, "w");
69 /* Copy it out, while searching for a line that begins with
70 * "From " and having something that looks like a date format.
73 if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
74 buf.buf[buf.len-2] == '\r') {
75 strbuf_setlen(&buf, buf.len-2);
76 strbuf_addch(&buf, '\n');
79 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
80 die_errno("cannot write output");
82 if (strbuf_getwholeline(&buf, mbox, '\n')) {
87 die_errno("cannot read mbox");
89 if (!is_bare && is_from_line(buf.buf, buf.len))
90 break; /* done with one message */
99 fprintf(stderr, "corrupt mailbox\n");
103 static int populate_maildir_list(struct string_list *list, const char *path)
108 char *subs[] = { "cur", "new", NULL };
111 for (sub = subs; *sub; ++sub) {
112 snprintf(name, sizeof(name), "%s/%s", path, *sub);
113 if ((dir = opendir(name)) == NULL) {
116 error("cannot opendir %s (%s)", name, strerror(errno));
120 while ((dent = readdir(dir)) != NULL) {
121 if (dent->d_name[0] == '.')
123 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
124 string_list_insert(list, name);
133 static int maildir_filename_cmp(const char *a, const char *b)
136 if (isdigit(*a) && isdigit(*b)) {
138 na = strtol(a, (char **)&a, 10);
139 nb = strtol(b, (char **)&b, 10);
142 /* strtol advanced our pointers */
146 return (unsigned char)*a - (unsigned char)*b;
151 return (unsigned char)*a - (unsigned char)*b;
154 static int split_maildir(const char *maildir, const char *dir,
155 int nr_prec, int skip)
161 struct string_list list = STRING_LIST_INIT_DUP;
163 list.cmp = maildir_filename_cmp;
165 if (populate_maildir_list(&list, maildir) < 0)
168 for (i = 0; i < list.nr; i++) {
170 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
171 f = fopen(file, "r");
173 error("cannot open mail %s (%s)", file, strerror(errno));
177 if (strbuf_getwholeline(&buf, f, '\n')) {
178 error("cannot read mail %s (%s)", file, strerror(errno));
182 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
183 split_one(f, name, 1);
190 string_list_clear(&list, 1);
194 static int split_mbox(const char *file, const char *dir, int allow_bare,
195 int nr_prec, int skip)
201 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
205 error("cannot open mbox %s", file);
211 } while (isspace(peek));
214 if (strbuf_getwholeline(&buf, f, '\n')) {
215 /* empty stdin is OK */
217 error("cannot read mbox %s", file);
224 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
225 file_done = split_one(f, name, allow_bare);
236 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
238 int nr = 0, nr_prec = 4, num = 0;
240 const char *dir = NULL;
242 static const char *stdin_only[] = { "-", NULL };
244 for (argp = argv+1; *argp; argp++) {
245 const char *arg = *argp;
250 if ( arg[1] == 'd' ) {
251 nr_prec = strtol(arg+2, NULL, 10);
252 if (nr_prec < 3 || 10 <= nr_prec)
253 usage(git_mailsplit_usage);
255 } else if ( arg[1] == 'f' ) {
256 nr = strtol(arg+2, NULL, 10);
257 } else if ( arg[1] == 'h' ) {
258 usage(git_mailsplit_usage);
259 } else if ( arg[1] == 'b' && !arg[2] ) {
261 } else if (!strcmp(arg, "--keep-cr")) {
263 } else if ( arg[1] == 'o' && arg[2] ) {
265 } else if ( arg[1] == '-' && !arg[2] ) {
266 argp++; /* -- marks end of options */
269 die("unknown option: %s", arg);
274 /* Backwards compatibility: if no -o specified, accept
275 <mbox> <dir> or just <dir> */
276 switch (argc - (argp-argv)) {
282 stdin_only[0] = argp[0];
287 usage(git_mailsplit_usage);
290 /* New usage: if no more argument, parse stdin */
296 const char *arg = *argp++;
300 if (arg[0] == '-' && arg[1] == 0) {
301 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
303 error("cannot split patches from stdin");
311 if (stat(arg, &argstat) == -1) {
312 error("cannot stat %s (%s)", arg, strerror(errno));
316 if (S_ISDIR(argstat.st_mode))
317 ret = split_maildir(arg, dir, nr_prec, nr);
319 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
322 error("cannot split patches from %s", arg);