~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-security

« back to all changes in this revision

Viewing changes to builtin-mailsplit.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-10-04 08:27:01 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20071004082701-rsd058ontoqz4i30
Tags: 1:1.5.3.4-1
new upstream point release (closes: #445188).

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 */
7
7
#include "cache.h"
8
8
#include "builtin.h"
 
9
#include "path-list.h"
9
10
 
10
11
static const char git_mailsplit_usage[] =
11
 
"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
 
12
"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>|<Maildir>...";
12
13
 
13
14
static int is_from_line(const char *line, int len)
14
15
{
96
97
        exit(1);
97
98
}
98
99
 
99
 
int 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));
 
100
static int populate_maildir_list(struct path_list *list, const char *path)
 
101
{
 
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];
102
127
        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);
 
128
        int i;
 
129
        struct path_list list = {NULL, 0, 0, 1};
 
130
 
 
131
        snprintf(curdir, sizeof(curdir), "%s/cur", maildir);
 
132
        if (populate_maildir_list(&list, curdir) < 0)
 
133
                goto out;
 
134
 
 
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));
111
141
                        goto out;
112
142
                }
113
143
 
114
144
                if (fgets(buf, sizeof(buf), f) == NULL) {
115
 
                        if (f == stdin)
116
 
                                break; /* empty stdin is OK */
 
145
                        error("cannot read mail %s (%s)", file, strerror(errno));
 
146
                        goto out;
 
147
                }
 
148
 
 
149
                sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
 
150
                split_one(f, name, 1);
 
151
 
 
152
                fclose(f);
 
153
        }
 
154
 
 
155
        path_list_clear(&list, 1);
 
156
 
 
157
        ret = skip;
 
158
out:
 
159
        return ret;
 
160
}
 
161
 
 
162
static int split_mbox(const char *file, const char *dir, int allow_bare,
 
163
                      int nr_prec, int skip)
 
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) {
117
179
                        error("cannot read mbox %s", file);
118
180
                        goto out;
119
181
                }
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
 
        }
 
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
 
129
193
        ret = skip;
130
194
out:
131
 
        free(name);
132
195
        return ret;
133
196
}
 
197
 
134
198
int cmd_mailsplit(int argc, const char **argv, const char *prefix)
135
199
{
136
 
        int nr = 0, nr_prec = 4, ret;
 
200
        int nr = 0, nr_prec = 4, num = 0;
137
201
        int allow_bare = 0;
138
202
        const char *dir = NULL;
139
203
        const char **argp;
186
250
                        argp = stdin_only;
187
251
        }
188
252
 
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;
 
253
        while (*argp) {
 
254
                const char *arg = *argp++;
 
255
                struct stat argstat;
 
256
                int ret = 0;
 
257
 
 
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
                        }
 
264
                        num += (ret - nr);
 
265
                        nr = ret;
 
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
                }
 
283
                num += (ret - nr);
 
284
                nr = ret;
 
285
        }
 
286
 
 
287
        printf("%d\n", num);
 
288
 
 
289
        return 0;
194
290
}