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