Took the general exclude-list improvements from this patch (memory
[rsync/rsync-patches.git] / filter.diff
1 After applying this patch and running configure, you MUST run this
2 command before "make":
3
4     make proto
5
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.
11
12 For example:
13
14   rsync -av --exclude='. .excl' from/ to
15
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
19 this:
20
21   + *.c
22   . .excl2
23   . ./.excl3
24   *.o
25
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.
29
30 ..wayne..
31
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;
35  extern int eol_nulls;
36  extern int list_only;
37  extern int recurse;
38 +extern int io_error;
39 +extern int sanitize_paths;
40  
41  extern char curr_dir[];
42  
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;
49  
50 +struct exclude_list_root {
51 +    struct exclude_list_struct *array;
52 +    int cnt;
53 +} local_lists;
54 +
55 +static char dirbuf[MAXPATHLEN];
56 +static unsigned int dirbuf_offset = 0;
57 +
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:
66 + *
67 + * Completely Empty                     Local Content Only
68 + * ==================================   ====================================
69 + * head -> NULL                         head -> Local1 -> Local2 -> NULL
70 + * tail -> NULL                         tail -------------^
71 + *
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 ---------^
76 + *
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.
87 + */
88 +
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_
93         const char *cp;
94         unsigned int ex_len;
95  
96 +       if (mflags & MATCHFLG_MERGE_FILE) {
97 +               struct exclude_struct *ex;
98 +               /* If the local include file was already mentioned, don't
99 +                * add it again. */
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)
104 +                               return;
105 +               }
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;
110 +               } else
111 +                       mflags &= ~MATCHFLG_CVSIGNORE;
112 +       }
113 +
114         ret = new(struct exclude_struct);
115         if (!ret)
116                 out_of_memory("make_exclude");
117 @@ -82,15 +140,32 @@ static void make_exclude(struct exclude_
118         }
119  
120         for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
121 -               ret->slash_cnt++;
122 +               ret->u.slash_cnt++;
123  
124 -       if (!listp->tail)
125 +       if (!listp->tail) {
126 +               ret->next = listp->head;
127                 listp->head = listp->tail = ret;
128 -       else {
129 +       } else {
130 +               ret->next = listp->tail->next;
131                 listp->tail->next = ret;
132                 listp->tail = ret;
133         }
134  
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;
144 +               lp->parent = ret;
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. */
148 +       }
149 +
150         ret->match_flags = mflags;
151  }
152  
153 @@ -100,18 +175,90 @@ static void free_exclude(struct exclude_
154         free(ex);
155  }
156  
157 -void free_exclude_list(struct exclude_list_struct *listp)
158 +static void free_exclude_list(struct exclude_list_struct *listp)
159  {
160 -       struct exclude_struct *ent, *next;
161 -
162 -       for (ent = listp->head; ent; ent = next) {
163 -               next = ent->next;
164 -               free_exclude(ent);
165 +       if (listp->tail) {
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) {
170 +                       next = ent->next;
171 +                       free_exclude(ent);
172 +               }
173         }
174  
175         listp->head = listp->tail = NULL;
176  }
177  
178 +void *push_local_excludes(char *fname, unsigned int offset)
179 +{
180 +       int i;
181 +       struct exclude_list_root *push = new_array(struct exclude_list_root, 1);
182 +
183 +       if (!push)
184 +               out_of_memory("push_local_excludes");
185 +
186 +       push->cnt = local_lists.cnt;
187 +       push->array = new_array(struct exclude_list_struct, local_lists.cnt);
188 +       if (!push->array)
189 +               out_of_memory("push_local_excludes");
190 +
191 +       memcpy(push->array, local_lists.array,
192 +           sizeof (struct exclude_list_struct) * local_lists.cnt);
193 +
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;
198 +
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;
202 +               int flags;
203 +
204 +               if (verbose > 2) {
205 +                       rprintf(FINFO, "[%s] pushing %sexclude list\n",
206 +                               who_am_i(), listp->debug_type);
207 +               }
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;
211 +               } else {
212 +                       flags = listp->parent->match_flags & MATCHFLG_INCLUDE
213 +                           ? XFLG_DEF_INCLUDE : 0;
214 +               }
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);
219 +               else {
220 +                       io_error |= IOERR_GENERAL;
221 +                       rprintf(FINFO,
222 +                           "cannot add local excludes in long-named directory %s\n",
223 +                           full_fname(fname));
224 +               }
225 +       }
226 +
227 +       return (void*)push;
228 +}
229 +
230 +void pop_local_excludes(void *mem)
231 +{
232 +       int i;
233 +
234 +       for (i = 0; i < local_lists.cnt; i++) {
235 +               struct exclude_list_struct *listp = &local_lists.array[i];
236 +               if (verbose > 2) {
237 +                       rprintf(FINFO, "[%s] popping %sexclude list\n",
238 +                               who_am_i(), listp->debug_type);
239 +               }
240 +               free_exclude_list(listp);
241 +       }
242 +       free(local_lists.array);
243 +       local_lists = *(struct exclude_list_root*)mem;
244 +       free(mem);
245 +}
246 +
247  static int check_one_exclude(char *name, struct exclude_struct *ex,
248                               int name_is_dir)
249  {
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)
257                         name = p+1;
258         }
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)
270                                         break;
271 @@ -221,6 +368,14 @@ int check_exclude(struct exclude_list_st
272         struct exclude_struct *ent;
273  
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);
279 +                       if (rc)
280 +                               return rc;
281 +                       continue;
282 +               }
283                 if (check_one_exclude(name, ent, name_is_dir)) {
284                         report_exclude_result(name, ent, name_is_dir,
285                                               listp->debug_type);
286 @@ -254,11 +409,16 @@ static const char *get_exclude_tok(const
287                 p = (const char *)s;
288         }
289  
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] == ' ') {
295                 if (*s == '+')
296                         mflags |= MATCHFLG_INCLUDE;
297 +               else if (*s == '.') {
298 +                       mflags |= MATCHFLG_MERGE_FILE;
299 +                       if (xflags & XFLG_DEF_INCLUDE)
300 +                               mflags |= MATCHFLG_INCLUDE;
301 +               }
302                 s += 2;
303         } else if (xflags & XFLG_DEF_INCLUDE)
304                 mflags |= MATCHFLG_INCLUDE;
305 @@ -307,11 +467,42 @@ void add_exclude(struct exclude_list_str
306                         continue;
307                 }
308  
309 +               if (mflags & MATCHFLG_MERGE_FILE) {
310 +                       char name[MAXPATHLEN];
311 +                       if (pat_len >= sizeof name) {
312 +                               rprintf(FERROR,
313 +                                       "merge filename too long: %s\n", cp);
314 +                               continue;
315 +                       }
316 +                       strlcpy(name, cp, pat_len+1);
317 +                       if (strchr(name, '/') != NULL) {
318 +                               if (sanitize_paths)
319 +                                       sanitize_path(name, curr_dir);
320 +                               if (*name == '/')
321 +                                       cp = name;
322 +                               else {
323 +                                       if (strlcpy(dirbuf + dirbuf_offset,
324 +                                           name, MAXPATHLEN - dirbuf_offset)
325 +                                           >= MAXPATHLEN - dirbuf_offset) {
326 +                                               rprintf(FERROR,
327 +                                                   "merge filename too long: %s...\n",
328 +                                                   dirbuf);
329 +                                               continue;
330 +                                       }
331 +                                       cp = dirbuf;
332 +                               }
333 +                               add_exclude_file(listp, cp,
334 +                                   xflags | XFLG_FATAL_ERRORS);
335 +                               continue;
336 +                       }
337 +               }
338 +
339                 make_exclude(listp, cp, pat_len, mflags);
340  
341                 if (verbose > 2) {
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");
347                 }
348         }
349 @@ -403,7 +594,11 @@ void send_exclude_list(int f)
350                 if (ent->match_flags & MATCHFLG_INCLUDE) {
351                         write_int(f, l + 2);
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 == '.')
358 +                   && p[1] == ' ') {
359                         write_int(f, l + 2);
360                         write_buf(f, "- ", 2);
361                 } else
362 @@ -444,6 +639,7 @@ void add_cvs_excludes(void)
363         char fname[MAXPATHLEN];
364         char *p;
365  
366 +       add_exclude(&exclude_list, ". .cvsignore", 0);
367         add_exclude(&exclude_list, default_cvsignore,
368                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
369  
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;
375  
376 -extern int cvs_exclude;
377 -
378  extern int recurse;
379  extern char curr_dir[MAXPATHLEN];
380  extern char *files_from;
381 @@ -65,7 +63,6 @@ extern int write_batch;
382  
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;
386  
387  int io_error;
388  
389 @@ -210,8 +207,6 @@ int link_stat(const char *path, STRUCT_S
390   */
391  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
392  {
393 -       int rc;
394 -
395  #if 0 /* This currently never happens, so avoid a useless compare. */
396         if (exclude_level == NO_EXCLUDES)
397                 return 0;
398 @@ -233,10 +228,7 @@ static int check_exclude_file(char *fnam
399         if (exclude_level != ALL_EXCLUDES)
400                 return 0;
401         if (exclude_list.head
402 -           && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
403 -               return rc < 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)
407                 return 1;
408         return 0;
409  }
410 @@ -946,15 +938,7 @@ void send_file_name(int f, struct file_l
411  
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));
417 -               if (verbose > 2) {
418 -                       rprintf(FINFO, "[%s] popping %sexclude list\n",
419 -                               who_am_i(), local_exclude_list.debug_type);
420 -               }
421 -               free_exclude_list(&local_exclude_list);
422 -               local_exclude_list = last_list;
423         }
424  }
425  
426 @@ -965,6 +949,7 @@ static void send_directory(int f, struct
427         struct dirent *di;
428         char fname[MAXPATHLEN];
429         unsigned int offset;
430 +       void *save_excludes;
431         char *p;
432  
433         d = opendir(dir);
434 @@ -988,18 +973,7 @@ static void send_directory(int f, struct
435                 offset++;
436         }
437  
438 -       if (cvs_exclude) {
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);
443 -               } else {
444 -                       io_error |= IOERR_GENERAL;
445 -                       rprintf(FINFO,
446 -                               "cannot cvs-exclude in long-named directory %s\n",
447 -                               full_fname(fname));
448 -               }
449 -       }
450 +       save_excludes = push_local_excludes(fname, offset);
451  
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);
457         }
458 +
459 +       pop_local_excludes(save_excludes);
460  
461         closedir(d);
462  }
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;
473         char *pattern;
474         unsigned int match_flags;
475 -       int slash_cnt;
476 +       union {
477 +               int slash_cnt;
478 +               int array_index;
479 +       } u;
480  };
481  
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;
487         char *debug_type;
488  };
489  
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 "*")
494  )
495  
496 +manpagesection(MERGING EXCLUDE FILES)
497 +
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
501 +per-directory:
502 +
503 +itemize(
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).
510 +
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
515 +  current directory.
516 +)
517 +
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
522 +subdirectories.
523 +
524 +Here's an example exclude file (which you'd specify via the normal
525 +--exclude-from option):
526 +
527 +verb(
528 +    . /home/user/.global_excludes
529 +    - *.gz
530 +    . .excl
531 +    + *.[ch]
532 +    - *.o
533 +)
534 +
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
538 +the .excl line.
539 +
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
543 +this:
544 +
545 +verb(
546 +  rsync -avC --exclude='. .cvsignore' --exclude-from=foo a/ b
547 +)
548 +
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
553 +this.)
554 +
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.
561 +
562  manpagesection(BATCH MODE)
563  
564  bf(Note:) Batch mode should be considered experimental in this version