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