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 "path-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)
15 {
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;
28 }
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;
43 }
45 /* Could be as small as 64, enough to hold a Unix "From " line. */
46 static char buf[4096];
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 len = strlen(buf);
57 int fd;
58 int status = 0;
59 int is_bare = !is_from_line(buf, len);
61 if (is_bare && !allow_bare)
62 goto corrupt;
64 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
65 if (fd < 0)
66 die("cannot open output file %s", name);
67 output = fdopen(fd, "w");
69 /* Copy it out, while searching for a line that begins with
70 * "From " and having something that looks like a date format.
71 */
72 for (;;) {
73 int is_partial = (buf[len-1] != '\n');
75 if (fputs(buf, output) == EOF)
76 die("cannot write output");
78 if (fgets(buf, sizeof(buf), mbox) == NULL) {
79 if (feof(mbox)) {
80 status = 1;
81 break;
82 }
83 die("cannot read mbox");
84 }
85 len = strlen(buf);
86 if (!is_partial && !is_bare && is_from_line(buf, len))
87 break; /* done with one message */
88 }
89 fclose(output);
90 return status;
92 corrupt:
93 if (output)
94 fclose(output);
95 unlink(name);
96 fprintf(stderr, "corrupt mailbox\n");
97 exit(1);
98 }
100 static int populate_maildir_list(struct path_list *list, const char *path)
101 {
102 DIR *dir;
103 struct dirent *dent;
105 if ((dir = opendir(path)) == NULL) {
106 error("cannot opendir %s (%s)", path, strerror(errno));
107 return -1;
108 }
110 while ((dent = readdir(dir)) != NULL) {
111 if (dent->d_name[0] == '.')
112 continue;
113 path_list_insert(dent->d_name, list);
114 }
116 closedir(dir);
118 return 0;
119 }
121 static int split_maildir(const char *maildir, const char *dir,
122 int nr_prec, int skip)
123 {
124 char file[PATH_MAX];
125 char curdir[PATH_MAX];
126 char name[PATH_MAX];
127 int ret = -1;
128 int i;
129 struct path_list list = {NULL, 0, 0, 1};
131 snprintf(curdir, sizeof(curdir), "%s/cur", maildir);
132 if (populate_maildir_list(&list, curdir) < 0)
133 goto out;
135 for (i = 0; i < list.nr; i++) {
136 FILE *f;
137 snprintf(file, sizeof(file), "%s/%s", curdir, list.items[i].path);
138 f = fopen(file, "r");
139 if (!f) {
140 error("cannot open mail %s (%s)", file, strerror(errno));
141 goto out;
142 }
144 if (fgets(buf, sizeof(buf), f) == NULL) {
145 error("cannot read mail %s (%s)", file, strerror(errno));
146 goto out;
147 }
149 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
150 split_one(f, name, 1);
152 fclose(f);
153 }
155 path_list_clear(&list, 1);
157 ret = skip;
158 out:
159 return ret;
160 }
162 static int split_mbox(const char *file, const char *dir, int allow_bare,
163 int nr_prec, int skip)
164 {
165 char name[PATH_MAX];
166 int ret = -1;
167 int peek;
169 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
170 int file_done = 0;
172 if (!f) {
173 error("cannot open mbox %s", file);
174 goto out;
175 }
177 do {
178 peek = fgetc(f);
179 } while (isspace(peek));
180 ungetc(peek, f);
182 if (fgets(buf, sizeof(buf), f) == NULL) {
183 /* empty stdin is OK */
184 if (f != stdin) {
185 error("cannot read mbox %s", file);
186 goto out;
187 }
188 file_done = 1;
189 }
191 while (!file_done) {
192 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
193 file_done = split_one(f, name, allow_bare);
194 }
196 if (f != stdin)
197 fclose(f);
199 ret = skip;
200 out:
201 return ret;
202 }
204 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
205 {
206 int nr = 0, nr_prec = 4, num = 0;
207 int allow_bare = 0;
208 const char *dir = NULL;
209 const char **argp;
210 static const char *stdin_only[] = { "-", NULL };
212 for (argp = argv+1; *argp; argp++) {
213 const char *arg = *argp;
215 if (arg[0] != '-')
216 break;
217 /* do flags here */
218 if ( arg[1] == 'd' ) {
219 nr_prec = strtol(arg+2, NULL, 10);
220 if (nr_prec < 3 || 10 <= nr_prec)
221 usage(git_mailsplit_usage);
222 continue;
223 } else if ( arg[1] == 'f' ) {
224 nr = strtol(arg+2, NULL, 10);
225 } else if ( arg[1] == 'b' && !arg[2] ) {
226 allow_bare = 1;
227 } else if ( arg[1] == 'o' && arg[2] ) {
228 dir = arg+2;
229 } else if ( arg[1] == '-' && !arg[2] ) {
230 argp++; /* -- marks end of options */
231 break;
232 } else {
233 die("unknown option: %s", arg);
234 }
235 }
237 if ( !dir ) {
238 /* Backwards compatibility: if no -o specified, accept
239 <mbox> <dir> or just <dir> */
240 switch (argc - (argp-argv)) {
241 case 1:
242 dir = argp[0];
243 argp = stdin_only;
244 break;
245 case 2:
246 stdin_only[0] = argp[0];
247 dir = argp[1];
248 argp = stdin_only;
249 break;
250 default:
251 usage(git_mailsplit_usage);
252 }
253 } else {
254 /* New usage: if no more argument, parse stdin */
255 if ( !*argp )
256 argp = stdin_only;
257 }
259 while (*argp) {
260 const char *arg = *argp++;
261 struct stat argstat;
262 int ret = 0;
264 if (arg[0] == '-' && arg[1] == 0) {
265 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
266 if (ret < 0) {
267 error("cannot split patches from stdin");
268 return 1;
269 }
270 num += (ret - nr);
271 nr = ret;
272 continue;
273 }
275 if (stat(arg, &argstat) == -1) {
276 error("cannot stat %s (%s)", arg, strerror(errno));
277 return 1;
278 }
280 if (S_ISDIR(argstat.st_mode))
281 ret = split_maildir(arg, dir, nr_prec, nr);
282 else
283 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
285 if (ret < 0) {
286 error("cannot split patches from %s", arg);
287 return 1;
288 }
289 num += (ret - nr);
290 nr = ret;
291 }
293 printf("%d\n", num);
295 return 0;
296 }