c2ec6ea3d51e760581dcb6f86c1783a545ebc07a
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 */
7#include "cache.h"
8#include "builtin.h"
9#include "string-list.h"
10#include "strbuf.h"
11
12static const char git_mailsplit_usage[] =
13"git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
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
46static struct strbuf buf = STRBUF_INIT;
47
48/* We cannot use fgets() because our lines can contain NULs */
49int read_line_with_nul(char *buf, int size, FILE *in)
50{
51 int len = 0, c;
52
53 for (;;) {
54 c = getc(in);
55 if (c == EOF)
56 break;
57 buf[len++] = c;
58 if (c == '\n' || len + 1 >= size)
59 break;
60 }
61 buf[len] = '\0';
62
63 return len;
64}
65
66/* Called with the first line (potentially partial)
67 * already in buf[] -- normally that should begin with
68 * the Unix "From " line. Write it into the specified
69 * file.
70 */
71static int split_one(FILE *mbox, const char *name, int allow_bare)
72{
73 FILE *output = NULL;
74 int fd;
75 int status = 0;
76 int is_bare = !is_from_line(buf.buf, buf.len);
77
78 if (is_bare && !allow_bare)
79 goto corrupt;
80
81 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
82 if (fd < 0)
83 die_errno("cannot open output file '%s'", name);
84 output = fdopen(fd, "w");
85
86 /* Copy it out, while searching for a line that begins with
87 * "From " and having something that looks like a date format.
88 */
89 for (;;) {
90 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
91 die_errno("cannot write output");
92
93 if (strbuf_getwholeline(&buf, mbox, '\n')) {
94 if (feof(mbox)) {
95 status = 1;
96 break;
97 }
98 die_errno("cannot read mbox");
99 }
100 if (!is_bare && is_from_line(buf.buf, buf.len))
101 break; /* done with one message */
102 }
103 fclose(output);
104 return status;
105
106 corrupt:
107 if (output)
108 fclose(output);
109 unlink(name);
110 fprintf(stderr, "corrupt mailbox\n");
111 exit(1);
112}
113
114static int populate_maildir_list(struct string_list *list, const char *path)
115{
116 DIR *dir;
117 struct dirent *dent;
118 char name[PATH_MAX];
119 char *subs[] = { "cur", "new", NULL };
120 char **sub;
121
122 for (sub = subs; *sub; ++sub) {
123 snprintf(name, sizeof(name), "%s/%s", path, *sub);
124 if ((dir = opendir(name)) == NULL) {
125 if (errno == ENOENT)
126 continue;
127 error("cannot opendir %s (%s)", name, strerror(errno));
128 return -1;
129 }
130
131 while ((dent = readdir(dir)) != NULL) {
132 if (dent->d_name[0] == '.')
133 continue;
134 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
135 string_list_insert(name, list);
136 }
137
138 closedir(dir);
139 }
140
141 return 0;
142}
143
144static int split_maildir(const char *maildir, const char *dir,
145 int nr_prec, int skip)
146{
147 char file[PATH_MAX];
148 char name[PATH_MAX];
149 int ret = -1;
150 int i;
151 struct string_list list = {NULL, 0, 0, 1};
152
153 if (populate_maildir_list(&list, maildir) < 0)
154 goto out;
155
156 for (i = 0; i < list.nr; i++) {
157 FILE *f;
158 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
159 f = fopen(file, "r");
160 if (!f) {
161 error("cannot open mail %s (%s)", file, strerror(errno));
162 goto out;
163 }
164
165 if (strbuf_getwholeline(&buf, f, '\n')) {
166 error("cannot read mail %s (%s)", file, strerror(errno));
167 goto out;
168 }
169
170 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
171 split_one(f, name, 1);
172
173 fclose(f);
174 }
175
176 ret = skip;
177out:
178 string_list_clear(&list, 1);
179 return ret;
180}
181
182static int split_mbox(const char *file, const char *dir, int allow_bare,
183 int nr_prec, int skip)
184{
185 char name[PATH_MAX];
186 int ret = -1;
187 int peek;
188
189 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
190 int file_done = 0;
191
192 if (!f) {
193 error("cannot open mbox %s", file);
194 goto out;
195 }
196
197 do {
198 peek = fgetc(f);
199 } while (isspace(peek));
200 ungetc(peek, f);
201
202 if (strbuf_getwholeline(&buf, f, '\n')) {
203 /* empty stdin is OK */
204 if (f != stdin) {
205 error("cannot read mbox %s", file);
206 goto out;
207 }
208 file_done = 1;
209 }
210
211 while (!file_done) {
212 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
213 file_done = split_one(f, name, allow_bare);
214 }
215
216 if (f != stdin)
217 fclose(f);
218
219 ret = skip;
220out:
221 return ret;
222}
223
224int cmd_mailsplit(int argc, const char **argv, const char *prefix)
225{
226 int nr = 0, nr_prec = 4, num = 0;
227 int allow_bare = 0;
228 const char *dir = NULL;
229 const char **argp;
230 static const char *stdin_only[] = { "-", NULL };
231
232 for (argp = argv+1; *argp; argp++) {
233 const char *arg = *argp;
234
235 if (arg[0] != '-')
236 break;
237 /* do flags here */
238 if ( arg[1] == 'd' ) {
239 nr_prec = strtol(arg+2, NULL, 10);
240 if (nr_prec < 3 || 10 <= nr_prec)
241 usage(git_mailsplit_usage);
242 continue;
243 } else if ( arg[1] == 'f' ) {
244 nr = strtol(arg+2, NULL, 10);
245 } else if ( arg[1] == 'b' && !arg[2] ) {
246 allow_bare = 1;
247 } else if ( arg[1] == 'o' && arg[2] ) {
248 dir = arg+2;
249 } else if ( arg[1] == '-' && !arg[2] ) {
250 argp++; /* -- marks end of options */
251 break;
252 } else {
253 die("unknown option: %s", arg);
254 }
255 }
256
257 if ( !dir ) {
258 /* Backwards compatibility: if no -o specified, accept
259 <mbox> <dir> or just <dir> */
260 switch (argc - (argp-argv)) {
261 case 1:
262 dir = argp[0];
263 argp = stdin_only;
264 break;
265 case 2:
266 stdin_only[0] = argp[0];
267 dir = argp[1];
268 argp = stdin_only;
269 break;
270 default:
271 usage(git_mailsplit_usage);
272 }
273 } else {
274 /* New usage: if no more argument, parse stdin */
275 if ( !*argp )
276 argp = stdin_only;
277 }
278
279 while (*argp) {
280 const char *arg = *argp++;
281 struct stat argstat;
282 int ret = 0;
283
284 if (arg[0] == '-' && arg[1] == 0) {
285 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
286 if (ret < 0) {
287 error("cannot split patches from stdin");
288 return 1;
289 }
290 num += (ret - nr);
291 nr = ret;
292 continue;
293 }
294
295 if (stat(arg, &argstat) == -1) {
296 error("cannot stat %s (%s)", arg, strerror(errno));
297 return 1;
298 }
299
300 if (S_ISDIR(argstat.st_mode))
301 ret = split_maildir(arg, dir, nr_prec, nr);
302 else
303 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
304
305 if (ret < 0) {
306 error("cannot split patches from %s", arg);
307 return 1;
308 }
309 num += (ret - nr);
310 nr = ret;
311 }
312
313 printf("%d\n", num);
314
315 return 0;
316}