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.
9 #include "string-list.h"
12 static const char git_mailsplit_usage
[] =
13 "git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
15 static int is_from_line(const char *line
, int len
)
19 if (len
< 20 || memcmp("From ", line
, 5))
22 colon
= line
+ len
- 2;
31 if (!isdigit(colon
[-4]) ||
32 !isdigit(colon
[-2]) ||
33 !isdigit(colon
[-1]) ||
34 !isdigit(colon
[ 1]) ||
39 if (strtol(colon
+3, NULL
, 10) <= 90)
42 /* Ok, close enough */
46 static struct strbuf buf
= STRBUF_INIT
;
48 /* We cannot use fgets() because our lines can contain NULs */
49 int read_line_with_nul(char *buf
, int size
, FILE *in
)
58 if (c
== '\n' || len
+ 1 >= size
)
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
71 static int split_one(FILE *mbox
, const char *name
, int allow_bare
)
76 int is_bare
= !is_from_line(buf
.buf
, buf
.len
);
78 if (is_bare
&& !allow_bare
)
81 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
83 die_errno("cannot open output file '%s'", name
);
84 output
= fdopen(fd
, "w");
86 /* Copy it out, while searching for a line that begins with
87 * "From " and having something that looks like a date format.
90 if (fwrite(buf
.buf
, 1, buf
.len
, output
) != buf
.len
)
91 die_errno("cannot write output");
93 if (strbuf_getwholeline(&buf
, mbox
, '\n')) {
98 die_errno("cannot read mbox");
100 if (!is_bare
&& is_from_line(buf
.buf
, buf
.len
))
101 break; /* done with one message */
110 fprintf(stderr
, "corrupt mailbox\n");
114 static int populate_maildir_list(struct string_list
*list
, const char *path
)
119 char *subs
[] = { "cur", "new", NULL
};
122 for (sub
= subs
; *sub
; ++sub
) {
123 snprintf(name
, sizeof(name
), "%s/%s", path
, *sub
);
124 if ((dir
= opendir(name
)) == NULL
) {
127 error("cannot opendir %s (%s)", name
, strerror(errno
));
131 while ((dent
= readdir(dir
)) != NULL
) {
132 if (dent
->d_name
[0] == '.')
134 snprintf(name
, sizeof(name
), "%s/%s", *sub
, dent
->d_name
);
135 string_list_insert(name
, list
);
144 static int split_maildir(const char *maildir
, const char *dir
,
145 int nr_prec
, int skip
)
151 struct string_list list
= {NULL
, 0, 0, 1};
153 if (populate_maildir_list(&list
, maildir
) < 0)
156 for (i
= 0; i
< list
.nr
; i
++) {
158 snprintf(file
, sizeof(file
), "%s/%s", maildir
, list
.items
[i
].string
);
159 f
= fopen(file
, "r");
161 error("cannot open mail %s (%s)", file
, strerror(errno
));
165 if (strbuf_getwholeline(&buf
, f
, '\n')) {
166 error("cannot read mail %s (%s)", file
, strerror(errno
));
170 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
171 split_one(f
, name
, 1);
178 string_list_clear(&list
, 1);
182 static int split_mbox(const char *file
, const char *dir
, int allow_bare
,
183 int nr_prec
, int skip
)
189 FILE *f
= !strcmp(file
, "-") ? stdin
: fopen(file
, "r");
193 error("cannot open mbox %s", file
);
199 } while (isspace(peek
));
202 if (strbuf_getwholeline(&buf
, f
, '\n')) {
203 /* empty stdin is OK */
205 error("cannot read mbox %s", file
);
212 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
213 file_done
= split_one(f
, name
, allow_bare
);
224 int cmd_mailsplit(int argc
, const char **argv
, const char *prefix
)
226 int nr
= 0, nr_prec
= 4, num
= 0;
228 const char *dir
= NULL
;
230 static const char *stdin_only
[] = { "-", NULL
};
232 for (argp
= argv
+1; *argp
; argp
++) {
233 const char *arg
= *argp
;
238 if ( arg
[1] == 'd' ) {
239 nr_prec
= strtol(arg
+2, NULL
, 10);
240 if (nr_prec
< 3 || 10 <= nr_prec
)
241 usage(git_mailsplit_usage
);
243 } else if ( arg
[1] == 'f' ) {
244 nr
= strtol(arg
+2, NULL
, 10);
245 } else if ( arg
[1] == 'b' && !arg
[2] ) {
247 } else if ( arg
[1] == 'o' && arg
[2] ) {
249 } else if ( arg
[1] == '-' && !arg
[2] ) {
250 argp
++; /* -- marks end of options */
253 die("unknown option: %s", arg
);
258 /* Backwards compatibility: if no -o specified, accept
259 <mbox> <dir> or just <dir> */
260 switch (argc
- (argp
-argv
)) {
266 stdin_only
[0] = argp
[0];
271 usage(git_mailsplit_usage
);
274 /* New usage: if no more argument, parse stdin */
280 const char *arg
= *argp
++;
284 if (arg
[0] == '-' && arg
[1] == 0) {
285 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
287 error("cannot split patches from stdin");
295 if (stat(arg
, &argstat
) == -1) {
296 error("cannot stat %s (%s)", arg
, strerror(errno
));
300 if (S_ISDIR(argstat
.st_mode
))
301 ret
= split_maildir(arg
, dir
, nr_prec
, nr
);
303 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
306 error("cannot split patches from %s", arg
);