]>
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 | ||
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 | */ | |
b3f041fb | 53 | static int split_one(FILE *mbox, const char *name, int allow_bare) |
2744b234 | 54 | { |
8b73edf4 JH |
55 | FILE *output = NULL; |
56 | int len = strlen(buf); | |
57 | int fd; | |
58 | int status = 0; | |
b3f041fb | 59 | int is_bare = !is_from_line(buf, len); |
2744b234 | 60 | |
b3f041fb | 61 | if (is_bare && !allow_bare) |
2744b234 LT |
62 | goto corrupt; |
63 | ||
8b73edf4 JH |
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"); | |
2744b234 | 68 | |
8b73edf4 JH |
69 | /* Copy it out, while searching for a line that begins with |
70 | * "From " and having something that looks like a date format. | |
2744b234 | 71 | */ |
8b73edf4 JH |
72 | for (;;) { |
73 | int is_partial = (buf[len-1] != '\n'); | |
74 | ||
75 | if (fputs(buf, output) == EOF) | |
76 | die("cannot write output"); | |
77 | ||
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); | |
b3f041fb | 86 | if (!is_partial && !is_bare && is_from_line(buf, len)) |
8b73edf4 JH |
87 | break; /* done with one message */ |
88 | } | |
89 | fclose(output); | |
90 | return status; | |
91 | ||
92 | corrupt: | |
93 | if (output) | |
94 | fclose(output); | |
95 | unlink(name); | |
2744b234 LT |
96 | fprintf(stderr, "corrupt mailbox\n"); |
97 | exit(1); | |
98 | } | |
99 | ||
d63bd9a2 | 100 | static int populate_maildir_list(struct path_list *list, const char *path) |
2744b234 | 101 | { |
d63bd9a2 FP |
102 | DIR *dir; |
103 | struct dirent *dent; | |
d50a4bc4 GP |
104 | char name[PATH_MAX]; |
105 | char *subs[] = { "cur", "new", NULL }; | |
106 | char **sub; | |
107 | ||
108 | for (sub = subs; *sub; ++sub) { | |
109 | snprintf(name, sizeof(name), "%s/%s", path, *sub); | |
110 | if ((dir = opendir(name)) == NULL) { | |
111 | if (errno == ENOENT) | |
112 | continue; | |
113 | error("cannot opendir %s (%s)", name, strerror(errno)); | |
114 | return -1; | |
115 | } | |
d63bd9a2 | 116 | |
d50a4bc4 GP |
117 | while ((dent = readdir(dir)) != NULL) { |
118 | if (dent->d_name[0] == '.') | |
119 | continue; | |
120 | snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name); | |
121 | path_list_insert(name, list); | |
122 | } | |
d63bd9a2 | 123 | |
d50a4bc4 | 124 | closedir(dir); |
d63bd9a2 FP |
125 | } |
126 | ||
d63bd9a2 FP |
127 | return 0; |
128 | } | |
129 | ||
130 | static int split_maildir(const char *maildir, const char *dir, | |
131 | int nr_prec, int skip) | |
132 | { | |
133 | char file[PATH_MAX]; | |
d63bd9a2 | 134 | char name[PATH_MAX]; |
e690e843 | 135 | int ret = -1; |
d63bd9a2 FP |
136 | int i; |
137 | struct path_list list = {NULL, 0, 0, 1}; | |
e690e843 | 138 | |
d50a4bc4 | 139 | if (populate_maildir_list(&list, maildir) < 0) |
d63bd9a2 | 140 | goto out; |
e690e843 | 141 | |
d63bd9a2 FP |
142 | for (i = 0; i < list.nr; i++) { |
143 | FILE *f; | |
d50a4bc4 | 144 | snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].path); |
d63bd9a2 FP |
145 | f = fopen(file, "r"); |
146 | if (!f) { | |
147 | error("cannot open mail %s (%s)", file, strerror(errno)); | |
e690e843 LS |
148 | goto out; |
149 | } | |
150 | ||
151 | if (fgets(buf, sizeof(buf), f) == NULL) { | |
d63bd9a2 | 152 | error("cannot read mail %s (%s)", file, strerror(errno)); |
e690e843 LS |
153 | goto out; |
154 | } | |
155 | ||
d63bd9a2 FP |
156 | sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); |
157 | split_one(f, name, 1); | |
158 | ||
159 | fclose(f); | |
160 | } | |
161 | ||
d63bd9a2 FP |
162 | ret = skip; |
163 | out: | |
d50a4bc4 | 164 | path_list_clear(&list, 1); |
d63bd9a2 FP |
165 | return ret; |
166 | } | |
167 | ||
fcd056a6 JH |
168 | static int split_mbox(const char *file, const char *dir, int allow_bare, |
169 | int nr_prec, int skip) | |
d63bd9a2 FP |
170 | { |
171 | char name[PATH_MAX]; | |
172 | int ret = -1; | |
f88a545a | 173 | int peek; |
d63bd9a2 FP |
174 | |
175 | FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r"); | |
176 | int file_done = 0; | |
177 | ||
178 | if (!f) { | |
179 | error("cannot open mbox %s", file); | |
180 | goto out; | |
181 | } | |
182 | ||
f88a545a SS |
183 | do { |
184 | peek = fgetc(f); | |
185 | } while (isspace(peek)); | |
186 | ungetc(peek, f); | |
187 | ||
d63bd9a2 FP |
188 | if (fgets(buf, sizeof(buf), f) == NULL) { |
189 | /* empty stdin is OK */ | |
190 | if (f != stdin) { | |
191 | error("cannot read mbox %s", file); | |
192 | goto out; | |
e690e843 | 193 | } |
d63bd9a2 FP |
194 | file_done = 1; |
195 | } | |
e690e843 | 196 | |
d63bd9a2 FP |
197 | while (!file_done) { |
198 | sprintf(name, "%s/%0*d", dir, nr_prec, ++skip); | |
199 | file_done = split_one(f, name, allow_bare); | |
e690e843 | 200 | } |
d63bd9a2 FP |
201 | |
202 | if (f != stdin) | |
203 | fclose(f); | |
204 | ||
e690e843 LS |
205 | ret = skip; |
206 | out: | |
e690e843 LS |
207 | return ret; |
208 | } | |
d63bd9a2 | 209 | |
a633fca0 | 210 | int cmd_mailsplit(int argc, const char **argv, const char *prefix) |
e690e843 | 211 | { |
d63bd9a2 | 212 | int nr = 0, nr_prec = 4, num = 0; |
b3f041fb PA |
213 | int allow_bare = 0; |
214 | const char *dir = NULL; | |
215 | const char **argp; | |
216 | static const char *stdin_only[] = { "-", NULL }; | |
8b73edf4 | 217 | |
b3f041fb PA |
218 | for (argp = argv+1; *argp; argp++) { |
219 | const char *arg = *argp; | |
8b73edf4 JH |
220 | |
221 | if (arg[0] != '-') | |
222 | break; | |
223 | /* do flags here */ | |
b3f041fb PA |
224 | if ( arg[1] == 'd' ) { |
225 | nr_prec = strtol(arg+2, NULL, 10); | |
8b73edf4 JH |
226 | if (nr_prec < 3 || 10 <= nr_prec) |
227 | usage(git_mailsplit_usage); | |
228 | continue; | |
b3f041fb PA |
229 | } else if ( arg[1] == 'f' ) { |
230 | nr = strtol(arg+2, NULL, 10); | |
231 | } else if ( arg[1] == 'b' && !arg[2] ) { | |
232 | allow_bare = 1; | |
233 | } else if ( arg[1] == 'o' && arg[2] ) { | |
234 | dir = arg+2; | |
235 | } else if ( arg[1] == '-' && !arg[2] ) { | |
236 | argp++; /* -- marks end of options */ | |
237 | break; | |
238 | } else { | |
239 | die("unknown option: %s", arg); | |
8b73edf4 | 240 | } |
2744b234 | 241 | } |
8b73edf4 | 242 | |
b3f041fb PA |
243 | if ( !dir ) { |
244 | /* Backwards compatibility: if no -o specified, accept | |
245 | <mbox> <dir> or just <dir> */ | |
246 | switch (argc - (argp-argv)) { | |
247 | case 1: | |
248 | dir = argp[0]; | |
249 | argp = stdin_only; | |
250 | break; | |
251 | case 2: | |
252 | stdin_only[0] = argp[0]; | |
253 | dir = argp[1]; | |
254 | argp = stdin_only; | |
255 | break; | |
256 | default: | |
257 | usage(git_mailsplit_usage); | |
258 | } | |
259 | } else { | |
260 | /* New usage: if no more argument, parse stdin */ | |
261 | if ( !*argp ) | |
262 | argp = stdin_only; | |
2744b234 | 263 | } |
8b73edf4 | 264 | |
d63bd9a2 FP |
265 | while (*argp) { |
266 | const char *arg = *argp++; | |
267 | struct stat argstat; | |
268 | int ret = 0; | |
269 | ||
270 | if (arg[0] == '-' && arg[1] == 0) { | |
271 | ret = split_mbox(arg, dir, allow_bare, nr_prec, nr); | |
272 | if (ret < 0) { | |
273 | error("cannot split patches from stdin"); | |
274 | return 1; | |
275 | } | |
b3327180 JH |
276 | num += (ret - nr); |
277 | nr = ret; | |
d63bd9a2 FP |
278 | continue; |
279 | } | |
280 | ||
281 | if (stat(arg, &argstat) == -1) { | |
282 | error("cannot stat %s (%s)", arg, strerror(errno)); | |
283 | return 1; | |
284 | } | |
285 | ||
286 | if (S_ISDIR(argstat.st_mode)) | |
287 | ret = split_maildir(arg, dir, nr_prec, nr); | |
288 | else | |
289 | ret = split_mbox(arg, dir, allow_bare, nr_prec, nr); | |
290 | ||
291 | if (ret < 0) { | |
292 | error("cannot split patches from %s", arg); | |
293 | return 1; | |
294 | } | |
b3327180 JH |
295 | num += (ret - nr); |
296 | nr = ret; | |
d63bd9a2 FP |
297 | } |
298 | ||
299 | printf("%d\n", num); | |
b3f041fb | 300 | |
d63bd9a2 | 301 | return 0; |
2744b234 | 302 | } |