builtin / mailsplit.con commit Merge branch 'nd/graph-width-padded' (f2140c3)
   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 "string-list.h"
  10#include "strbuf.h"
  11
  12static const char git_mailsplit_usage[] =
  13"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
  14
  15static int is_from_line(const char *line, int len)
  16{
  17        const char *colon;
  18
  19        if (len < 20 || memcmp("From ", line, 5))
  20                return 0;
  21
  22        colon = line + len - 2;
  23        line += 5;
  24        for (;;) {
  25                if (colon < line)
  26                        return 0;
  27                if (*--colon == ':')
  28                        break;
  29        }
  30
  31        if (!isdigit(colon[-4]) ||
  32            !isdigit(colon[-2]) ||
  33            !isdigit(colon[-1]) ||
  34            !isdigit(colon[ 1]) ||
  35            !isdigit(colon[ 2]))
  36                return 0;
  37
  38        /* year */
  39        if (strtol(colon+3, NULL, 10) <= 90)
  40                return 0;
  41
  42        /* Ok, close enough */
  43        return 1;
  44}
  45
  46static struct strbuf buf = STRBUF_INIT;
  47static int keep_cr;
  48static int mboxrd;
  49
  50static int is_gtfrom(const struct strbuf *buf)
  51{
  52        size_t min = strlen(">From ");
  53        size_t ngt;
  54
  55        if (buf->len < min)
  56                return 0;
  57
  58        ngt = strspn(buf->buf, ">");
  59        return ngt && starts_with(buf->buf + ngt, "From ");
  60}
  61
  62/* Called with the first line (potentially partial)
  63 * already in buf[] -- normally that should begin with
  64 * the Unix "From " line.  Write it into the specified
  65 * file.
  66 */
  67static int split_one(FILE *mbox, const char *name, int allow_bare)
  68{
  69        FILE *output;
  70        int fd;
  71        int status = 0;
  72        int is_bare = !is_from_line(buf.buf, buf.len);
  73
  74        if (is_bare && !allow_bare) {
  75                fprintf(stderr, "corrupt mailbox\n");
  76                exit(1);
  77        }
  78        fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
  79        if (fd < 0)
  80                die_errno("cannot open output file '%s'", name);
  81        output = xfdopen(fd, "w");
  82
  83        /* Copy it out, while searching for a line that begins with
  84         * "From " and having something that looks like a date format.
  85         */
  86        for (;;) {
  87                if (!keep_cr && buf.len > 1 && buf.buf[buf.len-1] == '\n' &&
  88                        buf.buf[buf.len-2] == '\r') {
  89                        strbuf_setlen(&buf, buf.len-2);
  90                        strbuf_addch(&buf, '\n');
  91                }
  92
  93                if (mboxrd && is_gtfrom(&buf))
  94                        strbuf_remove(&buf, 0, 1);
  95
  96                if (fwrite(buf.buf, 1, buf.len, output) != buf.len)
  97                        die_errno("cannot write output");
  98
  99                if (strbuf_getwholeline(&buf, mbox, '\n')) {
 100                        if (feof(mbox)) {
 101                                status = 1;
 102                                break;
 103                        }
 104                        die_errno("cannot read mbox");
 105                }
 106                if (!is_bare && is_from_line(buf.buf, buf.len))
 107                        break; /* done with one message */
 108        }
 109        fclose(output);
 110        return status;
 111}
 112
 113static int populate_maildir_list(struct string_list *list, const char *path)
 114{
 115        DIR *dir;
 116        struct dirent *dent;
 117        char *name = NULL;
 118        char *subs[] = { "cur", "new", NULL };
 119        char **sub;
 120        int ret = -1;
 121
 122        for (sub = subs; *sub; ++sub) {
 123                free(name);
 124                name = xstrfmt("%s/%s", path, *sub);
 125                if ((dir = opendir(name)) == NULL) {
 126                        if (errno == ENOENT)
 127                                continue;
 128                        error_errno("cannot opendir %s", name);
 129                        goto out;
 130                }
 131
 132                while ((dent = readdir(dir)) != NULL) {
 133                        if (dent->d_name[0] == '.')
 134                                continue;
 135                        free(name);
 136                        name = xstrfmt("%s/%s", *sub, dent->d_name);
 137                        string_list_insert(list, name);
 138                }
 139
 140                closedir(dir);
 141        }
 142
 143        ret = 0;
 144
 145out:
 146        free(name);
 147        return ret;
 148}
 149
 150static int maildir_filename_cmp(const char *a, const char *b)
 151{
 152        while (*a && *b) {
 153                if (isdigit(*a) && isdigit(*b)) {
 154                        long int na, nb;
 155                        na = strtol(a, (char **)&a, 10);
 156                        nb = strtol(b, (char **)&b, 10);
 157                        if (na != nb)
 158                                return na - nb;
 159                        /* strtol advanced our pointers */
 160                }
 161                else {
 162                        if (*a != *b)
 163                                return (unsigned char)*a - (unsigned char)*b;
 164                        a++;
 165                        b++;
 166                }
 167        }
 168        return (unsigned char)*a - (unsigned char)*b;
 169}
 170
 171static int split_maildir(const char *maildir, const char *dir,
 172        int nr_prec, int skip)
 173{
 174        char *file = NULL;
 175        FILE *f = NULL;
 176        int ret = -1;
 177        int i;
 178        struct string_list list = STRING_LIST_INIT_DUP;
 179
 180        list.cmp = maildir_filename_cmp;
 181
 182        if (populate_maildir_list(&list, maildir) < 0)
 183                goto out;
 184
 185        for (i = 0; i < list.nr; i++) {
 186                char *name;
 187
 188                free(file);
 189                file = xstrfmt("%s/%s", maildir, list.items[i].string);
 190
 191                f = fopen(file, "r");
 192                if (!f) {
 193                        error_errno("cannot open mail %s", file);
 194                        goto out;
 195                }
 196
 197                if (strbuf_getwholeline(&buf, f, '\n')) {
 198                        error_errno("cannot read mail %s", file);
 199                        goto out;
 200                }
 201
 202                name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
 203                split_one(f, name, 1);
 204                free(name);
 205
 206                fclose(f);
 207                f = NULL;
 208        }
 209
 210        ret = skip;
 211out:
 212        if (f)
 213                fclose(f);
 214        free(file);
 215        string_list_clear(&list, 1);
 216        return ret;
 217}
 218
 219static int split_mbox(const char *file, const char *dir, int allow_bare,
 220                      int nr_prec, int skip)
 221{
 222        int ret = -1;
 223        int peek;
 224
 225        FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
 226        int file_done = 0;
 227
 228        if (!f) {
 229                error_errno("cannot open mbox %s", file);
 230                goto out;
 231        }
 232
 233        do {
 234                peek = fgetc(f);
 235        } while (isspace(peek));
 236        ungetc(peek, f);
 237
 238        if (strbuf_getwholeline(&buf, f, '\n')) {
 239                /* empty stdin is OK */
 240                if (f != stdin) {
 241                        error("cannot read mbox %s", file);
 242                        goto out;
 243                }
 244                file_done = 1;
 245        }
 246
 247        while (!file_done) {
 248                char *name = xstrfmt("%s/%0*d", dir, nr_prec, ++skip);
 249                file_done = split_one(f, name, allow_bare);
 250                free(name);
 251        }
 252
 253        if (f != stdin)
 254                fclose(f);
 255
 256        ret = skip;
 257out:
 258        return ret;
 259}
 260
 261int cmd_mailsplit(int argc, const char **argv, const char *prefix)
 262{
 263        int nr = 0, nr_prec = 4, num = 0;
 264        int allow_bare = 0;
 265        const char *dir = NULL;
 266        const char **argp;
 267        static const char *stdin_only[] = { "-", NULL };
 268
 269        for (argp = argv+1; *argp; argp++) {
 270                const char *arg = *argp;
 271
 272                if (arg[0] != '-')
 273                        break;
 274                /* do flags here */
 275                if ( arg[1] == 'd' ) {
 276                        nr_prec = strtol(arg+2, NULL, 10);
 277                        if (nr_prec < 3 || 10 <= nr_prec)
 278                                usage(git_mailsplit_usage);
 279                        continue;
 280                } else if ( arg[1] == 'f' ) {
 281                        nr = strtol(arg+2, NULL, 10);
 282                } else if ( arg[1] == 'h' ) {
 283                        usage(git_mailsplit_usage);
 284                } else if ( arg[1] == 'b' && !arg[2] ) {
 285                        allow_bare = 1;
 286                } else if (!strcmp(arg, "--keep-cr")) {
 287                        keep_cr = 1;
 288                } else if ( arg[1] == 'o' && arg[2] ) {
 289                        dir = arg+2;
 290                } else if (!strcmp(arg, "--mboxrd")) {
 291                        mboxrd = 1;
 292                } else if ( arg[1] == '-' && !arg[2] ) {
 293                        argp++; /* -- marks end of options */
 294                        break;
 295                } else {
 296                        die("unknown option: %s", arg);
 297                }
 298        }
 299
 300        if ( !dir ) {
 301                /* Backwards compatibility: if no -o specified, accept
 302                   <mbox> <dir> or just <dir> */
 303                switch (argc - (argp-argv)) {
 304                case 1:
 305                        dir = argp[0];
 306                        argp = stdin_only;
 307                        break;
 308                case 2:
 309                        stdin_only[0] = argp[0];
 310                        dir = argp[1];
 311                        argp = stdin_only;
 312                        break;
 313                default:
 314                        usage(git_mailsplit_usage);
 315                }
 316        } else {
 317                /* New usage: if no more argument, parse stdin */
 318                if ( !*argp )
 319                        argp = stdin_only;
 320        }
 321
 322        while (*argp) {
 323                const char *arg = *argp++;
 324                struct stat argstat;
 325                int ret = 0;
 326
 327                if (arg[0] == '-' && arg[1] == 0) {
 328                        ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
 329                        if (ret < 0) {
 330                                error("cannot split patches from stdin");
 331                                return 1;
 332                        }
 333                        num += (ret - nr);
 334                        nr = ret;
 335                        continue;
 336                }
 337
 338                if (stat(arg, &argstat) == -1) {
 339                        error_errno("cannot stat %s", arg);
 340                        return 1;
 341                }
 342
 343                if (S_ISDIR(argstat.st_mode))
 344                        ret = split_maildir(arg, dir, nr_prec, nr);
 345                else
 346                        ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
 347
 348                if (ret < 0) {
 349                        error("cannot split patches from %s", arg);
 350                        return 1;
 351                }
 352                num += (ret - nr);
 353                nr = ret;
 354        }
 355
 356        printf("%d\n", num);
 357
 358        return 0;
 359}