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
)
56 if (c
== EOF
|| c
== '\n' || len
+ 1 >= size
)
67 /* Called with the first line (potentially partial)
68 * already in buf[] -- normally that should begin with
69 * the Unix "From " line. Write it into the specified
72 static int split_one(FILE *mbox
, const char *name
, int allow_bare
)
75 int len
= strlen(buf
);
78 int is_bare
= !is_from_line(buf
, len
);
80 if (is_bare
&& !allow_bare
)
83 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
85 die("cannot open output file %s", name
);
86 output
= fdopen(fd
, "w");
88 /* Copy it out, while searching for a line that begins with
89 * "From " and having something that looks like a date format.
92 int is_partial
= len
&& buf
[len
-1] != '\n';
94 if (fwrite(buf
, 1, len
, output
) != len
)
95 die("cannot write output");
97 len
= read_line_with_nul(buf
, sizeof(buf
), mbox
);
103 die("cannot read mbox");
105 if (!is_partial
&& !is_bare
&& is_from_line(buf
, len
))
106 break; /* done with one message */
115 fprintf(stderr
, "corrupt mailbox\n");
119 static int populate_maildir_list(struct path_list
*list
, const char *path
)
124 char *subs
[] = { "cur", "new", NULL
};
127 for (sub
= subs
; *sub
; ++sub
) {
128 snprintf(name
, sizeof(name
), "%s/%s", path
, *sub
);
129 if ((dir
= opendir(name
)) == NULL
) {
132 error("cannot opendir %s (%s)", name
, strerror(errno
));
136 while ((dent
= readdir(dir
)) != NULL
) {
137 if (dent
->d_name
[0] == '.')
139 snprintf(name
, sizeof(name
), "%s/%s", *sub
, dent
->d_name
);
140 path_list_insert(name
, list
);
149 static int split_maildir(const char *maildir
, const char *dir
,
150 int nr_prec
, int skip
)
156 struct path_list list
= {NULL
, 0, 0, 1};
158 if (populate_maildir_list(&list
, maildir
) < 0)
161 for (i
= 0; i
< list
.nr
; i
++) {
163 snprintf(file
, sizeof(file
), "%s/%s", maildir
, list
.items
[i
].path
);
164 f
= fopen(file
, "r");
166 error("cannot open mail %s (%s)", file
, strerror(errno
));
170 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
171 error("cannot read mail %s (%s)", file
, strerror(errno
));
175 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
176 split_one(f
, name
, 1);
183 path_list_clear(&list
, 1);
187 static int split_mbox(const char *file
, const char *dir
, int allow_bare
,
188 int nr_prec
, int skip
)
194 FILE *f
= !strcmp(file
, "-") ? stdin
: fopen(file
, "r");
198 error("cannot open mbox %s", file
);
204 } while (isspace(peek
));
207 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
208 /* empty stdin is OK */
210 error("cannot read mbox %s", file
);
217 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
218 file_done
= split_one(f
, name
, allow_bare
);
229 int cmd_mailsplit(int argc
, const char **argv
, const char *prefix
)
231 int nr
= 0, nr_prec
= 4, num
= 0;
233 const char *dir
= NULL
;
235 static const char *stdin_only
[] = { "-", NULL
};
237 for (argp
= argv
+1; *argp
; argp
++) {
238 const char *arg
= *argp
;
243 if ( arg
[1] == 'd' ) {
244 nr_prec
= strtol(arg
+2, NULL
, 10);
245 if (nr_prec
< 3 || 10 <= nr_prec
)
246 usage(git_mailsplit_usage
);
248 } else if ( arg
[1] == 'f' ) {
249 nr
= strtol(arg
+2, NULL
, 10);
250 } else if ( arg
[1] == 'b' && !arg
[2] ) {
252 } else if ( arg
[1] == 'o' && arg
[2] ) {
254 } else if ( arg
[1] == '-' && !arg
[2] ) {
255 argp
++; /* -- marks end of options */
258 die("unknown option: %s", arg
);
263 /* Backwards compatibility: if no -o specified, accept
264 <mbox> <dir> or just <dir> */
265 switch (argc
- (argp
-argv
)) {
271 stdin_only
[0] = argp
[0];
276 usage(git_mailsplit_usage
);
279 /* New usage: if no more argument, parse stdin */
285 const char *arg
= *argp
++;
289 if (arg
[0] == '-' && arg
[1] == 0) {
290 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
292 error("cannot split patches from stdin");
300 if (stat(arg
, &argstat
) == -1) {
301 error("cannot stat %s (%s)", arg
, strerror(errno
));
305 if (S_ISDIR(argstat
.st_mode
))
306 ret
= split_maildir(arg
, dir
, nr_prec
, nr
);
308 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
311 error("cannot split patches from %s", arg
);