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