Improved the option syntax by eliminating the --inherit=DEPTH option and
[rsync/rsync-patches.git] / filter.diff
1 After applying this patch and running configure, you MUST run this
2 command before "make":
3
4     make proto
5
6 This patch adds the ability to merge rules into your excludes/includes
7 using a ". FILE" idiom.  If you specify a name with a preceding -p
8 option, that filename will be looked for in every subdirectory that
9 rsync visits, and the rules found in that subdirectory's file will
10 affect either just that dir or, if -i is specified, that dir and its
11 subdirs.
12
13 For example:
14
15   rsync -av --exclude='. -pi .excl' from/ to
16
17 The above will look for a file named ".excl" in every directory of the
18 hierarchy that rsync visits, and it will exclude (by default) names
19 based on the rules found therein.  If one of the .excl files contains
20 this:
21
22   + *.c
23   . -p .excl2
24   . -i .excl3
25   *.o
26
27 Then the file ".excl2" will also be read in from the current dir and all
28 its subdirs (due to the -p option) with each file's rules only affecting
29 the file's current directory.  The file ".excl3" would just be read in
30 for the current dir (because it was not specified with the -p option),
31 but its rules would be inherited by all its subdirectories (because of
32 the -i option).
33
34 ..wayne..
35
36 --- orig/exclude.c      2004-08-05 23:16:37
37 +++ exclude.c   2004-08-06 09:01:33
38 @@ -27,17 +27,66 @@
39  #include "rsync.h"
40  
41  extern int verbose;
42 +extern int am_sender;
43  extern int eol_nulls;
44  extern int list_only;
45  extern int recurse;
46 +extern int io_error;
47 +extern int module_id;
48 +extern int delete_mode;
49 +extern int delete_excluded;
50 +extern int sanitize_paths;
51  
52  extern char curr_dir[];
53 +extern unsigned int curr_dir_len;
54  
55  struct exclude_list_struct exclude_list = { 0, 0, "" };
56 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
57  struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
58  char *exclude_path_prefix = NULL;
59  
60 +struct mergelist_save_struct {
61 +    struct exclude_list_struct *array;
62 +    int count;
63 +};
64 +
65 +static char dirbuf[MAXPATHLEN];
66 +static unsigned int dirbuf_offset = 0;
67 +static struct exclude_struct **mergelist_parents;
68 +static int mergelist_cnt = 0;
69 +static int mergelist_size = 0;
70 +static int initialized_mergelist_cnt = -1;
71 +
72 +/* Each exclude_list_struct describes a singly-linked list by keeping track
73 + * of both the head and tail pointers.  The list is slightly unusual in that
74 + * a parent-dir's content can be appended to the end of the local list in a
75 + * special way:  the last item in the local list has its "next" pointer set
76 + * to point to the inherited list, but the local list's tail pointer points
77 + * at the end of the local list.  Thus, if the local list is empty, the head
78 + * will be pointing at the inherited content but the tail will be NULL.  To
79 + * help you visualize this, here are the possible list arrangements:
80 + *
81 + * Completely Empty                     Local Content Only
82 + * ==================================   ====================================
83 + * head -> NULL                         head -> Local1 -> Local2 -> NULL
84 + * tail -> NULL                         tail -------------^
85 + *
86 + * Inherited Content Only               Both Local and Inherited Content
87 + * ==================================   ====================================
88 + * head -> Parent1 -> Parent2 -> NULL   head -> L1 -> L2 -> P1 -> P2 -> NULL
89 + * tail -> NULL                         tail ---------^
90 + *
91 + * This means that anyone wanting to traverse the whole list to USE it just
92 + * needs to start at the head and use the "next" pointers until it goes
93 + * NULL.  To add new local content, we insert the item after the tail item
94 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
95 + * head).  To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
96 + * because it is shared between the current list and our parent list(s).
97 + * The easiest way to handle this is to simply truncate the list after the
98 + * tail item and then free the local list from the head.  When inheriting
99 + * the list for a new local dir, we just save off the exclude_list_struct
100 + * values (so we can pop back to them later) and set the tail to NULL.
101 + */
102 +
103  /** Build an exclude structure given an exclude pattern. */
104  static void make_exclude(struct exclude_list_struct *listp, const char *pat,
105                          unsigned int pat_len, unsigned int mflags)
106 @@ -46,6 +95,31 @@ static void make_exclude(struct exclude_
107         const char *cp;
108         unsigned int ex_len;
109  
110 +       if (verbose > 2) {
111 +               rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
112 +                       who_am_i(), (int)pat_len, pat, listp->debug_type,
113 +                       mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
114 +                       mflags & MATCHFLG_INCLUDE ? "in" : "ex");
115 +       }
116 +
117 +       if (mflags & MATCHFLG_MERGE_FILE) {
118 +               int i;
119 +               /* If the local include file was already mentioned, don't
120 +                * add it again. */
121 +               for (i = 0; i < mergelist_cnt; i++) {
122 +                       struct exclude_struct *ex = mergelist_parents[i];
123 +                       if (strlen(ex->pattern) == pat_len
124 +                           && memcmp(ex->pattern, pat, pat_len) == 0)
125 +                               return;
126 +               }
127 +               if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
128 +                   && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
129 +                       mflags |= MATCHFLG_CVSIGNORE;
130 +                       mflags &= ~(MATCHFLG_INCLUDE | MATCHFLG_INHERIT);
131 +               } else
132 +                       mflags &= ~MATCHFLG_CVSIGNORE;
133 +       }
134 +
135         ret = new(struct exclude_struct);
136         if (!ret)
137                 out_of_memory("make_exclude");
138 @@ -81,14 +155,40 @@ static void make_exclude(struct exclude_
139                 mflags |= MATCHFLG_DIRECTORY;
140         }
141  
142 -       for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
143 -               ret->slash_cnt++;
144 +       if (mflags & MATCHFLG_MERGE_FILE) {
145 +               struct exclude_list_struct *lp
146 +                   = new_array(struct exclude_list_struct, 1);
147 +               if (!lp)
148 +                       out_of_memory("make_exclude");
149 +               lp->head = lp->tail = NULL;
150 +               if ((cp = strrchr(ret->pattern, '/')) != NULL)
151 +                       cp++;
152 +               else
153 +                       cp = ret->pattern;
154 +               if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
155 +                       out_of_memory("make_exclude");
156 +               ret->u.mergelist = lp;
157 +               if (mergelist_cnt == mergelist_size) {
158 +                       mergelist_size += 5;
159 +                       mergelist_parents = realloc_array(mergelist_parents,
160 +                                               struct exclude_struct *,
161 +                                               mergelist_size);
162 +                       if (!mergelist_parents)
163 +                               out_of_memory("make_exclude");
164 +               }
165 +               mergelist_parents[mergelist_cnt++] = ret;
166 +       } else {
167 +               for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
168 +                       ret->u.slash_cnt++;
169 +       }
170  
171         ret->match_flags = mflags;
172  
173 -       if (!listp->tail)
174 +       if (!listp->tail) {
175 +               ret->next = listp->head;
176                 listp->head = listp->tail = ret;
177 -       else {
178 +       } else {
179 +               ret->next = listp->tail->next;
180                 listp->tail->next = ret;
181                 listp->tail = ret;
182         }
183 @@ -96,22 +196,180 @@ static void make_exclude(struct exclude_
184  
185  static void free_exclude(struct exclude_struct *ex)
186  {
187 +       if (ex->match_flags & MATCHFLG_MERGE_FILE) {
188 +               free(ex->u.mergelist->debug_type);
189 +               free(ex->u.mergelist);
190 +       }
191         free(ex->pattern);
192         free(ex);
193  }
194  
195 -void clear_exclude_list(struct exclude_list_struct *listp)
196 +static void clear_exclude_list(struct exclude_list_struct *listp)
197  {
198 -       struct exclude_struct *ent, *next;
199 -
200 -       for (ent = listp->head; ent; ent = next) {
201 -               next = ent->next;
202 -               free_exclude(ent);
203 +       if (listp->tail) {
204 +               struct exclude_struct *ent, *next;
205 +               /* Truncate any inherited items from the local list. */
206 +               listp->tail->next = NULL;
207 +               /* Now free everything that is left. */
208 +               for (ent = listp->head; ent; ent = next) {
209 +                       next = ent->next;
210 +                       free_exclude(ent);
211 +               }
212         }
213  
214         listp->head = listp->tail = NULL;
215  }
216  
217 +static void prep_merge_file(struct exclude_struct *ex,
218 +                           struct exclude_list_struct *lp, int flags,
219 +                           char *dir, unsigned int dirlen)
220 +{
221 +       char buf[MAXPATHLEN];
222 +       char *x, *y, *fn = ex->pattern;
223 +
224 +       if (!(x = strrchr(fn, '/')))
225 +               return;
226 +
227 +       *x = '\0';
228 +       if (dir[dirlen])
229 +               dir[dirlen] = '\0';
230 +       pathjoin(dirbuf, MAXPATHLEN, sanitize_paths ? "" : curr_dir, dir);
231 +       if (dirlen == 2 && *dir == '.') {
232 +               int len = strlen(dirbuf);
233 +               dirbuf[len-2] = '\0';
234 +       }
235 +       if (!*fn)
236 +               fn = "/";
237 +       if (*fn == '/')
238 +               strcpy(buf, fn); /* safe */
239 +       else
240 +               pathjoin(buf, MAXPATHLEN, dirbuf, fn);
241 +       fn = x + 1;
242 +
243 +       clean_fname(buf);
244 +       if (!*buf || buf[1])
245 +               strlcat(buf, "/", MAXPATHLEN);
246 +       for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
247 +       if (*x)
248 +               y += strlen(y);
249 +
250 +       while (*y) {
251 +               char save[MAXPATHLEN];
252 +               strcpy(save, y); /* safe */
253 +               dirbuf_offset = y - dirbuf;
254 +               strlcpy(x, fn, MAXPATHLEN - (x - buf));
255 +               lp->tail = NULL;
256 +               add_exclude_file(lp, buf, flags);
257 +               strcpy(y, save); /* safe */
258 +               while ((*x++ = *y++) != '/') {}
259 +       }
260 +
261 +       if (dirlen == 2 && *dir == '.') /* dir[dirlen-1] is always '/' */
262 +               dirbuf_offset = 0;
263 +       else
264 +               dirbuf_offset = dirlen;
265 +       memcpy(dirbuf, dir, dirbuf_offset);
266 +
267 +       memmove(ex->pattern, fn, strlen(fn) + 1);
268 +}
269 +
270 +void *push_local_excludes(char *dir, unsigned int dirlen)
271 +{
272 +       struct mergelist_save_struct *push;
273 +       struct exclude_list_struct *ap;
274 +       int i;
275 +
276 +       /* Make it easy to construct the full path for a merge-file that was
277 +        * specified with a relative path by saving off the current dir. */
278 +       if (dirlen == 2 && *dir == '.') /* dir[dirlen-1] is always '/' */
279 +               dirbuf_offset = 0;
280 +       else
281 +               dirbuf_offset = dirlen;
282 +       memcpy(dirbuf, dir, dirbuf_offset);
283 +
284 +
285 +       if (!(push = new_array(struct mergelist_save_struct, 1)))
286 +               out_of_memory("push_local_excludes");
287 +
288 +       push->count = mergelist_cnt;
289 +       push->array = new_array(struct exclude_list_struct, mergelist_cnt);
290 +       if (!push->array)
291 +               out_of_memory("push_local_excludes");
292 +
293 +       for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
294 +               memcpy(ap++, mergelist_parents[i]->u.mergelist,
295 +                      sizeof (struct exclude_list_struct));
296 +       }
297 +
298 +       /* Note: add_exclude_file() might increase mergelist_cnt, so keep
299 +        * this loop separate from the above loop. */
300 +       for (i = 0; i < mergelist_cnt; i++) {
301 +               struct exclude_struct *ex = mergelist_parents[i];
302 +               struct exclude_list_struct *lp = ex->u.mergelist;
303 +               int flags;
304 +
305 +               if (verbose > 2) {
306 +                       rprintf(FINFO, "[%s] pushing %sexclude list\n",
307 +                               who_am_i(), lp->debug_type);
308 +               }
309 +
310 +               if (ex->match_flags & MATCHFLG_CVSIGNORE)
311 +                       flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
312 +               else {
313 +                       flags = ex->match_flags & MATCHFLG_INCLUDE
314 +                           ? XFLG_DEF_INCLUDE : 0;
315 +               }
316 +
317 +               if (initialized_mergelist_cnt < i) {
318 +                       prep_merge_file(ex, lp, flags, dir, dirlen);
319 +                       initialized_mergelist_cnt = i;
320 +               }
321 +
322 +               if (!(ex->match_flags & MATCHFLG_INHERIT))
323 +                       lp->head = NULL;
324 +               lp->tail = NULL;
325 +               if (strlcpy(dirbuf +  dirbuf_offset, ex->pattern,
326 +                   MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
327 +                       add_exclude_file(lp, dirbuf, flags);
328 +               else {
329 +                       io_error |= IOERR_GENERAL;
330 +                       rprintf(FINFO,
331 +                           "cannot add local excludes in long-named directory %s\n",
332 +                           full_fname(dirbuf));
333 +               }
334 +       }
335 +
336 +       return (void*)push;
337 +}
338 +
339 +void pop_local_excludes(void *mem)
340 +{
341 +       struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
342 +       struct exclude_list_struct *ap;
343 +       int i;
344 +
345 +       for (i = mergelist_cnt; i-- > 0; ) {
346 +               struct exclude_struct *ex = mergelist_parents[i];
347 +               struct exclude_list_struct *lp = ex->u.mergelist;
348 +
349 +               if (verbose > 2) {
350 +                       rprintf(FINFO, "[%s] popping %sexclude list\n",
351 +                               who_am_i(), lp->debug_type);
352 +               }
353 +
354 +               clear_exclude_list(lp);
355 +       }
356 +
357 +       mergelist_cnt = initialized_mergelist_cnt = pop->count;
358 +       for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
359 +               memcpy(mergelist_parents[i]->u.mergelist, ap++,
360 +                      sizeof (struct exclude_list_struct));
361 +       }
362 +
363 +       free(pop->array);
364 +       free(pop);
365 +}
366 +
367  static int check_one_exclude(char *name, struct exclude_struct *ex,
368                               int name_is_dir)
369  {
370 @@ -122,7 +380,7 @@ static int check_one_exclude(char *name,
371         /* If the pattern does not have any slashes AND it does not have
372          * a "**" (which could match a slash), then we just match the
373          * name portion of the path. */
374 -       if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
375 +       if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
376                 if ((p = strrchr(name,'/')) != NULL)
377                         name = p+1;
378         }
379 @@ -133,7 +391,8 @@ static int check_one_exclude(char *name,
380                 name = full_name;
381         }
382  
383 -       if (!name[0]) return 0;
384 +       if (!name[0])
385 +               return 0;
386  
387         if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
388                 return 0;
389 @@ -148,9 +407,9 @@ static int check_one_exclude(char *name,
390         if (ex->match_flags & MATCHFLG_WILD) {
391                 /* A non-anchored match with an infix slash and no "**"
392                  * needs to match the last slash_cnt+1 name elements. */
393 -               if (!match_start && ex->slash_cnt
394 +               if (!match_start && ex->u.slash_cnt
395                     && !(ex->match_flags & MATCHFLG_WILD2)) {
396 -                       int cnt = ex->slash_cnt + 1;
397 +                       int cnt = ex->u.slash_cnt + 1;
398                         for (p = name + strlen(name) - 1; p >= name; p--) {
399                                 if (*p == '/' && !--cnt)
400                                         break;
401 @@ -221,6 +480,13 @@ int check_exclude(struct exclude_list_st
402         struct exclude_struct *ent;
403  
404         for (ent = listp->head; ent; ent = ent->next) {
405 +               if (ent->match_flags & MATCHFLG_MERGE_FILE) {
406 +                       int rc = check_exclude(ent->u.mergelist, name,
407 +                                              name_is_dir);
408 +                       if (rc)
409 +                               return rc;
410 +                       continue;
411 +               }
412                 if (check_one_exclude(name, ent, name_is_dir)) {
413                         report_exclude_result(name, ent, name_is_dir,
414                                               listp->debug_type);
415 @@ -253,12 +519,41 @@ static const char *get_exclude_tok(const
416                 p = (const char *)s;
417         }
418  
419 -       /* Is this a '+' or '-' followed by a space (not whitespace)? */
420 +       /* Check for a +/-/. followed by a space (not whitespace). */
421         if (!(xflags & XFLG_WORDS_ONLY)
422 -           && (*s == '-' || *s == '+') && s[1] == ' ') {
423 +           && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
424                 if (*s == '+')
425                         mflags |= MATCHFLG_INCLUDE;
426 +               else if (*s == '.') {
427 +                       mflags |= MATCHFLG_MERGE_FILE;
428 +                       if (xflags & XFLG_DEF_INCLUDE)
429 +                               mflags |= MATCHFLG_INCLUDE;
430 +                       while (s[2] == '-') {
431 +                               s += 2;
432 +                               do {
433 +                                       switch (*++s) {
434 +                                       case 'i':
435 +                                               mflags |= MATCHFLG_INHERIT;
436 +                                               break;
437 +                                       case 'p':
438 +                                               mflags |= MATCHFLG_PERDIR_MERGE;
439 +                                               break;
440 +                                       case '-':
441 +                                               if (s[1] == ' ')
442 +                                                       goto done;
443 +                                       default:
444 +                                               rprintf(FERROR,
445 +                                                   "invalid merge options: %s\n",
446 +                                                   p);
447 +                                               exit_cleanup(RERR_SYNTAX);
448 +                                       }
449 +                               } while (s[1] != ' ');
450 +                       }
451 +               }
452 +           done:
453                 s += 2;
454 +               if (mflags & MATCHFLG_MERGE_FILE && *s == '.' && s[1] == '/')
455 +                       s += 2;
456         } else if (xflags & XFLG_DEF_INCLUDE)
457                 mflags |= MATCHFLG_INCLUDE;
458  
459 @@ -292,6 +587,7 @@ void add_exclude(struct exclude_list_str
460         cp = pattern;
461         pat_len = 0;
462         while (1) {
463 +               /* Remember that the returned string is NOT '\0' terminated! */
464                 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
465                 if (!pat_len)
466                         break;
467 @@ -306,13 +602,55 @@ void add_exclude(struct exclude_list_str
468                         continue;
469                 }
470  
471 -               make_exclude(listp, cp, pat_len, mflags);
472 -
473 -               if (verbose > 2) {
474 -                       rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
475 -                               who_am_i(), (int)pat_len, cp, listp->debug_type,
476 -                               mflags & MATCHFLG_INCLUDE ? "in" : "ex");
477 +               if (mflags & MATCHFLG_MERGE_FILE) {
478 +                       char buf1[MAXPATHLEN], buf2[MAXPATHLEN];
479 +                       unsigned int len = pat_len;
480 +                       const char *fn = cp;
481 +                       if (len >= MAXPATHLEN) {
482 +                               rprintf(FERROR,
483 +                                       "merge filename too long: %.*s\n",
484 +                                       (int)len, fn);
485 +                               continue;
486 +                       }
487 +                       /* We need a null-terminated version of the filename. */
488 +                       if (fn[len]) {
489 +                               strlcpy(buf1, fn, len+1);
490 +                               fn = buf1;
491 +                       }
492 +                       if (sanitize_paths) {
493 +                               dirbuf[dirbuf_offset] = '\0';
494 +                               if (!sanitize_path(buf2, fn, dirbuf)) {
495 +                                       rprintf(FERROR,
496 +                                           "merge filename too long: %s\n",
497 +                                           fn);
498 +                                       continue;
499 +                               }
500 +                               len = strlen(fn = buf2);
501 +                       }
502 +                       if (*fn != '/' && dirbuf_offset
503 +                           && (!(mflags & MATCHFLG_PERDIR_MERGE)
504 +                            || *dirbuf == '/')) {
505 +                               len = strlcpy(dirbuf + dirbuf_offset, fn,
506 +                                             MAXPATHLEN - dirbuf_offset)
507 +                                   + dirbuf_offset;
508 +                               if (len >= MAXPATHLEN) {
509 +                                       rprintf(FERROR,
510 +                                           "merge filename too long: %s...\n",
511 +                                           dirbuf);
512 +                                       continue;
513 +                               }
514 +                               fn = dirbuf;
515 +                       }
516 +                       if (mflags & MATCHFLG_PERDIR_MERGE)
517 +                               make_exclude(listp, fn, len, mflags);
518 +                       else {
519 +                               add_exclude_file(listp, fn,
520 +                                                xflags | XFLG_FATAL_ERRORS);
521 +                       }
522 +                       continue;
523                 }
524 +
525 +               make_exclude(listp, cp, pat_len, mflags);
526         }
527  }
528  
529 @@ -343,6 +681,11 @@ void add_exclude_file(struct exclude_lis
530                 return;
531         }
532  
533 +       if (verbose > 2) {
534 +               rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
535 +                       who_am_i(), fname, xflags);
536 +       }
537 +
538         while (1) {
539                 char *s = line;
540                 int ch, overflow = 0;
541 @@ -402,7 +745,11 @@ void send_exclude_list(int f)
542                 if (ent->match_flags & MATCHFLG_INCLUDE) {
543                         write_int(f, l + 2);
544                         write_buf(f, "+ ", 2);
545 -               } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
546 +               } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
547 +                       write_int(f, l + 2);
548 +                       write_buf(f, ". ", 2);
549 +               } else if ((*p == '-' || *p == '+' || *p == '.')
550 +                   && p[1] == ' ') {
551                         write_int(f, l + 2);
552                         write_buf(f, "- ", 2);
553                 } else
554 @@ -411,6 +758,14 @@ void send_exclude_list(int f)
555         }
556  
557         write_int(f, 0);
558 +
559 +#if 0
560 +       /* If we're the receiver and we don't need the excludes, dump them. */
561 +       if (!am_sender && (!delete_mode || delete_excluded)) {
562 +               clear_exclude_list(&exclude_list);
563 +               mergelist_cnt = 0;
564 +       }
565 +#endif
566  }
567  
568  
569 @@ -443,6 +798,7 @@ void add_cvs_excludes(void)
570         char fname[MAXPATHLEN];
571         char *p;
572  
573 +       add_exclude(&exclude_list, ". -p .cvsignore", 0);
574         add_exclude(&exclude_list, default_cvsignore,
575                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
576  
577 --- orig/flist.c        2004-08-05 21:57:29
578 +++ flist.c     2004-08-06 07:56:11
579 @@ -39,10 +39,9 @@ extern int module_id;
580  extern int ignore_errors;
581  extern int numeric_ids;
582  
583 -extern int cvs_exclude;
584 -
585  extern int recurse;
586  extern char curr_dir[MAXPATHLEN];
587 +extern unsigned int curr_dir_len;
588  extern char *files_from;
589  extern int filesfrom_fd;
590  
591 @@ -66,7 +65,6 @@ extern int list_only;
592  
593  extern struct exclude_list_struct exclude_list;
594  extern struct exclude_list_struct server_exclude_list;
595 -extern struct exclude_list_struct local_exclude_list;
596  
597  int io_error;
598  
599 @@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
600   */
601  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
602  {
603 -       int rc;
604 -
605  #if 0 /* This currently never happens, so avoid a useless compare. */
606         if (exclude_level == NO_EXCLUDES)
607                 return 0;
608 @@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
609         if (exclude_level != ALL_EXCLUDES)
610                 return 0;
611         if (exclude_list.head
612 -           && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
613 -               return rc < 0;
614 -       if (local_exclude_list.head
615 -           && check_exclude(&local_exclude_list, fname, is_dir) < 0)
616 +           && check_exclude(&exclude_list, fname, is_dir) < 0)
617                 return 1;
618         return 0;
619  }
620 @@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
621  
622         if (recursive && S_ISDIR(file->mode)
623             && !(file->flags & FLAG_MOUNT_POINT)) {
624 -               struct exclude_list_struct last_list = local_exclude_list;
625 -               local_exclude_list.head = local_exclude_list.tail = NULL;
626                 send_directory(f, flist, f_name_to(file, fbuf));
627 -               if (verbose > 2) {
628 -                       rprintf(FINFO, "[%s] popping %sexclude list\n",
629 -                               who_am_i(), local_exclude_list.debug_type);
630 -               }
631 -               clear_exclude_list(&local_exclude_list);
632 -               local_exclude_list = last_list;
633         }
634  }
635  
636 @@ -973,6 +958,7 @@ static void send_directory(int f, struct
637         struct dirent *di;
638         char fname[MAXPATHLEN];
639         unsigned int offset;
640 +       void *save_excludes;
641         char *p;
642  
643         d = opendir(dir);
644 @@ -996,18 +982,7 @@ static void send_directory(int f, struct
645                 offset++;
646         }
647  
648 -       if (cvs_exclude) {
649 -               if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
650 -                   < MAXPATHLEN - offset) {
651 -                       add_exclude_file(&local_exclude_list, fname,
652 -                                        XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
653 -               } else {
654 -                       io_error |= IOERR_GENERAL;
655 -                       rprintf(FINFO,
656 -                               "cannot cvs-exclude in long-named directory %s\n",
657 -                               full_fname(fname));
658 -               }
659 -       }
660 +       save_excludes = push_local_excludes(fname, offset);
661  
662         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
663                 char *dname = d_name(di);
664 @@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
665                 rsyserr(FERROR, errno, "readdir(%s)", dir);
666         }
667  
668 +       pop_local_excludes(save_excludes);
669 +
670         closedir(d);
671  }
672  
673 @@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f, 
674         char *p, *dir, olddir[sizeof curr_dir];
675         char lastpath[MAXPATHLEN] = "";
676         struct file_list *flist;
677 +       void *first_push = NULL;
678         int64 start_write;
679         int use_ff_fd = 0;
680  
681 @@ -1067,6 +1045,14 @@ struct file_list *send_file_list(int f, 
682                                 exit_cleanup(RERR_FILESELECT);
683                         }
684                         use_ff_fd = 1;
685 +                       if (curr_dir_len < MAXPATHLEN - 1) {
686 +                               char buf[MAXPATHLEN];
687 +                               strcpy(buf, curr_dir);
688 +                               buf[curr_dir_len] = '/';
689 +                               buf[curr_dir_len+1] = '\0';
690 +                               first_push = push_local_excludes(buf,
691 +                                   curr_dir_len+1);
692 +                       }
693                 }
694         }
695  
696 @@ -1097,6 +1083,22 @@ struct file_list *send_file_list(int f, 
697                         }
698                 }
699  
700 +               if (!first_push) {
701 +                       char ch;
702 +                       if ((p = strrchr(fname, '/')) != NULL) {
703 +                               dir = fname;
704 +                               p++;
705 +                       } else {
706 +                               dir = "./";
707 +                               p = dir + 2;
708 +                       }
709 +                       if ((ch = *p) != '\0')
710 +                               *p = '\0';
711 +                       first_push = push_local_excludes(dir, p - dir);
712 +                       if (ch)
713 +                               *p = ch;
714 +               }
715 +
716                 if (link_stat(fname, &st, keep_dirlinks) != 0) {
717                         if (f != -1) {
718                                 io_error |= IOERR_GENERAL;
719 --- orig/options.c      2004-08-05 21:57:29
720 +++ options.c   2004-08-05 21:57:19
721 @@ -389,6 +389,7 @@ static struct poptOption long_options[] 
722    {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
723    {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
724    {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
725 +  {0,                 'E', POPT_ARG_NONE,   0,              'E', 0, 0 },
726    {0,                 'P', POPT_ARG_NONE,   0,              'P', 0, 0 },
727    {"config",           0,  POPT_ARG_STRING, &config_file, 0, 0, 0 },
728    {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
729 @@ -589,6 +590,11 @@ int parse_arguments(int *argc, const cha
730                         am_sender = 1;
731                         break;
732  
733 +               case 'E':
734 +                       add_exclude(&exclude_list,
735 +                                   ". -ip /.rsync-excludes", 0);
736 +                       break;
737 +
738                 case 'P':
739                         do_progress = 1;
740                         keep_partial = 1;
741 --- orig/rsync.h        2004-08-03 15:41:32
742 +++ rsync.h     2004-08-06 06:23:10
743 @@ -499,11 +499,18 @@ struct map_struct {
744  #define MATCHFLG_INCLUDE       (1<<4) /* this is an include, not an exclude */
745  #define MATCHFLG_DIRECTORY     (1<<5) /* this matches only directories */
746  #define MATCHFLG_CLEAR_LIST    (1<<6) /* this item is the "!" token */
747 +#define MATCHFLG_MERGE_FILE    (1<<7) /* specifies a file to merge */
748 +#define MATCHFLG_CVSIGNORE     (1<<8) /* parse this as a .cvsignore file */
749 +#define MATCHFLG_PERDIR_MERGE  (1<<9) /* content is inherited by subdirs */
750 +#define MATCHFLG_INHERIT       (1<<10)/* content is inherited by subdirs */
751  struct exclude_struct {
752         struct exclude_struct *next;
753         char *pattern;
754         unsigned int match_flags;
755 -       int slash_cnt;
756 +       union {
757 +               int slash_cnt;
758 +               struct exclude_list_struct *mergelist;
759 +       } u;
760  };
761  
762  struct exclude_list_struct {
763 --- orig/rsync.yo       2004-08-03 15:34:32
764 +++ rsync.yo    2004-08-06 09:32:25
765 @@ -335,6 +335,7 @@ verb(
766       --include=PATTERN       don't exclude files matching PATTERN
767       --include-from=FILE     don't exclude patterns listed in FILE
768       --files-from=FILE       read FILE for list of source-file names
769 + -E                          same as --exclude='. -pi /.rsync-excludes'
770   -0  --from0                 all file lists are delimited by nulls
771       --version               print version number
772       --daemon                run as an rsync daemon
773 @@ -1086,6 +1087,11 @@ itemize(
774    then it is always considered an exclude pattern, even if specified as
775    part of an include option. The prefix is discarded before matching.
776  
777 +  it() if the pattern starts with ". " (a dot followed by a space) then its
778 +  pattern is taken to be a merge file that is read in to supplement the
779 +  current rules.  See the section on MERGING EXCLUDE FILES for more
780 +  information.
781 +
782    it() if the pattern is a single exclamation mark ! then the current
783    include/exclude list is reset, removing all previously defined patterns.
784  )
785 @@ -1138,6 +1144,97 @@ itemize(
786    it would be excluded by the "*")
787  )
788  
789 +manpagesection(MERGING EXCLUDE FILES)
790 +
791 +You can merge whole files into an exclude file by specifying a rule that
792 +starts with a ". " (a dot followed by a space) and putting a filename in
793 +place of the pattern.  The filename may be preceded by one or more options:
794 +
795 +startdit()
796 +
797 +dit(bf(-i)) All subdirectories of the current directory inherit the rules we
798 +read in from this file.  When applied to a per-directory merge file, the
799 +rules read in are prefixed to the inherited rules from a parent directory,
800 +which gives the newest rules a higher priority than the inherited rules.
801 +
802 +Note also that you can eliminate all the inherited rules for a directory
803 +and its subdirectories by putting the list-clearing token ! at the start of
804 +a per-directory file.  This only clears the rules in the current sub-list,
805 +not all the rules.
806 +
807 +dit(bf(-p)) Make the file a per-directory merge-file.  Rsync will scan
808 +every directory that it traverses for the named file, merging its contents
809 +when the file exists.
810 +
811 +dit(bf(--)) End the scanning of options.  Useful if you want to specify a
812 +filename that begins with a dash.
813 +
814 +enddit()
815 +
816 +Here's an example exclude file (which you'd specify via the normal
817 +--exclude-from=FILE option):
818 +
819 +verb(
820 +    . /home/user/.global_excludes
821 +    - *.gz
822 +    . -pi .excl
823 +    + *.[ch]
824 +    - *.o
825 +)
826 +
827 +This will merge the contents of the /home/user/.global_excludes file at the
828 +start of the list and also turns the ".excl" filename into a per-directory
829 +exclude file whose local contents will be merged into the list in place of
830 +the .excl line.  The rules read in from the .global_excludes file affect
831 +all the directories because it was read in before the directory scanning
832 +began (just as if it were an --exclude-from option).  The rules read in
833 +from each directory's .excl file are inherited by that directory's
834 +subdirectories because the -i option was specified.
835 +
836 +Note also that the parsing of any merge-file named ".cvsignore" is always
837 +done in a CVS-compatible manner, even if -C wasn't specified.  This means
838 +that its rules are always excludes (even if an include option specified the
839 +file), tokens are split on whitespace, the rules are never inherited, and
840 +no special characters are honored.
841 +
842 +Additionally, you can affect where the --cvs-exclude (-C) option's
843 +inclusion of a per-directory .cvsignore file gets placed into your rules by
844 +adding an explicit per-directory merge rule for ".cvsignore".  For example,
845 +specifying this:
846 +
847 +verb(
848 +  rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
849 +)
850 +
851 +will merge all the per-directory .cvsignore rules at the start of your list
852 +rather than at the end.  This allows their dir-specific rules to supersede
853 +your rules instead of being subservient to them.  (The global rules taken
854 +from the $HOME/.cvsignore file and from $CVSIGNORE are not repositioned by
855 +this.)
856 +
857 +If a per-directory merge file is specified with a path that is a parent
858 +directory of the first transfer directory, rsync will scan all the parent
859 +dirs from that starting point to the transfer directory for the indicated
860 +per-directory file.  For instance, the -E option is an abbreviation for
861 +this command:
862 +
863 +verb(
864 +  --exclude='. -pi /.rsync-excludes'
865 +)
866 +
867 +That exclude tells rsync to scan for the file .rsync-excludes in all
868 +directories from the root up through the source of the transfer.  For
869 +example:
870 +
871 +verb(
872 +  rsync -avE /src/path/ /dest/dir
873 +)
874 +
875 +For the above command rsync would try to open /.rsync-excludes and
876 +/src/.rsync-excludes before looking for the file in /src/path.  If you
877 +specify the long version of the option manually and leave off the leading
878 +slash from "/.rsync-excludes", rsync would start scanning in /src/path.
879 +
880  manpagesection(BATCH MODE)
881  
882  bf(Note:) Batch mode should be considered experimental in this version
883 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
884 +++ testsuite/exclude.test      2004-08-06 09:02:29
885 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
886  makepath "$fromdir/foo/down/to/you"
887  makepath "$fromdir/bar/down/to/foo/too"
888  makepath "$fromdir/mid/for/foo/and/that/is/who"
889 +cat >"$fromdir/.excl" <<EOF
890 +.excl
891 +*.bak
892 +*.old
893 +*.junk
894 +EOF
895  echo kept >"$fromdir/foo/file1"
896  echo removed >"$fromdir/foo/file2"
897  echo cvsout >"$fromdir/foo/file2.old"
898 +cat >"$fromdir/foo/.excl" <<EOF
899 ++ .excl
900 +- file1
901 +EOF
902 +cat >"$fromdir/bar/.excl" <<EOF
903 +home-cvs-exclude
904 +. -pi .excl2
905 ++ to
906 +EOF
907  echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
908 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
909 +.excl2
910 +EOF
911  echo keeper >"$fromdir/bar/down/to/foo/file1"
912  echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
913  echo gone >"$fromdir/bar/down/to/foo/file3"
914  echo lost >"$fromdir/bar/down/to/foo/file4"
915  echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
916  echo smashed >"$fromdir/bar/down/to/foo/to"
917 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
918 ++ *.junk
919 +EOF
920 +# This one should be ineffectual
921 +cat >"$fromdir/mid/.excl2" <<EOF
922 +extra
923 +EOF
924  echo cvsout >"$fromdir/mid/one-in-one-out"
925  echo one-in-one-out >"$fromdir/mid/.cvsignore"
926  echo cvsin >"$fromdir/mid/one-for-all"
927 +cat >"$fromdir/mid/.excl" <<EOF
928 +. -p .cvsignore
929 +EOF
930  echo cvsin >"$fromdir/mid/for/one-in-one-out"
931  echo expunged >"$fromdir/mid/for/foo/extra"
932  echo retained >"$fromdir/mid/for/foo/keep"
933 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
934  checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
935      --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
936  
937 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
938 +
939 +rm "$chkdir"/.excl
940 +rm "$chkdir"/foo/file1
941 +rm "$chkdir"/bar/.excl
942 +rm "$chkdir"/bar/down/to/.excl2
943 +rm "$chkdir"/bar/down/to/foo/.excl2
944 +rm "$chkdir"/mid/.excl
945 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
946 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
947 +
948 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
949 +
950 +# Now, test if rsync excludes the same files, this time with a merge-exclude
951 +# file.
952 +
953 +checkit "$RSYNC -avv --exclude='. -pi .excl' --exclude-from=\"$excl\" \
954 +    --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
955 +
956  # The script would have aborted on error, so getting here means we've won.
957  exit 0