builtin-mailsplit.con commit Merge branch 'fl/cvsserver' (abbf594)
   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
  10static const char git_mailsplit_usage[] =
  11"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
  12
  13static int is_from_line(const char *line, int len)
  14{
  15        const char *colon;
  16
  17        if (len < 20 || memcmp("From ", line, 5))
  18                return 0;
  19
  20        colon = line + len - 2;
  21        line += 5;
  22        for (;;) {
  23                if (colon < line)
  24                        return 0;
  25                if (*--colon == ':')
  26                        break;
  27        }
  28
  29        if (!isdigit(colon[-4]) ||
  30            !isdigit(colon[-2]) ||
  31            !isdigit(colon[-1]) ||
  32            !isdigit(colon[ 1]) ||
  33            !isdigit(colon[ 2]))
  34                return 0;
  35
  36        /* year */
  37        if (strtol(colon+3, NULL, 10) <= 90)
  38                return 0;
  39
  40        /* Ok, close enough */
  41        return 1;
  42}
  43
  44/* Could be as small as 64, enough to hold a Unix "From " line. */
  45static char buf[4096];
  46
  47/* Called with the first line (potentially partial)
  48 * already in buf[] -- normally that should begin with
  49 * the Unix "From " line.  Write it into the specified
  50 * file.
  51 */
  52static int split_one(FILE *mbox, const char *name, int allow_bare)
  53{
  54        FILE *output = NULL;
  55        int len = strlen(buf);
  56        int fd;
  57        int status = 0;
  58        int is_bare = !is_from_line(buf, len);
  59
  60        if (is_bare && !allow_bare)
  61                goto corrupt;
  62
  63        fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
  64        if (fd < 0)
  65                die("cannot open output file %s", name);
  66        output = fdopen(fd, "w");
  67
  68        /* Copy it out, while searching for a line that begins with
  69         * "From " and having something that looks like a date format.
  70         */
  71        for (;;) {
  72                int is_partial = (buf[len-1] != '\n');
  73
  74                if (fputs(buf, output) == EOF)
  75                        die("cannot write output");
  76
  77                if (fgets(buf, sizeof(buf), mbox) == NULL) {
  78                        if (feof(mbox)) {
  79                                status = 1;
  80                                break;
  81                        }
  82                        die("cannot read mbox");
  83                }
  84                len = strlen(buf);
  85                if (!is_partial && !is_bare && is_from_line(buf, len))
  86                        break; /* done with one message */
  87        }
  88        fclose(output);
  89        return status;
  90
  91 corrupt:
  92        if (output)
  93                fclose(output);
  94        unlink(name);
  95        fprintf(stderr, "corrupt mailbox\n");
  96        exit(1);
  97}
  98
  99int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
 100{
 101        char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
 102        int ret = -1;
 103
 104        while (*mbox) {
 105                const char *file = *mbox++;
 106                FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
 107                int file_done = 0;
 108
 109                if ( !f ) {
 110                        error("cannot open mbox %s", file);
 111                        goto out;
 112                }
 113
 114                if (fgets(buf, sizeof(buf), f) == NULL) {
 115                        if (f == stdin)
 116                                break; /* empty stdin is OK */
 117                        error("cannot read mbox %s", file);
 118                        goto out;
 119                }
 120
 121                while (!file_done) {
 122                        sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
 123                        file_done = split_one(f, name, allow_bare);
 124                }
 125
 126                if (f != stdin)
 127                        fclose(f);
 128        }
 129        ret = skip;
 130out:
 131        free(name);
 132        return ret;
 133}
 134int cmd_mailsplit(int argc, const char **argv, const char *prefix)
 135{
 136        int nr = 0, nr_prec = 4, ret;
 137        int allow_bare = 0;
 138        const char *dir = NULL;
 139        const char **argp;
 140        static const char *stdin_only[] = { "-", NULL };
 141
 142        for (argp = argv+1; *argp; argp++) {
 143                const char *arg = *argp;
 144
 145                if (arg[0] != '-')
 146                        break;
 147                /* do flags here */
 148                if ( arg[1] == 'd' ) {
 149                        nr_prec = strtol(arg+2, NULL, 10);
 150                        if (nr_prec < 3 || 10 <= nr_prec)
 151                                usage(git_mailsplit_usage);
 152                        continue;
 153                } else if ( arg[1] == 'f' ) {
 154                        nr = strtol(arg+2, NULL, 10);
 155                } else if ( arg[1] == 'b' && !arg[2] ) {
 156                        allow_bare = 1;
 157                } else if ( arg[1] == 'o' && arg[2] ) {
 158                        dir = arg+2;
 159                } else if ( arg[1] == '-' && !arg[2] ) {
 160                        argp++; /* -- marks end of options */
 161                        break;
 162                } else {
 163                        die("unknown option: %s", arg);
 164                }
 165        }
 166
 167        if ( !dir ) {
 168                /* Backwards compatibility: if no -o specified, accept
 169                   <mbox> <dir> or just <dir> */
 170                switch (argc - (argp-argv)) {
 171                case 1:
 172                        dir = argp[0];
 173                        argp = stdin_only;
 174                        break;
 175                case 2:
 176                        stdin_only[0] = argp[0];
 177                        dir = argp[1];
 178                        argp = stdin_only;
 179                        break;
 180                default:
 181                        usage(git_mailsplit_usage);
 182                }
 183        } else {
 184                /* New usage: if no more argument, parse stdin */
 185                if ( !*argp )
 186                        argp = stdin_only;
 187        }
 188
 189        ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
 190        if (ret != -1)
 191                printf("%d\n", ret);
 192
 193        return ret == -1;
 194}