Fixed a problem where the "listp" pointer in make_exclude() might become
[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 14:08:34 -0000      1.79
33 +++ exclude.c   16 May 2004 22:04:31 -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 @@ -81,14 +139,35 @@ static void make_exclude(struct exclude_
118                 mflags |= MATCHFLG_DIRECTORY;
119         }
120  
121 -       for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
122 -               ret->slash_cnt++;
123 +       if (mflags & MATCHFLG_MERGE_FILE) {
124 +               struct exclude_list_struct *lp = local_lists.array;
125 +               int ndx = local_lists.cnt++;
126 +               local_lists.array = realloc_array(local_lists.array,
127 +                   struct exclude_list_struct, local_lists.cnt);
128 +               if (!local_lists.array)
129 +                       out_of_memory("make_exclude");
130 +               /* If "listp" was pointing inside the memory we just
131 +                * reallocated, update it. */
132 +               if (ndx && listp >= lp && listp < lp + ndx)
133 +                       listp = &local_lists.array[listp - lp];
134 +               lp = &local_lists.array[ndx];
135 +               lp->head = lp->tail = NULL;
136 +               lp->parent = ret;
137 +               if (asprintf(&lp->debug_type, "per-dir %s ", ret->pattern) < 0)
138 +                       out_of_memory("make_exclude");
139 +               ret->u.array_index = ndx;
140 +       } else {
141 +               for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
142 +                       ret->u.slash_cnt++;
143 +       }
144  
145         ret->match_flags = mflags;
146  
147 -       if (!listp->tail)
148 +       if (!listp->tail) {
149 +               ret->next = listp->head;
150                 listp->head = listp->tail = ret;
151 -       else {
152 +       } else {
153 +               ret->next = listp->tail->next;
154                 listp->tail->next = ret;
155                 listp->tail = ret;
156         }
157 @@ -100,18 +179,90 @@ static void free_exclude(struct exclude_
158         free(ex);
159  }
160  
161 -void free_exclude_list(struct exclude_list_struct *listp)
162 +static void free_exclude_list(struct exclude_list_struct *listp)
163  {
164 -       struct exclude_struct *ent, *next;
165 -
166 -       for (ent = listp->head; ent; ent = next) {
167 -               next = ent->next;
168 -               free_exclude(ent);
169 +       if (listp->tail) {
170 +               struct exclude_struct *ent, *next;
171 +               /* Truncate any inherited items from the local list. */
172 +               listp->tail->next = NULL;
173 +               for (ent = listp->head; ent; ent = next) {
174 +                       next = ent->next;
175 +                       free_exclude(ent);
176 +               }
177         }
178  
179         listp->head = listp->tail = NULL;
180  }
181  
182 +void *push_local_excludes(char *fname, unsigned int offset)
183 +{
184 +       int i;
185 +       struct exclude_list_root *push = new_array(struct exclude_list_root, 1);
186 +
187 +       if (!push)
188 +               out_of_memory("push_local_excludes");
189 +
190 +       push->cnt = local_lists.cnt;
191 +       push->array = new_array(struct exclude_list_struct, local_lists.cnt);
192 +       if (!push->array)
193 +               out_of_memory("push_local_excludes");
194 +
195 +       memcpy(push->array, local_lists.array,
196 +           sizeof (struct exclude_list_struct) * local_lists.cnt);
197 +
198 +       /* Make it easy to construct the full path for a merge-file that was
199 +        * specified with a relative path by saving off the current dir. */
200 +       memcpy(dirbuf, fname, offset);
201 +       dirbuf_offset = offset;
202 +
203 +       for (i = 0; i < local_lists.cnt; i++) {
204 +               struct exclude_list_struct *listp = &local_lists.array[i];
205 +               char *file = listp->parent->pattern;
206 +               int flags;
207 +
208 +               if (verbose > 2) {
209 +                       rprintf(FINFO, "[%s] pushing %sexclude list\n",
210 +                               who_am_i(), listp->debug_type);
211 +               }
212 +               if (listp->parent->match_flags & MATCHFLG_CVSIGNORE) {
213 +                       listp->head = NULL; /* Subdirs don't inherit rules. */
214 +                       flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
215 +               } else {
216 +                       flags = listp->parent->match_flags & MATCHFLG_INCLUDE
217 +                           ? XFLG_DEF_INCLUDE : 0;
218 +               }
219 +               listp->tail = NULL; /* Signals no local content. */
220 +               if (strlcpy(fname +  offset, file, MAXPATHLEN - offset)
221 +                   < MAXPATHLEN - offset)
222 +                       add_exclude_file(listp, fname, flags);
223 +               else {
224 +                       io_error |= IOERR_GENERAL;
225 +                       rprintf(FINFO,
226 +                           "cannot add local excludes in long-named directory %s\n",
227 +                           full_fname(fname));
228 +               }
229 +       }
230 +
231 +       return (void*)push;
232 +}
233 +
234 +void pop_local_excludes(void *mem)
235 +{
236 +       int i;
237 +
238 +       for (i = 0; i < local_lists.cnt; i++) {
239 +               struct exclude_list_struct *listp = &local_lists.array[i];
240 +               if (verbose > 2) {
241 +                       rprintf(FINFO, "[%s] popping %sexclude list\n",
242 +                               who_am_i(), listp->debug_type);
243 +               }
244 +               free_exclude_list(listp);
245 +       }
246 +       free(local_lists.array);
247 +       local_lists = *(struct exclude_list_root*)mem;
248 +       free(mem);
249 +}
250 +
251  static int check_one_exclude(char *name, struct exclude_struct *ex,
252                               int name_is_dir)
253  {
254 @@ -122,7 +273,7 @@ static int check_one_exclude(char *name,
255         /* If the pattern does not have any slashes AND it does not have
256          * a "**" (which could match a slash), then we just match the
257          * name portion of the path. */
258 -       if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
259 +       if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
260                 if ((p = strrchr(name,'/')) != NULL)
261                         name = p+1;
262         }
263 @@ -148,9 +299,9 @@ static int check_one_exclude(char *name,
264         if (ex->match_flags & MATCHFLG_WILD) {
265                 /* A non-anchored match with an infix slash and no "**"
266                  * needs to match the last slash_cnt+1 name elements. */
267 -               if (!match_start && ex->slash_cnt
268 +               if (!match_start && ex->u.slash_cnt
269                     && !(ex->match_flags & MATCHFLG_WILD2)) {
270 -                       int cnt = ex->slash_cnt + 1;
271 +                       int cnt = ex->u.slash_cnt + 1;
272                         for (p = name + strlen(name) - 1; p >= name; p--) {
273                                 if (*p == '/' && !--cnt)
274                                         break;
275 @@ -221,6 +372,14 @@ int check_exclude(struct exclude_list_st
276         struct exclude_struct *ent;
277  
278         for (ent = listp->head; ent; ent = ent->next) {
279 +               if (ent->match_flags & MATCHFLG_MERGE_FILE) {
280 +                       struct exclude_list_struct *lp
281 +                           = &local_lists.array[ent->u.array_index];
282 +                       int rc = check_exclude(lp, name, name_is_dir);
283 +                       if (rc)
284 +                               return rc;
285 +                       continue;
286 +               }
287                 if (check_one_exclude(name, ent, name_is_dir)) {
288                         report_exclude_result(name, ent, name_is_dir,
289                                               listp->debug_type);
290 @@ -254,11 +413,16 @@ static const char *get_exclude_tok(const
291                 p = (const char *)s;
292         }
293  
294 -       /* Is this a '+' or '-' followed by a space (not whitespace)? */
295 +       /* Is this a +/-/. followed by a space (not whitespace)? */
296         if (!(xflags & XFLG_WORDS_ONLY)
297 -           && (*s == '-' || *s == '+') && s[1] == ' ') {
298 +           && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
299                 if (*s == '+')
300                         mflags |= MATCHFLG_INCLUDE;
301 +               else if (*s == '.') {
302 +                       mflags |= MATCHFLG_MERGE_FILE;
303 +                       if (xflags & XFLG_DEF_INCLUDE)
304 +                               mflags |= MATCHFLG_INCLUDE;
305 +               }
306                 s += 2;
307         } else if (xflags & XFLG_DEF_INCLUDE)
308                 mflags |= MATCHFLG_INCLUDE;
309 @@ -307,11 +471,42 @@ void add_exclude(struct exclude_list_str
310                         continue;
311                 }
312  
313 +               if (mflags & MATCHFLG_MERGE_FILE) {
314 +                       char name[MAXPATHLEN];
315 +                       if (pat_len >= sizeof name) {
316 +                               rprintf(FERROR,
317 +                                       "merge filename too long: %s\n", cp);
318 +                               continue;
319 +                       }
320 +                       strlcpy(name, cp, pat_len+1);
321 +                       if (strchr(name, '/') != NULL) {
322 +                               if (sanitize_paths)
323 +                                       sanitize_path(name, curr_dir);
324 +                               if (*name == '/')
325 +                                       cp = name;
326 +                               else {
327 +                                       if (strlcpy(dirbuf + dirbuf_offset,
328 +                                           name, MAXPATHLEN - dirbuf_offset)
329 +                                           >= MAXPATHLEN - dirbuf_offset) {
330 +                                               rprintf(FERROR,
331 +                                                   "merge filename too long: %s...\n",
332 +                                                   dirbuf);
333 +                                               continue;
334 +                                       }
335 +                                       cp = dirbuf;
336 +                               }
337 +                               add_exclude_file(listp, cp,
338 +                                   xflags | XFLG_FATAL_ERRORS);
339 +                               continue;
340 +                       }
341 +               }
342 +
343                 make_exclude(listp, cp, pat_len, mflags);
344  
345                 if (verbose > 2) {
346 -                       rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
347 +                       rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
348                                 who_am_i(), pat_len, cp, listp->debug_type,
349 +                               mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
350                                 mflags & MATCHFLG_INCLUDE ? "in" : "ex");
351                 }
352         }
353 @@ -403,7 +598,11 @@ void send_exclude_list(int f)
354                 if (ent->match_flags & MATCHFLG_INCLUDE) {
355                         write_int(f, l + 2);
356                         write_buf(f, "+ ", 2);
357 -               } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
358 +               } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
359 +                       write_int(f, l + 2);
360 +                       write_buf(f, ". ", 2);
361 +               } else if ((*p == '-' || *p == '+' || *p == '.')
362 +                   && p[1] == ' ') {
363                         write_int(f, l + 2);
364                         write_buf(f, "- ", 2);
365                 } else
366 @@ -444,6 +643,7 @@ void add_cvs_excludes(void)
367         char fname[MAXPATHLEN];
368         char *p;
369  
370 +       add_exclude(&exclude_list, ". .cvsignore", 0);
371         add_exclude(&exclude_list, default_cvsignore,
372                     XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
373  
374 --- flist.c     15 May 2004 19:31:10 -0000      1.223
375 +++ flist.c     16 May 2004 22:04:31 -0000
376 @@ -39,8 +39,6 @@ extern int module_id;
377  extern int ignore_errors;
378  extern int numeric_ids;
379  
380 -extern int cvs_exclude;
381 -
382  extern int recurse;
383  extern char curr_dir[MAXPATHLEN];
384  extern char *files_from;
385 @@ -65,7 +63,6 @@ extern int write_batch;
386  
387  extern struct exclude_list_struct exclude_list;
388  extern struct exclude_list_struct server_exclude_list;
389 -extern struct exclude_list_struct local_exclude_list;
390  
391  int io_error;
392  
393 @@ -210,8 +207,6 @@ int link_stat(const char *path, STRUCT_S
394   */
395  static int check_exclude_file(char *fname, int is_dir, int exclude_level)
396  {
397 -       int rc;
398 -
399  #if 0 /* This currently never happens, so avoid a useless compare. */
400         if (exclude_level == NO_EXCLUDES)
401                 return 0;
402 @@ -233,10 +228,7 @@ static int check_exclude_file(char *fnam
403         if (exclude_level != ALL_EXCLUDES)
404                 return 0;
405         if (exclude_list.head
406 -           && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
407 -               return rc < 0;
408 -       if (local_exclude_list.head
409 -           && check_exclude(&local_exclude_list, fname, is_dir) < 0)
410 +           && check_exclude(&exclude_list, fname, is_dir) < 0)
411                 return 1;
412         return 0;
413  }
414 @@ -946,15 +938,7 @@ void send_file_name(int f, struct file_l
415  
416         if (recursive && S_ISDIR(file->mode)
417             && !(file->flags & FLAG_MOUNT_POINT)) {
418 -               struct exclude_list_struct last_list = local_exclude_list;
419 -               local_exclude_list.head = local_exclude_list.tail = NULL;
420                 send_directory(f, flist, f_name_to(file, fbuf));
421 -               if (verbose > 2) {
422 -                       rprintf(FINFO, "[%s] popping %sexclude list\n",
423 -                               who_am_i(), local_exclude_list.debug_type);
424 -               }
425 -               free_exclude_list(&local_exclude_list);
426 -               local_exclude_list = last_list;
427         }
428  }
429  
430 @@ -965,6 +949,7 @@ static void send_directory(int f, struct
431         struct dirent *di;
432         char fname[MAXPATHLEN];
433         unsigned int offset;
434 +       void *save_excludes;
435         char *p;
436  
437         d = opendir(dir);
438 @@ -988,18 +973,7 @@ static void send_directory(int f, struct
439                 offset++;
440         }
441  
442 -       if (cvs_exclude) {
443 -               if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
444 -                   < MAXPATHLEN - offset) {
445 -                       add_exclude_file(&local_exclude_list, fname,
446 -                                        XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
447 -               } else {
448 -                       io_error |= IOERR_GENERAL;
449 -                       rprintf(FINFO,
450 -                               "cannot cvs-exclude in long-named directory %s\n",
451 -                               full_fname(fname));
452 -               }
453 -       }
454 +       save_excludes = push_local_excludes(fname, offset);
455  
456         for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
457                 char *dname = d_name(di);
458 @@ -1019,6 +993,8 @@ static void send_directory(int f, struct
459                 io_error |= IOERR_GENERAL;
460                 rsyserr(FERROR, errno, "readdir(%s)", dir);
461         }
462 +
463 +       pop_local_excludes(save_excludes);
464  
465         closedir(d);
466  }
467 --- rsync.h     16 May 2004 07:28:24 -0000      1.204
468 +++ rsync.h     16 May 2004 22:04:33 -0000
469 @@ -496,16 +496,21 @@ struct map_struct {
470  #define MATCHFLG_INCLUDE       (1<<4) /* this is an include, not an exclude */
471  #define MATCHFLG_DIRECTORY     (1<<5) /* this matches only directories */
472  #define MATCHFLG_CLEAR_LIST    (1<<6) /* this item is the "!" token */
473 +#define MATCHFLG_MERGE_FILE    (1<<7) /* specifies a file to merge */
474 +#define MATCHFLG_CVSIGNORE     (1<<8) /* parse this as a .cvsignore file */
475  struct exclude_struct {
476         struct exclude_struct *next;
477         char *pattern;
478         unsigned int match_flags;
479 -       int slash_cnt;
480 +       union {
481 +               int slash_cnt;
482 +               int array_index;
483 +       } u;
484  };
485  
486  struct exclude_list_struct {
487 -       struct exclude_struct *head;
488 -       struct exclude_struct *tail;
489 +       struct exclude_struct *head, *tail;
490 +       struct exclude_struct *parent;
491         char *debug_type;
492  };
493  
494 --- rsync.yo    7 May 2004 00:18:37 -0000       1.169
495 +++ rsync.yo    16 May 2004 22:04:33 -0000
496 @@ -1075,6 +1075,72 @@ itemize(
497    it would be excluded by the "*")
498  )
499  
500 +manpagesection(MERGING EXCLUDE FILES)
501 +
502 +You can merge whole files into an exclude file using a rule that starts
503 +with a ". " (a dot followed by a space) and has a filename in place of the
504 +pattern.  There are two types of merge rules, single-instance and
505 +per-directory:
506 +
507 +itemize(
508 +  it() If the filename has no slashes in it, it is a per-directory merge;
509 +  rsync scans every directory that it traverses for the named file, merging
510 +  its contents (when it exists) file at the start of this per-directory
511 +  sub-list (subdirectories inherit the contents of their parent directories
512 +  by default, and each subdirectory's rules have precedence over the parent
513 +  directory's rules).
514 +
515 +  it() If a filename has a slash in it, it is a single-instance merge; the
516 +  named file's contents will be merged into the current exclude file,
517 +  replacing the merge rule.  Thus, you should use the name ./foo instead of
518 +  foo if you don't want to scan for "foo" in all the subdirectories of the
519 +  current directory.
520 +)
521 +
522 +Note also that you can eliminate all the inherited rules for the current
523 +per-directory ruleset by putting the list-clearing token (!) in the file.
524 +This only clears the rules for the current per-directory sub-list (up
525 +through the ! token) and only for the current directory and its
526 +subdirectories.
527 +
528 +Here's an example exclude file (which you'd specify via the normal
529 +--exclude-from option):
530 +
531 +verb(
532 +    . /home/user/.global_excludes
533 +    - *.gz
534 +    . .excl
535 +    + *.[ch]
536 +    - *.o
537 +)
538 +
539 +This will merge the contents of the /home/user/.global_excludes file at the
540 +start of the list and also turns the ".excl" filename into a per-directory
541 +exclude file whose local contents will be merged into the list in place of
542 +the .excl line.
543 +
544 +Additionally, you can affect where the --cvs-exclude (-C) option's
545 +inclusion of a per-directory .cvsignore file gets placed into your rules by
546 +adding an explicit merge rule for ".cvsignore".  For instance, specifying
547 +this:
548 +
549 +verb(
550 +  rsync -avC --exclude='. .cvsignore' --exclude-from=foo a/ b
551 +)
552 +
553 +will merge all the per-directory .cvsignore rules at the start of your list
554 +rather than at the end.  This allows their dir-specific rules to supersede
555 +your rules instead of being subservient to them.  (The global rules taken
556 +from the $HOME/.cvsignore file and from $CVSIGNORE are not affected by
557 +this.)
558 +
559 +Note also that the parsing of any merge-file named ".cvsignore" is always
560 +done in a CVS-compatible manner (even if -C wasn't specified) -- i.e. the
561 +rules are always exclude rules (even when specified by an include option),
562 +they are split on whitespace, no special prefixes or list-clearing tokens
563 +are honored, and (for per-directory files) subdirectories don't inherit the
564 +parent directory's rules.
565 +
566  manpagesection(BATCH MODE)
567  
568  bf(Note:) Batch mode should be considered experimental in this version