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
26 Then the file ".excl2" will also be read in from the current dir and all
27 its subdirs (due to the -p option). The file ".excl3" would just be
28 read in from the current dir (because it was not specified with the -p
33 --- orig/clientserver.c 2004-08-02 02:29:16
34 +++ clientserver.c 2004-08-09 02:25:28
35 @@ -48,9 +48,10 @@ extern int no_detach;
36 extern int default_af_hint;
37 extern char *bind_address;
38 extern struct exclude_list_struct server_exclude_list;
39 -extern char *exclude_path_prefix;
40 extern char *config_file;
41 extern char *files_from;
42 +extern char dirbuf[MAXPATHLEN];
43 +extern unsigned int dirbuf_offset;
47 @@ -300,26 +301,27 @@ static int rsync_module(int f_in, int f_
48 /* TODO: Perhaps take a list of gids, and make them into the
49 * supplementary groups. */
51 - exclude_path_prefix = use_chroot? "" : lp_path(i);
52 - if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
53 - exclude_path_prefix = "";
55 + set_current_subdir("", 0);
57 + set_current_subdir(lp_path(i), strlen(lp_path(i)));
59 p = lp_include_from(i);
60 add_exclude_file(&server_exclude_list, p,
61 - XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
62 + XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
65 add_exclude(&server_exclude_list, p,
66 - XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
67 + XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
69 p = lp_exclude_from(i);
70 add_exclude_file(&server_exclude_list, p,
72 + XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
75 - add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
76 + add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
78 - exclude_path_prefix = NULL;
79 + set_current_subdir("", 0);
83 --- orig/exclude.c 2004-08-05 23:16:37
84 +++ exclude.c 2004-08-09 03:19:40
89 +extern int am_sender;
94 +extern int module_id;
95 +extern int delete_mode;
96 +extern int delete_excluded;
97 +extern int sanitize_paths;
99 extern char curr_dir[];
100 +extern unsigned int curr_dir_len;
102 struct exclude_list_struct exclude_list = { 0, 0, "" };
103 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
104 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
105 -char *exclude_path_prefix = NULL;
107 +struct mergelist_save_struct {
108 + struct exclude_list_struct *array;
112 +/* The dirbuf is set by push_local_excludes() to the current subdirectory
113 + * relative to curr_dir that is being processed. The path always has a
114 + * trailing slash appended, and the variable dirbuf_offset contains the
115 + * length of this path prefix (i.e. it is an offset where you can copy a
116 + * filename to create a pathname that can be opened for reading/writing.
118 + * The path is normally relative (e.g. "sub/dir/foo"), but it is set to an
119 + * absolute path when the push code is working on a parent-dir scan of dirs
120 + * that might be higher than the root of the transfer. In that case, the
121 + * path is absolute, and any newly-created per-dir merge files will (at
122 + * least temporarily) get an absolute file name so that we know at what
123 + * point in the hierarchy it first makes an appearance. */
124 +static char dirbuf[MAXPATHLEN];
125 +static unsigned int dirbuf_offset = 0;
126 +static BOOL parent_dirscan = 0;
128 +/* This array contains a list of all the currently active per-dir merge
129 + * files. This makes it easier to save the appropriate values when we
130 + * "push" down into each subdirectory. */
131 +static struct exclude_struct **mergelist_parents;
132 +static int mergelist_cnt = 0;
133 +static int mergelist_size = 0;
135 +/* Each exclude_list_struct describes a singly-linked list by keeping track
136 + * of both the head and tail pointers. The list is slightly unusual in that
137 + * a parent-dir's content can be appended to the end of the local list in a
138 + * special way: the last item in the local list has its "next" pointer set
139 + * to point to the inherited list, but the local list's tail pointer points
140 + * at the end of the local list. Thus, if the local list is empty, the head
141 + * will be pointing at the inherited content but the tail will be NULL. To
142 + * help you visualize this, here are the possible list arrangements:
144 + * Completely Empty Local Content Only
145 + * ================================== ====================================
146 + * head -> NULL head -> Local1 -> Local2 -> NULL
147 + * tail -> NULL tail -------------^
149 + * Inherited Content Only Both Local and Inherited Content
150 + * ================================== ====================================
151 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
152 + * tail -> NULL tail ---------^
154 + * This means that anyone wanting to traverse the whole list to USE it just
155 + * needs to start at the head and use the "next" pointers until it goes
156 + * NULL. To add new local content, we insert the item after the tail item
157 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
158 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
159 + * because it is shared between the current list and our parent list(s).
160 + * The easiest way to handle this is to simply truncate the list after the
161 + * tail item and then free the local list from the head. When inheriting
162 + * the list for a new local dir, we just save off the exclude_list_struct
163 + * values (so we can pop back to them later) and set the tail to NULL.
166 /** Build an exclude structure given an exclude pattern. */
167 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
168 @@ -46,23 +110,56 @@ static void make_exclude(struct exclude_
173 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
174 + who_am_i(), (int)pat_len, pat, listp->debug_type,
175 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
176 + mflags & MATCHFLG_INCLUDE ? "in" : "ex");
179 + if (mflags & MATCHFLG_MERGE_FILE) {
181 + /* If the local include file was already mentioned, don't
183 + for (i = 0; i < mergelist_cnt; i++) {
184 + struct exclude_struct *ex = mergelist_parents[i];
185 + if (strlen(ex->pattern) == pat_len
186 + && memcmp(ex->pattern, pat, pat_len) == 0)
189 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
190 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
191 + mflags |= MATCHFLG_CVSIGNORE;
192 + mflags &= ~MATCHFLG_INCLUDE;
194 + mflags &= ~MATCHFLG_CVSIGNORE;
197 ret = new(struct exclude_struct);
199 out_of_memory("make_exclude");
201 memset(ret, 0, sizeof ret[0]);
203 - if (exclude_path_prefix)
204 - mflags |= MATCHFLG_ABS_PATH;
205 - if (exclude_path_prefix && *pat == '/')
206 - ex_len = strlen(exclude_path_prefix);
208 + if (mflags & MATCHFLG_ABS_PATH) {
210 + mflags &= ~MATCHFLG_ABS_PATH;
213 + ex_len = *dirbuf == '/' ? dirbuf_offset - 1
214 + : curr_dir_len + dirbuf_offset - !curr_dir[1];
218 ret->pattern = new_array(char, ex_len + pat_len + 1);
220 out_of_memory("make_exclude");
222 - memcpy(ret->pattern, exclude_path_prefix, ex_len);
224 + if (*dirbuf == '/')
225 + strlcpy(ret->pattern, dirbuf, ex_len + 1);
227 + pathjoin(ret->pattern, ex_len + 1, curr_dir, dirbuf);
229 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
232 @@ -81,14 +178,40 @@ static void make_exclude(struct exclude_
233 mflags |= MATCHFLG_DIRECTORY;
236 - for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
238 + if (mflags & MATCHFLG_MERGE_FILE) {
239 + struct exclude_list_struct *lp
240 + = new_array(struct exclude_list_struct, 1);
242 + out_of_memory("make_exclude");
243 + lp->head = lp->tail = NULL;
244 + if ((cp = strrchr(ret->pattern, '/')) != NULL)
248 + if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
249 + out_of_memory("make_exclude");
250 + ret->u.mergelist = lp;
251 + if (mergelist_cnt == mergelist_size) {
252 + mergelist_size += 5;
253 + mergelist_parents = realloc_array(mergelist_parents,
254 + struct exclude_struct *,
256 + if (!mergelist_parents)
257 + out_of_memory("make_exclude");
259 + mergelist_parents[mergelist_cnt++] = ret;
261 + for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
262 + ret->u.slash_cnt++;
265 ret->match_flags = mflags;
268 + if (!listp->tail) {
269 + ret->next = listp->head;
270 listp->head = listp->tail = ret;
273 + ret->next = listp->tail->next;
274 listp->tail->next = ret;
277 @@ -96,22 +219,265 @@ static void make_exclude(struct exclude_
279 static void free_exclude(struct exclude_struct *ex)
281 + if (ex->match_flags & MATCHFLG_MERGE_FILE) {
282 + free(ex->u.mergelist->debug_type);
283 + free(ex->u.mergelist);
289 -void clear_exclude_list(struct exclude_list_struct *listp)
290 +static void clear_exclude_list(struct exclude_list_struct *listp)
292 - struct exclude_struct *ent, *next;
294 - for (ent = listp->head; ent; ent = next) {
298 + struct exclude_struct *ent, *next;
299 + /* Truncate any inherited items from the local list. */
300 + listp->tail->next = NULL;
301 + /* Now free everything that is left. */
302 + for (ent = listp->head; ent; ent = next) {
308 listp->head = listp->tail = NULL;
311 +/* This returns an expanded (absolute) filename for the merge-file name if
312 + * the name has any slashes in it OR if the parent_dirscan var is non-zero;
313 + * otherwise it returns the original merge_file name. If the len_ptr value
314 + * is non-NULL the merge_file name is not null terminated and the length
315 + * value is contained therein (and will be updated with the new length). We
316 + * always return a name that is null terminated. */
317 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr)
319 + static char buf[MAXPATHLEN];
320 + char *fn, tmpbuf[MAXPATHLEN];
321 + unsigned int fn_len, cd_len;
323 + cd_len = *dirbuf == '/' ? 0 : curr_dir_len + 1;
324 + if (!parent_dirscan && *merge_file != '/') {
325 + /* Return the name unchanged it doesn't have any slashes. */
327 + const char *p = merge_file + *len_ptr;
328 + while (--p > merge_file && *p != '/') {}
329 + if (p == merge_file) {
330 + strlcpy(buf, merge_file, *len_ptr + 1);
333 + } else if (strchr(merge_file, '/') == NULL)
334 + return (char *)merge_file;
337 + fn = *merge_file == '/' ? buf : tmpbuf;
338 + if (sanitize_paths) {
339 + /* null-terminate the name if it isn't already */
340 + if (len_ptr && merge_file[*len_ptr]) {
341 + char *to = fn == buf ? tmpbuf : buf;
342 + strlcpy(to, merge_file, *len_ptr + 1);
345 + if (!sanitize_path(fn, merge_file, dirbuf)) {
346 + rprintf(FERROR, "merge filename overflows: %s\n",
351 + strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
358 + fn_len = strlen(fn);
359 + if (cd_len + dirbuf_offset + fn_len >= MAXPATHLEN) {
360 + rprintf(FERROR, "merge filename overflows: %s\n", fn);
364 + memcpy(buf, curr_dir, curr_dir_len);
365 + buf[curr_dir_len] = '/';
368 + memcpy(buf + cd_len, dirbuf, dirbuf_offset);
369 + memcpy(buf + cd_len + dirbuf_offset, fn, fn_len + 1);
370 + fn_len = clean_fname(buf);
377 +/* Sets the dirbuf and dirbuf_offset values */
378 +void set_current_subdir(const char *dir, unsigned int dirlen)
380 + memcpy(dirbuf, dir, dirlen);
381 + dirbuf[dirlen] = '\0';
382 + dirbuf_offset = clean_fname(dirbuf);
383 + if (dirbuf_offset == 1 && *dirbuf == '.')
386 + dirbuf[dirbuf_offset++] = '/';
387 + dirbuf[dirbuf_offset] = '\0';
390 +/* This routine takes a per-dir merge file entry and finishes its setup.
391 + * If the name has a path portion then we check to see if it refers to a
392 + * parent directory of the first transfer dir. If it does, we scan all the
393 + * dirs from that point through the parent dir of the transfer dir looking
394 + * for the per-dir merge file in each one. */
395 +static void setup_merge_file(struct exclude_struct *ex,
396 + struct exclude_list_struct *lp, int flags,
397 + const char *dir, unsigned int dirlen)
399 + char buf[MAXPATHLEN];
400 + char *x, *y, *pat = ex->pattern;
403 + if (!(x = parse_merge_name(pat, NULL)) || *x != '/')
406 + y = strrchr(x, '/');
408 + ex->pattern = strdup(y+1);
409 + if (*dirbuf != '/') {
410 + pathjoin(dirbuf, MAXPATHLEN, sanitize_paths ? "/" : curr_dir,
416 + strlcpy(buf, x, MAXPATHLEN);
418 + pathjoin(buf, MAXPATHLEN, dirbuf, x);
420 + len = clean_fname(buf);
421 + if (len != 1 && len < MAXPATHLEN-1) {
426 + if (sanitize_paths)
427 + x += strlen(lp_path(module_id));
428 + /* This ensures that the specified dir is a parent of the transfer. */
429 + for (y = dirbuf; *x && *x == *y; x++, y++) {}
431 + y += strlen(y); /* nope -- skip the scan */
433 + parent_dirscan = 1;
435 + char save[MAXPATHLEN];
436 + strlcpy(save, y, MAXPATHLEN);
438 + dirbuf_offset = y - dirbuf;
439 + strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
440 + add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
441 + if (ex->match_flags & MATCHFLG_CVSIGNORE)
442 + lp->head = NULL; /* CVS doesn't inherit rules. */
444 + strlcpy(y, save, MAXPATHLEN);
445 + while ((*x++ = *y++) != '/') {}
447 + parent_dirscan = 0;
449 + set_current_subdir(dir, dirlen);
452 +/* Each time rsync changes to a new directory it call this function to
453 + * handle all the per-dir merge files. The "dir" value is the current path
454 + * relative to curr_dir (with a mandatory trailing slash). We copy it into
455 + * dirbuf so that the routines this calls can append a file name. */
456 +void *push_local_excludes(const char *dir, unsigned int dirlen)
458 + struct mergelist_save_struct *push;
459 + struct exclude_list_struct *ap;
462 + set_current_subdir(dir, dirlen);
464 + if (!(push = new_array(struct mergelist_save_struct, 1)))
465 + out_of_memory("push_local_excludes");
467 + push->count = mergelist_cnt;
468 + push->array = new_array(struct exclude_list_struct, mergelist_cnt);
470 + out_of_memory("push_local_excludes");
472 + for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
473 + memcpy(ap++, mergelist_parents[i]->u.mergelist,
474 + sizeof (struct exclude_list_struct));
477 + /* Note: add_exclude_file() might increase mergelist_cnt, so keep
478 + * this loop separate from the above loop. */
479 + for (i = 0; i < mergelist_cnt; i++) {
480 + struct exclude_struct *ex = mergelist_parents[i];
481 + struct exclude_list_struct *lp = ex->u.mergelist;
485 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
486 + who_am_i(), lp->debug_type);
489 + if (ex->match_flags & MATCHFLG_CVSIGNORE) {
490 + lp->head = NULL; /* CVS doesn't inherit rules. */
491 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
493 + flags = ex->match_flags & MATCHFLG_INCLUDE
494 + ? XFLG_DEF_INCLUDE : 0;
496 + lp->tail = NULL; /* Switch any local rules to inherited. */
498 + if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
499 + ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
500 + setup_merge_file(ex, lp, flags, dir, dirlen);
503 + if (strlcpy(dirbuf + dirbuf_offset, ex->pattern,
504 + MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
505 + add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
507 + io_error |= IOERR_GENERAL;
509 + "cannot add local excludes in long-named directory %s\n",
510 + full_fname(dirbuf));
512 + dirbuf[dirbuf_offset] = '\0';
515 + return (void*)push;
518 +void pop_local_excludes(void *mem)
520 + struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
521 + struct exclude_list_struct *ap;
524 + for (i = mergelist_cnt; i-- > 0; ) {
525 + struct exclude_struct *ex = mergelist_parents[i];
526 + struct exclude_list_struct *lp = ex->u.mergelist;
529 + rprintf(FINFO, "[%s] popping %sexclude list\n",
530 + who_am_i(), lp->debug_type);
533 + clear_exclude_list(lp);
536 + mergelist_cnt = pop->count;
537 + for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
538 + memcpy(mergelist_parents[i]->u.mergelist, ap++,
539 + sizeof (struct exclude_list_struct));
546 static int check_one_exclude(char *name, struct exclude_struct *ex,
549 @@ -122,7 +488,7 @@ static int check_one_exclude(char *name,
550 /* If the pattern does not have any slashes AND it does not have
551 * a "**" (which could match a slash), then we just match the
552 * name portion of the path. */
553 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
554 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
555 if ((p = strrchr(name,'/')) != NULL)
558 @@ -133,7 +499,8 @@ static int check_one_exclude(char *name,
562 - if (!name[0]) return 0;
566 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
568 @@ -148,9 +515,9 @@ static int check_one_exclude(char *name,
569 if (ex->match_flags & MATCHFLG_WILD) {
570 /* A non-anchored match with an infix slash and no "**"
571 * needs to match the last slash_cnt+1 name elements. */
572 - if (!match_start && ex->slash_cnt
573 + if (!match_start && ex->u.slash_cnt
574 && !(ex->match_flags & MATCHFLG_WILD2)) {
575 - int cnt = ex->slash_cnt + 1;
576 + int cnt = ex->u.slash_cnt + 1;
577 for (p = name + strlen(name) - 1; p >= name; p--) {
578 if (*p == '/' && !--cnt)
580 @@ -221,6 +588,13 @@ int check_exclude(struct exclude_list_st
581 struct exclude_struct *ent;
583 for (ent = listp->head; ent; ent = ent->next) {
584 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
585 + int rc = check_exclude(ent->u.mergelist, name,
591 if (check_one_exclude(name, ent, name_is_dir)) {
592 report_exclude_result(name, ent, name_is_dir,
594 @@ -253,11 +627,36 @@ static const char *get_exclude_tok(const
598 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
599 + /* Check for a +/-/. followed by a space (not whitespace). */
600 if (!(xflags & XFLG_WORDS_ONLY)
601 - && (*s == '-' || *s == '+') && s[1] == ' ') {
602 + && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
604 mflags |= MATCHFLG_INCLUDE;
605 + else if (*s == '.') {
606 + mflags |= MATCHFLG_MERGE_FILE;
607 + if (xflags & XFLG_DEF_INCLUDE)
608 + mflags |= MATCHFLG_INCLUDE;
609 + while (s[2] == '-') {
614 + mflags |= MATCHFLG_PERDIR_MERGE
615 + | MATCHFLG_FINISH_SETUP;
622 + "invalid merge options: %s\n",
624 + exit_cleanup(RERR_SYNTAX);
626 + } while (s[1] != ' ');
631 } else if (xflags & XFLG_DEF_INCLUDE)
632 mflags |= MATCHFLG_INCLUDE;
633 @@ -273,6 +672,8 @@ static const char *get_exclude_tok(const
635 if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
636 mflags |= MATCHFLG_CLEAR_LIST;
637 + if (xflags & XFLG_ABS_PATH)
638 + mflags |= MATCHFLG_ABS_PATH;
642 @@ -292,9 +693,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 @@ -306,13 +713,23 @@ 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 + if (mflags & MATCHFLG_PERDIR_MERGE) {
670 + if (parent_dirscan) {
671 + if (!(cp = parse_merge_name(cp, &pat_len)))
673 + make_exclude(listp, cp, pat_len, mflags);
677 + if (!(cp = parse_merge_name(cp, &pat_len)))
679 + add_exclude_file(listp, cp, xflags | XFLG_FATAL_ERRORS);
684 + make_exclude(listp, cp, pat_len, mflags);
688 @@ -321,7 +738,7 @@ void add_exclude_file(struct exclude_lis
692 - char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
693 + char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
694 char *eob = line + sizeof line - 1;
695 int word_split = xflags & XFLG_WORD_SPLIT;
697 @@ -343,6 +760,11 @@ void add_exclude_file(struct exclude_lis
702 + rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
703 + who_am_i(), fname, xflags);
708 int ch, overflow = 0;
709 @@ -402,7 +824,21 @@ void send_exclude_list(int f)
710 if (ent->match_flags & MATCHFLG_INCLUDE) {
712 write_buf(f, "+ ", 2);
713 - } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
714 + } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
715 + char buf[32], *op = buf;
718 + if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
725 + write_int(f, l + (op - buf));
726 + write_buf(f, buf, op - buf);
727 + } else if ((*p == '-' || *p == '+' || *p == '.')
730 write_buf(f, "- ", 2);
732 @@ -443,6 +879,7 @@ void add_cvs_excludes(void)
733 char fname[MAXPATHLEN];
736 + add_exclude(&exclude_list, ". -p .cvsignore", 0);
737 add_exclude(&exclude_list, default_cvsignore,
738 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
740 --- orig/flist.c 2004-08-05 21:57:29
741 +++ flist.c 2004-08-09 02:45:42
742 @@ -39,10 +39,9 @@ extern int module_id;
743 extern int ignore_errors;
744 extern int numeric_ids;
746 -extern int cvs_exclude;
749 extern char curr_dir[MAXPATHLEN];
750 +extern unsigned int curr_dir_len;
751 extern char *files_from;
752 extern int filesfrom_fd;
754 @@ -66,7 +65,6 @@ extern int list_only;
756 extern struct exclude_list_struct exclude_list;
757 extern struct exclude_list_struct server_exclude_list;
758 -extern struct exclude_list_struct local_exclude_list;
762 @@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
764 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
768 #if 0 /* This currently never happens, so avoid a useless compare. */
769 if (exclude_level == NO_EXCLUDES)
771 @@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
772 if (exclude_level != ALL_EXCLUDES)
774 if (exclude_list.head
775 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
777 - if (local_exclude_list.head
778 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
779 + && check_exclude(&exclude_list, fname, is_dir) < 0)
783 @@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
785 if (recursive && S_ISDIR(file->mode)
786 && !(file->flags & FLAG_MOUNT_POINT)) {
787 - struct exclude_list_struct last_list = local_exclude_list;
788 - local_exclude_list.head = local_exclude_list.tail = NULL;
789 send_directory(f, flist, f_name_to(file, fbuf));
791 - rprintf(FINFO, "[%s] popping %sexclude list\n",
792 - who_am_i(), local_exclude_list.debug_type);
794 - clear_exclude_list(&local_exclude_list);
795 - local_exclude_list = last_list;
799 @@ -973,6 +958,7 @@ static void send_directory(int f, struct
801 char fname[MAXPATHLEN];
803 + void *save_excludes;
807 @@ -996,18 +982,7 @@ static void send_directory(int f, struct
812 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
813 - < MAXPATHLEN - offset) {
814 - add_exclude_file(&local_exclude_list, fname,
815 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
817 - io_error |= IOERR_GENERAL;
819 - "cannot cvs-exclude in long-named directory %s\n",
820 - full_fname(fname));
823 + save_excludes = push_local_excludes(fname, offset);
825 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
826 char *dname = d_name(di);
827 @@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
828 rsyserr(FERROR, errno, "readdir(%s)", dir);
831 + pop_local_excludes(save_excludes);
836 @@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f,
837 char *p, *dir, olddir[sizeof curr_dir];
838 char lastpath[MAXPATHLEN] = "";
839 struct file_list *flist;
840 + BOOL did_first_push = 0;
844 @@ -1067,6 +1045,10 @@ struct file_list *send_file_list(int f,
845 exit_cleanup(RERR_FILESELECT);
848 + if (curr_dir_len < MAXPATHLEN - 1) {
849 + push_local_excludes(curr_dir, curr_dir_len);
850 + did_first_push = 1;
855 @@ -1097,6 +1079,16 @@ struct file_list *send_file_list(int f,
859 + if (!did_first_push) {
860 + if ((p = strrchr(fname, '/')) != NULL) {
865 + push_local_excludes(dir, p - dir);
866 + did_first_push = 1;
869 if (link_stat(fname, &st, keep_dirlinks) != 0) {
871 io_error |= IOERR_GENERAL;
872 --- orig/options.c 2004-08-05 21:57:29
873 +++ options.c 2004-08-09 03:33:31
874 @@ -287,6 +287,7 @@ void usage(enum logcode F)
875 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
876 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
877 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
878 + rprintf(F," -E same as --exclude='. -p /.rsync-excludes'\n");
879 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
880 rprintf(F," --version print version number\n");
881 rprintf(F," --daemon run as an rsync daemon\n");
882 @@ -389,6 +390,7 @@ static struct poptOption long_options[]
883 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
884 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
885 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
886 + {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
887 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
888 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
889 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
890 @@ -589,6 +591,11 @@ int parse_arguments(int *argc, const cha
895 + add_exclude(&exclude_list,
896 + ". -p /.rsync-excludes", 0);
902 --- orig/rsync.h 2004-08-03 15:41:32
903 +++ rsync.h 2004-08-08 06:07:01
905 #define XFLG_DEF_INCLUDE (1<<1)
906 #define XFLG_WORDS_ONLY (1<<2)
907 #define XFLG_WORD_SPLIT (1<<3)
908 +#define XFLG_ABS_PATH (1<<4)
910 #define PERMS_REPORT (1<<0)
911 #define PERMS_SKIP_MTIME (1<<1)
912 @@ -499,11 +500,18 @@ struct map_struct {
913 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
914 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
915 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
916 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
917 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
918 +#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
919 +#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
920 struct exclude_struct {
921 struct exclude_struct *next;
923 unsigned int match_flags;
927 + struct exclude_list_struct *mergelist;
931 struct exclude_list_struct {
932 --- orig/rsync.yo 2004-08-03 15:34:32
933 +++ rsync.yo 2004-08-09 03:10:29
934 @@ -335,6 +335,7 @@ verb(
935 --include=PATTERN don't exclude files matching PATTERN
936 --include-from=FILE don't exclude patterns listed in FILE
937 --files-from=FILE read FILE for list of source-file names
938 + -E same as --exclude='. -p /.rsync-excludes'
939 -0 --from0 all file lists are delimited by nulls
940 --version print version number
941 --daemon run as an rsync daemon
942 @@ -1086,6 +1087,11 @@ itemize(
943 then it is always considered an exclude pattern, even if specified as
944 part of an include option. The prefix is discarded before matching.
946 + it() if the pattern starts with ". " (a dot followed by a space) then its
947 + pattern is taken to be a merge file that is read in to supplement the
948 + current rules. See the section on MERGING EXCLUDE FILES for more
951 it() if the pattern is a single exclamation mark ! then the current
952 include/exclude list is reset, removing all previously defined patterns.
954 @@ -1138,6 +1144,104 @@ itemize(
955 it would be excluded by the "*")
958 +manpagesection(MERGING EXCLUDE FILES)
960 +You can merge whole files into an exclude file by specifying a rule that
961 +starts with a ". " (a dot followed by a space) and putting a filename in
962 +place of the pattern. The filename may be preceded by one or more options:
966 +dit(bf(-p)) Make the file a per-directory merge-file. Rsync will scan
967 +every directory that it traverses for the named file, merging its contents
968 +when the file exists. (Without this option rsync just merges the rules into
971 +Rules are inherited in all subdirectories of the directory where the
972 +per-dir merge file was found. Each subdirectory's rules are prefixed to
973 +the inherited rules from a parent directory, which gives the newest rules a
974 +higher priority than the inherited rules. If you don't want a rule to be
975 +inherited, anchor it with a leading slash. Anchored rules in a
976 +per-directory merge file are relative to the current directory, so a rule
977 +"/foo" would only exclude the file "foo" in the current directory.
979 +Note also that you can eliminate all the inherited rules for a directory
980 +and its subdirectories by putting the list-clearing token (!) at the start
981 +of a per-directory file. This only clears the rules in the current
982 +sub-list, not all the rules.
984 +dit(bf(--)) End the scanning of options. Useful if you want to specify a
985 +filename that begins with a dash.
989 +Here's an example exclude file (which you'd specify via the normal
990 +--exclude-from=FILE option):
993 + . /home/user/.global_excludes
1000 +This will merge the contents of the /home/user/.global_excludes file at the
1001 +start of the list and also turns the ".excl" filename into a per-directory
1002 +exclude file. All the merged rules default to being exclude rules because
1003 +an exclude statement was used to specify them.
1005 +Note also that the parsing of any merge-file named ".cvsignore" is always
1006 +done in a CVS-compatible manner, even if -C wasn't specified. This means
1007 +that its rules are always excludes (even if an include option specified the
1008 +file), tokens are split on whitespace, the rules are never inherited, and
1009 +no special characters are honored (e.g. no comments, no "!", etc.).
1011 +Additionally, you can affect where the --cvs-exclude (-C) option's
1012 +inclusion of the per-directory .cvsignore file gets placed into your rules
1013 +by adding your own explicit per-directory merge rule for ".cvsignore".
1014 +Without this rsync would add its this rule at the end of all your other
1015 +rules (giving it a lower priority than your command-line rules). For
1016 +example, specifying this:
1019 + rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1022 +will merge all the per-directory .cvsignore rules at the start of your list
1023 +rather than at the end. This allows their dir-specific rules to supersede
1024 +your rules instead of being subservient to them. (The global rules taken
1025 +from the $HOME/.cvsignore file and from $CVSIGNORE are not repositioned by
1028 +If a per-directory merge file is specified with a path that is a parent
1029 +directory of the first transfer directory, rsync will scan all the parent
1030 +dirs from that starting point to the transfer directory for the indicated
1031 +per-directory file. For instance, the -E option is an abbreviation for
1035 + --exclude='. -p /.rsync-excludes'
1038 +That exclude tells rsync to scan for the file .rsync-excludes in all
1039 +directories from the root up through the source of the transfer. For an
1040 +rsync daemon, the root dir is always the module's "path" setting, not the
1041 +root of the filesystem (unless the two are the same).
1043 +Some examples of this pre-scanning for per-directory files:
1046 + rsync -avE /src/path/ /dest/dir
1047 + rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
1048 + rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
1051 +The first two commands above will look for .rsync-excludes in "/" and
1052 +"/src" before the normal scan begins looking for the file in "/src/path"
1053 +and its subdirectories. The last command above just starts scanning
1056 manpagesection(BATCH MODE)
1058 bf(Note:) Batch mode should be considered experimental in this version
1059 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
1060 +++ testsuite/exclude.test 2004-08-08 06:35:15
1061 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
1062 makepath "$fromdir/foo/down/to/you"
1063 makepath "$fromdir/bar/down/to/foo/too"
1064 makepath "$fromdir/mid/for/foo/and/that/is/who"
1065 +cat >"$fromdir/.excl" <<EOF
1071 echo kept >"$fromdir/foo/file1"
1072 echo removed >"$fromdir/foo/file2"
1073 echo cvsout >"$fromdir/foo/file2.old"
1074 +cat >"$fromdir/foo/.excl" <<EOF
1078 +cat >"$fromdir/bar/.excl" <<EOF
1083 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1084 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
1087 echo keeper >"$fromdir/bar/down/to/foo/file1"
1088 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1089 echo gone >"$fromdir/bar/down/to/foo/file3"
1090 echo lost >"$fromdir/bar/down/to/foo/file4"
1091 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1092 echo smashed >"$fromdir/bar/down/to/foo/to"
1093 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1096 +# This one should be ineffectual
1097 +cat >"$fromdir/mid/.excl2" <<EOF
1100 echo cvsout >"$fromdir/mid/one-in-one-out"
1101 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1102 echo cvsin >"$fromdir/mid/one-for-all"
1103 +cat >"$fromdir/mid/.excl" <<EOF
1106 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1107 echo expunged >"$fromdir/mid/for/foo/extra"
1108 echo retained >"$fromdir/mid/for/foo/keep"
1109 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1110 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1111 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1113 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1116 +rm "$chkdir"/foo/file1
1117 +rm "$chkdir"/bar/.excl
1118 +rm "$chkdir"/bar/down/to/.excl2
1119 +rm "$chkdir"/bar/down/to/foo/.excl2
1120 +rm "$chkdir"/mid/.excl
1121 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1122 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1124 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1126 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1129 +checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1130 + --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1132 # The script would have aborted on error, so getting here means we've won.