Code

d288fde2c2cef606a60507a7a56b81352ab5d06e
[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"
12 static const char git_mailsplit_usage[] =
13 "git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
15 static int is_from_line(const char *line, int len)
16 {
17         const char *colon;
19         if (len < 20 || memcmp("From ", line, 5))
20                 return 0;
22         colon = line + len - 2;
23         line += 5;
24         for (;;) {
25                 if (colon < line)
26                         return 0;
27                 if (*--colon == ':')
28                         break;
29         }
31         if (!isdigit(colon[-4]) ||
32             !isdigit(colon[-2]) ||
33             !isdigit(colon[-1]) ||
34             !isdigit(colon[ 1]) ||
35             !isdigit(colon[ 2]))
36                 return 0;
38         /* year */
39         if (strtol(colon+3, NULL, 10) <= 90)
40                 return 0;
42         /* Ok, close enough */
43         return 1;
44 }
46 static struct strbuf buf = STRBUF_INIT;
48 /* Called with the first line (potentially partial)
49  * already in buf[] -- normally that should begin with
50  * the Unix "From " line.  Write it into the specified
51  * file.
52  */
53 static int split_one(FILE *mbox, const char *name, int allow_bare)
54 {
55         FILE *output = NULL;
56         int fd;
57         int status = 0;
58         int is_bare = !is_from_line(buf.buf, buf.len);
60         if (is_bare && !allow_bare)
61                 goto corrupt;
63         fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
64         if (fd < 0)
65                 die_errno("cannot open output file '%s'", name);
66         output = fdopen(fd, "w");
68         /* Copy it out, while searching for a line that begins with
69          * "From " and having something that looks like a date format.
70          */
71         for (;;) {
72                 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
73                         die_errno("cannot write output");
75                 if (strbuf_getwholeline(&buf, mbox, '\n')) {
76                         if (feof(mbox)) {
77                                 status = 1;
78                                 break;
79                         }
80                         die_errno("cannot read mbox");
81                 }
82                 if (!is_bare && is_from_line(buf.buf, buf.len))
83                         break; /* done with one message */
84         }
85         fclose(output);
86         return status;
88  corrupt:
89         if (output)
90                 fclose(output);
91         unlink(name);
92         fprintf(stderr, "corrupt mailbox\n");
93         exit(1);
94 }
96 static int populate_maildir_list(struct string_list *list, const char *path)
97 {
98         DIR *dir;
99         struct dirent *dent;
100         char name[PATH_MAX];
101         char *subs[] = { "cur", "new", NULL };
102         char **sub;
104         for (sub = subs; *sub; ++sub) {
105                 snprintf(name, sizeof(name), "%s/%s", path, *sub);
106                 if ((dir = opendir(name)) == NULL) {
107                         if (errno == ENOENT)
108                                 continue;
109                         error("cannot opendir %s (%s)", name, strerror(errno));
110                         return -1;
111                 }
113                 while ((dent = readdir(dir)) != NULL) {
114                         if (dent->d_name[0] == '.')
115                                 continue;
116                         snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
117                         string_list_insert(name, list);
118                 }
120                 closedir(dir);
121         }
123         return 0;
126 static int split_maildir(const char *maildir, const char *dir,
127         int nr_prec, int skip)
129         char file[PATH_MAX];
130         char name[PATH_MAX];
131         int ret = -1;
132         int i;
133         struct string_list list = {NULL, 0, 0, 1};
135         if (populate_maildir_list(&list, maildir) < 0)
136                 goto out;
138         for (i = 0; i < list.nr; i++) {
139                 FILE *f;
140                 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
141                 f = fopen(file, "r");
142                 if (!f) {
143                         error("cannot open mail %s (%s)", file, strerror(errno));
144                         goto out;
145                 }
147                 if (strbuf_getwholeline(&buf, f, '\n')) {
148                         error("cannot read mail %s (%s)", file, strerror(errno));
149                         goto out;
150                 }
152                 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
153                 split_one(f, name, 1);
155                 fclose(f);
156         }
158         ret = skip;
159 out:
160         string_list_clear(&list, 1);
161         return ret;
164 static int split_mbox(const char *file, const char *dir, int allow_bare,
165                       int nr_prec, int skip)
167         char name[PATH_MAX];
168         int ret = -1;
169         int peek;
171         FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
172         int file_done = 0;
174         if (!f) {
175                 error("cannot open mbox %s", file);
176                 goto out;
177         }
179         do {
180                 peek = fgetc(f);
181         } while (isspace(peek));
182         ungetc(peek, f);
184         if (strbuf_getwholeline(&buf, f, '\n')) {
185                 /* empty stdin is OK */
186                 if (f != stdin) {
187                         error("cannot read mbox %s", file);
188                         goto out;
189                 }
190                 file_done = 1;
191         }
193         while (!file_done) {
194                 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
195                 file_done = split_one(f, name, allow_bare);
196         }
198         if (f != stdin)
199                 fclose(f);
201         ret = skip;
202 out:
203         return ret;
206 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
208         int nr = 0, nr_prec = 4, num = 0;
209         int allow_bare = 0;
210         const char *dir = NULL;
211         const char **argp;
212         static const char *stdin_only[] = { "-", NULL };
214         for (argp = argv+1; *argp; argp++) {
215                 const char *arg = *argp;
217                 if (arg[0] != '-')
218                         break;
219                 /* do flags here */
220                 if ( arg[1] == 'd' ) {
221                         nr_prec = strtol(arg+2, NULL, 10);
222                         if (nr_prec < 3 || 10 <= nr_prec)
223                                 usage(git_mailsplit_usage);
224                         continue;
225                 } else if ( arg[1] == 'f' ) {
226                         nr = strtol(arg+2, NULL, 10);
227                 } else if ( arg[1] == 'b' && !arg[2] ) {
228                         allow_bare = 1;
229                 } else if ( arg[1] == 'o' && arg[2] ) {
230                         dir = arg+2;
231                 } else if ( arg[1] == '-' && !arg[2] ) {
232                         argp++; /* -- marks end of options */
233                         break;
234                 } else {
235                         die("unknown option: %s", arg);
236                 }
237         }
239         if ( !dir ) {
240                 /* Backwards compatibility: if no -o specified, accept
241                    <mbox> <dir> or just <dir> */
242                 switch (argc - (argp-argv)) {
243                 case 1:
244                         dir = argp[0];
245                         argp = stdin_only;
246                         break;
247                 case 2:
248                         stdin_only[0] = argp[0];
249                         dir = argp[1];
250                         argp = stdin_only;
251                         break;
252                 default:
253                         usage(git_mailsplit_usage);
254                 }
255         } else {
256                 /* New usage: if no more argument, parse stdin */
257                 if ( !*argp )
258                         argp = stdin_only;
259         }
261         while (*argp) {
262                 const char *arg = *argp++;
263                 struct stat argstat;
264                 int ret = 0;
266                 if (arg[0] == '-' && arg[1] == 0) {
267                         ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
268                         if (ret < 0) {
269                                 error("cannot split patches from stdin");
270                                 return 1;
271                         }
272                         num += (ret - nr);
273                         nr = ret;
274                         continue;
275                 }
277                 if (stat(arg, &argstat) == -1) {
278                         error("cannot stat %s (%s)", arg, strerror(errno));
279                         return 1;
280                 }
282                 if (S_ISDIR(argstat.st_mode))
283                         ret = split_maildir(arg, dir, nr_prec, nr);
284                 else
285                         ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
287                 if (ret < 0) {
288                         error("cannot split patches from %s", arg);
289                         return 1;
290                 }
291                 num += (ret - nr);
292                 nr = ret;
293         }
295         printf("%d\n", num);
297         return 0;