]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-mailsplit.c
builtin-mailsplit.c: remove read_line_with_nul() since it is no longer used
[thirdparty/git.git] / builtin-mailsplit.c
CommitLineData
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"
c455c87c 9#include "string-list.h"
c8f373a5 10#include "strbuf.h"
2744b234 11
8b73edf4 12static const char git_mailsplit_usage[] =
588c038a 13"git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
2744b234
LT
14
15static int is_from_line(const char *line, int len)
16{
17 const char *colon;
18
19 if (len < 20 || memcmp("From ", line, 5))
20 return 0;
21
22 colon = line + len - 2;
23 line += 5;
24 for (;;) {
25 if (colon < line)
26 return 0;
27 if (*--colon == ':')
28 break;
29 }
30
31 if (!isdigit(colon[-4]) ||
32 !isdigit(colon[-2]) ||
33 !isdigit(colon[-1]) ||
34 !isdigit(colon[ 1]) ||
35 !isdigit(colon[ 2]))
36 return 0;
37
38 /* year */
39 if (strtol(colon+3, NULL, 10) <= 90)
40 return 0;
41
42 /* Ok, close enough */
43 return 1;
44}
45
c8f373a5 46static struct strbuf buf = STRBUF_INIT;
8b73edf4
JH
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 53static int split_one(FILE *mbox, const char *name, int allow_bare)
2744b234 54{
8b73edf4 55 FILE *output = NULL;
8b73edf4
JH
56 int fd;
57 int status = 0;
c8f373a5 58 int is_bare = !is_from_line(buf.buf, buf.len);
2744b234 59
b3f041fb 60 if (is_bare && !allow_bare)
2744b234
LT
61 goto corrupt;
62
8b73edf4
JH
63 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
64 if (fd < 0)
0721c314 65 die_errno("cannot open output file '%s'", name);
8b73edf4 66 output = fdopen(fd, "w");
2744b234 67
8b73edf4
JH
68 /* Copy it out, while searching for a line that begins with
69 * "From " and having something that looks like a date format.
2744b234 70 */
8b73edf4 71 for (;;) {
c8f373a5 72 if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
0721c314 73 die_errno("cannot write output");
8b73edf4 74
c8f373a5 75 if (strbuf_getwholeline(&buf, mbox, '\n')) {
8b73edf4
JH
76 if (feof(mbox)) {
77 status = 1;
78 break;
79 }
0721c314 80 die_errno("cannot read mbox");
8b73edf4 81 }
c8f373a5 82 if (!is_bare && is_from_line(buf.buf, buf.len))
8b73edf4
JH
83 break; /* done with one message */
84 }
85 fclose(output);
86 return status;
87
88 corrupt:
89 if (output)
90 fclose(output);
91 unlink(name);
2744b234
LT
92 fprintf(stderr, "corrupt mailbox\n");
93 exit(1);
94}
95
c455c87c 96static int populate_maildir_list(struct string_list *list, const char *path)
2744b234 97{
d63bd9a2
FP
98 DIR *dir;
99 struct dirent *dent;
d50a4bc4
GP
100 char name[PATH_MAX];
101 char *subs[] = { "cur", "new", NULL };
102 char **sub;
103
104 for (sub = subs; *sub; ++sub) {
105 snprintf(name, sizeof(name), "%s/%s", path, *sub);
106 if ((dir = opendir(name)) == NULL) {
107 if (errno == ENOENT)
108 continue;
109 error("cannot opendir %s (%s)", name, strerror(errno));
110 return -1;
111 }
d63bd9a2 112
d50a4bc4
GP
113 while ((dent = readdir(dir)) != NULL) {
114 if (dent->d_name[0] == '.')
115 continue;
116 snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
c455c87c 117 string_list_insert(name, list);
d50a4bc4 118 }
d63bd9a2 119
d50a4bc4 120 closedir(dir);
d63bd9a2
FP
121 }
122
d63bd9a2
FP
123 return 0;
124}
125
126static int split_maildir(const char *maildir, const char *dir,
127 int nr_prec, int skip)
128{
129 char file[PATH_MAX];
d63bd9a2 130 char name[PATH_MAX];
e690e843 131 int ret = -1;
d63bd9a2 132 int i;
c455c87c 133 struct string_list list = {NULL, 0, 0, 1};
e690e843 134
d50a4bc4 135 if (populate_maildir_list(&list, maildir) < 0)
d63bd9a2 136 goto out;
e690e843 137
d63bd9a2
FP
138 for (i = 0; i < list.nr; i++) {
139 FILE *f;
c455c87c 140 snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].string);
d63bd9a2
FP
141 f = fopen(file, "r");
142 if (!f) {
143 error("cannot open mail %s (%s)", file, strerror(errno));
e690e843
LS
144 goto out;
145 }
146
c8f373a5 147 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2 148 error("cannot read mail %s (%s)", file, strerror(errno));
e690e843
LS
149 goto out;
150 }
151
d63bd9a2
FP
152 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
153 split_one(f, name, 1);
154
155 fclose(f);
156 }
157
d63bd9a2
FP
158 ret = skip;
159out:
c455c87c 160 string_list_clear(&list, 1);
d63bd9a2
FP
161 return ret;
162}
163
fcd056a6
JH
164static int split_mbox(const char *file, const char *dir, int allow_bare,
165 int nr_prec, int skip)
d63bd9a2
FP
166{
167 char name[PATH_MAX];
168 int ret = -1;
f88a545a 169 int peek;
d63bd9a2
FP
170
171 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
172 int file_done = 0;
173
174 if (!f) {
175 error("cannot open mbox %s", file);
176 goto out;
177 }
178
f88a545a
SS
179 do {
180 peek = fgetc(f);
181 } while (isspace(peek));
182 ungetc(peek, f);
183
c8f373a5 184 if (strbuf_getwholeline(&buf, f, '\n')) {
d63bd9a2
FP
185 /* empty stdin is OK */
186 if (f != stdin) {
187 error("cannot read mbox %s", file);
188 goto out;
e690e843 189 }
d63bd9a2
FP
190 file_done = 1;
191 }
e690e843 192
d63bd9a2
FP
193 while (!file_done) {
194 sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
195 file_done = split_one(f, name, allow_bare);
e690e843 196 }
d63bd9a2
FP
197
198 if (f != stdin)
199 fclose(f);
200
e690e843
LS
201 ret = skip;
202out:
e690e843
LS
203 return ret;
204}
d63bd9a2 205
a633fca0 206int cmd_mailsplit(int argc, const char **argv, const char *prefix)
e690e843 207{
d63bd9a2 208 int nr = 0, nr_prec = 4, num = 0;
b3f041fb
PA
209 int allow_bare = 0;
210 const char *dir = NULL;
211 const char **argp;
212 static const char *stdin_only[] = { "-", NULL };
8b73edf4 213
b3f041fb
PA
214 for (argp = argv+1; *argp; argp++) {
215 const char *arg = *argp;
8b73edf4
JH
216
217 if (arg[0] != '-')
218 break;
219 /* do flags here */
b3f041fb
PA
220 if ( arg[1] == 'd' ) {
221 nr_prec = strtol(arg+2, NULL, 10);
8b73edf4
JH
222 if (nr_prec < 3 || 10 <= nr_prec)
223 usage(git_mailsplit_usage);
224 continue;
b3f041fb
PA
225 } else if ( arg[1] == 'f' ) {
226 nr = strtol(arg+2, NULL, 10);
227 } else if ( arg[1] == 'b' && !arg[2] ) {
228 allow_bare = 1;
229 } else if ( arg[1] == 'o' && arg[2] ) {
230 dir = arg+2;
231 } else if ( arg[1] == '-' && !arg[2] ) {
232 argp++; /* -- marks end of options */
233 break;
234 } else {
235 die("unknown option: %s", arg);
8b73edf4 236 }
2744b234 237 }
8b73edf4 238
b3f041fb
PA
239 if ( !dir ) {
240 /* Backwards compatibility: if no -o specified, accept
241 <mbox> <dir> or just <dir> */
242 switch (argc - (argp-argv)) {
243 case 1:
244 dir = argp[0];
245 argp = stdin_only;
246 break;
247 case 2:
248 stdin_only[0] = argp[0];
249 dir = argp[1];
250 argp = stdin_only;
251 break;
252 default:
253 usage(git_mailsplit_usage);
254 }
255 } else {
256 /* New usage: if no more argument, parse stdin */
257 if ( !*argp )
258 argp = stdin_only;
2744b234 259 }
8b73edf4 260
d63bd9a2
FP
261 while (*argp) {
262 const char *arg = *argp++;
263 struct stat argstat;
264 int ret = 0;
265
266 if (arg[0] == '-' && arg[1] == 0) {
267 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
268 if (ret < 0) {
269 error("cannot split patches from stdin");
270 return 1;
271 }
b3327180
JH
272 num += (ret - nr);
273 nr = ret;
d63bd9a2
FP
274 continue;
275 }
276
277 if (stat(arg, &argstat) == -1) {
278 error("cannot stat %s (%s)", arg, strerror(errno));
279 return 1;
280 }
281
282 if (S_ISDIR(argstat.st_mode))
283 ret = split_maildir(arg, dir, nr_prec, nr);
284 else
285 ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
286
287 if (ret < 0) {
288 error("cannot split patches from %s", arg);
289 return 1;
290 }
b3327180
JH
291 num += (ret - nr);
292 nr = ret;
d63bd9a2
FP
293 }
294
295 printf("%d\n", num);
b3f041fb 296
d63bd9a2 297 return 0;
2744b234 298}