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