- Added modifiers "C" and "E" to the include/exclude +/- rules: -C is
[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 either "+m FILE" (for merged includes) or "-m FILE" (for merged
8 excludes).  It also lets you specify either "+p FILE" or "-p FILE" in
9 order to specify a per-directory merge file -- one that will be looked
10 for in every sub-directory that rsync visits, and the rules found in
11 that subdirectory's file will affect that dir and (if desired) its
12 subdirs.
13
14 For example:
15
16   rsync -av --exclude='-p .excl' from/ to
17
18 The above will look for a file named ".excl" in every directory of the
19 hierarchy that rsync visits, and it will exclude (by default) names
20 based on the rules found therein.  If one of the .excl files contains
21 this:
22
23   + *.c
24   -p .excl2
25   -m .excl3
26   *.o
27   /foobar
28
29 Then the file ".excl2" will also be read in from the current dir and all
30 its subdirs.  The file ".excl3" would just be read in from the current
31 dir.  The exclusion of "foobar" will only happen in that .excl file's
32 directory because the rule is anchored, so that's how you can make rules
33 local instead of inherited.
34
35 ..wayne..
36
37 --- orig/clientserver.c 2005-01-01 21:11:00
38 +++ clientserver.c      2004-08-10 15:44:15
39 @@ -49,12 +49,14 @@ extern int no_detach;
40  extern int default_af_hint;
41  extern char *bind_address;
42  extern struct exclude_list_struct server_exclude_list;
43 -extern char *exclude_path_prefix;
44  extern char *config_file;
45  extern char *files_from;
46  
47  char *auth_user;
48  
49 +/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
50 +unsigned int module_dirlen = 0;
51 +
52  /**
53   * Run a client connected to an rsyncd.  The alternative to this
54   * function for remote-shell connections is do_cmd().
55 @@ -304,26 +306,28 @@ static int rsync_module(int f_in, int f_
56         /* TODO: Perhaps take a list of gids, and make them into the
57          * supplementary groups. */
58  
59 -       exclude_path_prefix = use_chroot? "" : lp_path(i);
60 -       if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
61 -               exclude_path_prefix = "";
62 +       if (use_chroot) {
63 +               module_dirlen = 0;
64 +               set_excludes_dir("/", 1);
65 +       } else {
66 +               module_dirlen = strlen(lp_path(i));
67 +               set_excludes_dir(lp_path(i), module_dirlen);
68 +       }
69  
70         p = lp_include_from(i);
71         add_exclude_file(&server_exclude_list, p,
72 -                        XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
73 +                        XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
74  
75         p = lp_include(i);
76         add_exclude(&server_exclude_list, p,
77 -                   XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
78 +                   XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
79  
80         p = lp_exclude_from(i);
81         add_exclude_file(&server_exclude_list, p,
82 -                        XFLG_FATAL_ERRORS);
83 +                        XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
84  
85         p = lp_exclude(i);
86 -       add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
87 -
88 -       exclude_path_prefix = NULL;
89 +       add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
90  
91         log_init();
92  
93 --- orig/exclude.c      2005-01-13 23:15:56
94 +++ exclude.c   2005-01-16 01:05:56
95 @@ -30,13 +30,69 @@ extern int verbose;
96  extern int eol_nulls;
97  extern int list_only;
98  extern int recurse;
99 +extern int io_error;
100 +extern int sanitize_paths;
101  
102  extern char curr_dir[];
103 +extern unsigned int curr_dir_len;
104 +extern unsigned int module_dirlen;
105  
106  struct exclude_list_struct exclude_list = { 0, 0, "" };
107 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
108  struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
109 -char *exclude_path_prefix = NULL;
110 +
111 +struct mergelist_save_struct {
112 +    struct exclude_list_struct *array;
113 +    int count;
114 +};
115 +
116 +/* The dirbuf is set by push_local_excludes() to the current subdirectory
117 + * relative to curr_dir that is being processed.  The path always has a
118 + * trailing slash appended, and the variable dirbuf_len contains the length
119 + * of this path prefix.  The path is always absolute. */
120 +static char dirbuf[MAXPATHLEN+1];
121 +static unsigned int dirbuf_len = 0;
122 +static int dirbuf_depth;
123 +
124 +/* This is True when we're scanning parent dirs for per-dir merge-files. */
125 +static BOOL parent_dirscan = False;
126 +
127 +/* This array contains a list of all the currently active per-dir merge
128 + * files.  This makes it easier to save the appropriate values when we
129 + * "push" down into each subdirectory. */
130 +static struct exclude_struct **mergelist_parents;
131 +static int mergelist_cnt = 0;
132 +static int mergelist_size = 0;
133 +
134 +/* Each exclude_list_struct describes a singly-linked list by keeping track
135 + * of both the head and tail pointers.  The list is slightly unusual in that
136 + * a parent-dir's content can be appended to the end of the local list in a
137 + * special way:  the last item in the local list has its "next" pointer set
138 + * to point to the inherited list, but the local list's tail pointer points
139 + * at the end of the local list.  Thus, if the local list is empty, the head
140 + * will be pointing at the inherited content but the tail will be NULL.  To
141 + * help you visualize this, here are the possible list arrangements:
142 + *
143 + * Completely Empty                     Local Content Only
144 + * ==================================   ====================================
145 + * head -> NULL                         head -> Local1 -> Local2 -> NULL
146 + * tail -> NULL                         tail -------------^
147 + *
148 + * Inherited Content Only               Both Local and Inherited Content
149 + * ==================================   ====================================
150 + * head -> Parent1 -> Parent2 -> NULL   head -> L1 -> L2 -> P1 -> P2 -> NULL
151 + * tail -> NULL                         tail ---------^
152 + *
153 + * This means that anyone wanting to traverse the whole list to use it just
154 + * needs to start at the head and use the "next" pointers until it goes
155 + * NULL.  To add new local content, we insert the item after the tail item
156 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
157 + * head).  To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
158 + * because it is shared between the current list and our parent list(s).
159 + * The easiest way to handle this is to simply truncate the list after the
160 + * tail item and then free the local list from the head.  When inheriting
161 + * the list for a new local dir, we just save off the exclude_list_struct
162 + * values (so we can pop back to them later) and set the tail to NULL.
163 + */
164  
165  /** Build an exclude structure given an exclude pattern. */
166  static void make_exclude(struct exclude_list_struct *listp, const char *pat,
167 @@ -46,23 +102,44 @@ static void make_exclude(struct exclude_
168         const char *cp;
169         unsigned int ex_len;
170  
171 +       if (verbose > 2) {
172 +               rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
173 +                       who_am_i(), (int)pat_len, pat, listp->debug_type,
174 +                       mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
175 +                       mflags & MATCHFLG_INCLUDE ? "in" : "ex");
176 +       }
177 +
178 +       if (mflags & MATCHFLG_MERGE_FILE) {
179 +               int i;
180 +               /* If the local merge file was already mentioned, don't
181 +                * add it again. */
182 +               for (i = 0; i < mergelist_cnt; i++) {
183 +                       struct exclude_struct *ex = mergelist_parents[i];
184 +                       if (strlen(ex->pattern) == pat_len
185 +                           && memcmp(ex->pattern, pat, pat_len) == 0)
186 +                               return;
187 +               }
188 +       }
189 +
190         ret = new(struct exclude_struct);
191         if (!ret)
192                 out_of_memory("make_exclude");
193  
194         memset(ret, 0, sizeof ret[0]);
195  
196 -       if (exclude_path_prefix)
197 -               mflags |= MATCHFLG_ABS_PATH;
198 -       if (exclude_path_prefix && *pat == '/')
199 -               ex_len = strlen(exclude_path_prefix);
200 -       else
201 +       if (mflags & MATCHFLG_ABS_PATH) {
202 +               if (*pat != '/') {
203 +                       mflags &= ~MATCHFLG_ABS_PATH;
204 +                       ex_len = 0;
205 +               } else
206 +                       ex_len = dirbuf_len - module_dirlen - 1;
207 +       } else
208                 ex_len = 0;
209         ret->pattern = new_array(char, ex_len + pat_len + 1);
210         if (!ret->pattern)
211                 out_of_memory("make_exclude");
212         if (ex_len)
213 -               memcpy(ret->pattern, exclude_path_prefix, ex_len);
214 +               memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
215         strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
216         pat_len += ex_len;
217  
218 @@ -81,14 +158,40 @@ static void make_exclude(struct exclude_
219                 mflags |= MATCHFLG_DIRECTORY;
220         }
221  
222 -       for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
223 -               ret->slash_cnt++;
224 +       if (mflags & MATCHFLG_MERGE_FILE) {
225 +               struct exclude_list_struct *lp
226 +                   = new_array(struct exclude_list_struct, 1);
227 +               if (!lp)
228 +                       out_of_memory("make_exclude");
229 +               lp->head = lp->tail = NULL;
230 +               if ((cp = strrchr(ret->pattern, '/')) != NULL)
231 +                       cp++;
232 +               else
233 +                       cp = ret->pattern;
234 +               if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
235 +                       out_of_memory("make_exclude");
236 +               ret->u.mergelist = lp;
237 +               if (mergelist_cnt == mergelist_size) {
238 +                       mergelist_size += 5;
239 +                       mergelist_parents = realloc_array(mergelist_parents,
240 +                                               struct exclude_struct *,
241 +                                               mergelist_size);
242 +                       if (!mergelist_parents)
243 +                               out_of_memory("make_exclude");
244 +               }
245 +               mergelist_parents[mergelist_cnt++] = ret;
246 +       } else {
247 +               for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
248 +                       ret->u.slash_cnt++;
249 +       }
250  
251         ret->match_flags = mflags;
252  
253 -       if (!listp->tail)
254 +       if (!listp->tail) {
255 +               ret->next = listp->head;
256                 listp->head = listp->tail = ret;
257 -       else {
258 +       } else {
259 +               ret->next = listp->tail->next;
260                 listp->tail->next = ret;
261                 listp->tail = ret;
262         }
263 @@ -96,22 +199,267 @@ static void make_exclude(struct exclude_
264  
265  static void free_exclude(struct exclude_struct *ex)
266  {
267 +       if (ex->match_flags & MATCHFLG_MERGE_FILE) {
268 +               free(ex->u.mergelist->debug_type);
269 +               free(ex->u.mergelist);
270 +       }
271         free(ex->pattern);
272         free(ex);
273  }
274  
275 -void clear_exclude_list(struct exclude_list_struct *listp)
276 +static void clear_exclude_list(struct exclude_list_struct *listp)
277  {
278 -       struct exclude_struct *ent, *next;
279 -
280 -       for (ent = listp->head; ent; ent = next) {
281 -               next = ent->next;
282 -               free_exclude(ent);
283 +       if (listp->tail) {
284 +               struct exclude_struct *ent, *next;
285 +               /* Truncate any inherited items from the local list. */
286 +               listp->tail->next = NULL;
287 +               /* Now free everything that is left. */
288 +               for (ent = listp->head; ent; ent = next) {
289 +                       next = ent->next;
290 +                       free_exclude(ent);
291 +               }
292         }
293  
294         listp->head = listp->tail = NULL;
295  }
296  
297 +/* This returns an expanded (absolute) filename for the merge-file name if
298 + * the name has any slashes in it OR if the parent_dirscan var is True;
299 + * otherwise it returns the original merge_file name.  If the len_ptr value
300 + * is non-NULL the merge_file name is limited by the referenced length
301 + * value and will be updated with the length of the resulting name.  We
302 + * always return a name that is null terminated, even if the merge_file
303 + * name was not. */
304 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
305 +                             unsigned int prefix_skip)
306 +{
307 +       static char buf[MAXPATHLEN];
308 +       char *fn, tmpbuf[MAXPATHLEN];
309 +       unsigned int fn_len;
310 +
311 +       if (!parent_dirscan && *merge_file != '/') {
312 +               /* Return the name unchanged it doesn't have any slashes. */
313 +               if (len_ptr) {
314 +                       const char *p = merge_file + *len_ptr;
315 +                       while (--p > merge_file && *p != '/') {}
316 +                       if (p == merge_file) {
317 +                               strlcpy(buf, merge_file, *len_ptr + 1);
318 +                               return buf;
319 +                       }
320 +               } else if (strchr(merge_file, '/') == NULL)
321 +                       return (char *)merge_file;
322 +       }
323 +
324 +       fn = *merge_file == '/' ? buf : tmpbuf;
325 +       if (sanitize_paths) {
326 +               const char *r = prefix_skip ? "/" : NULL;
327 +               /* null-terminate the name if it isn't already */
328 +               if (len_ptr && merge_file[*len_ptr]) {
329 +                       char *to = fn == buf ? tmpbuf : buf;
330 +                       strlcpy(to, merge_file, *len_ptr + 1);
331 +                       merge_file = to;
332 +               }
333 +               if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
334 +                       rprintf(FERROR, "merge-file name overflows: %s\n",
335 +                               merge_file);
336 +                       return NULL;
337 +               }
338 +       } else {
339 +               strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
340 +               clean_fname(fn, 1);
341 +       }
342 +       
343 +       fn_len = strlen(fn);
344 +       if (fn == buf)
345 +               goto done;
346 +
347 +       if (dirbuf_len + fn_len >= MAXPATHLEN) {
348 +               rprintf(FERROR, "merge-file name overflows: %s\n", fn);
349 +               return NULL;
350 +       }
351 +       memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
352 +       memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
353 +       fn_len = clean_fname(buf, 1);
354 +
355 +    done:
356 +       if (len_ptr)
357 +               *len_ptr = fn_len;
358 +       return buf;
359 +}
360 +
361 +/* Sets the dirbuf and dirbuf_len values. */
362 +void set_excludes_dir(const char *dir, unsigned int dirlen)
363 +{
364 +       unsigned int len;
365 +       if (*dir != '/') {
366 +               memcpy(dirbuf, curr_dir, curr_dir_len);
367 +               dirbuf[curr_dir_len] = '/';
368 +               len = curr_dir_len + 1;
369 +               if (len + dirlen >= MAXPATHLEN)
370 +                       dirlen = 0;
371 +       } else
372 +               len = 0;
373 +       memcpy(dirbuf + len, dir, dirlen);
374 +       dirbuf[dirlen + len] = '\0';
375 +       dirbuf_len = clean_fname(dirbuf, 1);
376 +       if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
377 +           && dirbuf[dirbuf_len-2] == '/')
378 +               dirbuf_len -= 2;
379 +       dirbuf[dirbuf_len++] = '/';
380 +       dirbuf[dirbuf_len] = '\0';
381 +       if (sanitize_paths)
382 +               dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
383 +}
384 +
385 +/* This routine takes a per-dir merge-file entry and finishes its setup.
386 + * If the name has a path portion then we check to see if it refers to a
387 + * parent directory of the first transfer dir.  If it does, we scan all the
388 + * dirs from that point through the parent dir of the transfer dir looking
389 + * for the per-dir merge-file in each one. */
390 +static BOOL setup_merge_file(struct exclude_struct *ex,
391 +                            struct exclude_list_struct *lp, int flags)
392 +{
393 +       char buf[MAXPATHLEN];
394 +       char *x, *y, *pat = ex->pattern;
395 +       unsigned int len;
396 +
397 +       if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
398 +               return 0;
399 +
400 +       y = strrchr(x, '/');
401 +       *y = '\0';
402 +       ex->pattern = strdup(y+1);
403 +       if (!*x)
404 +               x = "/";
405 +       if (*x == '/')
406 +               strlcpy(buf, x, MAXPATHLEN);
407 +       else
408 +               pathjoin(buf, MAXPATHLEN, dirbuf, x);
409 +
410 +       len = clean_fname(buf, 1);
411 +       if (len != 1 && len < MAXPATHLEN-1) {
412 +               buf[len++] = '/';
413 +               buf[len] = '\0';
414 +       }
415 +       /* This ensures that the specified dir is a parent of the transfer. */
416 +       for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
417 +       if (*x)
418 +               y += strlen(y); /* nope -- skip the scan */
419 +
420 +       parent_dirscan = True;
421 +       while (*y) {
422 +               char save[MAXPATHLEN];
423 +               strlcpy(save, y, MAXPATHLEN);
424 +               *y = '\0';
425 +               dirbuf_len = y - dirbuf;
426 +               strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
427 +               add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
428 +               if (ex->match_flags & MATCHFLG_CVSIGNORE)
429 +                       lp->head = NULL; /* CVS doesn't inherit rules. */
430 +               lp->tail = NULL;
431 +               strlcpy(y, save, MAXPATHLEN);
432 +               while ((*x++ = *y++) != '/') {}
433 +       }
434 +       parent_dirscan = False;
435 +       free(pat);
436 +       return 1;
437 +}
438 +
439 +/* Each time rsync changes to a new directory it call this function to
440 + * handle all the per-dir merge-files.  The "dir" value is the current path
441 + * relative to curr_dir (which might not be null-terminated).  We copy it
442 + * into dirbuf so that we can easily append a file name on the end. */
443 +void *push_local_excludes(const char *dir, unsigned int dirlen)
444 +{
445 +       struct mergelist_save_struct *push;
446 +       struct exclude_list_struct *ap;
447 +       int i;
448 +
449 +       set_excludes_dir(dir, dirlen);
450 +
451 +       if (!(push = new_array(struct mergelist_save_struct, 1)))
452 +               out_of_memory("push_local_excludes");
453 +
454 +       push->count = mergelist_cnt;
455 +       push->array = new_array(struct exclude_list_struct, mergelist_cnt);
456 +       if (!push->array)
457 +               out_of_memory("push_local_excludes");
458 +
459 +       for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
460 +               memcpy(ap++, mergelist_parents[i]->u.mergelist,
461 +                      sizeof (struct exclude_list_struct));
462 +       }
463 +
464 +       /* Note: add_exclude_file() might increase mergelist_cnt, so keep
465 +        * this loop separate from the above loop. */
466 +       for (i = 0; i < mergelist_cnt; i++) {
467 +               struct exclude_struct *ex = mergelist_parents[i];
468 +               struct exclude_list_struct *lp = ex->u.mergelist;
469 +               int flags;
470 +
471 +               if (verbose > 2) {
472 +                       rprintf(FINFO, "[%s] pushing %sexclude list\n",
473 +                               who_am_i(), lp->debug_type);
474 +               }
475 +
476 +               if (ex->match_flags & MATCHFLG_CVSIGNORE) {
477 +                       lp->head = NULL; /* CVS doesn't inherit rules. */
478 +                       flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
479 +               } else {
480 +                       flags = ex->match_flags & MATCHFLG_INCLUDE
481 +                           ? XFLG_DEF_INCLUDE : 0;
482 +               }
483 +               lp->tail = NULL; /* Switch any local rules to inherited. */
484 +
485 +               if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
486 +                       ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
487 +                       if (setup_merge_file(ex, lp, flags))
488 +                               set_excludes_dir(dir, dirlen);
489 +               }
490 +
491 +               if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
492 +                   MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
493 +                       add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
494 +               else {
495 +                       io_error |= IOERR_GENERAL;
496 +                       rprintf(FINFO,
497 +                           "cannot add local excludes in long-named directory %s\n",
498 +                           full_fname(dirbuf));
499 +               }
500 +               dirbuf[dirbuf_len] = '\0';
501 +       }
502 +
503 +       return (void*)push;
504 +}
505 +
506 +void pop_local_excludes(void *mem)
507 +{
508 +       struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
509 +       struct exclude_list_struct *ap;
510 +       int i;
511 +
512 +       for (i = mergelist_cnt; i-- > 0; ) {
513 +               struct exclude_struct *ex = mergelist_parents[i];
514 +               struct exclude_list_struct *lp = ex->u.mergelist;
515 +
516 +               if (verbose > 2) {
517 +                       rprintf(FINFO, "[%s] popping %sexclude list\n",
518 +                               who_am_i(), lp->debug_type);
519 +               }
520 +
521 +               clear_exclude_list(lp);
522 +       }
523 +
524 +       mergelist_cnt = pop->count;
525 +       for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
526 +               memcpy(mergelist_parents[i]->u.mergelist, ap++,
527 +                      sizeof (struct exclude_list_struct));
528 +       }
529 +
530 +       free(pop->array);
531 +       free(pop);
532 +}
533 +
534  static int check_one_exclude(char *name, struct exclude_struct *ex,
535                               int name_is_dir)
536  {
537 @@ -125,13 +473,14 @@ static int check_one_exclude(char *name,
538         /* If the pattern does not have any slashes AND it does not have
539          * a "**" (which could match a slash), then we just match the
540          * name portion of the path. */
541 -       if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
542 +       if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
543                 if ((p = strrchr(name,'/')) != NULL)
544                         name = p+1;
545         }
546         else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
547 -           && curr_dir[1]) {
548 -               pathjoin(full_name, sizeof full_name, curr_dir + 1, name);
549 +           && curr_dir_len > module_dirlen + 1) {
550 +               pathjoin(full_name, sizeof full_name,
551 +                        curr_dir + module_dirlen + 1, name);
552                 name = full_name;
553         }
554  
555 @@ -148,9 +497,9 @@ static int check_one_exclude(char *name,
556         if (ex->match_flags & MATCHFLG_WILD) {
557                 /* A non-anchored match with an infix slash and no "**"
558                  * needs to match the last slash_cnt+1 name elements. */
559 -               if (!match_start && ex->slash_cnt
560 +               if (!match_start && ex->u.slash_cnt
561                     && !(ex->match_flags & MATCHFLG_WILD2)) {
562 -                       int cnt = ex->slash_cnt + 1;
563 +                       int cnt = ex->u.slash_cnt + 1;
564                         for (p = name + strlen(name) - 1; p >= name; p--) {
565                                 if (*p == '/' && !--cnt)
566                                         break;
567 @@ -221,6 +570,13 @@ int check_exclude(struct exclude_list_st
568         struct exclude_struct *ent;
569  
570         for (ent = listp->head; ent; ent = ent->next) {
571 +               if (ent->match_flags & MATCHFLG_MERGE_FILE) {
572 +                       int rc = check_exclude(ent->u.mergelist, name,
573 +                                              name_is_dir);
574 +                       if (rc)
575 +                               return rc;
576 +                       continue;
577 +               }
578                 if (check_one_exclude(name, ent, name_is_dir)) {
579                         report_exclude_result(name, ent, name_is_dir,
580                                               listp->debug_type);
581 @@ -254,12 +610,45 @@ static const char *get_exclude_tok(const
582                 p = (const char *)s;
583         }
584  
585 -       /* Is this a '+' or '-' followed by a space (not whitespace)? */
586 -       if (!(xflags & XFLG_WORDS_ONLY)
587 -           && (*s == '-' || *s == '+') && s[1] == ' ') {
588 +       /* Check for a leading '+' or '-'. */
589 +       if (!(xflags & XFLG_WORDS_ONLY) && (*s == '-' || *s == '+')) {
590 +               if (!s[1]) {
591 +                       rprintf(FERROR,
592 +                               "no pattern followed %c in %sclude file.\n",
593 +                               *s, xflags & XFLG_DEF_INCLUDE ? "in" : "ex");
594 +                       exit_cleanup(RERR_SYNTAX);
595 +               }
596                 if (*s == '+')
597                         mflags |= MATCHFLG_INCLUDE;
598 -               s += 2;
599 +               while (*++s && *s != ' ') {
600 +                       switch (*s) {
601 +                       case 'C':
602 +                               mflags |= MATCHFLG_MERGE_FILE
603 +                                       | MATCHFLG_PERDIR_MERGE
604 +                                       | MATCHFLG_FINISH_SETUP
605 +                                       | MATCHFLG_CVSIGNORE;
606 +                               mflags &= ~MATCHFLG_INCLUDE;
607 +                               break;
608 +                       case 'E':
609 +                               mflags |= MATCHFLG_EXCLUDE_SELF;
610 +                               break;
611 +                       case 'p':
612 +                               mflags |= MATCHFLG_MERGE_FILE
613 +                                       | MATCHFLG_PERDIR_MERGE
614 +                                       | MATCHFLG_FINISH_SETUP;
615 +                               break;
616 +                       case 'm':
617 +                               mflags |= MATCHFLG_MERGE_FILE;
618 +                               break;
619 +                       default:
620 +                               rprintf(FERROR,
621 +                                       "invalid include/exclude option after %c: %c\n",
622 +                                       *p, *s);
623 +                               exit_cleanup(RERR_SYNTAX);
624 +                       }
625 +               }
626 +               if (*s)
627 +                       s++;
628         } else if (xflags & XFLG_DEF_INCLUDE)
629                 mflags |= MATCHFLG_INCLUDE;
630         if (xflags & XFLG_DIRECTORY)
631 @@ -274,8 +663,27 @@ static const char *get_exclude_tok(const
632         } else
633                 len = strlen(s);
634  
635 +       if (mflags & MATCHFLG_PERDIR_MERGE) {
636 +               if (mflags & MATCHFLG_CVSIGNORE) {
637 +                       if (len) {
638 +                               rprintf(FERROR,
639 +                                       "unexpected trailing char%s after -C: `%s'\n",
640 +                                       len == 1 ? "" : "s", s);
641 +                               exit_cleanup(RERR_SYNTAX);
642 +                       }
643 +                       s = ".cvsignore";
644 +                       len = 10;
645 +               } else if ((len == 10 || (len > 10 && s[len-11] == '/'))
646 +                   && strncmp(s+len-10, ".cvsignore", 10) == 0) {
647 +                       mflags |= MATCHFLG_CVSIGNORE;
648 +                       mflags &= ~MATCHFLG_INCLUDE;
649 +               }
650 +       }
651 +
652         if (*p == '!' && len == 1)
653                 mflags |= MATCHFLG_CLEAR_LIST;
654 +       if (xflags & XFLG_ABS_PATH)
655 +               mflags |= MATCHFLG_ABS_PATH;
656  
657         *len_ptr = len;
658         *flag_ptr = mflags;
659 @@ -287,7 +695,7 @@ void add_exclude(struct exclude_list_str
660                  int xflags)
661  {
662         unsigned int pat_len, mflags;
663 -       const char *cp;
664 +       const char *cp, *p;
665  
666         if (!pattern)
667                 return;
668 @@ -295,9 +703,15 @@ void add_exclude(struct exclude_list_str
669         cp = pattern;
670         pat_len = 0;
671         while (1) {
672 +               /* Remember that the returned string is NOT '\0' terminated! */
673                 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
674                 if (!pat_len)
675                         break;
676 +               if (pat_len >= MAXPATHLEN) {
677 +                       rprintf(FERROR, "discarding over-long exclude: %s\n",
678 +                               cp);
679 +                       continue;
680 +               }
681  
682                 if (mflags & MATCHFLG_CLEAR_LIST) {
683                         if (verbose > 2) {
684 @@ -309,13 +723,37 @@ void add_exclude(struct exclude_list_str
685                         continue;
686                 }
687  
688 -               make_exclude(listp, cp, pat_len, mflags);
689 -
690 -               if (verbose > 2) {
691 -                       rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
692 -                               who_am_i(), (int)pat_len, cp, listp->debug_type,
693 -                               mflags & MATCHFLG_INCLUDE ? "in" : "ex");
694 +               if (mflags & MATCHFLG_MERGE_FILE) {
695 +                       unsigned int len = pat_len;
696 +                       if (mflags & MATCHFLG_EXCLUDE_SELF) {
697 +                               const char *name = strrchr(cp, '/');
698 +                               if (name)
699 +                                       len -= ++name - cp;
700 +                               else
701 +                                       name = cp;
702 +                               make_exclude(listp, name, len, 0);
703 +                               mflags &= ~MATCHFLG_EXCLUDE_SELF;
704 +                               len = pat_len;
705 +                       }
706 +                       if (mflags & MATCHFLG_PERDIR_MERGE) {
707 +                               if (parent_dirscan) {
708 +                                       if (!(p = parse_merge_name(cp, &len, module_dirlen)))
709 +                                               continue;
710 +                                       make_exclude(listp, p, len, mflags);
711 +                                       continue;
712 +                               }
713 +                       } else {
714 +                               int flgs = XFLG_FATAL_ERRORS;
715 +                               if (!(p = parse_merge_name(cp, &len, 0)))
716 +                                       continue;
717 +                               if (mflags & MATCHFLG_INCLUDE)
718 +                                       flgs |= XFLG_DEF_INCLUDE;
719 +                               add_exclude_file(listp, p, flgs);
720 +                               continue;
721 +                       }
722                 }
723 +
724 +               make_exclude(listp, cp, pat_len, mflags);
725         }
726  }
727  
728 @@ -324,7 +762,7 @@ void add_exclude_file(struct exclude_lis
729                       int xflags)
730  {
731         FILE *fp;
732 -       char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
733 +       char line[MAXPATHLEN+4]; /* Room for prefix chars and trailing slash. */
734         char *eob = line + sizeof line - 1;
735         int word_split = xflags & XFLG_WORD_SPLIT;
736  
737 @@ -338,13 +776,19 @@ void add_exclude_file(struct exclude_lis
738         if (!fp) {
739                 if (xflags & XFLG_FATAL_ERRORS) {
740                         rsyserr(FERROR, errno,
741 -                               "failed to open %s file %s",
742 -                               xflags & XFLG_DEF_INCLUDE ? "include" : "exclude",
743 +                               "failed to open %sclude file %s",
744 +                               xflags & XFLG_DEF_INCLUDE ? "in" : "ex",
745                                 fname);
746                         exit_cleanup(RERR_FILEIO);
747                 }
748                 return;
749         }
750 +       dirbuf[dirbuf_len] = '\0';
751 +
752 +       if (verbose > 2) {
753 +               rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
754 +                       who_am_i(), fname, xflags);
755 +       }
756  
757         while (1) {
758                 char *s = line;
759 @@ -402,7 +846,20 @@ void send_exclude_list(int f)
760                         p[l] = '\0';
761                 }
762  
763 -               if (ent->match_flags & MATCHFLG_INCLUDE) {
764 +               if (ent->match_flags & MATCHFLG_MERGE_FILE) {
765 +                       char buf[32], *op = buf;
766 +                       if (ent->match_flags & MATCHFLG_INCLUDE)
767 +                               *op++ = '+';
768 +                       else
769 +                               *op++ = '-';
770 +                       if (ent->match_flags & MATCHFLG_PERDIR_MERGE)
771 +                               *op++ = 'p';
772 +                       else
773 +                               *op++ = 'm';
774 +                       *op++ = ' ';
775 +                       write_int(f, l + (op - buf));
776 +                       write_buf(f, buf, op - buf);
777 +               } else if (ent->match_flags & MATCHFLG_INCLUDE) {
778                         write_int(f, l + 2);
779                         write_buf(f, "+ ", 2);
780                 } else if (*p == '-' || *p == '+') {
781 @@ -419,7 +876,7 @@ void send_exclude_list(int f)
782  
783  void recv_exclude_list(int f)
784  {
785 -       char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
786 +       char line[MAXPATHLEN+4]; /* Room for prefix and trailing slash. */
787         unsigned int l;
788  
789         while ((l = read_int(f)) != 0) {
790 @@ -446,6 +903,7 @@ void add_cvs_excludes(void)
791         char fname[MAXPATHLEN];
792         char *p;
793  
794 +       add_exclude(&exclude_list, "-C", 0);
795         add_exclude(&exclude_list, default_cvsignore,
796                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
797  
798 --- orig/flist.c        2005-01-01 21:11:00
799 +++ flist.c     2004-08-12 18:59:28
800 @@ -40,10 +40,9 @@ extern int module_id;
801  extern int ignore_errors;
802  extern int numeric_ids;
803  
804 -extern int cvs_exclude;
805 -
806  extern int recurse;
807  extern char curr_dir[MAXPATHLEN];
808 +extern unsigned int curr_dir_len;
809  extern char *files_from;
810  extern int filesfrom_fd;
811  
812 @@ -67,7 +66,6 @@ extern int list_only;
813  
814  extern struct exclude_list_struct exclude_list;
815  extern struct exclude_list_struct server_exclude_list;
816 -extern struct exclude_list_struct local_exclude_list;
817  
818  int io_error;
819  
820 @@ -223,8 +221,6 @@ int link_stat(const char *path, STRUCT_S
821   */
822  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
823  {
824 -       int rc;
825 -
826  #if 0 /* This currently never happens, so avoid a useless compare. */
827         if (exclude_level == NO_EXCLUDES)
828                 return 0;
829 @@ -246,10 +242,7 @@ static int check_exclude_file(char *fnam
830         if (exclude_level != ALL_EXCLUDES)
831                 return 0;
832         if (exclude_list.head
833 -           && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
834 -               return rc < 0;
835 -       if (local_exclude_list.head
836 -           && check_exclude(&local_exclude_list, fname, is_dir) < 0)
837 +           && check_exclude(&exclude_list, fname, is_dir) < 0)
838                 return 1;
839         return 0;
840  }
841 @@ -983,15 +976,7 @@ void send_file_name(int f, struct file_l
842  
843         if (recursive && S_ISDIR(file->mode)
844             && !(file->flags & FLAG_MOUNT_POINT)) {
845 -               struct exclude_list_struct last_list = local_exclude_list;
846 -               local_exclude_list.head = local_exclude_list.tail = NULL;
847                 send_directory(f, flist, f_name_to(file, fbuf));
848 -               if (verbose > 2) {
849 -                       rprintf(FINFO, "[%s] popping %sexclude list\n",
850 -                               who_am_i(), local_exclude_list.debug_type);
851 -               }
852 -               clear_exclude_list(&local_exclude_list);
853 -               local_exclude_list = last_list;
854         }
855  }
856  
857 @@ -1002,6 +987,7 @@ static void send_directory(int f, struct
858         struct dirent *di;
859         char fname[MAXPATHLEN];
860         unsigned int offset;
861 +       void *save_excludes;
862         char *p;
863  
864         d = opendir(dir);
865 @@ -1025,18 +1011,7 @@ static void send_directory(int f, struct
866                 offset++;
867         }
868  
869 -       if (cvs_exclude) {
870 -               if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
871 -                   < MAXPATHLEN - offset) {
872 -                       add_exclude_file(&local_exclude_list, fname,
873 -                                        XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
874 -               } else {
875 -                       io_error |= IOERR_GENERAL;
876 -                       rprintf(FINFO,
877 -                               "cannot cvs-exclude in long-named directory %s\n",
878 -                               full_fname(fname));
879 -               }
880 -       }
881 +       save_excludes = push_local_excludes(fname, offset);
882  
883         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
884                 char *dname = d_name(di);
885 @@ -1057,6 +1032,8 @@ static void send_directory(int f, struct
886                 rsyserr(FERROR, errno, "readdir(%s)", dir);
887         }
888  
889 +       pop_local_excludes(save_excludes);
890 +
891         closedir(d);
892  }
893  
894 @@ -1076,6 +1053,7 @@ struct file_list *send_file_list(int f, 
895         char *p, *dir, olddir[sizeof curr_dir];
896         char lastpath[MAXPATHLEN] = "";
897         struct file_list *flist;
898 +       BOOL need_first_push = True;
899         int64 start_write;
900         int use_ff_fd = 0;
901  
902 @@ -1096,6 +1074,10 @@ struct file_list *send_file_list(int f, 
903                                 exit_cleanup(RERR_FILESELECT);
904                         }
905                         use_ff_fd = 1;
906 +                       if (curr_dir_len < MAXPATHLEN - 1) {
907 +                               push_local_excludes(curr_dir, curr_dir_len);
908 +                               need_first_push = False;
909 +                       }
910                 }
911         }
912  
913 @@ -1126,6 +1108,15 @@ struct file_list *send_file_list(int f, 
914                         }
915                 }
916  
917 +               if (need_first_push) {
918 +                       if ((p = strrchr(fname, '/')) != NULL) {
919 +                               if (*++p && strcmp(p, ".") != 0)
920 +                                       push_local_excludes(fname, p - fname);
921 +                       } else if (strcmp(fname, ".") != 0)
922 +                               push_local_excludes(fname, 0);
923 +                       need_first_push = False;
924 +               }
925 +
926                 if (link_stat(fname, &st, keep_dirlinks) != 0) {
927                         if (f != -1) {
928                                 io_error |= IOERR_GENERAL;
929 --- orig/options.c      2005-01-15 21:23:15
930 +++ options.c   2005-01-15 23:50:05
931 @@ -144,6 +144,7 @@ int list_only = 0;
932  char *batch_name = NULL;
933  
934  static int daemon_opt;   /* sets am_daemon after option error-reporting */
935 +static int E_option_cnt = 0;
936  static int modify_window_set;
937  static char *dest_option = NULL;
938  static char *max_size_arg;
939 @@ -295,6 +296,8 @@ void usage(enum logcode F)
940    rprintf(F,"     --exclude-from=FILE     exclude patterns listed in FILE\n");
941    rprintf(F,"     --include=PATTERN       don't exclude files matching PATTERN\n");
942    rprintf(F,"     --include-from=FILE     don't exclude patterns listed in FILE\n");
943 +  rprintf(F," -E                          same as --exclude='-p /.rsync-excludes'\n");
944 +  rprintf(F,"                             repeated: --exclude=.rsync-excludes\n");
945    rprintf(F,"     --files-from=FILE       read FILE for list of source-file names\n");
946    rprintf(F," -0, --from0                 all *-from file lists are delimited by nulls\n");
947    rprintf(F,"     --version               print version number\n");
948 @@ -393,6 +396,7 @@ static struct poptOption long_options[] 
949    {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
950    {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
951    {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
952 +  {0,                 'E', POPT_ARG_NONE,   0, 'E', 0, 0 },
953    {0,                 'P', POPT_ARG_NONE,   0, 'P', 0, 0 },
954    {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
955    {"log-format",       0,  POPT_ARG_STRING, &log_format, 0, 0, 0 },
956 @@ -668,6 +672,19 @@ int parse_arguments(int *argc, const cha
957                         am_sender = 1;
958                         break;
959  
960 +               case 'E':
961 +                       switch (++E_option_cnt) {
962 +                       case 1:
963 +                               add_exclude(&exclude_list,
964 +                                           "-p /.rsync-excludes", 0);
965 +                               break;
966 +                       case 2:
967 +                               add_exclude(&exclude_list,
968 +                                           ".rsync-excludes", 0);
969 +                               break;
970 +                       }
971 +                       break;
972 +
973                 case 'P':
974                         do_progress = 1;
975                         keep_partial = 1;
976 --- orig/rsync.h        2005-01-15 21:18:09
977 +++ rsync.h     2005-01-16 00:37:39
978 @@ -111,6 +111,7 @@
979  #define XFLG_WORDS_ONLY        (1<<2)
980  #define XFLG_WORD_SPLIT        (1<<3)
981  #define XFLG_DIRECTORY         (1<<4)
982 +#define XFLG_ABS_PATH          (1<<5)
983  
984  #define PERMS_REPORT           (1<<0)
985  #define PERMS_SKIP_MTIME       (1<<1)
986 @@ -512,11 +513,19 @@ struct map_struct {
987  #define MATCHFLG_INCLUDE       (1<<4) /* this is an include, not an exclude */
988  #define MATCHFLG_DIRECTORY     (1<<5) /* this matches only directories */
989  #define MATCHFLG_CLEAR_LIST    (1<<6) /* this item is the "!" token */
990 +#define MATCHFLG_MERGE_FILE    (1<<7) /* specifies a file to merge */
991 +#define MATCHFLG_CVSIGNORE     (1<<8) /* parse this as a .cvsignore file */
992 +#define MATCHFLG_PERDIR_MERGE  (1<<9) /* merge-file is searched per-dir */
993 +#define MATCHFLG_FINISH_SETUP  (1<<10)/* per-dir merge file needs setup */
994 +#define MATCHFLG_EXCLUDE_SELF  (1<<11)/* merge-file name should be excluded */
995  struct exclude_struct {
996         struct exclude_struct *next;
997         char *pattern;
998         unsigned int match_flags;
999 -       int slash_cnt;
1000 +       union {
1001 +               int slash_cnt;
1002 +               struct exclude_list_struct *mergelist;
1003 +       } u;
1004  };
1005  
1006  struct exclude_list_struct {
1007 --- orig/rsync.yo       2005-01-15 04:36:32
1008 +++ rsync.yo    2005-01-16 00:39:05
1009 @@ -365,6 +365,8 @@ verb(
1010       --exclude-from=FILE     exclude patterns listed in FILE
1011       --include=PATTERN       don't exclude files matching PATTERN
1012       --include-from=FILE     don't exclude patterns listed in FILE
1013 + -E                          same as --exclude='-p /.rsync-excludes'
1014 +                             repeated: --exclude=.rsync-excludes
1015       --files-from=FILE       read FILE for list of source-file names
1016   -0  --from0                 all file lists are delimited by nulls
1017       --version               print version number
1018 @@ -779,6 +781,24 @@ dit(bf(--include-from=FILE)) This specif
1019  from a file.
1020  If em(FILE) is "-" the list will be read from standard input.
1021  
1022 +dit(bf(-E)) The -E option is a shorthand for adding --exclude rules to
1023 +your command.  The first time it is used is a shorthand for this rule:
1024 +
1025 +verb(
1026 +  --exclude='-p /.rsync-excludes'
1027 +)
1028 +
1029 +If it is repeated, it is a shorthand for this rule:
1030 +
1031 +verb(
1032 +  --exclude=.rsync-excludes
1033 +)
1034 +
1035 +This allows you to copy files from a hierarchy that has been sprinkled
1036 +with ".rsync-excludes" files and those rules will affect the transfer by
1037 +using specifying -E.  If you don't want the .rsync-excludes files to be
1038 +sent along with the other files, specify -E a second time.
1039 +
1040  dit(bf(--files-from=FILE)) Using this option allows you to specify the
1041  exact list of files to transfer (as read from the specified FILE or "-"
1042  for standard input).  It also tweaks the default behavior of rsync to make
1043 @@ -1114,24 +1134,32 @@ The exclude and include patterns specifi
1044  selection of which files to transfer and which files to skip.
1045  
1046  Rsync builds an ordered list of include/exclude options as specified on
1047 -the command line. Rsync checks each file and directory 
1048 -name against each exclude/include pattern in turn. The first matching
1049 +the command line.
1050 +It can also be told to check for include/exclude options in each
1051 +directory that rsync visits during the transfer (see the section on
1052 +MERGED EXCLUDE FILES for the details on these per-directory exclude
1053 +files).
1054 +
1055 +As the list of files/directories to transfer is built, rsync checks each
1056 +name against every exclude/include pattern in turn. The first matching
1057  pattern is acted on. If it is an exclude pattern, then that file is
1058  skipped. If it is an include pattern then that filename is not
1059  skipped. If no matching include/exclude pattern is found then the
1060  filename is not skipped.
1061  
1062 -The filenames matched against the exclude/include patterns are relative
1063 -to the "root of the transfer".  If you think of the transfer as a
1064 -subtree of names that are being sent from sender to receiver, the root
1065 -is where the tree starts to be duplicated in the destination directory.
1066 -This root governs where patterns that start with a / match (see below).
1067 +The global include/exclude rules are anchored at the "root of the
1068 +transfer" (as opposed to per-directory rules, which are anchored at
1069 +the merge-file's directory).  If you think of the transfer as a
1070 +subtree of names that are being sent from sender to receiver, the
1071 +transfer-root is where the tree starts to be duplicated in the
1072 +destination directory.  This root governs where patterns that start
1073 +with a / match (as described in the list on pattern forms below).
1074  
1075  Because the matching is relative to the transfer-root, changing the
1076  trailing slash on a source path or changing your use of the --relative
1077  option affects the path you need to use in your matching (in addition to
1078  changing how much of the file tree is duplicated on the destination
1079 -system).  The following examples demonstrate this.
1080 +host).  The following examples demonstrate this.
1081  
1082  Let's say that we want to match two source files, one with an absolute
1083  path of "/home/me/foo/bar", and one with a path of "/home/you/bar/baz".
1084 @@ -1178,23 +1206,27 @@ because rsync did not descend through th
1085  hierarchy.
1086  
1087  Note also that the --include and --exclude options take one pattern
1088 -each. To add multiple patterns use the --include-from and
1089 ---exclude-from options or multiple --include and --exclude options. 
1090 +each. To add multiple patterns use the --include-from and --exclude-from
1091 +options or multiple --include and --exclude options.
1092  
1093 -The patterns can take several forms. The rules are:
1094 +The include/exclude patterns can take several forms. The rules are:
1095  
1096  itemize(
1097  
1098 -  it() if the pattern starts with a / then it is matched against the
1099 -  start of the filename, otherwise it is matched against the end of
1100 -  the filename.
1101 -  This is the equivalent of a leading ^ in regular expressions.
1102 -  Thus "/foo" would match a file called "foo" at the transfer-root
1103 -  (see above for how this is different from the filesystem-root).
1104 -  On the other hand, "foo" would match any file called "foo"
1105 +  it() if the pattern starts with a / then it is anchored to a
1106 +  particular spot in the hierarchy of files, otherwise it is matched
1107 +  against the end of the pathname.  This is similar to a leading ^ in
1108 +  regular expressions.
1109 +  Thus "/foo" would match a file called "foo" at either the "root of the
1110 +  transfer" (for a global rule) or in the merge-file's directory (for a
1111 +  per-directory rule).
1112 +  An unqualified "foo" would match any file or directory named "foo"
1113    anywhere in the tree because the algorithm is applied recursively from
1114 +  the
1115    top down; it behaves as if each path component gets a turn at being the
1116 -  end of the file name.
1117 +  end of the file name.  Even the unanchored "sub/foo" would match at
1118 +  any point in the hierarchy where a "foo" was found within a directory
1119 +  named "sub".
1120  
1121    it() if the pattern ends with a / then it will only match a
1122    directory, not a file, link, or device.
1123 @@ -1207,24 +1239,55 @@ itemize(
1124    single asterisk pattern "*" will stop at slashes.
1125  
1126    it() if the pattern contains a / (not counting a trailing /) or a "**"
1127 -  then it is matched against the full filename, including any leading
1128 -  directory. If the pattern doesn't contain a / or a "**", then it is
1129 +  then it is matched against the full pathname, including any leading
1130 +  directories. If the pattern doesn't contain a / or a "**", then it is
1131    matched only against the final component of the filename.  Again,
1132    remember that the algorithm is applied recursively so "full filename" can
1133    actually be any portion of a path below the starting directory.
1134  
1135 -  it() if the pattern starts with "+ " (a plus followed by a space)
1136 -  then it is always considered an include pattern, even if specified as
1137 -  part of an exclude option. The prefix is discarded before matching.
1138 -
1139 -  it() if the pattern starts with "- " (a minus followed by a space)
1140 -  then it is always considered an exclude pattern, even if specified as
1141 -  part of an include option. The prefix is discarded before matching.
1142 +  it() if the pattern starts with "+" (a plus), the actual pattern begins
1143 +  after the first space, and is always considered to be an include pattern,
1144 +  even if specified as part of an exclude file/option.  Option letters may
1145 +  follow the "+" prior to the separating space (see below).
1146 +
1147 +  it() if the pattern starts with "-" (a minus), the actual pattern begins
1148 +  after the first space, and is always considered to be an exclude pattern,
1149 +  even if specified as part of an include file/option.  Option letters may
1150 +  follow the "-" prior to the separating space (see below).
1151  
1152 -  it() if the pattern is a single exclamation mark ! then the current
1153 +  it() if the pattern is a "!" (single exclamation mark) then the current
1154    include/exclude list is reset, removing all previously defined patterns.
1155 +  The "current" list is either the global list of rules (which are
1156 +  specified via options) or a set of per-directory rules (which are
1157 +  inherited in their own sub-list, so a subdirectory can use this to
1158 +  clear out the parent's rules).
1159 +
1160 +)
1161 +
1162 +For a line that starts with a "+" (plus) or a "-" (minus), the following
1163 +option letters may be suffixed:
1164 +
1165 +itemize(
1166 +
1167 +  it() An "m" means that the string following the space is to be taken to
1168 +  be a merge-file that is read in to supplement the current rules.
1169 +
1170 +  it() A "p" means that the string following the space is to be taken to be
1171 +  a per-directory merge-file that is read in to supplement the current
1172 +  rules.
1173 +
1174 +  it() A "C" (which must be followed by an empty pattern) will be
1175 +  interpreted as though "-p .cvsignore" had been specified.
1176 +
1177 +  it() A "E" may be used in combination with any of the prior 3 letters in
1178 +  order to specify that the merge-file name should be also excluded from
1179 +  the transfer (e.g. "-pE .excl" is like "-p .excl" and "- .excl").
1180 +
1181  )
1182  
1183 +See the section on MERGED EXCLUDE FILES for more information on how the
1184 +above options work.
1185 +
1186  The +/- rules are most useful in a list that was read from a file, allowing
1187  you to have a single exclude list that contains both include and exclude
1188  options in the proper order.
1189 @@ -1269,8 +1332,166 @@ itemize(
1190    it() --include "*/" --include "*.c" --exclude "*" would include all 
1191    directories and C source files
1192    it() --include "foo/" --include "foo/bar.c" --exclude "*" would include
1193 -  only foo/bar.c (the foo/ directory must be explicitly included or
1194 -  it would be excluded by the "*")
1195 +  only the foo directory and foo/bar.c (the foo directory must be
1196 +  explicitly included or it would be excluded by the "*")
1197 +)
1198 +
1199 +manpagesection(MERGED EXCLUDE FILES)
1200 +
1201 +You can merge whole files into an exclude file by specifying either the
1202 +"m" or "p" option following the initial "+" or "-", and putting a filename
1203 +in place of the pattern.  There are two kinds of merged exclude files --
1204 +single-instance ("m") and per-directory ("p").  For example:
1205 +
1206 +verb(
1207 +    -m /etc/rsync/default.excludes
1208 +    +p .per-dir-includes
1209 +)
1210 +
1211 +For a per-directory merge file, rsync will scan
1212 +every directory that it traverses for the named file, merging its contents
1213 +when the file exists.  These exclude files must exist on the sending side
1214 +because it is the sending side that is being scanned for available files
1215 +to send.  The files may also need to be transferred to the receiving side
1216 +if you want them to affect what files don't get deleted (see PER-DIRECTORY
1217 +EXCLUDES AND DELETE below).
1218 +
1219 +Per-directory rules are inherited in all subdirectories of the directory
1220 +where the merge-file was found.  Each subdirectory's rules are prefixed
1221 +to the inherited rules from the parent directories, which gives the
1222 +newest rules a higher priority than the inherited rules.  The entire set
1223 +of per-dir rules is grouped together in the spot where the merge-file was
1224 +specified, so it is possible to override per-dir rules via a rule that
1225 +got specified earlier in the list of global rules.
1226 +
1227 +If you don't want a per-dir rule to be inherited, anchor it with a leading
1228 +slash.  Anchored rules in a per-directory merge-file are relative to the
1229 +merge-file's directory, so a rule "/foo" would only exclude the file "foo"
1230 +in the directory where the per-dir exclude file was found.
1231 +
1232 +Here's an example exclude file which you'd specify via the normal
1233 +--exclude-from=FILE option:
1234 +
1235 +verb(
1236 +    -m /home/user/.global_excludes
1237 +    *.gz
1238 +    +p .incl
1239 +    + *.[ch]
1240 +    *.o
1241 +)
1242 +
1243 +This will merge the contents of the /home/user/.global_excludes file at the
1244 +start of the list and also turns the ".incl" filename into a per-directory
1245 +include file.  All rules read in prior to the start of the directory scan
1246 +follow the global anchoring rules (i.e. a leading slash matches at the root
1247 +of the transfer).
1248 +
1249 +If a per-directory merge-file is specified with a path that is a parent
1250 +directory of the first transfer directory, rsync will scan all the parent
1251 +dirs from that starting point to the transfer directory for the indicated
1252 +per-directory file.  For instance, here is a common exclude (see -E):
1253 +
1254 +verb(
1255 +  --exclude='-p /.rsync-excludes'
1256 +)
1257 +
1258 +That exclude tells rsync to scan for the file .rsync-excludes in all
1259 +directories from the root down through the parent directory of the
1260 +transfer prior to the start of the normal directory scan of the file in
1261 +the directories that are sent as a part of the transfer.  (Note: for an
1262 +rsync daemon, the root is always the same as the module's "path".)
1263 +
1264 +Some examples of this pre-scanning for per-directory files:
1265 +
1266 +verb(
1267 +  rsync -avE /src/path/ /dest/dir
1268 +  rsync -av --exclude='-p ../../.rsync-excludes' /src/path/ /dest/dir
1269 +  rsync -av --exclude='-p .rsync-excludes' /src/path/ /dest/dir
1270 +)
1271 +
1272 +The first two commands above will look for ".rsync-excludes" in "/" and
1273 +"/src" before the normal scan begins looking for the file in "/src/path"
1274 +and its subdirectories.  The last command avoids the parent-dir scan
1275 +and only looks for the ".rsync-excludes" files in each directory that is
1276 +a part of the transfer.
1277 +
1278 +Finally, note that the parsing of any merge-file named ".cvsignore" is
1279 +always done in a CVS-compatible manner, even if -C wasn't specified.  This
1280 +means that its rules are always excludes (even if an include option
1281 +specified the file), patterns are split on whitespace, the rules are never
1282 +inherited, and no special characters are honored except for "!" (e.g. no
1283 +comments, and no +/- prefixes).
1284 +
1285 +Additionally, you can affect where the --cvs-exclude (-C) option's
1286 +inclusion of the per-directory .cvsignore file gets placed into your rules
1287 +by putting a "-C" somewhere in your exclude rules.  Without this, rsync
1288 +would add the per-dir rule for the .cvignore file at the end of all your
1289 +other rules (giving it a lower priority than your command-line rules).
1290 +For example:
1291 +
1292 +verb(
1293 +  cat <<EOT
1294 +  + foo.o
1295 +  -C
1296 +  *.old
1297 +  EOT | rsync -avC --exclude-from=- a/ b
1298 +
1299 +  rsync -avC --include=foo.o --exclude=-C --exclude='*.old' a/ b
1300 +)
1301 +
1302 +Both of the above rsync commands are identical.  Each one will merge all
1303 +the per-directory .cvsignore rules in the middle of the list rather than
1304 +at the end.  This allows their dir-specific rules to supersede the rules
1305 +that follow the -C instead of being subservient to all your rules.  (The
1306 +global rules taken from the $HOME/.cvsignore file and from $CVSIGNORE are
1307 +not repositioned from their spot at the end of your rules, however.)
1308 +
1309 +manpagesection(PER-DIRECTORY EXCLUDES AND DELETE)
1310 +
1311 +Without a delete option, per-directory excludes are only relevant on the
1312 +sending side, so you can feel free to exclude the merge files themselves
1313 +without affecting the transfer:
1314 +
1315 +verb(
1316 +  rsync -av --exclude='-p .excl' --exclude=.excl host:src/dir /dest
1317 +)
1318 +
1319 +However, if you want to do a delete on the receiving side AND you want some
1320 +files to be excluded from being deleted, you'll need to be sure that the
1321 +receiving side knows what files to exclude.  The easiest way is to include
1322 +the per-directory merge files in the transfer and use --delete-after
1323 +because this ensures that the receiving side gets all the same exclude
1324 +rules as the sending side before it tries to delete anything:
1325 +
1326 +verb(
1327 +  rsync -avE --delete-after host:src/dir /dest
1328 +)
1329 +
1330 +However, if the merge files are not a part of the transfer, you'll need to
1331 +either specify some global exclude rules (i.e. specified on the command
1332 +line), or you'll need to maintain your own per-directory merge files on
1333 +the receiving side.  An example of the first is this (assume that the
1334 +remote .ctrl files exclude themselves):
1335 +
1336 +verb(
1337 +  rsync -av --exclude='-p .ctrl' --exclude-from=/my/extra.rules
1338 +    --delete host:src/dir /dest
1339 +)
1340 +
1341 +In the above example the extra.rules file can affect both sides of the
1342 +transfer, but the rules are subservient to the rules merged from the .ctrl
1343 +files because they were specified after the per-directory merge rule.
1344 +
1345 +In one final example, the remote side is excluding the .rsync-excludes
1346 +files from the transfer, but we want to use our own .rsync-excludes files
1347 +to control what gets deleted on the receiving side.  To do this we must
1348 +specifically exclude the per-directory merge files (so that they don't get
1349 +deleted) and then put rules into the local files to control what else
1350 +should not get deleted.  Like one of these commands:
1351 +
1352 +verb(
1353 +  rsync -av --exclude='-pE /.rsync-excludes' --delete host:src/dir /dest
1354 +  rsync -avEE --delete host:src/dir /dest
1355  )
1356  
1357  manpagesection(BATCH MODE)
1358 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
1359 +++ testsuite/exclude.test      2005-01-16 01:04:44
1360 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
1361  makepath "$fromdir/foo/down/to/you"
1362  makepath "$fromdir/bar/down/to/foo/too"
1363  makepath "$fromdir/mid/for/foo/and/that/is/who"
1364 +cat >"$fromdir/.excl" <<EOF
1365 +.excl
1366 +*.bak
1367 +*.old
1368 +*.junk
1369 +EOF
1370  echo kept >"$fromdir/foo/file1"
1371  echo removed >"$fromdir/foo/file2"
1372  echo cvsout >"$fromdir/foo/file2.old"
1373 +cat >"$fromdir/foo/.excl" <<EOF
1374 ++ .excl
1375 +- file1
1376 +EOF
1377 +cat >"$fromdir/bar/.excl" <<EOF
1378 +home-cvs-exclude
1379 +-p .excl2
1380 ++ to
1381 +EOF
1382  echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1383 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
1384 +.excl2
1385 +EOF
1386  echo keeper >"$fromdir/bar/down/to/foo/file1"
1387  echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1388  echo gone >"$fromdir/bar/down/to/foo/file3"
1389  echo lost >"$fromdir/bar/down/to/foo/file4"
1390  echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1391  echo smashed >"$fromdir/bar/down/to/foo/to"
1392 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1393 ++ *.junk
1394 +EOF
1395 +# This one should be ineffectual
1396 +cat >"$fromdir/mid/.excl2" <<EOF
1397 +extra
1398 +EOF
1399  echo cvsout >"$fromdir/mid/one-in-one-out"
1400  echo one-in-one-out >"$fromdir/mid/.cvsignore"
1401  echo cvsin >"$fromdir/mid/one-for-all"
1402 +cat >"$fromdir/mid/.excl" <<EOF
1403 +-C
1404 +EOF
1405  echo cvsin >"$fromdir/mid/for/one-in-one-out"
1406  echo expunged >"$fromdir/mid/for/foo/extra"
1407  echo retained >"$fromdir/mid/for/foo/keep"
1408 @@ -57,7 +85,7 @@ cat >"$excl" <<EOF
1409  - to
1410  + file4
1411  - file[2-9]
1412 -- /mid/for/foo/extra
1413 +/mid/for/foo/extra
1414  EOF
1415  
1416  cat >"$scratchdir/.cvsignore" <<EOF
1417 @@ -97,7 +125,26 @@ $RSYNC -av --existing --include='*/' --e
1418  # Now, test if rsync excludes the same files, this time with --cvs-exclude
1419  # and --delete-excluded.
1420  
1421 -checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1422 +checkit "$RSYNC -avvC --include=\"-m $excl\" \
1423 +    --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1424 +
1425 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1426 +
1427 +rm "$chkdir"/.excl
1428 +rm "$chkdir"/foo/file1
1429 +rm "$chkdir"/bar/.excl
1430 +rm "$chkdir"/bar/down/to/.excl2
1431 +rm "$chkdir"/bar/down/to/foo/.excl2
1432 +rm "$chkdir"/mid/.excl
1433 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1434 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1435 +
1436 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1437 +
1438 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1439 +# file.
1440 +
1441 +checkit "$RSYNC -avv --exclude='-p .excl' --exclude=\"-m $excl\" \
1442      --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1443  
1444  # The script would have aborted on error, so getting here means we've won.