]>
Commit | Line | Data |
---|---|---|
2744b234 LT |
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 | */ | |
8b73edf4 | 7 | #include "cache.h" |
e690e843 | 8 | #include "builtin.h" |
d63bd9a2 | 9 | #include "path-list.h" |
2744b234 | 10 | |
8b73edf4 | 11 | static const char git_mailsplit_usage[] = |
d63bd9a2 | 12 | "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>|<Maildir>..."; |
2744b234 LT |
13 | |
14 | static int is_from_line(const char *line, int len) | |
15 | { | |
16 | const char *colon; | |
17 | ||
18 | if (len < 20 || memcmp("From ", line, 5)) | |
19 | return 0; | |
20 | ||
21 | colon = line + len - 2; | |
22 | line += 5; | |
23 | for (;;) { | |
24 | if (colon < line) | |
25 | return 0; | |
26 | if (*--colon == ':') | |
27 | break; | |
28 | } | |
29 | ||
30 | if (!isdigit(colon[-4]) || | |
31 | !isdigit(colon[-2]) || | |
32 | !isdigit(colon[-1]) || | |
33 | !isdigit(colon[ 1]) || | |
34 | !isdigit(colon[ 2])) | |
35 | return 0; | |
36 | ||
37 | /* year */ | |
38 | if (strtol(colon+3, NULL, 10) <= 90) | |
39 | return 0; | |
40 | ||
41 | /* Ok, close enough */ | |
42 | return 1; | |
43 | } | |
44 | ||
8b73edf4 JH |
45 | /* Could be as small as 64, enough to hold a Unix "From " line. */ |
46 | static char buf[4096]; | |
47 | ||
cce8d6fd JS |
48 | /* We cannot use fgets() because our lines can contain NULs */ |
49 | int read_line_with_nul(char *buf, int size, FILE *in) | |
50 | { | |
51 | int len = 0, c; | |
52 | ||
53 | for (;;) { | |
54 | c = getc(in); | |
edc55941 JH |
55 | if (c == EOF) |
56 | break; | |
cce8d6fd | 57 | buf[len++] = c; |
edc55941 | 58 | if (c == '\n' || len + 1 >= size) |
cce8d6fd JS |
59 | break; |
60 | } | |
cce8d6fd JS |
61 | buf[len] = '\0'; |
62 | ||
63 | return len; | |
64 | } | |
65 | ||
8b73edf4 JH |
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. | |
70 | */ | |
b3f041fb | 71 | static int split_one(FILE *mbox, const char *name, int allow_bare) |
2744b234 | 72 | { |
8b73edf4 JH |
73 | FILE *output = NULL; |
74 | int len = strlen(buf); | |
75 | int fd; | |
76 | int status = 0; | |
b3f041fb | 77 | int is_bare = !is_from_line(buf, len); |
2744b234 | 78 | |
b3f041fb | 79 | if (is_bare && !allow_bare) |
2744b234 LT |
80 | goto corrupt; |
81 | ||
8b73edf4 JH |
82 | fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); |
83 | if (fd < 0) | |
84 | die("cannot open output file %s", name); | |
85 | output = fdopen(fd, "w"); | |
2744b234 | 86 | |
8b73edf4 JH |
87 | /* Copy it out, while searching for a line that begins with |
88 | * "From " and having something that looks like a date format. | |
2744b234 | 89 | */ |
8b73edf4 | 90 | for (;;) { |
cce8d6fd | 91 | int is_partial = len && buf[len-1] != '\n'; |
8b73edf4 | 92 | |
cce8d6fd | 93 | if (fwrite(buf, 1, len, output) != len) |
8b73edf4 JH |
94 | die("cannot write output"); |
95 | ||
cce8d6fd JS |
96 | len = read_line_with_nul(buf, sizeof(buf), mbox); |
97 | if (len == 0) { | |
8b73edf4 JH |
98 | if (feof(mbox)) { |
99 | status = 1; | |
100 | break; | |
101 | } | |
102 | die("cannot read mbox"); | |
103 | } | |
b3f041fb | 104 | if (!is_partial && !is_bare && is_from_line(buf, len)) |
8b73edf4 JH |
105 | break; /* done with one message */ |
106 | } | |
107 | fclose(output); | |
108 | return status; | |
109 | ||
110 | corrupt: | |
111 | if (output) | |
112 | fclose(output); | |
113 | unlink(name); | |
2744b234 LT |
114 | fprintf(stderr, "corrupt mailbox\n"); |
115 | exit(1); | |
116 | } | |
117 | ||
d63bd9a2 | 118 | static int populate_maildir_list(struct path_list *list, const char *path) |
2744b234 | 119 | { |
d63bd9a2 FP |
120 | DIR *dir; |
121 | struct dirent *dent; | |
d50a4bc4 GP |
122 | char name[PATH_MAX]; |
123 | char *subs[] = { "cur", "new", NULL }; | |
124 | char **sub; | |
125 | ||
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; | |
133 | } | |
d63bd9a2 | 134 | |
d50a4bc4 GP |
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 | path_list_insert(name, list); | |
140 | } | |
d63bd9a2 | 141 | |
d50a4bc4 | 142 | closedir(dir); |
d63bd9a2 FP |
143 | } |
144 | ||
d63bd9a2 FP |
145 | return 0; |
146 | } | |
147 | ||
148 | static int split_maildir(const char *maildir, const char *dir, | |
149 | int nr_prec, int skip) | |
150 | { | |
151 | char file[PATH_MAX]; | |
d63bd9a2 | 152 | char name[PATH_MAX]; |
e690e843 | 153 | int ret = -1; |
d63bd9a2 FP |
154 | int i; |
155 | struct path_list list = {NULL, 0, 0, 1}; | |
e690e843 | 156 | |
d50a4bc4 | 157 | if (populate_maildir_list(&list, maildir) < 0) |
d63bd9a2 | 158 | goto out; |
e690e843 | 159 | |
d63bd9a2 FP |
160 | for (i = 0; i < list.nr; i++) { |
161 | FILE *f; | |
d50a4bc4 | 162 | snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].path); |
d63bd9a2 FP |
163 | f = fopen(file, "r"); |
164 | if (!f) { | |
165 | error("cannot open mail %s (%s)", file, strerror(errno)); | |
e690e843 LS |
166 | goto out; |
167 | } | |
168 | ||
169 | if (fgets(buf, sizeof(buf), f) == NULL) { | |
d63bd9a2 | 170 | error("cannot read mail %s (%s)", file, strerror(errno)); |
e690e843 LS |
171 | goto out; |
172 | } | |
173 | ||
d63bd9a2 FP |
174 | sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); |
175 | split_one(f, name, 1); | |
176 | ||
177 | fclose(f); | |
178 | } | |
179 | ||
d63bd9a2 FP |
180 | ret = skip; |
181 | out: | |
d50a4bc4 | 182 | path_list_clear(&list, 1); |
d63bd9a2 FP |
183 | return ret; |
184 | } | |
185 | ||
fcd056a6 JH |
186 | static int split_mbox(const char *file, const char *dir, int allow_bare, |
187 | int nr_prec, int skip) | |
d63bd9a2 FP |
188 | { |
189 | char name[PATH_MAX]; | |
190 | int ret = -1; | |
f88a545a | 191 | int peek; |
d63bd9a2 FP |
192 | |
193 | FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r"); | |
194 | int file_done = 0; | |
195 | ||
196 | if (!f) { | |
197 | error("cannot open mbox %s", file); | |
198 | goto out; | |
199 | } | |
200 | ||
f88a545a SS |
201 | do { |
202 | peek = fgetc(f); | |
203 | } while (isspace(peek)); | |
204 | ungetc(peek, f); | |
205 | ||
d63bd9a2 FP |
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; | |
e690e843 | 211 | } |
d63bd9a2 FP |
212 | file_done = 1; |
213 | } | |
e690e843 | 214 | |
d63bd9a2 FP |
215 | while (!file_done) { |
216 | sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); | |
217 | file_done = split_one(f, name, allow_bare); | |
e690e843 | 218 | } |
d63bd9a2 FP |
219 | |
220 | if (f != stdin) | |
221 | fclose(f); | |
222 | ||
e690e843 LS |
223 | ret = skip; |
224 | out: | |
e690e843 LS |
225 | return ret; |
226 | } | |
d63bd9a2 | 227 | |
a633fca0 | 228 | int cmd_mailsplit(int argc, const char **argv, const char *prefix) |
e690e843 | 229 | { |
d63bd9a2 | 230 | int nr = 0, nr_prec = 4, num = 0; |
b3f041fb PA |
231 | int allow_bare = 0; |
232 | const char *dir = NULL; | |
233 | const char **argp; | |
234 | static const char *stdin_only[] = { "-", NULL }; | |
8b73edf4 | 235 | |
b3f041fb PA |
236 | for (argp = argv+1; *argp; argp++) { |
237 | const char *arg = *argp; | |
8b73edf4 JH |
238 | |
239 | if (arg[0] != '-') | |
240 | break; | |
241 | /* do flags here */ | |
b3f041fb PA |
242 | if ( arg[1] == 'd' ) { |
243 | nr_prec = strtol(arg+2, NULL, 10); | |
8b73edf4 JH |
244 | if (nr_prec < 3 || 10 <= nr_prec) |
245 | usage(git_mailsplit_usage); | |
246 | continue; | |
b3f041fb PA |
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); | |
8b73edf4 | 258 | } |
2744b234 | 259 | } |
8b73edf4 | 260 | |
b3f041fb PA |
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); | |
276 | } | |
277 | } else { | |
278 | /* New usage: if no more argument, parse stdin */ | |
279 | if ( !*argp ) | |
280 | argp = stdin_only; | |
2744b234 | 281 | } |
8b73edf4 | 282 | |
d63bd9a2 FP |
283 | while (*argp) { |
284 | const char *arg = *argp++; | |
285 | struct stat argstat; | |
286 | int ret = 0; | |
287 | ||
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; | |
293 | } | |
b3327180 JH |
294 | num += (ret - nr); |
295 | nr = ret; | |
d63bd9a2 FP |
296 | continue; |
297 | } | |
298 | ||
299 | if (stat(arg, &argstat) == -1) { | |
300 | error("cannot stat %s (%s)", arg, strerror(errno)); | |
301 | return 1; | |
302 | } | |
303 | ||
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); | |
308 | ||
309 | if (ret < 0) { | |
310 | error("cannot split patches from %s", arg); | |
311 | return 1; | |
312 | } | |
b3327180 JH |
313 | num += (ret - nr); |
314 | nr = ret; | |
d63bd9a2 FP |
315 | } |
316 | ||
317 | printf("%d\n", num); | |
b3f041fb | 318 | |
d63bd9a2 | 319 | return 0; |
2744b234 | 320 | } |