1 After applying this patch and running configure, you MUST run this
6 This patch adds the ability to merge rules into your excludes/includes
7 using a ". FILE" idiom. If you specify a name without slashes, that
8 filename will be looked for in every subdirectory that rsync visits,
9 and the rules found in that subdirectory's file will affect that dir
10 and its subdirectories.
14 rsync -av --exclude='. .excl' from/ to
16 The above will look for a file named ".excl" in every directory of the
17 hierarchy that rsync visits, and it will exclude (by default) names
18 based on the rules found therein. If one of the .excl files contains
26 Then the file ".excl2" will also be read in the current dir, and all
27 subdirs of the current dir. The file ".excl3" would just be read in
28 for the current dir because its name contained a slash.
32 --- exclude.c 16 May 2004 23:54:12 -0000 1.80
33 +++ exclude.c 17 May 2004 16:16:23 -0000
34 @@ -30,13 +30,54 @@ extern int verbose;
39 +extern int sanitize_paths;
41 extern char curr_dir[];
43 struct exclude_list_struct exclude_list = { 0, 0, "" };
44 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
45 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
46 char *exclude_path_prefix = NULL;
47 +int merge_list_cnt = 0;
49 +struct mergelist_save {
50 + struct exclude_list_struct *array;
54 +static char dirbuf[MAXPATHLEN];
55 +static unsigned int dirbuf_offset = 0;
57 +/* Each exclude_list_struct describes a singly-linked list by keeping track
58 + * of both the head and tail pointers. The list is slightly unusual in that
59 + * a parent-dir's content can be appended to the end of the local list in a
60 + * special way: the last item in the local list has its "next" pointer set
61 + * to point to the inherited list, but the local list's tail pointer points
62 + * at the end of the local list. Thus, if the local list is empty, the head
63 + * will be pointing at the inherited content but the tail will be NULL. To
64 + * help you visualize this, here are the possible list arrangements:
66 + * Completely Empty Local Content Only
67 + * ================================== ====================================
68 + * head -> NULL head -> Local1 -> Local2 -> NULL
69 + * tail -> NULL tail -------------^
71 + * Inherited Content Only Both Local and Inherited Content
72 + * ================================== ====================================
73 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
74 + * tail -> NULL tail ---------^
76 + * This means that anyone wanting to traverse the whole list to USE it just
77 + * needs to start at the head and use the "next" pointers until it goes
78 + * NULL. To add new local content, we insert the item after the tail item
79 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
80 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
81 + * because it is shared between the current list and our parent list(s).
82 + * The easiest way to handle this is to simply truncate the list after the
83 + * tail item and then free the local list from the head. When inheriting
84 + * the list for a new local dir, we just save off the exclude_list_struct
85 + * values (so we can pop back to them later) and set the tail to NULL.
88 /** Build an exclude structure given an exclude pattern. */
89 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
90 @@ -46,6 +87,24 @@ static void make_exclude(struct exclude_
94 + if (mflags & MATCHFLG_MERGE_FILE) {
95 + struct exclude_struct *ex;
96 + /* If the local include file was already mentioned, don't
98 + for (ex = listp->head; ex; ex = ex->next) {
99 + if (ex->match_flags & MATCHFLG_MERGE_FILE
100 + && strlen(ex->pattern) == pat_len
101 + && memcmp(ex->pattern, pat, pat_len) == 0)
104 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
105 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
106 + mflags |= MATCHFLG_CVSIGNORE;
107 + mflags &= ~MATCHFLG_INCLUDE;
109 + mflags &= ~MATCHFLG_CVSIGNORE;
112 ret = new(struct exclude_struct);
114 out_of_memory("make_exclude");
115 @@ -81,14 +140,28 @@ static void make_exclude(struct exclude_
116 mflags |= MATCHFLG_DIRECTORY;
119 - for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
121 + if (mflags & MATCHFLG_MERGE_FILE) {
122 + struct exclude_list_struct *lp
123 + = new_array(struct exclude_list_struct, 1);
125 + out_of_memory("make_exclude");
126 + lp->head = lp->tail = NULL;
127 + if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
128 + out_of_memory("make_exclude");
129 + ret->u.merge_list = lp;
132 + for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
133 + ret->u.slash_cnt++;
136 ret->match_flags = mflags;
139 + if (!listp->tail) {
140 + ret->next = listp->head;
141 listp->head = listp->tail = ret;
144 + ret->next = listp->tail->next;
145 listp->tail->next = ret;
148 @@ -96,22 +169,128 @@ static void make_exclude(struct exclude_
150 static void free_exclude(struct exclude_struct *ex)
152 + if (ex->match_flags & MATCHFLG_MERGE_FILE) {
153 + free(ex->u.merge_list->debug_type);
154 + free(ex->u.merge_list);
160 -void clear_exclude_list(struct exclude_list_struct *listp)
161 +static void clear_exclude_list(struct exclude_list_struct *listp)
163 - struct exclude_struct *ent, *next;
165 - for (ent = listp->head; ent; ent = next) {
169 + struct exclude_struct *ent, *next;
170 + /* Truncate any inherited items from the local list. */
171 + listp->tail->next = NULL;
172 + for (ent = listp->head; ent; ent = next) {
178 listp->head = listp->tail = NULL;
181 +static struct exclude_list_struct *
182 +push_merge_files(struct exclude_struct *ent, struct exclude_list_struct *array)
184 + struct exclude_list_struct *lp;
186 + for (; ent; ent = ent->next) {
188 + if (!(ent->match_flags & MATCHFLG_MERGE_FILE))
190 + lp = ent->u.merge_list;
193 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
194 + who_am_i(), lp->debug_type);
197 + memcpy(array++, lp, sizeof (struct exclude_list_struct));
199 + array = push_merge_files(lp->head, array);
201 + if (ent->match_flags & MATCHFLG_CVSIGNORE) {
202 + lp->head = NULL; /* CVS doesn't inherit rules. */
203 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
205 + flags = ent->match_flags & MATCHFLG_INCLUDE
206 + ? XFLG_DEF_INCLUDE : 0;
208 + lp->tail = NULL; /* Switch any local rules to inherited. */
209 + if (strlcpy(dirbuf + dirbuf_offset, ent->pattern,
210 + MAXPATHLEN - dirbuf_offset) < MAXPATHLEN - dirbuf_offset)
211 + add_exclude_file(lp, dirbuf, flags);
213 + io_error |= IOERR_GENERAL;
215 + "cannot add local excludes in long-named directory %s\n",
216 + full_fname(dirbuf));
223 +void *push_local_excludes(char *fname, unsigned int offset)
225 + struct mergelist_save *push;
227 + /* Make it easy to construct the full path for a merge-file that was
228 + * specified with a relative path by saving off the current dir. */
229 + memcpy(dirbuf, fname, offset);
230 + dirbuf_offset = offset;
232 + if (!(push = new_array(struct mergelist_save, 1)))
233 + out_of_memory("push_local_excludes");
235 + push->count = merge_list_cnt;
236 + push->array = new_array(struct exclude_list_struct, merge_list_cnt);
238 + out_of_memory("push_local_excludes");
240 + push_merge_files(exclude_list.head, push->array);
242 + return (void*)push;
245 +static struct exclude_list_struct *
246 +pop_merge_files(struct exclude_struct *ent, struct exclude_list_struct *array)
248 + struct exclude_list_struct *lp;
250 + for (; ent; ent = ent->next) {
251 + if (!(ent->match_flags & MATCHFLG_MERGE_FILE))
253 + lp = ent->u.merge_list;
256 + rprintf(FINFO, "[%s] popping %sexclude list\n",
257 + who_am_i(), lp->debug_type);
260 + clear_exclude_list(lp);
261 + memcpy(lp, array++, sizeof (struct exclude_list_struct));
263 + array = pop_merge_files(lp->head, array);
269 +void pop_local_excludes(void *mem)
271 + struct mergelist_save *pop = (struct mergelist_save*)mem;
273 + pop_merge_files(exclude_list.head, pop->array);
274 + merge_list_cnt = pop->count;
280 static int check_one_exclude(char *name, struct exclude_struct *ex,
283 @@ -122,7 +301,7 @@ static int check_one_exclude(char *name,
284 /* If the pattern does not have any slashes AND it does not have
285 * a "**" (which could match a slash), then we just match the
286 * name portion of the path. */
287 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
288 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
289 if ((p = strrchr(name,'/')) != NULL)
292 @@ -148,9 +327,9 @@ static int check_one_exclude(char *name,
293 if (ex->match_flags & MATCHFLG_WILD) {
294 /* A non-anchored match with an infix slash and no "**"
295 * needs to match the last slash_cnt+1 name elements. */
296 - if (!match_start && ex->slash_cnt
297 + if (!match_start && ex->u.slash_cnt
298 && !(ex->match_flags & MATCHFLG_WILD2)) {
299 - int cnt = ex->slash_cnt + 1;
300 + int cnt = ex->u.slash_cnt + 1;
301 for (p = name + strlen(name) - 1; p >= name; p--) {
302 if (*p == '/' && !--cnt)
304 @@ -221,6 +400,13 @@ int check_exclude(struct exclude_list_st
305 struct exclude_struct *ent;
307 for (ent = listp->head; ent; ent = ent->next) {
308 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
309 + int rc = check_exclude(ent->u.merge_list, name,
315 if (check_one_exclude(name, ent, name_is_dir)) {
316 report_exclude_result(name, ent, name_is_dir,
318 @@ -254,11 +440,16 @@ static const char *get_exclude_tok(const
322 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
323 + /* Is this a +/-/. followed by a space (not whitespace)? */
324 if (!(xflags & XFLG_WORDS_ONLY)
325 - && (*s == '-' || *s == '+') && s[1] == ' ') {
326 + && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
328 mflags |= MATCHFLG_INCLUDE;
329 + else if (*s == '.') {
330 + mflags |= MATCHFLG_MERGE_FILE;
331 + if (xflags & XFLG_DEF_INCLUDE)
332 + mflags |= MATCHFLG_INCLUDE;
335 } else if (xflags & XFLG_DEF_INCLUDE)
336 mflags |= MATCHFLG_INCLUDE;
337 @@ -307,11 +498,42 @@ void add_exclude(struct exclude_list_str
341 + if (mflags & MATCHFLG_MERGE_FILE) {
342 + char name[MAXPATHLEN];
343 + if (pat_len >= sizeof name) {
345 + "merge filename too long: %s\n", cp);
348 + strlcpy(name, cp, pat_len+1);
349 + if (strchr(name, '/') != NULL) {
350 + if (sanitize_paths)
351 + sanitize_path(name, curr_dir);
355 + if (strlcpy(dirbuf + dirbuf_offset,
356 + name, MAXPATHLEN - dirbuf_offset)
357 + >= MAXPATHLEN - dirbuf_offset) {
359 + "merge filename too long: %s...\n",
365 + add_exclude_file(listp, cp,
366 + xflags | XFLG_FATAL_ERRORS);
371 make_exclude(listp, cp, pat_len, mflags);
374 - rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
375 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
376 who_am_i(), pat_len, cp, listp->debug_type,
377 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
378 mflags & MATCHFLG_INCLUDE ? "in" : "ex");
381 @@ -403,7 +625,11 @@ void send_exclude_list(int f)
382 if (ent->match_flags & MATCHFLG_INCLUDE) {
384 write_buf(f, "+ ", 2);
385 - } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
386 + } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
387 + write_int(f, l + 2);
388 + write_buf(f, ". ", 2);
389 + } else if ((*p == '-' || *p == '+' || *p == '.')
392 write_buf(f, "- ", 2);
394 @@ -444,6 +670,7 @@ void add_cvs_excludes(void)
395 char fname[MAXPATHLEN];
398 + add_exclude(&exclude_list, ". .cvsignore", 0);
399 add_exclude(&exclude_list, default_cvsignore,
400 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
402 --- flist.c 16 May 2004 23:54:12 -0000 1.224
403 +++ flist.c 17 May 2004 16:16:24 -0000
404 @@ -39,8 +39,6 @@ extern int module_id;
405 extern int ignore_errors;
406 extern int numeric_ids;
408 -extern int cvs_exclude;
411 extern char curr_dir[MAXPATHLEN];
412 extern char *files_from;
413 @@ -65,7 +63,6 @@ extern int write_batch;
415 extern struct exclude_list_struct exclude_list;
416 extern struct exclude_list_struct server_exclude_list;
417 -extern struct exclude_list_struct local_exclude_list;
421 @@ -210,8 +207,6 @@ int link_stat(const char *path, STRUCT_S
423 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
427 #if 0 /* This currently never happens, so avoid a useless compare. */
428 if (exclude_level == NO_EXCLUDES)
430 @@ -233,10 +228,7 @@ static int check_exclude_file(char *fnam
431 if (exclude_level != ALL_EXCLUDES)
433 if (exclude_list.head
434 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
436 - if (local_exclude_list.head
437 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
438 + && check_exclude(&exclude_list, fname, is_dir) < 0)
442 @@ -946,15 +938,7 @@ void send_file_name(int f, struct file_l
444 if (recursive && S_ISDIR(file->mode)
445 && !(file->flags & FLAG_MOUNT_POINT)) {
446 - struct exclude_list_struct last_list = local_exclude_list;
447 - local_exclude_list.head = local_exclude_list.tail = NULL;
448 send_directory(f, flist, f_name_to(file, fbuf));
450 - rprintf(FINFO, "[%s] popping %sexclude list\n",
451 - who_am_i(), local_exclude_list.debug_type);
453 - clear_exclude_list(&local_exclude_list);
454 - local_exclude_list = last_list;
458 @@ -965,6 +949,7 @@ static void send_directory(int f, struct
460 char fname[MAXPATHLEN];
462 + void *save_excludes;
466 @@ -988,18 +973,7 @@ static void send_directory(int f, struct
471 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
472 - < MAXPATHLEN - offset) {
473 - add_exclude_file(&local_exclude_list, fname,
474 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
476 - io_error |= IOERR_GENERAL;
478 - "cannot cvs-exclude in long-named directory %s\n",
479 - full_fname(fname));
482 + save_excludes = push_local_excludes(fname, offset);
484 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
485 char *dname = d_name(di);
486 @@ -1019,6 +993,8 @@ static void send_directory(int f, struct
487 io_error |= IOERR_GENERAL;
488 rsyserr(FERROR, errno, "readdir(%s)", dir);
491 + pop_local_excludes(save_excludes);
495 --- rsync.h 16 May 2004 07:28:24 -0000 1.204
496 +++ rsync.h 17 May 2004 16:16:25 -0000
497 @@ -496,11 +496,16 @@ struct map_struct {
498 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
499 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
500 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
501 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
502 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
503 struct exclude_struct {
504 struct exclude_struct *next;
506 unsigned int match_flags;
510 + struct exclude_list_struct *merge_list;
514 struct exclude_list_struct {
515 --- rsync.yo 7 May 2004 00:18:37 -0000 1.169
516 +++ rsync.yo 17 May 2004 16:16:26 -0000
517 @@ -1075,6 +1075,72 @@ itemize(
518 it would be excluded by the "*")
521 +manpagesection(MERGING EXCLUDE FILES)
523 +You can merge whole files into an exclude file using a rule that starts
524 +with a ". " (a dot followed by a space) and has a filename in place of the
525 +pattern. There are two types of merge rules, single-instance and
529 + it() If the filename has no slashes in it, it is a per-directory merge;
530 + rsync scans every directory that it traverses for the named file, merging
531 + its contents (when it exists) file at the start of this per-directory
532 + sub-list (subdirectories inherit the contents of their parent directories
533 + by default, and each subdirectory's rules have precedence over the parent
534 + directory's rules).
536 + it() If a filename has a slash in it, it is a single-instance merge; the
537 + named file's contents will be merged into the current exclude file,
538 + replacing the merge rule. Thus, you should use the name ./foo instead of
539 + foo if you don't want to scan for "foo" in all the subdirectories of the
543 +Note also that you can eliminate all the inherited rules for the current
544 +per-directory ruleset by putting the list-clearing token (!) in the file.
545 +This only clears the rules for the current per-directory sub-list (up
546 +through the ! token) and only for the current directory and its
549 +Here's an example exclude file (which you'd specify via the normal
550 +--exclude-from option):
553 + . /home/user/.global_excludes
560 +This will merge the contents of the /home/user/.global_excludes file at the
561 +start of the list and also turns the ".excl" filename into a per-directory
562 +exclude file whose local contents will be merged into the list in place of
565 +Additionally, you can affect where the --cvs-exclude (-C) option's
566 +inclusion of a per-directory .cvsignore file gets placed into your rules by
567 +adding an explicit merge rule for ".cvsignore". For instance, specifying
571 + rsync -avC --exclude='. .cvsignore' --exclude-from=foo a/ b
574 +will merge all the per-directory .cvsignore rules at the start of your list
575 +rather than at the end. This allows their dir-specific rules to supersede
576 +your rules instead of being subservient to them. (The global rules taken
577 +from the $HOME/.cvsignore file and from $CVSIGNORE are not affected by
580 +Note also that the parsing of any merge-file named ".cvsignore" is always
581 +done in a CVS-compatible manner (even if -C wasn't specified) -- i.e. the
582 +rules are always exclude rules (even when specified by an include option),
583 +they are split on whitespace, no special prefixes or list-clearing tokens
584 +are honored, and (for per-directory files) subdirectories don't inherit the
585 +parent directory's rules.
587 manpagesection(BATCH MODE)
589 bf(Note:) Batch mode should be considered experimental in this version