Need to make sure that the destination file doesn't exist before we
[rsync/rsync-patches.git] / filter.diff
index 52fc1ca..ffaf81c 100644 (file)
@@ -4,14 +4,14 @@ command before "make":
     make proto
 
 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 the rules found in that subdirectory's file will affect that dir
-and (optionally) its subdirectories.
+using a ". FILE" idiom.  If you specify a name with a preceding -p
+option, that filename will be looked for in every subdirectory that
+rsync visits, and the rules found in that subdirectory's file will
+affect that dir and its subdirs.
 
 For example:
 
-  rsync -av --exclude='. .excl' --inherit=. from/ to
+  rsync -av --exclude='. -p .excl' 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
@@ -19,46 +19,115 @@ based on the rules found therein.  If one of the .excl files contains
 this:
 
   + *.c
-  . .excl2
-  . ./.excl3
+  . -p .excl2
+  . .excl3
   *.o
+  /foobar
 
-Then the file ".excl2" will also be read in the current dir, and all
-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.
+Then the file ".excl2" will also be read in from the current dir and all
+its subdirs (due to the -p option).  The file ".excl3" would just be
+read in from the current dir.   The exclusion of "foobar" will only
+happen in that .excl file's directory because the rule is anchored (so
+that's how you can make rules local instead of inherited).
 
 ..wayne..
 
---- exclude.c  22 May 2004 05:32:20 -0000      1.82
-+++ exclude.c  4 Jun 2004 07:07:33 -0000
-@@ -30,13 +30,59 @@ extern int verbose;
+--- orig/clientserver.c        2004-08-02 02:29:16
++++ clientserver.c     2004-08-10 15:44:15
+@@ -48,12 +48,14 @@ extern int no_detach;
+ extern int default_af_hint;
+ extern char *bind_address;
+ extern struct exclude_list_struct server_exclude_list;
+-extern char *exclude_path_prefix;
+ extern char *config_file;
+ extern char *files_from;
+ char *auth_user;
++/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
++unsigned int module_dirlen = 0;
++
+ /**
+  * Run a client connected to an rsyncd.  The alternative to this
+  * function for remote-shell connections is do_cmd().
+@@ -300,26 +302,28 @@ static int rsync_module(int f_in, int f_
+       /* TODO: Perhaps take a list of gids, and make them into the
+        * supplementary groups. */
+-      exclude_path_prefix = use_chroot? "" : lp_path(i);
+-      if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
+-              exclude_path_prefix = "";
++      if (use_chroot) {
++              module_dirlen = 0;
++              set_excludes_dir("/", 1);
++      } else {
++              module_dirlen = strlen(lp_path(i));
++              set_excludes_dir(lp_path(i), module_dirlen);
++      }
+       p = lp_include_from(i);
+       add_exclude_file(&server_exclude_list, p,
+-                       XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
++                       XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
+       p = lp_include(i);
+       add_exclude(&server_exclude_list, p,
+-                  XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
++                  XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
+       p = lp_exclude_from(i);
+       add_exclude_file(&server_exclude_list, p,
+-                       XFLG_FATAL_ERRORS);
++                       XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
+       p = lp_exclude(i);
+-      add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
+-
+-      exclude_path_prefix = NULL;
++      add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
+       log_init();
+--- orig/exclude.c     2004-08-10 18:17:01
++++ exclude.c  2004-08-13 07:40:08
+@@ -30,13 +30,69 @@ 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;
++extern unsigned int module_dirlen;
  
  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 " };
- char *exclude_path_prefix = NULL;
-+struct exclude_struct **mergelist_parents;
-+int mergelist_cnt = 0;
-+int mergelist_size = 0;
+-char *exclude_path_prefix = NULL;
 +
 +struct mergelist_save_struct {
 +    struct exclude_list_struct *array;
 +    int count;
 +};
 +
-+static char dirbuf[MAXPATHLEN];
-+static unsigned int dirbuf_offset = 0;
++/* The dirbuf is set by push_local_excludes() to the current subdirectory
++ * relative to curr_dir that is being processed.  The path always has a
++ * trailing slash appended, and the variable dirbuf_len contains the length
++ * of this path prefix.  The path is always absolute. */
++static char dirbuf[MAXPATHLEN+1];
++static unsigned int dirbuf_len = 0;
++static int dirbuf_depth;
++
++/* This is True when we're scanning parent dirs for per-dir merge-files. */
++static BOOL parent_dirscan = False;
++
++/* This array contains a list of all the currently active per-dir merge
++ * files.  This makes it easier to save the appropriate values when we
++ * "push" down into each subdirectory. */
++static struct exclude_struct **mergelist_parents;
++static int mergelist_cnt = 0;
++static int mergelist_size = 0;
 +
 +/* Each exclude_list_struct describes a singly-linked list by keeping track
 + * of both the head and tail pointers.  The list is slightly unusual in that
@@ -93,10 +162,17 @@ contained a slash.
  
  /** 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_
+@@ -46,23 +102,50 @@ static void make_exclude(struct exclude_
        const char *cp;
        unsigned int ex_len;
  
++      if (verbose > 2) {
++              rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
++                      who_am_i(), (int)pat_len, pat, listp->debug_type,
++                      mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
++                      mflags & MATCHFLG_INCLUDE ? "in" : "ex");
++      }
++
 +      if (mflags & MATCHFLG_MERGE_FILE) {
 +              int i;
 +              /* If the local include file was already mentioned, don't
@@ -118,7 +194,32 @@ contained a slash.
        ret = new(struct exclude_struct);
        if (!ret)
                out_of_memory("make_exclude");
-@@ -81,14 +145,36 @@ static void make_exclude(struct exclude_
+       memset(ret, 0, sizeof ret[0]);
+-      if (exclude_path_prefix)
+-              mflags |= MATCHFLG_ABS_PATH;
+-      if (exclude_path_prefix && *pat == '/')
+-              ex_len = strlen(exclude_path_prefix);
+-      else
++      if (mflags & MATCHFLG_ABS_PATH) {
++              if (*pat != '/') {
++                      mflags &= ~MATCHFLG_ABS_PATH;
++                      ex_len = 0;
++              } else
++                      ex_len = dirbuf_len - module_dirlen - 1;
++      } else
+               ex_len = 0;
+       ret->pattern = new_array(char, ex_len + pat_len + 1);
+       if (!ret->pattern)
+               out_of_memory("make_exclude");
+       if (ex_len)
+-              memcpy(ret->pattern, exclude_path_prefix, ex_len);
++              memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
+       strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
+       pat_len += ex_len;
+@@ -81,14 +164,40 @@ static void make_exclude(struct exclude_
                mflags |= MATCHFLG_DIRECTORY;
        }
  
@@ -130,16 +231,20 @@ contained a slash.
 +              if (!lp)
 +                      out_of_memory("make_exclude");
 +              lp->head = lp->tail = NULL;
-+              if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
++              if ((cp = strrchr(ret->pattern, '/')) != NULL)
++                      cp++;
++              else
++                      cp = ret->pattern;
++              if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 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_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 {
@@ -159,7 +264,7 @@ contained a slash.
                listp->tail->next = ret;
                listp->tail = ret;
        }
-@@ -96,22 +182,150 @@ static void make_exclude(struct exclude_
+@@ -96,22 +205,267 @@ static void make_exclude(struct exclude_
  
  static void free_exclude(struct exclude_struct *ex)
  {
@@ -193,50 +298,159 @@ contained a slash.
        listp->head = listp->tail = NULL;
  }
  
-+void pre_inherit_files(char *fname, unsigned int len)
++/* This returns an expanded (absolute) filename for the merge-file name if
++ * the name has any slashes in it OR if the parent_dirscan var is True;
++ * otherwise it returns the original merge_file name.  If the len_ptr value
++ * is non-NULL the merge_file name is limited by the referenced length
++ * value and will be updated with the length of the resulting name.  We
++ * always return a name that is null terminated, even if the merge_file
++ * name was not. */
++static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
++                            unsigned int prefix_skip)
 +{
-+      char path[MAXPATHLEN], save[MAXPATHLEN];
-+      unsigned int limit;
-+      int i;
++      static char buf[MAXPATHLEN];
++      char *fn, tmpbuf[MAXPATHLEN];
++      unsigned int fn_len;
++
++      if (!parent_dirscan && *merge_file != '/') {
++              /* Return the name unchanged it doesn't have any slashes. */
++              if (len_ptr) {
++                      const char *p = merge_file + *len_ptr;
++                      while (--p > merge_file && *p != '/') {}
++                      if (p == merge_file) {
++                              strlcpy(buf, merge_file, *len_ptr + 1);
++                              return buf;
++                      }
++              } else if (strchr(merge_file, '/') == NULL)
++                      return (char *)merge_file;
++      }
++
++      fn = *merge_file == '/' ? buf : tmpbuf;
++      if (sanitize_paths) {
++              const char *r = prefix_skip ? "/" : NULL;
++              /* null-terminate the name if it isn't already */
++              if (len_ptr && merge_file[*len_ptr]) {
++                      char *to = fn == buf ? tmpbuf : buf;
++                      strlcpy(to, merge_file, *len_ptr + 1);
++                      merge_file = to;
++              }
++              if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
++                      rprintf(FERROR, "merge-file name overflows: %s\n",
++                              merge_file);
++                      return NULL;
++              }
++      } else {
++              strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
++              clean_fname(fn, 1);
++      }
++      
++      fn_len = strlen(fn);
++      if (fn == buf)
++              goto done;
++
++      if (dirbuf_len + fn_len >= MAXPATHLEN) {
++              rprintf(FERROR, "merge-file name overflows: %s\n", fn);
++              return NULL;
++      }
++      memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
++      memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
++      fn_len = clean_fname(buf, 1);
++
++    done:
++      if (len_ptr)
++              *len_ptr = fn_len;
++      return buf;
++}
 +
++/* Sets the dirbuf and dirbuf_len values. */
++void set_excludes_dir(const char *dir, unsigned int dirlen)
++{
++      unsigned int len;
++      if (*dir != '/') {
++              memcpy(dirbuf, curr_dir, curr_dir_len);
++              dirbuf[curr_dir_len] = '/';
++              len = curr_dir_len + 1;
++              if (len + dirlen >= MAXPATHLEN)
++                      dirlen = 0;
++      } else
++              len = 0;
++      memcpy(dirbuf + len, dir, dirlen);
++      dirbuf[dirlen + len] = '\0';
++      dirbuf_len = clean_fname(dirbuf, 1);
++      if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
++          && dirbuf[dirbuf_len-2] == '/')
++              dirbuf_len -= 2;
++      dirbuf[dirbuf_len++] = '/';
++      dirbuf[dirbuf_len] = '\0';
 +      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);
++              dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
++}
++
++/* This routine takes a per-dir merge-file entry and finishes its setup.
++ * If the name has a path portion then we check to see if it refers to a
++ * parent directory of the first transfer dir.  If it does, we scan all the
++ * dirs from that point through the parent dir of the transfer dir looking
++ * for the per-dir merge-file in each one. */
++static BOOL setup_merge_file(struct exclude_struct *ex,
++                           struct exclude_list_struct *lp, int flags)
++{
++      char buf[MAXPATHLEN];
++      char *x, *y, *pat = ex->pattern;
++      unsigned int len;
++
++      if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
++              return 0;
 +
-+      if (len >= MAXPATHLEN || len == 0)
-+              return;
++      y = strrchr(x, '/');
++      *y = '\0';
++      ex->pattern = strdup(y+1);
++      if (!*x)
++              x = "/";
++      if (*x == '/')
++              strlcpy(buf, x, MAXPATHLEN);
++      else
++              pathjoin(buf, MAXPATHLEN, dirbuf, x);
 +
-+      for (i = inherit_exclude_levels, len--; i && len > limit; ) {
-+              if (path[--len] == '/')
-+                      i--;
++      len = clean_fname(buf, 1);
++      if (len != 1 && len < MAXPATHLEN-1) {
++              buf[len++] = '/';
++              buf[len] = '\0';
 +      }
-+      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;
++      /* This ensures that the specified dir is a parent of the transfer. */
++      for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
++      if (*x)
++              y += strlen(y); /* nope -- skip the scan */
++
++      parent_dirscan = True;
++      while (*y) {
++              char save[MAXPATHLEN];
++              strlcpy(save, y, MAXPATHLEN);
++              *y = '\0';
++              dirbuf_len = y - dirbuf;
++              strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
++              add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
++              if (ex->match_flags & MATCHFLG_CVSIGNORE)
++                      lp->head = NULL; /* CVS doesn't inherit rules. */
++              lp->tail = NULL;
++              strlcpy(y, save, MAXPATHLEN);
++              while ((*x++ = *y++) != '/') {}
 +      }
++      parent_dirscan = False;
++      free(pat);
++      return 1;
 +}
 +
-+void *push_local_excludes(char *fname, unsigned int offset)
++/* Each time rsync changes to a new directory it call this function to
++ * handle all the per-dir merge-files.  The "dir" value is the current path
++ * relative to curr_dir (which might not be null-terminated).  We copy it
++ * into dirbuf so that we can easily append a file name on the end. */
++void *push_local_excludes(const char *dir, unsigned int dirlen)
 +{
 +      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;
++      set_excludes_dir(dir, dirlen);
 +
 +      if (!(push = new_array(struct mergelist_save_struct, 1)))
 +              out_of_memory("push_local_excludes");
@@ -271,15 +485,23 @@ contained a slash.
 +                          ? XFLG_DEF_INCLUDE : 0;
 +              }
 +              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);
++
++              if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
++                      ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
++                      if (setup_merge_file(ex, lp, flags))
++                              set_excludes_dir(dir, dirlen);
++              }
++
++              if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
++                  MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
++                      add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
 +              else {
 +                      io_error |= IOERR_GENERAL;
 +                      rprintf(FINFO,
 +                          "cannot add local excludes in long-named directory %s\n",
 +                          full_fname(dirbuf));
 +              }
++              dirbuf[dirbuf_len] = '\0';
 +      }
 +
 +      return (void*)push;
@@ -316,7 +538,7 @@ contained a slash.
  static int check_one_exclude(char *name, struct exclude_struct *ex,
                               int name_is_dir)
  {
-@@ -122,7 +336,7 @@ static int check_one_exclude(char *name,
+@@ -125,13 +479,14 @@ 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. */
@@ -325,7 +547,16 @@ contained a slash.
                if ((p = strrchr(name,'/')) != NULL)
                        name = p+1;
        }
-@@ -148,9 +362,9 @@ static int check_one_exclude(char *name,
+       else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
+-          && curr_dir[1]) {
+-              pathjoin(full_name, sizeof full_name, curr_dir + 1, name);
++          && curr_dir_len > module_dirlen + 1) {
++              pathjoin(full_name, sizeof full_name,
++                       curr_dir + module_dirlen + 1, name);
+               name = full_name;
+       }
+@@ -148,9 +503,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. */
@@ -337,7 +568,7 @@ contained a slash.
                        for (p = name + strlen(name) - 1; p >= name; p--) {
                                if (*p == '/' && !--cnt)
                                        break;
-@@ -221,6 +435,13 @@ int check_exclude(struct exclude_list_st
+@@ -221,6 +576,13 @@ int check_exclude(struct exclude_list_st
        struct exclude_struct *ent;
  
        for (ent = listp->head; ent; ent = ent->next) {
@@ -351,12 +582,12 @@ contained a slash.
                if (check_one_exclude(name, ent, name_is_dir)) {
                        report_exclude_result(name, ent, name_is_dir,
                                              listp->debug_type);
-@@ -253,11 +474,16 @@ static const char *get_exclude_tok(const
+@@ -253,11 +615,36 @@ static const char *get_exclude_tok(const
                p = (const char *)s;
        }
  
 -      /* Is this a '+' or '-' followed by a space (not whitespace)? */
-+      /* Is this a +/-/. followed by a space (not whitespace)? */
++      /* Check for a +/-/. followed by a space (not whitespace). */
        if (!(xflags & XFLG_WORDS_ONLY)
 -          && (*s == '-' || *s == '+') && s[1] == ' ') {
 +          && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
@@ -366,102 +597,151 @@ contained a slash.
 +                      mflags |= MATCHFLG_MERGE_FILE;
 +                      if (xflags & XFLG_DEF_INCLUDE)
 +                              mflags |= MATCHFLG_INCLUDE;
++                      while (s[2] == '-') {
++                              s += 2;
++                              do {
++                                      switch (*++s) {
++                                      case 'p':
++                                              mflags |= MATCHFLG_PERDIR_MERGE
++                                                      | MATCHFLG_FINISH_SETUP;
++                                              break;
++                                      case '-':
++                                              if (s[1] == ' ')
++                                                      goto done;
++                                      default:
++                                              rprintf(FERROR,
++                                                  "invalid merge options: %s\n",
++                                                  p);
++                                              exit_cleanup(RERR_SYNTAX);
++                                      }
++                              } while (s[1] != ' ');
++                      }
 +              }
++          done:
                s += 2;
        } else if (xflags & XFLG_DEF_INCLUDE)
                mflags |= MATCHFLG_INCLUDE;
-@@ -306,11 +532,54 @@ void add_exclude(struct exclude_list_str
+@@ -273,6 +660,8 @@ static const char *get_exclude_tok(const
+       if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
+               mflags |= MATCHFLG_CLEAR_LIST;
++      if (xflags & XFLG_ABS_PATH)
++              mflags |= MATCHFLG_ABS_PATH;
+       *len_ptr = len;
+       *flag_ptr = mflags;
+@@ -284,7 +673,7 @@ void add_exclude(struct exclude_list_str
+                int xflags)
+ {
+       unsigned int pat_len, mflags;
+-      const char *cp;
++      const char *cp, *p;
+       if (!pattern)
+               return;
+@@ -292,9 +681,15 @@ void add_exclude(struct exclude_list_str
+       cp = pattern;
+       pat_len = 0;
+       while (1) {
++              /* Remember that the returned string is NOT '\0' terminated! */
+               cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
+               if (!pat_len)
+                       break;
++              if (pat_len >= MAXPATHLEN) {
++                      rprintf(FERROR, "discarding over-long exclude: %s\n",
++                              cp);
++                      continue;
++              }
+               if (mflags & MATCHFLG_CLEAR_LIST) {
+                       if (verbose > 2) {
+@@ -306,13 +701,24 @@ void add_exclude(struct exclude_list_str
                        continue;
                }
  
+-              make_exclude(listp, cp, pat_len, mflags);
+-
+-              if (verbose > 2) {
+-                      rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
+-                              who_am_i(), (int)pat_len, cp, listp->debug_type,
+-                              mflags & MATCHFLG_INCLUDE ? "in" : "ex");
 +              if (mflags & MATCHFLG_MERGE_FILE) {
-+                      char name[MAXPATHLEN];
-+                      if (pat_len >= sizeof name) {
-+                              rprintf(FERROR,
-+                                      "merge filename too long: %s\n", cp);
-+                              continue;
-+                      }
-+                      strlcpy(name, cp, pat_len+1);
-+                      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) {
-+                                              rprintf(FERROR,
-+                                                  "merge filename too long: %s...\n",
-+                                                  dirbuf);
++                      unsigned int len = pat_len;
++                      if (mflags & MATCHFLG_PERDIR_MERGE) {
++                              if (parent_dirscan) {
++                                      if (!(p = parse_merge_name(cp, &len, module_dirlen)))
 +                                              continue;
-+                                      }
-+                                      cp = dirbuf;
++                                      make_exclude(listp, p, len, mflags);
++                                      continue;
 +                              }
-+                              add_exclude_file(listp, cp,
-+                                               xflags | XFLG_FATAL_ERRORS);
-+                              if (mem)
-+                                      free(mem);
++                      } else {
++                              if (!(p = parse_merge_name(cp, &len, 0)))
++                                      continue;
++                              add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
 +                              continue;
 +                      }
-+              }
+               }
 +
-               make_exclude(listp, cp, pat_len, mflags);
++              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(), (int)pat_len, cp, listp->debug_type,
-+                              mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
-                               mflags & MATCHFLG_INCLUDE ? "in" : "ex");
+@@ -321,7 +727,7 @@ void add_exclude_file(struct exclude_lis
+                     int xflags)
+ {
+       FILE *fp;
+-      char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
++      char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
+       char *eob = line + sizeof line - 1;
+       int word_split = xflags & XFLG_WORD_SPLIT;
+@@ -342,6 +748,12 @@ void add_exclude_file(struct exclude_lis
                }
-       }
-@@ -343,6 +612,11 @@ void add_exclude_file(struct exclude_lis
                return;
        }
++      dirbuf[dirbuf_len] = '\0';
++
 +      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)
+@@ -402,7 +814,21 @@ 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] == ' ') {
 +              } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
-+                      write_int(f, l + 2);
-+                      write_buf(f, ". ", 2);
++                      char buf[32], *op = buf;
++                      *op++ = '.';
++                      *op++ = ' ';
++                      if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
++                              *op++ = '-';
++                              *op++ = 'p';
++                              if (*p == '-')
++                                      *op++ = '-';
++                              *op++ = ' ';
++                      }
++                      write_int(f, l + (op - buf));
++                      write_buf(f, buf, op - buf);
 +              } else if ((*p == '-' || *p == '+' || *p == '.')
 +                  && p[1] == ' ') {
                        write_int(f, l + 2);
                        write_buf(f, "- ", 2);
                } else
-@@ -443,6 +721,7 @@ void add_cvs_excludes(void)
+@@ -443,6 +869,7 @@ void add_cvs_excludes(void)
        char fname[MAXPATHLEN];
        char *p;
  
-+      add_exclude(&exclude_list, ". .cvsignore", XFLG_PERDIR_MERGE);
++      add_exclude(&exclude_list, ". -p .cvsignore", 0);
        add_exclude(&exclude_list, default_cvsignore,
                    XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
  
---- flist.c    29 May 2004 21:21:17 -0000      1.226
-+++ flist.c    4 Jun 2004 07:07:33 -0000
-@@ -39,8 +39,6 @@ extern int module_id;
+--- orig/flist.c       2004-09-21 09:40:27
++++ flist.c    2004-08-12 18:59:28
+@@ -40,10 +40,9 @@ extern int module_id;
  extern int ignore_errors;
  extern int numeric_ids;
  
@@ -469,16 +749,11 @@ contained a slash.
 -
  extern int recurse;
  extern char curr_dir[MAXPATHLEN];
++extern unsigned int curr_dir_len;
  extern char *files_from;
-@@ -58,6 +56,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 filesfrom_fd;
  
- extern int read_batch;
-@@ -65,7 +64,6 @@ extern int write_batch;
+@@ -67,7 +66,6 @@ extern int list_only;
  
  extern struct exclude_list_struct exclude_list;
  extern struct exclude_list_struct server_exclude_list;
@@ -486,7 +761,7 @@ contained a slash.
  
  int io_error;
  
-@@ -210,8 +208,6 @@ int link_stat(const char *path, STRUCT_S
+@@ -223,8 +221,6 @@ int link_stat(const char *path, STRUCT_S
   */
  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
  {
@@ -495,7 +770,7 @@ contained a slash.
  #if 0 /* This currently never happens, so avoid a useless compare. */
        if (exclude_level == NO_EXCLUDES)
                return 0;
-@@ -233,10 +229,7 @@ static int check_exclude_file(char *fnam
+@@ -246,10 +242,7 @@ static int check_exclude_file(char *fnam
        if (exclude_level != ALL_EXCLUDES)
                return 0;
        if (exclude_list.head
@@ -507,7 +782,7 @@ contained a slash.
                return 1;
        return 0;
  }
-@@ -942,15 +935,7 @@ void send_file_name(int f, struct file_l
+@@ -978,15 +971,7 @@ void send_file_name(int f, struct file_l
  
        if (recursive && S_ISDIR(file->mode)
            && !(file->flags & FLAG_MOUNT_POINT)) {
@@ -523,7 +798,7 @@ contained a slash.
        }
  }
  
-@@ -961,6 +946,7 @@ static void send_directory(int f, struct
+@@ -997,6 +982,7 @@ static void send_directory(int f, struct
        struct dirent *di;
        char fname[MAXPATHLEN];
        unsigned int offset;
@@ -531,7 +806,7 @@ contained a slash.
        char *p;
  
        d = opendir(dir);
-@@ -984,19 +970,13 @@ static void send_directory(int f, struct
+@@ -1020,18 +1006,7 @@ static void send_directory(int f, struct
                offset++;
        }
  
@@ -546,17 +821,12 @@ contained a slash.
 -                              "cannot cvs-exclude in long-named directory %s\n",
 -                              full_fname(fname));
 -              }
-+      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);
-               if (dname[0] == '.' && (dname[1] == '\0'
-@@ -1016,6 +996,8 @@ static void send_directory(int f, struct
+@@ -1052,6 +1027,8 @@ static void send_directory(int f, struct
                rsyserr(FERROR, errno, "readdir(%s)", dir);
        }
  
@@ -565,104 +835,89 @@ contained a slash.
        closedir(d);
  }
  
---- options.c  27 May 2004 21:51:53 -0000      1.153
-+++ options.c  4 Jun 2004 07:07:34 -0000
-@@ -48,6 +48,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;
-@@ -306,7 +307,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};
-@@ -331,6 +332,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 },
-@@ -543,6 +545,31 @@ int parse_arguments(int *argc, const cha
-                                        XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
-                       break;
+@@ -1071,6 +1048,7 @@ struct file_list *send_file_list(int f, 
+       char *p, *dir, olddir[sizeof curr_dir];
+       char lastpath[MAXPATHLEN] = "";
+       struct file_list *flist;
++      BOOL need_first_push = True;
+       int64 start_write;
+       int use_ff_fd = 0;
  
-+              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;
-+                              }
+@@ -1091,6 +1069,10 @@ struct file_list *send_file_list(int f, 
+                               exit_cleanup(RERR_FILESELECT);
+                       }
+                       use_ff_fd = 1;
++                      if (curr_dir_len < MAXPATHLEN - 1) {
++                              push_local_excludes(curr_dir, curr_dir_len);
++                              need_first_push = False;
 +                      }
-+                      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);
-@@ -767,6 +794,9 @@ int parse_arguments(int *argc, const cha
                }
        }
  
-+      if (inherit_exclude_levels < 0)
-+              inherit_exclude_levels = -1;
-+
-       return 1;
- }
+@@ -1121,6 +1103,15 @@ struct file_list *send_file_list(int f, 
+                       }
+               }
  
-@@ -866,6 +896,12 @@ void server_options(char **args,int *arg
-       if (x != 1)
-               args[ac++] = argstr;
++              if (need_first_push) {
++                      if ((p = strrchr(fname, '/')) != NULL) {
++                              if (*++p && strcmp(p, ".") != 0)
++                                      push_local_excludes(fname, p - fname);
++                      } else if (strcmp(fname, ".") != 0)
++                              push_local_excludes(fname, 0);
++                      need_first_push = False;
++              }
++
+               if (link_stat(fname, &st, keep_dirlinks) != 0) {
+                       if (f != -1) {
+                               io_error |= IOERR_GENERAL;
+--- orig/options.c     2004-09-20 05:10:48
++++ options.c  2004-08-12 18:59:28
+@@ -287,6 +287,7 @@ void usage(enum logcode F)
+   rprintf(F,"     --include=PATTERN       don't exclude files matching PATTERN\n");
+   rprintf(F,"     --include-from=FILE     don't exclude patterns listed in FILE\n");
+   rprintf(F,"     --files-from=FILE       read FILE for list of source-file names\n");
++  rprintf(F," -E                          same as --exclude='. -p /.rsync-excludes'\n");
+   rprintf(F," -0, --from0                 all *-from file lists are delimited by nulls\n");
+   rprintf(F,"     --version               print version number\n");
+   rprintf(F,"     --daemon                run as an rsync daemon\n");
+@@ -389,6 +390,7 @@ static struct poptOption long_options[] 
+   {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
+   {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
+   {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
++  {0,                 'E', POPT_ARG_NONE,   0,              'E', 0, 0 },
+   {0,                 'P', POPT_ARG_NONE,   0,              'P', 0, 0 },
+   {"config",           0,  POPT_ARG_STRING, &config_file, 0, 0, 0 },
+   {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
+@@ -585,6 +587,11 @@ int parse_arguments(int *argc, const cha
+                       am_sender = 1;
+                       break;
  
-+      if (inherit_exclude_levels >= 0) {
-+              if (asprintf(&arg, "--inherit=%d", inherit_exclude_levels) < 0)
-+                      goto oom;
-+              args[ac++] = arg;
-+      }
++              case 'E':
++                      add_exclude(&exclude_list,
++                                  ". -p /.rsync-excludes", 0);
++                      break;
 +
-       if (block_size) {
-               if (asprintf(&arg, "-B%u", block_size) < 0)
-                       goto oom;
---- rsync.h    16 May 2004 07:28:24 -0000      1.204
-+++ rsync.h    4 Jun 2004 07:07:34 -0000
+               case 'P':
+                       do_progress = 1;
+                       keep_partial = 1;
+--- orig/rsync.h       2004-08-03 15:41:32
++++ rsync.h    2004-08-08 06:07:01
 @@ -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 XFLG_ABS_PATH         (1<<4)
  
  #define PERMS_REPORT          (1<<0)
  #define PERMS_SKIP_MTIME      (1<<1)
-@@ -496,11 +497,16 @@ struct map_struct {
+@@ -499,11 +500,18 @@ 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 */
++#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
++#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
  struct exclude_struct {
        struct exclude_struct *next;
        char *pattern;
@@ -675,127 +930,297 @@ contained a slash.
  };
  
  struct exclude_list_struct {
---- rsync.yo   21 May 2004 09:44:32 -0000      1.170
-+++ rsync.yo   4 Jun 2004 07:07:35 -0000
-@@ -331,6 +331,7 @@ verb(
-      --exclude-from=FILE     exclude patterns listed in FILE
+--- orig/rsync.yo      2004-09-20 05:10:48
++++ rsync.yo   2004-08-13 00:43:31
+@@ -364,6 +364,7 @@ verb(
       --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
++ -E                          same as --exclude='. -p /.rsync-excludes'
   -0  --from0                 all file lists are delimited by nulls
       --version               print version number
-@@ -683,6 +684,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
-@@ -1038,6 +1061,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.
+      --daemon                run as an rsync daemon
+@@ -1025,24 +1026,32 @@ The exclude and include patterns specifi
+ selection of which files to transfer and which files to skip.
  
-+  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.
+ Rsync builds an ordered list of include/exclude options as specified on
+-the command line. Rsync checks each file and directory 
+-name against each exclude/include pattern in turn. The first matching
++the command line.
++It can also be told to check for include/exclude options in each
++directory that rsync visits during the transfer (see the section on
++MERGED EXCLUDE FILES for the details on these per-directory exclude
++files).
 +
++As the list of files/directories to transfer is built, rsync checks each
++name against every exclude/include pattern in turn. The first matching
+ pattern is acted on. If it is an exclude pattern, then that file is
+ skipped. If it is an include pattern then that filename is not
+ skipped. If no matching include/exclude pattern is found then the
+ filename is not skipped.
+-The filenames matched against the exclude/include patterns are relative
+-to the "root of the transfer".  If you think of the transfer as a
+-subtree of names that are being sent from sender to receiver, the root
+-is where the tree starts to be duplicated in the destination directory.
+-This root governs where patterns that start with a / match (see below).
++The global include/exclude rules are anchored at the "root of the
++transfer" (as opposed to per-directory rules, which are anchored at
++the merge-file's directory).  If you think of the transfer as a
++subtree of names that are being sent from sender to receiver, the
++transfer-root is where the tree starts to be duplicated in the
++destination directory.  This root governs where patterns that start
++with a / match (as described in the list on pattern forms below).
+ Because the matching is relative to the transfer-root, changing the
+ trailing slash on a source path or changing your use of the --relative
+ option affects the path you need to use in your matching (in addition to
+ changing how much of the file tree is duplicated on the destination
+-system).  The following examples demonstrate this.
++host).  The following examples demonstrate this.
+ Let's say that we want to match two source files, one with an absolute
+ path of "/home/me/foo/bar", and one with a path of "/home/you/bar/baz".
+@@ -1089,23 +1098,27 @@ because rsync did not descend through th
+ hierarchy.
+ Note also that the --include and --exclude options take one pattern
+-each. To add multiple patterns use the --include-from and
+---exclude-from options or multiple --include and --exclude options. 
++each. To add multiple patterns use the --include-from and --exclude-from
++options or multiple --include and --exclude options.
+-The patterns can take several forms. The rules are:
++The include/exclude patterns can take several forms. The rules are:
+ itemize(
+-  it() if the pattern starts with a / then it is matched against the
+-  start of the filename, otherwise it is matched against the end of
+-  the filename.
+-  This is the equivalent of a leading ^ in regular expressions.
+-  Thus "/foo" would match a file called "foo" at the transfer-root
+-  (see above for how this is different from the filesystem-root).
+-  On the other hand, "foo" would match any file called "foo"
++  it() if the pattern starts with a / then it is anchored to a
++  particular spot in the hierarchy of files, otherwise it is matched
++  against the end of the pathname.  This is similar to a leading ^ in
++  regular expressions.
++  Thus "/foo" would match a file called "foo" at either the "root of the
++  transfer" (for a global rule) or in the merge-file's directory (for a
++  per-directory rule).
++  An unqualified "foo" would match any file or directory named "foo"
+   anywhere in the tree because the algorithm is applied recursively from
++  the
+   top down; it behaves as if each path component gets a turn at being the
+-  end of the file name.
++  end of the file name.  Even the unanchored "sub/foo" would match at
++  any point in the hierarchy where a "foo" was found within a directory
++  named "sub".
+   it() if the pattern ends with a / then it will only match a
+   directory, not a file, link, or device.
+@@ -1118,22 +1131,31 @@ itemize(
+   single asterisk pattern "*" will stop at slashes.
+   it() if the pattern contains a / (not counting a trailing /) or a "**"
+-  then it is matched against the full filename, including any leading
+-  directory. If the pattern doesn't contain a / or a "**", then it is
++  then it is matched against the full pathname, including any leading
++  directories. If the pattern doesn't contain a / or a "**", then it is
+   matched only against the final component of the filename.  Again,
+   remember that the algorithm is applied recursively so "full filename" can
+   actually be any portion of a path below the starting directory.
+   it() if the pattern starts with "+ " (a plus followed by a space)
+   then it is always considered an include pattern, even if specified as
+-  part of an exclude option. The prefix is discarded before matching.
++  part of an exclude option. (The prefix is discarded before matching.)
+   it() if the pattern starts with "- " (a minus followed by a space)
+   then it is always considered an exclude pattern, even if specified as
+-  part of an include option. The prefix is discarded before matching.
++  part of an include option. (The prefix is discarded before matching.)
++
++  it() if the pattern starts with ". " (a dot followed by a space) then its
++  pattern is taken to be a merge-file that is read in to supplement the
++  current rules.  See the section on MERGED 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.
- )
-@@ -1090,6 +1118,67 @@ itemize(
-   it would be excluded by the "*")
++  The "current" list is either the global list of rules (which are
++  specified via options) or a set of per-directory rules (which are
++  inherited in their own sub-list, so a subdirectory can use this to
++  clear out the parent's rules).
  )
  
-+manpagesection(MERGING EXCLUDE FILES)
+ The +/- rules are most useful in a list that was read from a file, allowing
+@@ -1180,8 +1202,160 @@ itemize(
+   it() --include "*/" --include "*.c" --exclude "*" would include all 
+   directories and C source files
+   it() --include "foo/" --include "foo/bar.c" --exclude "*" would include
+-  only foo/bar.c (the foo/ directory must be explicitly included or
+-  it would be excluded by the "*")
++  only the foo directory and foo/bar.c (the foo directory must be
++  explicitly included or it would be excluded by the "*")
++)
 +
-+You can merge whole files into an exclude file using a rule that starts
-+with a ". " (a dot followed by a space) and has a filename in place of the
-+pattern.  There are two types of merge rules, single-instance and
-+per-directory:
++manpagesection(MERGED EXCLUDE FILES)
 +
-+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) at the start of this per-directory
-+  sub-list.
++You can merge whole files into an exclude file by specifying a rule that
++starts with a ". " (a dot followed by a space) and putting a filename in
++place of the pattern.  There are two kinds of merged exclude files --
++single-instance and per-directory.  The choice is made via an option
++placed prior to the merge-file name:
 +
-+  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 in the
-+  transferred tree of directories.
-+)
++startdit()
++
++dit(bf(-p)) Make the file a per-directory merge-file.  Rsync will scan
++every directory that it traverses for the named file, merging its contents
++when the file exists.  These exclude files must exist on the sending side
++because it is the sending side that is being scanned for available files
++to send.  The files may also need to be transferred to the receiving side
++if you want them to affect what files don't get deleted (see PER-DIRECTORY
++EXCLUDES AND DELETE below).
++
++dit(bf(--)) End the scanning of options.  Useful if you want to specify a
++filename that begins with a dash.
++
++enddit()
++
++Per-directory rules are inherited in all subdirectories of the directory
++where the merge-file was found.  Each subdirectory's rules are prefixed
++to the inherited rules from the parent directories, which gives the
++newest rules a higher priority than the inherited rules.  The entire set
++of per-dir rules is grouped together in the spot where the merge-file was
++specified, so it is possible to override per-dir rules via a rule that
++got specified earlier in the list of global rules.
 +
-+Here's an example exclude file (which you'd specify via the normal
-+--exclude-from option):
++If you don't want a per-dir rule to be inherited, anchor it with a leading
++slash.  Anchored rules in a per-directory merge-file are relative to the
++merge-file's directory, so a rule "/foo" would only exclude the file "foo"
++in the directory where the per-dir exclude file was found.
++
++Here's an example exclude file which you'd specify via the normal
++--exclude-from=FILE option:
 +
 +verb(
 +    . /home/user/.global_excludes
-+    *.gz
-+    . .excl
++    *.gz
++    . -p .excl
 +    + *.[ch]
-+    *.o
++    *.o
 +)
 +
 +This will merge the contents of the /home/user/.global_excludes file at the
 +start of the list and also turns the ".excl" filename into a per-directory
-+exclude file whose local contents will be merged into the list in place of
-+the .excl line.
++exclude file.  All the merged rules default to being exclude rules because
++an exclude statement was used to specify them.  Rules read in from the
++.global_excludes file are anchored just like all other global rules.
++
++If a per-directory merge-file is specified with a path that is a parent
++directory of the first transfer directory, rsync will scan all the parent
++dirs from that starting point to the transfer directory for the indicated
++per-directory file.  For instance, the -E option is an abbreviation for
++this command:
++
++verb(
++  --exclude='. -p /.rsync-excludes'
++)
++
++That exclude tells rsync to scan for the file .rsync-excludes in all
++directories from the root down through the source of the transfer.  (For
++an rsync daemon, the "root dir" is always the module's "path" setting.)
++
++Some examples of this pre-scanning for per-directory files:
++
++verb(
++  rsync -avE /src/path/ /dest/dir
++  rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
++  rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
++)
++
++The first two commands above will look for ".rsync-excludes" in "/" and
++"/src" before the normal scan begins looking for the file in "/src/path"
++and its subdirectories.  The last command avoids the parent-dir scan
++and only looks for the ".rsync-excludes" files in each directory that is
++a part of the transfer.
++
++Finally, note that the parsing of any merge-file named ".cvsignore" is
++always done in a CVS-compatible manner, even if -C wasn't specified.  This
++means that its rules are always excludes (even if an include option
++specified the file), patterns are split on whitespace, the rules are never
++inherited, and no special characters are honored (e.g. no comments, no "!",
++etc.).
 +
 +Additionally, you can affect where the --cvs-exclude (-C) option's
-+inclusion of a per-directory .cvsignore file gets placed into your rules by
-+adding an explicit merge rule for ".cvsignore".  For instance, specifying
-+this:
++inclusion of the per-directory .cvsignore file gets placed into your rules
++by adding your own explicit per-directory merge rule for ".cvsignore".
++Without this, rsync would add its this rule at the end of all your other
++rules (giving it a lower priority than your command-line rules).  For
++example:
 +
 +verb(
-+  rsync -avC --exclude='. .cvsignore' --exclude-from=foo a/ b
++  rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
 +)
 +
-+will merge all the per-directory .cvsignore rules at the start of your list
-+rather than at the end.  This allows their dir-specific rules to supersede
-+your rules instead of being subservient to them.  (The global rules taken
-+from the $HOME/.cvsignore file and from $CVSIGNORE are not affected by
-+this.)
++The above will merge all the per-directory .cvsignore rules at the start of
++your list rather than at the end.  This allows their dir-specific rules to
++supersede your rules instead of being subservient to them.  (The global
++rules taken from the $HOME/.cvsignore file and from $CVSIGNORE are not
++repositioned by 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; and no special prefixes, list-clearing
-+tokens, or comment characters are honored).
++manpagesection(PER-DIRECTORY EXCLUDES AND DELETE)
 +
-+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.
++Without a delete option, per-directory excludes are only relevant on the
++sending side, so you can feel free to exclude the merge files themselves
++without affecting the transfer:
 +
- manpagesection(BATCH MODE)
++verb(
++  rsync -av --exclude='. -p .excl' --exclude=.excl host:src/dir /dest
++)
++
++However, if you want to do a delete on the receiving side AND you want some
++files to be excluded from being deleted, you'll need to be sure that the
++receiving side knows what files to exclude.  The easiest way is to include
++the per-directory merge files in the transfer and use --delete-after
++because this ensures that the receiving side gets all the same exclude
++rules as the sending side before it tries to delete anything:
++
++verb(
++  rsync -avE --delete-after host:src/dir /dest
++)
++
++However, if you the merge files are not a part of the transfer, you'll need
++to either use a global exclude rule (i.e. specified on the command line),
++or you'll need to maintain your own per-directory merge files on the
++receiving side.  An example of the first is this (assume that the remote
++.ctrl files exclude themselves):
++
++verb(
++  rsync -av --exclude='. -p .ctrl' --exclude-from=/my/extra.rules
++    --delete host:src/dir /dest
++)
++
++In the above example the extra.rules file can affect both sides of the
++transfer, but the rules are subservient to the rules merged from the .ctrl
++files because they were specified after the per-directory merge rule.
++
++In the final example, the remote side is excluding the .rsync-excludes
++files from the transfer, but we want to use our own .rsync-excludes files
++to control what gets deleted on the receiving side.  To do this we must
++specifically exclude the per-directory merge files (so that they don't get
++deleted) and then put rules into the local files to control what else
++should not get deleted.  Like this:
++
++verb(
++  rsync -avE --exclude=.rsync-excludes --delete host:src/dir /dest
+ )
  
- bf(Note:) Batch mode should be considered experimental in this version
---- testsuite/exclude.test     24 May 2004 00:16:07 -0000      1.8
-+++ testsuite/exclude.test     4 Jun 2004 07:07:35 -0000
+ manpagesection(BATCH MODE)
+--- orig/testsuite/exclude.test        2004-05-29 21:25:45
++++ testsuite/exclude.test     2004-08-08 06:35:15
 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
  makepath "$fromdir/foo/down/to/you"
  makepath "$fromdir/bar/down/to/foo/too"
@@ -815,7 +1240,7 @@ contained a slash.
 +EOF
 +cat >"$fromdir/bar/.excl" <<EOF
 +home-cvs-exclude
-+. .excl2
++. -p .excl2
 ++ to
 +EOF
  echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
@@ -839,7 +1264,7 @@ contained a slash.
  echo one-in-one-out >"$fromdir/mid/.cvsignore"
  echo cvsin >"$fromdir/mid/one-for-all"
 +cat >"$fromdir/mid/.excl" <<EOF
-+. .cvsignore
++. -p .cvsignore
 +EOF
  echo cvsin >"$fromdir/mid/for/one-in-one-out"
  echo expunged >"$fromdir/mid/for/foo/extra"
@@ -864,7 +1289,7 @@ contained a slash.
 +# Now, test if rsync excludes the same files, this time with a merge-exclude
 +# file.
 +
-+checkit "$RSYNC -avv --exclude='. .excl' --exclude-from=\"$excl\" --inherit=. \
++checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
 +    --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
 +
  # The script would have aborted on error, so getting here means we've won.