Don't ever return corrupt objects from "parse_object()"
[git/jnareb-git.git] / builtin-mailsplit.c
blob3bca855aae857cde15f2a249f8e0a5c30b3e7d82
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"
10 static const char git_mailsplit_usage[] =
11 "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
13 static int is_from_line(const char *line, int len)
15 const char *colon;
17 if (len < 20 || memcmp("From ", line, 5))
18 return 0;
20 colon = line + len - 2;
21 line += 5;
22 for (;;) {
23 if (colon < line)
24 return 0;
25 if (*--colon == ':')
26 break;
29 if (!isdigit(colon[-4]) ||
30 !isdigit(colon[-2]) ||
31 !isdigit(colon[-1]) ||
32 !isdigit(colon[ 1]) ||
33 !isdigit(colon[ 2]))
34 return 0;
36 /* year */
37 if (strtol(colon+3, NULL, 10) <= 90)
38 return 0;
40 /* Ok, close enough */
41 return 1;
44 /* Could be as small as 64, enough to hold a Unix "From " line. */
45 static char buf[4096];
47 /* Called with the first line (potentially partial)
48 * already in buf[] -- normally that should begin with
49 * the Unix "From " line. Write it into the specified
50 * file.
52 static int split_one(FILE *mbox, const char *name, int allow_bare)
54 FILE *output = NULL;
55 int len = strlen(buf);
56 int fd;
57 int status = 0;
58 int is_bare = !is_from_line(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("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.
71 for (;;) {
72 int is_partial = (buf[len-1] != '\n');
74 if (fputs(buf, output) == EOF)
75 die("cannot write output");
77 if (fgets(buf, sizeof(buf), mbox) == NULL) {
78 if (feof(mbox)) {
79 status = 1;
80 break;
82 die("cannot read mbox");
84 len = strlen(buf);
85 if (!is_partial && !is_bare && is_from_line(buf, len))
86 break; /* done with one message */
88 fclose(output);
89 return status;
91 corrupt:
92 if (output)
93 fclose(output);
94 unlink(name);
95 fprintf(stderr, "corrupt mailbox\n");
96 exit(1);
99 int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
101 char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
102 int ret = -1;
104 while (*mbox) {
105 const char *file = *mbox++;
106 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
107 int file_done = 0;
109 if ( !f ) {
110 error("cannot open mbox %s", file);
111 goto out;
114 if (fgets(buf, sizeof(buf), f) == NULL) {
115 if (f == stdin)
116 break; /* empty stdin is OK */
117 error("cannot read mbox %s", file);
118 goto out;
121 while (!file_done) {
122 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
123 file_done = split_one(f, name, allow_bare);
126 if (f != stdin)
127 fclose(f);
129 ret = skip;
130 out:
131 free(name);
132 return ret;
134 int cmd_mailsplit(int argc, const char **argv, const char *prefix)
136 int nr = 0, nr_prec = 4, ret;
137 int allow_bare = 0;
138 const char *dir = NULL;
139 const char **argp;
140 static const char *stdin_only[] = { "-", NULL };
142 for (argp = argv+1; *argp; argp++) {
143 const char *arg = *argp;
145 if (arg[0] != '-')
146 break;
147 /* do flags here */
148 if ( arg[1] == 'd' ) {
149 nr_prec = strtol(arg+2, NULL, 10);
150 if (nr_prec < 3 || 10 <= nr_prec)
151 usage(git_mailsplit_usage);
152 continue;
153 } else if ( arg[1] == 'f' ) {
154 nr = strtol(arg+2, NULL, 10);
155 } else if ( arg[1] == 'b' && !arg[2] ) {
156 allow_bare = 1;
157 } else if ( arg[1] == 'o' && arg[2] ) {
158 dir = arg+2;
159 } else if ( arg[1] == '-' && !arg[2] ) {
160 argp++; /* -- marks end of options */
161 break;
162 } else {
163 die("unknown option: %s", arg);
167 if ( !dir ) {
168 /* Backwards compatibility: if no -o specified, accept
169 <mbox> <dir> or just <dir> */
170 switch (argc - (argp-argv)) {
171 case 1:
172 dir = argp[0];
173 argp = stdin_only;
174 break;
175 case 2:
176 stdin_only[0] = argp[0];
177 dir = argp[1];
178 argp = stdin_only;
179 break;
180 default:
181 usage(git_mailsplit_usage);
183 } else {
184 /* New usage: if no more argument, parse stdin */
185 if ( !*argp )
186 argp = stdin_only;
189 ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
190 if (ret != -1)
191 printf("%d\n", ret);
193 return ret == -1;