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 a ". FILE" idiom. If you specify a name with a preceding -p
8 option, that filename will be looked for in every subdirectory that
9 rsync visits, and the rules found in that subdirectory's file will
10 affect that dir and its subdirs.
14 rsync -av --exclude='. -p .excl' from/ to
16 The above will look for a file named ".excl" in every directory of the
17 hierarchy that rsync visits, and it will exclude (by default) names
18 based on the rules found therein. If one of the .excl files contains
27 Then the file ".excl2" will also be read in from the current dir and all
28 its subdirs (due to the -p option). The file ".excl3" would just be
29 read in from the current dir. The exclusion of "foobar" will only
30 happen in that .excl file's directory because the rule is anchored (so
31 that's how you can make rules local instead of inherited).
35 --- orig/clientserver.c 2004-08-02 02:29:16
36 +++ clientserver.c 2004-08-10 15:44:15
37 @@ -48,12 +48,14 @@ extern int no_detach;
38 extern int default_af_hint;
39 extern char *bind_address;
40 extern struct exclude_list_struct server_exclude_list;
41 -extern char *exclude_path_prefix;
42 extern char *config_file;
43 extern char *files_from;
47 +/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
48 +unsigned int module_dirlen = 0;
51 * Run a client connected to an rsyncd. The alternative to this
52 * function for remote-shell connections is do_cmd().
53 @@ -300,26 +302,28 @@ static int rsync_module(int f_in, int f_
54 /* TODO: Perhaps take a list of gids, and make them into the
55 * supplementary groups. */
57 - exclude_path_prefix = use_chroot? "" : lp_path(i);
58 - if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
59 - exclude_path_prefix = "";
62 + set_excludes_dir("/", 1);
64 + module_dirlen = strlen(lp_path(i));
65 + set_excludes_dir(lp_path(i), module_dirlen);
68 p = lp_include_from(i);
69 add_exclude_file(&server_exclude_list, p,
70 - XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
71 + XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
74 add_exclude(&server_exclude_list, p,
75 - XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
76 + XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
78 p = lp_exclude_from(i);
79 add_exclude_file(&server_exclude_list, p,
81 + XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
84 - add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
86 - exclude_path_prefix = NULL;
87 + add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
91 --- orig/exclude.c 2004-10-06 00:12:16
92 +++ exclude.c 2004-08-13 07:40:08
93 @@ -30,13 +30,69 @@ extern int verbose;
98 +extern int sanitize_paths;
100 extern char curr_dir[];
101 +extern unsigned int curr_dir_len;
102 +extern unsigned int module_dirlen;
104 struct exclude_list_struct exclude_list = { 0, 0, "" };
105 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
106 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
107 -char *exclude_path_prefix = NULL;
109 +struct mergelist_save_struct {
110 + struct exclude_list_struct *array;
114 +/* The dirbuf is set by push_local_excludes() to the current subdirectory
115 + * relative to curr_dir that is being processed. The path always has a
116 + * trailing slash appended, and the variable dirbuf_len contains the length
117 + * of this path prefix. The path is always absolute. */
118 +static char dirbuf[MAXPATHLEN+1];
119 +static unsigned int dirbuf_len = 0;
120 +static int dirbuf_depth;
122 +/* This is True when we're scanning parent dirs for per-dir merge-files. */
123 +static BOOL parent_dirscan = False;
125 +/* This array contains a list of all the currently active per-dir merge
126 + * files. This makes it easier to save the appropriate values when we
127 + * "push" down into each subdirectory. */
128 +static struct exclude_struct **mergelist_parents;
129 +static int mergelist_cnt = 0;
130 +static int mergelist_size = 0;
132 +/* Each exclude_list_struct describes a singly-linked list by keeping track
133 + * of both the head and tail pointers. The list is slightly unusual in that
134 + * a parent-dir's content can be appended to the end of the local list in a
135 + * special way: the last item in the local list has its "next" pointer set
136 + * to point to the inherited list, but the local list's tail pointer points
137 + * at the end of the local list. Thus, if the local list is empty, the head
138 + * will be pointing at the inherited content but the tail will be NULL. To
139 + * help you visualize this, here are the possible list arrangements:
141 + * Completely Empty Local Content Only
142 + * ================================== ====================================
143 + * head -> NULL head -> Local1 -> Local2 -> NULL
144 + * tail -> NULL tail -------------^
146 + * Inherited Content Only Both Local and Inherited Content
147 + * ================================== ====================================
148 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
149 + * tail -> NULL tail ---------^
151 + * This means that anyone wanting to traverse the whole list to USE it just
152 + * needs to start at the head and use the "next" pointers until it goes
153 + * NULL. To add new local content, we insert the item after the tail item
154 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
155 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
156 + * because it is shared between the current list and our parent list(s).
157 + * The easiest way to handle this is to simply truncate the list after the
158 + * tail item and then free the local list from the head. When inheriting
159 + * the list for a new local dir, we just save off the exclude_list_struct
160 + * values (so we can pop back to them later) and set the tail to NULL.
163 /** Build an exclude structure given an exclude pattern. */
164 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
165 @@ -46,23 +102,50 @@ static void make_exclude(struct exclude_
170 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
171 + who_am_i(), (int)pat_len, pat, listp->debug_type,
172 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
173 + mflags & MATCHFLG_INCLUDE ? "in" : "ex");
176 + if (mflags & MATCHFLG_MERGE_FILE) {
178 + /* If the local include file was already mentioned, don't
180 + for (i = 0; i < mergelist_cnt; i++) {
181 + struct exclude_struct *ex = mergelist_parents[i];
182 + if (strlen(ex->pattern) == pat_len
183 + && memcmp(ex->pattern, pat, pat_len) == 0)
186 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
187 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
188 + mflags |= MATCHFLG_CVSIGNORE;
189 + mflags &= ~MATCHFLG_INCLUDE;
191 + mflags &= ~MATCHFLG_CVSIGNORE;
194 ret = new(struct exclude_struct);
196 out_of_memory("make_exclude");
198 memset(ret, 0, sizeof ret[0]);
200 - if (exclude_path_prefix)
201 - mflags |= MATCHFLG_ABS_PATH;
202 - if (exclude_path_prefix && *pat == '/')
203 - ex_len = strlen(exclude_path_prefix);
205 + if (mflags & MATCHFLG_ABS_PATH) {
207 + mflags &= ~MATCHFLG_ABS_PATH;
210 + ex_len = dirbuf_len - module_dirlen - 1;
213 ret->pattern = new_array(char, ex_len + pat_len + 1);
215 out_of_memory("make_exclude");
217 - memcpy(ret->pattern, exclude_path_prefix, ex_len);
218 + memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
219 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
222 @@ -81,14 +164,40 @@ static void make_exclude(struct exclude_
223 mflags |= MATCHFLG_DIRECTORY;
226 - for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
228 + if (mflags & MATCHFLG_MERGE_FILE) {
229 + struct exclude_list_struct *lp
230 + = new_array(struct exclude_list_struct, 1);
232 + out_of_memory("make_exclude");
233 + lp->head = lp->tail = NULL;
234 + if ((cp = strrchr(ret->pattern, '/')) != NULL)
238 + if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
239 + out_of_memory("make_exclude");
240 + ret->u.mergelist = lp;
241 + if (mergelist_cnt == mergelist_size) {
242 + mergelist_size += 5;
243 + mergelist_parents = realloc_array(mergelist_parents,
244 + struct exclude_struct *,
246 + if (!mergelist_parents)
247 + out_of_memory("make_exclude");
249 + mergelist_parents[mergelist_cnt++] = ret;
251 + for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
252 + ret->u.slash_cnt++;
255 ret->match_flags = mflags;
258 + if (!listp->tail) {
259 + ret->next = listp->head;
260 listp->head = listp->tail = ret;
263 + ret->next = listp->tail->next;
264 listp->tail->next = ret;
267 @@ -96,22 +205,267 @@ static void make_exclude(struct exclude_
269 static void free_exclude(struct exclude_struct *ex)
271 + if (ex->match_flags & MATCHFLG_MERGE_FILE) {
272 + free(ex->u.mergelist->debug_type);
273 + free(ex->u.mergelist);
279 -void clear_exclude_list(struct exclude_list_struct *listp)
280 +static void clear_exclude_list(struct exclude_list_struct *listp)
282 - struct exclude_struct *ent, *next;
284 - for (ent = listp->head; ent; ent = next) {
288 + struct exclude_struct *ent, *next;
289 + /* Truncate any inherited items from the local list. */
290 + listp->tail->next = NULL;
291 + /* Now free everything that is left. */
292 + for (ent = listp->head; ent; ent = next) {
298 listp->head = listp->tail = NULL;
301 +/* This returns an expanded (absolute) filename for the merge-file name if
302 + * the name has any slashes in it OR if the parent_dirscan var is True;
303 + * otherwise it returns the original merge_file name. If the len_ptr value
304 + * is non-NULL the merge_file name is limited by the referenced length
305 + * value and will be updated with the length of the resulting name. We
306 + * always return a name that is null terminated, even if the merge_file
308 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
309 + unsigned int prefix_skip)
311 + static char buf[MAXPATHLEN];
312 + char *fn, tmpbuf[MAXPATHLEN];
313 + unsigned int fn_len;
315 + if (!parent_dirscan && *merge_file != '/') {
316 + /* Return the name unchanged it doesn't have any slashes. */
318 + const char *p = merge_file + *len_ptr;
319 + while (--p > merge_file && *p != '/') {}
320 + if (p == merge_file) {
321 + strlcpy(buf, merge_file, *len_ptr + 1);
324 + } else if (strchr(merge_file, '/') == NULL)
325 + return (char *)merge_file;
328 + fn = *merge_file == '/' ? buf : tmpbuf;
329 + if (sanitize_paths) {
330 + const char *r = prefix_skip ? "/" : NULL;
331 + /* null-terminate the name if it isn't already */
332 + if (len_ptr && merge_file[*len_ptr]) {
333 + char *to = fn == buf ? tmpbuf : buf;
334 + strlcpy(to, merge_file, *len_ptr + 1);
337 + if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
338 + rprintf(FERROR, "merge-file name overflows: %s\n",
343 + strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
344 + clean_fname(fn, 1);
347 + fn_len = strlen(fn);
351 + if (dirbuf_len + fn_len >= MAXPATHLEN) {
352 + rprintf(FERROR, "merge-file name overflows: %s\n", fn);
355 + memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
356 + memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
357 + fn_len = clean_fname(buf, 1);
365 +/* Sets the dirbuf and dirbuf_len values. */
366 +void set_excludes_dir(const char *dir, unsigned int dirlen)
370 + memcpy(dirbuf, curr_dir, curr_dir_len);
371 + dirbuf[curr_dir_len] = '/';
372 + len = curr_dir_len + 1;
373 + if (len + dirlen >= MAXPATHLEN)
377 + memcpy(dirbuf + len, dir, dirlen);
378 + dirbuf[dirlen + len] = '\0';
379 + dirbuf_len = clean_fname(dirbuf, 1);
380 + if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
381 + && dirbuf[dirbuf_len-2] == '/')
383 + dirbuf[dirbuf_len++] = '/';
384 + dirbuf[dirbuf_len] = '\0';
385 + if (sanitize_paths)
386 + dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
389 +/* This routine takes a per-dir merge-file entry and finishes its setup.
390 + * If the name has a path portion then we check to see if it refers to a
391 + * parent directory of the first transfer dir. If it does, we scan all the
392 + * dirs from that point through the parent dir of the transfer dir looking
393 + * for the per-dir merge-file in each one. */
394 +static BOOL setup_merge_file(struct exclude_struct *ex,
395 + struct exclude_list_struct *lp, int flags)
397 + char buf[MAXPATHLEN];
398 + char *x, *y, *pat = ex->pattern;
401 + if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
404 + y = strrchr(x, '/');
406 + ex->pattern = strdup(y+1);
410 + strlcpy(buf, x, MAXPATHLEN);
412 + pathjoin(buf, MAXPATHLEN, dirbuf, x);
414 + len = clean_fname(buf, 1);
415 + if (len != 1 && len < MAXPATHLEN-1) {
419 + /* This ensures that the specified dir is a parent of the transfer. */
420 + for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
422 + y += strlen(y); /* nope -- skip the scan */
424 + parent_dirscan = True;
426 + char save[MAXPATHLEN];
427 + strlcpy(save, y, MAXPATHLEN);
429 + dirbuf_len = y - dirbuf;
430 + strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
431 + add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
432 + if (ex->match_flags & MATCHFLG_CVSIGNORE)
433 + lp->head = NULL; /* CVS doesn't inherit rules. */
435 + strlcpy(y, save, MAXPATHLEN);
436 + while ((*x++ = *y++) != '/') {}
438 + parent_dirscan = False;
443 +/* Each time rsync changes to a new directory it call this function to
444 + * handle all the per-dir merge-files. The "dir" value is the current path
445 + * relative to curr_dir (which might not be null-terminated). We copy it
446 + * into dirbuf so that we can easily append a file name on the end. */
447 +void *push_local_excludes(const char *dir, unsigned int dirlen)
449 + struct mergelist_save_struct *push;
450 + struct exclude_list_struct *ap;
453 + set_excludes_dir(dir, dirlen);
455 + if (!(push = new_array(struct mergelist_save_struct, 1)))
456 + out_of_memory("push_local_excludes");
458 + push->count = mergelist_cnt;
459 + push->array = new_array(struct exclude_list_struct, mergelist_cnt);
461 + out_of_memory("push_local_excludes");
463 + for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
464 + memcpy(ap++, mergelist_parents[i]->u.mergelist,
465 + sizeof (struct exclude_list_struct));
468 + /* Note: add_exclude_file() might increase mergelist_cnt, so keep
469 + * this loop separate from the above loop. */
470 + for (i = 0; i < mergelist_cnt; i++) {
471 + struct exclude_struct *ex = mergelist_parents[i];
472 + struct exclude_list_struct *lp = ex->u.mergelist;
476 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
477 + who_am_i(), lp->debug_type);
480 + if (ex->match_flags & MATCHFLG_CVSIGNORE) {
481 + lp->head = NULL; /* CVS doesn't inherit rules. */
482 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
484 + flags = ex->match_flags & MATCHFLG_INCLUDE
485 + ? XFLG_DEF_INCLUDE : 0;
487 + lp->tail = NULL; /* Switch any local rules to inherited. */
489 + if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
490 + ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
491 + if (setup_merge_file(ex, lp, flags))
492 + set_excludes_dir(dir, dirlen);
495 + if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
496 + MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
497 + add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
499 + io_error |= IOERR_GENERAL;
501 + "cannot add local excludes in long-named directory %s\n",
502 + full_fname(dirbuf));
504 + dirbuf[dirbuf_len] = '\0';
507 + return (void*)push;
510 +void pop_local_excludes(void *mem)
512 + struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
513 + struct exclude_list_struct *ap;
516 + for (i = mergelist_cnt; i-- > 0; ) {
517 + struct exclude_struct *ex = mergelist_parents[i];
518 + struct exclude_list_struct *lp = ex->u.mergelist;
521 + rprintf(FINFO, "[%s] popping %sexclude list\n",
522 + who_am_i(), lp->debug_type);
525 + clear_exclude_list(lp);
528 + mergelist_cnt = pop->count;
529 + for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
530 + memcpy(mergelist_parents[i]->u.mergelist, ap++,
531 + sizeof (struct exclude_list_struct));
538 static int check_one_exclude(char *name, struct exclude_struct *ex,
541 @@ -125,13 +479,14 @@ static int check_one_exclude(char *name,
542 /* If the pattern does not have any slashes AND it does not have
543 * a "**" (which could match a slash), then we just match the
544 * name portion of the path. */
545 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
546 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
547 if ((p = strrchr(name,'/')) != NULL)
550 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
552 - pathjoin(full_name, sizeof full_name, curr_dir + 1, name);
553 + && curr_dir_len > module_dirlen + 1) {
554 + pathjoin(full_name, sizeof full_name,
555 + curr_dir + module_dirlen + 1, name);
559 @@ -148,9 +503,9 @@ static int check_one_exclude(char *name,
560 if (ex->match_flags & MATCHFLG_WILD) {
561 /* A non-anchored match with an infix slash and no "**"
562 * needs to match the last slash_cnt+1 name elements. */
563 - if (!match_start && ex->slash_cnt
564 + if (!match_start && ex->u.slash_cnt
565 && !(ex->match_flags & MATCHFLG_WILD2)) {
566 - int cnt = ex->slash_cnt + 1;
567 + int cnt = ex->u.slash_cnt + 1;
568 for (p = name + strlen(name) - 1; p >= name; p--) {
569 if (*p == '/' && !--cnt)
571 @@ -221,6 +576,13 @@ int check_exclude(struct exclude_list_st
572 struct exclude_struct *ent;
574 for (ent = listp->head; ent; ent = ent->next) {
575 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
576 + int rc = check_exclude(ent->u.mergelist, name,
582 if (check_one_exclude(name, ent, name_is_dir)) {
583 report_exclude_result(name, ent, name_is_dir,
585 @@ -254,11 +616,36 @@ static const char *get_exclude_tok(const
589 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
590 + /* Check for a +/-/. followed by a space (not whitespace). */
591 if (!(xflags & XFLG_WORDS_ONLY)
592 - && (*s == '-' || *s == '+') && s[1] == ' ') {
593 + && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
595 mflags |= MATCHFLG_INCLUDE;
596 + else if (*s == '.') {
597 + mflags |= MATCHFLG_MERGE_FILE;
598 + if (xflags & XFLG_DEF_INCLUDE)
599 + mflags |= MATCHFLG_INCLUDE;
600 + while (s[2] == '-') {
605 + mflags |= MATCHFLG_PERDIR_MERGE
606 + | MATCHFLG_FINISH_SETUP;
613 + "invalid merge options: %s\n",
615 + exit_cleanup(RERR_SYNTAX);
617 + } while (s[1] != ' ');
622 } else if (xflags & XFLG_DEF_INCLUDE)
623 mflags |= MATCHFLG_INCLUDE;
624 @@ -276,6 +663,8 @@ static const char *get_exclude_tok(const
626 if (*p == '!' && len == 1)
627 mflags |= MATCHFLG_CLEAR_LIST;
628 + if (xflags & XFLG_ABS_PATH)
629 + mflags |= MATCHFLG_ABS_PATH;
633 @@ -287,7 +676,7 @@ void add_exclude(struct exclude_list_str
636 unsigned int pat_len, mflags;
638 + const char *cp, *p;
642 @@ -295,9 +684,15 @@ void add_exclude(struct exclude_list_str
646 + /* Remember that the returned string is NOT '\0' terminated! */
647 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
650 + if (pat_len >= MAXPATHLEN) {
651 + rprintf(FERROR, "discarding over-long exclude: %s\n",
656 if (mflags & MATCHFLG_CLEAR_LIST) {
658 @@ -309,13 +704,24 @@ void add_exclude(struct exclude_list_str
662 - make_exclude(listp, cp, pat_len, mflags);
665 - rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
666 - who_am_i(), (int)pat_len, cp, listp->debug_type,
667 - mflags & MATCHFLG_INCLUDE ? "in" : "ex");
668 + if (mflags & MATCHFLG_MERGE_FILE) {
669 + unsigned int len = pat_len;
670 + if (mflags & MATCHFLG_PERDIR_MERGE) {
671 + if (parent_dirscan) {
672 + if (!(p = parse_merge_name(cp, &len, module_dirlen)))
674 + make_exclude(listp, p, len, mflags);
678 + if (!(p = parse_merge_name(cp, &len, 0)))
680 + add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
685 + make_exclude(listp, cp, pat_len, mflags);
689 @@ -324,7 +730,7 @@ void add_exclude_file(struct exclude_lis
693 - char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
694 + char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
695 char *eob = line + sizeof line - 1;
696 int word_split = xflags & XFLG_WORD_SPLIT;
698 @@ -345,6 +751,12 @@ void add_exclude_file(struct exclude_lis
702 + dirbuf[dirbuf_len] = '\0';
705 + rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
706 + who_am_i(), fname, xflags);
711 @@ -405,7 +817,21 @@ void send_exclude_list(int f)
712 if (ent->match_flags & MATCHFLG_INCLUDE) {
714 write_buf(f, "+ ", 2);
715 - } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
716 + } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
717 + char buf[32], *op = buf;
720 + if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
727 + write_int(f, l + (op - buf));
728 + write_buf(f, buf, op - buf);
729 + } else if ((*p == '-' || *p == '+' || *p == '.')
732 write_buf(f, "- ", 2);
734 @@ -446,6 +872,7 @@ void add_cvs_excludes(void)
735 char fname[MAXPATHLEN];
738 + add_exclude(&exclude_list, ". -p .cvsignore", 0);
739 add_exclude(&exclude_list, default_cvsignore,
740 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
742 --- orig/flist.c 2004-09-21 09:40:27
743 +++ flist.c 2004-08-12 18:59:28
744 @@ -40,10 +40,9 @@ extern int module_id;
745 extern int ignore_errors;
746 extern int numeric_ids;
748 -extern int cvs_exclude;
751 extern char curr_dir[MAXPATHLEN];
752 +extern unsigned int curr_dir_len;
753 extern char *files_from;
754 extern int filesfrom_fd;
756 @@ -67,7 +66,6 @@ extern int list_only;
758 extern struct exclude_list_struct exclude_list;
759 extern struct exclude_list_struct server_exclude_list;
760 -extern struct exclude_list_struct local_exclude_list;
764 @@ -223,8 +221,6 @@ int link_stat(const char *path, STRUCT_S
766 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
770 #if 0 /* This currently never happens, so avoid a useless compare. */
771 if (exclude_level == NO_EXCLUDES)
773 @@ -246,10 +242,7 @@ static int check_exclude_file(char *fnam
774 if (exclude_level != ALL_EXCLUDES)
776 if (exclude_list.head
777 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
779 - if (local_exclude_list.head
780 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
781 + && check_exclude(&exclude_list, fname, is_dir) < 0)
785 @@ -978,15 +971,7 @@ void send_file_name(int f, struct file_l
787 if (recursive && S_ISDIR(file->mode)
788 && !(file->flags & FLAG_MOUNT_POINT)) {
789 - struct exclude_list_struct last_list = local_exclude_list;
790 - local_exclude_list.head = local_exclude_list.tail = NULL;
791 send_directory(f, flist, f_name_to(file, fbuf));
793 - rprintf(FINFO, "[%s] popping %sexclude list\n",
794 - who_am_i(), local_exclude_list.debug_type);
796 - clear_exclude_list(&local_exclude_list);
797 - local_exclude_list = last_list;
801 @@ -997,6 +982,7 @@ static void send_directory(int f, struct
803 char fname[MAXPATHLEN];
805 + void *save_excludes;
809 @@ -1020,18 +1006,7 @@ static void send_directory(int f, struct
814 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
815 - < MAXPATHLEN - offset) {
816 - add_exclude_file(&local_exclude_list, fname,
817 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
819 - io_error |= IOERR_GENERAL;
821 - "cannot cvs-exclude in long-named directory %s\n",
822 - full_fname(fname));
825 + save_excludes = push_local_excludes(fname, offset);
827 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
828 char *dname = d_name(di);
829 @@ -1052,6 +1027,8 @@ static void send_directory(int f, struct
830 rsyserr(FERROR, errno, "readdir(%s)", dir);
833 + pop_local_excludes(save_excludes);
838 @@ -1071,6 +1048,7 @@ struct file_list *send_file_list(int f,
839 char *p, *dir, olddir[sizeof curr_dir];
840 char lastpath[MAXPATHLEN] = "";
841 struct file_list *flist;
842 + BOOL need_first_push = True;
846 @@ -1091,6 +1069,10 @@ struct file_list *send_file_list(int f,
847 exit_cleanup(RERR_FILESELECT);
850 + if (curr_dir_len < MAXPATHLEN - 1) {
851 + push_local_excludes(curr_dir, curr_dir_len);
852 + need_first_push = False;
857 @@ -1121,6 +1103,15 @@ struct file_list *send_file_list(int f,
861 + if (need_first_push) {
862 + if ((p = strrchr(fname, '/')) != NULL) {
863 + if (*++p && strcmp(p, ".") != 0)
864 + push_local_excludes(fname, p - fname);
865 + } else if (strcmp(fname, ".") != 0)
866 + push_local_excludes(fname, 0);
867 + need_first_push = False;
870 if (link_stat(fname, &st, keep_dirlinks) != 0) {
872 io_error |= IOERR_GENERAL;
873 --- orig/options.c 2004-10-14 17:11:40
874 +++ options.c 2004-10-14 17:26:10
875 @@ -287,6 +287,7 @@ void usage(enum logcode F)
876 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
877 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
878 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
879 + rprintf(F," -E same as --exclude='. -p /.rsync-excludes'\n");
880 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
881 rprintf(F," --version print version number\n");
882 rprintf(F," --blocking-io use blocking I/O for the remote shell\n");
883 @@ -380,6 +381,7 @@ static struct poptOption long_options[]
884 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
885 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
886 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
887 + {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
888 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
889 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
890 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
891 @@ -652,6 +654,11 @@ int parse_arguments(int *argc, const cha
896 + add_exclude(&exclude_list,
897 + ". -p /.rsync-excludes", 0);
903 --- orig/rsync.h 2004-10-09 03:21:56
904 +++ rsync.h 2004-09-22 08:48:53
906 #define XFLG_WORDS_ONLY (1<<2)
907 #define XFLG_WORD_SPLIT (1<<3)
908 #define XFLG_DIRECTORY (1<<4)
909 +#define XFLG_ABS_PATH (1<<5)
911 #define PERMS_REPORT (1<<0)
912 #define PERMS_SKIP_MTIME (1<<1)
913 @@ -503,11 +504,18 @@ struct map_struct {
914 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
915 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
916 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
917 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
918 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
919 +#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
920 +#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
921 struct exclude_struct {
922 struct exclude_struct *next;
924 unsigned int match_flags;
928 + struct exclude_list_struct *mergelist;
932 struct exclude_list_struct {
933 --- orig/rsync.yo 2004-10-06 00:12:16
934 +++ rsync.yo 2004-08-13 00:43:31
935 @@ -364,6 +364,7 @@ verb(
936 --include=PATTERN don't exclude files matching PATTERN
937 --include-from=FILE don't exclude patterns listed in FILE
938 --files-from=FILE read FILE for list of source-file names
939 + -E same as --exclude='. -p /.rsync-excludes'
940 -0 --from0 all file lists are delimited by nulls
941 --version print version number
942 --daemon run as an rsync daemon
943 @@ -1037,24 +1038,32 @@ The exclude and include patterns specifi
944 selection of which files to transfer and which files to skip.
946 Rsync builds an ordered list of include/exclude options as specified on
947 -the command line. Rsync checks each file and directory
948 -name against each exclude/include pattern in turn. The first matching
950 +It can also be told to check for include/exclude options in each
951 +directory that rsync visits during the transfer (see the section on
952 +MERGED EXCLUDE FILES for the details on these per-directory exclude
955 +As the list of files/directories to transfer is built, rsync checks each
956 +name against every exclude/include pattern in turn. The first matching
957 pattern is acted on. If it is an exclude pattern, then that file is
958 skipped. If it is an include pattern then that filename is not
959 skipped. If no matching include/exclude pattern is found then the
960 filename is not skipped.
962 -The filenames matched against the exclude/include patterns are relative
963 -to the "root of the transfer". If you think of the transfer as a
964 -subtree of names that are being sent from sender to receiver, the root
965 -is where the tree starts to be duplicated in the destination directory.
966 -This root governs where patterns that start with a / match (see below).
967 +The global include/exclude rules are anchored at the "root of the
968 +transfer" (as opposed to per-directory rules, which are anchored at
969 +the merge-file's directory). If you think of the transfer as a
970 +subtree of names that are being sent from sender to receiver, the
971 +transfer-root is where the tree starts to be duplicated in the
972 +destination directory. This root governs where patterns that start
973 +with a / match (as described in the list on pattern forms below).
975 Because the matching is relative to the transfer-root, changing the
976 trailing slash on a source path or changing your use of the --relative
977 option affects the path you need to use in your matching (in addition to
978 changing how much of the file tree is duplicated on the destination
979 -system). The following examples demonstrate this.
980 +host). The following examples demonstrate this.
982 Let's say that we want to match two source files, one with an absolute
983 path of "/home/me/foo/bar", and one with a path of "/home/you/bar/baz".
984 @@ -1101,23 +1110,27 @@ because rsync did not descend through th
987 Note also that the --include and --exclude options take one pattern
988 -each. To add multiple patterns use the --include-from and
989 ---exclude-from options or multiple --include and --exclude options.
990 +each. To add multiple patterns use the --include-from and --exclude-from
991 +options or multiple --include and --exclude options.
993 -The patterns can take several forms. The rules are:
994 +The include/exclude patterns can take several forms. The rules are:
998 - it() if the pattern starts with a / then it is matched against the
999 - start of the filename, otherwise it is matched against the end of
1001 - This is the equivalent of a leading ^ in regular expressions.
1002 - Thus "/foo" would match a file called "foo" at the transfer-root
1003 - (see above for how this is different from the filesystem-root).
1004 - On the other hand, "foo" would match any file called "foo"
1005 + it() if the pattern starts with a / then it is anchored to a
1006 + particular spot in the hierarchy of files, otherwise it is matched
1007 + against the end of the pathname. This is similar to a leading ^ in
1008 + regular expressions.
1009 + Thus "/foo" would match a file called "foo" at either the "root of the
1010 + transfer" (for a global rule) or in the merge-file's directory (for a
1011 + per-directory rule).
1012 + An unqualified "foo" would match any file or directory named "foo"
1013 anywhere in the tree because the algorithm is applied recursively from
1015 top down; it behaves as if each path component gets a turn at being the
1016 - end of the file name.
1017 + end of the file name. Even the unanchored "sub/foo" would match at
1018 + any point in the hierarchy where a "foo" was found within a directory
1021 it() if the pattern ends with a / then it will only match a
1022 directory, not a file, link, or device.
1023 @@ -1130,22 +1143,31 @@ itemize(
1024 single asterisk pattern "*" will stop at slashes.
1026 it() if the pattern contains a / (not counting a trailing /) or a "**"
1027 - then it is matched against the full filename, including any leading
1028 - directory. If the pattern doesn't contain a / or a "**", then it is
1029 + then it is matched against the full pathname, including any leading
1030 + directories. If the pattern doesn't contain a / or a "**", then it is
1031 matched only against the final component of the filename. Again,
1032 remember that the algorithm is applied recursively so "full filename" can
1033 actually be any portion of a path below the starting directory.
1035 it() if the pattern starts with "+ " (a plus followed by a space)
1036 then it is always considered an include pattern, even if specified as
1037 - part of an exclude option. The prefix is discarded before matching.
1038 + part of an exclude option. (The prefix is discarded before matching.)
1040 it() if the pattern starts with "- " (a minus followed by a space)
1041 then it is always considered an exclude pattern, even if specified as
1042 - part of an include option. The prefix is discarded before matching.
1043 + part of an include option. (The prefix is discarded before matching.)
1045 + it() if the pattern starts with ". " (a dot followed by a space) then its
1046 + pattern is taken to be a merge-file that is read in to supplement the
1047 + current rules. See the section on MERGED EXCLUDE FILES for more
1050 it() if the pattern is a single exclamation mark ! then the current
1051 include/exclude list is reset, removing all previously defined patterns.
1052 + The "current" list is either the global list of rules (which are
1053 + specified via options) or a set of per-directory rules (which are
1054 + inherited in their own sub-list, so a subdirectory can use this to
1055 + clear out the parent's rules).
1058 The +/- rules are most useful in a list that was read from a file, allowing
1059 @@ -1192,8 +1214,160 @@ itemize(
1060 it() --include "*/" --include "*.c" --exclude "*" would include all
1061 directories and C source files
1062 it() --include "foo/" --include "foo/bar.c" --exclude "*" would include
1063 - only foo/bar.c (the foo/ directory must be explicitly included or
1064 - it would be excluded by the "*")
1065 + only the foo directory and foo/bar.c (the foo directory must be
1066 + explicitly included or it would be excluded by the "*")
1069 +manpagesection(MERGED EXCLUDE FILES)
1071 +You can merge whole files into an exclude file by specifying a rule that
1072 +starts with a ". " (a dot followed by a space) and putting a filename in
1073 +place of the pattern. There are two kinds of merged exclude files --
1074 +single-instance and per-directory. The choice is made via an option
1075 +placed prior to the merge-file name:
1079 +dit(bf(-p)) Make the file a per-directory merge-file. Rsync will scan
1080 +every directory that it traverses for the named file, merging its contents
1081 +when the file exists. These exclude files must exist on the sending side
1082 +because it is the sending side that is being scanned for available files
1083 +to send. The files may also need to be transferred to the receiving side
1084 +if you want them to affect what files don't get deleted (see PER-DIRECTORY
1085 +EXCLUDES AND DELETE below).
1087 +dit(bf(--)) End the scanning of options. Useful if you want to specify a
1088 +filename that begins with a dash.
1092 +Per-directory rules are inherited in all subdirectories of the directory
1093 +where the merge-file was found. Each subdirectory's rules are prefixed
1094 +to the inherited rules from the parent directories, which gives the
1095 +newest rules a higher priority than the inherited rules. The entire set
1096 +of per-dir rules is grouped together in the spot where the merge-file was
1097 +specified, so it is possible to override per-dir rules via a rule that
1098 +got specified earlier in the list of global rules.
1100 +If you don't want a per-dir rule to be inherited, anchor it with a leading
1101 +slash. Anchored rules in a per-directory merge-file are relative to the
1102 +merge-file's directory, so a rule "/foo" would only exclude the file "foo"
1103 +in the directory where the per-dir exclude file was found.
1105 +Here's an example exclude file which you'd specify via the normal
1106 +--exclude-from=FILE option:
1109 + . /home/user/.global_excludes
1116 +This will merge the contents of the /home/user/.global_excludes file at the
1117 +start of the list and also turns the ".excl" filename into a per-directory
1118 +exclude file. All the merged rules default to being exclude rules because
1119 +an exclude statement was used to specify them. Rules read in from the
1120 +.global_excludes file are anchored just like all other global rules.
1122 +If a per-directory merge-file is specified with a path that is a parent
1123 +directory of the first transfer directory, rsync will scan all the parent
1124 +dirs from that starting point to the transfer directory for the indicated
1125 +per-directory file. For instance, the -E option is an abbreviation for
1129 + --exclude='. -p /.rsync-excludes'
1132 +That exclude tells rsync to scan for the file .rsync-excludes in all
1133 +directories from the root down through the source of the transfer. (For
1134 +an rsync daemon, the "root dir" is always the module's "path" setting.)
1136 +Some examples of this pre-scanning for per-directory files:
1139 + rsync -avE /src/path/ /dest/dir
1140 + rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
1141 + rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
1144 +The first two commands above will look for ".rsync-excludes" in "/" and
1145 +"/src" before the normal scan begins looking for the file in "/src/path"
1146 +and its subdirectories. The last command avoids the parent-dir scan
1147 +and only looks for the ".rsync-excludes" files in each directory that is
1148 +a part of the transfer.
1150 +Finally, note that the parsing of any merge-file named ".cvsignore" is
1151 +always done in a CVS-compatible manner, even if -C wasn't specified. This
1152 +means that its rules are always excludes (even if an include option
1153 +specified the file), patterns are split on whitespace, the rules are never
1154 +inherited, and no special characters are honored (e.g. no comments, no "!",
1157 +Additionally, you can affect where the --cvs-exclude (-C) option's
1158 +inclusion of the per-directory .cvsignore file gets placed into your rules
1159 +by adding your own explicit per-directory merge rule for ".cvsignore".
1160 +Without this, rsync would add its this rule at the end of all your other
1161 +rules (giving it a lower priority than your command-line rules). For
1165 + rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1168 +The above will merge all the per-directory .cvsignore rules at the start of
1169 +your list rather than at the end. This allows their dir-specific rules to
1170 +supersede your rules instead of being subservient to them. (The global
1171 +rules taken from the $HOME/.cvsignore file and from $CVSIGNORE are not
1172 +repositioned by this.)
1174 +manpagesection(PER-DIRECTORY EXCLUDES AND DELETE)
1176 +Without a delete option, per-directory excludes are only relevant on the
1177 +sending side, so you can feel free to exclude the merge files themselves
1178 +without affecting the transfer:
1181 + rsync -av --exclude='. -p .excl' --exclude=.excl host:src/dir /dest
1184 +However, if you want to do a delete on the receiving side AND you want some
1185 +files to be excluded from being deleted, you'll need to be sure that the
1186 +receiving side knows what files to exclude. The easiest way is to include
1187 +the per-directory merge files in the transfer and use --delete-after
1188 +because this ensures that the receiving side gets all the same exclude
1189 +rules as the sending side before it tries to delete anything:
1192 + rsync -avE --delete-after host:src/dir /dest
1195 +However, if you the merge files are not a part of the transfer, you'll need
1196 +to either use a global exclude rule (i.e. specified on the command line),
1197 +or you'll need to maintain your own per-directory merge files on the
1198 +receiving side. An example of the first is this (assume that the remote
1199 +.ctrl files exclude themselves):
1202 + rsync -av --exclude='. -p .ctrl' --exclude-from=/my/extra.rules
1203 + --delete host:src/dir /dest
1206 +In the above example the extra.rules file can affect both sides of the
1207 +transfer, but the rules are subservient to the rules merged from the .ctrl
1208 +files because they were specified after the per-directory merge rule.
1210 +In the final example, the remote side is excluding the .rsync-excludes
1211 +files from the transfer, but we want to use our own .rsync-excludes files
1212 +to control what gets deleted on the receiving side. To do this we must
1213 +specifically exclude the per-directory merge files (so that they don't get
1214 +deleted) and then put rules into the local files to control what else
1215 +should not get deleted. Like this:
1218 + rsync -avE --exclude=.rsync-excludes --delete host:src/dir /dest
1221 manpagesection(BATCH MODE)
1222 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
1223 +++ testsuite/exclude.test 2004-08-08 06:35:15
1224 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
1225 makepath "$fromdir/foo/down/to/you"
1226 makepath "$fromdir/bar/down/to/foo/too"
1227 makepath "$fromdir/mid/for/foo/and/that/is/who"
1228 +cat >"$fromdir/.excl" <<EOF
1234 echo kept >"$fromdir/foo/file1"
1235 echo removed >"$fromdir/foo/file2"
1236 echo cvsout >"$fromdir/foo/file2.old"
1237 +cat >"$fromdir/foo/.excl" <<EOF
1241 +cat >"$fromdir/bar/.excl" <<EOF
1246 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1247 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
1250 echo keeper >"$fromdir/bar/down/to/foo/file1"
1251 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1252 echo gone >"$fromdir/bar/down/to/foo/file3"
1253 echo lost >"$fromdir/bar/down/to/foo/file4"
1254 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1255 echo smashed >"$fromdir/bar/down/to/foo/to"
1256 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1259 +# This one should be ineffectual
1260 +cat >"$fromdir/mid/.excl2" <<EOF
1263 echo cvsout >"$fromdir/mid/one-in-one-out"
1264 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1265 echo cvsin >"$fromdir/mid/one-for-all"
1266 +cat >"$fromdir/mid/.excl" <<EOF
1269 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1270 echo expunged >"$fromdir/mid/for/foo/extra"
1271 echo retained >"$fromdir/mid/for/foo/keep"
1272 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1273 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1274 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1276 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1279 +rm "$chkdir"/foo/file1
1280 +rm "$chkdir"/bar/.excl
1281 +rm "$chkdir"/bar/down/to/.excl2
1282 +rm "$chkdir"/bar/down/to/foo/.excl2
1283 +rm "$chkdir"/mid/.excl
1284 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1285 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1287 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1289 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1292 +checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1293 + --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1295 # The script would have aborted on error, so getting here means we've won.