- Changed the way that daemon excludes get anchored to the module dir
[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 02:25:28
35@@ -48,9 +48,10 @@ extern int no_detach;
36 extern int default_af_hint;
37 extern char *bind_address;
38 extern struct exclude_list_struct server_exclude_list;
39-extern char *exclude_path_prefix;
40 extern char *config_file;
41 extern char *files_from;
42+extern char dirbuf[MAXPATHLEN];
43+extern unsigned int dirbuf_offset;
44
45 char *auth_user;
46
47@@ -300,26 +301,27 @@ static int rsync_module(int f_in, int f_
48 /* TODO: Perhaps take a list of gids, and make them into the
49 * supplementary groups. */
50
51- exclude_path_prefix = use_chroot? "" : lp_path(i);
52- if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
53- exclude_path_prefix = "";
54+ if (use_chroot)
55+ set_current_subdir("", 0);
56+ else
57+ set_current_subdir(lp_path(i), strlen(lp_path(i)));
58
59 p = lp_include_from(i);
60 add_exclude_file(&server_exclude_list, p,
61- XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
62+ XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
63
64 p = lp_include(i);
65 add_exclude(&server_exclude_list, p,
66- XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
67+ XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
68
69 p = lp_exclude_from(i);
70 add_exclude_file(&server_exclude_list, p,
71- XFLG_FATAL_ERRORS);
72+ XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
73
74 p = lp_exclude(i);
75- add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
76+ add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
77
78- exclude_path_prefix = NULL;
79+ set_current_subdir("", 0);
80
81 log_init();
82
83--- orig/exclude.c 2004-08-05 23:16:37
84+++ exclude.c 2004-08-09 03:19:40
85@@ -27,16 +27,80 @@
86 #include "rsync.h"
87
88 extern int verbose;
89+extern int am_sender;
90 extern int eol_nulls;
91 extern int list_only;
92 extern int recurse;
93+extern int io_error;
94+extern int module_id;
95+extern int delete_mode;
96+extern int delete_excluded;
97+extern int sanitize_paths;
98
99 extern char curr_dir[];
100+extern unsigned int curr_dir_len;
101
102 struct exclude_list_struct exclude_list = { 0, 0, "" };
103-struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
104 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
105-char *exclude_path_prefix = NULL;
106+
107+struct mergelist_save_struct {
108+ struct exclude_list_struct *array;
109+ int count;
110+};
111+
112+/* The dirbuf is set by push_local_excludes() to the current subdirectory
113+ * relative to curr_dir that is being processed. The path always has a
114+ * trailing slash appended, and the variable dirbuf_offset contains the
115+ * length of this path prefix (i.e. it is an offset where you can copy a
116+ * filename to create a pathname that can be opened for reading/writing.
117+ *
118+ * The path is normally relative (e.g. "sub/dir/foo"), but it is set to an
119+ * absolute path when the push code is working on a parent-dir scan of dirs
120+ * that might be higher than the root of the transfer. In that case, the
121+ * path is absolute, and any newly-created per-dir merge files will (at
122+ * least temporarily) get an absolute file name so that we know at what
123+ * point in the hierarchy it first makes an appearance. */
124+static char dirbuf[MAXPATHLEN];
125+static unsigned int dirbuf_offset = 0;
126+static BOOL parent_dirscan = 0;
127+
128+/* This array contains a list of all the currently active per-dir merge
129+ * files. This makes it easier to save the appropriate values when we
130+ * "push" down into each subdirectory. */
131+static struct exclude_struct **mergelist_parents;
132+static int mergelist_cnt = 0;
133+static int mergelist_size = 0;
134+
135+/* Each exclude_list_struct describes a singly-linked list by keeping track
136+ * of both the head and tail pointers. The list is slightly unusual in that
137+ * a parent-dir's content can be appended to the end of the local list in a
138+ * special way: the last item in the local list has its "next" pointer set
139+ * to point to the inherited list, but the local list's tail pointer points
140+ * at the end of the local list. Thus, if the local list is empty, the head
141+ * will be pointing at the inherited content but the tail will be NULL. To
142+ * help you visualize this, here are the possible list arrangements:
143+ *
144+ * Completely Empty Local Content Only
145+ * ================================== ====================================
146+ * head -> NULL head -> Local1 -> Local2 -> NULL
147+ * tail -> NULL tail -------------^
148+ *
149+ * Inherited Content Only Both Local and Inherited Content
150+ * ================================== ====================================
151+ * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
152+ * tail -> NULL tail ---------^
153+ *
154+ * This means that anyone wanting to traverse the whole list to USE it just
155+ * needs to start at the head and use the "next" pointers until it goes
156+ * NULL. To add new local content, we insert the item after the tail item
157+ * and update the tail (obviously, if "tail" was NULL, we insert it at the
158+ * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
159+ * because it is shared between the current list and our parent list(s).
160+ * The easiest way to handle this is to simply truncate the list after the
161+ * tail item and then free the local list from the head. When inheriting
162+ * the list for a new local dir, we just save off the exclude_list_struct
163+ * values (so we can pop back to them later) and set the tail to NULL.
164+ */
165
166 /** Build an exclude structure given an exclude pattern. */
167 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
168@@ -46,23 +110,56 @@ static void make_exclude(struct exclude_
169 const char *cp;
170 unsigned int ex_len;
171
172+ if (verbose > 2) {
173+ rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
174+ who_am_i(), (int)pat_len, pat, listp->debug_type,
175+ mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
176+ mflags & MATCHFLG_INCLUDE ? "in" : "ex");
177+ }
178+
179+ if (mflags & MATCHFLG_MERGE_FILE) {
180+ int i;
181+ /* If the local include file was already mentioned, don't
182+ * add it again. */
183+ for (i = 0; i < mergelist_cnt; i++) {
184+ struct exclude_struct *ex = mergelist_parents[i];
185+ if (strlen(ex->pattern) == pat_len
186+ && memcmp(ex->pattern, pat, pat_len) == 0)
187+ return;
188+ }
189+ if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
190+ && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
191+ mflags |= MATCHFLG_CVSIGNORE;
192+ mflags &= ~MATCHFLG_INCLUDE;
193+ } else
194+ mflags &= ~MATCHFLG_CVSIGNORE;
195+ }
196+
197 ret = new(struct exclude_struct);
198 if (!ret)
199 out_of_memory("make_exclude");
200
201 memset(ret, 0, sizeof ret[0]);
202
203- if (exclude_path_prefix)
204- mflags |= MATCHFLG_ABS_PATH;
205- if (exclude_path_prefix && *pat == '/')
206- ex_len = strlen(exclude_path_prefix);
207- else
208+ if (mflags & MATCHFLG_ABS_PATH) {
209+ if (*pat != '/') {
210+ mflags &= ~MATCHFLG_ABS_PATH;
211+ ex_len = 0;
212+ } else {
213+ ex_len = *dirbuf == '/' ? dirbuf_offset - 1
214+ : curr_dir_len + dirbuf_offset - !curr_dir[1];
215+ }
216+ } else
217 ex_len = 0;
218 ret->pattern = new_array(char, ex_len + pat_len + 1);
219 if (!ret->pattern)
220 out_of_memory("make_exclude");
221- if (ex_len)
222- memcpy(ret->pattern, exclude_path_prefix, ex_len);
223+ if (ex_len) {
224+ if (*dirbuf == '/')
225+ strlcpy(ret->pattern, dirbuf, ex_len + 1);
226+ else
227+ pathjoin(ret->pattern, ex_len + 1, curr_dir, dirbuf);
228+ }
229 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
230 pat_len += ex_len;
231
232@@ -81,14 +178,40 @@ static void make_exclude(struct exclude_
233 mflags |= MATCHFLG_DIRECTORY;
234 }
235
236- for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
237- ret->slash_cnt++;
238+ if (mflags & MATCHFLG_MERGE_FILE) {
239+ struct exclude_list_struct *lp
240+ = new_array(struct exclude_list_struct, 1);
241+ if (!lp)
242+ out_of_memory("make_exclude");
243+ lp->head = lp->tail = NULL;
244+ if ((cp = strrchr(ret->pattern, '/')) != NULL)
245+ cp++;
246+ else
247+ cp = ret->pattern;
248+ if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
249+ out_of_memory("make_exclude");
250+ ret->u.mergelist = lp;
251+ if (mergelist_cnt == mergelist_size) {
252+ mergelist_size += 5;
253+ mergelist_parents = realloc_array(mergelist_parents,
254+ struct exclude_struct *,
255+ mergelist_size);
256+ if (!mergelist_parents)
257+ out_of_memory("make_exclude");
258+ }
259+ mergelist_parents[mergelist_cnt++] = ret;
260+ } else {
261+ for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
262+ ret->u.slash_cnt++;
263+ }
264
265 ret->match_flags = mflags;
266
267- if (!listp->tail)
268+ if (!listp->tail) {
269+ ret->next = listp->head;
270 listp->head = listp->tail = ret;
271- else {
272+ } else {
273+ ret->next = listp->tail->next;
274 listp->tail->next = ret;
275 listp->tail = ret;
276 }
277@@ -96,22 +219,265 @@ static void make_exclude(struct exclude_
278
279 static void free_exclude(struct exclude_struct *ex)
280 {
281+ if (ex->match_flags & MATCHFLG_MERGE_FILE) {
282+ free(ex->u.mergelist->debug_type);
283+ free(ex->u.mergelist);
284+ }
285 free(ex->pattern);
286 free(ex);
287 }
288
289-void clear_exclude_list(struct exclude_list_struct *listp)
290+static void clear_exclude_list(struct exclude_list_struct *listp)
291 {
292- struct exclude_struct *ent, *next;
293-
294- for (ent = listp->head; ent; ent = next) {
295- next = ent->next;
296- free_exclude(ent);
297+ if (listp->tail) {
298+ struct exclude_struct *ent, *next;
299+ /* Truncate any inherited items from the local list. */
300+ listp->tail->next = NULL;
301+ /* Now free everything that is left. */
302+ for (ent = listp->head; ent; ent = next) {
303+ next = ent->next;
304+ free_exclude(ent);
305+ }
306 }
307
308 listp->head = listp->tail = NULL;
309 }
310
311+/* This returns an expanded (absolute) filename for the merge-file name if
312+ * the name has any slashes in it OR if the parent_dirscan var is non-zero;
313+ * otherwise it returns the original merge_file name. If the len_ptr value
314+ * is non-NULL the merge_file name is not null terminated and the length
315+ * value is contained therein (and will be updated with the new length). We
316+ * always return a name that is null terminated. */
317+static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr)
318+{
319+ static char buf[MAXPATHLEN];
320+ char *fn, tmpbuf[MAXPATHLEN];
321+ unsigned int fn_len, cd_len;
322+
323+ cd_len = *dirbuf == '/' ? 0 : curr_dir_len + 1;
324+ if (!parent_dirscan && *merge_file != '/') {
325+ /* Return the name unchanged it doesn't have any slashes. */
326+ if (len_ptr) {
327+ const char *p = merge_file + *len_ptr;
328+ while (--p > merge_file && *p != '/') {}
329+ if (p == merge_file) {
330+ strlcpy(buf, merge_file, *len_ptr + 1);
331+ return buf;
332+ }
333+ } else if (strchr(merge_file, '/') == NULL)
334+ return (char *)merge_file;
335+ }
336+
337+ fn = *merge_file == '/' ? buf : tmpbuf;
338+ if (sanitize_paths) {
339+ /* null-terminate the name if it isn't already */
340+ if (len_ptr && merge_file[*len_ptr]) {
341+ char *to = fn == buf ? tmpbuf : buf;
342+ strlcpy(to, merge_file, *len_ptr + 1);
343+ merge_file = to;
344+ }
345+ if (!sanitize_path(fn, merge_file, dirbuf)) {
346+ rprintf(FERROR, "merge filename overflows: %s\n",
347+ merge_file);
348+ return NULL;
349+ }
350+ } else {
351+ strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
352+ clean_fname(fn);
353+ }
354+
355+ if (*fn == '/')
356+ return fn;
357+
358+ fn_len = strlen(fn);
359+ if (cd_len + dirbuf_offset + fn_len >= MAXPATHLEN) {
360+ rprintf(FERROR, "merge filename overflows: %s\n", fn);
361+ return NULL;
362+ }
363+ if (cd_len) {
364+ memcpy(buf, curr_dir, curr_dir_len);
365+ buf[curr_dir_len] = '/';
366+ }
367+ if (dirbuf_offset)
368+ memcpy(buf + cd_len, dirbuf, dirbuf_offset);
369+ memcpy(buf + cd_len + dirbuf_offset, fn, fn_len + 1);
370+ fn_len = clean_fname(buf);
371+ if (len_ptr)
372+ *len_ptr = fn_len;
373+
374+ return buf;
375+}
376+
377+/* Sets the dirbuf and dirbuf_offset values */
378+void set_current_subdir(const char *dir, unsigned int dirlen)
379+{
380+ memcpy(dirbuf, dir, dirlen);
381+ dirbuf[dirlen] = '\0';
382+ dirbuf_offset = clean_fname(dirbuf);
383+ if (dirbuf_offset == 1 && *dirbuf == '.')
384+ dirbuf_offset = 0;
385+ else
386+ dirbuf[dirbuf_offset++] = '/';
387+ dirbuf[dirbuf_offset] = '\0';
388+}
389+
390+/* This routine takes a per-dir merge file entry and finishes its setup.
391+ * If the name has a path portion then we check to see if it refers to a
392+ * parent directory of the first transfer dir. If it does, we scan all the
393+ * dirs from that point through the parent dir of the transfer dir looking
394+ * for the per-dir merge file in each one. */
395+static void setup_merge_file(struct exclude_struct *ex,
396+ struct exclude_list_struct *lp, int flags,
397+ const char *dir, unsigned int dirlen)
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)) || *x != '/')
404+ return;
405+
406+ y = strrchr(x, '/');
407+ *y = '\0';
408+ ex->pattern = strdup(y+1);
409+ if (*dirbuf != '/') {
410+ pathjoin(dirbuf, MAXPATHLEN, sanitize_paths ? "/" : curr_dir,
411+ dir);
412+ }
413+ if (!*x)
414+ x = "/";
415+ if (*x == '/')
416+ strlcpy(buf, x, MAXPATHLEN);
417+ else
418+ pathjoin(buf, MAXPATHLEN, dirbuf, x);
419+
420+ len = clean_fname(buf);
421+ if (len != 1 && len < MAXPATHLEN-1) {
422+ buf[len++] = '/';
423+ buf[len] = '\0';
424+ }
425+ x = buf;
426+ if (sanitize_paths)
427+ x += strlen(lp_path(module_id));
428+ /* This ensures that the specified dir is a parent of the transfer. */
429+ for (y = dirbuf; *x && *x == *y; x++, y++) {}
430+ if (*x)
431+ y += strlen(y); /* nope -- skip the scan */
432+
433+ parent_dirscan = 1;
434+ while (*y) {
435+ char save[MAXPATHLEN];
436+ strlcpy(save, y, MAXPATHLEN);
437+ *y = '\0';
438+ dirbuf_offset = y - dirbuf;
439+ strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
440+ add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
441+ if (ex->match_flags & MATCHFLG_CVSIGNORE)
442+ lp->head = NULL; /* CVS doesn't inherit rules. */
443+ lp->tail = NULL;
444+ strlcpy(y, save, MAXPATHLEN);
445+ while ((*x++ = *y++) != '/') {}
446+ }
447+ parent_dirscan = 0;
448+ free(pat);
449+ set_current_subdir(dir, dirlen);
450+}
451+
452+/* Each time rsync changes to a new directory it call this function to
453+ * handle all the per-dir merge files. The "dir" value is the current path
454+ * relative to curr_dir (with a mandatory trailing slash). We copy it into
455+ * dirbuf so that the routines this calls can append a file name. */
456+void *push_local_excludes(const char *dir, unsigned int dirlen)
457+{
458+ struct mergelist_save_struct *push;
459+ struct exclude_list_struct *ap;
460+ int i;
461+
462+ set_current_subdir(dir, dirlen);
463+
464+ if (!(push = new_array(struct mergelist_save_struct, 1)))
465+ out_of_memory("push_local_excludes");
466+
467+ push->count = mergelist_cnt;
468+ push->array = new_array(struct exclude_list_struct, mergelist_cnt);
469+ if (!push->array)
470+ out_of_memory("push_local_excludes");
471+
472+ for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
473+ memcpy(ap++, mergelist_parents[i]->u.mergelist,
474+ sizeof (struct exclude_list_struct));
475+ }
476+
477+ /* Note: add_exclude_file() might increase mergelist_cnt, so keep
478+ * this loop separate from the above loop. */
479+ for (i = 0; i < mergelist_cnt; i++) {
480+ struct exclude_struct *ex = mergelist_parents[i];
481+ struct exclude_list_struct *lp = ex->u.mergelist;
482+ int flags;
483+
484+ if (verbose > 2) {
485+ rprintf(FINFO, "[%s] pushing %sexclude list\n",
486+ who_am_i(), lp->debug_type);
487+ }
488+
489+ if (ex->match_flags & MATCHFLG_CVSIGNORE) {
490+ lp->head = NULL; /* CVS doesn't inherit rules. */
491+ flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
492+ } else {
493+ flags = ex->match_flags & MATCHFLG_INCLUDE
494+ ? XFLG_DEF_INCLUDE : 0;
495+ }
496+ lp->tail = NULL; /* Switch any local rules to inherited. */
497+
498+ if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
499+ ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
500+ setup_merge_file(ex, lp, flags, dir, dirlen);
501+ }
502+
503+ if (strlcpy(dirbuf + dirbuf_offset, ex->pattern,
504+ MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
505+ add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
506+ else {
507+ io_error |= IOERR_GENERAL;
508+ rprintf(FINFO,
509+ "cannot add local excludes in long-named directory %s\n",
510+ full_fname(dirbuf));
511+ }
512+ dirbuf[dirbuf_offset] = '\0';
513+ }
514+
515+ return (void*)push;
516+}
517+
518+void pop_local_excludes(void *mem)
519+{
520+ struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
521+ struct exclude_list_struct *ap;
522+ int i;
523+
524+ for (i = mergelist_cnt; i-- > 0; ) {
525+ struct exclude_struct *ex = mergelist_parents[i];
526+ struct exclude_list_struct *lp = ex->u.mergelist;
527+
528+ if (verbose > 2) {
529+ rprintf(FINFO, "[%s] popping %sexclude list\n",
530+ who_am_i(), lp->debug_type);
531+ }
532+
533+ clear_exclude_list(lp);
534+ }
535+
536+ mergelist_cnt = pop->count;
537+ for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
538+ memcpy(mergelist_parents[i]->u.mergelist, ap++,
539+ sizeof (struct exclude_list_struct));
540+ }
541+
542+ free(pop->array);
543+ free(pop);
544+}
545+
546 static int check_one_exclude(char *name, struct exclude_struct *ex,
547 int name_is_dir)
548 {
549@@ -122,7 +488,7 @@ static int check_one_exclude(char *name,
550 /* If the pattern does not have any slashes AND it does not have
551 * a "**" (which could match a slash), then we just match the
552 * name portion of the path. */
553- if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
554+ if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
555 if ((p = strrchr(name,'/')) != NULL)
556 name = p+1;
557 }
558@@ -133,7 +499,8 @@ static int check_one_exclude(char *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 +515,9 @@ static int check_one_exclude(char *name,
569 if (ex->match_flags & MATCHFLG_WILD) {
570 /* A non-anchored match with an infix slash and no "**"
571 * needs to match the last slash_cnt+1 name elements. */
572- if (!match_start && ex->slash_cnt
573+ if (!match_start && ex->u.slash_cnt
574 && !(ex->match_flags & MATCHFLG_WILD2)) {
575- int cnt = ex->slash_cnt + 1;
576+ int cnt = ex->u.slash_cnt + 1;
577 for (p = name + strlen(name) - 1; p >= name; p--) {
578 if (*p == '/' && !--cnt)
579 break;
580@@ -221,6 +588,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 +627,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 +672,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@@ -292,9 +693,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 +713,23 @@ 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+ if (mflags & MATCHFLG_PERDIR_MERGE) {
670+ if (parent_dirscan) {
671+ if (!(cp = parse_merge_name(cp, &pat_len)))
672+ continue;
673+ make_exclude(listp, cp, pat_len, mflags);
674+ continue;
675+ }
676+ } else {
677+ if (!(cp = parse_merge_name(cp, &pat_len)))
678+ continue;
679+ add_exclude_file(listp, cp, xflags | XFLG_FATAL_ERRORS);
680+ continue;
681+ }
682 }
683+
684+ make_exclude(listp, cp, pat_len, mflags);
685 }
686 }
687
688@@ -321,7 +738,7 @@ void add_exclude_file(struct exclude_lis
689 int xflags)
690 {
691 FILE *fp;
692- char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
693+ char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
694 char *eob = line + sizeof line - 1;
695 int word_split = xflags & XFLG_WORD_SPLIT;
696
697@@ -343,6 +760,11 @@ void add_exclude_file(struct exclude_lis
698 return;
699 }
700
701+ if (verbose > 2) {
702+ rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
703+ who_am_i(), fname, xflags);
704+ }
705+
706 while (1) {
707 char *s = line;
708 int ch, overflow = 0;
709@@ -402,7 +824,21 @@ void send_exclude_list(int f)
710 if (ent->match_flags & MATCHFLG_INCLUDE) {
711 write_int(f, l + 2);
712 write_buf(f, "+ ", 2);
713- } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
714+ } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
715+ char buf[32], *op = buf;
716+ *op++ = '.';
717+ *op++ = ' ';
718+ if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
719+ *op++ = '-';
720+ *op++ = 'p';
721+ if (*p == '-')
722+ *op++ = '-';
723+ *op++ = ' ';
724+ }
725+ write_int(f, l + (op - buf));
726+ write_buf(f, buf, op - buf);
727+ } else if ((*p == '-' || *p == '+' || *p == '.')
728+ && p[1] == ' ') {
729 write_int(f, l + 2);
730 write_buf(f, "- ", 2);
731 } else
732@@ -443,6 +879,7 @@ void add_cvs_excludes(void)
733 char fname[MAXPATHLEN];
734 char *p;
735
736+ add_exclude(&exclude_list, ". -p .cvsignore", 0);
737 add_exclude(&exclude_list, default_cvsignore,
738 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
739
740--- orig/flist.c 2004-08-05 21:57:29
741+++ flist.c 2004-08-09 02:45:42
742@@ -39,10 +39,9 @@ extern int module_id;
743 extern int ignore_errors;
744 extern int numeric_ids;
745
746-extern int cvs_exclude;
747-
748 extern int recurse;
749 extern char curr_dir[MAXPATHLEN];
750+extern unsigned int curr_dir_len;
751 extern char *files_from;
752 extern int filesfrom_fd;
753
754@@ -66,7 +65,6 @@ extern int list_only;
755
756 extern struct exclude_list_struct exclude_list;
757 extern struct exclude_list_struct server_exclude_list;
758-extern struct exclude_list_struct local_exclude_list;
759
760 int io_error;
761
762@@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
763 */
764 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
765 {
766- int rc;
767-
768 #if 0 /* This currently never happens, so avoid a useless compare. */
769 if (exclude_level == NO_EXCLUDES)
770 return 0;
771@@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
772 if (exclude_level != ALL_EXCLUDES)
773 return 0;
774 if (exclude_list.head
775- && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
776- return rc < 0;
777- if (local_exclude_list.head
778- && check_exclude(&local_exclude_list, fname, is_dir) < 0)
779+ && check_exclude(&exclude_list, fname, is_dir) < 0)
780 return 1;
781 return 0;
782 }
783@@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
784
785 if (recursive && S_ISDIR(file->mode)
786 && !(file->flags & FLAG_MOUNT_POINT)) {
787- struct exclude_list_struct last_list = local_exclude_list;
788- local_exclude_list.head = local_exclude_list.tail = NULL;
789 send_directory(f, flist, f_name_to(file, fbuf));
790- if (verbose > 2) {
791- rprintf(FINFO, "[%s] popping %sexclude list\n",
792- who_am_i(), local_exclude_list.debug_type);
793- }
794- clear_exclude_list(&local_exclude_list);
795- local_exclude_list = last_list;
796 }
797 }
798
799@@ -973,6 +958,7 @@ static void send_directory(int f, struct
800 struct dirent *di;
801 char fname[MAXPATHLEN];
802 unsigned int offset;
803+ void *save_excludes;
804 char *p;
805
806 d = opendir(dir);
807@@ -996,18 +982,7 @@ static void send_directory(int f, struct
808 offset++;
809 }
810
811- if (cvs_exclude) {
812- if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
813- < MAXPATHLEN - offset) {
814- add_exclude_file(&local_exclude_list, fname,
815- XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
816- } else {
817- io_error |= IOERR_GENERAL;
818- rprintf(FINFO,
819- "cannot cvs-exclude in long-named directory %s\n",
820- full_fname(fname));
821- }
822- }
823+ save_excludes = push_local_excludes(fname, offset);
824
825 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
826 char *dname = d_name(di);
827@@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
828 rsyserr(FERROR, errno, "readdir(%s)", dir);
829 }
830
831+ pop_local_excludes(save_excludes);
832+
833 closedir(d);
834 }
835
836@@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f,
837 char *p, *dir, olddir[sizeof curr_dir];
838 char lastpath[MAXPATHLEN] = "";
839 struct file_list *flist;
840+ BOOL did_first_push = 0;
841 int64 start_write;
842 int use_ff_fd = 0;
843
844@@ -1067,6 +1045,10 @@ struct file_list *send_file_list(int f,
845 exit_cleanup(RERR_FILESELECT);
846 }
847 use_ff_fd = 1;
848+ if (curr_dir_len < MAXPATHLEN - 1) {
849+ push_local_excludes(curr_dir, curr_dir_len);
850+ did_first_push = 1;
851+ }
852 }
853 }
854
855@@ -1097,6 +1079,16 @@ struct file_list *send_file_list(int f,
856 }
857 }
858
859+ if (!did_first_push) {
860+ if ((p = strrchr(fname, '/')) != NULL) {
861+ dir = fname;
862+ p++;
863+ } else
864+ dir = p = "";
865+ push_local_excludes(dir, p - dir);
866+ did_first_push = 1;
867+ }
868+
869 if (link_stat(fname, &st, keep_dirlinks) != 0) {
870 if (f != -1) {
871 io_error |= IOERR_GENERAL;
872--- orig/options.c 2004-08-05 21:57:29
873+++ options.c 2004-08-09 03:33:31
874@@ -287,6 +287,7 @@ void usage(enum logcode F)
875 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
876 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
877 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
878+ rprintf(F," -E same as --exclude='. -p /.rsync-excludes'\n");
879 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
880 rprintf(F," --version print version number\n");
881 rprintf(F," --daemon run as an rsync daemon\n");
882@@ -389,6 +390,7 @@ static struct poptOption long_options[]
883 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
884 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
885 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
886+ {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
887 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
888 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
889 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
890@@ -589,6 +591,11 @@ int parse_arguments(int *argc, const cha
891 am_sender = 1;
892 break;
893
894+ case 'E':
895+ add_exclude(&exclude_list,
896+ ". -p /.rsync-excludes", 0);
897+ break;
898+
899 case 'P':
900 do_progress = 1;
901 keep_partial = 1;
902--- orig/rsync.h 2004-08-03 15:41:32
903+++ rsync.h 2004-08-08 06:07:01
904@@ -108,6 +108,7 @@
905 #define XFLG_DEF_INCLUDE (1<<1)
906 #define XFLG_WORDS_ONLY (1<<2)
907 #define XFLG_WORD_SPLIT (1<<3)
908+#define XFLG_ABS_PATH (1<<4)
909
910 #define PERMS_REPORT (1<<0)
911 #define PERMS_SKIP_MTIME (1<<1)
912@@ -499,11 +500,18 @@ struct map_struct {
913 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
914 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
915 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
916+#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
917+#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
918+#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
919+#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
920 struct exclude_struct {
921 struct exclude_struct *next;
922 char *pattern;
923 unsigned int match_flags;
924- int slash_cnt;
925+ union {
926+ int slash_cnt;
927+ struct exclude_list_struct *mergelist;
928+ } u;
929 };
930
931 struct exclude_list_struct {
932--- orig/rsync.yo 2004-08-03 15:34:32
933+++ rsync.yo 2004-08-09 03:10:29
934@@ -335,6 +335,7 @@ verb(
935 --include=PATTERN don't exclude files matching PATTERN
936 --include-from=FILE don't exclude patterns listed in FILE
937 --files-from=FILE read FILE for list of source-file names
938+ -E same as --exclude='. -p /.rsync-excludes'
939 -0 --from0 all file lists are delimited by nulls
940 --version print version number
941 --daemon run as an rsync daemon
942@@ -1086,6 +1087,11 @@ itemize(
943 then it is always considered an exclude pattern, even if specified as
944 part of an include option. The prefix is discarded before matching.
945
946+ it() if the pattern starts with ". " (a dot followed by a space) then its
947+ pattern is taken to be a merge file that is read in to supplement the
948+ current rules. See the section on MERGING EXCLUDE FILES for more
949+ information.
950+
951 it() if the pattern is a single exclamation mark ! then the current
952 include/exclude list is reset, removing all previously defined patterns.
953 )
954@@ -1138,6 +1144,104 @@ itemize(
955 it would be excluded by the "*")
956 )
957
958+manpagesection(MERGING EXCLUDE FILES)
959+
960+You can merge whole files into an exclude file by specifying a rule that
961+starts with a ". " (a dot followed by a space) and putting a filename in
962+place of the pattern. The filename may be preceded by one or more options:
963+
964+startdit()
965+
966+dit(bf(-p)) Make the file a per-directory merge-file. Rsync will scan
967+every directory that it traverses for the named file, merging its contents
968+when the file exists. (Without this option rsync just merges the rules into
969+the parent file.)
970+
971+Rules are inherited in all subdirectories of the directory where the
972+per-dir merge file was found. Each subdirectory's rules are prefixed to
973+the inherited rules from a parent directory, which gives the newest rules a
974+higher priority than the inherited rules. If you don't want a rule to be
975+inherited, anchor it with a leading slash. Anchored rules in a
976+per-directory merge file are relative to the current directory, so a rule
977+"/foo" would only exclude the file "foo" in the current directory.
978+
979+Note also that you can eliminate all the inherited rules for a directory
980+and its subdirectories by putting the list-clearing token (!) at the start
981+of a per-directory file. This only clears the rules in the current
982+sub-list, not all the rules.
983+
984+dit(bf(--)) End the scanning of options. Useful if you want to specify a
985+filename that begins with a dash.
986+
987+enddit()
988+
989+Here's an example exclude file (which you'd specify via the normal
990+--exclude-from=FILE option):
991+
992+verb(
993+ . /home/user/.global_excludes
994+ *.gz
995+ . -p .excl
996+ + *.[ch]
997+ *.o
998+)
999+
1000+This will merge the contents of the /home/user/.global_excludes file at the
1001+start of the list and also turns the ".excl" filename into a per-directory
1002+exclude file. All the merged rules default to being exclude rules because
1003+an exclude statement was used to specify them.
1004+
1005+Note also that the parsing of any merge-file named ".cvsignore" is always
1006+done in a CVS-compatible manner, even if -C wasn't specified. This means
1007+that its rules are always excludes (even if an include option specified the
1008+file), tokens are split on whitespace, the rules are never inherited, and
1009+no special characters are honored (e.g. no comments, no "!", etc.).
1010+
1011+Additionally, you can affect where the --cvs-exclude (-C) option's
1012+inclusion of the per-directory .cvsignore file gets placed into your rules
1013+by adding your own explicit per-directory merge rule for ".cvsignore".
1014+Without this rsync would add its this rule at the end of all your other
1015+rules (giving it a lower priority than your command-line rules). For
1016+example, specifying this:
1017+
1018+verb(
1019+ rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1020+)
1021+
1022+will merge all the per-directory .cvsignore rules at the start of your list
1023+rather than at the end. This allows their dir-specific rules to supersede
1024+your rules instead of being subservient to them. (The global rules taken
1025+from the $HOME/.cvsignore file and from $CVSIGNORE are not repositioned by
1026+this.)
1027+
1028+If a per-directory merge file is specified with a path that is a parent
1029+directory of the first transfer directory, rsync will scan all the parent
1030+dirs from that starting point to the transfer directory for the indicated
1031+per-directory file. For instance, the -E option is an abbreviation for
1032+this command:
1033+
1034+verb(
1035+ --exclude='. -p /.rsync-excludes'
1036+)
1037+
1038+That exclude tells rsync to scan for the file .rsync-excludes in all
1039+directories from the root up through the source of the transfer. For an
1040+rsync daemon, the root dir is always the module's "path" setting, not the
1041+root of the filesystem (unless the two are the same).
1042+
1043+Some examples of this pre-scanning for per-directory files:
1044+
1045+verb(
1046+ rsync -avE /src/path/ /dest/dir
1047+ rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
1048+ rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
1049+)
1050+
1051+The first two commands above will look for .rsync-excludes in "/" and
1052+"/src" before the normal scan begins looking for the file in "/src/path"
1053+and its subdirectories. The last command above just starts scanning
1054+from "/src/path".
1055+
1056 manpagesection(BATCH MODE)
1057
1058 bf(Note:) Batch mode should be considered experimental in this version
1059--- orig/testsuite/exclude.test 2004-05-29 21:25:45
1060+++ testsuite/exclude.test 2004-08-08 06:35:15
1061@@ -23,19 +23,47 @@ export HOME CVSIGNORE
1062 makepath "$fromdir/foo/down/to/you"
1063 makepath "$fromdir/bar/down/to/foo/too"
1064 makepath "$fromdir/mid/for/foo/and/that/is/who"
1065+cat >"$fromdir/.excl" <<EOF
1066+.excl
1067+*.bak
1068+*.old
1069+*.junk
1070+EOF
1071 echo kept >"$fromdir/foo/file1"
1072 echo removed >"$fromdir/foo/file2"
1073 echo cvsout >"$fromdir/foo/file2.old"
1074+cat >"$fromdir/foo/.excl" <<EOF
1075++ .excl
1076+- file1
1077+EOF
1078+cat >"$fromdir/bar/.excl" <<EOF
1079+home-cvs-exclude
1080+. -p .excl2
1081++ to
1082+EOF
1083 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1084+cat >"$fromdir/bar/down/to/.excl2" <<EOF
1085+.excl2
1086+EOF
1087 echo keeper >"$fromdir/bar/down/to/foo/file1"
1088 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1089 echo gone >"$fromdir/bar/down/to/foo/file3"
1090 echo lost >"$fromdir/bar/down/to/foo/file4"
1091 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1092 echo smashed >"$fromdir/bar/down/to/foo/to"
1093+cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1094++ *.junk
1095+EOF
1096+# This one should be ineffectual
1097+cat >"$fromdir/mid/.excl2" <<EOF
1098+extra
1099+EOF
1100 echo cvsout >"$fromdir/mid/one-in-one-out"
1101 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1102 echo cvsin >"$fromdir/mid/one-for-all"
1103+cat >"$fromdir/mid/.excl" <<EOF
1104+. -p .cvsignore
1105+EOF
1106 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1107 echo expunged >"$fromdir/mid/for/foo/extra"
1108 echo retained >"$fromdir/mid/for/foo/keep"
1109@@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1110 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1111 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1112
1113+# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1114+
1115+rm "$chkdir"/.excl
1116+rm "$chkdir"/foo/file1
1117+rm "$chkdir"/bar/.excl
1118+rm "$chkdir"/bar/down/to/.excl2
1119+rm "$chkdir"/bar/down/to/foo/.excl2
1120+rm "$chkdir"/mid/.excl
1121+cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1122+cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1123+
1124+$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1125+
1126+# Now, test if rsync excludes the same files, this time with a merge-exclude
1127+# file.
1128+
1129+checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1130+ --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1131+
1132 # The script would have aborted on error, so getting here means we've won.
1133 exit 0