Need to make sure that the destination file doesn't exist before we
[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 /foobar
26
27Then the file ".excl2" will also be read in from the current dir and all
28its subdirs (due to the -p option). The file ".excl3" would just be
29read in from the current dir. The exclusion of "foobar" will only
30happen in that .excl file's directory because the rule is anchored (so
31that's how you can make rules local instead of inherited).
32
33..wayne..
34
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;
44
45 char *auth_user;
46
47+/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
48+unsigned int module_dirlen = 0;
49+
50 /**
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. */
56
57- exclude_path_prefix = use_chroot? "" : lp_path(i);
58- if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
59- exclude_path_prefix = "";
60+ if (use_chroot) {
61+ module_dirlen = 0;
62+ set_excludes_dir("/", 1);
63+ } else {
64+ module_dirlen = strlen(lp_path(i));
65+ set_excludes_dir(lp_path(i), module_dirlen);
66+ }
67
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);
72
73 p = lp_include(i);
74 add_exclude(&server_exclude_list, p,
75- XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
76+ XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
77
78 p = lp_exclude_from(i);
79 add_exclude_file(&server_exclude_list, p,
80- XFLG_FATAL_ERRORS);
81+ XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
82
83 p = lp_exclude(i);
84- add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
85-
86- exclude_path_prefix = NULL;
87+ add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
88
89 log_init();
90
91--- orig/exclude.c 2004-08-10 18:17:01
92+++ exclude.c 2004-08-13 07:40:08
93@@ -30,13 +30,69 @@ extern int verbose;
94 extern int eol_nulls;
95 extern int list_only;
96 extern int recurse;
97+extern int io_error;
98+extern int sanitize_paths;
99
100 extern char curr_dir[];
101+extern unsigned int curr_dir_len;
102+extern unsigned int module_dirlen;
103
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;
108+
109+struct mergelist_save_struct {
110+ struct exclude_list_struct *array;
111+ int count;
112+};
113+
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;
121+
122+/* This is True when we're scanning parent dirs for per-dir merge-files. */
123+static BOOL parent_dirscan = False;
124+
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;
131+
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:
140+ *
141+ * Completely Empty Local Content Only
142+ * ================================== ====================================
143+ * head -> NULL head -> Local1 -> Local2 -> NULL
144+ * tail -> NULL tail -------------^
145+ *
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 ---------^
150+ *
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.
161+ */
162
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_
166 const char *cp;
167 unsigned int ex_len;
168
169+ if (verbose > 2) {
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");
174+ }
175+
176+ if (mflags & MATCHFLG_MERGE_FILE) {
177+ int i;
178+ /* If the local include file was already mentioned, don't
179+ * add it again. */
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)
184+ return;
185+ }
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;
190+ } else
191+ mflags &= ~MATCHFLG_CVSIGNORE;
192+ }
193+
194 ret = new(struct exclude_struct);
195 if (!ret)
196 out_of_memory("make_exclude");
197
198 memset(ret, 0, sizeof ret[0]);
199
200- if (exclude_path_prefix)
201- mflags |= MATCHFLG_ABS_PATH;
202- if (exclude_path_prefix && *pat == '/')
203- ex_len = strlen(exclude_path_prefix);
204- else
205+ if (mflags & MATCHFLG_ABS_PATH) {
206+ if (*pat != '/') {
207+ mflags &= ~MATCHFLG_ABS_PATH;
208+ ex_len = 0;
209+ } else
210+ ex_len = dirbuf_len - module_dirlen - 1;
211+ } else
212 ex_len = 0;
213 ret->pattern = new_array(char, ex_len + pat_len + 1);
214 if (!ret->pattern)
215 out_of_memory("make_exclude");
216 if (ex_len)
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);
220 pat_len += ex_len;
221
222@@ -81,14 +164,40 @@ static void make_exclude(struct exclude_
223 mflags |= MATCHFLG_DIRECTORY;
224 }
225
226- for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
227- ret->slash_cnt++;
228+ if (mflags & MATCHFLG_MERGE_FILE) {
229+ struct exclude_list_struct *lp
230+ = new_array(struct exclude_list_struct, 1);
231+ if (!lp)
232+ out_of_memory("make_exclude");
233+ lp->head = lp->tail = NULL;
234+ if ((cp = strrchr(ret->pattern, '/')) != NULL)
235+ cp++;
236+ else
237+ cp = ret->pattern;
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 *,
245+ mergelist_size);
246+ if (!mergelist_parents)
247+ out_of_memory("make_exclude");
248+ }
249+ mergelist_parents[mergelist_cnt++] = ret;
250+ } else {
251+ for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
252+ ret->u.slash_cnt++;
253+ }
254
255 ret->match_flags = mflags;
256
257- if (!listp->tail)
258+ if (!listp->tail) {
259+ ret->next = listp->head;
260 listp->head = listp->tail = ret;
261- else {
262+ } else {
263+ ret->next = listp->tail->next;
264 listp->tail->next = ret;
265 listp->tail = ret;
266 }
267@@ -96,22 +205,267 @@ static void make_exclude(struct exclude_
268
269 static void free_exclude(struct exclude_struct *ex)
270 {
271+ if (ex->match_flags & MATCHFLG_MERGE_FILE) {
272+ free(ex->u.mergelist->debug_type);
273+ free(ex->u.mergelist);
274+ }
275 free(ex->pattern);
276 free(ex);
277 }
278
279-void clear_exclude_list(struct exclude_list_struct *listp)
280+static void clear_exclude_list(struct exclude_list_struct *listp)
281 {
282- struct exclude_struct *ent, *next;
283-
284- for (ent = listp->head; ent; ent = next) {
285- next = ent->next;
286- free_exclude(ent);
287+ if (listp->tail) {
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) {
293+ next = ent->next;
294+ free_exclude(ent);
295+ }
296 }
297
298 listp->head = listp->tail = NULL;
299 }
300
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
307+ * name was not. */
308+static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
309+ unsigned int prefix_skip)
310+{
311+ static char buf[MAXPATHLEN];
312+ char *fn, tmpbuf[MAXPATHLEN];
313+ unsigned int fn_len;
314+
315+ if (!parent_dirscan && *merge_file != '/') {
316+ /* Return the name unchanged it doesn't have any slashes. */
317+ if (len_ptr) {
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);
322+ return buf;
323+ }
324+ } else if (strchr(merge_file, '/') == NULL)
325+ return (char *)merge_file;
326+ }
327+
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);
335+ merge_file = to;
336+ }
337+ if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
338+ rprintf(FERROR, "merge-file name overflows: %s\n",
339+ merge_file);
340+ return NULL;
341+ }
342+ } else {
343+ strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
344+ clean_fname(fn, 1);
345+ }
346+
347+ fn_len = strlen(fn);
348+ if (fn == buf)
349+ goto done;
350+
351+ if (dirbuf_len + fn_len >= MAXPATHLEN) {
352+ rprintf(FERROR, "merge-file name overflows: %s\n", fn);
353+ return NULL;
354+ }
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);
358+
359+ done:
360+ if (len_ptr)
361+ *len_ptr = fn_len;
362+ return buf;
363+}
364+
365+/* Sets the dirbuf and dirbuf_len values. */
366+void set_excludes_dir(const char *dir, unsigned int dirlen)
367+{
368+ unsigned int len;
369+ if (*dir != '/') {
370+ memcpy(dirbuf, curr_dir, curr_dir_len);
371+ dirbuf[curr_dir_len] = '/';
372+ len = curr_dir_len + 1;
373+ if (len + dirlen >= MAXPATHLEN)
374+ dirlen = 0;
375+ } else
376+ len = 0;
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] == '/')
382+ 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);
387+}
388+
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)
396+{
397+ char buf[MAXPATHLEN];
398+ char *x, *y, *pat = ex->pattern;
399+ unsigned int len;
400+
401+ if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
402+ return 0;
403+
404+ y = strrchr(x, '/');
405+ *y = '\0';
406+ ex->pattern = strdup(y+1);
407+ if (!*x)
408+ x = "/";
409+ if (*x == '/')
410+ strlcpy(buf, x, MAXPATHLEN);
411+ else
412+ pathjoin(buf, MAXPATHLEN, dirbuf, x);
413+
414+ len = clean_fname(buf, 1);
415+ if (len != 1 && len < MAXPATHLEN-1) {
416+ buf[len++] = '/';
417+ buf[len] = '\0';
418+ }
419+ /* This ensures that the specified dir is a parent of the transfer. */
420+ for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
421+ if (*x)
422+ y += strlen(y); /* nope -- skip the scan */
423+
424+ parent_dirscan = True;
425+ while (*y) {
426+ char save[MAXPATHLEN];
427+ strlcpy(save, y, MAXPATHLEN);
428+ *y = '\0';
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. */
434+ lp->tail = NULL;
435+ strlcpy(y, save, MAXPATHLEN);
436+ while ((*x++ = *y++) != '/') {}
437+ }
438+ parent_dirscan = False;
439+ free(pat);
440+ return 1;
441+}
442+
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)
448+{
449+ struct mergelist_save_struct *push;
450+ struct exclude_list_struct *ap;
451+ int i;
452+
453+ set_excludes_dir(dir, dirlen);
454+
455+ if (!(push = new_array(struct mergelist_save_struct, 1)))
456+ out_of_memory("push_local_excludes");
457+
458+ push->count = mergelist_cnt;
459+ push->array = new_array(struct exclude_list_struct, mergelist_cnt);
460+ if (!push->array)
461+ out_of_memory("push_local_excludes");
462+
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));
466+ }
467+
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;
473+ int flags;
474+
475+ if (verbose > 2) {
476+ rprintf(FINFO, "[%s] pushing %sexclude list\n",
477+ who_am_i(), lp->debug_type);
478+ }
479+
480+ if (ex->match_flags & MATCHFLG_CVSIGNORE) {
481+ lp->head = NULL; /* CVS doesn't inherit rules. */
482+ flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
483+ } else {
484+ flags = ex->match_flags & MATCHFLG_INCLUDE
485+ ? XFLG_DEF_INCLUDE : 0;
486+ }
487+ lp->tail = NULL; /* Switch any local rules to inherited. */
488+
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);
493+ }
494+
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);
498+ else {
499+ io_error |= IOERR_GENERAL;
500+ rprintf(FINFO,
501+ "cannot add local excludes in long-named directory %s\n",
502+ full_fname(dirbuf));
503+ }
504+ dirbuf[dirbuf_len] = '\0';
505+ }
506+
507+ return (void*)push;
508+}
509+
510+void pop_local_excludes(void *mem)
511+{
512+ struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
513+ struct exclude_list_struct *ap;
514+ int i;
515+
516+ for (i = mergelist_cnt; i-- > 0; ) {
517+ struct exclude_struct *ex = mergelist_parents[i];
518+ struct exclude_list_struct *lp = ex->u.mergelist;
519+
520+ if (verbose > 2) {
521+ rprintf(FINFO, "[%s] popping %sexclude list\n",
522+ who_am_i(), lp->debug_type);
523+ }
524+
525+ clear_exclude_list(lp);
526+ }
527+
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));
532+ }
533+
534+ free(pop->array);
535+ free(pop);
536+}
537+
538 static int check_one_exclude(char *name, struct exclude_struct *ex,
539 int name_is_dir)
540 {
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)
548 name = p+1;
549 }
550 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/'
551- && curr_dir[1]) {
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);
556 name = full_name;
557 }
558
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)
570 break;
571@@ -221,6 +576,13 @@ int check_exclude(struct exclude_list_st
572 struct exclude_struct *ent;
573
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,
577+ name_is_dir);
578+ if (rc)
579+ return rc;
580+ continue;
581+ }
582 if (check_one_exclude(name, ent, name_is_dir)) {
583 report_exclude_result(name, ent, name_is_dir,
584 listp->debug_type);
585@@ -253,11 +615,36 @@ static const char *get_exclude_tok(const
586 p = (const char *)s;
587 }
588
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] == ' ') {
594 if (*s == '+')
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] == '-') {
601+ s += 2;
602+ do {
603+ switch (*++s) {
604+ case 'p':
605+ mflags |= MATCHFLG_PERDIR_MERGE
606+ | MATCHFLG_FINISH_SETUP;
607+ break;
608+ case '-':
609+ if (s[1] == ' ')
610+ goto done;
611+ default:
612+ rprintf(FERROR,
613+ "invalid merge options: %s\n",
614+ p);
615+ exit_cleanup(RERR_SYNTAX);
616+ }
617+ } while (s[1] != ' ');
618+ }
619+ }
620+ done:
621 s += 2;
622 } else if (xflags & XFLG_DEF_INCLUDE)
623 mflags |= MATCHFLG_INCLUDE;
624@@ -273,6 +660,8 @@ static const char *get_exclude_tok(const
625
626 if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
627 mflags |= MATCHFLG_CLEAR_LIST;
628+ if (xflags & XFLG_ABS_PATH)
629+ mflags |= MATCHFLG_ABS_PATH;
630
631 *len_ptr = len;
632 *flag_ptr = mflags;
633@@ -284,7 +673,7 @@ void add_exclude(struct exclude_list_str
634 int xflags)
635 {
636 unsigned int pat_len, mflags;
637- const char *cp;
638+ const char *cp, *p;
639
640 if (!pattern)
641 return;
642@@ -292,9 +681,15 @@ void add_exclude(struct exclude_list_str
643 cp = pattern;
644 pat_len = 0;
645 while (1) {
646+ /* Remember that the returned string is NOT '\0' terminated! */
647 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
648 if (!pat_len)
649 break;
650+ if (pat_len >= MAXPATHLEN) {
651+ rprintf(FERROR, "discarding over-long exclude: %s\n",
652+ cp);
653+ continue;
654+ }
655
656 if (mflags & MATCHFLG_CLEAR_LIST) {
657 if (verbose > 2) {
658@@ -306,13 +701,24 @@ void add_exclude(struct exclude_list_str
659 continue;
660 }
661
662- make_exclude(listp, cp, pat_len, mflags);
663-
664- if (verbose > 2) {
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)))
673+ continue;
674+ make_exclude(listp, p, len, mflags);
675+ continue;
676+ }
677+ } else {
678+ if (!(p = parse_merge_name(cp, &len, 0)))
679+ continue;
680+ add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
681+ continue;
682+ }
683 }
684+
685+ make_exclude(listp, cp, pat_len, mflags);
686 }
687 }
688
689@@ -321,7 +727,7 @@ void add_exclude_file(struct exclude_lis
690 int xflags)
691 {
692 FILE *fp;
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;
697
698@@ -342,6 +748,12 @@ void add_exclude_file(struct exclude_lis
699 }
700 return;
701 }
702+ dirbuf[dirbuf_len] = '\0';
703+
704+ if (verbose > 2) {
705+ rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
706+ who_am_i(), fname, xflags);
707+ }
708
709 while (1) {
710 char *s = line;
711@@ -402,7 +814,21 @@ void send_exclude_list(int f)
712 if (ent->match_flags & MATCHFLG_INCLUDE) {
713 write_int(f, l + 2);
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;
718+ *op++ = '.';
719+ *op++ = ' ';
720+ if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
721+ *op++ = '-';
722+ *op++ = 'p';
723+ if (*p == '-')
724+ *op++ = '-';
725+ *op++ = ' ';
726+ }
727+ write_int(f, l + (op - buf));
728+ write_buf(f, buf, op - buf);
729+ } else if ((*p == '-' || *p == '+' || *p == '.')
730+ && p[1] == ' ') {
731 write_int(f, l + 2);
732 write_buf(f, "- ", 2);
733 } else
734@@ -443,6 +869,7 @@ void add_cvs_excludes(void)
735 char fname[MAXPATHLEN];
736 char *p;
737
738+ add_exclude(&exclude_list, ". -p .cvsignore", 0);
739 add_exclude(&exclude_list, default_cvsignore,
740 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
741
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;
747
748-extern int cvs_exclude;
749-
750 extern int recurse;
751 extern char curr_dir[MAXPATHLEN];
752+extern unsigned int curr_dir_len;
753 extern char *files_from;
754 extern int filesfrom_fd;
755
756@@ -67,7 +66,6 @@ extern int list_only;
757
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;
761
762 int io_error;
763
764@@ -223,8 +221,6 @@ int link_stat(const char *path, STRUCT_S
765 */
766 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
767 {
768- int rc;
769-
770 #if 0 /* This currently never happens, so avoid a useless compare. */
771 if (exclude_level == NO_EXCLUDES)
772 return 0;
773@@ -246,10 +242,7 @@ static int check_exclude_file(char *fnam
774 if (exclude_level != ALL_EXCLUDES)
775 return 0;
776 if (exclude_list.head
777- && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
778- return rc < 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)
782 return 1;
783 return 0;
784 }
785@@ -978,15 +971,7 @@ void send_file_name(int f, struct file_l
786
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));
792- if (verbose > 2) {
793- rprintf(FINFO, "[%s] popping %sexclude list\n",
794- who_am_i(), local_exclude_list.debug_type);
795- }
796- clear_exclude_list(&local_exclude_list);
797- local_exclude_list = last_list;
798 }
799 }
800
801@@ -997,6 +982,7 @@ static void send_directory(int f, struct
802 struct dirent *di;
803 char fname[MAXPATHLEN];
804 unsigned int offset;
805+ void *save_excludes;
806 char *p;
807
808 d = opendir(dir);
809@@ -1020,18 +1006,7 @@ static void send_directory(int f, struct
810 offset++;
811 }
812
813- if (cvs_exclude) {
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);
818- } else {
819- io_error |= IOERR_GENERAL;
820- rprintf(FINFO,
821- "cannot cvs-exclude in long-named directory %s\n",
822- full_fname(fname));
823- }
824- }
825+ save_excludes = push_local_excludes(fname, offset);
826
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);
831 }
832
833+ pop_local_excludes(save_excludes);
834+
835 closedir(d);
836 }
837
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;
843 int64 start_write;
844 int use_ff_fd = 0;
845
846@@ -1091,6 +1069,10 @@ struct file_list *send_file_list(int f,
847 exit_cleanup(RERR_FILESELECT);
848 }
849 use_ff_fd = 1;
850+ if (curr_dir_len < MAXPATHLEN - 1) {
851+ push_local_excludes(curr_dir, curr_dir_len);
852+ need_first_push = False;
853+ }
854 }
855 }
856
857@@ -1121,6 +1103,15 @@ struct file_list *send_file_list(int f,
858 }
859 }
860
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;
868+ }
869+
870 if (link_stat(fname, &st, keep_dirlinks) != 0) {
871 if (f != -1) {
872 io_error |= IOERR_GENERAL;
873--- orig/options.c 2004-09-20 05:10:48
874+++ options.c 2004-08-12 18:59:28
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," --daemon run as an rsync daemon\n");
883@@ -389,6 +390,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 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
890 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
891@@ -585,6 +587,11 @@ int parse_arguments(int *argc, const cha
892 am_sender = 1;
893 break;
894
895+ case 'E':
896+ add_exclude(&exclude_list,
897+ ". -p /.rsync-excludes", 0);
898+ break;
899+
900 case 'P':
901 do_progress = 1;
902 keep_partial = 1;
903--- orig/rsync.h 2004-08-03 15:41:32
904+++ rsync.h 2004-08-08 06:07:01
905@@ -108,6 +108,7 @@
906 #define XFLG_DEF_INCLUDE (1<<1)
907 #define XFLG_WORDS_ONLY (1<<2)
908 #define XFLG_WORD_SPLIT (1<<3)
909+#define XFLG_ABS_PATH (1<<4)
910
911 #define PERMS_REPORT (1<<0)
912 #define PERMS_SKIP_MTIME (1<<1)
913@@ -499,11 +500,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;
923 char *pattern;
924 unsigned int match_flags;
925- int slash_cnt;
926+ union {
927+ int slash_cnt;
928+ struct exclude_list_struct *mergelist;
929+ } u;
930 };
931
932 struct exclude_list_struct {
933--- orig/rsync.yo 2004-09-20 05:10:48
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@@ -1025,24 +1026,32 @@ The exclude and include patterns specifi
944 selection of which files to transfer and which files to skip.
945
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
949+the command line.
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
953+files).
954+
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.
961
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).
974
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.
981
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@@ -1089,23 +1098,27 @@ because rsync did not descend through th
985 hierarchy.
986
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.
992
993-The patterns can take several forms. The rules are:
994+The include/exclude patterns can take several forms. The rules are:
995
996 itemize(
997
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
1000- the filename.
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
1014+ the
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
1019+ named "sub".
1020
1021 it() if the pattern ends with a / then it will only match a
1022 directory, not a file, link, or device.
1023@@ -1118,22 +1131,31 @@ itemize(
1024 single asterisk pattern "*" will stop at slashes.
1025
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.
1034
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.)
1039
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.)
1044+
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
1048+ information.
1049
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).
1056 )
1057
1058 The +/- rules are most useful in a list that was read from a file, allowing
1059@@ -1180,8 +1202,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 "*")
1067+)
1068+
1069+manpagesection(MERGED EXCLUDE FILES)
1070+
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:
1076+
1077+startdit()
1078+
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).
1086+
1087+dit(bf(--)) End the scanning of options. Useful if you want to specify a
1088+filename that begins with a dash.
1089+
1090+enddit()
1091+
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.
1099+
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.
1104+
1105+Here's an example exclude file which you'd specify via the normal
1106+--exclude-from=FILE option:
1107+
1108+verb(
1109+ . /home/user/.global_excludes
1110+ *.gz
1111+ . -p .excl
1112+ + *.[ch]
1113+ *.o
1114+)
1115+
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.
1121+
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
1126+this command:
1127+
1128+verb(
1129+ --exclude='. -p /.rsync-excludes'
1130+)
1131+
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.)
1135+
1136+Some examples of this pre-scanning for per-directory files:
1137+
1138+verb(
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
1142+)
1143+
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.
1149+
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 "!",
1155+etc.).
1156+
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
1162+example:
1163+
1164+verb(
1165+ rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1166+)
1167+
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.)
1173+
1174+manpagesection(PER-DIRECTORY EXCLUDES AND DELETE)
1175+
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:
1179+
1180+verb(
1181+ rsync -av --exclude='. -p .excl' --exclude=.excl host:src/dir /dest
1182+)
1183+
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:
1190+
1191+verb(
1192+ rsync -avE --delete-after host:src/dir /dest
1193+)
1194+
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):
1200+
1201+verb(
1202+ rsync -av --exclude='. -p .ctrl' --exclude-from=/my/extra.rules
1203+ --delete host:src/dir /dest
1204+)
1205+
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.
1209+
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:
1216+
1217+verb(
1218+ rsync -avE --exclude=.rsync-excludes --delete host:src/dir /dest
1219 )
1220
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
1229+.excl
1230+*.bak
1231+*.old
1232+*.junk
1233+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
1238++ .excl
1239+- file1
1240+EOF
1241+cat >"$fromdir/bar/.excl" <<EOF
1242+home-cvs-exclude
1243+. -p .excl2
1244++ to
1245+EOF
1246 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1247+cat >"$fromdir/bar/down/to/.excl2" <<EOF
1248+.excl2
1249+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
1257++ *.junk
1258+EOF
1259+# This one should be ineffectual
1260+cat >"$fromdir/mid/.excl2" <<EOF
1261+extra
1262+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
1267+. -p .cvsignore
1268+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"
1275
1276+# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1277+
1278+rm "$chkdir"/.excl
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
1286+
1287+$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1288+
1289+# Now, test if rsync excludes the same files, this time with a merge-exclude
1290+# file.
1291+
1292+checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1293+ --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1294+
1295 # The script would have aborted on error, so getting here means we've won.
1296 exit 0