Updated to apply to latest source.
[rsync/rsync-patches.git] / filter.diff
index eddb92b..e7790e1 100644 (file)
@@ -6,11 +6,12 @@ command before "make":
 This patch adds the ability to merge rules into your excludes/includes
 using a ". FILE" idiom.  If you specify a name without slashes, that
 filename will be looked for in every subdirectory that rsync visits,
-and its rules will affect the current directory and its subdirectories.
+and the rules found in that subdirectory's file will affect that dir
+and (optionally) its subdirectories.
 
 For example:
 
-  rsync -av --exclude='. .excl' from/ to
+  rsync -av --exclude='. .excl' --inherit=. from/ to
 
 The above will look for a file named ".excl" in every directory of the
 hierarchy that rsync visits, and it will exclude (by default) names
@@ -23,36 +24,38 @@ this:
   *.o
 
 Then the file ".excl2" will also be read in the current dir, and all
-subdirs of the current dir.  The file ".excl3" would just be read in
-for the current dir because its name contained a slash.
+subdirs of the current dir (due to the --inherit option).  The file
+".excl3" would just be read in for the current dir because its name
+contained a slash.
 
 ..wayne..
 
---- exclude.c  15 May 2004 19:31:13 -0000      1.77
-+++ exclude.c  15 May 2004 20:03:30 -0000
-@@ -30,32 +30,89 @@ extern int verbose;
+--- orig/exclude.c     2004-05-22 19:30:03
++++ exclude.c  2004-07-03 20:21:27
+@@ -30,13 +30,59 @@ extern int verbose;
  extern int eol_nulls;
  extern int list_only;
  extern int recurse;
 +extern int io_error;
++extern int module_id;
++extern int inherit_exclude_levels;
 +extern int sanitize_paths;
  
  extern char curr_dir[];
++extern unsigned int curr_dir_len;
  
--struct exclude_list_struct exclude_list = { 0, 0, "" };
+ struct exclude_list_struct exclude_list = { 0, 0, "" };
 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
--struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
-+struct exclude_list_struct exclude_list = { 0, 0, 0, "" };
-+struct exclude_list_struct server_exclude_list = { 0, 0, 0, "server " };
+ struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
  char *exclude_path_prefix = NULL;
--/** Build an exclude structure given a exclude pattern */
--static void make_exclude(struct exclude_list_struct *listp, const char *pattern,
--                       int pat_len, int include)
-+struct exclude_list_root {
++struct exclude_struct **mergelist_parents;
++int mergelist_cnt = 0;
++int mergelist_size = 0;
++
++struct mergelist_save_struct {
 +    struct exclude_list_struct *array;
-+    int cnt;
-+} local_lists;
++    int count;
++};
 +
 +static char dirbuf[MAXPATHLEN];
 +static unsigned int dirbuf_offset = 0;
@@ -61,9 +64,9 @@ for the current dir because its name contained a slash.
 + * of both the head and tail pointers.  The list is slightly unusual in that
 + * a parent-dir's content can be appended to the end of the local list in a
 + * special way:  the last item in the local list has its "next" pointer set
-+ * to point to the parent's list, but the local list's tail pointer points
++ * to point to the inherited list, but the local list's tail pointer points
 + * at the end of the local list.  Thus, if the local list is empty, the head
-+ * will be pointing at the parent content but the tail will be NULL.  To
++ * will be pointing at the inherited content but the tail will be NULL.  To
 + * help you visualize this, here are the possible list arrangements:
 + *
 + * Completely Empty                     Local Content Only
@@ -85,26 +88,23 @@ for the current dir because its name contained a slash.
 + * The easiest way to handle this is to simply truncate the list after the
 + * tail item and then free the local list from the head.  When inheriting
 + * the list for a new local dir, we just save off the exclude_list_struct
-+ * values (so we can pop back to them) and set the tail to NULL.
++ * values (so we can pop back to them later) and set the tail to NULL.
 + */
-+
-+/** Build an exclude structure given an exclude pattern. */
-+static void make_exclude(struct exclude_list_struct *listp, const char *pat,
-+                       unsigned int pat_len, unsigned int mflags)
- {
-       struct exclude_struct *ret;
+ /** Build an exclude structure given an exclude pattern. */
+ static void make_exclude(struct exclude_list_struct *listp, const char *pat,
+@@ -46,6 +92,24 @@ static void make_exclude(struct exclude_
        const char *cp;
--      int ex_len;
-+      unsigned int ex_len;
-+
+       unsigned int ex_len;
 +      if (mflags & MATCHFLG_MERGE_FILE) {
-+              struct exclude_struct *ex;
++              int i;
 +              /* If the local include file was already mentioned, don't
 +               * add it again. */
-+              for (ex = listp->head; ex; ex = ex->next) {
-+                      if ((ex->match_flags & MATCHFLG_MERGE_FILE)
-+                          && strlen(ex->pattern) == pat_len
-+                          && strncmp(ex->pattern, pat, pat_len) == 0)
++              for (i = 0; i < mergelist_cnt; i++) {
++                      struct exclude_struct *ex = mergelist_parents[i];
++                      if (strlen(ex->pattern) == pat_len
++                          && memcmp(ex->pattern, pat, pat_len) == 0)
 +                              return;
 +              }
 +              if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
@@ -114,52 +114,40 @@ for the current dir because its name contained a slash.
 +              } else
 +                      mflags &= ~MATCHFLG_CVSIGNORE;
 +      }
++
        ret = new(struct exclude_struct);
        if (!ret)
                out_of_memory("make_exclude");
-       memset(ret, 0, sizeof ret[0]);
--      ret->include = include;
-       if (exclude_path_prefix)
--              ret->match_flags |= MATCHFLG_ABS_PATH;
--      if (exclude_path_prefix && *pattern == '/')
-+              mflags |= MATCHFLG_ABS_PATH;
-+      if (exclude_path_prefix && *pat == '/')
-               ex_len = strlen(exclude_path_prefix);
-       else
-               ex_len = 0;
-@@ -64,33 +121,52 @@ static void make_exclude(struct exclude_
-               out_of_memory("make_exclude");
-       if (ex_len)
-               memcpy(ret->pattern, exclude_path_prefix, ex_len);
--      strlcpy(ret->pattern + ex_len, pattern, pat_len + 1);
-+      strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
-       pat_len += ex_len;
-       if (strpbrk(ret->pattern, "*[?")) {
--              ret->match_flags |= MATCHFLG_WILD;
-+              mflags |= MATCHFLG_WILD;
-               if ((cp = strstr(ret->pattern, "**")) != NULL) {
--                      ret->match_flags |= MATCHFLG_WILD2;
-+                      mflags |= MATCHFLG_WILD2;
-                       /* If the pattern starts with **, note that. */
-                       if (cp == ret->pattern)
--                              ret->match_flags |= MATCHFLG_WILD2_PREFIX;
-+                              mflags |= MATCHFLG_WILD2_PREFIX;
-               }
-       }
-       if (pat_len > 1 && ret->pattern[pat_len-1] == '/') {
-               ret->pattern[pat_len-1] = 0;
--              ret->directory = 1;
-+              mflags |= MATCHFLG_DIRECTORY;
+@@ -81,14 +145,36 @@ static void make_exclude(struct exclude_
+               mflags |= MATCHFLG_DIRECTORY;
        }
  
-       for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
+-      for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
 -              ret->slash_cnt++;
-+              ret->u.slash_cnt++;
++      if (mflags & MATCHFLG_MERGE_FILE) {
++              struct exclude_list_struct *lp
++                  = new_array(struct exclude_list_struct, 1);
++              if (!lp)
++                      out_of_memory("make_exclude");
++              lp->head = lp->tail = NULL;
++              if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
++                      out_of_memory("make_exclude");
++              ret->u.mergelist = lp;
++              if (mergelist_cnt == mergelist_size) {
++                  mergelist_size += 5;
++                  mergelist_parents = realloc_array(mergelist_parents,
++                                                    struct exclude_struct *,
++                                                    mergelist_size);
++                  if (!mergelist_parents)
++                          out_of_memory("make_exclude");
++              }
++              mergelist_parents[mergelist_cnt++] = ret;
++      } else {
++              for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
++                      ret->u.slash_cnt++;
++      }
+       ret->match_flags = mflags;
  
 -      if (!listp->tail)
 +      if (!listp->tail) {
@@ -171,32 +159,20 @@ for the current dir because its name contained a slash.
                listp->tail->next = ret;
                listp->tail = ret;
        }
-+
-+      if (mflags & MATCHFLG_MERGE_FILE) {
-+              struct exclude_list_struct *lp;
-+              int ndx = local_lists.cnt++;
-+              local_lists.array = realloc_array(local_lists.array,
-+                  struct exclude_list_struct, local_lists.cnt);
-+              if (!local_lists.array)
-+                      out_of_memory("make_exclude");
-+              lp = &local_lists.array[ndx];
-+              lp->head = lp->tail = NULL;
-+              lp->parent = ret;
-+              if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
-+                      out_of_memory("make_exclude");
-+              ret->u.array_index = ndx; /* Overwrites u.slash_cnt. */
-+      }
-+
-+      ret->match_flags = mflags;
- }
+@@ -96,22 +182,150 @@ static void make_exclude(struct exclude_
  
  static void free_exclude(struct exclude_struct *ex)
-@@ -99,18 +175,90 @@ static void free_exclude(struct exclude_
+ {
++      if (ex->match_flags & MATCHFLG_MERGE_FILE) {
++              free(ex->u.mergelist->debug_type);
++              free(ex->u.mergelist);
++      }
+       free(ex->pattern);
        free(ex);
  }
  
--void free_exclude_list(struct exclude_list_struct *listp)
-+static void free_exclude_list(struct exclude_list_struct *listp)
+-void clear_exclude_list(struct exclude_list_struct *listp)
++static void clear_exclude_list(struct exclude_list_struct *listp)
  {
 -      struct exclude_struct *ent, *next;
 -
@@ -207,6 +183,7 @@ for the current dir because its name contained a slash.
 +              struct exclude_struct *ent, *next;
 +              /* Truncate any inherited items from the local list. */
 +              listp->tail->next = NULL;
++              /* Now free everything that is left. */
 +              for (ent = listp->head; ent; ent = next) {
 +                      next = ent->next;
 +                      free_exclude(ent);
@@ -216,52 +193,92 @@ for the current dir because its name contained a slash.
        listp->head = listp->tail = NULL;
  }
  
-+void *push_local_excludes(char *fname, unsigned int offset)
++void pre_inherit_files(char *fname, unsigned int len)
 +{
++      char path[MAXPATHLEN], save[MAXPATHLEN];
++      unsigned int limit;
 +      int i;
-+      struct exclude_list_root *push = new_array(struct exclude_list_root, 1);
 +
-+      if (!push)
-+              out_of_memory("push_local_excludes");
++      if (sanitize_paths)
++              limit = strlen(lp_path(module_id));
++      else
++              limit = 0;
++      fname[len] = '\0';
++      if (*fname == '/')
++              strcpy(path, fname);
++      else
++              len = pathjoin(path, sizeof path, curr_dir, fname);
 +
-+      push->cnt = local_lists.cnt;
-+      push->array = new_array(struct exclude_list_struct, local_lists.cnt);
-+      if (!push->array)
-+              out_of_memory("push_local_excludes");
++      if (len >= MAXPATHLEN || len == 0)
++              return;
 +
-+      memcpy(push->array, local_lists.array,
-+          sizeof (struct exclude_list_struct) * local_lists.cnt);
++      for (i = inherit_exclude_levels, len--; i && len > limit; ) {
++              if (path[--len] == '/')
++                      i--;
++      }
++      for (len++; i < inherit_exclude_levels; i++) {
++              char *s = path + len;
++              strcpy(save, s);
++              push_local_excludes(path, len);
++              strcpy(s, save);
++              if (!(s = strchr(s, '/')))
++                      break; /* Impossible ... */
++              len = s - path + 1;
++      }
++}
++
++void *push_local_excludes(char *fname, unsigned int offset)
++{
++      struct mergelist_save_struct *push;
++      struct exclude_list_struct *ap;
++      int i;
 +
 +      /* Make it easy to construct the full path for a merge-file that was
 +       * specified with a relative path by saving off the current dir. */
 +      memcpy(dirbuf, fname, offset);
 +      dirbuf_offset = offset;
 +
-+      for (i = 0; i < local_lists.cnt; i++) {
-+              struct exclude_list_struct *listp = &local_lists.array[i];
-+              char *file = listp->parent->pattern;
++      if (!(push = new_array(struct mergelist_save_struct, 1)))
++              out_of_memory("push_local_excludes");
++
++      push->count = mergelist_cnt;
++      push->array = new_array(struct exclude_list_struct, mergelist_cnt);
++      if (!push->array)
++              out_of_memory("push_local_excludes");
++
++      for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
++              memcpy(ap++, mergelist_parents[i]->u.mergelist,
++                     sizeof (struct exclude_list_struct));
++      }
++
++      /* Note: add_exclude_file() might increase mergelist_cnt, so keep
++       * this loop separate from the above loop. */
++      for (i = 0; i < mergelist_cnt; i++) {
++              struct exclude_struct *ex = mergelist_parents[i];
++              struct exclude_list_struct *lp = ex->u.mergelist;
 +              int flags;
 +
 +              if (verbose > 2) {
 +                      rprintf(FINFO, "[%s] pushing %sexclude list\n",
-+                              who_am_i(), listp->debug_type);
++                              who_am_i(), lp->debug_type);
 +              }
-+              if (listp->parent->match_flags & MATCHFLG_CVSIGNORE) {
-+                      listp->head = NULL; /* Subdirs don't inherit rules. */
++
++              if (ex->match_flags & MATCHFLG_CVSIGNORE) {
++                      lp->head = NULL; /* CVS doesn't inherit rules. */
 +                      flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
 +              } else {
-+                      flags = listp->parent->match_flags & MATCHFLG_INCLUDE
++                      flags = ex->match_flags & MATCHFLG_INCLUDE
 +                          ? XFLG_DEF_INCLUDE : 0;
 +              }
-+              listp->tail = NULL; /* Signals no local content. */
-+              if (strlcpy(fname +  offset, file, MAXPATHLEN - offset)
-+                  < MAXPATHLEN - offset)
-+                      add_exclude_file(listp, fname, flags);
++              lp->tail = NULL; /* Switch any local rules to inherited. */
++              if (strlcpy(dirbuf +  dirbuf_offset, ex->pattern,
++                  MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
++                      add_exclude_file(lp, dirbuf, flags);
 +              else {
 +                      io_error |= IOERR_GENERAL;
 +                      rprintf(FINFO,
 +                          "cannot add local excludes in long-named directory %s\n",
-+                          full_fname(fname));
++                          full_fname(dirbuf));
 +              }
 +      }
 +
@@ -270,25 +287,36 @@ for the current dir because its name contained a slash.
 +
 +void pop_local_excludes(void *mem)
 +{
++      struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
++      struct exclude_list_struct *ap;
 +      int i;
 +
-+      for (i = 0; i < local_lists.cnt; i++) {
-+              struct exclude_list_struct *listp = &local_lists.array[i];
++      for (i = mergelist_cnt; i-- > 0; ) {
++              struct exclude_struct *ex = mergelist_parents[i];
++              struct exclude_list_struct *lp = ex->u.mergelist;
++
 +              if (verbose > 2) {
 +                      rprintf(FINFO, "[%s] popping %sexclude list\n",
-+                              who_am_i(), listp->debug_type);
++                              who_am_i(), lp->debug_type);
 +              }
-+              free_exclude_list(listp);
++
++              clear_exclude_list(lp);
++      }
++
++      mergelist_cnt = pop->count;
++      for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
++              memcpy(mergelist_parents[i]->u.mergelist, ap++,
++                     sizeof (struct exclude_list_struct));
 +      }
-+      free(local_lists.array);
-+      local_lists = *(struct exclude_list_root*)mem;
-+      free(mem);
++
++      free(pop->array);
++      free(pop);
 +}
 +
  static int check_one_exclude(char *name, struct exclude_struct *ex,
                               int name_is_dir)
  {
-@@ -121,7 +269,7 @@ static int check_one_exclude(char *name,
+@@ -122,7 +336,7 @@ static int check_one_exclude(char *name,
        /* If the pattern does not have any slashes AND it does not have
         * a "**" (which could match a slash), then we just match the
         * name portion of the path. */
@@ -297,50 +325,25 @@ for the current dir because its name contained a slash.
                if ((p = strrchr(name,'/')) != NULL)
                        name = p+1;
        }
-@@ -134,7 +282,8 @@ static int check_one_exclude(char *name,
-       if (!name[0]) return 0;
--      if (ex->directory && !name_is_dir) return 0;
-+      if ((ex->match_flags & MATCHFLG_DIRECTORY) && !name_is_dir)
-+              return 0;
-       if (*pattern == '/') {
-               match_start = 1;
-@@ -146,9 +295,9 @@ static int check_one_exclude(char *name,
+@@ -148,9 +362,9 @@ static int check_one_exclude(char *name,
        if (ex->match_flags & MATCHFLG_WILD) {
                /* A non-anchored match with an infix slash and no "**"
                 * needs to match the last slash_cnt+1 name elements. */
--              if (!match_start && ex->slash_cnt &&
-+              if (!match_start && ex->u.slash_cnt &&
-                   !(ex->match_flags & MATCHFLG_WILD2)) {
+-              if (!match_start && ex->slash_cnt
++              if (!match_start && ex->u.slash_cnt
+                   && !(ex->match_flags & MATCHFLG_WILD2)) {
 -                      int cnt = ex->slash_cnt + 1;
 +                      int cnt = ex->u.slash_cnt + 1;
                        for (p = name + strlen(name) - 1; p >= name; p--) {
                                if (*p == '/' && !--cnt)
                                        break;
-@@ -201,9 +350,11 @@ static void report_exclude_result(char c
-       if (verbose >= 2) {
-               rprintf(FINFO, "[%s] %scluding %s %s because of %spattern %s%s\n",
--                      who_am_i(), ent->include ? "in" : "ex",
-+                      who_am_i(),
-+                      ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex",
-                       name_is_dir ? "directory" : "file", name, type,
--                      ent->pattern, ent->directory ? "/" : "");
-+                      ent->pattern,
-+                      ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "");
-       }
- }
-@@ -217,10 +368,18 @@ int check_exclude(struct exclude_list_st
+@@ -221,6 +435,13 @@ int check_exclude(struct exclude_list_st
        struct exclude_struct *ent;
  
        for (ent = listp->head; ent; ent = ent->next) {
 +              if (ent->match_flags & MATCHFLG_MERGE_FILE) {
-+                      struct exclude_list_struct *lp
-+                          = &local_lists.array[ent->u.array_index];
-+                      int rc = check_exclude(lp, name, name_is_dir);
++                      int rc = check_exclude(ent->u.mergelist, name,
++                                             name_is_dir);
 +                      if (rc)
 +                              return rc;
 +                      continue;
@@ -348,27 +351,7 @@ for the current dir because its name contained a slash.
                if (check_one_exclude(name, ent, name_is_dir)) {
                        report_exclude_result(name, ent, name_is_dir,
                                              listp->debug_type);
--                      return ent->include ? 1 : -1;
-+                      return (ent->match_flags & MATCHFLG_INCLUDE) ? 1 : -1;
-               }
-       }
-@@ -236,11 +395,11 @@ int check_exclude(struct exclude_list_st
-  * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for
-  * the list-clearing "!" token.
-  */
--static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr,
--                                 int xflags)
-+static const char *get_exclude_tok(const char *p, unsigned int *len_ptr,
-+                                 unsigned int *flag_ptr, int xflags)
- {
-       const unsigned char *s = (const unsigned char *)p;
--      int len;
-+      unsigned int len, mflags = 0;
-       if (xflags & XFLG_WORD_SPLIT) {
-               /* Skip over any initial whitespace. */
-@@ -250,13 +409,19 @@ static const char *get_exclude_tok(const
+@@ -253,11 +474,16 @@ static const char *get_exclude_tok(const
                p = (const char *)s;
        }
  
@@ -376,71 +359,21 @@ for the current dir because its name contained a slash.
 +      /* Is this a +/-/. followed by a space (not whitespace)? */
        if (!(xflags & XFLG_WORDS_ONLY)
 -          && (*s == '-' || *s == '+') && s[1] == ' ') {
--              *incl_ptr = *s == '+';
 +          && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
-+              if (*s == '+')
-+                      mflags |= MATCHFLG_INCLUDE;
+               if (*s == '+')
+                       mflags |= MATCHFLG_INCLUDE;
 +              else if (*s == '.') {
 +                      mflags |= MATCHFLG_MERGE_FILE;
 +                      if (xflags & XFLG_DEF_INCLUDE)
 +                              mflags |= MATCHFLG_INCLUDE;
 +              }
                s += 2;
--      } else
--              *incl_ptr = xflags & XFLG_DEF_INCLUDE;
-+      } else if (xflags & XFLG_DEF_INCLUDE)
-+              mflags |= MATCHFLG_INCLUDE;
-       if (xflags & XFLG_WORD_SPLIT) {
-               const unsigned char *cp = s;
-@@ -268,9 +433,10 @@ static const char *get_exclude_tok(const
-               len = strlen(s);
-       if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
--              *incl_ptr = -1;
-+              mflags |= MATCHFLG_CLEAR_LIST;
-       *len_ptr = len;
-+      *flag_ptr = mflags;
-       return (const char *)s;
- }
-@@ -278,7 +444,7 @@ static const char *get_exclude_tok(const
- void add_exclude(struct exclude_list_struct *listp, const char *pattern,
-                int xflags)
- {
--      int pat_len, incl;
-+      unsigned int pat_len, mflags;
-       const char *cp;
+       } else if (xflags & XFLG_DEF_INCLUDE)
+               mflags |= MATCHFLG_INCLUDE;
+@@ -306,11 +532,54 @@ void add_exclude(struct exclude_list_str
+                       continue;
+               }
  
-       if (!pattern)
-@@ -287,27 +453,56 @@ void add_exclude(struct exclude_list_str
-       cp = pattern;
-       pat_len = 0;
-       while (1) {
--              cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags);
-+              cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
-               if (!pat_len)
-                       break;
--              /* If we got the special "!" token, clear the list. */
--              if (incl < 0) {
-+              if (mflags & MATCHFLG_CLEAR_LIST) {
-                       if (verbose > 2) {
-                               rprintf(FINFO,
-                                       "[%s] clearing %sexclude list\n",
-                                       who_am_i(), listp->debug_type);
-                       }
-                       free_exclude_list(listp);
--              } else {
--                      make_exclude(listp, cp, pat_len, incl);
--
--                      if (verbose > 2) {
--                              rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s)\n",
--                                      who_am_i(), pat_len, cp,
--                                      listp->debug_type,
--                                      incl ? "include" : "exclude");
-+                      continue;
-+              }
 +              if (mflags & MATCHFLG_MERGE_FILE) {
 +                      char name[MAXPATHLEN];
 +                      if (pat_len >= sizeof name) {
@@ -449,12 +382,22 @@ for the current dir because its name contained a slash.
 +                              continue;
 +                      }
 +                      strlcpy(name, cp, pat_len+1);
-+                      if (strchr(name, '/') != NULL) {
-+                              if (sanitize_paths)
-+                                      sanitize_path(name, curr_dir);
-+                              if (*name == '/')
-+                                      cp = name;
-+                              else {
++                      if (!(xflags & XFLG_PERDIR_MERGE)
++                          && (inherit_exclude_levels == -1
++                           || strchr(name, '/') != NULL)) {
++                              char *mem = NULL;
++                              if (*name == '/') {
++                                      if (sanitize_paths) {
++                                              mem = alloc_sanitize_path(name,
++                                                              curr_dir);
++                                              cp = mem;
++                                      } else
++                                              cp = name;
++                              } else {
++                                      if (sanitize_paths) {
++                                              dirbuf[dirbuf_offset] = '\0';
++                                              sanitize_path(name, dirbuf);
++                                      }
 +                                      if (strlcpy(dirbuf + dirbuf_offset,
 +                                          name, MAXPATHLEN - dirbuf_offset)
 +                                          >= MAXPATHLEN - dirbuf_offset) {
@@ -466,34 +409,37 @@ for the current dir because its name contained a slash.
 +                                      cp = dirbuf;
 +                              }
 +                              add_exclude_file(listp, cp,
-+                                  xflags | XFLG_FATAL_ERRORS);
++                                               xflags | XFLG_FATAL_ERRORS);
++                              if (mem)
++                                      free(mem);
 +                              continue;
-                       }
-               }
-+
-+              make_exclude(listp, cp, pat_len, mflags);
++                      }
++              }
 +
-+              if (verbose > 2) {
+               make_exclude(listp, cp, pat_len, mflags);
+               if (verbose > 2) {
+-                      rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
 +                      rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
-+                              who_am_i(), pat_len, cp, listp->debug_type,
+                               who_am_i(), (int)pat_len, cp, listp->debug_type,
 +                              mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
-+                              mflags & MATCHFLG_INCLUDE ? "in" : "ex");
-+              }
-       }
- }
-@@ -389,15 +584,19 @@ void send_exclude_list(int f)
-               l = strlcpy(p, ent->pattern, sizeof p);
-               if (l == 0 || l >= MAXPATHLEN)
-                       continue;
--              if (ent->directory) {
-+              if (ent->match_flags & MATCHFLG_DIRECTORY) {
-                       p[l++] = '/';
-                       p[l] = '\0';
+                               mflags & MATCHFLG_INCLUDE ? "in" : "ex");
                }
+       }
+@@ -343,6 +612,11 @@ void add_exclude_file(struct exclude_lis
+               return;
+       }
  
--              if (ent->include) {
-+              if (ent->match_flags & MATCHFLG_INCLUDE) {
++      if (verbose > 2) {
++              rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
++                      who_am_i(), fname, xflags);
++      }
++
+       while (1) {
+               char *s = line;
+               int ch, overflow = 0;
+@@ -402,7 +676,11 @@ void send_exclude_list(int f)
+               if (ent->match_flags & MATCHFLG_INCLUDE) {
                        write_int(f, l + 2);
                        write_buf(f, "+ ", 2);
 -              } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
@@ -505,16 +451,16 @@ for the current dir because its name contained a slash.
                        write_int(f, l + 2);
                        write_buf(f, "- ", 2);
                } else
-@@ -438,6 +637,7 @@ void add_cvs_excludes(void)
+@@ -443,6 +721,7 @@ void add_cvs_excludes(void)
        char fname[MAXPATHLEN];
        char *p;
  
-+      add_exclude(&exclude_list, ". .cvsignore", 0);
++      add_exclude(&exclude_list, ". .cvsignore", XFLG_PERDIR_MERGE);
        add_exclude(&exclude_list, default_cvsignore,
                    XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
  
---- flist.c    15 May 2004 19:31:10 -0000      1.223
-+++ flist.c    15 May 2004 20:03:31 -0000
+--- orig/flist.c       2004-07-15 02:21:10
++++ flist.c    2004-07-03 20:21:27
 @@ -39,8 +39,6 @@ extern int module_id;
  extern int ignore_errors;
  extern int numeric_ids;
@@ -524,7 +470,15 @@ for the current dir because its name contained a slash.
  extern int recurse;
  extern char curr_dir[MAXPATHLEN];
  extern char *files_from;
-@@ -65,7 +63,6 @@ extern int write_batch;
+@@ -59,6 +57,7 @@ extern int implied_dirs;
+ extern int copy_links;
+ extern int copy_unsafe_links;
+ extern int protocol_version;
++extern int inherit_exclude_levels;
+ extern int sanitize_paths;
+ extern int delete_excluded;
+ extern int orig_umask;
+@@ -66,7 +65,6 @@ extern int list_only;
  
  extern struct exclude_list_struct exclude_list;
  extern struct exclude_list_struct server_exclude_list;
@@ -532,7 +486,7 @@ for the current dir because its name contained a slash.
  
  int io_error;
  
-@@ -210,8 +207,6 @@ int link_stat(const char *path, STRUCT_S
+@@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
   */
  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
  {
@@ -541,7 +495,7 @@ for the current dir because its name contained a slash.
  #if 0 /* This currently never happens, so avoid a useless compare. */
        if (exclude_level == NO_EXCLUDES)
                return 0;
-@@ -233,10 +228,7 @@ static int check_exclude_file(char *fnam
+@@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
        if (exclude_level != ALL_EXCLUDES)
                return 0;
        if (exclude_list.head
@@ -553,7 +507,7 @@ for the current dir because its name contained a slash.
                return 1;
        return 0;
  }
-@@ -946,15 +938,7 @@ void send_file_name(int f, struct file_l
+@@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
  
        if (recursive && S_ISDIR(file->mode)
            && !(file->flags & FLAG_MOUNT_POINT)) {
@@ -564,12 +518,12 @@ for the current dir because its name contained a slash.
 -                      rprintf(FINFO, "[%s] popping %sexclude list\n",
 -                              who_am_i(), local_exclude_list.debug_type);
 -              }
--              free_exclude_list(&local_exclude_list);
+-              clear_exclude_list(&local_exclude_list);
 -              local_exclude_list = last_list;
        }
  }
  
-@@ -965,6 +949,7 @@ static void send_directory(int f, struct
+@@ -973,6 +958,7 @@ static void send_directory(int f, struct
        struct dirent *di;
        char fname[MAXPATHLEN];
        unsigned int offset;
@@ -577,7 +531,7 @@ for the current dir because its name contained a slash.
        char *p;
  
        d = opendir(dir);
-@@ -988,18 +973,7 @@ static void send_directory(int f, struct
+@@ -996,19 +982,13 @@ static void send_directory(int f, struct
                offset++;
        }
  
@@ -592,56 +546,187 @@ for the current dir because its name contained a slash.
 -                              "cannot cvs-exclude in long-named directory %s\n",
 -                              full_fname(fname));
 -              }
--      }
-+      save_excludes = push_local_excludes(fname, offset);
++      if (inherit_exclude_levels > 0) {
++              pre_inherit_files(fname, offset);
++              inherit_exclude_levels = 0;
+       }
  
++      save_excludes = push_local_excludes(fname, offset);
++
        for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
                char *dname = d_name(di);
-@@ -1019,6 +993,8 @@ static void send_directory(int f, struct
-               io_error |= IOERR_GENERAL;
+               if (dname[0] == '.' && (dname[1] == '\0'
+@@ -1028,6 +1008,8 @@ static void send_directory(int f, struct
                rsyserr(FERROR, errno, "readdir(%s)", dir);
        }
-+
-+      pop_local_excludes(save_excludes);
  
++      pop_local_excludes(save_excludes);
++
        closedir(d);
  }
---- rsync.h    13 May 2004 18:51:22 -0000      1.203
-+++ rsync.h    15 May 2004 20:03:31 -0000
-@@ -493,18 +493,24 @@ struct map_struct {
- #define MATCHFLG_WILD2                (1<<1) /* pattern has '**' */
- #define MATCHFLG_WILD2_PREFIX (1<<2) /* pattern starts with '**' */
- #define MATCHFLG_ABS_PATH     (1<<3) /* path-match on absolute path */
-+#define MATCHFLG_INCLUDE      (1<<4) /* this is an include, not an exclude */
-+#define MATCHFLG_CLEAR_LIST   (1<<5) /* this item is the "!" token */
-+#define MATCHFLG_DIRECTORY    (1<<6) /* this matches only directories */
+--- orig/options.c     2004-07-15 16:51:50
++++ options.c  2004-07-03 20:21:27
+@@ -50,6 +50,7 @@ int preserve_gid = 0;
+ int preserve_times = 0;
+ int update_only = 0;
+ int cvs_exclude = 0;
++int inherit_exclude_levels = -9999;
+ int dry_run = 0;
+ int local_server = 0;
+ int ignore_times = 0;
+@@ -308,7 +309,7 @@ void usage(enum logcode F)
+ enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
+       OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
+-      OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
++      OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_INHERIT, OPT_MODIFY_WINDOW,
+       OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
+       OPT_REFUSED_BASE = 9000};
+@@ -333,6 +334,7 @@ static struct poptOption long_options[] 
+   {"include",          0,  POPT_ARG_STRING, 0,              OPT_INCLUDE, 0, 0 },
+   {"exclude-from",     0,  POPT_ARG_STRING, 0,              OPT_EXCLUDE_FROM, 0, 0 },
+   {"include-from",     0,  POPT_ARG_STRING, 0,              OPT_INCLUDE_FROM, 0, 0 },
++  {"inherit",          0,  POPT_ARG_STRING, 0,              OPT_INHERIT, 0, 0 },
+   {"safe-links",       0,  POPT_ARG_NONE,   &safe_symlinks, 0, 0, 0 },
+   {"help",            'h', POPT_ARG_NONE,   0,              'h', 0, 0 },
+   {"backup",          'b', POPT_ARG_NONE,   &make_backups, 0, 0, 0 },
+@@ -541,6 +543,31 @@ int parse_arguments(int *argc, const cha
+                                        XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
+                       break;
++              case OPT_INHERIT:
++                      arg = poptGetOptArg(pc);
++                      if (isdigit(*arg))
++                              inherit_exclude_levels = atoi(arg);
++                      else if (*arg == '.') {
++                              if (!arg[1])
++                                      inherit_exclude_levels = 0;
++                              else if (arg[1] == '.') {
++                                      inherit_exclude_levels = 0;
++                                      arg--;
++                                      do {
++                                              inherit_exclude_levels++;
++                                              arg += 3;
++                                      } while (strncmp(arg, "/..", 3) == 0);
++                                      if (*arg)
++                                              inherit_exclude_levels = -1;
++                              }
++                      }
++                      if (inherit_exclude_levels < 0) {
++                              snprintf(err_buf, sizeof err_buf,
++                                  "Invalid argument given to --inherit.\n");
++                              return 0;
++                      }
++                      break;
++
+               case 'h':
+                       usage(FINFO);
+                       exit_cleanup(0);
+@@ -770,6 +797,9 @@ int parse_arguments(int *argc, const cha
+               }
+       }
++      if (inherit_exclude_levels < 0)
++              inherit_exclude_levels = -1;
++
+       return 1;
+ }
+@@ -871,6 +901,12 @@ void server_options(char **args,int *arg
+       if (x != 1)
+               args[ac++] = argstr;
++      if (inherit_exclude_levels >= 0) {
++              if (asprintf(&arg, "--inherit=%d", inherit_exclude_levels) < 0)
++                      goto oom;
++              args[ac++] = arg;
++      }
++
+       if (block_size) {
+               if (asprintf(&arg, "-B%u", block_size) < 0)
+                       goto oom;
+--- orig/rsync.h       2004-07-07 08:27:00
++++ rsync.h    2004-07-03 20:21:27
+@@ -108,6 +108,7 @@
+ #define XFLG_DEF_INCLUDE      (1<<1)
+ #define XFLG_WORDS_ONLY       (1<<2)
+ #define XFLG_WORD_SPLIT       (1<<3)
++#define XFLG_PERDIR_MERGE     (1<<4)
+ #define PERMS_REPORT          (1<<0)
+ #define PERMS_SKIP_MTIME      (1<<1)
+@@ -496,11 +497,16 @@ struct map_struct {
+ #define MATCHFLG_INCLUDE      (1<<4) /* this is an include, not an exclude */
+ #define MATCHFLG_DIRECTORY    (1<<5) /* this matches only directories */
+ #define MATCHFLG_CLEAR_LIST   (1<<6) /* this item is the "!" token */
 +#define MATCHFLG_MERGE_FILE   (1<<7) /* specifies a file to merge */
 +#define MATCHFLG_CVSIGNORE    (1<<8) /* parse this as a .cvsignore file */
  struct exclude_struct {
        struct exclude_struct *next;
        char *pattern;
--      int match_flags;
--      int include;
--      int directory;
+       unsigned int match_flags;
 -      int slash_cnt;
-+      unsigned int match_flags;
 +      union {
 +              int slash_cnt;
-+              int array_index;
++              struct exclude_list_struct *mergelist;
 +      } u;
  };
  
  struct exclude_list_struct {
--      struct exclude_struct *head;
--      struct exclude_struct *tail;
-+      struct exclude_struct *head, *tail;
-+      struct exclude_struct *parent;
-       char *debug_type;
- };
---- rsync.yo   7 May 2004 00:18:37 -0000       1.169
-+++ rsync.yo   15 May 2004 20:03:32 -0000
-@@ -1075,6 +1075,72 @@ itemize(
+--- orig/rsync.yo      2004-07-15 02:21:11
++++ rsync.yo   2004-07-03 20:21:27
+@@ -332,6 +332,7 @@ verb(
+      --exclude-from=FILE     exclude patterns listed in FILE
+      --include=PATTERN       don't exclude files matching PATTERN
+      --include-from=FILE     don't exclude patterns listed in FILE
++     --inherit=DEPTH         make per-dir merge files inherited
+      --files-from=FILE       read FILE for list of source-file names
+  -0  --from0                 all file lists are delimited by nulls
+      --version               print version number
+@@ -688,6 +689,28 @@ dit(bf(--include-from=FILE)) This specif
+ from a file.
+ If em(FILE) is bf(-) the list will be read from standard input.
++dit(bf(--inherit=DEPTH)) Using this option allows you to specify that the
++contents of per-directory merge files is inherited by the subdirectories of
++the spot where the rules were read in.  If a subdirectory has its own
++per-directory merge file, its contents are prefixed to the inherited rules,
++which gives them higher priority.
++
++The DEPTH value tells rsync how much deeper than the root directory of the
++transfer should be scanned for merge files.  If you don't need any higher
++directories scanned, use "." (dot).  If you want the parent directory
++scanned, specify ".." (dot dot) or "1" (i.e. one directory higher).  You
++can continue to specify either more ".."s (separated by slashes) or simply
++supply the count of how many parent-directory levels should be scanned.
++The reason this is useful is that you may wish to transfer just a small
++portion of a larger tree of files, but to be sure to get all the
++appropriate exclude rules, you need to be sure that rsync reads in all the
++merge files from the top of the tree of related files.
++
++Note also that you can eliminate all the inherited rules for the current
++per-directory ruleset by putting the list-clearing token (!) in the file.
++This only clears the rules in the current per-directory sub-list for the
++current directory and its subdirectories.
++
+ dit(bf(--files-from=FILE)) Using this option allows you to specify the
+ exact list of files to transfer (as read from the specified FILE or "-"
+ for stdin).  It also tweaks the default behavior of rsync to make
+@@ -1039,6 +1062,11 @@ itemize(
+   then it is always considered an exclude pattern, even if specified as
+   part of an include option. The prefix is discarded before matching.
++  it() if the pattern starts with ". " (a dot followed by a space) then it
++  its pattern is taken to be a merge file that is read in to supplement the
++  current rules.  See the section on MERGING EXCLUDE FILES for more
++  information.
++
+   it() if the pattern is a single exclamation mark ! then the current
+   include/exclude list is reset, removing all previously defined patterns.
+ )
+@@ -1091,6 +1119,67 @@ itemize(
    it would be excluded by the "*")
  )
  
@@ -655,24 +740,16 @@ for the current dir because its name contained a slash.
 +itemize(
 +  it() If the filename has no slashes in it, it is a per-directory merge;
 +  rsync scans every directory that it traverses for the named file, merging
-+  its contents (when it exists) file at the start of this per-directory
-+  sub-list (subdirectories inherit the contents of their parent directories
-+  by default, and each subdirectory's rules have precedence over the parent
-+  directory's rules).
++  its contents (when it exists) at the start of this per-directory
++  sub-list.
 +
 +  it() If a filename has a slash in it, it is a single-instance merge; the
 +  named file's contents will be merged into the current exclude file,
 +  replacing the merge rule.  Thus, you should use the name ./foo instead of
-+  foo if you don't want to scan for "foo" in all the subdirectories of the
-+  current directory.
++  foo if you don't want to scan for "foo" in all the subdirectories in the
++  transferred tree of directories.
 +)
 +
-+Note also that you can eliminate all the inherited rules for the current
-+per-directory ruleset by putting the list-clearing token (!) in the file.
-+This only clears the rules for the current per-directory sub-list (up
-+through the ! token) and only for the current directory and its
-+subdirectories.
-+
 +Here's an example exclude file (which you'd specify via the normal
 +--exclude-from option):
 +
@@ -705,12 +782,90 @@ for the current dir because its name contained a slash.
 +this.)
 +
 +Note also that the parsing of any merge-file named ".cvsignore" is always
-+done in a CVS-compatible manner (even if -C wasn't specified) -- i.e. the
-+rules are always exclude rules (even when specified by an include option),
-+they are split on whitespace, no special prefixes or list-clearing tokens
-+are honored, and (for per-directory files) subdirectories don't inherit the
-+parent directory's rules.
++done in a CVS-compatible manner, even if -C wasn't specified (i.e. the
++rules are always exclude rules (even when specified by an include option);
++they are split on whitespace; and no special prefixes, list-clearing
++tokens, or comment characters are honored).
++
++See the --inherit option for how to make the rules read from a
++per-directory merge file inherited by all the subdirectories of the
++spot where the per-directory rule file was found.
 +
  manpagesection(BATCH MODE)
  
  bf(Note:) Batch mode should be considered experimental in this version
+--- orig/testsuite/exclude.test        2004-05-29 21:25:45
++++ testsuite/exclude.test     2004-07-03 20:21:27
+@@ -23,19 +23,47 @@ export HOME CVSIGNORE
+ makepath "$fromdir/foo/down/to/you"
+ makepath "$fromdir/bar/down/to/foo/too"
+ makepath "$fromdir/mid/for/foo/and/that/is/who"
++cat >"$fromdir/.excl" <<EOF
++.excl
++*.bak
++*.old
++*.junk
++EOF
+ echo kept >"$fromdir/foo/file1"
+ echo removed >"$fromdir/foo/file2"
+ echo cvsout >"$fromdir/foo/file2.old"
++cat >"$fromdir/foo/.excl" <<EOF
+++ .excl
++- file1
++EOF
++cat >"$fromdir/bar/.excl" <<EOF
++home-cvs-exclude
++. .excl2
+++ to
++EOF
+ echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
++cat >"$fromdir/bar/down/to/.excl2" <<EOF
++.excl2
++EOF
+ echo keeper >"$fromdir/bar/down/to/foo/file1"
+ echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
+ echo gone >"$fromdir/bar/down/to/foo/file3"
+ echo lost >"$fromdir/bar/down/to/foo/file4"
+ echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
+ echo smashed >"$fromdir/bar/down/to/foo/to"
++cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
+++ *.junk
++EOF
++# This one should be ineffectual
++cat >"$fromdir/mid/.excl2" <<EOF
++extra
++EOF
+ echo cvsout >"$fromdir/mid/one-in-one-out"
+ echo one-in-one-out >"$fromdir/mid/.cvsignore"
+ echo cvsin >"$fromdir/mid/one-for-all"
++cat >"$fromdir/mid/.excl" <<EOF
++. .cvsignore
++EOF
+ echo cvsin >"$fromdir/mid/for/one-in-one-out"
+ echo expunged >"$fromdir/mid/for/foo/extra"
+ echo retained >"$fromdir/mid/for/foo/keep"
+@@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
+ checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
+     --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
++# Modify the chk dir for our merge-exclude test and then tweak the dir times.
++
++rm "$chkdir"/.excl
++rm "$chkdir"/foo/file1
++rm "$chkdir"/bar/.excl
++rm "$chkdir"/bar/down/to/.excl2
++rm "$chkdir"/bar/down/to/foo/.excl2
++rm "$chkdir"/mid/.excl
++cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
++cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
++
++$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
++
++# Now, test if rsync excludes the same files, this time with a merge-exclude
++# file.
++
++checkit "$RSYNC -avv --exclude='. .excl' --exclude-from=\"$excl\" --inherit=. \
++    --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
++
+ # The script would have aborted on error, so getting here means we've won.
+ exit 0