git

Форк
0
/
mailsplit.c 
372 строки · 7.4 Кб
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
 */
7
#include "builtin.h"
8
#include "gettext.h"
9
#include "string-list.h"
10
#include "strbuf.h"
11

12
static const char git_mailsplit_usage[] =
13
"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
14

15
static 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

46
static struct strbuf buf = STRBUF_INIT;
47
static int keep_cr;
48
static int mboxrd;
49

50
static int is_gtfrom(const struct strbuf *buf)
51
{
52
	size_t min = strlen(">From ");
53
	size_t ngt;
54

55
	if (buf->len < min)
56
		return 0;
57

58
	ngt = strspn(buf->buf, ">");
59
	return ngt && starts_with(buf->buf + ngt, "From ");
60
}
61

62
/* Called with the first line (potentially partial)
63
 * already in buf[] -- normally that should begin with
64
 * the Unix "From " line.  Write it into the specified
65
 * file.
66
 */
67
static int split_one(FILE *mbox, const char *name, int allow_bare)
68
{
69
	FILE *output;
70
	int fd;
71
	int status = 0;
72
	int is_bare = !is_from_line(buf.buf, buf.len);
73

74
	if (is_bare && !allow_bare) {
75
		fprintf(stderr, "corrupt mailbox\n");
76
		exit(1);
77
	}
78
	fd = xopen(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
79
	output = xfdopen(fd, "w");
80

81
	/* Copy it out, while searching for a line that begins with
82
	 * "From " and having something that looks like a date format.
83
	 */
84
	for (;;) {
85
		if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
86
			buf.buf[buf.len-2] == '\r') {
87
			strbuf_setlen(&buf, buf.len-2);
88
			strbuf_addch(&buf, '\n');
89
		}
90

91
		if (mboxrd && is_gtfrom(&buf))
92
			strbuf_remove(&buf, 0, 1);
93

94
		if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
95
			die_errno("cannot write output");
96

97
		if (strbuf_getwholeline(&buf, mbox, '\n')) {
98
			if (feof(mbox)) {
99
				status = 1;
100
				break;
101
			}
102
			die_errno("cannot read mbox");
103
		}
104
		if (!is_bare && is_from_line(buf.buf, buf.len))
105
			break; /* done with one message */
106
	}
107
	fclose(output);
108
	return status;
109
}
110

111
static int populate_maildir_list(struct string_list *list, const char *path)
112
{
113
	DIR *dir;
114
	struct dirent *dent;
115
	char *name = NULL;
116
	const char *subs[] = { "cur", "new", NULL };
117
	const char **sub;
118
	int ret = -1;
119

120
	for (sub = subs; *sub; ++sub) {
121
		free(name);
122
		name = xstrfmt("%s/%s", path, *sub);
123
		if (!(dir = opendir(name))) {
124
			if (errno == ENOENT)
125
				continue;
126
			error_errno("cannot opendir %s", name);
127
			goto out;
128
		}
129

130
		while ((dent = readdir(dir)) != NULL) {
131
			if (dent->d_name[0] == '.')
132
				continue;
133
			free(name);
134
			name = xstrfmt("%s/%s", *sub, dent->d_name);
135
			string_list_insert(list, name);
136
		}
137

138
		closedir(dir);
139
	}
140

141
	ret = 0;
142

143
out:
144
	free(name);
145
	return ret;
146
}
147

148
static int maildir_filename_cmp(const char *a, const char *b)
149
{
150
	while (*a && *b) {
151
		if (isdigit(*a) && isdigit(*b)) {
152
			long int na, nb;
153
			na = strtol(a, (char **)&a, 10);
154
			nb = strtol(b, (char **)&b, 10);
155
			if (na != nb)
156
				return na - nb;
157
			/* strtol advanced our pointers */
158
		}
159
		else {
160
			if (*a != *b)
161
				return (unsigned char)*a - (unsigned char)*b;
162
			a++;
163
			b++;
164
		}
165
	}
166
	return (unsigned char)*a - (unsigned char)*b;
167
}
168

169
static int split_maildir(const char *maildir, const char *dir,
170
	int nr_prec, int skip)
171
{
172
	char *file = NULL;
173
	FILE *f = NULL;
174
	int ret = -1;
175
	int i;
176
	struct string_list list = STRING_LIST_INIT_DUP;
177

178
	list.cmp = maildir_filename_cmp;
179

180
	if (populate_maildir_list(&list, maildir) < 0)
181
		goto out;
182

183
	for (i = 0; i < list.nr; i++) {
184
		char *name;
185

186
		free(file);
187
		file = xstrfmt("%s/%s", maildir, list.items[i].string);
188

189
		f = fopen(file, "r");
190
		if (!f) {
191
			error_errno("cannot open mail %s", file);
192
			goto out;
193
		}
194

195
		if (strbuf_getwholeline(&buf, f, '\n')) {
196
			error_errno("cannot read mail %s", file);
197
			goto out;
198
		}
199

200
		name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
201
		split_one(f, name, 1);
202
		free(name);
203

204
		fclose(f);
205
		f = NULL;
206
	}
207

208
	ret = skip;
209
out:
210
	if (f)
211
		fclose(f);
212
	free(file);
213
	string_list_clear(&list, 1);
214
	return ret;
215
}
216

217
static int split_mbox(const char *file, const char *dir, int allow_bare,
218
		      int nr_prec, int skip)
219
{
220
	int ret = -1;
221
	int peek;
222

223
	FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
224
	int file_done = 0;
225

226
	if (isatty(fileno(f)))
227
		warning(_("reading patches from stdin/tty..."));
228

229
	if (!f) {
230
		error_errno("cannot open mbox %s", file);
231
		goto out;
232
	}
233

234
	do {
235
		peek = fgetc(f);
236
		if (peek == EOF) {
237
			if (f == stdin)
238
				/* empty stdin is OK */
239
				ret = skip;
240
			else {
241
				fclose(f);
242
				error(_("empty mbox: '%s'"), file);
243
			}
244
			goto out;
245
		}
246
	} while (isspace(peek));
247
	ungetc(peek, f);
248

249
	if (strbuf_getwholeline(&buf, f, '\n')) {
250
		/* empty stdin is OK */
251
		if (f != stdin) {
252
			error("cannot read mbox %s", file);
253
			goto out;
254
		}
255
		file_done = 1;
256
	}
257

258
	while (!file_done) {
259
		char *name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
260
		file_done = split_one(f, name, allow_bare);
261
		free(name);
262
	}
263

264
	if (f != stdin)
265
		fclose(f);
266

267
	ret = skip;
268
out:
269
	return ret;
270
}
271

272
int cmd_mailsplit(int argc, const char **argv, const char *prefix)
273
{
274
	int nr = 0, nr_prec = 4, num = 0;
275
	int allow_bare = 0;
276
	const char *dir = NULL;
277
	const char **argp;
278
	static const char *stdin_only[] = { "-", NULL };
279

280
	BUG_ON_NON_EMPTY_PREFIX(prefix);
281

282
	for (argp = argv+1; *argp; argp++) {
283
		const char *arg = *argp;
284

285
		if (arg[0] != '-')
286
			break;
287
		/* do flags here */
288
		if ( arg[1] == 'd' ) {
289
			nr_prec = strtol(arg+2, NULL, 10);
290
			if (nr_prec < 3 || 10 <= nr_prec)
291
				usage(git_mailsplit_usage);
292
			continue;
293
		} else if ( arg[1] == 'f' ) {
294
			nr = strtol(arg+2, NULL, 10);
295
		} else if ( arg[1] == 'h' ) {
296
			usage(git_mailsplit_usage);
297
		} else if ( arg[1] == 'b' && !arg[2] ) {
298
			allow_bare = 1;
299
		} else if (!strcmp(arg, "--keep-cr")) {
300
			keep_cr = 1;
301
		} else if ( arg[1] == 'o' && arg[2] ) {
302
			dir = arg+2;
303
		} else if (!strcmp(arg, "--mboxrd")) {
304
			mboxrd = 1;
305
		} else if ( arg[1] == '-' && !arg[2] ) {
306
			argp++;	/* -- marks end of options */
307
			break;
308
		} else {
309
			die("unknown option: %s", arg);
310
		}
311
	}
312

313
	if ( !dir ) {
314
		/* Backwards compatibility: if no -o specified, accept
315
		   <mbox> <dir> or just <dir> */
316
		switch (argc - (argp-argv)) {
317
		case 1:
318
			dir = argp[0];
319
			argp = stdin_only;
320
			break;
321
		case 2:
322
			stdin_only[0] = argp[0];
323
			dir = argp[1];
324
			argp = stdin_only;
325
			break;
326
		default:
327
			usage(git_mailsplit_usage);
328
		}
329
	} else {
330
		/* New usage: if no more argument, parse stdin */
331
		if ( !*argp )
332
			argp = stdin_only;
333
	}
334

335
	while (*argp) {
336
		const char *arg = *argp++;
337
		struct stat argstat;
338
		int ret = 0;
339

340
		if (arg[0] == '-' && arg[1] == 0) {
341
			ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
342
			if (ret < 0) {
343
				error("cannot split patches from stdin");
344
				return 1;
345
			}
346
			num += (ret - nr);
347
			nr = ret;
348
			continue;
349
		}
350

351
		if (stat(arg, &argstat) == -1) {
352
			error_errno("cannot stat %s", arg);
353
			return 1;
354
		}
355

356
		if (S_ISDIR(argstat.st_mode))
357
			ret = split_maildir(arg, dir, nr_prec, nr);
358
		else
359
			ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
360

361
		if (ret < 0) {
362
			error("cannot split patches from %s", arg);
363
			return 1;
364
		}
365
		num += (ret - nr);
366
		nr = ret;
367
	}
368

369
	printf("%d\n", num);
370

371
	return 0;
372
}
373

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.