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 07:28:21 -0000 1.78
33 +++ exclude.c 16 May 2004 07:44:55 -0000
34 @@ -30,14 +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 +struct exclude_list_struct exclude_list = { 0, 0, 0, "" };
47 +struct exclude_list_struct server_exclude_list = { 0, 0, 0, "server " };
48 char *exclude_path_prefix = NULL;
50 +struct exclude_list_root {
51 + struct exclude_list_struct *array;
55 +static char dirbuf[MAXPATHLEN];
56 +static unsigned int dirbuf_offset = 0;
58 +/* Each exclude_list_struct describes a singly-linked list by keeping track
59 + * of both the head and tail pointers. The list is slightly unusual in that
60 + * a parent-dir's content can be appended to the end of the local list in a
61 + * special way: the last item in the local list has its "next" pointer set
62 + * to point to the inherited list, but the local list's tail pointer points
63 + * at the end of the local list. Thus, if the local list is empty, the head
64 + * will be pointing at the inherited content but the tail will be NULL. To
65 + * help you visualize this, here are the possible list arrangements:
67 + * Completely Empty Local Content Only
68 + * ================================== ====================================
69 + * head -> NULL head -> Local1 -> Local2 -> NULL
70 + * tail -> NULL tail -------------^
72 + * Inherited Content Only Both Local and Inherited Content
73 + * ================================== ====================================
74 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
75 + * tail -> NULL tail ---------^
77 + * This means that anyone wanting to traverse the whole list to USE it just
78 + * needs to start at the head and use the "next" pointers until it goes
79 + * NULL. To add new local content, we insert the item after the tail item
80 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
81 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
82 + * because it is shared between the current list and our parent list(s).
83 + * The easiest way to handle this is to simply truncate the list after the
84 + * tail item and then free the local list from the head. When inheriting
85 + * the list for a new local dir, we just save off the exclude_list_struct
86 + * values (so we can pop back to them later) and set the tail to NULL.
89 /** Build an exclude structure given an exclude pattern. */
90 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
91 unsigned int pat_len, unsigned int mflags)
92 @@ -46,6 +86,24 @@ static void make_exclude(struct exclude_
96 + if (mflags & MATCHFLG_MERGE_FILE) {
97 + struct exclude_struct *ex;
98 + /* If the local include file was already mentioned, don't
100 + for (ex = listp->head; ex; ex = ex->next) {
101 + if (ex->match_flags & MATCHFLG_MERGE_FILE
102 + && strlen(ex->pattern) == pat_len
103 + && strncmp(ex->pattern, pat, pat_len) == 0)
106 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
107 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
108 + mflags |= MATCHFLG_CVSIGNORE;
109 + mflags &= ~MATCHFLG_INCLUDE;
111 + mflags &= ~MATCHFLG_CVSIGNORE;
114 ret = new(struct exclude_struct);
116 out_of_memory("make_exclude");
117 @@ -82,15 +140,32 @@ static void make_exclude(struct exclude_
120 for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
122 + ret->u.slash_cnt++;
125 + if (!listp->tail) {
126 + ret->next = listp->head;
127 listp->head = listp->tail = ret;
130 + ret->next = listp->tail->next;
131 listp->tail->next = ret;
135 + if (mflags & MATCHFLG_MERGE_FILE) {
136 + struct exclude_list_struct *lp;
137 + int ndx = local_lists.cnt++;
138 + local_lists.array = realloc_array(local_lists.array,
139 + struct exclude_list_struct, local_lists.cnt);
140 + if (!local_lists.array)
141 + out_of_memory("make_exclude");
142 + lp = &local_lists.array[ndx];
143 + lp->head = lp->tail = NULL;
145 + if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
146 + out_of_memory("make_exclude");
147 + ret->u.array_index = ndx; /* Overwrites u.slash_cnt. */
150 ret->match_flags = mflags;
153 @@ -100,18 +175,90 @@ static void free_exclude(struct exclude_
157 -void free_exclude_list(struct exclude_list_struct *listp)
158 +static void free_exclude_list(struct exclude_list_struct *listp)
160 - struct exclude_struct *ent, *next;
162 - for (ent = listp->head; ent; ent = next) {
166 + struct exclude_struct *ent, *next;
167 + /* Truncate any inherited items from the local list. */
168 + listp->tail->next = NULL;
169 + for (ent = listp->head; ent; ent = next) {
175 listp->head = listp->tail = NULL;
178 +void *push_local_excludes(char *fname, unsigned int offset)
181 + struct exclude_list_root *push = new_array(struct exclude_list_root, 1);
184 + out_of_memory("push_local_excludes");
186 + push->cnt = local_lists.cnt;
187 + push->array = new_array(struct exclude_list_struct, local_lists.cnt);
189 + out_of_memory("push_local_excludes");
191 + memcpy(push->array, local_lists.array,
192 + sizeof (struct exclude_list_struct) * local_lists.cnt);
194 + /* Make it easy to construct the full path for a merge-file that was
195 + * specified with a relative path by saving off the current dir. */
196 + memcpy(dirbuf, fname, offset);
197 + dirbuf_offset = offset;
199 + for (i = 0; i < local_lists.cnt; i++) {
200 + struct exclude_list_struct *listp = &local_lists.array[i];
201 + char *file = listp->parent->pattern;
205 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
206 + who_am_i(), listp->debug_type);
208 + if (listp->parent->match_flags & MATCHFLG_CVSIGNORE) {
209 + listp->head = NULL; /* Subdirs don't inherit rules. */
210 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
212 + flags = listp->parent->match_flags & MATCHFLG_INCLUDE
213 + ? XFLG_DEF_INCLUDE : 0;
215 + listp->tail = NULL; /* Signals no local content. */
216 + if (strlcpy(fname + offset, file, MAXPATHLEN - offset)
217 + < MAXPATHLEN - offset)
218 + add_exclude_file(listp, fname, flags);
220 + io_error |= IOERR_GENERAL;
222 + "cannot add local excludes in long-named directory %s\n",
223 + full_fname(fname));
227 + return (void*)push;
230 +void pop_local_excludes(void *mem)
234 + for (i = 0; i < local_lists.cnt; i++) {
235 + struct exclude_list_struct *listp = &local_lists.array[i];
237 + rprintf(FINFO, "[%s] popping %sexclude list\n",
238 + who_am_i(), listp->debug_type);
240 + free_exclude_list(listp);
242 + free(local_lists.array);
243 + local_lists = *(struct exclude_list_root*)mem;
247 static int check_one_exclude(char *name, struct exclude_struct *ex,
250 @@ -122,7 +269,7 @@ static int check_one_exclude(char *name,
251 /* If the pattern does not have any slashes AND it does not have
252 * a "**" (which could match a slash), then we just match the
253 * name portion of the path. */
254 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
255 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
256 if ((p = strrchr(name,'/')) != NULL)
259 @@ -148,9 +295,9 @@ static int check_one_exclude(char *name,
260 if (ex->match_flags & MATCHFLG_WILD) {
261 /* A non-anchored match with an infix slash and no "**"
262 * needs to match the last slash_cnt+1 name elements. */
263 - if (!match_start && ex->slash_cnt
264 + if (!match_start && ex->u.slash_cnt
265 && !(ex->match_flags & MATCHFLG_WILD2)) {
266 - int cnt = ex->slash_cnt + 1;
267 + int cnt = ex->u.slash_cnt + 1;
268 for (p = name + strlen(name) - 1; p >= name; p--) {
269 if (*p == '/' && !--cnt)
271 @@ -221,6 +368,14 @@ int check_exclude(struct exclude_list_st
272 struct exclude_struct *ent;
274 for (ent = listp->head; ent; ent = ent->next) {
275 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
276 + struct exclude_list_struct *lp
277 + = &local_lists.array[ent->u.array_index];
278 + int rc = check_exclude(lp, name, name_is_dir);
283 if (check_one_exclude(name, ent, name_is_dir)) {
284 report_exclude_result(name, ent, name_is_dir,
286 @@ -254,11 +409,16 @@ static const char *get_exclude_tok(const
290 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
291 + /* Is this a +/-/. followed by a space (not whitespace)? */
292 if (!(xflags & XFLG_WORDS_ONLY)
293 - && (*s == '-' || *s == '+') && s[1] == ' ') {
294 + && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
296 mflags |= MATCHFLG_INCLUDE;
297 + else if (*s == '.') {
298 + mflags |= MATCHFLG_MERGE_FILE;
299 + if (xflags & XFLG_DEF_INCLUDE)
300 + mflags |= MATCHFLG_INCLUDE;
303 } else if (xflags & XFLG_DEF_INCLUDE)
304 mflags |= MATCHFLG_INCLUDE;
305 @@ -307,11 +467,42 @@ void add_exclude(struct exclude_list_str
309 + if (mflags & MATCHFLG_MERGE_FILE) {
310 + char name[MAXPATHLEN];
311 + if (pat_len >= sizeof name) {
313 + "merge filename too long: %s\n", cp);
316 + strlcpy(name, cp, pat_len+1);
317 + if (strchr(name, '/') != NULL) {
318 + if (sanitize_paths)
319 + sanitize_path(name, curr_dir);
323 + if (strlcpy(dirbuf + dirbuf_offset,
324 + name, MAXPATHLEN - dirbuf_offset)
325 + >= MAXPATHLEN - dirbuf_offset) {
327 + "merge filename too long: %s...\n",
333 + add_exclude_file(listp, cp,
334 + xflags | XFLG_FATAL_ERRORS);
339 make_exclude(listp, cp, pat_len, mflags);
342 - rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
343 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
344 who_am_i(), pat_len, cp, listp->debug_type,
345 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
346 mflags & MATCHFLG_INCLUDE ? "in" : "ex");
349 @@ -403,7 +594,11 @@ void send_exclude_list(int f)
350 if (ent->match_flags & MATCHFLG_INCLUDE) {
352 write_buf(f, "+ ", 2);
353 - } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
354 + } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
355 + write_int(f, l + 2);
356 + write_buf(f, ". ", 2);
357 + } else if ((*p == '-' || *p == '+' || *p == '.')
360 write_buf(f, "- ", 2);
362 @@ -444,6 +639,7 @@ void add_cvs_excludes(void)
363 char fname[MAXPATHLEN];
366 + add_exclude(&exclude_list, ". .cvsignore", 0);
367 add_exclude(&exclude_list, default_cvsignore,
368 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
370 --- flist.c 15 May 2004 19:31:10 -0000 1.223
371 +++ flist.c 16 May 2004 07:44:56 -0000
372 @@ -39,8 +39,6 @@ extern int module_id;
373 extern int ignore_errors;
374 extern int numeric_ids;
376 -extern int cvs_exclude;
379 extern char curr_dir[MAXPATHLEN];
380 extern char *files_from;
381 @@ -65,7 +63,6 @@ extern int write_batch;
383 extern struct exclude_list_struct exclude_list;
384 extern struct exclude_list_struct server_exclude_list;
385 -extern struct exclude_list_struct local_exclude_list;
389 @@ -210,8 +207,6 @@ int link_stat(const char *path, STRUCT_S
391 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
395 #if 0 /* This currently never happens, so avoid a useless compare. */
396 if (exclude_level == NO_EXCLUDES)
398 @@ -233,10 +228,7 @@ static int check_exclude_file(char *fnam
399 if (exclude_level != ALL_EXCLUDES)
401 if (exclude_list.head
402 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
404 - if (local_exclude_list.head
405 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
406 + && check_exclude(&exclude_list, fname, is_dir) < 0)
410 @@ -946,15 +938,7 @@ void send_file_name(int f, struct file_l
412 if (recursive && S_ISDIR(file->mode)
413 && !(file->flags & FLAG_MOUNT_POINT)) {
414 - struct exclude_list_struct last_list = local_exclude_list;
415 - local_exclude_list.head = local_exclude_list.tail = NULL;
416 send_directory(f, flist, f_name_to(file, fbuf));
418 - rprintf(FINFO, "[%s] popping %sexclude list\n",
419 - who_am_i(), local_exclude_list.debug_type);
421 - free_exclude_list(&local_exclude_list);
422 - local_exclude_list = last_list;
426 @@ -965,6 +949,7 @@ static void send_directory(int f, struct
428 char fname[MAXPATHLEN];
430 + void *save_excludes;
434 @@ -988,18 +973,7 @@ static void send_directory(int f, struct
439 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
440 - < MAXPATHLEN - offset) {
441 - add_exclude_file(&local_exclude_list, fname,
442 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
444 - io_error |= IOERR_GENERAL;
446 - "cannot cvs-exclude in long-named directory %s\n",
447 - full_fname(fname));
450 + save_excludes = push_local_excludes(fname, offset);
452 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
453 char *dname = d_name(di);
454 @@ -1019,6 +993,8 @@ static void send_directory(int f, struct
455 io_error |= IOERR_GENERAL;
456 rsyserr(FERROR, errno, "readdir(%s)", dir);
459 + pop_local_excludes(save_excludes);
463 --- rsync.h 16 May 2004 07:28:24 -0000 1.204
464 +++ rsync.h 16 May 2004 07:44:57 -0000
465 @@ -496,16 +496,21 @@ struct map_struct {
466 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
467 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
468 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
469 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
470 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
471 struct exclude_struct {
472 struct exclude_struct *next;
474 unsigned int match_flags;
482 struct exclude_list_struct {
483 - struct exclude_struct *head;
484 - struct exclude_struct *tail;
485 + struct exclude_struct *head, *tail;
486 + struct exclude_struct *parent;
490 --- rsync.yo 7 May 2004 00:18:37 -0000 1.169
491 +++ rsync.yo 16 May 2004 07:44:58 -0000
492 @@ -1075,6 +1075,72 @@ itemize(
493 it would be excluded by the "*")
496 +manpagesection(MERGING EXCLUDE FILES)
498 +You can merge whole files into an exclude file using a rule that starts
499 +with a ". " (a dot followed by a space) and has a filename in place of the
500 +pattern. There are two types of merge rules, single-instance and
504 + it() If the filename has no slashes in it, it is a per-directory merge;
505 + rsync scans every directory that it traverses for the named file, merging
506 + its contents (when it exists) file at the start of this per-directory
507 + sub-list (subdirectories inherit the contents of their parent directories
508 + by default, and each subdirectory's rules have precedence over the parent
509 + directory's rules).
511 + it() If a filename has a slash in it, it is a single-instance merge; the
512 + named file's contents will be merged into the current exclude file,
513 + replacing the merge rule. Thus, you should use the name ./foo instead of
514 + foo if you don't want to scan for "foo" in all the subdirectories of the
518 +Note also that you can eliminate all the inherited rules for the current
519 +per-directory ruleset by putting the list-clearing token (!) in the file.
520 +This only clears the rules for the current per-directory sub-list (up
521 +through the ! token) and only for the current directory and its
524 +Here's an example exclude file (which you'd specify via the normal
525 +--exclude-from option):
528 + . /home/user/.global_excludes
535 +This will merge the contents of the /home/user/.global_excludes file at the
536 +start of the list and also turns the ".excl" filename into a per-directory
537 +exclude file whose local contents will be merged into the list in place of
540 +Additionally, you can affect where the --cvs-exclude (-C) option's
541 +inclusion of a per-directory .cvsignore file gets placed into your rules by
542 +adding an explicit merge rule for ".cvsignore". For instance, specifying
546 + rsync -avC --exclude='. .cvsignore' --exclude-from=foo a/ b
549 +will merge all the per-directory .cvsignore rules at the start of your list
550 +rather than at the end. This allows their dir-specific rules to supersede
551 +your rules instead of being subservient to them. (The global rules taken
552 +from the $HOME/.cvsignore file and from $CVSIGNORE are not affected by
555 +Note also that the parsing of any merge-file named ".cvsignore" is always
556 +done in a CVS-compatible manner (even if -C wasn't specified) -- i.e. the
557 +rules are always exclude rules (even when specified by an include option),
558 +they are split on whitespace, no special prefixes or list-clearing tokens
559 +are honored, and (for per-directory files) subdirectories don't inherit the
560 +parent directory's rules.
562 manpagesection(BATCH MODE)
564 bf(Note:) Batch mode should be considered experimental in this version