Applied.
[rsync/rsync-patches.git] / filter.diff
... / ...
CommitLineData
1After applying this patch and running configure, you MUST run this
2command before "make":
3
4 make proto
5
6This patch adds the ability to merge rules into your excludes/includes
7using a ". FILE" idiom. If you specify a name with a preceding -p
8option, that filename will be looked for in every subdirectory that
9rsync visits, and the rules found in that subdirectory's file will
10affect that dir and its subdirs.
11
12For example:
13
14 rsync -av --exclude='. -p .excl' from/ to
15
16The above will look for a file named ".excl" in every directory of the
17hierarchy that rsync visits, and it will exclude (by default) names
18based on the rules found therein. If one of the .excl files contains
19this:
20
21 + *.c
22 . -p .excl2
23 . .excl3
24 *.o
25
26Then the file ".excl2" will also be read in from the current dir and all
27its subdirs (due to the -p option). The file ".excl3" would just be
28read in from the current dir (because it was not specified with the -p
29option).
30
31..wayne..
32
33--- orig/clientserver.c 2004-08-02 02:29:16
34+++ clientserver.c 2004-08-09 17:35:56
35@@ -48,12 +48,14 @@ 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
43 char *auth_user;
44
45+/* The length of the lp_path() string (when we're not chrooted). */
46+unsigned int module_dirlen = 0;
47+
48 /**
49 * Run a client connected to an rsyncd. The alternative to this
50 * function for remote-shell connections is do_cmd().
51@@ -300,26 +302,28 @@ static int rsync_module(int f_in, int f_
52 /* TODO: Perhaps take a list of gids, and make them into the
53 * supplementary groups. */
54
55- exclude_path_prefix = use_chroot? "" : lp_path(i);
56- if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
57- exclude_path_prefix = "";
58+ if (use_chroot) {
59+ module_dirlen = 0;
60+ set_excludes_dir("/", 1);
61+ } else {
62+ module_dirlen = strlen(lp_path(i));
63+ set_excludes_dir(lp_path(i), module_dirlen);
64+ }
65
66 p = lp_include_from(i);
67 add_exclude_file(&server_exclude_list, p,
68- XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
69+ XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
70
71 p = lp_include(i);
72 add_exclude(&server_exclude_list, p,
73- XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
74+ XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
75
76 p = lp_exclude_from(i);
77 add_exclude_file(&server_exclude_list, p,
78- XFLG_FATAL_ERRORS);
79+ XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
80
81 p = lp_exclude(i);
82- add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
83-
84- exclude_path_prefix = NULL;
85+ add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
86
87 log_init();
88
89--- orig/exclude.c 2004-08-05 23:16:37
90+++ exclude.c 2004-08-09 18:36:18
91@@ -27,16 +27,75 @@
92 #include "rsync.h"
93
94 extern int verbose;
95+extern int am_sender;
96 extern int eol_nulls;
97 extern int list_only;
98 extern int recurse;
99+extern int io_error;
100+extern int module_id;
101+extern int delete_mode;
102+extern int delete_excluded;
103+extern int sanitize_paths;
104
105 extern char curr_dir[];
106+extern unsigned int curr_dir_len;
107+extern unsigned int module_dirlen;
108
109 struct exclude_list_struct exclude_list = { 0, 0, "" };
110-struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
111 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
112-char *exclude_path_prefix = NULL;
113+
114+struct mergelist_save_struct {
115+ struct exclude_list_struct *array;
116+ int count;
117+};
118+
119+/* The dirbuf is set by push_local_excludes() to the current subdirectory
120+ * relative to curr_dir that is being processed. The path always has a
121+ * trailing slash appended, and the variable dirbuf_len contains the length
122+ * of this path prefix. The path is always absolute. */
123+static char dirbuf[MAXPATHLEN+1];
124+static unsigned int dirbuf_len = 0;
125+
126+/* This is True when we're scanning parent dirs for per-dir merge-files. */
127+static BOOL parent_dirscan = False;
128+
129+/* This array contains a list of all the currently active per-dir merge
130+ * files. This makes it easier to save the appropriate values when we
131+ * "push" down into each subdirectory. */
132+static struct exclude_struct **mergelist_parents;
133+static int mergelist_cnt = 0;
134+static int mergelist_size = 0;
135+
136+/* Each exclude_list_struct describes a singly-linked list by keeping track
137+ * of both the head and tail pointers. The list is slightly unusual in that
138+ * a parent-dir's content can be appended to the end of the local list in a
139+ * special way: the last item in the local list has its "next" pointer set
140+ * to point to the inherited list, but the local list's tail pointer points
141+ * at the end of the local list. Thus, if the local list is empty, the head
142+ * will be pointing at the inherited content but the tail will be NULL. To
143+ * help you visualize this, here are the possible list arrangements:
144+ *
145+ * Completely Empty Local Content Only
146+ * ================================== ====================================
147+ * head -> NULL head -> Local1 -> Local2 -> NULL
148+ * tail -> NULL tail -------------^
149+ *
150+ * Inherited Content Only Both Local and Inherited Content
151+ * ================================== ====================================
152+ * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
153+ * tail -> NULL tail ---------^
154+ *
155+ * This means that anyone wanting to traverse the whole list to USE it just
156+ * needs to start at the head and use the "next" pointers until it goes
157+ * NULL. To add new local content, we insert the item after the tail item
158+ * and update the tail (obviously, if "tail" was NULL, we insert it at the
159+ * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
160+ * because it is shared between the current list and our parent list(s).
161+ * The easiest way to handle this is to simply truncate the list after the
162+ * tail item and then free the local list from the head. When inheriting
163+ * the list for a new local dir, we just save off the exclude_list_struct
164+ * values (so we can pop back to them later) and set the tail to NULL.
165+ */
166
167 /** Build an exclude structure given an exclude pattern. */
168 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
169@@ -46,23 +105,50 @@ static void make_exclude(struct exclude_
170 const char *cp;
171 unsigned int ex_len;
172
173+ if (verbose > 2) {
174+ rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
175+ who_am_i(), (int)pat_len, pat, listp->debug_type,
176+ mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
177+ mflags & MATCHFLG_INCLUDE ? "in" : "ex");
178+ }
179+
180+ if (mflags & MATCHFLG_MERGE_FILE) {
181+ int i;
182+ /* If the local include file was already mentioned, don't
183+ * add it again. */
184+ for (i = 0; i < mergelist_cnt; i++) {
185+ struct exclude_struct *ex = mergelist_parents[i];
186+ if (strlen(ex->pattern) == pat_len
187+ && memcmp(ex->pattern, pat, pat_len) == 0)
188+ return;
189+ }
190+ if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
191+ && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
192+ mflags |= MATCHFLG_CVSIGNORE;
193+ mflags &= ~MATCHFLG_INCLUDE;
194+ } else
195+ mflags &= ~MATCHFLG_CVSIGNORE;
196+ }
197+
198 ret = new(struct exclude_struct);
199 if (!ret)
200 out_of_memory("make_exclude");
201
202 memset(ret, 0, sizeof ret[0]);
203
204- if (exclude_path_prefix)
205- mflags |= MATCHFLG_ABS_PATH;
206- if (exclude_path_prefix && *pat == '/')
207- ex_len = strlen(exclude_path_prefix);
208- else
209+ if (mflags & MATCHFLG_ABS_PATH) {
210+ if (*pat != '/') {
211+ mflags &= ~MATCHFLG_ABS_PATH;
212+ ex_len = 0;
213+ } else
214+ ex_len = dirbuf_len - module_dirlen - 1;
215+ } else
216 ex_len = 0;
217 ret->pattern = new_array(char, ex_len + pat_len + 1);
218 if (!ret->pattern)
219 out_of_memory("make_exclude");
220 if (ex_len)
221- memcpy(ret->pattern, exclude_path_prefix, ex_len);
222+ strlcpy(ret->pattern, dirbuf + module_dirlen, ex_len + 1);
223 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
224 pat_len += ex_len;
225
226@@ -81,14 +167,40 @@ static void make_exclude(struct exclude_
227 mflags |= MATCHFLG_DIRECTORY;
228 }
229
230- for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
231- ret->slash_cnt++;
232+ if (mflags & MATCHFLG_MERGE_FILE) {
233+ struct exclude_list_struct *lp
234+ = new_array(struct exclude_list_struct, 1);
235+ if (!lp)
236+ out_of_memory("make_exclude");
237+ lp->head = lp->tail = NULL;
238+ if ((cp = strrchr(ret->pattern, '/')) != NULL)
239+ cp++;
240+ else
241+ cp = ret->pattern;
242+ if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
243+ out_of_memory("make_exclude");
244+ ret->u.mergelist = lp;
245+ if (mergelist_cnt == mergelist_size) {
246+ mergelist_size += 5;
247+ mergelist_parents = realloc_array(mergelist_parents,
248+ struct exclude_struct *,
249+ mergelist_size);
250+ if (!mergelist_parents)
251+ out_of_memory("make_exclude");
252+ }
253+ mergelist_parents[mergelist_cnt++] = ret;
254+ } else {
255+ for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
256+ ret->u.slash_cnt++;
257+ }
258
259 ret->match_flags = mflags;
260
261- if (!listp->tail)
262+ if (!listp->tail) {
263+ ret->next = listp->head;
264 listp->head = listp->tail = ret;
265- else {
266+ } else {
267+ ret->next = listp->tail->next;
268 listp->tail->next = ret;
269 listp->tail = ret;
270 }
271@@ -96,22 +208,265 @@ static void make_exclude(struct exclude_
272
273 static void free_exclude(struct exclude_struct *ex)
274 {
275+ if (ex->match_flags & MATCHFLG_MERGE_FILE) {
276+ free(ex->u.mergelist->debug_type);
277+ free(ex->u.mergelist);
278+ }
279 free(ex->pattern);
280 free(ex);
281 }
282
283-void clear_exclude_list(struct exclude_list_struct *listp)
284+static void clear_exclude_list(struct exclude_list_struct *listp)
285 {
286- struct exclude_struct *ent, *next;
287-
288- for (ent = listp->head; ent; ent = next) {
289- next = ent->next;
290- free_exclude(ent);
291+ if (listp->tail) {
292+ struct exclude_struct *ent, *next;
293+ /* Truncate any inherited items from the local list. */
294+ listp->tail->next = NULL;
295+ /* Now free everything that is left. */
296+ for (ent = listp->head; ent; ent = next) {
297+ next = ent->next;
298+ free_exclude(ent);
299+ }
300 }
301
302 listp->head = listp->tail = NULL;
303 }
304
305+/* This returns an expanded (absolute) filename for the merge-file name if
306+ * the name has any slashes in it OR if the parent_dirscan var is True;
307+ * otherwise it returns the original merge_file name. If the len_ptr value
308+ * is non-NULL the merge_file name is limited by the referenced length
309+ * value and will be updated with the length of the resulting name. We
310+ * always return a name that is null terminated, even if the merge_file
311+ * name was not. */
312+static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
313+ unsigned int prefix_skip)
314+{
315+ static char buf[MAXPATHLEN];
316+ char *fn, tmpbuf[MAXPATHLEN];
317+ unsigned int fn_len;
318+
319+ if (!parent_dirscan && *merge_file != '/') {
320+ /* Return the name unchanged it doesn't have any slashes. */
321+ if (len_ptr) {
322+ const char *p = merge_file + *len_ptr;
323+ while (--p > merge_file && *p != '/') {}
324+ if (p == merge_file) {
325+ strlcpy(buf, merge_file, *len_ptr + 1);
326+ return buf;
327+ }
328+ } else if (strchr(merge_file, '/') == NULL)
329+ return (char *)merge_file;
330+ }
331+
332+ fn = *merge_file == '/' ? buf : tmpbuf;
333+ if (sanitize_paths) {
334+ const char *r = prefix_skip ? "/" : NULL;
335+ /* null-terminate the name if it isn't already */
336+ if (len_ptr && merge_file[*len_ptr]) {
337+ char *to = fn == buf ? tmpbuf : buf;
338+ strlcpy(to, merge_file, *len_ptr + 1);
339+ merge_file = to;
340+ }
341+ if (!sanitize_path(fn, merge_file, r, dirbuf + module_dirlen)) {
342+ rprintf(FERROR, "merge-file name overflows: %s\n",
343+ merge_file);
344+ return NULL;
345+ }
346+ } else {
347+ strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
348+ clean_fname(fn);
349+ }
350+
351+ fn_len = strlen(fn);
352+ if (fn == buf)
353+ goto done;
354+
355+ if (dirbuf_len + fn_len >= MAXPATHLEN) {
356+ rprintf(FERROR, "merge-file name overflows: %s\n", fn);
357+ return NULL;
358+ }
359+ memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
360+ memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
361+ fn_len = clean_fname(buf);
362+
363+ done:
364+ if (len_ptr)
365+ *len_ptr = fn_len;
366+ return buf;
367+}
368+
369+/* Sets the dirbuf and dirbuf_len values. */
370+void set_excludes_dir(const char *dir, unsigned int dirlen)
371+{
372+ unsigned int len;
373+ if (*dir != '/') {
374+ memcpy(dirbuf, curr_dir, curr_dir_len);
375+ dirbuf[curr_dir_len] = '/';
376+ len = curr_dir_len + 1;
377+ if (dirlen >= MAXPATHLEN - len)
378+ dirlen = MAXPATHLEN - len - 1;
379+ } else
380+ len = 0;
381+ memcpy(dirbuf + len, dir, dirlen);
382+ dirbuf[dirlen + len] = '\0';
383+ dirbuf_len = clean_fname(dirbuf);
384+ if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
385+ && dirbuf[dirbuf_len-2] == '/')
386+ dirbuf_len -= 2;
387+ dirbuf[dirbuf_len++] = '/';
388+ dirbuf[dirbuf_len] = '\0';
389+}
390+
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)
398+{
399+ char buf[MAXPATHLEN];
400+ char *x, *y, *pat = ex->pattern;
401+ unsigned int len;
402+
403+ if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
404+ return 0;
405+
406+ y = strrchr(x, '/');
407+ *y = '\0';
408+ ex->pattern = strdup(y+1);
409+ if (!*x)
410+ x = "/";
411+ if (*x == '/')
412+ strlcpy(buf, x, MAXPATHLEN);
413+ else
414+ pathjoin(buf, MAXPATHLEN, dirbuf, x);
415+
416+ len = clean_fname(buf);
417+ if (len != 1 && len < MAXPATHLEN-1) {
418+ buf[len++] = '/';
419+ buf[len] = '\0';
420+ }
421+ /* This ensures that the specified dir is a parent of the transfer. */
422+ for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
423+ if (*x)
424+ y += strlen(y); /* nope -- skip the scan */
425+
426+ parent_dirscan = True;
427+ while (*y) {
428+ char save[MAXPATHLEN];
429+ strlcpy(save, y, MAXPATHLEN);
430+ *y = '\0';
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. */
436+ lp->tail = NULL;
437+ strlcpy(y, save, MAXPATHLEN);
438+ while ((*x++ = *y++) != '/') {}
439+ }
440+ parent_dirscan = False;
441+ free(pat);
442+ return 1;
443+}
444+
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)
450+{
451+ struct mergelist_save_struct *push;
452+ struct exclude_list_struct *ap;
453+ int i;
454+
455+ set_excludes_dir(dir, dirlen);
456+
457+ if (!(push = new_array(struct mergelist_save_struct, 1)))
458+ out_of_memory("push_local_excludes");
459+
460+ push->count = mergelist_cnt;
461+ push->array = new_array(struct exclude_list_struct, mergelist_cnt);
462+ if (!push->array)
463+ out_of_memory("push_local_excludes");
464+
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));
468+ }
469+
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;
475+ int flags;
476+
477+ if (verbose > 2) {
478+ rprintf(FINFO, "[%s] pushing %sexclude list\n",
479+ who_am_i(), lp->debug_type);
480+ }
481+
482+ if (ex->match_flags & MATCHFLG_CVSIGNORE) {
483+ lp->head = NULL; /* CVS doesn't inherit rules. */
484+ flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
485+ } else {
486+ flags = ex->match_flags & MATCHFLG_INCLUDE
487+ ? XFLG_DEF_INCLUDE : 0;
488+ }
489+ lp->tail = NULL; /* Switch any local rules to inherited. */
490+
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);
495+ }
496+
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);
500+ else {
501+ io_error |= IOERR_GENERAL;
502+ rprintf(FINFO,
503+ "cannot add local excludes in long-named directory %s\n",
504+ full_fname(dirbuf));
505+ }
506+ dirbuf[dirbuf_len] = '\0';
507+ }
508+
509+ return (void*)push;
510+}
511+
512+void pop_local_excludes(void *mem)
513+{
514+ struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
515+ struct exclude_list_struct *ap;
516+ int i;
517+
518+ for (i = mergelist_cnt; i-- > 0; ) {
519+ struct exclude_struct *ex = mergelist_parents[i];
520+ struct exclude_list_struct *lp = ex->u.mergelist;
521+
522+ if (verbose > 2) {
523+ rprintf(FINFO, "[%s] popping %sexclude list\n",
524+ who_am_i(), lp->debug_type);
525+ }
526+
527+ clear_exclude_list(lp);
528+ }
529+
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));
534+ }
535+
536+ free(pop->array);
537+ free(pop);
538+}
539+
540 static int check_one_exclude(char *name, struct exclude_struct *ex,
541 int name_is_dir)
542 {
543@@ -122,18 +477,20 @@ 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)
550 name = p+1;
551 }
552 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/') {
553 static char full_name[MAXPATHLEN];
554- int plus = curr_dir[1] == '\0'? 1 : 0;
555- pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
556+ char *cd = curr_dir + module_dirlen;
557+ int plus = cd[1] == '\0'? 1 : 0;
558+ pathjoin(full_name, sizeof full_name, cd+plus, name);
559 name = full_name;
560 }
561
562- if (!name[0]) return 0;
563+ if (!name[0])
564+ return 0;
565
566 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
567 return 0;
568@@ -148,9 +505,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)
579 break;
580@@ -221,6 +578,13 @@ int check_exclude(struct exclude_list_st
581 struct exclude_struct *ent;
582
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,
586+ name_is_dir);
587+ if (rc)
588+ return rc;
589+ continue;
590+ }
591 if (check_one_exclude(name, ent, name_is_dir)) {
592 report_exclude_result(name, ent, name_is_dir,
593 listp->debug_type);
594@@ -253,11 +617,36 @@ static const char *get_exclude_tok(const
595 p = (const char *)s;
596 }
597
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] == ' ') {
603 if (*s == '+')
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] == '-') {
610+ s += 2;
611+ do {
612+ switch (*++s) {
613+ case 'p':
614+ mflags |= MATCHFLG_PERDIR_MERGE
615+ | MATCHFLG_FINISH_SETUP;
616+ break;
617+ case '-':
618+ if (s[1] == ' ')
619+ goto done;
620+ default:
621+ rprintf(FERROR,
622+ "invalid merge options: %s\n",
623+ p);
624+ exit_cleanup(RERR_SYNTAX);
625+ }
626+ } while (s[1] != ' ');
627+ }
628+ }
629+ done:
630 s += 2;
631 } else if (xflags & XFLG_DEF_INCLUDE)
632 mflags |= MATCHFLG_INCLUDE;
633@@ -273,6 +662,8 @@ static const char *get_exclude_tok(const
634
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;
639
640 *len_ptr = len;
641 *flag_ptr = mflags;
642@@ -284,7 +675,7 @@ void add_exclude(struct exclude_list_str
643 int xflags)
644 {
645 unsigned int pat_len, mflags;
646- const char *cp;
647+ const char *cp, *p;
648
649 if (!pattern)
650 return;
651@@ -292,9 +683,15 @@ void add_exclude(struct exclude_list_str
652 cp = pattern;
653 pat_len = 0;
654 while (1) {
655+ /* Remember that the returned string is NOT '\0' terminated! */
656 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
657 if (!pat_len)
658 break;
659+ if (pat_len >= MAXPATHLEN) {
660+ rprintf(FERROR, "discarding over-long exclude: %s\n",
661+ cp);
662+ continue;
663+ }
664
665 if (mflags & MATCHFLG_CLEAR_LIST) {
666 if (verbose > 2) {
667@@ -306,13 +703,24 @@ void add_exclude(struct exclude_list_str
668 continue;
669 }
670
671- make_exclude(listp, cp, pat_len, mflags);
672-
673- if (verbose > 2) {
674- rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
675- who_am_i(), (int)pat_len, cp, listp->debug_type,
676- mflags & MATCHFLG_INCLUDE ? "in" : "ex");
677+ if (mflags & MATCHFLG_MERGE_FILE) {
678+ unsigned int len = pat_len;
679+ if (mflags & MATCHFLG_PERDIR_MERGE) {
680+ if (parent_dirscan) {
681+ if (!(p = parse_merge_name(cp, &len, module_dirlen)))
682+ continue;
683+ make_exclude(listp, p, len, mflags);
684+ continue;
685+ }
686+ } else {
687+ if (!(p = parse_merge_name(cp, &len, 0)))
688+ continue;
689+ add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
690+ continue;
691+ }
692 }
693+
694+ make_exclude(listp, cp, pat_len, mflags);
695 }
696 }
697
698@@ -321,7 +729,7 @@ void add_exclude_file(struct exclude_lis
699 int xflags)
700 {
701 FILE *fp;
702- char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
703+ char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
704 char *eob = line + sizeof line - 1;
705 int word_split = xflags & XFLG_WORD_SPLIT;
706
707@@ -342,6 +750,12 @@ void add_exclude_file(struct exclude_lis
708 }
709 return;
710 }
711+ dirbuf[dirbuf_len] = '\0';
712+
713+ if (verbose > 2) {
714+ rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
715+ who_am_i(), fname, xflags);
716+ }
717
718 while (1) {
719 char *s = line;
720@@ -402,7 +816,21 @@ void send_exclude_list(int f)
721 if (ent->match_flags & MATCHFLG_INCLUDE) {
722 write_int(f, l + 2);
723 write_buf(f, "+ ", 2);
724- } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
725+ } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
726+ char buf[32], *op = buf;
727+ *op++ = '.';
728+ *op++ = ' ';
729+ if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
730+ *op++ = '-';
731+ *op++ = 'p';
732+ if (*p == '-')
733+ *op++ = '-';
734+ *op++ = ' ';
735+ }
736+ write_int(f, l + (op - buf));
737+ write_buf(f, buf, op - buf);
738+ } else if ((*p == '-' || *p == '+' || *p == '.')
739+ && p[1] == ' ') {
740 write_int(f, l + 2);
741 write_buf(f, "- ", 2);
742 } else
743@@ -443,6 +871,7 @@ void add_cvs_excludes(void)
744 char fname[MAXPATHLEN];
745 char *p;
746
747+ add_exclude(&exclude_list, ". -p .cvsignore", 0);
748 add_exclude(&exclude_list, default_cvsignore,
749 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
750
751--- orig/flist.c 2004-08-05 21:57:29
752+++ flist.c 2004-08-09 18:21:55
753@@ -39,10 +39,9 @@ extern int module_id;
754 extern int ignore_errors;
755 extern int numeric_ids;
756
757-extern int cvs_exclude;
758-
759 extern int recurse;
760 extern char curr_dir[MAXPATHLEN];
761+extern unsigned int curr_dir_len;
762 extern char *files_from;
763 extern int filesfrom_fd;
764
765@@ -66,7 +65,6 @@ extern int list_only;
766
767 extern struct exclude_list_struct exclude_list;
768 extern struct exclude_list_struct server_exclude_list;
769-extern struct exclude_list_struct local_exclude_list;
770
771 int io_error;
772
773@@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
774 */
775 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
776 {
777- int rc;
778-
779 #if 0 /* This currently never happens, so avoid a useless compare. */
780 if (exclude_level == NO_EXCLUDES)
781 return 0;
782@@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
783 if (exclude_level != ALL_EXCLUDES)
784 return 0;
785 if (exclude_list.head
786- && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
787- return rc < 0;
788- if (local_exclude_list.head
789- && check_exclude(&local_exclude_list, fname, is_dir) < 0)
790+ && check_exclude(&exclude_list, fname, is_dir) < 0)
791 return 1;
792 return 0;
793 }
794@@ -573,7 +566,7 @@ void receive_file_entry(struct file_stru
795 clean_fname(thisname);
796
797 if (sanitize_paths)
798- sanitize_path(thisname, thisname, NULL);
799+ sanitize_path(thisname, thisname, "", NULL);
800
801 if ((basename = strrchr(thisname, '/')) != NULL) {
802 dirname_len = ++basename - thisname; /* counts future '\0' */
803@@ -671,7 +664,7 @@ void receive_file_entry(struct file_stru
804 file->u.link = bp;
805 read_sbuf(f, bp, linkname_len - 1);
806 if (sanitize_paths)
807- sanitize_path(bp, bp, lastdir);
808+ sanitize_path(bp, bp, "", lastdir);
809 bp += linkname_len;
810 }
811 #endif
812@@ -761,7 +754,7 @@ struct file_struct *make_file(char *fnam
813 }
814 clean_fname(thisname);
815 if (sanitize_paths)
816- sanitize_path(thisname, thisname, NULL);
817+ sanitize_path(thisname, thisname, "", NULL);
818
819 memset(sum, 0, SUM_LENGTH);
820
821@@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
822
823 if (recursive && S_ISDIR(file->mode)
824 && !(file->flags & FLAG_MOUNT_POINT)) {
825- struct exclude_list_struct last_list = local_exclude_list;
826- local_exclude_list.head = local_exclude_list.tail = NULL;
827 send_directory(f, flist, f_name_to(file, fbuf));
828- if (verbose > 2) {
829- rprintf(FINFO, "[%s] popping %sexclude list\n",
830- who_am_i(), local_exclude_list.debug_type);
831- }
832- clear_exclude_list(&local_exclude_list);
833- local_exclude_list = last_list;
834 }
835 }
836
837@@ -973,6 +958,7 @@ static void send_directory(int f, struct
838 struct dirent *di;
839 char fname[MAXPATHLEN];
840 unsigned int offset;
841+ void *save_excludes;
842 char *p;
843
844 d = opendir(dir);
845@@ -996,18 +982,7 @@ static void send_directory(int f, struct
846 offset++;
847 }
848
849- if (cvs_exclude) {
850- if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
851- < MAXPATHLEN - offset) {
852- add_exclude_file(&local_exclude_list, fname,
853- XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
854- } else {
855- io_error |= IOERR_GENERAL;
856- rprintf(FINFO,
857- "cannot cvs-exclude in long-named directory %s\n",
858- full_fname(fname));
859- }
860- }
861+ save_excludes = push_local_excludes(fname, offset);
862
863 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
864 char *dname = d_name(di);
865@@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
866 rsyserr(FERROR, errno, "readdir(%s)", dir);
867 }
868
869+ pop_local_excludes(save_excludes);
870+
871 closedir(d);
872 }
873
874@@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f,
875 char *p, *dir, olddir[sizeof curr_dir];
876 char lastpath[MAXPATHLEN] = "";
877 struct file_list *flist;
878+ BOOL need_first_push = True;
879 int64 start_write;
880 int use_ff_fd = 0;
881
882@@ -1067,6 +1045,10 @@ struct file_list *send_file_list(int f,
883 exit_cleanup(RERR_FILESELECT);
884 }
885 use_ff_fd = 1;
886+ if (curr_dir_len < MAXPATHLEN - 1) {
887+ push_local_excludes(curr_dir, curr_dir_len);
888+ need_first_push = False;
889+ }
890 }
891 }
892
893@@ -1077,13 +1059,13 @@ struct file_list *send_file_list(int f,
894 if (use_ff_fd) {
895 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
896 break;
897- sanitize_path(fname, fname, NULL);
898+ sanitize_path(fname, fname, "", NULL);
899 } else {
900 if (argc-- == 0)
901 break;
902 strlcpy(fname, *argv++, MAXPATHLEN);
903 if (sanitize_paths)
904- sanitize_path(fname, fname, NULL);
905+ sanitize_path(fname, fname, "", NULL);
906 }
907
908 l = strlen(fname);
909@@ -1097,6 +1079,15 @@ struct file_list *send_file_list(int f,
910 }
911 }
912
913+ if (need_first_push) {
914+ if ((p = strrchr(fname, '/')) != NULL) {
915+ if (*++p && strcmp(p, ".") != 0)
916+ push_local_excludes(fname, p - fname);
917+ } else if (strcmp(fname, ".") != 0)
918+ push_local_excludes(fname, 0);
919+ need_first_push = False;
920+ }
921+
922 if (link_stat(fname, &st, keep_dirlinks) != 0) {
923 if (f != -1) {
924 io_error |= IOERR_GENERAL;
925--- orig/options.c 2004-08-05 21:57:29
926+++ options.c 2004-08-09 18:22:26
927@@ -287,6 +287,7 @@ void usage(enum logcode F)
928 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
929 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
930 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
931+ rprintf(F," -E same as --exclude='. -p /.rsync-excludes'\n");
932 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
933 rprintf(F," --version print version number\n");
934 rprintf(F," --daemon run as an rsync daemon\n");
935@@ -389,6 +390,7 @@ static struct poptOption long_options[]
936 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
937 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
938 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
939+ {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
940 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
941 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
942 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
943@@ -589,6 +591,11 @@ int parse_arguments(int *argc, const cha
944 am_sender = 1;
945 break;
946
947+ case 'E':
948+ add_exclude(&exclude_list,
949+ ". -p /.rsync-excludes", 0);
950+ break;
951+
952 case 'P':
953 do_progress = 1;
954 keep_partial = 1;
955@@ -728,17 +735,17 @@ int parse_arguments(int *argc, const cha
956 if (sanitize_paths) {
957 int i;
958 for (i = *argc; i-- > 0; )
959- (*argv)[i] = sanitize_path(NULL, (*argv)[i], NULL);
960+ (*argv)[i] = sanitize_path(NULL, (*argv)[i], "", NULL);
961 if (tmpdir)
962- tmpdir = sanitize_path(NULL, tmpdir, "");
963+ tmpdir = sanitize_path(NULL, tmpdir, NULL, NULL);
964 if (partial_dir)
965- partial_dir = sanitize_path(NULL, partial_dir, "");
966+ partial_dir = sanitize_path(NULL, partial_dir, NULL, NULL);
967 if (compare_dest)
968- compare_dest = sanitize_path(NULL, compare_dest, "");
969+ compare_dest = sanitize_path(NULL, compare_dest, NULL, NULL);
970 if (backup_dir)
971- backup_dir = sanitize_path(NULL, backup_dir, "");
972+ backup_dir = sanitize_path(NULL, backup_dir, NULL, NULL);
973 if (files_from)
974- files_from = sanitize_path(NULL, files_from, "");
975+ files_from = sanitize_path(NULL, files_from, NULL, NULL);
976 }
977 if (server_exclude_list.head && !am_sender) {
978 struct exclude_list_struct *elp = &server_exclude_list;
979--- orig/rsync.h 2004-08-03 15:41:32
980+++ rsync.h 2004-08-08 06:07:01
981@@ -108,6 +108,7 @@
982 #define XFLG_DEF_INCLUDE (1<<1)
983 #define XFLG_WORDS_ONLY (1<<2)
984 #define XFLG_WORD_SPLIT (1<<3)
985+#define XFLG_ABS_PATH (1<<4)
986
987 #define PERMS_REPORT (1<<0)
988 #define PERMS_SKIP_MTIME (1<<1)
989@@ -499,11 +500,18 @@ struct map_struct {
990 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
991 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
992 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
993+#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
994+#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
995+#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
996+#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
997 struct exclude_struct {
998 struct exclude_struct *next;
999 char *pattern;
1000 unsigned int match_flags;
1001- int slash_cnt;
1002+ union {
1003+ int slash_cnt;
1004+ struct exclude_list_struct *mergelist;
1005+ } u;
1006 };
1007
1008 struct exclude_list_struct {
1009--- orig/rsync.yo 2004-08-03 15:34:32
1010+++ rsync.yo 2004-08-09 03:10:29
1011@@ -335,6 +335,7 @@ verb(
1012 --include=PATTERN don't exclude files matching PATTERN
1013 --include-from=FILE don't exclude patterns listed in FILE
1014 --files-from=FILE read FILE for list of source-file names
1015+ -E same as --exclude='. -p /.rsync-excludes'
1016 -0 --from0 all file lists are delimited by nulls
1017 --version print version number
1018 --daemon run as an rsync daemon
1019@@ -1086,6 +1087,11 @@ itemize(
1020 then it is always considered an exclude pattern, even if specified as
1021 part of an include option. The prefix is discarded before matching.
1022
1023+ it() if the pattern starts with ". " (a dot followed by a space) then its
1024+ pattern is taken to be a merge file that is read in to supplement the
1025+ current rules. See the section on MERGING EXCLUDE FILES for more
1026+ information.
1027+
1028 it() if the pattern is a single exclamation mark ! then the current
1029 include/exclude list is reset, removing all previously defined patterns.
1030 )
1031@@ -1138,6 +1144,104 @@ itemize(
1032 it would be excluded by the "*")
1033 )
1034
1035+manpagesection(MERGING EXCLUDE FILES)
1036+
1037+You can merge whole files into an exclude file by specifying a rule that
1038+starts with a ". " (a dot followed by a space) and putting a filename in
1039+place of the pattern. The filename may be preceded by one or more options:
1040+
1041+startdit()
1042+
1043+dit(bf(-p)) Make the file a per-directory merge-file. Rsync will scan
1044+every directory that it traverses for the named file, merging its contents
1045+when the file exists. (Without this option rsync just merges the rules into
1046+the parent file.)
1047+
1048+Rules are inherited in all subdirectories of the directory where the
1049+per-dir merge file was found. Each subdirectory's rules are prefixed to
1050+the inherited rules from a parent directory, which gives the newest rules a
1051+higher priority than the inherited rules. If you don't want a rule to be
1052+inherited, anchor it with a leading slash. Anchored rules in a
1053+per-directory merge file are relative to the current directory, so a rule
1054+"/foo" would only exclude the file "foo" in the current directory.
1055+
1056+Note also that you can eliminate all the inherited rules for a directory
1057+and its subdirectories by putting the list-clearing token (!) at the start
1058+of a per-directory file. This only clears the rules in the current
1059+sub-list, not all the rules.
1060+
1061+dit(bf(--)) End the scanning of options. Useful if you want to specify a
1062+filename that begins with a dash.
1063+
1064+enddit()
1065+
1066+Here's an example exclude file (which you'd specify via the normal
1067+--exclude-from=FILE option):
1068+
1069+verb(
1070+ . /home/user/.global_excludes
1071+ *.gz
1072+ . -p .excl
1073+ + *.[ch]
1074+ *.o
1075+)
1076+
1077+This will merge the contents of the /home/user/.global_excludes file at the
1078+start of the list and also turns the ".excl" filename into a per-directory
1079+exclude file. All the merged rules default to being exclude rules because
1080+an exclude statement was used to specify them.
1081+
1082+Note also that the parsing of any merge-file named ".cvsignore" is always
1083+done in a CVS-compatible manner, even if -C wasn't specified. This means
1084+that its rules are always excludes (even if an include option specified the
1085+file), tokens are split on whitespace, the rules are never inherited, and
1086+no special characters are honored (e.g. no comments, no "!", etc.).
1087+
1088+Additionally, you can affect where the --cvs-exclude (-C) option's
1089+inclusion of the per-directory .cvsignore file gets placed into your rules
1090+by adding your own explicit per-directory merge rule for ".cvsignore".
1091+Without this rsync would add its this rule at the end of all your other
1092+rules (giving it a lower priority than your command-line rules). For
1093+example, specifying this:
1094+
1095+verb(
1096+ rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1097+)
1098+
1099+will merge all the per-directory .cvsignore rules at the start of your list
1100+rather than at the end. This allows their dir-specific rules to supersede
1101+your rules instead of being subservient to them. (The global rules taken
1102+from the $HOME/.cvsignore file and from $CVSIGNORE are not repositioned by
1103+this.)
1104+
1105+If a per-directory merge file is specified with a path that is a parent
1106+directory of the first transfer directory, rsync will scan all the parent
1107+dirs from that starting point to the transfer directory for the indicated
1108+per-directory file. For instance, the -E option is an abbreviation for
1109+this command:
1110+
1111+verb(
1112+ --exclude='. -p /.rsync-excludes'
1113+)
1114+
1115+That exclude tells rsync to scan for the file .rsync-excludes in all
1116+directories from the root up through the source of the transfer. For an
1117+rsync daemon, the root dir is always the module's "path" setting, not the
1118+root of the filesystem (unless the two are the same).
1119+
1120+Some examples of this pre-scanning for per-directory files:
1121+
1122+verb(
1123+ rsync -avE /src/path/ /dest/dir
1124+ rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
1125+ rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
1126+)
1127+
1128+The first two commands above will look for .rsync-excludes in "/" and
1129+"/src" before the normal scan begins looking for the file in "/src/path"
1130+and its subdirectories. The last command above just starts scanning
1131+from "/src/path".
1132+
1133 manpagesection(BATCH MODE)
1134
1135 bf(Note:) Batch mode should be considered experimental in this version
1136--- orig/testsuite/exclude.test 2004-05-29 21:25:45
1137+++ testsuite/exclude.test 2004-08-08 06:35:15
1138@@ -23,19 +23,47 @@ export HOME CVSIGNORE
1139 makepath "$fromdir/foo/down/to/you"
1140 makepath "$fromdir/bar/down/to/foo/too"
1141 makepath "$fromdir/mid/for/foo/and/that/is/who"
1142+cat >"$fromdir/.excl" <<EOF
1143+.excl
1144+*.bak
1145+*.old
1146+*.junk
1147+EOF
1148 echo kept >"$fromdir/foo/file1"
1149 echo removed >"$fromdir/foo/file2"
1150 echo cvsout >"$fromdir/foo/file2.old"
1151+cat >"$fromdir/foo/.excl" <<EOF
1152++ .excl
1153+- file1
1154+EOF
1155+cat >"$fromdir/bar/.excl" <<EOF
1156+home-cvs-exclude
1157+. -p .excl2
1158++ to
1159+EOF
1160 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1161+cat >"$fromdir/bar/down/to/.excl2" <<EOF
1162+.excl2
1163+EOF
1164 echo keeper >"$fromdir/bar/down/to/foo/file1"
1165 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1166 echo gone >"$fromdir/bar/down/to/foo/file3"
1167 echo lost >"$fromdir/bar/down/to/foo/file4"
1168 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1169 echo smashed >"$fromdir/bar/down/to/foo/to"
1170+cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1171++ *.junk
1172+EOF
1173+# This one should be ineffectual
1174+cat >"$fromdir/mid/.excl2" <<EOF
1175+extra
1176+EOF
1177 echo cvsout >"$fromdir/mid/one-in-one-out"
1178 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1179 echo cvsin >"$fromdir/mid/one-for-all"
1180+cat >"$fromdir/mid/.excl" <<EOF
1181+. -p .cvsignore
1182+EOF
1183 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1184 echo expunged >"$fromdir/mid/for/foo/extra"
1185 echo retained >"$fromdir/mid/for/foo/keep"
1186@@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1187 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1188 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1189
1190+# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1191+
1192+rm "$chkdir"/.excl
1193+rm "$chkdir"/foo/file1
1194+rm "$chkdir"/bar/.excl
1195+rm "$chkdir"/bar/down/to/.excl2
1196+rm "$chkdir"/bar/down/to/foo/.excl2
1197+rm "$chkdir"/mid/.excl
1198+cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1199+cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1200+
1201+$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1202+
1203+# Now, test if rsync excludes the same files, this time with a merge-exclude
1204+# file.
1205+
1206+checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1207+ --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1208+
1209 # The script would have aborted on error, so getting here means we've won.
1210 exit 0
1211--- orig/util.c 2004-08-07 20:57:02
1212+++ util.c 2004-08-09 18:28:59
1213@@ -524,7 +524,7 @@ static void glob_expand_one(char *s, cha
1214 s = ".";
1215
1216 if (sanitize_paths)
1217- s = sanitize_path(NULL, s, NULL);
1218+ s = sanitize_path(NULL, s, "", NULL);
1219 else
1220 s = strdup(s);
1221
1222@@ -706,18 +706,16 @@ unsigned int clean_fname(char *name)
1223 * "/" (either removing it or expanding it) and any leading or embedded
1224 * ".." components that attempt to escape past the module's top dir.
1225 *
1226- * If dest is NULL, a buffer is allocated to hold the result. If dest is
1227- * the same buffer as p (the path) OR if reldir is NULL, a leading slash
1228- * is dropped instead of being expanded to be the module's top dir.
1229+ * If dest is NULL, a buffer is allocated to hold the result. It is legal
1230+ * to call with the dest and the path (p) pointing to the same buffer, but
1231+ * rootdir is ignored to avoid expansion of the string.
1232+ *
1233+ * The rootdir string contains a value to use in place of a leading slash.
1234+ * Specify NULL to get the default of lp_path(module_id).
1235 *
1236 * If reldir is non-NULL (and non-empty), it is a sanitized directory that
1237 * the path will be relative to, so allow as many '..'s at the beginning of
1238- * the path as there are components in reldir. This is used for symbolic
1239- * link targets. If reldir is non-null and the path began with "/", to be
1240- * completely like a chroot we should add in depth levels of ".." at the
1241- * beginning of the path, but that would blow the assumption that the path
1242- * doesn't grow and it is not likely to end up being a valid symlink
1243- * anyway, so just do the normal removal of the leading "/" instead.
1244+ * the path as there are components in reldir.
1245 *
1246 * While we're at it, remove double slashes and "." components like
1247 * clean_fname() does, but DON'T remove a trailing slash because that is
1248@@ -725,7 +723,8 @@ unsigned int clean_fname(char *name)
1249 *
1250 * If the resulting path would be empty, change it into ".".
1251 */
1252-char *sanitize_path(char *dest, const char *p, const char *reldir)
1253+char *sanitize_path(char *dest, const char *p, const char *rootdir,
1254+ const char *reldir)
1255 {
1256 char *start, *sanp;
1257 int depth = 0;
1258@@ -734,8 +733,10 @@ char *sanitize_path(char *dest, const ch
1259
1260 if (dest != p) {
1261 int plen = strlen(p);
1262- if (*p == '/' && reldir) {
1263- rlen = strlen(lp_path(module_id));
1264+ if (*p == '/') {
1265+ if (!rootdir)
1266+ rootdir = lp_path(module_id);
1267+ rlen = strlen(rootdir);
1268 reldir = NULL;
1269 p++;
1270 }
1271@@ -745,7 +746,7 @@ char *sanitize_path(char *dest, const ch
1272 } else if (!(dest = new_array(char, rlen + plen + 1)))
1273 out_of_memory("sanitize_path");
1274 if (rlen) {
1275- memcpy(dest, lp_path(module_id), rlen);
1276+ memcpy(dest, rootdir, rlen);
1277 if (rlen > 1)
1278 dest[rlen++] = '/';
1279 }