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.
11 static const char git_mailsplit_usage
[] =
12 "git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
14 static int is_from_line(const char *line
, int len
)
18 if (len
< 20 || memcmp("From ", line
, 5))
21 colon
= line
+ len
- 2;
30 if (!isdigit(colon
[-4]) ||
31 !isdigit(colon
[-2]) ||
32 !isdigit(colon
[-1]) ||
33 !isdigit(colon
[ 1]) ||
38 if (strtol(colon
+3, NULL
, 10) <= 90)
41 /* Ok, close enough */
45 /* Could be as small as 64, enough to hold a Unix "From " line. */
46 static char buf
[4096];
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
)
74 int len
= strlen(buf
);
77 int is_bare
= !is_from_line(buf
, len
);
79 if (is_bare
&& !allow_bare
)
82 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
84 die("cannot open output file %s", name
);
85 output
= fdopen(fd
, "w");
87 /* Copy it out, while searching for a line that begins with
88 * "From " and having something that looks like a date format.
91 int is_partial
= len
&& buf
[len
-1] != '\n';
93 if (fwrite(buf
, 1, len
, output
) != len
)
94 die("cannot write output");
96 len
= read_line_with_nul(buf
, sizeof(buf
), mbox
);
102 die("cannot read mbox");
104 if (!is_partial
&& !is_bare
&& is_from_line(buf
, len
))
105 break; /* done with one message */
114 fprintf(stderr
, "corrupt mailbox\n");
118 static int populate_maildir_list(struct path_list
*list
, const char *path
)
123 char *subs
[] = { "cur", "new", NULL
};
126 for (sub
= subs
; *sub
; ++sub
) {
127 snprintf(name
, sizeof(name
), "%s/%s", path
, *sub
);
128 if ((dir
= opendir(name
)) == NULL
) {
131 error("cannot opendir %s (%s)", name
, strerror(errno
));
135 while ((dent
= readdir(dir
)) != NULL
) {
136 if (dent
->d_name
[0] == '.')
138 snprintf(name
, sizeof(name
), "%s/%s", *sub
, dent
->d_name
);
139 path_list_insert(name
, list
);
148 static int split_maildir(const char *maildir
, const char *dir
,
149 int nr_prec
, int skip
)
155 struct path_list list
= {NULL
, 0, 0, 1};
157 if (populate_maildir_list(&list
, maildir
) < 0)
160 for (i
= 0; i
< list
.nr
; i
++) {
162 snprintf(file
, sizeof(file
), "%s/%s", maildir
, list
.items
[i
].path
);
163 f
= fopen(file
, "r");
165 error("cannot open mail %s (%s)", file
, strerror(errno
));
169 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
170 error("cannot read mail %s (%s)", file
, strerror(errno
));
174 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
175 split_one(f
, name
, 1);
182 path_list_clear(&list
, 1);
186 static int split_mbox(const char *file
, const char *dir
, int allow_bare
,
187 int nr_prec
, int skip
)
193 FILE *f
= !strcmp(file
, "-") ? stdin
: fopen(file
, "r");
197 error("cannot open mbox %s", file
);
203 } while (isspace(peek
));
206 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
207 /* empty stdin is OK */
209 error("cannot read mbox %s", file
);
216 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
217 file_done
= split_one(f
, name
, allow_bare
);
228 int cmd_mailsplit(int argc
, const char **argv
, const char *prefix
)
230 int nr
= 0, nr_prec
= 4, num
= 0;
232 const char *dir
= NULL
;
234 static const char *stdin_only
[] = { "-", NULL
};
236 for (argp
= argv
+1; *argp
; argp
++) {
237 const char *arg
= *argp
;
242 if ( arg
[1] == 'd' ) {
243 nr_prec
= strtol(arg
+2, NULL
, 10);
244 if (nr_prec
< 3 || 10 <= nr_prec
)
245 usage(git_mailsplit_usage
);
247 } else if ( arg
[1] == 'f' ) {
248 nr
= strtol(arg
+2, NULL
, 10);
249 } else if ( arg
[1] == 'b' && !arg
[2] ) {
251 } else if ( arg
[1] == 'o' && arg
[2] ) {
253 } else if ( arg
[1] == '-' && !arg
[2] ) {
254 argp
++; /* -- marks end of options */
257 die("unknown option: %s", arg
);
262 /* Backwards compatibility: if no -o specified, accept
263 <mbox> <dir> or just <dir> */
264 switch (argc
- (argp
-argv
)) {
270 stdin_only
[0] = argp
[0];
275 usage(git_mailsplit_usage
);
278 /* New usage: if no more argument, parse stdin */
284 const char *arg
= *argp
++;
288 if (arg
[0] == '-' && arg
[1] == 0) {
289 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
291 error("cannot split patches from stdin");
299 if (stat(arg
, &argstat
) == -1) {
300 error("cannot stat %s (%s)", arg
, strerror(errno
));
304 if (S_ISDIR(argstat
.st_mode
))
305 ret
= split_maildir(arg
, dir
, nr_prec
, nr
);
307 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
310 error("cannot split patches from %s", arg
);