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