e4d977bafbc04fa2bfedfc2912fc87bed9de8e1e
   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 "cache.h"
   8#include "builtin.h"
   9#include "path-list.h"
  10
  11static const char git_mailsplit_usage[] =
  12"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>|<Maildir>...";
  13
  14static 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
  45/* Could be as small as 64, enough to hold a Unix "From " line. */
  46static char buf[4096];
  47
  48/* We cannot use fgets() because our lines can contain NULs */
  49int read_line_with_nul(char *buf, int size, FILE *in)
  50{
  51        int len = 0, c;
  52
  53        for (;;) {
  54                c = getc(in);
  55                buf[len++] = c;
  56                if (c == EOF || c == '\n' || len + 1 >= size)
  57                        break;
  58        }
  59
  60        if (c == EOF)
  61                len--;
  62        buf[len] = '\0';
  63
  64        return len;
  65}
  66
  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
  70 * file.
  71 */
  72static int split_one(FILE *mbox, const char *name, int allow_bare)
  73{
  74        FILE *output = NULL;
  75        int len = strlen(buf);
  76        int fd;
  77        int status = 0;
  78        int is_bare = !is_from_line(buf, len);
  79
  80        if (is_bare && !allow_bare)
  81                goto corrupt;
  82
  83        fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
  84        if (fd < 0)
  85                die("cannot open output file %s", name);
  86        output = fdopen(fd, "w");
  87
  88        /* Copy it out, while searching for a line that begins with
  89         * "From " and having something that looks like a date format.
  90         */
  91        for (;;) {
  92                int is_partial = len && buf[len-1] != '\n';
  93
  94                if (fwrite(buf, 1, len, output) != len)
  95                        die("cannot write output");
  96
  97                len = read_line_with_nul(buf, sizeof(buf), mbox);
  98                if (len == 0) {
  99                        if (feof(mbox)) {
 100                                status = 1;
 101                                break;
 102                        }
 103                        die("cannot read mbox");
 104                }
 105                if (!is_partial && !is_bare && is_from_line(buf, len))
 106                        break; /* done with one message */
 107        }
 108        fclose(output);
 109        return status;
 110
 111 corrupt:
 112        if (output)
 113                fclose(output);
 114        unlink(name);
 115        fprintf(stderr, "corrupt mailbox\n");
 116        exit(1);
 117}
 118
 119static int populate_maildir_list(struct path_list *list, const char *path)
 120{
 121        DIR *dir;
 122        struct dirent *dent;
 123        char name[PATH_MAX];
 124        char *subs[] = { "cur", "new", NULL };
 125        char **sub;
 126
 127        for (sub = subs; *sub; ++sub) {
 128                snprintf(name, sizeof(name), "%s/%s", path, *sub);
 129                if ((dir = opendir(name)) == NULL) {
 130                        if (errno == ENOENT)
 131                                continue;
 132                        error("cannot opendir %s (%s)", name, strerror(errno));
 133                        return -1;
 134                }
 135
 136                while ((dent = readdir(dir)) != NULL) {
 137                        if (dent->d_name[0] == '.')
 138                                continue;
 139                        snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
 140                        path_list_insert(name, list);
 141                }
 142
 143                closedir(dir);
 144        }
 145
 146        return 0;
 147}
 148
 149static int split_maildir(const char *maildir, const char *dir,
 150        int nr_prec, int skip)
 151{
 152        char file[PATH_MAX];
 153        char name[PATH_MAX];
 154        int ret = -1;
 155        int i;
 156        struct path_list list = {NULL, 0, 0, 1};
 157
 158        if (populate_maildir_list(&list, maildir) < 0)
 159                goto out;
 160
 161        for (i = 0; i < list.nr; i++) {
 162                FILE *f;
 163                snprintf(file, sizeof(file), "%s/%s", maildir, list.items[i].path);
 164                f = fopen(file, "r");
 165                if (!f) {
 166                        error("cannot open mail %s (%s)", file, strerror(errno));
 167                        goto out;
 168                }
 169
 170                if (fgets(buf, sizeof(buf), f) == NULL) {
 171                        error("cannot read mail %s (%s)", file, strerror(errno));
 172                        goto out;
 173                }
 174
 175                sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
 176                split_one(f, name, 1);
 177
 178                fclose(f);
 179        }
 180
 181        ret = skip;
 182out:
 183        path_list_clear(&list, 1);
 184        return ret;
 185}
 186
 187static int split_mbox(const char *file, const char *dir, int allow_bare,
 188                      int nr_prec, int skip)
 189{
 190        char name[PATH_MAX];
 191        int ret = -1;
 192        int peek;
 193
 194        FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
 195        int file_done = 0;
 196
 197        if (!f) {
 198                error("cannot open mbox %s", file);
 199                goto out;
 200        }
 201
 202        do {
 203                peek = fgetc(f);
 204        } while (isspace(peek));
 205        ungetc(peek, f);
 206
 207        if (fgets(buf, sizeof(buf), f) == NULL) {
 208                /* empty stdin is OK */
 209                if (f != stdin) {
 210                        error("cannot read mbox %s", file);
 211                        goto out;
 212                }
 213                file_done = 1;
 214        }
 215
 216        while (!file_done) {
 217                sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
 218                file_done = split_one(f, name, allow_bare);
 219        }
 220
 221        if (f != stdin)
 222                fclose(f);
 223
 224        ret = skip;
 225out:
 226        return ret;
 227}
 228
 229int cmd_mailsplit(int argc, const char **argv, const char *prefix)
 230{
 231        int nr = 0, nr_prec = 4, num = 0;
 232        int allow_bare = 0;
 233        const char *dir = NULL;
 234        const char **argp;
 235        static const char *stdin_only[] = { "-", NULL };
 236
 237        for (argp = argv+1; *argp; argp++) {
 238                const char *arg = *argp;
 239
 240                if (arg[0] != '-')
 241                        break;
 242                /* do flags here */
 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);
 247                        continue;
 248                } else if ( arg[1] == 'f' ) {
 249                        nr = strtol(arg+2, NULL, 10);
 250                } else if ( arg[1] == 'b' && !arg[2] ) {
 251                        allow_bare = 1;
 252                } else if ( arg[1] == 'o' && arg[2] ) {
 253                        dir = arg+2;
 254                } else if ( arg[1] == '-' && !arg[2] ) {
 255                        argp++; /* -- marks end of options */
 256                        break;
 257                } else {
 258                        die("unknown option: %s", arg);
 259                }
 260        }
 261
 262        if ( !dir ) {
 263                /* Backwards compatibility: if no -o specified, accept
 264                   <mbox> <dir> or just <dir> */
 265                switch (argc - (argp-argv)) {
 266                case 1:
 267                        dir = argp[0];
 268                        argp = stdin_only;
 269                        break;
 270                case 2:
 271                        stdin_only[0] = argp[0];
 272                        dir = argp[1];
 273                        argp = stdin_only;
 274                        break;
 275                default:
 276                        usage(git_mailsplit_usage);
 277                }
 278        } else {
 279                /* New usage: if no more argument, parse stdin */
 280                if ( !*argp )
 281                        argp = stdin_only;
 282        }
 283
 284        while (*argp) {
 285                const char *arg = *argp++;
 286                struct stat argstat;
 287                int ret = 0;
 288
 289                if (arg[0] == '-' && arg[1] == 0) {
 290                        ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
 291                        if (ret < 0) {
 292                                error("cannot split patches from stdin");
 293                                return 1;
 294                        }
 295                        num += (ret - nr);
 296                        nr = ret;
 297                        continue;
 298                }
 299
 300                if (stat(arg, &argstat) == -1) {
 301                        error("cannot stat %s (%s)", arg, strerror(errno));
 302                        return 1;
 303                }
 304
 305                if (S_ISDIR(argstat.st_mode))
 306                        ret = split_maildir(arg, dir, nr_prec, nr);
 307                else
 308                        ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
 309
 310                if (ret < 0) {
 311                        error("cannot split patches from %s", arg);
 312                        return 1;
 313                }
 314                num += (ret - nr);
 315                nr = ret;
 316        }
 317
 318        printf("%d\n", num);
 319
 320        return 0;
 321}