mailsplit: make PATH_MAX buffers dynamic
[git/git.git] / builtin / mailsplit.c
CommitLineData
2744b234
LT
1/*
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 */
8b73edf4 7#include "cache.h"
e690e843 8#include "builtin.h"
c455c87c 9#include "string-list.h"
c8f373a5 10#include "strbuf.h"
2744b234 11
8b73edf4 12static const char git_mailsplit_usage[] =
0adda936 13"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
2744b234
LT
14
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
c8f373a5 46static struct strbuf buf = STRBUF_INIT;
c2ca1d79 47static int keep_cr;
8b73edf4
JH
48
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
52 * file.
53 */
b3f041fb 54static int split_one(FILE *mbox, const char *name, int allow_bare)
2744b234 55{
13b08125 56 FILE *output;
8b73edf4
JH
57 int fd;
58 int status = 0;
c8f373a5 59 int is_bare = !is_from_line(buf.buf, buf.len);
2744b234 60
13b08125 61 if (is_bare && !allow_bare) {
13b08125
SB
62 fprintf(stderr, "corrupt mailbox\n");
63 exit(1);
64 }
8b73edf4
JH
65 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
66 if (fd < 0)
0721c314 67 die_errno("cannot open output file '%s'", name);
41698375 68 output = xfdopen(fd, "w");
2744b234 69
8b73edf4
JH
70 /* Copy it out, while searching for a line that begins with
71 * "From " and having something that looks like a date format.
2744b234 72 */
8b73edf4 73 for (;;) {
c2ca1d79
JH
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');
78 }
79
c8f373a5 80 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
0721c314 81 die_errno("cannot write output");
8b73edf4 82
c8f373a5 83 if (strbuf_getwholeline(&buf, mbox, '\n')) {
8b73edf4
JH
84 if (feof(mbox)) {
85 status = 1;
86 break;
87 }
0721c314 88 die_errno("cannot read mbox");
8b73edf4 89 }
c8f373a5 90 if (!is_bare && is_from_line(buf.buf, buf.len))
8b73edf4
JH
91 break; /* done with one message */
92 }
93 fclose(output);
94 return status;
2744b234
LT
95}
96
c455c87c 97static int populate_maildir_list(struct string_list *list, const char *path)
2744b234 98{
d63bd9a2
FP
99 DIR *dir;
100 struct dirent *dent;
1d895f19 101 char *name = NULL;
d50a4bc4
GP
102 char *subs[] = { "cur", "new", NULL };
103 char **sub;
1d895f19 104 int ret = -1;
d50a4bc4
GP
105
106 for (sub = subs; *sub; ++sub) {
1d895f19
JK
107 free(name);
108 name = xstrfmt("%s/%s", path, *sub);
d50a4bc4
GP
109 if ((dir = opendir(name)) == NULL) {
110 if (errno == ENOENT)
111 continue;
112 error("cannot opendir %s (%s)", name, strerror(errno));
1d895f19 113 goto out;
d50a4bc4 114 }
d63bd9a2 115
d50a4bc4
GP
116 while ((dent = readdir(dir)) != NULL) {
117 if (dent->d_name[0] == '.')
118 continue;
1d895f19
JK
119 free(name);
120 name = xstrfmt("%s/%s", *sub, dent->d_name);
78a395d3 121 string_list_insert(list, name);
d50a4bc4 122 }
d63bd9a2 123
d50a4bc4 124 closedir(dir);
d63bd9a2
FP
125 }
126
1d895f19
JK
127 ret = 0;
128
129out:
130 free(name);
131 return ret;
d63bd9a2
FP
132}
133
18505c34
JK
134static int maildir_filename_cmp(const char *a, const char *b)
135{
136 while (*a && *b) {
137 if (isdigit(*a) && isdigit(*b)) {
138 long int na, nb;
139 na = strtol(a, (char **)&a, 10);
140 nb = strtol(b, (char **)&b, 10);
141 if (na != nb)
142 return na - nb;
143 /* strtol advanced our pointers */
144 }
145 else {
146 if (*a != *b)
147 return (unsigned char)*a - (unsigned char)*b;
148 a++;
149 b++;
150 }
151 }
152 return (unsigned char)*a - (unsigned char)*b;
153}
154
d63bd9a2
FP
155static int split_maildir(const char *maildir, const char *dir,
156 int nr_prec, int skip)
157{
1d895f19 158 char *file = NULL;
d270d7b7 159 FILE *f = NULL;
e690e843 160 int ret = -1;
d63bd9a2 161 int i;
183113a5 162 struct string_list list = STRING_LIST_INIT_DUP;
e690e843 163
18505c34
JK
164 list.cmp = maildir_filename_cmp;
165
d50a4bc4 166 if (populate_maildir_list(&list, maildir) < 0)
d63bd9a2 167 goto out;
e690e843 168
d63bd9a2 169 for (i = 0; i < list.nr; i++) {
1d895f19
JK
170 char *name;
171
172 free(file);
173 file = xstrfmt("%s/%s", maildir, list.items[i].string);
174
d63bd9a2
FP
175 f = fopen(file, "r");
176 if (!f) {
177 error("cannot open mail %s (%s)", file, strerror(errno));
e690e843
LS
178 goto out;
179 }
180
c8f373a5 181 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2 182 error("cannot read mail %s (%s)", file, strerror(errno));
e690e843
LS
183 goto out;
184 }
185
1d895f19 186 name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
d63bd9a2 187 split_one(f, name, 1);
1d895f19 188 free(name);
d63bd9a2
FP
189
190 fclose(f);
d270d7b7 191 f = NULL;
d63bd9a2
FP
192 }
193
d63bd9a2
FP
194 ret = skip;
195out:
d270d7b7
JK
196 if (f)
197 fclose(f);
1d895f19 198 free(file);
c455c87c 199 string_list_clear(&list, 1);
d63bd9a2
FP
200 return ret;
201}
202
fcd056a6
JH
203static int split_mbox(const char *file, const char *dir, int allow_bare,
204 int nr_prec, int skip)
d63bd9a2 205{
d63bd9a2 206 int ret = -1;
f88a545a 207 int peek;
d63bd9a2
FP
208
209 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
210 int file_done = 0;
211
212 if (!f) {
213 error("cannot open mbox %s", file);
214 goto out;
215 }
216
f88a545a
SS
217 do {
218 peek = fgetc(f);
219 } while (isspace(peek));
220 ungetc(peek, f);
221
c8f373a5 222 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2
FP
223 /* empty stdin is OK */
224 if (f != stdin) {
225 error("cannot read mbox %s", file);
226 goto out;
e690e843 227 }
d63bd9a2
FP
228 file_done = 1;
229 }
e690e843 230
d63bd9a2 231 while (!file_done) {
1d895f19 232 char *name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
d63bd9a2 233 file_done = split_one(f, name, allow_bare);
1d895f19 234 free(name);
e690e843 235 }
d63bd9a2
FP
236
237 if (f != stdin)
238 fclose(f);
239
e690e843
LS
240 ret = skip;
241out:
e690e843
LS
242 return ret;
243}
d63bd9a2 244
a633fca0 245int cmd_mailsplit(int argc, const char **argv, const char *prefix)
e690e843 246{
d63bd9a2 247 int nr = 0, nr_prec = 4, num = 0;
b3f041fb
PA
248 int allow_bare = 0;
249 const char *dir = NULL;
250 const char **argp;
251 static const char *stdin_only[] = { "-", NULL };
8b73edf4 252
b3f041fb
PA
253 for (argp = argv+1; *argp; argp++) {
254 const char *arg = *argp;
8b73edf4
JH
255
256 if (arg[0] != '-')
257 break;
258 /* do flags here */
b3f041fb
PA
259 if ( arg[1] == 'd' ) {
260 nr_prec = strtol(arg+2, NULL, 10);
8b73edf4
JH
261 if (nr_prec < 3 || 10 <= nr_prec)
262 usage(git_mailsplit_usage);
263 continue;
b3f041fb
PA
264 } else if ( arg[1] == 'f' ) {
265 nr = strtol(arg+2, NULL, 10);
aa481d38
JN
266 } else if ( arg[1] == 'h' ) {
267 usage(git_mailsplit_usage);
b3f041fb
PA
268 } else if ( arg[1] == 'b' && !arg[2] ) {
269 allow_bare = 1;
c2ca1d79
JH
270 } else if (!strcmp(arg, "--keep-cr")) {
271 keep_cr = 1;
b3f041fb
PA
272 } else if ( arg[1] == 'o' && arg[2] ) {
273 dir = arg+2;
274 } else if ( arg[1] == '-' && !arg[2] ) {
275 argp++; /* -- marks end of options */
276 break;
277 } else {
278 die("unknown option: %s", arg);
8b73edf4 279 }
2744b234 280 }
8b73edf4 281
b3f041fb
PA
282 if ( !dir ) {
283 /* Backwards compatibility: if no -o specified, accept
284 <mbox> <dir> or just <dir> */
285 switch (argc - (argp-argv)) {
286 case 1:
287 dir = argp[0];
288 argp = stdin_only;
289 break;
290 case 2:
291 stdin_only[0] = argp[0];
292 dir = argp[1];
293 argp = stdin_only;
294 break;
295 default:
296 usage(git_mailsplit_usage);
297 }
298 } else {
299 /* New usage: if no more argument, parse stdin */
300 if ( !*argp )
301 argp = stdin_only;
2744b234 302 }
8b73edf4 303
d63bd9a2
FP
304 while (*argp) {
305 const char *arg = *argp++;
306 struct stat argstat;
307 int ret = 0;
308
309 if (arg[0] == '-' && arg[1] == 0) {
310 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
311 if (ret < 0) {
312 error("cannot split patches from stdin");
313 return 1;
314 }
b3327180
JH
315 num += (ret - nr);
316 nr = ret;
d63bd9a2
FP
317 continue;
318 }
319
320 if (stat(arg, &argstat) == -1) {
321 error("cannot stat %s (%s)", arg, strerror(errno));
322 return 1;
323 }
324
325 if (S_ISDIR(argstat.st_mode))
326 ret = split_maildir(arg, dir, nr_prec, nr);
327 else
328 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
329
330 if (ret < 0) {
331 error("cannot split patches from %s", arg);
332 return 1;
333 }
b3327180
JH
334 num += (ret - nr);
335 nr = ret;
d63bd9a2
FP
336 }
337
338 printf("%d\n", num);
b3f041fb 339
d63bd9a2 340 return 0;
2744b234 341}