blob: 3af9ddb8ae5c7e902937ada927590d908c193e0f [file] [log] [blame]
Linus Torvalds2744b232005-04-11 23:46:50 -07001/*
2 * Totally braindamaged mbox splitter program.
3 *
4 * It just splits a mbox into a list of files: "0001" "0002" ..
5 * so you can process them further from there.
6 */
Lukas Sandströme690e842006-06-13 22:21:46 +02007#include "builtin.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00008#include "gettext.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01009#include "string-list.h"
Brandon Caseyc8f373a2009-08-04 22:31:57 -050010#include "strbuf.h"
Linus Torvalds2744b232005-04-11 23:46:50 -070011
Junio C Hamano8b73edf2005-10-06 15:55:43 -070012static const char git_mailsplit_usage[] =
Štěpán Němec0adda932010-10-08 19:31:17 +020013"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
Linus Torvalds2744b232005-04-11 23:46:50 -070014
15static int is_from_line(const char *line, int len)
16{
17 const char *colon;
18
19 if (len < 20 || memcmp("From ", line, 5))
20 return 0;
21
22 colon = line + len - 2;
23 line += 5;
24 for (;;) {
25 if (colon < line)
26 return 0;
27 if (*--colon == ':')
28 break;
29 }
30
31 if (!isdigit(colon[-4]) ||
32 !isdigit(colon[-2]) ||
33 !isdigit(colon[-1]) ||
34 !isdigit(colon[ 1]) ||
35 !isdigit(colon[ 2]))
36 return 0;
37
38 /* year */
39 if (strtol(colon+3, NULL, 10) <= 90)
40 return 0;
41
42 /* Ok, close enough */
43 return 1;
44}
45
Brandon Caseyc8f373a2009-08-04 22:31:57 -050046static struct strbuf buf = STRBUF_INIT;
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -050047static int keep_cr;
Eric Wongc88098d2016-06-05 04:46:40 +000048static int mboxrd;
49
50static int is_gtfrom(const struct strbuf *buf)
51{
52 size_t min = strlen(">From ");
53 size_t ngt;
54
55 if (buf->len < min)
56 return 0;
57
58 ngt = strspn(buf->buf, ">");
59 return ngt && starts_with(buf->buf + ngt, "From ");
60}
Linus Torvalds2744b232005-04-11 23:46:50 -070061
Junio C Hamano8b73edf2005-10-06 15:55:43 -070062/* Called with the first line (potentially partial)
63 * already in buf[] -- normally that should begin with
64 * the Unix "From " line. Write it into the specified
65 * file.
66 */
H. Peter Anvinb3f041f2005-12-13 22:39:23 -080067static int split_one(FILE *mbox, const char *name, int allow_bare)
Junio C Hamano8b73edf2005-10-06 15:55:43 -070068{
Stefan Beller13b08122014-08-12 23:21:27 +020069 FILE *output;
Junio C Hamano8b73edf2005-10-06 15:55:43 -070070 int fd;
71 int status = 0;
Brandon Caseyc8f373a2009-08-04 22:31:57 -050072 int is_bare = !is_from_line(buf.buf, buf.len);
Junio C Hamano8b73edf2005-10-06 15:55:43 -070073
Stefan Beller13b08122014-08-12 23:21:27 +020074 if (is_bare && !allow_bare) {
Stefan Beller13b08122014-08-12 23:21:27 +020075 fprintf(stderr, "corrupt mailbox\n");
76 exit(1);
77 }
René Scharfe66e905b2021-08-25 22:16:46 +020078 fd = xopen(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
Jim Meyering41698372009-09-12 10:43:27 +020079 output = xfdopen(fd, "w");
Linus Torvalds2744b232005-04-11 23:46:50 -070080
Junio C Hamano8b73edf2005-10-06 15:55:43 -070081 /* Copy it out, while searching for a line that begins with
82 * "From " and having something that looks like a date format.
Linus Torvalds2744b232005-04-11 23:46:50 -070083 */
Junio C Hamano8b73edf2005-10-06 15:55:43 -070084 for (;;) {
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -050085 if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
86 buf.buf[buf.len-2] == '\r') {
87 strbuf_setlen(&buf, buf.len-2);
88 strbuf_addch(&buf, '\n');
89 }
90
Eric Wongc88098d2016-06-05 04:46:40 +000091 if (mboxrd && is_gtfrom(&buf))
92 strbuf_remove(&buf, 0, 1);
93
Brandon Caseyc8f373a2009-08-04 22:31:57 -050094 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
Thomas Rast0721c312009-06-27 17:58:47 +020095 die_errno("cannot write output");
Junio C Hamano8b73edf2005-10-06 15:55:43 -070096
Brandon Caseyc8f373a2009-08-04 22:31:57 -050097 if (strbuf_getwholeline(&buf, mbox, '\n')) {
Junio C Hamano8b73edf2005-10-06 15:55:43 -070098 if (feof(mbox)) {
99 status = 1;
100 break;
101 }
Thomas Rast0721c312009-06-27 17:58:47 +0200102 die_errno("cannot read mbox");
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700103 }
Brandon Caseyc8f373a2009-08-04 22:31:57 -0500104 if (!is_bare && is_from_line(buf.buf, buf.len))
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700105 break; /* done with one message */
106 }
107 fclose(output);
108 return status;
Linus Torvalds2744b232005-04-11 23:46:50 -0700109}
110
Johannes Schindelinc455c872008-07-21 19:03:49 +0100111static int populate_maildir_list(struct string_list *list, const char *path)
Linus Torvalds2744b232005-04-11 23:46:50 -0700112{
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200113 DIR *dir;
114 struct dirent *dent;
Jeff King1d895f12015-09-24 17:05:51 -0400115 char *name = NULL;
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000116 char *subs[] = { "cur", "new", NULL };
117 char **sub;
Jeff King1d895f12015-09-24 17:05:51 -0400118 int ret = -1;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200119
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000120 for (sub = subs; *sub; ++sub) {
Jeff King1d895f12015-09-24 17:05:51 -0400121 free(name);
122 name = xstrfmt("%s/%s", path, *sub);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700123 if (!(dir = opendir(name))) {
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000124 if (errno == ENOENT)
125 continue;
Nguyễn Thái Ngọc Duy880c0ae2016-05-08 16:47:28 +0700126 error_errno("cannot opendir %s", name);
Jeff King1d895f12015-09-24 17:05:51 -0400127 goto out;
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000128 }
129
130 while ((dent = readdir(dir)) != NULL) {
131 if (dent->d_name[0] == '.')
132 continue;
Jeff King1d895f12015-09-24 17:05:51 -0400133 free(name);
134 name = xstrfmt("%s/%s", *sub, dent->d_name);
Julian Phillips78a395d2010-06-26 00:41:35 +0100135 string_list_insert(list, name);
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000136 }
137
138 closedir(dir);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200139 }
140
Jeff King1d895f12015-09-24 17:05:51 -0400141 ret = 0;
142
143out:
144 free(name);
145 return ret;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200146}
147
Jeff King18505c32013-03-01 18:35:48 -0500148static int maildir_filename_cmp(const char *a, const char *b)
149{
150 while (*a && *b) {
151 if (isdigit(*a) && isdigit(*b)) {
152 long int na, nb;
153 na = strtol(a, (char **)&a, 10);
154 nb = strtol(b, (char **)&b, 10);
155 if (na != nb)
156 return na - nb;
157 /* strtol advanced our pointers */
158 }
159 else {
160 if (*a != *b)
161 return (unsigned char)*a - (unsigned char)*b;
162 a++;
163 b++;
164 }
165 }
166 return (unsigned char)*a - (unsigned char)*b;
167}
168
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200169static int split_maildir(const char *maildir, const char *dir,
170 int nr_prec, int skip)
171{
Jeff King1d895f12015-09-24 17:05:51 -0400172 char *file = NULL;
Jeff Kingd270d7b2015-09-24 17:03:05 -0400173 FILE *f = NULL;
Lukas Sandströme690e842006-06-13 22:21:46 +0200174 int ret = -1;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200175 int i;
Thiago Farina183113a2010-07-04 16:46:19 -0300176 struct string_list list = STRING_LIST_INIT_DUP;
Lukas Sandströme690e842006-06-13 22:21:46 +0200177
Jeff King18505c32013-03-01 18:35:48 -0500178 list.cmp = maildir_filename_cmp;
179
Gerrit Paped50a4bc2007-11-06 08:54:18 +0000180 if (populate_maildir_list(&list, maildir) < 0)
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200181 goto out;
Lukas Sandströme690e842006-06-13 22:21:46 +0200182
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200183 for (i = 0; i < list.nr; i++) {
Jeff King1d895f12015-09-24 17:05:51 -0400184 char *name;
185
186 free(file);
187 file = xstrfmt("%s/%s", maildir, list.items[i].string);
188
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200189 f = fopen(file, "r");
190 if (!f) {
Nguyễn Thái Ngọc Duy880c0ae2016-05-08 16:47:28 +0700191 error_errno("cannot open mail %s", file);
Lukas Sandströme690e842006-06-13 22:21:46 +0200192 goto out;
193 }
194
Brandon Caseyc8f373a2009-08-04 22:31:57 -0500195 if (strbuf_getwholeline(&buf, f, '\n')) {
Nguyễn Thái Ngọc Duy880c0ae2016-05-08 16:47:28 +0700196 error_errno("cannot read mail %s", file);
Lukas Sandströme690e842006-06-13 22:21:46 +0200197 goto out;
198 }
199
Jeff King1d895f12015-09-24 17:05:51 -0400200 name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200201 split_one(f, name, 1);
Jeff King1d895f12015-09-24 17:05:51 -0400202 free(name);
Lukas Sandströme690e842006-06-13 22:21:46 +0200203
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200204 fclose(f);
Jeff Kingd270d7b2015-09-24 17:03:05 -0400205 f = NULL;
Lukas Sandströme690e842006-06-13 22:21:46 +0200206 }
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200207
Lukas Sandströme690e842006-06-13 22:21:46 +0200208 ret = skip;
209out:
Jeff Kingd270d7b2015-09-24 17:03:05 -0400210 if (f)
211 fclose(f);
Jeff King1d895f12015-09-24 17:05:51 -0400212 free(file);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100213 string_list_clear(&list, 1);
Lukas Sandströme690e842006-06-13 22:21:46 +0200214 return ret;
215}
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200216
Junio C Hamanofcd056a2007-06-08 02:22:56 -0700217static int split_mbox(const char *file, const char *dir, int allow_bare,
218 int nr_prec, int skip)
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200219{
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200220 int ret = -1;
Simon Sasburgf88a5452007-11-01 23:57:45 +0100221 int peek;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200222
223 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
224 int file_done = 0;
225
Junio C Hamano7b20af62022-03-03 13:58:39 -0800226 if (isatty(fileno(f)))
227 warning(_("reading patches from stdin/tty..."));
228
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200229 if (!f) {
Nguyễn Thái Ngọc Duy880c0ae2016-05-08 16:47:28 +0700230 error_errno("cannot open mbox %s", file);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200231 goto out;
232 }
233
Simon Sasburgf88a5452007-11-01 23:57:45 +0100234 do {
235 peek = fgetc(f);
Johannes Schindelinf0733c12017-05-04 15:56:14 +0200236 if (peek == EOF) {
237 if (f == stdin)
238 /* empty stdin is OK */
239 ret = skip;
240 else {
241 fclose(f);
242 error(_("empty mbox: '%s'"), file);
243 }
244 goto out;
245 }
Simon Sasburgf88a5452007-11-01 23:57:45 +0100246 } while (isspace(peek));
247 ungetc(peek, f);
248
Brandon Caseyc8f373a2009-08-04 22:31:57 -0500249 if (strbuf_getwholeline(&buf, f, '\n')) {
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200250 /* empty stdin is OK */
251 if (f != stdin) {
252 error("cannot read mbox %s", file);
253 goto out;
254 }
255 file_done = 1;
256 }
257
258 while (!file_done) {
Jeff King1d895f12015-09-24 17:05:51 -0400259 char *name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200260 file_done = split_one(f, name, allow_bare);
Jeff King1d895f12015-09-24 17:05:51 -0400261 free(name);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200262 }
263
264 if (f != stdin)
265 fclose(f);
266
267 ret = skip;
268out:
269 return ret;
270}
271
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700272int cmd_mailsplit(int argc, const char **argv, const char *prefix)
Lukas Sandströme690e842006-06-13 22:21:46 +0200273{
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200274 int nr = 0, nr_prec = 4, num = 0;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800275 int allow_bare = 0;
276 const char *dir = NULL;
277 const char **argp;
278 static const char *stdin_only[] = { "-", NULL };
Linus Torvalds2744b232005-04-11 23:46:50 -0700279
Jeff King79156912023-03-28 16:55:17 -0400280 BUG_ON_NON_EMPTY_PREFIX(prefix);
281
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800282 for (argp = argv+1; *argp; argp++) {
283 const char *arg = *argp;
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700284
285 if (arg[0] != '-')
286 break;
287 /* do flags here */
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800288 if ( arg[1] == 'd' ) {
289 nr_prec = strtol(arg+2, NULL, 10);
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700290 if (nr_prec < 3 || 10 <= nr_prec)
291 usage(git_mailsplit_usage);
292 continue;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800293 } else if ( arg[1] == 'f' ) {
294 nr = strtol(arg+2, NULL, 10);
Jonathan Niederaa481d32009-11-09 09:04:52 -0600295 } else if ( arg[1] == 'h' ) {
296 usage(git_mailsplit_usage);
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800297 } else if ( arg[1] == 'b' && !arg[2] ) {
298 allow_bare = 1;
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -0500299 } else if (!strcmp(arg, "--keep-cr")) {
300 keep_cr = 1;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800301 } else if ( arg[1] == 'o' && arg[2] ) {
302 dir = arg+2;
Eric Wongc88098d2016-06-05 04:46:40 +0000303 } else if (!strcmp(arg, "--mboxrd")) {
304 mboxrd = 1;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800305 } else if ( arg[1] == '-' && !arg[2] ) {
306 argp++; /* -- marks end of options */
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700307 break;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800308 } else {
309 die("unknown option: %s", arg);
Linus Torvalds2744b232005-04-11 23:46:50 -0700310 }
Junio C Hamano8b73edf2005-10-06 15:55:43 -0700311 }
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800312
313 if ( !dir ) {
314 /* Backwards compatibility: if no -o specified, accept
315 <mbox> <dir> or just <dir> */
316 switch (argc - (argp-argv)) {
317 case 1:
318 dir = argp[0];
319 argp = stdin_only;
320 break;
321 case 2:
322 stdin_only[0] = argp[0];
323 dir = argp[1];
324 argp = stdin_only;
325 break;
326 default:
327 usage(git_mailsplit_usage);
328 }
329 } else {
330 /* New usage: if no more argument, parse stdin */
331 if ( !*argp )
332 argp = stdin_only;
333 }
334
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200335 while (*argp) {
336 const char *arg = *argp++;
337 struct stat argstat;
338 int ret = 0;
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800339
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200340 if (arg[0] == '-' && arg[1] == 0) {
341 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
342 if (ret < 0) {
343 error("cannot split patches from stdin");
344 return 1;
345 }
Junio C Hamanob3327182007-05-28 15:48:07 -0700346 num += (ret - nr);
347 nr = ret;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200348 continue;
349 }
350
351 if (stat(arg, &argstat) == -1) {
Nguyễn Thái Ngọc Duy880c0ae2016-05-08 16:47:28 +0700352 error_errno("cannot stat %s", arg);
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200353 return 1;
354 }
355
356 if (S_ISDIR(argstat.st_mode))
357 ret = split_maildir(arg, dir, nr_prec, nr);
358 else
359 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
360
361 if (ret < 0) {
362 error("cannot split patches from %s", arg);
363 return 1;
364 }
Junio C Hamanob3327182007-05-28 15:48:07 -0700365 num += (ret - nr);
366 nr = ret;
Fernando J. Peredad63bd9a2007-05-25 00:15:36 +0200367 }
368
369 printf("%d\n", num);
370
371 return 0;
Linus Torvalds2744b232005-04-11 23:46:50 -0700372}