151
static int add_excludes_from_file_1(const char *fname,
152
const char *base,
153
int baselen,
154
+ char **buf_p,
155
struct exclude_list *which)
156
{
157
struct stat st;
172
goto err;
173
close(fd);
174
175
+ if (buf_p)
176
+ *buf_p = buf;
177
buf[size++] = '\n';
178
entry = buf;
179
for (i = 0; i < size; i++) {
195
196
void add_excludes_from_file(struct dir_struct *dir, const char *fname)
197
{
195
- if (add_excludes_from_file_1(fname, "", 0,
198
+ if (add_excludes_from_file_1(fname, "", 0, NULL,
199
&dir->exclude_list[EXC_FILE]) < 0)
200
die("cannot use %s as an exclude file", fname);
201
}
202
200
-int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
203
+static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
204
{
202
- char exclude_file[PATH_MAX];
203
- struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
204
- int current_nr = el->nr;
205
-
206
- if (dir->exclude_per_dir) {
207
- memcpy(exclude_file, base, baselen);
208
- strcpy(exclude_file + baselen, dir->exclude_per_dir);
209
- add_excludes_from_file_1(exclude_file, base, baselen, el);
205
+ struct exclude_list *el;
206
+ struct exclude_stack *stk = NULL;
207
+ int current;
208
+
209
+ if ((!dir->exclude_per_dir) ||
210
+ (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
211
+ return; /* too long a path -- ignore */
212
+
213
+ /* Pop the ones that are not the prefix of the path being checked. */
214
+ el = &dir->exclude_list[EXC_DIRS];
215
+ while ((stk = dir->exclude_stack) != NULL) {
216
+ if (stk->baselen <= baselen &&
217
+ !strncmp(dir->basebuf, base, stk->baselen))
218
+ break;
219
+ dir->exclude_stack = stk->prev;
220
+ while (stk->exclude_ix < el->nr)
221
+ free(el->excludes[--el->nr]);
222
+ free(stk->filebuf);
223
+ free(stk);
224
}
211
- return current_nr;
212
-}
225
214
-void pop_exclude_per_directory(struct dir_struct *dir, int stk)
215
-{
216
- struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
226
+ /* Read from the parent directories and push them down. */
227
+ current = stk ? stk->baselen : -1;
228
+ while (current < baselen) {
229
+ struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
230
+ const char *cp;
231
218
- while (stk < el->nr)
219
- free(el->excludes[--el->nr]);
232
+ if (current < 0) {
233
+ cp = base;
234
+ current = 0;
235
+ }
236
+ else {
237
+ cp = strchr(base + current + 1, '/');
238
+ if (!cp)
239
+ die("oops in prep_exclude");
240
+ cp++;
241
+ }
242
+ stk->prev = dir->exclude_stack;
243
+ stk->baselen = cp - base;
244
+ stk->exclude_ix = el->nr;
245
+ memcpy(dir->basebuf + current, base + current,
246
+ stk->baselen - current);
247
+ strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
248
+ add_excludes_from_file_1(dir->basebuf,
249
+ dir->basebuf, stk->baselen,
250
+ &stk->filebuf, el);
251
+ dir->exclude_stack = stk;
252
+ current = stk->baselen;
253
+ }
254
+ dir->basebuf[baselen] = '\0';
255
}
256
257
/* Scan the list and let the last match determines the fate.
318
const char *basename = strrchr(pathname, '/');
319
basename = (basename) ? basename+1 : pathname;
320
321
+ prep_exclude(dir, pathname, basename-pathname);
322
for (st = EXC_CMDL; st <= EXC_FILE; st++) {
323
switch (excluded_1(pathname, pathlen, basename, &dir->exclude_list[st])) {
324
case 0:
536
int contents = 0;
537
538
if (fdir) {
503
- int exclude_stk;
539
struct dirent *de;
540
char fullname[PATH_MAX + 1];
541
memcpy(fullname, base, baselen);
542
508
- exclude_stk = push_exclude_per_directory(dir, base, baselen);
509
-
543
while ((de = readdir(fdir)) != NULL) {
544
int len, dtype;
545
int exclude;
613
}
614
exit_early:
615
closedir(fdir);
583
-
584
- pop_exclude_per_directory(dir, exclude_stk);
616
}
617
618
return contents;
681
int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
682
{
683
struct path_simplify *simplify = create_simplify(pathspec);
653
- char *pp = NULL;
654
-
655
- /*
656
- * Make sure to do the per-directory exclude for all the
657
- * directories leading up to our base.
658
- */
659
- if (baselen) {
660
- if (dir->exclude_per_dir) {
661
- char *p;
662
- pp = xmalloc(baselen+1);
663
- memcpy(pp, base, baselen+1);
664
- p = pp;
665
- while (1) {
666
- char save = *p;
667
- *p = 0;
668
- push_exclude_per_directory(dir, pp, p-pp);
669
- *p++ = save;
670
- if (!save)
671
- break;
672
- p = strchr(p, '/');
673
- if (p)
674
- p++;
675
- else
676
- p = pp + baselen;
677
- }
678
- }
679
- }
684
685
read_directory_recursive(dir, path, base, baselen, 0, simplify);
686
free_simplify(simplify);
683
- free(pp);
687
qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
688
qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
689
return dir->nr;