Updated.
[rsync/rsync-patches.git] / filter.diff
CommitLineData
8a529471
WD
1After applying this patch and running configure, you MUST run this
2command before "make":
3
4 make proto
5
524989ae 6This patch adds the ability to merge rules into your excludes/includes
d34d9fad
WD
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
40e20a69 10affect that dir and its subdirs.
a55d21aa
WD
11
12For example:
13
40e20a69 14 rsync -av --exclude='. -p .excl' from/ to
a55d21aa
WD
15
16The above will look for a file named ".excl" in every directory of the
524989ae
WD
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:
a55d21aa
WD
20
21 + *.c
d34d9fad 22 . -p .excl2
db4f43dd 23 . .excl3
524989ae 24 *.o
5f98a4b1 25 /foobar
a55d21aa 26
d34d9fad 27Then the file ".excl2" will also be read in from the current dir and all
40e20a69 28its subdirs (due to the -p option). The file ".excl3" would just be
5f98a4b1
WD
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).
a55d21aa
WD
32
33..wayne..
34
40e20a69 35--- orig/clientserver.c 2004-08-02 02:29:16
3d3aaf9f 36+++ clientserver.c 2004-08-10 15:44:15
45795ec6 37@@ -48,12 +48,14 @@ extern int no_detach;
40e20a69
WD
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;
40e20a69
WD
44
45 char *auth_user;
46
3d3aaf9f 47+/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
45795ec6
WD
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_
40e20a69
WD
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 = "";
45795ec6
WD
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+ }
40e20a69
WD
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);
45795ec6 85-
40e20a69 86- exclude_path_prefix = NULL;
45795ec6 87+ add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
40e20a69
WD
88
89 log_init();
90
3d3aaf9f 91--- orig/exclude.c 2004-08-10 18:17:01
20f8d513 92+++ exclude.c 2004-08-13 07:40:08
b338da5b 93@@ -30,13 +30,69 @@ extern int verbose;
a55d21aa
WD
94 extern int eol_nulls;
95 extern int list_only;
96 extern int recurse;
97+extern int io_error;
56babefa 98+extern int sanitize_paths;
a55d21aa
WD
99
100 extern char curr_dir[];
56babefa 101+extern unsigned int curr_dir_len;
45795ec6 102+extern unsigned int module_dirlen;
a55d21aa 103
d8af8661 104 struct exclude_list_struct exclude_list = { 0, 0, "" };
524989ae 105-struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
d8af8661 106 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
40e20a69
WD
107-char *exclude_path_prefix = NULL;
108+
eabda998 109+struct mergelist_save_struct {
d1e7c1c8 110+ struct exclude_list_struct *array;
d8af8661
WD
111+ int count;
112+};
524989ae 113+
db4f43dd
WD
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
45795ec6
WD
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;
b338da5b 120+static int dirbuf_depth;
45795ec6
WD
121+
122+/* This is True when we're scanning parent dirs for per-dir merge-files. */
123+static BOOL parent_dirscan = False;
db4f43dd
WD
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. */
d34d9fad
WD
128+static struct exclude_struct **mergelist_parents;
129+static int mergelist_cnt = 0;
130+static int mergelist_size = 0;
a55d21aa 131+
d1e7c1c8
WD
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
0c7d1fd8 136+ * to point to the inherited list, but the local list's tail pointer points
d1e7c1c8 137+ * at the end of the local list. Thus, if the local list is empty, the head
0c7d1fd8 138+ * will be pointing at the inherited content but the tail will be NULL. To
d1e7c1c8
WD
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
bc95f62b
WD
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
0c7d1fd8 160+ * values (so we can pop back to them later) and set the tail to NULL.
d1e7c1c8 161+ */
40e20a69 162
0c7d1fd8
WD
163 /** Build an exclude structure given an exclude pattern. */
164 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
b338da5b 165@@ -46,23 +102,50 @@ static void make_exclude(struct exclude_
a55d21aa 166 const char *cp;
0c7d1fd8
WD
167 unsigned int ex_len;
168
d34d9fad
WD
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+
524989ae 176+ if (mflags & MATCHFLG_MERGE_FILE) {
eabda998 177+ int i;
a55d21aa 178+ /* If the local include file was already mentioned, don't
524989ae 179+ * add it again. */
eabda998
WD
180+ for (i = 0; i < mergelist_cnt; i++) {
181+ struct exclude_struct *ex = mergelist_parents[i];
182+ if (strlen(ex->pattern) == pat_len
d8af8661 183+ && memcmp(ex->pattern, pat, pat_len) == 0)
a55d21aa
WD
184+ return;
185+ }
524989ae
WD
186+ if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
187+ && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
a55d21aa 188+ mflags |= MATCHFLG_CVSIGNORE;
40e20a69 189+ mflags &= ~MATCHFLG_INCLUDE;
a55d21aa
WD
190+ } else
191+ mflags &= ~MATCHFLG_CVSIGNORE;
192+ }
0c7d1fd8 193+
a55d21aa
WD
194 ret = new(struct exclude_struct);
195 if (!ret)
196 out_of_memory("make_exclude");
40e20a69
WD
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;
45795ec6
WD
209+ } else
210+ ex_len = dirbuf_len - module_dirlen - 1;
40e20a69
WD
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");
45795ec6 216 if (ex_len)
40e20a69 217- memcpy(ret->pattern, exclude_path_prefix, ex_len);
5f98a4b1 218+ memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
40e20a69
WD
219 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
220 pat_len += ex_len;
221
b338da5b 222@@ -81,14 +164,40 @@ static void make_exclude(struct exclude_
7b22909b 223 mflags |= MATCHFLG_DIRECTORY;
a55d21aa
WD
224 }
225
7b22909b 226- for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
bc95f62b 227- ret->slash_cnt++;
524989ae 228+ if (mflags & MATCHFLG_MERGE_FILE) {
d8af8661
WD
229+ struct exclude_list_struct *lp
230+ = new_array(struct exclude_list_struct, 1);
231+ if (!lp)
a55d21aa 232+ out_of_memory("make_exclude");
7cb7ae4e 233+ lp->head = lp->tail = NULL;
d34d9fad
WD
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)
a55d21aa 239+ out_of_memory("make_exclude");
eabda998
WD
240+ ret->u.mergelist = lp;
241+ if (mergelist_cnt == mergelist_size) {
d34d9fad
WD
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");
eabda998
WD
248+ }
249+ mergelist_parents[mergelist_cnt++] = ret;
7b22909b
WD
250+ } else {
251+ for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
252+ ret->u.slash_cnt++;
abe86b1f 253+ }
7b22909b 254
0c7d1fd8 255 ret->match_flags = mflags;
a55d21aa 256
7b22909b
WD
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 }
b338da5b 267@@ -96,22 +205,267 @@ static void make_exclude(struct exclude_
d8af8661
WD
268
269 static void free_exclude(struct exclude_struct *ex)
270 {
271+ if (ex->match_flags & MATCHFLG_MERGE_FILE) {
eabda998
WD
272+ free(ex->u.mergelist->debug_type);
273+ free(ex->u.mergelist);
d8af8661
WD
274+ }
275 free(ex->pattern);
a55d21aa
WD
276 free(ex);
277 }
278
d8af8661
WD
279-void clear_exclude_list(struct exclude_list_struct *listp)
280+static void clear_exclude_list(struct exclude_list_struct *listp)
a55d21aa 281 {
7cb7ae4e
WD
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;
bc95f62b 289+ /* Truncate any inherited items from the local list. */
7cb7ae4e 290+ listp->tail->next = NULL;
dc5cce3c 291+ /* Now free everything that is left. */
7cb7ae4e
WD
292+ for (ent = listp->head; ent; ent = next) {
293+ next = ent->next;
294+ free_exclude(ent);
295+ }
524989ae
WD
296 }
297
7cb7ae4e
WD
298 listp->head = listp->tail = NULL;
299 }
a55d21aa 300
db4f43dd 301+/* This returns an expanded (absolute) filename for the merge-file name if
45795ec6 302+ * the name has any slashes in it OR if the parent_dirscan var is True;
db4f43dd 303+ * otherwise it returns the original merge_file name. If the len_ptr value
45795ec6
WD
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)
41ebea83
WD
310+{
311+ static char buf[MAXPATHLEN];
312+ char *fn, tmpbuf[MAXPATHLEN];
45795ec6 313+ unsigned int fn_len;
41ebea83 314+
40e20a69 315+ if (!parent_dirscan && *merge_file != '/') {
db4f43dd 316+ /* Return the name unchanged it doesn't have any slashes. */
41ebea83 317+ if (len_ptr) {
db4f43dd
WD
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)
41ebea83
WD
325+ return (char *)merge_file;
326+ }
327+
328+ fn = *merge_file == '/' ? buf : tmpbuf;
329+ if (sanitize_paths) {
45795ec6 330+ const char *r = prefix_skip ? "/" : NULL;
41ebea83
WD
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+ }
b338da5b 337+ if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) {
45795ec6 338+ rprintf(FERROR, "merge-file name overflows: %s\n",
41ebea83
WD
339+ merge_file);
340+ return NULL;
341+ }
342+ } else {
343+ strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
20f8d513 344+ clean_fname(fn, 1);
41ebea83
WD
345+ }
346+
41ebea83 347+ fn_len = strlen(fn);
45795ec6
WD
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);
41ebea83
WD
353+ return NULL;
354+ }
45795ec6
WD
355+ memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
356+ memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
20f8d513 357+ fn_len = clean_fname(buf, 1);
45795ec6
WD
358+
359+ done:
41ebea83
WD
360+ if (len_ptr)
361+ *len_ptr = fn_len;
41ebea83
WD
362+ return buf;
363+}
364+
45795ec6
WD
365+/* Sets the dirbuf and dirbuf_len values. */
366+void set_excludes_dir(const char *dir, unsigned int dirlen)
40e20a69 367+{
45795ec6
WD
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;
470aa8d5
WD
373+ if (len + dirlen >= MAXPATHLEN)
374+ dirlen = 0;
45795ec6
WD
375+ } else
376+ len = 0;
377+ memcpy(dirbuf + len, dir, dirlen);
378+ dirbuf[dirlen + len] = '\0';
20f8d513 379+ dirbuf_len = clean_fname(dirbuf, 1);
45795ec6
WD
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';
b338da5b
WD
385+ if (sanitize_paths)
386+ dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
40e20a69
WD
387+}
388+
45795ec6 389+/* This routine takes a per-dir merge-file entry and finishes its setup.
db4f43dd
WD
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
45795ec6
WD
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)
1e476835 396+{
d34d9fad 397+ char buf[MAXPATHLEN];
db4f43dd
WD
398+ char *x, *y, *pat = ex->pattern;
399+ unsigned int len;
56babefa 400+
45795ec6
WD
401+ if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
402+ return 0;
1e476835 403+
db4f43dd
WD
404+ y = strrchr(x, '/');
405+ *y = '\0';
406+ ex->pattern = strdup(y+1);
db4f43dd
WD
407+ if (!*x)
408+ x = "/";
409+ if (*x == '/')
410+ strlcpy(buf, x, MAXPATHLEN);
d34d9fad 411+ else
db4f43dd 412+ pathjoin(buf, MAXPATHLEN, dirbuf, x);
d34d9fad 413+
20f8d513 414+ len = clean_fname(buf, 1);
db4f43dd
WD
415+ if (len != 1 && len < MAXPATHLEN-1) {
416+ buf[len++] = '/';
417+ buf[len] = '\0';
418+ }
db4f43dd 419+ /* This ensures that the specified dir is a parent of the transfer. */
45795ec6 420+ for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
d34d9fad 421+ if (*x)
db4f43dd 422+ y += strlen(y); /* nope -- skip the scan */
d34d9fad 423+
45795ec6 424+ parent_dirscan = True;
d34d9fad
WD
425+ while (*y) {
426+ char save[MAXPATHLEN];
41ebea83 427+ strlcpy(save, y, MAXPATHLEN);
db4f43dd 428+ *y = '\0';
45795ec6 429+ dirbuf_len = y - dirbuf;
db4f43dd 430+ strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
40e20a69
WD
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. */
d34d9fad 434+ lp->tail = NULL;
41ebea83 435+ strlcpy(y, save, MAXPATHLEN);
d34d9fad 436+ while ((*x++ = *y++) != '/') {}
1e476835 437+ }
45795ec6 438+ parent_dirscan = False;
db4f43dd 439+ free(pat);
45795ec6 440+ return 1;
1e476835
WD
441+}
442+
db4f43dd 443+/* Each time rsync changes to a new directory it call this function to
45795ec6
WD
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. */
40e20a69 447+void *push_local_excludes(const char *dir, unsigned int dirlen)
a55d21aa 448+{
eabda998
WD
449+ struct mergelist_save_struct *push;
450+ struct exclude_list_struct *ap;
451+ int i;
452+
45795ec6 453+ set_excludes_dir(dir, dirlen);
eabda998
WD
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");
524989ae 462+
eabda998
WD
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;
a55d21aa 473+ int flags;
524989ae
WD
474+
475+ if (verbose > 2) {
476+ rprintf(FINFO, "[%s] pushing %sexclude list\n",
d8af8661 477+ who_am_i(), lp->debug_type);
524989ae 478+ }
d8af8661 479+
40e20a69
WD
480+ if (ex->match_flags & MATCHFLG_CVSIGNORE) {
481+ lp->head = NULL; /* CVS doesn't inherit rules. */
ee1af13c 482+ flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
40e20a69 483+ } else {
eabda998 484+ flags = ex->match_flags & MATCHFLG_INCLUDE
524989ae 485+ ? XFLG_DEF_INCLUDE : 0;
a55d21aa 486+ }
40e20a69 487+ lp->tail = NULL; /* Switch any local rules to inherited. */
d34d9fad 488+
40e20a69
WD
489+ if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
490+ ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
45795ec6
WD
491+ if (setup_merge_file(ex, lp, flags))
492+ set_excludes_dir(dir, dirlen);
d34d9fad
WD
493+ }
494+
45795ec6
WD
495+ if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
496+ MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
40e20a69 497+ add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
524989ae 498+ else {
a55d21aa
WD
499+ io_error |= IOERR_GENERAL;
500+ rprintf(FINFO,
501+ "cannot add local excludes in long-named directory %s\n",
d8af8661 502+ full_fname(dirbuf));
a55d21aa 503+ }
45795ec6 504+ dirbuf[dirbuf_len] = '\0';
a55d21aa
WD
505+ }
506+
524989ae 507+ return (void*)push;
a55d21aa
WD
508+}
509+
eabda998 510+void pop_local_excludes(void *mem)
a55d21aa 511+{
eabda998
WD
512+ struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
513+ struct exclude_list_struct *ap;
514+ int i;
d8af8661 515+
066e3b3e 516+ for (i = mergelist_cnt; i-- > 0; ) {
eabda998
WD
517+ struct exclude_struct *ex = mergelist_parents[i];
518+ struct exclude_list_struct *lp = ex->u.mergelist;
524989ae 519+
524989ae
WD
520+ if (verbose > 2) {
521+ rprintf(FINFO, "[%s] popping %sexclude list\n",
d8af8661 522+ who_am_i(), lp->debug_type);
524989ae 523+ }
d8af8661
WD
524+
525+ clear_exclude_list(lp);
a55d21aa 526+ }
d8af8661 527+
40e20a69 528+ mergelist_cnt = pop->count;
eabda998
WD
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+ }
d8af8661
WD
533+
534+ free(pop->array);
535+ free(pop);
7cb7ae4e
WD
536+}
537+
a55d21aa 538 static int check_one_exclude(char *name, struct exclude_struct *ex,
7cb7ae4e
WD
539 int name_is_dir)
540 {
b338da5b 541@@ -125,13 +479,14 @@ static int check_one_exclude(char *name,
bc95f62b
WD
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 }
3d3aaf9f
WD
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);
045caa90
WD
556 name = full_name;
557 }
558
b338da5b 559@@ -148,9 +503,9 @@ static int check_one_exclude(char *name,
bc95f62b
WD
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. */
0c7d1fd8
WD
563- if (!match_start && ex->slash_cnt
564+ if (!match_start && ex->u.slash_cnt
565 && !(ex->match_flags & MATCHFLG_WILD2)) {
bc95f62b
WD
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;
b338da5b 571@@ -221,6 +576,13 @@ int check_exclude(struct exclude_list_st
a55d21aa
WD
572 struct exclude_struct *ent;
573
574 for (ent = listp->head; ent; ent = ent->next) {
524989ae 575+ if (ent->match_flags & MATCHFLG_MERGE_FILE) {
eabda998 576+ int rc = check_exclude(ent->u.mergelist, name,
d8af8661 577+ name_is_dir);
a55d21aa
WD
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);
b338da5b 585@@ -253,11 +615,36 @@ static const char *get_exclude_tok(const
a55d21aa
WD
586 p = (const char *)s;
587 }
588
589- /* Is this a '+' or '-' followed by a space (not whitespace)? */
d34d9fad 590+ /* Check for a +/-/. followed by a space (not whitespace). */
ee1af13c 591 if (!(xflags & XFLG_WORDS_ONLY)
a55d21aa 592- && (*s == '-' || *s == '+') && s[1] == ' ') {
a55d21aa 593+ && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
0c7d1fd8
WD
594 if (*s == '+')
595 mflags |= MATCHFLG_INCLUDE;
a55d21aa 596+ else if (*s == '.') {
524989ae 597+ mflags |= MATCHFLG_MERGE_FILE;
a55d21aa
WD
598+ if (xflags & XFLG_DEF_INCLUDE)
599+ mflags |= MATCHFLG_INCLUDE;
d34d9fad
WD
600+ while (s[2] == '-') {
601+ s += 2;
602+ do {
603+ switch (*++s) {
d34d9fad 604+ case 'p':
40e20a69
WD
605+ mflags |= MATCHFLG_PERDIR_MERGE
606+ | MATCHFLG_FINISH_SETUP;
d34d9fad
WD
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+ }
a55d21aa 619+ }
d34d9fad 620+ done:
a55d21aa 621 s += 2;
0c7d1fd8
WD
622 } else if (xflags & XFLG_DEF_INCLUDE)
623 mflags |= MATCHFLG_INCLUDE;
b338da5b 624@@ -273,6 +660,8 @@ static const char *get_exclude_tok(const
40e20a69
WD
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;
b338da5b 633@@ -284,7 +673,7 @@ void add_exclude(struct exclude_list_str
45795ec6
WD
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;
b338da5b 642@@ -292,9 +681,15 @@ void add_exclude(struct exclude_list_str
045caa90
WD
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;
41ebea83
WD
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) {
b338da5b 658@@ -306,13 +701,24 @@ void add_exclude(struct exclude_list_str
0c7d1fd8
WD
659 continue;
660 }
a55d21aa 661
d34d9fad
WD
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");
524989ae 668+ if (mflags & MATCHFLG_MERGE_FILE) {
45795ec6 669+ unsigned int len = pat_len;
41ebea83 670+ if (mflags & MATCHFLG_PERDIR_MERGE) {
40e20a69 671+ if (parent_dirscan) {
45795ec6 672+ if (!(p = parse_merge_name(cp, &len, module_dirlen)))
41ebea83 673+ continue;
45795ec6 674+ make_exclude(listp, p, len, mflags);
d34d9fad
WD
675+ continue;
676+ }
41ebea83 677+ } else {
45795ec6 678+ if (!(p = parse_merge_name(cp, &len, 0)))
d34d9fad 679+ continue;
45795ec6 680+ add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
41ebea83 681+ continue;
0c7d1fd8 682+ }
a55d21aa 683 }
d34d9fad
WD
684+
685+ make_exclude(listp, cp, pat_len, mflags);
0c7d1fd8 686 }
d34d9fad
WD
687 }
688
b338da5b 689@@ -321,7 +727,7 @@ void add_exclude_file(struct exclude_lis
41ebea83
WD
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
b338da5b 698@@ -342,6 +748,12 @@ void add_exclude_file(struct exclude_lis
45795ec6 699 }
c4e46f36
WD
700 return;
701 }
45795ec6
WD
702+ dirbuf[dirbuf_len] = '\0';
703+
c4e46f36 704+ if (verbose > 2) {
1e476835
WD
705+ rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
706+ who_am_i(), fname, xflags);
c4e46f36 707+ }
45795ec6 708
c4e46f36
WD
709 while (1) {
710 char *s = line;
b338da5b 711@@ -402,7 +814,21 @@ void send_exclude_list(int f)
0c7d1fd8 712 if (ent->match_flags & MATCHFLG_INCLUDE) {
a55d21aa
WD
713 write_int(f, l + 2);
714 write_buf(f, "+ ", 2);
715- } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
524989ae 716+ } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
41ebea83
WD
717+ char buf[32], *op = buf;
718+ *op++ = '.';
719+ *op++ = ' ';
720+ if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
721+ *op++ = '-';
722+ *op++ = 'p';
40e20a69
WD
723+ if (*p == '-')
724+ *op++ = '-';
41ebea83
WD
725+ *op++ = ' ';
726+ }
727+ write_int(f, l + (op - buf));
728+ write_buf(f, buf, op - buf);
a55d21aa
WD
729+ } else if ((*p == '-' || *p == '+' || *p == '.')
730+ && p[1] == ' ') {
731 write_int(f, l + 2);
732 write_buf(f, "- ", 2);
733 } else
b338da5b 734@@ -443,6 +869,7 @@ void add_cvs_excludes(void)
a55d21aa
WD
735 char fname[MAXPATHLEN];
736 char *p;
737
d34d9fad 738+ add_exclude(&exclude_list, ". -p .cvsignore", 0);
a55d21aa 739 add_exclude(&exclude_list, default_cvsignore,
ee1af13c 740 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
a55d21aa 741
20f8d513
WD
742--- orig/flist.c 2004-08-12 18:34:38
743+++ flist.c 2004-08-12 18:59:28
d34d9fad 744@@ -39,10 +39,9 @@ extern int module_id;
a55d21aa
WD
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];
d34d9fad 752+extern unsigned int curr_dir_len;
a55d21aa 753 extern char *files_from;
d34d9fad
WD
754 extern int filesfrom_fd;
755
9be39c35 756@@ -66,7 +65,6 @@ extern int list_only;
a55d21aa
WD
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
9be39c35 764@@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
a55d21aa
WD
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;
9be39c35 773@@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
a55d21aa
WD
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 }
b338da5b 785@@ -956,15 +949,7 @@ void send_file_name(int f, struct file_l
a55d21aa
WD
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));
7cb7ae4e
WD
792- if (verbose > 2) {
793- rprintf(FINFO, "[%s] popping %sexclude list\n",
794- who_am_i(), local_exclude_list.debug_type);
795- }
d8af8661 796- clear_exclude_list(&local_exclude_list);
a55d21aa
WD
797- local_exclude_list = last_list;
798 }
799 }
800
b338da5b 801@@ -975,6 +960,7 @@ static void send_directory(int f, struct
a55d21aa
WD
802 struct dirent *di;
803 char fname[MAXPATHLEN];
804 unsigned int offset;
805+ void *save_excludes;
806 char *p;
807
808 d = opendir(dir);
b338da5b 809@@ -998,18 +984,7 @@ static void send_directory(int f, struct
a55d21aa
WD
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,
ee1af13c 817- XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
a55d21aa
WD
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- }
045caa90 824- }
045caa90
WD
825+ save_excludes = push_local_excludes(fname, offset);
826
a55d21aa
WD
827 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
828 char *dname = d_name(di);
b338da5b 829@@ -1030,6 +1005,8 @@ static void send_directory(int f, struct
bc95f62b 830 rsyserr(FERROR, errno, "readdir(%s)", dir);
a55d21aa 831 }
a55d21aa 832
eabda998
WD
833+ pop_local_excludes(save_excludes);
834+
a55d21aa
WD
835 closedir(d);
836 }
eabda998 837
b338da5b 838@@ -1049,6 +1026,7 @@ struct file_list *send_file_list(int f,
d34d9fad
WD
839 char *p, *dir, olddir[sizeof curr_dir];
840 char lastpath[MAXPATHLEN] = "";
841 struct file_list *flist;
45795ec6 842+ BOOL need_first_push = True;
d34d9fad
WD
843 int64 start_write;
844 int use_ff_fd = 0;
845
b338da5b 846@@ -1069,6 +1047,10 @@ struct file_list *send_file_list(int f,
d34d9fad
WD
847 exit_cleanup(RERR_FILESELECT);
848 }
849 use_ff_fd = 1;
850+ if (curr_dir_len < MAXPATHLEN - 1) {
40e20a69 851+ push_local_excludes(curr_dir, curr_dir_len);
45795ec6 852+ need_first_push = False;
1e476835 853+ }
d34d9fad
WD
854 }
855 }
856
b338da5b 857@@ -1099,6 +1081,15 @@ struct file_list *send_file_list(int f,
d34d9fad
WD
858 }
859 }
860
45795ec6 861+ if (need_first_push) {
d34d9fad 862+ if ((p = strrchr(fname, '/')) != NULL) {
45795ec6
WD
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;
d34d9fad 868+ }
1e476835 869+
d34d9fad
WD
870 if (link_stat(fname, &st, keep_dirlinks) != 0) {
871 if (f != -1) {
872 io_error |= IOERR_GENERAL;
20f8d513
WD
873--- orig/options.c 2004-08-12 18:34:38
874+++ options.c 2004-08-12 18:59:28
40e20a69
WD
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[]
d34d9fad
WD
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 },
40e20a69 891@@ -589,6 +591,11 @@ int parse_arguments(int *argc, const cha
d34d9fad
WD
892 am_sender = 1;
893 break;
894
895+ case 'E':
896+ add_exclude(&exclude_list,
40e20a69 897+ ". -p /.rsync-excludes", 0);
d34d9fad 898+ break;
1e476835 899+
d34d9fad
WD
900 case 'P':
901 do_progress = 1;
902 keep_partial = 1;
045caa90 903--- orig/rsync.h 2004-08-03 15:41:32
40e20a69
WD
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 {
0c7d1fd8
WD
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 */
524989ae 917+#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
a55d21aa 918+#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
40e20a69
WD
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 */
a55d21aa
WD
921 struct exclude_struct {
922 struct exclude_struct *next;
923 char *pattern;
0c7d1fd8 924 unsigned int match_flags;
bc95f62b 925- int slash_cnt;
bc95f62b
WD
926+ union {
927+ int slash_cnt;
eabda998 928+ struct exclude_list_struct *mergelist;
bc95f62b 929+ } u;
a55d21aa
WD
930 };
931
932 struct exclude_list_struct {
20f8d513
WD
933--- orig/rsync.yo 2004-08-13 07:18:59
934+++ rsync.yo 2004-08-13 00:43:31
935@@ -364,6 +364,7 @@ verb(
1e476835
WD
936 --include=PATTERN don't exclude files matching PATTERN
937 --include-from=FILE don't exclude patterns listed in FILE
1e476835 938 --files-from=FILE read FILE for list of source-file names
40e20a69 939+ -E same as --exclude='. -p /.rsync-excludes'
1e476835
WD
940 -0 --from0 all file lists are delimited by nulls
941 --version print version number
d34d9fad 942 --daemon run as an rsync daemon
20f8d513 943@@ -1008,24 +1009,32 @@ The exclude and include patterns specifi
5f98a4b1
WD
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
85c7c40a
WD
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).
5f98a4b1
WD
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
85c7c40a
WD
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
5f98a4b1
WD
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".
20f8d513 984@@ -1072,23 +1081,27 @@ because rsync did not descend through th
5f98a4b1
WD
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
85c7c40a 991+options or multiple --include and --exclude options.
5f98a4b1
WD
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
85c7c40a 1010+ transfer" (for a global rule) or in the merge-file's directory (for a
5f98a4b1
WD
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.
20f8d513 1023@@ -1101,22 +1114,31 @@ itemize(
5f98a4b1
WD
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)
1e476835 1041 then it is always considered an exclude pattern, even if specified as
5f98a4b1
WD
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+
623f2443 1045+ it() if the pattern starts with ". " (a dot followed by a space) then its
5f98a4b1
WD
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
1e476835 1048+ information.
5f98a4b1 1049
1e476835
WD
1050 it() if the pattern is a single exclamation mark ! then the current
1051 include/exclude list is reset, removing all previously defined patterns.
5f98a4b1
WD
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).
524989ae
WD
1056 )
1057
5f98a4b1 1058 The +/- rules are most useful in a list that was read from a file, allowing
20f8d513 1059@@ -1163,8 +1185,160 @@ itemize(
5f98a4b1
WD
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)
524989ae 1070+
d34d9fad
WD
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
5f98a4b1 1073+place of the pattern. There are two kinds of merged exclude files --
85c7c40a
WD
1074+single-instance and per-directory. The choice is made via an option
1075+placed prior to the merge-file name:
d34d9fad
WD
1076+
1077+startdit()
1078+
d34d9fad
WD
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
3d3aaf9f
WD
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).
5f98a4b1 1086+
d34d9fad
WD
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()
524989ae 1091+
85c7c40a
WD
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:
524989ae
WD
1107+
1108+verb(
1109+ . /home/user/.global_excludes
41ebea83 1110+ *.gz
40e20a69 1111+ . -p .excl
524989ae 1112+ + *.[ch]
41ebea83 1113+ *.o
3d3aaf9f
WD
1114+)
1115+
524989ae
WD
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
40e20a69 1118+exclude file. All the merged rules default to being exclude rules because
5f98a4b1 1119+an exclude statement was used to specify them. Rules read in from the
85c7c40a 1120+.global_excludes file are anchored just like all other global rules.
524989ae 1121+
5f98a4b1 1122+If a per-directory merge-file is specified with a path that is a parent
d34d9fad
WD
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(
40e20a69 1129+ --exclude='. -p /.rsync-excludes'
d34d9fad
WD
1130+)
1131+
1132+That exclude tells rsync to scan for the file .rsync-excludes in all
85c7c40a
WD
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.)
41ebea83
WD
1135+
1136+Some examples of this pre-scanning for per-directory files:
d34d9fad
WD
1137+
1138+verb(
1139+ rsync -avE /src/path/ /dest/dir
41ebea83 1140+ rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
40e20a69 1141+ rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
d34d9fad 1142+)
1e476835 1143+
5f98a4b1 1144+The first two commands above will look for ".rsync-excludes" in "/" and
41ebea83 1145+"/src" before the normal scan begins looking for the file in "/src/path"
5f98a4b1
WD
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".
85c7c40a 1160+Without this, rsync would add its this rule at the end of all your other
5f98a4b1
WD
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.)
524989ae 1173+
3d3aaf9f
WD
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 )
524989ae 1220
3d3aaf9f 1221 manpagesection(BATCH MODE)
13bed3dd 1222--- orig/testsuite/exclude.test 2004-05-29 21:25:45
40e20a69 1223+++ testsuite/exclude.test 2004-08-08 06:35:15
c4e46f36 1224@@ -23,19 +23,47 @@ export HOME CVSIGNORE
7d31425d
WD
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"
c4e46f36 1228+cat >"$fromdir/.excl" <<EOF
7d31425d
WD
1229+.excl
1230+*.bak
1231+*.old
1232+*.junk
c4e46f36 1233+EOF
7d31425d
WD
1234 echo kept >"$fromdir/foo/file1"
1235 echo removed >"$fromdir/foo/file2"
1236 echo cvsout >"$fromdir/foo/file2.old"
c4e46f36 1237+cat >"$fromdir/foo/.excl" <<EOF
7d31425d
WD
1238++ .excl
1239+- file1
c4e46f36
WD
1240+EOF
1241+cat >"$fromdir/bar/.excl" <<EOF
7d31425d 1242+home-cvs-exclude
40e20a69 1243+. -p .excl2
7d31425d 1244++ to
c4e46f36 1245+EOF
7d31425d 1246 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
c4e46f36 1247+cat >"$fromdir/bar/down/to/.excl2" <<EOF
7d31425d 1248+.excl2
c4e46f36 1249+EOF
7d31425d
WD
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"
c4e46f36 1256+cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
7d31425d 1257++ *.junk
c4e46f36 1258+EOF
7d31425d 1259+# This one should be ineffectual
c4e46f36 1260+cat >"$fromdir/mid/.excl2" <<EOF
7d31425d 1261+extra
c4e46f36 1262+EOF
7d31425d
WD
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"
c4e46f36 1266+cat >"$fromdir/mid/.excl" <<EOF
d34d9fad 1267+. -p .cvsignore
c4e46f36 1268+EOF
7d31425d
WD
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"
c4e46f36
WD
1272@@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1273 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1274 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
7d31425d
WD
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+
40e20a69 1292+checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
c4e46f36 1293+ --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
7d31425d
WD
1294+
1295 # The script would have aborted on error, so getting here means we've won.
1296 exit 0