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