1 After applying this patch and running configure, you MUST run this
6 This patch adds the ability to merge rules into your excludes/includes
7 using either "+m FILE" (for merged includes) or "-m FILE" (for merged
8 excludes). It also lets you specify either "+p FILE" or "-p FILE" in
9 order to specify a per-directory merge file -- one that will be looked
10 for in every sub-directory that rsync visits, and the rules found in
11 that subdirectory's file will affect that dir and (if desired) its
16 rsync -av --exclude='-p .excl' from/ to
18 The above will look for a file named ".excl" in every directory of the
19 hierarchy that rsync visits, and it will exclude (by default) names
20 based on the rules found therein. If one of the .excl files contains
29 Then the file ".excl2" will also be read in from the current dir and all
30 its subdirs. The file ".excl3" would just be read in from the current
31 dir. The exclusion of "foobar" will only happen in that .excl file's
32 directory because the rule is anchored, so that's how you can make rules
33 local instead of inherited.
37 --- orig/clientserver.c 2005-01-01 21:11:00
38 +++ clientserver.c 2004-08-10 15:44:15
39 @@ -49,12 +49,14 @@ extern int no_detach;
40 extern int default_af_hint;
41 extern char *bind_address;
42 extern struct exclude_list_struct server_exclude_list;
43 -extern char *exclude_path_prefix;
44 extern char *config_file;
45 extern char *files_from;
49 +/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
50 +unsigned int module_dirlen = 0;
53 * Run a client connected to an rsyncd. The alternative to this
54 * function for remote-shell connections is do_cmd().
55 @@ -304,26 +306,28 @@ static int rsync_module(int f_in, int f_
56 /* TODO: Perhaps take a list of gids, and make them into the
57 * supplementary groups. */
59 - exclude_path_prefix = use_chroot? "" : lp_path(i);
60 - if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
61 - exclude_path_prefix = "";
64 + set_excludes_dir("/", 1);
66 + module_dirlen = strlen(lp_path(i));
67 + set_excludes_dir(lp_path(i), module_dirlen);
70 p = lp_include_from(i);
71 add_exclude_file(&server_exclude_list, p,
72 - XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
73 + XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
76 add_exclude(&server_exclude_list, p,
77 - XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
78 + XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
80 p = lp_exclude_from(i);
81 add_exclude_file(&server_exclude_list, p,
83 + XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
86 - add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
88 - exclude_path_prefix = NULL;
89 + add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
93 --- orig/exclude.c 2005-01-13 23:15:56
94 +++ exclude.c 2005-01-15 05:29:58
95 @@ -30,13 +30,69 @@ extern int verbose;
100 +extern int sanitize_paths;
102 extern char curr_dir[];
103 +extern unsigned int curr_dir_len;
104 +extern unsigned int module_dirlen;
106 struct exclude_list_struct exclude_list = { 0, 0, "" };
107 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
108 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
109 -char *exclude_path_prefix = NULL;
111 +struct mergelist_save_struct {
112 + struct exclude_list_struct *array;
116 +/* The dirbuf is set by push_local_excludes() to the current subdirectory
117 + * relative to curr_dir that is being processed. The path always has a
118 + * trailing slash appended, and the variable dirbuf_len contains the length
119 + * of this path prefix. The path is always absolute. */
120 +static char dirbuf[MAXPATHLEN+1];
121 +static unsigned int dirbuf_len = 0;
122 +static int dirbuf_depth;
124 +/* This is True when we're scanning parent dirs for per-dir merge-files. */
125 +static BOOL parent_dirscan = False;
127 +/* This array contains a list of all the currently active per-dir merge
128 + * files. This makes it easier to save the appropriate values when we
129 + * "push" down into each subdirectory. */
130 +static struct exclude_struct **mergelist_parents;
131 +static int mergelist_cnt = 0;
132 +static int mergelist_size = 0;
134 +/* Each exclude_list_struct describes a singly-linked list by keeping track
135 + * of both the head and tail pointers. The list is slightly unusual in that
136 + * a parent-dir's content can be appended to the end of the local list in a
137 + * special way: the last item in the local list has its "next" pointer set
138 + * to point to the inherited list, but the local list's tail pointer points
139 + * at the end of the local list. Thus, if the local list is empty, the head
140 + * will be pointing at the inherited content but the tail will be NULL. To
141 + * help you visualize this, here are the possible list arrangements:
143 + * Completely Empty Local Content Only
144 + * ================================== ====================================
145 + * head -> NULL head -> Local1 -> Local2 -> NULL
146 + * tail -> NULL tail -------------^
148 + * Inherited Content Only Both Local and Inherited Content
149 + * ================================== ====================================
150 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
151 + * tail -> NULL tail ---------^
153 + * This means that anyone wanting to traverse the whole list to use it just
154 + * needs to start at the head and use the "next" pointers until it goes
155 + * NULL. To add new local content, we insert the item after the tail item
156 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
157 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
158 + * because it is shared between the current list and our parent list(s).
159 + * The easiest way to handle this is to simply truncate the list after the
160 + * tail item and then free the local list from the head. When inheriting
161 + * the list for a new local dir, we just save off the exclude_list_struct
162 + * values (so we can pop back to them later) and set the tail to NULL.
165 /** Build an exclude structure given an exclude pattern. */
166 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
167 @@ -46,23 +102,50 @@ static void make_exclude(struct exclude_
172 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
173 + who_am_i(), (int)pat_len, pat, listp->debug_type,
174 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
175 + mflags & MATCHFLG_INCLUDE ? "in" : "ex");
178 + if (mflags & MATCHFLG_MERGE_FILE) {
180 + /* If the local merge file was already mentioned, don't
182 + for (i = 0; i < mergelist_cnt; i++) {
183 + struct exclude_struct *ex = mergelist_parents[i];
184 + if (strlen(ex->pattern) == pat_len
185 + && memcmp(ex->pattern, pat, pat_len) == 0)
188 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
189 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
190 + mflags |= MATCHFLG_CVSIGNORE;
191 + mflags &= ~MATCHFLG_INCLUDE;
193 + mflags &= ~MATCHFLG_CVSIGNORE;
196 ret = new(struct exclude_struct);
198 out_of_memory("make_exclude");
200 memset(ret, 0, sizeof ret[0]);
202 - if (exclude_path_prefix)
203 - mflags |= MATCHFLG_ABS_PATH;
204 - if (exclude_path_prefix && *pat == '/')
205 - ex_len = strlen(exclude_path_prefix);
207 + if (mflags & MATCHFLG_ABS_PATH) {
209 + mflags &= ~MATCHFLG_ABS_PATH;
212 + ex_len = dirbuf_len - module_dirlen - 1;
215 ret->pattern = new_array(char, ex_len + pat_len + 1);
217 out_of_memory("make_exclude");
219 - memcpy(ret->pattern, exclude_path_prefix, ex_len);
220 + memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
221 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
224 @@ -81,14 +164,40 @@ static void make_exclude(struct exclude_
225 mflags |= MATCHFLG_DIRECTORY;
228 - for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
230 + if (mflags & MATCHFLG_MERGE_FILE) {
231 + struct exclude_list_struct *lp
232 + = new_array(struct exclude_list_struct, 1);
234 + out_of_memory("make_exclude");
235 + lp->head = lp->tail = NULL;
236 + if ((cp = strrchr(ret->pattern, '/')) != NULL)
240 + if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
241 + out_of_memory("make_exclude");
242 + ret->u.mergelist = lp;
243 + if (mergelist_cnt == mergelist_size) {
244 + mergelist_size += 5;
245 + mergelist_parents = realloc_array(mergelist_parents,
246 + struct exclude_struct *,
248 + if (!mergelist_parents)
249 + out_of_memory("make_exclude");
251 + mergelist_parents[mergelist_cnt++] = ret;
253 + for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
254 + ret->u.slash_cnt++;
257 ret->match_flags = mflags;
260 + if (!listp->tail) {
261 + ret->next = listp->head;
262 listp->head = listp->tail = ret;
265 + ret->next = listp->tail->next;
266 listp->tail->next = ret;
269 @@ -96,22 +205,267 @@ static void make_exclude(struct exclude_
271 static void free_exclude(struct exclude_struct *ex)
273 + if (ex->match_flags & MATCHFLG_MERGE_FILE) {
274 + free(ex->u.mergelist->debug_type);
275 + free(ex->u.mergelist);
281 -void clear_exclude_list(struct exclude_list_struct *listp)
282 +static void clear_exclude_list(struct exclude_list_struct *listp)
284 - struct exclude_struct *ent, *next;
286 - for (ent = listp->head; ent; ent = next) {
290 + struct exclude_struct *ent, *next;
291 + /* Truncate any inherited items from the local list. */
292 + listp->tail->next = NULL;
293 + /* Now free everything that is left. */
294 + for (ent = listp->head; ent; ent = next) {
300 listp->head = listp->tail = NULL;
303 +/* This returns an expanded (absolute) filename for the merge-file name if
304 + * the name has any slashes in it OR if the parent_dirscan var is True;
305 + * otherwise it returns the original merge_file name. If the len_ptr value
306 + * is non-NULL the merge_file name is limited by the referenced length
307 + * value and will be updated with the length of the resulting name. We
308 + * always return a name that is null terminated, even if the merge_file
310 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
311 + unsigned int prefix_skip)
313 + static char buf[MAXPATHLEN];
314 + char *fn, tmpbuf[MAXPATHLEN];
315 + unsigned int fn_len;
317 + if (!parent_dirscan && *merge_file != '/') {
318 + /* Return the name unchanged it doesn't have any slashes. */
320 + const char *p = merge_file + *len_ptr;
321 + while (--p > merge_file && *p != '/') {}
322 + if (p == merge_file) {
323 + strlcpy(buf, merge_file, *len_ptr + 1);
326 + } else if (strchr(merge_file, '/') == NULL)
327 + return (char *)merge_file;
330 + fn = *merge_file == '/' ? buf : tmpbuf;
331 + if (sanitize_paths) {
332 + const char *r = prefix_skip ? "/" : NULL;
333 + /* null-terminate the name if it isn't already */
334 + if (len_ptr && merge_file[*len_ptr]) {
335 + char *to = fn == buf ? tmpbuf : buf;
336 + strlcpy(to, merge_file, *len_ptr + 1);
339 + if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
340 + rprintf(FERROR, "merge-file name overflows: %s\n",
345 + strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
346 + clean_fname(fn, 1);
349 + fn_len = strlen(fn);
353 + if (dirbuf_len + fn_len >= MAXPATHLEN) {
354 + rprintf(FERROR, "merge-file name overflows: %s\n", fn);
357 + memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
358 + memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
359 + fn_len = clean_fname(buf, 1);
367 +/* Sets the dirbuf and dirbuf_len values. */
368 +void set_excludes_dir(const char *dir, unsigned int dirlen)
372 + memcpy(dirbuf, curr_dir, curr_dir_len);
373 + dirbuf[curr_dir_len] = '/';
374 + len = curr_dir_len + 1;
375 + if (len + dirlen >= MAXPATHLEN)
379 + memcpy(dirbuf + len, dir, dirlen);
380 + dirbuf[dirlen + len] = '\0';
381 + dirbuf_len = clean_fname(dirbuf, 1);
382 + if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
383 + && dirbuf[dirbuf_len-2] == '/')
385 + dirbuf[dirbuf_len++] = '/';
386 + dirbuf[dirbuf_len] = '\0';
387 + if (sanitize_paths)
388 + dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
391 +/* This routine takes a per-dir merge-file entry and finishes its setup.
392 + * If the name has a path portion then we check to see if it refers to a
393 + * parent directory of the first transfer dir. If it does, we scan all the
394 + * dirs from that point through the parent dir of the transfer dir looking
395 + * for the per-dir merge-file in each one. */
396 +static BOOL setup_merge_file(struct exclude_struct *ex,
397 + struct exclude_list_struct *lp, int flags)
399 + char buf[MAXPATHLEN];
400 + char *x, *y, *pat = ex->pattern;
403 + if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
406 + y = strrchr(x, '/');
408 + ex->pattern = strdup(y+1);
412 + strlcpy(buf, x, MAXPATHLEN);
414 + pathjoin(buf, MAXPATHLEN, dirbuf, x);
416 + len = clean_fname(buf, 1);
417 + if (len != 1 && len < MAXPATHLEN-1) {
421 + /* This ensures that the specified dir is a parent of the transfer. */
422 + for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
424 + y += strlen(y); /* nope -- skip the scan */
426 + parent_dirscan = True;
428 + char save[MAXPATHLEN];
429 + strlcpy(save, y, MAXPATHLEN);
431 + dirbuf_len = y - dirbuf;
432 + strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
433 + add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
434 + if (ex->match_flags & MATCHFLG_CVSIGNORE)
435 + lp->head = NULL; /* CVS doesn't inherit rules. */
437 + strlcpy(y, save, MAXPATHLEN);
438 + while ((*x++ = *y++) != '/') {}
440 + parent_dirscan = False;
445 +/* Each time rsync changes to a new directory it call this function to
446 + * handle all the per-dir merge-files. The "dir" value is the current path
447 + * relative to curr_dir (which might not be null-terminated). We copy it
448 + * into dirbuf so that we can easily append a file name on the end. */
449 +void *push_local_excludes(const char *dir, unsigned int dirlen)
451 + struct mergelist_save_struct *push;
452 + struct exclude_list_struct *ap;
455 + set_excludes_dir(dir, dirlen);
457 + if (!(push = new_array(struct mergelist_save_struct, 1)))
458 + out_of_memory("push_local_excludes");
460 + push->count = mergelist_cnt;
461 + push->array = new_array(struct exclude_list_struct, mergelist_cnt);
463 + out_of_memory("push_local_excludes");
465 + for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
466 + memcpy(ap++, mergelist_parents[i]->u.mergelist,
467 + sizeof (struct exclude_list_struct));
470 + /* Note: add_exclude_file() might increase mergelist_cnt, so keep
471 + * this loop separate from the above loop. */
472 + for (i = 0; i < mergelist_cnt; i++) {
473 + struct exclude_struct *ex = mergelist_parents[i];
474 + struct exclude_list_struct *lp = ex->u.mergelist;
478 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
479 + who_am_i(), lp->debug_type);
482 + if (ex->match_flags & MATCHFLG_CVSIGNORE) {
483 + lp->head = NULL; /* CVS doesn't inherit rules. */
484 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
486 + flags = ex->match_flags & MATCHFLG_INCLUDE
487 + ? XFLG_DEF_INCLUDE : 0;
489 + lp->tail = NULL; /* Switch any local rules to inherited. */
491 + if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
492 + ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
493 + if (setup_merge_file(ex, lp, flags))
494 + set_excludes_dir(dir, dirlen);
497 + if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
498 + MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
499 + add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
501 + io_error |= IOERR_GENERAL;
503 + "cannot add local excludes in long-named directory %s\n",
504 + full_fname(dirbuf));
506 + dirbuf[dirbuf_len] = '\0';
509 + return (void*)push;
512 +void pop_local_excludes(void *mem)
514 + struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
515 + struct exclude_list_struct *ap;
518 + for (i = mergelist_cnt; i-- > 0; ) {
519 + struct exclude_struct *ex = mergelist_parents[i];
520 + struct exclude_list_struct *lp = ex->u.mergelist;
523 + rprintf(FINFO, "[%s] popping %sexclude list\n",
524 + who_am_i(), lp->debug_type);
527 + clear_exclude_list(lp);
530 + mergelist_cnt = pop->count;
531 + for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
532 + memcpy(mergelist_parents[i]->u.mergelist, ap++,
533 + sizeof (struct exclude_list_struct));
540 static int check_one_exclude(char *name, struct exclude_struct *ex,
543 @@ -125,13 +479,14 @@ static int check_one_exclude(char *name,
544 /* If the pattern does not have any slashes AND it does not have
545 * a "**" (which could match a slash), then we just match the
546 * name portion of the path. */
547 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
548 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
549 if ((p = strrchr(name,'/')) != NULL)
552 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
554 - pathjoin(full_name, sizeof full_name, curr_dir + 1, name);
555 + && curr_dir_len > module_dirlen + 1) {
556 + pathjoin(full_name, sizeof full_name,
557 + curr_dir + module_dirlen + 1, name);
561 @@ -148,9 +503,9 @@ static int check_one_exclude(char *name,
562 if (ex->match_flags & MATCHFLG_WILD) {
563 /* A non-anchored match with an infix slash and no "**"
564 * needs to match the last slash_cnt+1 name elements. */
565 - if (!match_start && ex->slash_cnt
566 + if (!match_start && ex->u.slash_cnt
567 && !(ex->match_flags & MATCHFLG_WILD2)) {
568 - int cnt = ex->slash_cnt + 1;
569 + int cnt = ex->u.slash_cnt + 1;
570 for (p = name + strlen(name) - 1; p >= name; p--) {
571 if (*p == '/' && !--cnt)
573 @@ -221,6 +576,13 @@ int check_exclude(struct exclude_list_st
574 struct exclude_struct *ent;
576 for (ent = listp->head; ent; ent = ent->next) {
577 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
578 + int rc = check_exclude(ent->u.mergelist, name,
584 if (check_one_exclude(name, ent, name_is_dir)) {
585 report_exclude_result(name, ent, name_is_dir,
587 @@ -254,12 +616,28 @@ static const char *get_exclude_tok(const
591 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
592 - if (!(xflags & XFLG_WORDS_ONLY)
593 - && (*s == '-' || *s == '+') && s[1] == ' ') {
594 + /* Check for a leading '+' or '-'. */
595 + if (!(xflags & XFLG_WORDS_ONLY) && (*s == '-' || *s == '+')) {
597 mflags |= MATCHFLG_INCLUDE;
599 + while (*++s != ' ') {
602 + mflags |= MATCHFLG_MERGE_FILE
603 + | MATCHFLG_PERDIR_MERGE
604 + | MATCHFLG_FINISH_SETUP;
607 + mflags |= MATCHFLG_MERGE_FILE;
611 + "invalid include/exclude option after %c: %c\n",
613 + exit_cleanup(RERR_SYNTAX);
617 } else if (xflags & XFLG_DEF_INCLUDE)
618 mflags |= MATCHFLG_INCLUDE;
619 if (xflags & XFLG_DIRECTORY)
620 @@ -276,6 +654,8 @@ static const char *get_exclude_tok(const
622 if (*p == '!' && len == 1)
623 mflags |= MATCHFLG_CLEAR_LIST;
624 + if (xflags & XFLG_ABS_PATH)
625 + mflags |= MATCHFLG_ABS_PATH;
629 @@ -287,7 +667,7 @@ void add_exclude(struct exclude_list_str
632 unsigned int pat_len, mflags;
634 + const char *cp, *p;
638 @@ -295,9 +675,15 @@ void add_exclude(struct exclude_list_str
642 + /* Remember that the returned string is NOT '\0' terminated! */
643 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
646 + if (pat_len >= MAXPATHLEN) {
647 + rprintf(FERROR, "discarding over-long exclude: %s\n",
652 if (mflags & MATCHFLG_CLEAR_LIST) {
654 @@ -309,13 +695,24 @@ void add_exclude(struct exclude_list_str
658 - make_exclude(listp, cp, pat_len, mflags);
661 - rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
662 - who_am_i(), (int)pat_len, cp, listp->debug_type,
663 - mflags & MATCHFLG_INCLUDE ? "in" : "ex");
664 + if (mflags & MATCHFLG_MERGE_FILE) {
665 + unsigned int len = pat_len;
666 + if (mflags & MATCHFLG_PERDIR_MERGE) {
667 + if (parent_dirscan) {
668 + if (!(p = parse_merge_name(cp, &len, module_dirlen)))
670 + make_exclude(listp, p, len, mflags);
674 + if (!(p = parse_merge_name(cp, &len, 0)))
676 + add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
681 + make_exclude(listp, cp, pat_len, mflags);
685 @@ -324,7 +721,7 @@ void add_exclude_file(struct exclude_lis
689 - char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
690 + char line[MAXPATHLEN+4]; /* Room for prefix chars and trailing slash. */
691 char *eob = line + sizeof line - 1;
692 int word_split = xflags & XFLG_WORD_SPLIT;
694 @@ -345,6 +742,12 @@ void add_exclude_file(struct exclude_lis
698 + dirbuf[dirbuf_len] = '\0';
701 + rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
702 + who_am_i(), fname, xflags);
707 @@ -402,7 +805,20 @@ void send_exclude_list(int f)
711 - if (ent->match_flags & MATCHFLG_INCLUDE) {
712 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
713 + char buf[32], *op = buf;
714 + if (ent->match_flags & MATCHFLG_INCLUDE)
718 + if (ent->match_flags & MATCHFLG_PERDIR_MERGE)
723 + write_int(f, l + (op - buf));
724 + write_buf(f, buf, op - buf);
725 + } else if (ent->match_flags & MATCHFLG_INCLUDE) {
727 write_buf(f, "+ ", 2);
728 } else if (*p == '-' || *p == '+') {
729 @@ -419,7 +835,7 @@ void send_exclude_list(int f)
731 void recv_exclude_list(int f)
733 - char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
734 + char line[MAXPATHLEN+4]; /* Room for prefix and trailing slash. */
737 while ((l = read_int(f)) != 0) {
738 @@ -446,6 +862,7 @@ void add_cvs_excludes(void)
739 char fname[MAXPATHLEN];
742 + add_exclude(&exclude_list, "-p .cvsignore", 0);
743 add_exclude(&exclude_list, default_cvsignore,
744 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
746 --- orig/flist.c 2005-01-01 21:11:00
747 +++ flist.c 2004-08-12 18:59:28
748 @@ -40,10 +40,9 @@ extern int module_id;
749 extern int ignore_errors;
750 extern int numeric_ids;
752 -extern int cvs_exclude;
755 extern char curr_dir[MAXPATHLEN];
756 +extern unsigned int curr_dir_len;
757 extern char *files_from;
758 extern int filesfrom_fd;
760 @@ -67,7 +66,6 @@ extern int list_only;
762 extern struct exclude_list_struct exclude_list;
763 extern struct exclude_list_struct server_exclude_list;
764 -extern struct exclude_list_struct local_exclude_list;
768 @@ -223,8 +221,6 @@ int link_stat(const char *path, STRUCT_S
770 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
774 #if 0 /* This currently never happens, so avoid a useless compare. */
775 if (exclude_level == NO_EXCLUDES)
777 @@ -246,10 +242,7 @@ static int check_exclude_file(char *fnam
778 if (exclude_level != ALL_EXCLUDES)
780 if (exclude_list.head
781 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
783 - if (local_exclude_list.head
784 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
785 + && check_exclude(&exclude_list, fname, is_dir) < 0)
789 @@ -983,15 +976,7 @@ void send_file_name(int f, struct file_l
791 if (recursive && S_ISDIR(file->mode)
792 && !(file->flags & FLAG_MOUNT_POINT)) {
793 - struct exclude_list_struct last_list = local_exclude_list;
794 - local_exclude_list.head = local_exclude_list.tail = NULL;
795 send_directory(f, flist, f_name_to(file, fbuf));
797 - rprintf(FINFO, "[%s] popping %sexclude list\n",
798 - who_am_i(), local_exclude_list.debug_type);
800 - clear_exclude_list(&local_exclude_list);
801 - local_exclude_list = last_list;
805 @@ -1002,6 +987,7 @@ static void send_directory(int f, struct
807 char fname[MAXPATHLEN];
809 + void *save_excludes;
813 @@ -1025,18 +1011,7 @@ static void send_directory(int f, struct
818 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
819 - < MAXPATHLEN - offset) {
820 - add_exclude_file(&local_exclude_list, fname,
821 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
823 - io_error |= IOERR_GENERAL;
825 - "cannot cvs-exclude in long-named directory %s\n",
826 - full_fname(fname));
829 + save_excludes = push_local_excludes(fname, offset);
831 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
832 char *dname = d_name(di);
833 @@ -1057,6 +1032,8 @@ static void send_directory(int f, struct
834 rsyserr(FERROR, errno, "readdir(%s)", dir);
837 + pop_local_excludes(save_excludes);
842 @@ -1076,6 +1053,7 @@ struct file_list *send_file_list(int f,
843 char *p, *dir, olddir[sizeof curr_dir];
844 char lastpath[MAXPATHLEN] = "";
845 struct file_list *flist;
846 + BOOL need_first_push = True;
850 @@ -1096,6 +1074,10 @@ struct file_list *send_file_list(int f,
851 exit_cleanup(RERR_FILESELECT);
854 + if (curr_dir_len < MAXPATHLEN - 1) {
855 + push_local_excludes(curr_dir, curr_dir_len);
856 + need_first_push = False;
861 @@ -1126,6 +1108,15 @@ struct file_list *send_file_list(int f,
865 + if (need_first_push) {
866 + if ((p = strrchr(fname, '/')) != NULL) {
867 + if (*++p && strcmp(p, ".") != 0)
868 + push_local_excludes(fname, p - fname);
869 + } else if (strcmp(fname, ".") != 0)
870 + push_local_excludes(fname, 0);
871 + need_first_push = False;
874 if (link_stat(fname, &st, keep_dirlinks) != 0) {
876 io_error |= IOERR_GENERAL;
877 --- orig/options.c 2005-01-15 04:40:15
878 +++ options.c 2005-01-13 23:52:00
879 @@ -296,6 +296,7 @@ void usage(enum logcode F)
880 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
881 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
882 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
883 + rprintf(F," -E same as --exclude='-p /.rsync-excludes'\n");
884 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
885 rprintf(F," --version print version number\n");
886 rprintf(F," --port=PORT specify double-colon alternate port number\n");
887 @@ -393,6 +394,7 @@ static struct poptOption long_options[]
888 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
889 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
890 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
891 + {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
892 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
893 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
894 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
895 @@ -668,6 +670,11 @@ int parse_arguments(int *argc, const cha
900 + add_exclude(&exclude_list,
901 + "-p /.rsync-excludes", 0);
907 --- orig/rsync.h 2005-01-10 00:21:12
908 +++ rsync.h 2004-09-22 08:48:53
910 #define XFLG_WORDS_ONLY (1<<2)
911 #define XFLG_WORD_SPLIT (1<<3)
912 #define XFLG_DIRECTORY (1<<4)
913 +#define XFLG_ABS_PATH (1<<5)
915 #define PERMS_REPORT (1<<0)
916 #define PERMS_SKIP_MTIME (1<<1)
917 @@ -510,11 +511,18 @@ struct map_struct {
918 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
919 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
920 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
921 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
922 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
923 +#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
924 +#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
925 struct exclude_struct {
926 struct exclude_struct *next;
928 unsigned int match_flags;
932 + struct exclude_list_struct *mergelist;
936 struct exclude_list_struct {
937 --- orig/rsync.yo 2005-01-15 04:36:32
938 +++ rsync.yo 2005-01-14 00:10:38
939 @@ -366,6 +366,7 @@ verb(
940 --include=PATTERN don't exclude files matching PATTERN
941 --include-from=FILE don't exclude patterns listed in FILE
942 --files-from=FILE read FILE for list of source-file names
943 + -E same as --exclude='-p /.rsync-excludes'
944 -0 --from0 all file lists are delimited by nulls
945 --version print version number
946 --port=PORT specify double-colon alternate port number
947 @@ -1114,24 +1115,32 @@ The exclude and include patterns specifi
948 selection of which files to transfer and which files to skip.
950 Rsync builds an ordered list of include/exclude options as specified on
951 -the command line. Rsync checks each file and directory
952 -name against each exclude/include pattern in turn. The first matching
954 +It can also be told to check for include/exclude options in each
955 +directory that rsync visits during the transfer (see the section on
956 +MERGED EXCLUDE FILES for the details on these per-directory exclude
959 +As the list of files/directories to transfer is built, rsync checks each
960 +name against every exclude/include pattern in turn. The first matching
961 pattern is acted on. If it is an exclude pattern, then that file is
962 skipped. If it is an include pattern then that filename is not
963 skipped. If no matching include/exclude pattern is found then the
964 filename is not skipped.
966 -The filenames matched against the exclude/include patterns are relative
967 -to the "root of the transfer". If you think of the transfer as a
968 -subtree of names that are being sent from sender to receiver, the root
969 -is where the tree starts to be duplicated in the destination directory.
970 -This root governs where patterns that start with a / match (see below).
971 +The global include/exclude rules are anchored at the "root of the
972 +transfer" (as opposed to per-directory rules, which are anchored at
973 +the merge-file's directory). If you think of the transfer as a
974 +subtree of names that are being sent from sender to receiver, the
975 +transfer-root is where the tree starts to be duplicated in the
976 +destination directory. This root governs where patterns that start
977 +with a / match (as described in the list on pattern forms below).
979 Because the matching is relative to the transfer-root, changing the
980 trailing slash on a source path or changing your use of the --relative
981 option affects the path you need to use in your matching (in addition to
982 changing how much of the file tree is duplicated on the destination
983 -system). The following examples demonstrate this.
984 +host). The following examples demonstrate this.
986 Let's say that we want to match two source files, one with an absolute
987 path of "/home/me/foo/bar", and one with a path of "/home/you/bar/baz".
988 @@ -1178,23 +1187,27 @@ because rsync did not descend through th
991 Note also that the --include and --exclude options take one pattern
992 -each. To add multiple patterns use the --include-from and
993 ---exclude-from options or multiple --include and --exclude options.
994 +each. To add multiple patterns use the --include-from and --exclude-from
995 +options or multiple --include and --exclude options.
997 -The patterns can take several forms. The rules are:
998 +The include/exclude patterns can take several forms. The rules are:
1002 - it() if the pattern starts with a / then it is matched against the
1003 - start of the filename, otherwise it is matched against the end of
1005 - This is the equivalent of a leading ^ in regular expressions.
1006 - Thus "/foo" would match a file called "foo" at the transfer-root
1007 - (see above for how this is different from the filesystem-root).
1008 - On the other hand, "foo" would match any file called "foo"
1009 + it() if the pattern starts with a / then it is anchored to a
1010 + particular spot in the hierarchy of files, otherwise it is matched
1011 + against the end of the pathname. This is similar to a leading ^ in
1012 + regular expressions.
1013 + Thus "/foo" would match a file called "foo" at either the "root of the
1014 + transfer" (for a global rule) or in the merge-file's directory (for a
1015 + per-directory rule).
1016 + An unqualified "foo" would match any file or directory named "foo"
1017 anywhere in the tree because the algorithm is applied recursively from
1019 top down; it behaves as if each path component gets a turn at being the
1020 - end of the file name.
1021 + end of the file name. Even the unanchored "sub/foo" would match at
1022 + any point in the hierarchy where a "foo" was found within a directory
1025 it() if the pattern ends with a / then it will only match a
1026 directory, not a file, link, or device.
1027 @@ -1207,22 +1220,44 @@ itemize(
1028 single asterisk pattern "*" will stop at slashes.
1030 it() if the pattern contains a / (not counting a trailing /) or a "**"
1031 - then it is matched against the full filename, including any leading
1032 - directory. If the pattern doesn't contain a / or a "**", then it is
1033 + then it is matched against the full pathname, including any leading
1034 + directories. If the pattern doesn't contain a / or a "**", then it is
1035 matched only against the final component of the filename. Again,
1036 remember that the algorithm is applied recursively so "full filename" can
1037 actually be any portion of a path below the starting directory.
1039 - it() if the pattern starts with "+ " (a plus followed by a space)
1040 - then it is always considered an include pattern, even if specified as
1041 - part of an exclude option. The prefix is discarded before matching.
1043 - it() if the pattern starts with "- " (a minus followed by a space)
1044 - then it is always considered an exclude pattern, even if specified as
1045 - part of an include option. The prefix is discarded before matching.
1046 + it() if the pattern starts with "+" (a plus), the actual pattern begins
1047 + after the first space, and is always considered to be an include pattern,
1048 + even if specified as part of an exclude file/option. Option letters may
1049 + follow the "+" prior to the separating space (see below).
1051 + it() if the pattern starts with "-" (a minus), the actual pattern begins
1052 + after the first space, and is always considered to be an exclude pattern,
1053 + even if specified as part of an include file/option. Option letters may
1054 + follow the "-" prior to the separating space (see below).
1056 - it() if the pattern is a single exclamation mark ! then the current
1057 + it() if the pattern is a "!" (single exclamation mark) then the current
1058 include/exclude list is reset, removing all previously defined patterns.
1059 + The "current" list is either the global list of rules (which are
1060 + specified via options) or a set of per-directory rules (which are
1061 + inherited in their own sub-list, so a subdirectory can use this to
1062 + clear out the parent's rules).
1066 +For a line that starts with a "+" (plus) or a "-" (minus), the following
1067 +option letters may be suffixed:
1071 + it() An "m" means that the string following the space is to be taken to
1072 + be a merge-file that is read in to supplement the current rules. See the
1073 + section on MERGED EXCLUDE FILES for more information.
1075 + it() A "p" means that the string following the space is to be taken to be
1076 + a per-directory merge-file that is read in to supplement the current
1077 + rules. See the section on MERGED EXCLUDE FILES for more information.
1081 The +/- rules are most useful in a list that was read from a file, allowing
1082 @@ -1269,8 +1304,157 @@ itemize(
1083 it() --include "*/" --include "*.c" --exclude "*" would include all
1084 directories and C source files
1085 it() --include "foo/" --include "foo/bar.c" --exclude "*" would include
1086 - only foo/bar.c (the foo/ directory must be explicitly included or
1087 - it would be excluded by the "*")
1088 + only the foo directory and foo/bar.c (the foo directory must be
1089 + explicitly included or it would be excluded by the "*")
1092 +manpagesection(MERGED EXCLUDE FILES)
1094 +You can merge whole files into an exclude file by specifying either the
1095 +"m" or "p" option following the initial "+" or "-", and putting a filename
1096 +in place of the pattern. There are two kinds of merged exclude files --
1097 +single-instance ("m") and per-directory ("p"). For example:
1100 + -m /etc/rsync/default.excludes
1101 + +p .per-dir-includes
1104 +For a per-directory merge file, rsync will scan
1105 +every directory that it traverses for the named file, merging its contents
1106 +when the file exists. These exclude files must exist on the sending side
1107 +because it is the sending side that is being scanned for available files
1108 +to send. The files may also need to be transferred to the receiving side
1109 +if you want them to affect what files don't get deleted (see PER-DIRECTORY
1110 +EXCLUDES AND DELETE below).
1112 +Per-directory rules are inherited in all subdirectories of the directory
1113 +where the merge-file was found. Each subdirectory's rules are prefixed
1114 +to the inherited rules from the parent directories, which gives the
1115 +newest rules a higher priority than the inherited rules. The entire set
1116 +of per-dir rules is grouped together in the spot where the merge-file was
1117 +specified, so it is possible to override per-dir rules via a rule that
1118 +got specified earlier in the list of global rules.
1120 +If you don't want a per-dir rule to be inherited, anchor it with a leading
1121 +slash. Anchored rules in a per-directory merge-file are relative to the
1122 +merge-file's directory, so a rule "/foo" would only exclude the file "foo"
1123 +in the directory where the per-dir exclude file was found.
1125 +Here's an example exclude file which you'd specify via the normal
1126 +--exclude-from=FILE option:
1129 + -m /home/user/.global_excludes
1136 +This will merge the contents of the /home/user/.global_excludes file at the
1137 +start of the list and also turns the ".incl" filename into a per-directory
1138 +include file. All rules read in prior to the start of the directory scan
1139 +follow the global anchoring rules (i.e. a leading slash matches at the root
1142 +If a per-directory merge-file is specified with a path that is a parent
1143 +directory of the first transfer directory, rsync will scan all the parent
1144 +dirs from that starting point to the transfer directory for the indicated
1145 +per-directory file. For instance, the -E option is an abbreviation for
1149 + --exclude='-p /.rsync-excludes'
1152 +That exclude tells rsync to scan for the file .rsync-excludes in all
1153 +directories from the root down through the source of the transfer. (For
1154 +an rsync daemon, the "root dir" is always the module's "path" setting.)
1156 +Some examples of this pre-scanning for per-directory files:
1159 + rsync -avE /src/path/ /dest/dir
1160 + rsync -av --exclude='-p ../../.rsync-excludes' /src/path/ /dest/dir
1161 + rsync -av --exclude='-p .rsync-excludes' /src/path/ /dest/dir
1164 +The first two commands above will look for ".rsync-excludes" in "/" and
1165 +"/src" before the normal scan begins looking for the file in "/src/path"
1166 +and its subdirectories. The last command avoids the parent-dir scan
1167 +and only looks for the ".rsync-excludes" files in each directory that is
1168 +a part of the transfer.
1170 +Finally, note that the parsing of any merge-file named ".cvsignore" is
1171 +always done in a CVS-compatible manner, even if -C wasn't specified. This
1172 +means that its rules are always excludes (even if an include option
1173 +specified the file), patterns are split on whitespace, the rules are never
1174 +inherited, and no special characters are honored except for "!" (e.g. no
1175 +comments, and no +/- prefixes).
1177 +Additionally, you can affect where the --cvs-exclude (-C) option's
1178 +inclusion of the per-directory .cvsignore file gets placed into your rules
1179 +by adding your own explicit per-directory merge rule for ".cvsignore".
1180 +Without this, rsync would add this rule at the end of all your other
1181 +rules (giving it a lower priority than your command-line rules). For
1185 + rsync -avC --exclude='-p .cvsignore' --exclude-from=foo a/ b
1188 +The above will merge all the per-directory .cvsignore rules at the start of
1189 +your list rather than at the end. This allows their dir-specific rules to
1190 +supersede your rules instead of being subservient to them. (The global
1191 +rules taken from the $HOME/.cvsignore file and from $CVSIGNORE are not
1192 +repositioned by this.)
1194 +manpagesection(PER-DIRECTORY EXCLUDES AND DELETE)
1196 +Without a delete option, per-directory excludes are only relevant on the
1197 +sending side, so you can feel free to exclude the merge files themselves
1198 +without affecting the transfer:
1201 + rsync -av --exclude='-p .excl' --exclude=.excl host:src/dir /dest
1204 +However, if you want to do a delete on the receiving side AND you want some
1205 +files to be excluded from being deleted, you'll need to be sure that the
1206 +receiving side knows what files to exclude. The easiest way is to include
1207 +the per-directory merge files in the transfer and use --delete-after
1208 +because this ensures that the receiving side gets all the same exclude
1209 +rules as the sending side before it tries to delete anything:
1212 + rsync -avE --delete-after host:src/dir /dest
1215 +However, if the merge files are not a part of the transfer, you'll need
1216 +to either use a global exclude rule (i.e. specified on the command line),
1217 +or you'll need to maintain your own per-directory merge files on the
1218 +receiving side. An example of the first is this (assume that the remote
1219 +.ctrl files exclude themselves):
1222 + rsync -av --exclude='-p .ctrl' --exclude-from=/my/extra.rules
1223 + --delete host:src/dir /dest
1226 +In the above example the extra.rules file can affect both sides of the
1227 +transfer, but the rules are subservient to the rules merged from the .ctrl
1228 +files because they were specified after the per-directory merge rule.
1230 +In one final example, the remote side is excluding the .rsync-excludes
1231 +files from the transfer, but we want to use our own .rsync-excludes files
1232 +to control what gets deleted on the receiving side. To do this we must
1233 +specifically exclude the per-directory merge files (so that they don't get
1234 +deleted) and then put rules into the local files to control what else
1235 +should not get deleted. Like this:
1238 + rsync -avE --exclude=.rsync-excludes --delete host:src/dir /dest
1241 manpagesection(BATCH MODE)
1242 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
1243 +++ testsuite/exclude.test 2005-01-14 00:14:33
1244 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
1245 makepath "$fromdir/foo/down/to/you"
1246 makepath "$fromdir/bar/down/to/foo/too"
1247 makepath "$fromdir/mid/for/foo/and/that/is/who"
1248 +cat >"$fromdir/.excl" <<EOF
1254 echo kept >"$fromdir/foo/file1"
1255 echo removed >"$fromdir/foo/file2"
1256 echo cvsout >"$fromdir/foo/file2.old"
1257 +cat >"$fromdir/foo/.excl" <<EOF
1261 +cat >"$fromdir/bar/.excl" <<EOF
1266 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1267 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
1270 echo keeper >"$fromdir/bar/down/to/foo/file1"
1271 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1272 echo gone >"$fromdir/bar/down/to/foo/file3"
1273 echo lost >"$fromdir/bar/down/to/foo/file4"
1274 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1275 echo smashed >"$fromdir/bar/down/to/foo/to"
1276 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1279 +# This one should be ineffectual
1280 +cat >"$fromdir/mid/.excl2" <<EOF
1283 echo cvsout >"$fromdir/mid/one-in-one-out"
1284 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1285 echo cvsin >"$fromdir/mid/one-for-all"
1286 +cat >"$fromdir/mid/.excl" <<EOF
1289 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1290 echo expunged >"$fromdir/mid/for/foo/extra"
1291 echo retained >"$fromdir/mid/for/foo/keep"
1292 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1293 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1294 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1296 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1299 +rm "$chkdir"/foo/file1
1300 +rm "$chkdir"/bar/.excl
1301 +rm "$chkdir"/bar/down/to/.excl2
1302 +rm "$chkdir"/bar/down/to/foo/.excl2
1303 +rm "$chkdir"/mid/.excl
1304 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1305 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1307 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1309 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1312 +checkit "$RSYNC -avv --exclude='-p .excl' --exclude-from=\"$excl\" \
1313 + --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1315 # The script would have aborted on error, so getting here means we've won.