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