builtin-mailinfo,builtin-mailsplit: use strbufs
[git/mjg.git] / builtin-mailsplit.c
blobc2ec6ea3d51e760581dcb6f86c1783a545ebc07a
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"
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)
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;
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;
46 static struct strbuf buf = STRBUF_INIT;
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 fd;
75 int status = 0;
76 int is_bare = !is_from_line(buf.buf, buf.len);
78 if (is_bare && !allow_bare)
79 goto corrupt;
81 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
82 if (fd < 0)
83 die_errno("cannot open output file '%s'", name);
84 output = fdopen(fd, "w");
86 /* Copy it out, while searching for a line that begins with
87 * "From " and having something that looks like a date format.
89 for (;;) {
90 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
91 die_errno("cannot write output");
93 if (strbuf_getwholeline(&buf, mbox, '\n')) {
94 if (feof(mbox)) {
95 status = 1;
96 break;
98 die_errno("cannot read mbox");
100 if (!is_bare && is_from_line(buf.buf, buf.len))
101 break; /* done with one message */
103 fclose(output);
104 return status;
106 corrupt:
107 if (output)
108 fclose(output);
109 unlink(name);
110 fprintf(stderr, "corrupt mailbox\n");
111 exit(1);
114 static int populate_maildir_list(struct string_list *list, const char *path)
116 DIR *dir;
117 struct dirent *dent;
118 char name[PATH_MAX];
119 char *subs[] = { "cur", "new", NULL };
120 char **sub;
122 for (sub = subs; *sub; ++sub) {
123 snprintf(name, sizeof(name), "%s/%s", path, *sub);
124 if ((dir = opendir(name)) == NULL) {
125 if (errno == ENOENT)
126 continue;
127 error("cannot opendir %s (%s)", name, strerror(errno));
128 return -1;
131 while ((dent = readdir(dir)) != NULL) {
132 if (dent->d_name[0] == '.')
133 continue;
134 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
135 string_list_insert(name, list);
138 closedir(dir);
141 return 0;
144 static int split_maildir(const char *maildir, const char *dir,
145 int nr_prec, int skip)
147 char file[PATH_MAX];
148 char name[PATH_MAX];
149 int ret = -1;
150 int i;
151 struct string_list list = {NULL, 0, 0, 1};
153 if (populate_maildir_list(&list, maildir) < 0)
154 goto out;
156 for (i = 0; i < list.nr; i++) {
157 FILE *f;
158 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
159 f = fopen(file, "r");
160 if (!f) {
161 error("cannot open mail %s (%s)", file, strerror(errno));
162 goto out;
165 if (strbuf_getwholeline(&buf, f, '\n')) {
166 error("cannot read mail %s (%s)", file, strerror(errno));
167 goto out;
170 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
171 split_one(f, name, 1);
173 fclose(f);
176 ret = skip;
177 out:
178 string_list_clear(&list, 1);
179 return ret;
182 static int split_mbox(const char *file, const char *dir, int allow_bare,
183 int nr_prec, int skip)
185 char name[PATH_MAX];
186 int ret = -1;
187 int peek;
189 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
190 int file_done = 0;
192 if (!f) {
193 error("cannot open mbox %s", file);
194 goto out;
197 do {
198 peek = fgetc(f);
199 } while (isspace(peek));
200 ungetc(peek, f);
202 if (strbuf_getwholeline(&buf, f, '\n')) {
203 /* empty stdin is OK */
204 if (f != stdin) {
205 error("cannot read mbox %s", file);
206 goto out;
208 file_done = 1;
211 while (!file_done) {
212 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
213 file_done = split_one(f, name, allow_bare);
216 if (f != stdin)
217 fclose(f);
219 ret = skip;
220 out:
221 return ret;
224 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
226 int nr = 0, nr_prec = 4, num = 0;
227 int allow_bare = 0;
228 const char *dir = NULL;
229 const char **argp;
230 static const char *stdin_only[] = { "-", NULL };
232 for (argp = argv+1; *argp; argp++) {
233 const char *arg = *argp;
235 if (arg[0] != '-')
236 break;
237 /* do flags here */
238 if ( arg[1] == 'd' ) {
239 nr_prec = strtol(arg+2, NULL, 10);
240 if (nr_prec < 3 || 10 <= nr_prec)
241 usage(git_mailsplit_usage);
242 continue;
243 } else if ( arg[1] == 'f' ) {
244 nr = strtol(arg+2, NULL, 10);
245 } else if ( arg[1] == 'b' && !arg[2] ) {
246 allow_bare = 1;
247 } else if ( arg[1] == 'o' && arg[2] ) {
248 dir = arg+2;
249 } else if ( arg[1] == '-' && !arg[2] ) {
250 argp++; /* -- marks end of options */
251 break;
252 } else {
253 die("unknown option: %s", arg);
257 if ( !dir ) {
258 /* Backwards compatibility: if no -o specified, accept
259 <mbox> <dir> or just <dir> */
260 switch (argc - (argp-argv)) {
261 case 1:
262 dir = argp[0];
263 argp = stdin_only;
264 break;
265 case 2:
266 stdin_only[0] = argp[0];
267 dir = argp[1];
268 argp = stdin_only;
269 break;
270 default:
271 usage(git_mailsplit_usage);
273 } else {
274 /* New usage: if no more argument, parse stdin */
275 if ( !*argp )
276 argp = stdin_only;
279 while (*argp) {
280 const char *arg = *argp++;
281 struct stat argstat;
282 int ret = 0;
284 if (arg[0] == '-' && arg[1] == 0) {
285 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
286 if (ret < 0) {
287 error("cannot split patches from stdin");
288 return 1;
290 num += (ret - nr);
291 nr = ret;
292 continue;
295 if (stat(arg, &argstat) == -1) {
296 error("cannot stat %s (%s)", arg, strerror(errno));
297 return 1;
300 if (S_ISDIR(argstat.st_mode))
301 ret = split_maildir(arg, dir, nr_prec, nr);
302 else
303 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
305 if (ret < 0) {
306 error("cannot split patches from %s", arg);
307 return 1;
309 num += (ret - nr);
310 nr = ret;
313 printf("%d\n", num);
315 return 0;