Fix mis-backport of t7002
[git/dscho.git] / builtin-mailsplit.c
blobad5f6b593df45f01360f3daa8b37d024ee793e9e
1 /*
2 * Totally braindamaged mbox splitter program.
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"
11 static const char git_mailsplit_usage[] =
12 "git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
14 static int is_from_line(const char *line, int len)
16 const char *colon;
18 if (len < 20 || memcmp("From ", line, 5))
19 return 0;
21 colon = line + len - 2;
22 line += 5;
23 for (;;) {
24 if (colon < line)
25 return 0;
26 if (*--colon == ':')
27 break;
30 if (!isdigit(colon[-4]) ||
31 !isdigit(colon[-2]) ||
32 !isdigit(colon[-1]) ||
33 !isdigit(colon[ 1]) ||
34 !isdigit(colon[ 2]))
35 return 0;
37 /* year */
38 if (strtol(colon+3, NULL, 10) <= 90)
39 return 0;
41 /* Ok, close enough */
42 return 1;
45 /* Could be as small as 64, enough to hold a Unix "From " line. */
46 static char buf[4096];
48 /* We cannot use fgets() because our lines can contain NULs */
49 int read_line_with_nul(char *buf, int size, FILE *in)
51 int len = 0, c;
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;
61 buf[len] = '\0';
63 return len;
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.
71 static int split_one(FILE *mbox, const char *name, int allow_bare)
73 FILE *output = NULL;
74 int len = strlen(buf);
75 int fd;
76 int status = 0;
77 int is_bare = !is_from_line(buf, len);
79 if (is_bare && !allow_bare)
80 goto corrupt;
82 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
83 if (fd < 0)
84 die_errno("cannot open output file '%s'", name);
85 output = fdopen(fd, "w");
87 /* Copy it out, while searching for a line that begins with
88 * "From " and having something that looks like a date format.
90 for (;;) {
91 int is_partial = len && buf[len-1] != '\n';
93 if (fwrite(buf, 1, len, output) != len)
94 die_errno("cannot write output");
96 len = read_line_with_nul(buf, sizeof(buf), mbox);
97 if (len == 0) {
98 if (feof(mbox)) {
99 status = 1;
100 break;
102 die_errno("cannot read mbox");
104 if (!is_partial && !is_bare && is_from_line(buf, len))
105 break; /* done with one message */
107 fclose(output);
108 return status;
110 corrupt:
111 if (output)
112 fclose(output);
113 unlink(name);
114 fprintf(stderr, "corrupt mailbox\n");
115 exit(1);
118 static int populate_maildir_list(struct string_list *list, const char *path)
120 DIR *dir;
121 struct dirent *dent;
122 char name[PATH_MAX];
123 char *subs[] = { "cur", "new", NULL };
124 char **sub;
126 for (sub = subs; *sub; ++sub) {
127 snprintf(name, sizeof(name), "%s/%s", path, *sub);
128 if ((dir = opendir(name)) == NULL) {
129 if (errno == ENOENT)
130 continue;
131 error("cannot opendir %s (%s)", name, strerror(errno));
132 return -1;
135 while ((dent = readdir(dir)) != NULL) {
136 if (dent->d_name[0] == '.')
137 continue;
138 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
139 string_list_insert(name, list);
142 closedir(dir);
145 return 0;
148 static int split_maildir(const char *maildir, const char *dir,
149 int nr_prec, int skip)
151 char file[PATH_MAX];
152 char name[PATH_MAX];
153 int ret = -1;
154 int i;
155 struct string_list list = {NULL, 0, 0, 1};
157 if (populate_maildir_list(&list, maildir) < 0)
158 goto out;
160 for (i = 0; i < list.nr; i++) {
161 FILE *f;
162 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
163 f = fopen(file, "r");
164 if (!f) {
165 error("cannot open mail %s (%s)", file, strerror(errno));
166 goto out;
169 if (fgets(buf, sizeof(buf), f) == NULL) {
170 error("cannot read mail %s (%s)", file, strerror(errno));
171 goto out;
174 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
175 split_one(f, name, 1);
177 fclose(f);
180 ret = skip;
181 out:
182 string_list_clear(&list, 1);
183 return ret;
186 static int split_mbox(const char *file, const char *dir, int allow_bare,
187 int nr_prec, int skip)
189 char name[PATH_MAX];
190 int ret = -1;
191 int peek;
193 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
194 int file_done = 0;
196 if (!f) {
197 error("cannot open mbox %s", file);
198 goto out;
201 do {
202 peek = fgetc(f);
203 } while (isspace(peek));
204 ungetc(peek, f);
206 if (fgets(buf, sizeof(buf), f) == NULL) {
207 /* empty stdin is OK */
208 if (f != stdin) {
209 error("cannot read mbox %s", file);
210 goto out;
212 file_done = 1;
215 while (!file_done) {
216 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
217 file_done = split_one(f, name, allow_bare);
220 if (f != stdin)
221 fclose(f);
223 ret = skip;
224 out:
225 return ret;
228 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
230 int nr = 0, nr_prec = 4, num = 0;
231 int allow_bare = 0;
232 const char *dir = NULL;
233 const char **argp;
234 static const char *stdin_only[] = { "-", NULL };
236 for (argp = argv+1; *argp; argp++) {
237 const char *arg = *argp;
239 if (arg[0] != '-')
240 break;
241 /* do flags here */
242 if ( arg[1] == 'd' ) {
243 nr_prec = strtol(arg+2, NULL, 10);
244 if (nr_prec < 3 || 10 <= nr_prec)
245 usage(git_mailsplit_usage);
246 continue;
247 } else if ( arg[1] == 'f' ) {
248 nr = strtol(arg+2, NULL, 10);
249 } else if ( arg[1] == 'b' && !arg[2] ) {
250 allow_bare = 1;
251 } else if ( arg[1] == 'o' && arg[2] ) {
252 dir = arg+2;
253 } else if ( arg[1] == '-' && !arg[2] ) {
254 argp++; /* -- marks end of options */
255 break;
256 } else {
257 die("unknown option: %s", arg);
261 if ( !dir ) {
262 /* Backwards compatibility: if no -o specified, accept
263 <mbox> <dir> or just <dir> */
264 switch (argc - (argp-argv)) {
265 case 1:
266 dir = argp[0];
267 argp = stdin_only;
268 break;
269 case 2:
270 stdin_only[0] = argp[0];
271 dir = argp[1];
272 argp = stdin_only;
273 break;
274 default:
275 usage(git_mailsplit_usage);
277 } else {
278 /* New usage: if no more argument, parse stdin */
279 if ( !*argp )
280 argp = stdin_only;
283 while (*argp) {
284 const char *arg = *argp++;
285 struct stat argstat;
286 int ret = 0;
288 if (arg[0] == '-' && arg[1] == 0) {
289 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
290 if (ret < 0) {
291 error("cannot split patches from stdin");
292 return 1;
294 num += (ret - nr);
295 nr = ret;
296 continue;
299 if (stat(arg, &argstat) == -1) {
300 error("cannot stat %s (%s)", arg, strerror(errno));
301 return 1;
304 if (S_ISDIR(argstat.st_mode))
305 ret = split_maildir(arg, dir, nr_prec, nr);
306 else
307 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
309 if (ret < 0) {
310 error("cannot split patches from %s", arg);
311 return 1;
313 num += (ret - nr);
314 nr = ret;
317 printf("%d\n", num);
319 return 0;