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