- Needed to send the new merge-file options across the socket.
[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 22:58:08
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,244 @@ 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 +/* This returns a fully expanded filename for the merge-file name if the
218 + * name has any slashes in it, otherwise it returns the original name. */
219 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr)
220 +{
221 +       static char buf[MAXPATHLEN];
222 +       char *fn, tmpbuf[MAXPATHLEN];
223 +       unsigned int fn_len, cd_len;
224 +
225 +       cd_len = dirbuf_offset && *dirbuf == '/' ? 0 : curr_dir_len + 1;
226 +       if (cd_len && *merge_file != '/') {
227 +               const char *p;
228 +               if (len_ptr) {
229 +                       for (p = merge_file + *len_ptr;
230 +                            --p > merge_file && *p != '/'; ) {}
231 +               } else
232 +                       p = strchr(merge_file, '/');
233 +               if (!p || p == merge_file)
234 +                       return (char *)merge_file;
235 +       }
236 +
237 +       fn = *merge_file == '/' ? buf : tmpbuf;
238 +       if (sanitize_paths) {
239 +               /* null-terminate the name if it isn't already */
240 +               if (len_ptr && merge_file[*len_ptr]) {
241 +                       char *to = fn == buf ? tmpbuf : buf;
242 +                       strlcpy(to, merge_file, *len_ptr + 1);
243 +                       merge_file = to;
244 +               }
245 +               dirbuf[dirbuf_offset] = '\0';
246 +               if (!sanitize_path(fn, merge_file, dirbuf)) {
247 +                       rprintf(FERROR, "merge filename overflows: %s\n",
248 +                               merge_file);
249 +                       return NULL;
250 +               }
251 +       } else {
252 +               strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
253 +               clean_fname(fn);
254 +       }
255 +       
256 +       if (*fn == '/')
257 +               return fn;
258 +
259 +       fn_len = strlen(fn);
260 +       if (cd_len + dirbuf_offset + fn_len >= MAXPATHLEN) {
261 +               rprintf(FERROR, "merge filename overflows: %s\n", fn);
262 +               return NULL;
263 +       }
264 +       if (cd_len) {
265 +               memcpy(buf, curr_dir, curr_dir_len);
266 +               buf[curr_dir_len] = '/';
267 +       }
268 +       if (dirbuf_offset)
269 +               memcpy(buf + cd_len, dirbuf, dirbuf_offset);
270 +       memcpy(buf + cd_len + dirbuf_offset, fn, fn_len + 1);
271 +       fn_len = clean_fname(buf);
272 +       if (len_ptr)
273 +               *len_ptr = fn_len;
274 +
275 +       return buf;
276 +}
277 +
278 +static void prep_merge_file(struct exclude_struct *ex,
279 +                           struct exclude_list_struct *lp, int flags,
280 +                           char *dir, unsigned int dirlen)
281 +{
282 +       char buf[MAXPATHLEN];
283 +       char *x, *y, *fn = ex->pattern;
284 +
285 +       if (!(fn = parse_merge_name(fn, NULL)) || *fn != '/')
286 +               return;
287 +
288 +       x = strrchr(fn, '/');
289 +       *x = '\0';
290 +       if (dir[dirlen]) /* avoid writing to a read-only string */
291 +               dir[dirlen] = '\0';
292 +       pathjoin(dirbuf, MAXPATHLEN, curr_dir, dir);
293 +       if (dirlen == 2 && *dir == '.') {
294 +               int len = strlen(dirbuf);
295 +               dirbuf[len-2] = '\0';
296 +       }
297 +       if (!*fn)
298 +               fn = "/";
299 +       if (*fn == '/')
300 +               strlcpy(buf, fn, MAXPATHLEN);
301 +       else
302 +               pathjoin(buf, MAXPATHLEN, dirbuf, fn);
303 +       fn = x + 1;
304 +
305 +       clean_fname(buf);
306 +       if (!*buf || buf[1])
307 +               strlcat(buf, "/", MAXPATHLEN);
308 +       for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
309 +       if (*x)
310 +               y += strlen(y);
311 +
312 +       while (*y) {
313 +               char save[MAXPATHLEN];
314 +               strlcpy(save, y, MAXPATHLEN);
315 +               dirbuf_offset = y - dirbuf;
316 +               strlcpy(x, fn, MAXPATHLEN - (x - buf));
317 +               lp->tail = NULL;
318 +               add_exclude_file(lp, buf, flags);
319 +               strlcpy(y, save, MAXPATHLEN);
320 +               while ((*x++ = *y++) != '/') {}
321 +       }
322 +
323 +       if (dirlen == 2 && *dir == '.') /* dir[dirlen-1] is always '/' */
324 +               dirbuf_offset = 0;
325 +       else
326 +               dirbuf_offset = dirlen;
327 +       memcpy(dirbuf, dir, dirbuf_offset);
328 +
329 +       x = ex->pattern;
330 +       ex->pattern = strdup(fn);
331 +       free(x);
332 +}
333 +
334 +void *push_local_excludes(char *dir, unsigned int dirlen)
335 +{
336 +       struct mergelist_save_struct *push;
337 +       struct exclude_list_struct *ap;
338 +       int i;
339 +
340 +       /* Make it easy to construct the full path for a merge-file that was
341 +        * specified with a relative path by saving off the current dir. */
342 +       if (dirlen == 2 && *dir == '.') /* dir[dirlen-1] is always '/' */
343 +               dirbuf_offset = 0;
344 +       else
345 +               dirbuf_offset = dirlen;
346 +       memcpy(dirbuf, dir, dirbuf_offset);
347 +
348 +
349 +       if (!(push = new_array(struct mergelist_save_struct, 1)))
350 +               out_of_memory("push_local_excludes");
351 +
352 +       push->count = mergelist_cnt;
353 +       push->array = new_array(struct exclude_list_struct, mergelist_cnt);
354 +       if (!push->array)
355 +               out_of_memory("push_local_excludes");
356 +
357 +       for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
358 +               memcpy(ap++, mergelist_parents[i]->u.mergelist,
359 +                      sizeof (struct exclude_list_struct));
360 +       }
361 +
362 +       /* Note: add_exclude_file() might increase mergelist_cnt, so keep
363 +        * this loop separate from the above loop. */
364 +       for (i = 0; i < mergelist_cnt; i++) {
365 +               struct exclude_struct *ex = mergelist_parents[i];
366 +               struct exclude_list_struct *lp = ex->u.mergelist;
367 +               int flags;
368 +
369 +               if (verbose > 2) {
370 +                       rprintf(FINFO, "[%s] pushing %sexclude list\n",
371 +                               who_am_i(), lp->debug_type);
372 +               }
373 +
374 +               if (ex->match_flags & MATCHFLG_CVSIGNORE)
375 +                       flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
376 +               else {
377 +                       flags = ex->match_flags & MATCHFLG_INCLUDE
378 +                           ? XFLG_DEF_INCLUDE : 0;
379 +               }
380 +
381 +               if (initialized_mergelist_cnt < i) {
382 +                       prep_merge_file(ex, lp, flags, dir, dirlen);
383 +                       initialized_mergelist_cnt = i;
384 +               }
385 +
386 +               if (!(ex->match_flags & MATCHFLG_INHERIT))
387 +                       lp->head = NULL;
388 +               lp->tail = NULL;
389 +               if (strlcpy(dirbuf +  dirbuf_offset, ex->pattern,
390 +                   MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
391 +                       add_exclude_file(lp, dirbuf, flags);
392 +               else {
393 +                       io_error |= IOERR_GENERAL;
394 +                       rprintf(FINFO,
395 +                           "cannot add local excludes in long-named directory %s\n",
396 +                           full_fname(dirbuf));
397 +               }
398 +       }
399 +
400 +       return (void*)push;
401 +}
402 +
403 +void pop_local_excludes(void *mem)
404 +{
405 +       struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
406 +       struct exclude_list_struct *ap;
407 +       int i;
408 +
409 +       for (i = mergelist_cnt; i-- > 0; ) {
410 +               struct exclude_struct *ex = mergelist_parents[i];
411 +               struct exclude_list_struct *lp = ex->u.mergelist;
412 +
413 +               if (verbose > 2) {
414 +                       rprintf(FINFO, "[%s] popping %sexclude list\n",
415 +                               who_am_i(), lp->debug_type);
416 +               }
417 +
418 +               clear_exclude_list(lp);
419 +       }
420 +
421 +       mergelist_cnt = initialized_mergelist_cnt = pop->count;
422 +       for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
423 +               memcpy(mergelist_parents[i]->u.mergelist, ap++,
424 +                      sizeof (struct exclude_list_struct));
425 +       }
426 +
427 +       free(pop->array);
428 +       free(pop);
429 +}
430 +
431  static int check_one_exclude(char *name, struct exclude_struct *ex,
432                               int name_is_dir)
433  {
434 @@ -122,7 +444,7 @@ static int check_one_exclude(char *name,
435         /* If the pattern does not have any slashes AND it does not have
436          * a "**" (which could match a slash), then we just match the
437          * name portion of the path. */
438 -       if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
439 +       if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
440                 if ((p = strrchr(name,'/')) != NULL)
441                         name = p+1;
442         }
443 @@ -133,7 +455,8 @@ static int check_one_exclude(char *name,
444                 name = full_name;
445         }
446  
447 -       if (!name[0]) return 0;
448 +       if (!name[0])
449 +               return 0;
450  
451         if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
452                 return 0;
453 @@ -148,9 +471,9 @@ static int check_one_exclude(char *name,
454         if (ex->match_flags & MATCHFLG_WILD) {
455                 /* A non-anchored match with an infix slash and no "**"
456                  * needs to match the last slash_cnt+1 name elements. */
457 -               if (!match_start && ex->slash_cnt
458 +               if (!match_start && ex->u.slash_cnt
459                     && !(ex->match_flags & MATCHFLG_WILD2)) {
460 -                       int cnt = ex->slash_cnt + 1;
461 +                       int cnt = ex->u.slash_cnt + 1;
462                         for (p = name + strlen(name) - 1; p >= name; p--) {
463                                 if (*p == '/' && !--cnt)
464                                         break;
465 @@ -221,6 +544,13 @@ int check_exclude(struct exclude_list_st
466         struct exclude_struct *ent;
467  
468         for (ent = listp->head; ent; ent = ent->next) {
469 +               if (ent->match_flags & MATCHFLG_MERGE_FILE) {
470 +                       int rc = check_exclude(ent->u.mergelist, name,
471 +                                              name_is_dir);
472 +                       if (rc)
473 +                               return rc;
474 +                       continue;
475 +               }
476                 if (check_one_exclude(name, ent, name_is_dir)) {
477                         report_exclude_result(name, ent, name_is_dir,
478                                               listp->debug_type);
479 @@ -253,12 +583,41 @@ static const char *get_exclude_tok(const
480                 p = (const char *)s;
481         }
482  
483 -       /* Is this a '+' or '-' followed by a space (not whitespace)? */
484 +       /* Check for a +/-/. followed by a space (not whitespace). */
485         if (!(xflags & XFLG_WORDS_ONLY)
486 -           && (*s == '-' || *s == '+') && s[1] == ' ') {
487 +           && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
488                 if (*s == '+')
489                         mflags |= MATCHFLG_INCLUDE;
490 +               else if (*s == '.') {
491 +                       mflags |= MATCHFLG_MERGE_FILE;
492 +                       if (xflags & XFLG_DEF_INCLUDE)
493 +                               mflags |= MATCHFLG_INCLUDE;
494 +                       while (s[2] == '-') {
495 +                               s += 2;
496 +                               do {
497 +                                       switch (*++s) {
498 +                                       case 'i':
499 +                                               mflags |= MATCHFLG_INHERIT;
500 +                                               break;
501 +                                       case 'p':
502 +                                               mflags |= MATCHFLG_PERDIR_MERGE;
503 +                                               break;
504 +                                       case '-':
505 +                                               if (s[1] == ' ')
506 +                                                       goto done;
507 +                                       default:
508 +                                               rprintf(FERROR,
509 +                                                   "invalid merge options: %s\n",
510 +                                                   p);
511 +                                               exit_cleanup(RERR_SYNTAX);
512 +                                       }
513 +                               } while (s[1] != ' ');
514 +                       }
515 +               }
516 +           done:
517                 s += 2;
518 +               if (mflags & MATCHFLG_MERGE_FILE && *s == '.' && s[1] == '/')
519 +                       s += 2;
520         } else if (xflags & XFLG_DEF_INCLUDE)
521                 mflags |= MATCHFLG_INCLUDE;
522  
523 @@ -292,9 +651,15 @@ void add_exclude(struct exclude_list_str
524         cp = pattern;
525         pat_len = 0;
526         while (1) {
527 +               /* Remember that the returned string is NOT '\0' terminated! */
528                 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
529                 if (!pat_len)
530                         break;
531 +               if (pat_len >= MAXPATHLEN) {
532 +                       rprintf(FERROR, "discarding over-long exclude: %s\n",
533 +                               cp);
534 +                       continue;
535 +               }
536  
537                 if (mflags & MATCHFLG_CLEAR_LIST) {
538                         if (verbose > 2) {
539 @@ -306,13 +671,23 @@ void add_exclude(struct exclude_list_str
540                         continue;
541                 }
542  
543 -               make_exclude(listp, cp, pat_len, mflags);
544 -
545 -               if (verbose > 2) {
546 -                       rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
547 -                               who_am_i(), (int)pat_len, cp, listp->debug_type,
548 -                               mflags & MATCHFLG_INCLUDE ? "in" : "ex");
549 +               if (mflags & MATCHFLG_MERGE_FILE) {
550 +                       if (mflags & MATCHFLG_PERDIR_MERGE) {
551 +                               if (dirbuf_offset && *dirbuf == '/') {
552 +                                       if (!(cp = parse_merge_name(cp, &pat_len)))
553 +                                               continue;
554 +                                       make_exclude(listp, cp, pat_len, mflags);
555 +                                       continue;
556 +                               }
557 +                       } else {
558 +                               if (!(cp = parse_merge_name(cp, &pat_len)))
559 +                                       continue;
560 +                               add_exclude_file(listp, cp, xflags | XFLG_FATAL_ERRORS);
561 +                               continue;
562 +                       }
563                 }
564 +
565 +               make_exclude(listp, cp, pat_len, mflags);
566         }
567  }
568  
569 @@ -321,7 +696,7 @@ void add_exclude_file(struct exclude_lis
570                       int xflags)
571  {
572         FILE *fp;
573 -       char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
574 +       char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
575         char *eob = line + sizeof line - 1;
576         int word_split = xflags & XFLG_WORD_SPLIT;
577  
578 @@ -343,6 +718,11 @@ void add_exclude_file(struct exclude_lis
579                 return;
580         }
581  
582 +       if (verbose > 2) {
583 +               rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
584 +                       who_am_i(), fname, xflags);
585 +       }
586 +
587         while (1) {
588                 char *s = line;
589                 int ch, overflow = 0;
590 @@ -402,7 +782,21 @@ void send_exclude_list(int f)
591                 if (ent->match_flags & MATCHFLG_INCLUDE) {
592                         write_int(f, l + 2);
593                         write_buf(f, "+ ", 2);
594 -               } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
595 +               } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
596 +                       char buf[32], *op = buf;
597 +                       *op++ = '.';
598 +                       *op++ = ' ';
599 +                       if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
600 +                               *op++ = '-';
601 +                               *op++ = 'p';
602 +                               if (ent->match_flags & MATCHFLG_INHERIT)
603 +                                       *op++ = 'i';
604 +                               *op++ = ' ';
605 +                       }
606 +                       write_int(f, l + (op - buf));
607 +                       write_buf(f, buf, op - buf);
608 +               } else if ((*p == '-' || *p == '+' || *p == '.')
609 +                   && p[1] == ' ') {
610                         write_int(f, l + 2);
611                         write_buf(f, "- ", 2);
612                 } else
613 @@ -443,6 +837,7 @@ void add_cvs_excludes(void)
614         char fname[MAXPATHLEN];
615         char *p;
616  
617 +       add_exclude(&exclude_list, ". -p .cvsignore", 0);
618         add_exclude(&exclude_list, default_cvsignore,
619                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
620  
621 --- orig/flist.c        2004-08-05 21:57:29
622 +++ flist.c     2004-08-06 20:52:26
623 @@ -39,10 +39,9 @@ extern int module_id;
624  extern int ignore_errors;
625  extern int numeric_ids;
626  
627 -extern int cvs_exclude;
628 -
629  extern int recurse;
630  extern char curr_dir[MAXPATHLEN];
631 +extern unsigned int curr_dir_len;
632  extern char *files_from;
633  extern int filesfrom_fd;
634  
635 @@ -66,7 +65,6 @@ extern int list_only;
636  
637  extern struct exclude_list_struct exclude_list;
638  extern struct exclude_list_struct server_exclude_list;
639 -extern struct exclude_list_struct local_exclude_list;
640  
641  int io_error;
642  
643 @@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
644   */
645  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
646  {
647 -       int rc;
648 -
649  #if 0 /* This currently never happens, so avoid a useless compare. */
650         if (exclude_level == NO_EXCLUDES)
651                 return 0;
652 @@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
653         if (exclude_level != ALL_EXCLUDES)
654                 return 0;
655         if (exclude_list.head
656 -           && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
657 -               return rc < 0;
658 -       if (local_exclude_list.head
659 -           && check_exclude(&local_exclude_list, fname, is_dir) < 0)
660 +           && check_exclude(&exclude_list, fname, is_dir) < 0)
661                 return 1;
662         return 0;
663  }
664 @@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
665  
666         if (recursive && S_ISDIR(file->mode)
667             && !(file->flags & FLAG_MOUNT_POINT)) {
668 -               struct exclude_list_struct last_list = local_exclude_list;
669 -               local_exclude_list.head = local_exclude_list.tail = NULL;
670                 send_directory(f, flist, f_name_to(file, fbuf));
671 -               if (verbose > 2) {
672 -                       rprintf(FINFO, "[%s] popping %sexclude list\n",
673 -                               who_am_i(), local_exclude_list.debug_type);
674 -               }
675 -               clear_exclude_list(&local_exclude_list);
676 -               local_exclude_list = last_list;
677         }
678  }
679  
680 @@ -973,6 +958,7 @@ static void send_directory(int f, struct
681         struct dirent *di;
682         char fname[MAXPATHLEN];
683         unsigned int offset;
684 +       void *save_excludes;
685         char *p;
686  
687         d = opendir(dir);
688 @@ -996,18 +982,7 @@ static void send_directory(int f, struct
689                 offset++;
690         }
691  
692 -       if (cvs_exclude) {
693 -               if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
694 -                   < MAXPATHLEN - offset) {
695 -                       add_exclude_file(&local_exclude_list, fname,
696 -                                        XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
697 -               } else {
698 -                       io_error |= IOERR_GENERAL;
699 -                       rprintf(FINFO,
700 -                               "cannot cvs-exclude in long-named directory %s\n",
701 -                               full_fname(fname));
702 -               }
703 -       }
704 +       save_excludes = push_local_excludes(fname, offset);
705  
706         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
707                 char *dname = d_name(di);
708 @@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
709                 rsyserr(FERROR, errno, "readdir(%s)", dir);
710         }
711  
712 +       pop_local_excludes(save_excludes);
713 +
714         closedir(d);
715  }
716  
717 @@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f, 
718         char *p, *dir, olddir[sizeof curr_dir];
719         char lastpath[MAXPATHLEN] = "";
720         struct file_list *flist;
721 +       void *first_push = NULL;
722         int64 start_write;
723         int use_ff_fd = 0;
724  
725 @@ -1067,6 +1045,14 @@ struct file_list *send_file_list(int f, 
726                                 exit_cleanup(RERR_FILESELECT);
727                         }
728                         use_ff_fd = 1;
729 +                       if (curr_dir_len < MAXPATHLEN - 1) {
730 +                               char buf[MAXPATHLEN];
731 +                               strcpy(buf, curr_dir);
732 +                               buf[curr_dir_len] = '/';
733 +                               buf[curr_dir_len+1] = '\0';
734 +                               first_push = push_local_excludes(buf,
735 +                                   curr_dir_len+1);
736 +                       }
737                 }
738         }
739  
740 @@ -1097,6 +1083,22 @@ struct file_list *send_file_list(int f, 
741                         }
742                 }
743  
744 +               if (!first_push) {
745 +                       char ch;
746 +                       if ((p = strrchr(fname, '/')) != NULL) {
747 +                               dir = fname;
748 +                               p++;
749 +                       } else {
750 +                               dir = "./";
751 +                               p = dir + 2;
752 +                       }
753 +                       if ((ch = *p) != '\0')
754 +                               *p = '\0';
755 +                       first_push = push_local_excludes(dir, p - dir);
756 +                       if (ch)
757 +                               *p = ch;
758 +               }
759 +
760                 if (link_stat(fname, &st, keep_dirlinks) != 0) {
761                         if (f != -1) {
762                                 io_error |= IOERR_GENERAL;
763 --- orig/options.c      2004-08-05 21:57:29
764 +++ options.c   2004-08-05 21:57:19
765 @@ -389,6 +389,7 @@ static struct poptOption long_options[] 
766    {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
767    {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
768    {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
769 +  {0,                 'E', POPT_ARG_NONE,   0,              'E', 0, 0 },
770    {0,                 'P', POPT_ARG_NONE,   0,              'P', 0, 0 },
771    {"config",           0,  POPT_ARG_STRING, &config_file, 0, 0, 0 },
772    {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
773 @@ -589,6 +590,11 @@ int parse_arguments(int *argc, const cha
774                         am_sender = 1;
775                         break;
776  
777 +               case 'E':
778 +                       add_exclude(&exclude_list,
779 +                                   ". -ip /.rsync-excludes", 0);
780 +                       break;
781 +
782                 case 'P':
783                         do_progress = 1;
784                         keep_partial = 1;
785 --- orig/rsync.h        2004-08-03 15:41:32
786 +++ rsync.h     2004-08-06 06:23:10
787 @@ -499,11 +499,18 @@ struct map_struct {
788  #define MATCHFLG_INCLUDE       (1<<4) /* this is an include, not an exclude */
789  #define MATCHFLG_DIRECTORY     (1<<5) /* this matches only directories */
790  #define MATCHFLG_CLEAR_LIST    (1<<6) /* this item is the "!" token */
791 +#define MATCHFLG_MERGE_FILE    (1<<7) /* specifies a file to merge */
792 +#define MATCHFLG_CVSIGNORE     (1<<8) /* parse this as a .cvsignore file */
793 +#define MATCHFLG_PERDIR_MERGE  (1<<9) /* content is inherited by subdirs */
794 +#define MATCHFLG_INHERIT       (1<<10)/* content is inherited by subdirs */
795  struct exclude_struct {
796         struct exclude_struct *next;
797         char *pattern;
798         unsigned int match_flags;
799 -       int slash_cnt;
800 +       union {
801 +               int slash_cnt;
802 +               struct exclude_list_struct *mergelist;
803 +       } u;
804  };
805  
806  struct exclude_list_struct {
807 --- orig/rsync.yo       2004-08-03 15:34:32
808 +++ rsync.yo    2004-08-06 17:05:29
809 @@ -335,6 +335,7 @@ verb(
810       --include=PATTERN       don't exclude files matching PATTERN
811       --include-from=FILE     don't exclude patterns listed in FILE
812       --files-from=FILE       read FILE for list of source-file names
813 + -E                          same as --exclude='. -pi /.rsync-excludes'
814   -0  --from0                 all file lists are delimited by nulls
815       --version               print version number
816       --daemon                run as an rsync daemon
817 @@ -1086,6 +1087,11 @@ itemize(
818    then it is always considered an exclude pattern, even if specified as
819    part of an include option. The prefix is discarded before matching.
820  
821 +  it() if the pattern starts with ". " (a dot followed by a space) then its
822 +  pattern is taken to be a merge file that is read in to supplement the
823 +  current rules.  See the section on MERGING EXCLUDE FILES for more
824 +  information.
825 +
826    it() if the pattern is a single exclamation mark ! then the current
827    include/exclude list is reset, removing all previously defined patterns.
828  )
829 @@ -1138,6 +1144,106 @@ itemize(
830    it would be excluded by the "*")
831  )
832  
833 +manpagesection(MERGING EXCLUDE FILES)
834 +
835 +You can merge whole files into an exclude file by specifying a rule that
836 +starts with a ". " (a dot followed by a space) and putting a filename in
837 +place of the pattern.  The filename may be preceded by one or more options:
838 +
839 +startdit()
840 +
841 +dit(bf(-i)) All subdirectories of the current directory inherit the rules we
842 +read in from this file.  Only affects a per-directory merge file.  The
843 +rules read in are prefixed to the inherited rules from a parent directory,
844 +which gives the newest rules a higher priority than the inherited rules.
845 +
846 +Note also that you can eliminate all the inherited rules for a directory
847 +and its subdirectories by putting the list-clearing token ! at the start of
848 +a per-directory file.  This only clears the rules in the current sub-list,
849 +not all the rules.
850 +
851 +dit(bf(-p)) Make the file a per-directory merge-file.  Rsync will scan
852 +every directory that it traverses for the named file, merging its contents
853 +when the file exists.  Without this option rsync just merges the rules into
854 +the parent file, giving them the same attributes as the parent.
855 +
856 +dit(bf(--)) End the scanning of options.  Useful if you want to specify a
857 +filename that begins with a dash.
858 +
859 +enddit()
860 +
861 +Here's an example exclude file (which you'd specify via the normal
862 +--exclude-from=FILE option):
863 +
864 +verb(
865 +    . /home/user/.global_excludes
866 +    *.gz
867 +    . -pi .excl
868 +    + *.[ch]
869 +    *.o
870 +)
871 +
872 +This will merge the contents of the /home/user/.global_excludes file at the
873 +start of the list and also turns the ".excl" filename into a per-directory
874 +exclude file.  The rules read in from the .global_excludes file affect all
875 +the directories because it is is being merged into an --exclude-from
876 +option.  The rules merged from each directory's .excl file are inherited
877 +by each directory's subdirectories because the -i option was specified
878 +(without -i the rules would only affect the directory where they were read
879 +in).  All the merged rules default to being exclude rules because an
880 +exclude statement was used to specify them.
881 +
882 +Note also that the parsing of any merge-file named ".cvsignore" is always
883 +done in a CVS-compatible manner, even if -C wasn't specified.  This means
884 +that its rules are always excludes (even if an include option specified the
885 +file), tokens are split on whitespace, the rules are never inherited, and
886 +no special characters are honored (e.g. no comments, no "!", etc.).
887 +
888 +Additionally, you can affect where the --cvs-exclude (-C) option's
889 +inclusion of the per-directory .cvsignore file gets placed into your rules
890 +by adding your own explicit per-directory merge rule for ".cvsignore".
891 +Without this rsync would add its this rule at the end of all your other
892 +rules (giving it a lower priority than your command-line rules).  For
893 +example, specifying this:
894 +
895 +verb(
896 +  rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
897 +)
898 +
899 +will merge all the per-directory .cvsignore rules at the start of your list
900 +rather than at the end.  This allows their dir-specific rules to supersede
901 +your rules instead of being subservient to them.  (The global rules taken
902 +from the $HOME/.cvsignore file and from $CVSIGNORE are not repositioned by
903 +this.)
904 +
905 +If a per-directory merge file is specified with a path that is a parent
906 +directory of the first transfer directory, rsync will scan all the parent
907 +dirs from that starting point to the transfer directory for the indicated
908 +per-directory file.  For instance, the -E option is an abbreviation for
909 +this command:
910 +
911 +verb(
912 +  --exclude='. -pi /.rsync-excludes'
913 +)
914 +
915 +That exclude tells rsync to scan for the file .rsync-excludes in all
916 +directories from the root up through the source of the transfer.  For an
917 +rsync daemon, the root dir is always the module's "path" setting, not the
918 +root of the filesystem (unless the two are the same).
919 +
920 +Some examples of this pre-scanning for per-directory files:
921 +
922 +verb(
923 +  rsync -avE /src/path/ /dest/dir
924 +  rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
925 +  rsync -av --exclude='. -pi .rsync-excludes' /src/path/ /dest/dir
926 +)
927 +
928 +The first two commands above will look for .rsync-excludes in "/" and
929 +"/src" before the normal scan begins looking for the file in "/src/path"
930 +and its subdirectories.  The last command above just starts scanning
931 +from "/src/path".
932 +
933  manpagesection(BATCH MODE)
934  
935  bf(Note:) Batch mode should be considered experimental in this version
936 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
937 +++ testsuite/exclude.test      2004-08-06 09:02:29
938 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
939  makepath "$fromdir/foo/down/to/you"
940  makepath "$fromdir/bar/down/to/foo/too"
941  makepath "$fromdir/mid/for/foo/and/that/is/who"
942 +cat >"$fromdir/.excl" <<EOF
943 +.excl
944 +*.bak
945 +*.old
946 +*.junk
947 +EOF
948  echo kept >"$fromdir/foo/file1"
949  echo removed >"$fromdir/foo/file2"
950  echo cvsout >"$fromdir/foo/file2.old"
951 +cat >"$fromdir/foo/.excl" <<EOF
952 ++ .excl
953 +- file1
954 +EOF
955 +cat >"$fromdir/bar/.excl" <<EOF
956 +home-cvs-exclude
957 +. -pi .excl2
958 ++ to
959 +EOF
960  echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
961 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
962 +.excl2
963 +EOF
964  echo keeper >"$fromdir/bar/down/to/foo/file1"
965  echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
966  echo gone >"$fromdir/bar/down/to/foo/file3"
967  echo lost >"$fromdir/bar/down/to/foo/file4"
968  echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
969  echo smashed >"$fromdir/bar/down/to/foo/to"
970 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
971 ++ *.junk
972 +EOF
973 +# This one should be ineffectual
974 +cat >"$fromdir/mid/.excl2" <<EOF
975 +extra
976 +EOF
977  echo cvsout >"$fromdir/mid/one-in-one-out"
978  echo one-in-one-out >"$fromdir/mid/.cvsignore"
979  echo cvsin >"$fromdir/mid/one-for-all"
980 +cat >"$fromdir/mid/.excl" <<EOF
981 +. -p .cvsignore
982 +EOF
983  echo cvsin >"$fromdir/mid/for/one-in-one-out"
984  echo expunged >"$fromdir/mid/for/foo/extra"
985  echo retained >"$fromdir/mid/for/foo/keep"
986 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
987  checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
988      --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
989  
990 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
991 +
992 +rm "$chkdir"/.excl
993 +rm "$chkdir"/foo/file1
994 +rm "$chkdir"/bar/.excl
995 +rm "$chkdir"/bar/down/to/.excl2
996 +rm "$chkdir"/bar/down/to/foo/.excl2
997 +rm "$chkdir"/mid/.excl
998 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
999 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1000 +
1001 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1002 +
1003 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1004 +# file.
1005 +
1006 +checkit "$RSYNC -avv --exclude='. -pi .excl' --exclude-from=\"$excl\" \
1007 +    --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1008 +
1009  # The script would have aborted on error, so getting here means we've won.
1010  exit 0