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 with a preceding -p
8 option, that filename will be looked for in every subdirectory that
9 rsync visits, and the rules found in that subdirectory's file will
10 affect that dir and its subdirs.
14 rsync -av --exclude='. -p .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
27 Then the file ".excl2" will also be read in from the current dir and all
28 its subdirs (due to the -p option). The file ".excl3" would just be
29 read in from the current dir. The exclusion of "foobar" will only
30 happen in that .excl file's directory because the rule is anchored (so
31 that's how you can make rules local instead of inherited).
35 --- orig/clientserver.c 2004-08-02 02:29:16
36 +++ clientserver.c 2004-08-09 17:35:56
37 @@ -48,12 +48,14 @@ extern int no_detach;
38 extern int default_af_hint;
39 extern char *bind_address;
40 extern struct exclude_list_struct server_exclude_list;
41 -extern char *exclude_path_prefix;
42 extern char *config_file;
43 extern char *files_from;
47 +/* The length of the lp_path() string (when we're not chrooted). */
48 +unsigned int module_dirlen = 0;
51 * Run a client connected to an rsyncd. The alternative to this
52 * function for remote-shell connections is do_cmd().
53 @@ -300,26 +302,28 @@ static int rsync_module(int f_in, int f_
54 /* TODO: Perhaps take a list of gids, and make them into the
55 * supplementary groups. */
57 - exclude_path_prefix = use_chroot? "" : lp_path(i);
58 - if (*exclude_path_prefix == '/' && !exclude_path_prefix[1])
59 - exclude_path_prefix = "";
62 + set_excludes_dir("/", 1);
64 + module_dirlen = strlen(lp_path(i));
65 + set_excludes_dir(lp_path(i), module_dirlen);
68 p = lp_include_from(i);
69 add_exclude_file(&server_exclude_list, p,
70 - XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE);
71 + XFLG_FATAL_ERRORS | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
74 add_exclude(&server_exclude_list, p,
75 - XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE);
76 + XFLG_WORD_SPLIT | XFLG_DEF_INCLUDE | XFLG_ABS_PATH);
78 p = lp_exclude_from(i);
79 add_exclude_file(&server_exclude_list, p,
81 + XFLG_FATAL_ERRORS | XFLG_ABS_PATH);
84 - add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT);
86 - exclude_path_prefix = NULL;
87 + add_exclude(&server_exclude_list, p, XFLG_WORD_SPLIT | XFLG_ABS_PATH);
91 --- orig/exclude.c 2004-08-05 23:16:37
92 +++ exclude.c 2004-08-10 04:51:05
97 +extern int am_sender;
101 +extern int io_error;
102 +extern int module_id;
103 +extern int delete_mode;
104 +extern int delete_excluded;
105 +extern int sanitize_paths;
107 extern char curr_dir[];
108 +extern unsigned int curr_dir_len;
109 +extern unsigned int module_dirlen;
111 struct exclude_list_struct exclude_list = { 0, 0, "" };
112 -struct exclude_list_struct local_exclude_list = { 0, 0, "per-dir .cvsignore " };
113 struct exclude_list_struct server_exclude_list = { 0, 0, "server " };
114 -char *exclude_path_prefix = NULL;
116 +struct mergelist_save_struct {
117 + struct exclude_list_struct *array;
121 +/* The dirbuf is set by push_local_excludes() to the current subdirectory
122 + * relative to curr_dir that is being processed. The path always has a
123 + * trailing slash appended, and the variable dirbuf_len contains the length
124 + * of this path prefix. The path is always absolute. */
125 +static char dirbuf[MAXPATHLEN+1];
126 +static unsigned int dirbuf_len = 0;
128 +/* This is True when we're scanning parent dirs for per-dir merge-files. */
129 +static BOOL parent_dirscan = False;
131 +/* This array contains a list of all the currently active per-dir merge
132 + * files. This makes it easier to save the appropriate values when we
133 + * "push" down into each subdirectory. */
134 +static struct exclude_struct **mergelist_parents;
135 +static int mergelist_cnt = 0;
136 +static int mergelist_size = 0;
138 +/* Each exclude_list_struct describes a singly-linked list by keeping track
139 + * of both the head and tail pointers. The list is slightly unusual in that
140 + * a parent-dir's content can be appended to the end of the local list in a
141 + * special way: the last item in the local list has its "next" pointer set
142 + * to point to the inherited list, but the local list's tail pointer points
143 + * at the end of the local list. Thus, if the local list is empty, the head
144 + * will be pointing at the inherited content but the tail will be NULL. To
145 + * help you visualize this, here are the possible list arrangements:
147 + * Completely Empty Local Content Only
148 + * ================================== ====================================
149 + * head -> NULL head -> Local1 -> Local2 -> NULL
150 + * tail -> NULL tail -------------^
152 + * Inherited Content Only Both Local and Inherited Content
153 + * ================================== ====================================
154 + * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
155 + * tail -> NULL tail ---------^
157 + * This means that anyone wanting to traverse the whole list to USE it just
158 + * needs to start at the head and use the "next" pointers until it goes
159 + * NULL. To add new local content, we insert the item after the tail item
160 + * and update the tail (obviously, if "tail" was NULL, we insert it at the
161 + * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
162 + * because it is shared between the current list and our parent list(s).
163 + * The easiest way to handle this is to simply truncate the list after the
164 + * tail item and then free the local list from the head. When inheriting
165 + * the list for a new local dir, we just save off the exclude_list_struct
166 + * values (so we can pop back to them later) and set the tail to NULL.
169 /** Build an exclude structure given an exclude pattern. */
170 static void make_exclude(struct exclude_list_struct *listp, const char *pat,
171 @@ -46,23 +105,50 @@ static void make_exclude(struct exclude_
176 + rprintf(FINFO, "[%s] add_exclude(%.*s, %s%s%sclude)\n",
177 + who_am_i(), (int)pat_len, pat, listp->debug_type,
178 + mflags & MATCHFLG_MERGE_FILE ? "FILE " : "",
179 + mflags & MATCHFLG_INCLUDE ? "in" : "ex");
182 + if (mflags & MATCHFLG_MERGE_FILE) {
184 + /* If the local include file was already mentioned, don't
186 + for (i = 0; i < mergelist_cnt; i++) {
187 + struct exclude_struct *ex = mergelist_parents[i];
188 + if (strlen(ex->pattern) == pat_len
189 + && memcmp(ex->pattern, pat, pat_len) == 0)
192 + if ((pat_len == 10 || (pat_len > 10 && pat[pat_len-11] == '/'))
193 + && strncmp(pat+pat_len-10, ".cvsignore", 10) == 0) {
194 + mflags |= MATCHFLG_CVSIGNORE;
195 + mflags &= ~MATCHFLG_INCLUDE;
197 + mflags &= ~MATCHFLG_CVSIGNORE;
200 ret = new(struct exclude_struct);
202 out_of_memory("make_exclude");
204 memset(ret, 0, sizeof ret[0]);
206 - if (exclude_path_prefix)
207 - mflags |= MATCHFLG_ABS_PATH;
208 - if (exclude_path_prefix && *pat == '/')
209 - ex_len = strlen(exclude_path_prefix);
211 + if (mflags & MATCHFLG_ABS_PATH) {
213 + mflags &= ~MATCHFLG_ABS_PATH;
216 + ex_len = dirbuf_len - module_dirlen - 1;
219 ret->pattern = new_array(char, ex_len + pat_len + 1);
221 out_of_memory("make_exclude");
223 - memcpy(ret->pattern, exclude_path_prefix, ex_len);
224 + memcpy(ret->pattern, dirbuf + module_dirlen, ex_len);
225 strlcpy(ret->pattern + ex_len, pat, pat_len + 1);
228 @@ -81,14 +167,40 @@ static void make_exclude(struct exclude_
229 mflags |= MATCHFLG_DIRECTORY;
232 - for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
234 + if (mflags & MATCHFLG_MERGE_FILE) {
235 + struct exclude_list_struct *lp
236 + = new_array(struct exclude_list_struct, 1);
238 + out_of_memory("make_exclude");
239 + lp->head = lp->tail = NULL;
240 + if ((cp = strrchr(ret->pattern, '/')) != NULL)
244 + if (asprintf(&lp->debug_type, "per-dir %s ", cp) < 0)
245 + out_of_memory("make_exclude");
246 + ret->u.mergelist = lp;
247 + if (mergelist_cnt == mergelist_size) {
248 + mergelist_size += 5;
249 + mergelist_parents = realloc_array(mergelist_parents,
250 + struct exclude_struct *,
252 + if (!mergelist_parents)
253 + out_of_memory("make_exclude");
255 + mergelist_parents[mergelist_cnt++] = ret;
257 + for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++)
258 + ret->u.slash_cnt++;
261 ret->match_flags = mflags;
264 + if (!listp->tail) {
265 + ret->next = listp->head;
266 listp->head = listp->tail = ret;
269 + ret->next = listp->tail->next;
270 listp->tail->next = ret;
273 @@ -96,22 +208,265 @@ static void make_exclude(struct exclude_
275 static void free_exclude(struct exclude_struct *ex)
277 + if (ex->match_flags & MATCHFLG_MERGE_FILE) {
278 + free(ex->u.mergelist->debug_type);
279 + free(ex->u.mergelist);
285 -void clear_exclude_list(struct exclude_list_struct *listp)
286 +static void clear_exclude_list(struct exclude_list_struct *listp)
288 - struct exclude_struct *ent, *next;
290 - for (ent = listp->head; ent; ent = next) {
294 + struct exclude_struct *ent, *next;
295 + /* Truncate any inherited items from the local list. */
296 + listp->tail->next = NULL;
297 + /* Now free everything that is left. */
298 + for (ent = listp->head; ent; ent = next) {
304 listp->head = listp->tail = NULL;
307 +/* This returns an expanded (absolute) filename for the merge-file name if
308 + * the name has any slashes in it OR if the parent_dirscan var is True;
309 + * otherwise it returns the original merge_file name. If the len_ptr value
310 + * is non-NULL the merge_file name is limited by the referenced length
311 + * value and will be updated with the length of the resulting name. We
312 + * always return a name that is null terminated, even if the merge_file
314 +static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
315 + unsigned int prefix_skip)
317 + static char buf[MAXPATHLEN];
318 + char *fn, tmpbuf[MAXPATHLEN];
319 + unsigned int fn_len;
321 + if (!parent_dirscan && *merge_file != '/') {
322 + /* Return the name unchanged it doesn't have any slashes. */
324 + const char *p = merge_file + *len_ptr;
325 + while (--p > merge_file && *p != '/') {}
326 + if (p == merge_file) {
327 + strlcpy(buf, merge_file, *len_ptr + 1);
330 + } else if (strchr(merge_file, '/') == NULL)
331 + return (char *)merge_file;
334 + fn = *merge_file == '/' ? buf : tmpbuf;
335 + if (sanitize_paths) {
336 + const char *r = prefix_skip ? "/" : NULL;
337 + /* null-terminate the name if it isn't already */
338 + if (len_ptr && merge_file[*len_ptr]) {
339 + char *to = fn == buf ? tmpbuf : buf;
340 + strlcpy(to, merge_file, *len_ptr + 1);
343 + if (!sanitize_path(fn, merge_file, r, dirbuf + module_dirlen)) {
344 + rprintf(FERROR, "merge-file name overflows: %s\n",
349 + strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
353 + fn_len = strlen(fn);
357 + if (dirbuf_len + fn_len >= MAXPATHLEN) {
358 + rprintf(FERROR, "merge-file name overflows: %s\n", fn);
361 + memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip);
362 + memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1);
363 + fn_len = clean_fname(buf);
371 +/* Sets the dirbuf and dirbuf_len values. */
372 +void set_excludes_dir(const char *dir, unsigned int dirlen)
376 + memcpy(dirbuf, curr_dir, curr_dir_len);
377 + dirbuf[curr_dir_len] = '/';
378 + len = curr_dir_len + 1;
379 + if (dirlen >= MAXPATHLEN - len)
380 + dirlen = MAXPATHLEN - len - 1;
383 + memcpy(dirbuf + len, dir, dirlen);
384 + dirbuf[dirlen + len] = '\0';
385 + dirbuf_len = clean_fname(dirbuf);
386 + if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
387 + && dirbuf[dirbuf_len-2] == '/')
389 + dirbuf[dirbuf_len++] = '/';
390 + dirbuf[dirbuf_len] = '\0';
393 +/* This routine takes a per-dir merge-file entry and finishes its setup.
394 + * If the name has a path portion then we check to see if it refers to a
395 + * parent directory of the first transfer dir. If it does, we scan all the
396 + * dirs from that point through the parent dir of the transfer dir looking
397 + * for the per-dir merge-file in each one. */
398 +static BOOL setup_merge_file(struct exclude_struct *ex,
399 + struct exclude_list_struct *lp, int flags)
401 + char buf[MAXPATHLEN];
402 + char *x, *y, *pat = ex->pattern;
405 + if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
408 + y = strrchr(x, '/');
410 + ex->pattern = strdup(y+1);
414 + strlcpy(buf, x, MAXPATHLEN);
416 + pathjoin(buf, MAXPATHLEN, dirbuf, x);
418 + len = clean_fname(buf);
419 + if (len != 1 && len < MAXPATHLEN-1) {
423 + /* This ensures that the specified dir is a parent of the transfer. */
424 + for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
426 + y += strlen(y); /* nope -- skip the scan */
428 + parent_dirscan = True;
430 + char save[MAXPATHLEN];
431 + strlcpy(save, y, MAXPATHLEN);
433 + dirbuf_len = y - dirbuf;
434 + strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
435 + add_exclude_file(lp, buf, flags | XFLG_ABS_PATH);
436 + if (ex->match_flags & MATCHFLG_CVSIGNORE)
437 + lp->head = NULL; /* CVS doesn't inherit rules. */
439 + strlcpy(y, save, MAXPATHLEN);
440 + while ((*x++ = *y++) != '/') {}
442 + parent_dirscan = False;
447 +/* Each time rsync changes to a new directory it call this function to
448 + * handle all the per-dir merge-files. The "dir" value is the current path
449 + * relative to curr_dir (which might not be null-terminated). We copy it
450 + * into dirbuf so that we can easily append a file name on the end. */
451 +void *push_local_excludes(const char *dir, unsigned int dirlen)
453 + struct mergelist_save_struct *push;
454 + struct exclude_list_struct *ap;
457 + set_excludes_dir(dir, dirlen);
459 + if (!(push = new_array(struct mergelist_save_struct, 1)))
460 + out_of_memory("push_local_excludes");
462 + push->count = mergelist_cnt;
463 + push->array = new_array(struct exclude_list_struct, mergelist_cnt);
465 + out_of_memory("push_local_excludes");
467 + for (i = 0, ap = push->array; i < mergelist_cnt; i++) {
468 + memcpy(ap++, mergelist_parents[i]->u.mergelist,
469 + sizeof (struct exclude_list_struct));
472 + /* Note: add_exclude_file() might increase mergelist_cnt, so keep
473 + * this loop separate from the above loop. */
474 + for (i = 0; i < mergelist_cnt; i++) {
475 + struct exclude_struct *ex = mergelist_parents[i];
476 + struct exclude_list_struct *lp = ex->u.mergelist;
480 + rprintf(FINFO, "[%s] pushing %sexclude list\n",
481 + who_am_i(), lp->debug_type);
484 + if (ex->match_flags & MATCHFLG_CVSIGNORE) {
485 + lp->head = NULL; /* CVS doesn't inherit rules. */
486 + flags = XFLG_WORD_SPLIT | XFLG_WORDS_ONLY;
488 + flags = ex->match_flags & MATCHFLG_INCLUDE
489 + ? XFLG_DEF_INCLUDE : 0;
491 + lp->tail = NULL; /* Switch any local rules to inherited. */
493 + if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
494 + ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
495 + if (setup_merge_file(ex, lp, flags))
496 + set_excludes_dir(dir, dirlen);
499 + if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
500 + MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len)
501 + add_exclude_file(lp, dirbuf, flags | XFLG_ABS_PATH);
503 + io_error |= IOERR_GENERAL;
505 + "cannot add local excludes in long-named directory %s\n",
506 + full_fname(dirbuf));
508 + dirbuf[dirbuf_len] = '\0';
511 + return (void*)push;
514 +void pop_local_excludes(void *mem)
516 + struct mergelist_save_struct *pop = (struct mergelist_save_struct*)mem;
517 + struct exclude_list_struct *ap;
520 + for (i = mergelist_cnt; i-- > 0; ) {
521 + struct exclude_struct *ex = mergelist_parents[i];
522 + struct exclude_list_struct *lp = ex->u.mergelist;
525 + rprintf(FINFO, "[%s] popping %sexclude list\n",
526 + who_am_i(), lp->debug_type);
529 + clear_exclude_list(lp);
532 + mergelist_cnt = pop->count;
533 + for (i = 0, ap = pop->array; i < mergelist_cnt; i++) {
534 + memcpy(mergelist_parents[i]->u.mergelist, ap++,
535 + sizeof (struct exclude_list_struct));
542 static int check_one_exclude(char *name, struct exclude_struct *ex,
545 @@ -122,18 +477,20 @@ static int check_one_exclude(char *name,
546 /* If the pattern does not have any slashes AND it does not have
547 * a "**" (which could match a slash), then we just match the
548 * name portion of the path. */
549 - if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
550 + if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
551 if ((p = strrchr(name,'/')) != NULL)
554 else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/') {
555 static char full_name[MAXPATHLEN];
556 - int plus = curr_dir[1] == '\0'? 1 : 0;
557 - pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
558 + char *cd = curr_dir + module_dirlen;
559 + int plus = cd[1] == '\0'? 1 : 0;
560 + pathjoin(full_name, sizeof full_name, cd+plus, name);
564 - if (!name[0]) return 0;
568 if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir)
570 @@ -148,9 +505,9 @@ static int check_one_exclude(char *name,
571 if (ex->match_flags & MATCHFLG_WILD) {
572 /* A non-anchored match with an infix slash and no "**"
573 * needs to match the last slash_cnt+1 name elements. */
574 - if (!match_start && ex->slash_cnt
575 + if (!match_start && ex->u.slash_cnt
576 && !(ex->match_flags & MATCHFLG_WILD2)) {
577 - int cnt = ex->slash_cnt + 1;
578 + int cnt = ex->u.slash_cnt + 1;
579 for (p = name + strlen(name) - 1; p >= name; p--) {
580 if (*p == '/' && !--cnt)
582 @@ -221,6 +578,13 @@ int check_exclude(struct exclude_list_st
583 struct exclude_struct *ent;
585 for (ent = listp->head; ent; ent = ent->next) {
586 + if (ent->match_flags & MATCHFLG_MERGE_FILE) {
587 + int rc = check_exclude(ent->u.mergelist, name,
593 if (check_one_exclude(name, ent, name_is_dir)) {
594 report_exclude_result(name, ent, name_is_dir,
596 @@ -253,11 +617,36 @@ static const char *get_exclude_tok(const
600 - /* Is this a '+' or '-' followed by a space (not whitespace)? */
601 + /* Check for a +/-/. followed by a space (not whitespace). */
602 if (!(xflags & XFLG_WORDS_ONLY)
603 - && (*s == '-' || *s == '+') && s[1] == ' ') {
604 + && (*s == '-' || *s == '+' || *s == '.') && s[1] == ' ') {
606 mflags |= MATCHFLG_INCLUDE;
607 + else if (*s == '.') {
608 + mflags |= MATCHFLG_MERGE_FILE;
609 + if (xflags & XFLG_DEF_INCLUDE)
610 + mflags |= MATCHFLG_INCLUDE;
611 + while (s[2] == '-') {
616 + mflags |= MATCHFLG_PERDIR_MERGE
617 + | MATCHFLG_FINISH_SETUP;
624 + "invalid merge options: %s\n",
626 + exit_cleanup(RERR_SYNTAX);
628 + } while (s[1] != ' ');
633 } else if (xflags & XFLG_DEF_INCLUDE)
634 mflags |= MATCHFLG_INCLUDE;
635 @@ -273,6 +662,8 @@ static const char *get_exclude_tok(const
637 if (*p == '!' && len == 1 && !(xflags & XFLG_WORDS_ONLY))
638 mflags |= MATCHFLG_CLEAR_LIST;
639 + if (xflags & XFLG_ABS_PATH)
640 + mflags |= MATCHFLG_ABS_PATH;
644 @@ -284,7 +675,7 @@ void add_exclude(struct exclude_list_str
647 unsigned int pat_len, mflags;
649 + const char *cp, *p;
653 @@ -292,9 +683,15 @@ void add_exclude(struct exclude_list_str
657 + /* Remember that the returned string is NOT '\0' terminated! */
658 cp = get_exclude_tok(cp + pat_len, &pat_len, &mflags, xflags);
661 + if (pat_len >= MAXPATHLEN) {
662 + rprintf(FERROR, "discarding over-long exclude: %s\n",
667 if (mflags & MATCHFLG_CLEAR_LIST) {
669 @@ -306,13 +703,24 @@ void add_exclude(struct exclude_list_str
673 - make_exclude(listp, cp, pat_len, mflags);
676 - rprintf(FINFO, "[%s] add_exclude(%.*s, %s%sclude)\n",
677 - who_am_i(), (int)pat_len, cp, listp->debug_type,
678 - mflags & MATCHFLG_INCLUDE ? "in" : "ex");
679 + if (mflags & MATCHFLG_MERGE_FILE) {
680 + unsigned int len = pat_len;
681 + if (mflags & MATCHFLG_PERDIR_MERGE) {
682 + if (parent_dirscan) {
683 + if (!(p = parse_merge_name(cp, &len, module_dirlen)))
685 + make_exclude(listp, p, len, mflags);
689 + if (!(p = parse_merge_name(cp, &len, 0)))
691 + add_exclude_file(listp, p, xflags | XFLG_FATAL_ERRORS);
696 + make_exclude(listp, cp, pat_len, mflags);
700 @@ -321,7 +729,7 @@ void add_exclude_file(struct exclude_lis
704 - char line[MAXPATHLEN+3]; /* Room for "x " prefix and trailing slash. */
705 + char line[MAXPATHLEN+7]; /* Room for prefix chars and trailing slash. */
706 char *eob = line + sizeof line - 1;
707 int word_split = xflags & XFLG_WORD_SPLIT;
709 @@ -342,6 +750,12 @@ void add_exclude_file(struct exclude_lis
713 + dirbuf[dirbuf_len] = '\0';
716 + rprintf(FINFO, "[%s] add_exclude_file(%s,%d)\n",
717 + who_am_i(), fname, xflags);
722 @@ -402,7 +816,21 @@ void send_exclude_list(int f)
723 if (ent->match_flags & MATCHFLG_INCLUDE) {
725 write_buf(f, "+ ", 2);
726 - } else if ((*p == '-' || *p == '+') && p[1] == ' ') {
727 + } else if (ent->match_flags & MATCHFLG_MERGE_FILE) {
728 + char buf[32], *op = buf;
731 + if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
738 + write_int(f, l + (op - buf));
739 + write_buf(f, buf, op - buf);
740 + } else if ((*p == '-' || *p == '+' || *p == '.')
743 write_buf(f, "- ", 2);
745 @@ -443,6 +871,7 @@ void add_cvs_excludes(void)
746 char fname[MAXPATHLEN];
749 + add_exclude(&exclude_list, ". -p .cvsignore", 0);
750 add_exclude(&exclude_list, default_cvsignore,
751 XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
753 --- orig/flist.c 2004-08-05 21:57:29
754 +++ flist.c 2004-08-09 18:21:55
755 @@ -39,10 +39,9 @@ extern int module_id;
756 extern int ignore_errors;
757 extern int numeric_ids;
759 -extern int cvs_exclude;
762 extern char curr_dir[MAXPATHLEN];
763 +extern unsigned int curr_dir_len;
764 extern char *files_from;
765 extern int filesfrom_fd;
767 @@ -66,7 +65,6 @@ extern int list_only;
769 extern struct exclude_list_struct exclude_list;
770 extern struct exclude_list_struct server_exclude_list;
771 -extern struct exclude_list_struct local_exclude_list;
775 @@ -221,8 +219,6 @@ int link_stat(const char *path, STRUCT_S
777 static int check_exclude_file(char *fname, int is_dir, int exclude_level)
781 #if 0 /* This currently never happens, so avoid a useless compare. */
782 if (exclude_level == NO_EXCLUDES)
784 @@ -244,10 +240,7 @@ static int check_exclude_file(char *fnam
785 if (exclude_level != ALL_EXCLUDES)
787 if (exclude_list.head
788 - && (rc = check_exclude(&exclude_list, fname, is_dir)) != 0)
790 - if (local_exclude_list.head
791 - && check_exclude(&local_exclude_list, fname, is_dir) < 0)
792 + && check_exclude(&exclude_list, fname, is_dir) < 0)
796 @@ -573,7 +566,7 @@ void receive_file_entry(struct file_stru
797 clean_fname(thisname);
800 - sanitize_path(thisname, thisname, NULL);
801 + sanitize_path(thisname, thisname, "", NULL);
803 if ((basename = strrchr(thisname, '/')) != NULL) {
804 dirname_len = ++basename - thisname; /* counts future '\0' */
805 @@ -671,7 +664,7 @@ void receive_file_entry(struct file_stru
807 read_sbuf(f, bp, linkname_len - 1);
809 - sanitize_path(bp, bp, lastdir);
810 + sanitize_path(bp, bp, "", lastdir);
814 @@ -761,7 +754,7 @@ struct file_struct *make_file(char *fnam
816 clean_fname(thisname);
818 - sanitize_path(thisname, thisname, NULL);
819 + sanitize_path(thisname, thisname, "", NULL);
821 memset(sum, 0, SUM_LENGTH);
823 @@ -954,15 +947,7 @@ void send_file_name(int f, struct file_l
825 if (recursive && S_ISDIR(file->mode)
826 && !(file->flags & FLAG_MOUNT_POINT)) {
827 - struct exclude_list_struct last_list = local_exclude_list;
828 - local_exclude_list.head = local_exclude_list.tail = NULL;
829 send_directory(f, flist, f_name_to(file, fbuf));
831 - rprintf(FINFO, "[%s] popping %sexclude list\n",
832 - who_am_i(), local_exclude_list.debug_type);
834 - clear_exclude_list(&local_exclude_list);
835 - local_exclude_list = last_list;
839 @@ -973,6 +958,7 @@ static void send_directory(int f, struct
841 char fname[MAXPATHLEN];
843 + void *save_excludes;
847 @@ -996,18 +982,7 @@ static void send_directory(int f, struct
852 - if (strlcpy(p, ".cvsignore", MAXPATHLEN - offset)
853 - < MAXPATHLEN - offset) {
854 - add_exclude_file(&local_exclude_list, fname,
855 - XFLG_WORD_SPLIT | XFLG_WORDS_ONLY);
857 - io_error |= IOERR_GENERAL;
859 - "cannot cvs-exclude in long-named directory %s\n",
860 - full_fname(fname));
863 + save_excludes = push_local_excludes(fname, offset);
865 for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
866 char *dname = d_name(di);
867 @@ -1028,6 +1003,8 @@ static void send_directory(int f, struct
868 rsyserr(FERROR, errno, "readdir(%s)", dir);
871 + pop_local_excludes(save_excludes);
876 @@ -1047,6 +1024,7 @@ struct file_list *send_file_list(int f,
877 char *p, *dir, olddir[sizeof curr_dir];
878 char lastpath[MAXPATHLEN] = "";
879 struct file_list *flist;
880 + BOOL need_first_push = True;
884 @@ -1067,6 +1045,10 @@ struct file_list *send_file_list(int f,
885 exit_cleanup(RERR_FILESELECT);
888 + if (curr_dir_len < MAXPATHLEN - 1) {
889 + push_local_excludes(curr_dir, curr_dir_len);
890 + need_first_push = False;
895 @@ -1077,13 +1059,13 @@ struct file_list *send_file_list(int f,
897 if (read_filesfrom_line(filesfrom_fd, fname) == 0)
899 - sanitize_path(fname, fname, NULL);
900 + sanitize_path(fname, fname, "", NULL);
904 strlcpy(fname, *argv++, MAXPATHLEN);
906 - sanitize_path(fname, fname, NULL);
907 + sanitize_path(fname, fname, "", NULL);
911 @@ -1097,6 +1079,15 @@ struct file_list *send_file_list(int f,
915 + if (need_first_push) {
916 + if ((p = strrchr(fname, '/')) != NULL) {
917 + if (*++p && strcmp(p, ".") != 0)
918 + push_local_excludes(fname, p - fname);
919 + } else if (strcmp(fname, ".") != 0)
920 + push_local_excludes(fname, 0);
921 + need_first_push = False;
924 if (link_stat(fname, &st, keep_dirlinks) != 0) {
926 io_error |= IOERR_GENERAL;
927 --- orig/options.c 2004-08-05 21:57:29
928 +++ options.c 2004-08-09 18:22:26
929 @@ -287,6 +287,7 @@ void usage(enum logcode F)
930 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
931 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
932 rprintf(F," --files-from=FILE read FILE for list of source-file names\n");
933 + rprintf(F," -E same as --exclude='. -p /.rsync-excludes'\n");
934 rprintf(F," -0, --from0 all *-from file lists are delimited by nulls\n");
935 rprintf(F," --version print version number\n");
936 rprintf(F," --daemon run as an rsync daemon\n");
937 @@ -389,6 +390,7 @@ static struct poptOption long_options[]
938 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
939 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
940 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
941 + {0, 'E', POPT_ARG_NONE, 0, 'E', 0, 0 },
942 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
943 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
944 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
945 @@ -589,6 +591,11 @@ int parse_arguments(int *argc, const cha
950 + add_exclude(&exclude_list,
951 + ". -p /.rsync-excludes", 0);
957 @@ -728,17 +735,17 @@ int parse_arguments(int *argc, const cha
958 if (sanitize_paths) {
960 for (i = *argc; i-- > 0; )
961 - (*argv)[i] = sanitize_path(NULL, (*argv)[i], NULL);
962 + (*argv)[i] = sanitize_path(NULL, (*argv)[i], "", NULL);
964 - tmpdir = sanitize_path(NULL, tmpdir, "");
965 + tmpdir = sanitize_path(NULL, tmpdir, NULL, NULL);
967 - partial_dir = sanitize_path(NULL, partial_dir, "");
968 + partial_dir = sanitize_path(NULL, partial_dir, NULL, NULL);
970 - compare_dest = sanitize_path(NULL, compare_dest, "");
971 + compare_dest = sanitize_path(NULL, compare_dest, NULL, NULL);
973 - backup_dir = sanitize_path(NULL, backup_dir, "");
974 + backup_dir = sanitize_path(NULL, backup_dir, NULL, NULL);
976 - files_from = sanitize_path(NULL, files_from, "");
977 + files_from = sanitize_path(NULL, files_from, NULL, NULL);
979 if (server_exclude_list.head && !am_sender) {
980 struct exclude_list_struct *elp = &server_exclude_list;
981 --- orig/rsync.h 2004-08-03 15:41:32
982 +++ rsync.h 2004-08-08 06:07:01
984 #define XFLG_DEF_INCLUDE (1<<1)
985 #define XFLG_WORDS_ONLY (1<<2)
986 #define XFLG_WORD_SPLIT (1<<3)
987 +#define XFLG_ABS_PATH (1<<4)
989 #define PERMS_REPORT (1<<0)
990 #define PERMS_SKIP_MTIME (1<<1)
991 @@ -499,11 +500,18 @@ struct map_struct {
992 #define MATCHFLG_INCLUDE (1<<4) /* this is an include, not an exclude */
993 #define MATCHFLG_DIRECTORY (1<<5) /* this matches only directories */
994 #define MATCHFLG_CLEAR_LIST (1<<6) /* this item is the "!" token */
995 +#define MATCHFLG_MERGE_FILE (1<<7) /* specifies a file to merge */
996 +#define MATCHFLG_CVSIGNORE (1<<8) /* parse this as a .cvsignore file */
997 +#define MATCHFLG_PERDIR_MERGE (1<<9) /* merge-file is searched per-dir */
998 +#define MATCHFLG_FINISH_SETUP (1<<10)/* per-dir merge file needs setup */
999 struct exclude_struct {
1000 struct exclude_struct *next;
1002 unsigned int match_flags;
1006 + struct exclude_list_struct *mergelist;
1010 struct exclude_list_struct {
1011 --- orig/rsync.yo 2004-08-03 15:34:32
1012 +++ rsync.yo 2004-08-10 04:49:06
1013 @@ -335,6 +335,7 @@ verb(
1014 --include=PATTERN don't exclude files matching PATTERN
1015 --include-from=FILE don't exclude patterns listed in FILE
1016 --files-from=FILE read FILE for list of source-file names
1017 + -E same as --exclude='. -p /.rsync-excludes'
1018 -0 --from0 all file lists are delimited by nulls
1019 --version print version number
1020 --daemon run as an rsync daemon
1021 @@ -979,15 +980,22 @@ The exclude and include patterns specifi
1022 selection of which files to transfer and which files to skip.
1024 Rsync builds an ordered list of include/exclude options as specified on
1025 -the command line. Rsync checks each file and directory
1026 -name against each exclude/include pattern in turn. The first matching
1028 +It can also be told to check for include/exclude options in each
1029 +directory that rsync visits during the transfer (see the section on
1030 +MERGED EXCLUDE FILES for the details on these per-directory exclude
1033 +As the list of files/directories to transfer is built, rsync checks each
1034 +name against every exclude/include pattern in turn. The first matching
1035 pattern is acted on. If it is an exclude pattern, then that file is
1036 skipped. If it is an include pattern then that filename is not
1037 skipped. If no matching include/exclude pattern is found then the
1038 filename is not skipped.
1040 -The filenames matched against the exclude/include patterns are relative
1041 -to the "root of the transfer". If you think of the transfer as a
1042 +The global include/exclude rules are anchored at the "root of the
1043 +transfer" (as opposed to per-directory rules, which are anchored at
1044 +the current directory). If you think of the transfer as a
1045 subtree of names that are being sent from sender to receiver, the root
1046 is where the tree starts to be duplicated in the destination directory.
1047 This root governs where patterns that start with a / match (see below).
1048 @@ -996,7 +1004,7 @@ Because the matching is relative to the
1049 trailing slash on a source path or changing your use of the --relative
1050 option affects the path you need to use in your matching (in addition to
1051 changing how much of the file tree is duplicated on the destination
1052 -system). The following examples demonstrate this.
1053 +host). The following examples demonstrate this.
1055 Let's say that we want to match two source files, one with an absolute
1056 path of "/home/me/foo/bar", and one with a path of "/home/you/bar/baz".
1057 @@ -1043,23 +1051,29 @@ because rsync did not descend through th
1060 Note also that the --include and --exclude options take one pattern
1061 -each. To add multiple patterns use the --include-from and
1062 ---exclude-from options or multiple --include and --exclude options.
1063 +each. To add multiple patterns use the --include-from and --exclude-from
1064 +options or multiple --include and --exclude options. See also the
1065 +section on MERGED EXCLUDE FILES for how to merge-files together and
1066 +specify local rules within a hierarchy of files.
1068 -The patterns can take several forms. The rules are:
1069 +The include/exclude patterns can take several forms. The rules are:
1073 - it() if the pattern starts with a / then it is matched against the
1074 - start of the filename, otherwise it is matched against the end of
1076 - This is the equivalent of a leading ^ in regular expressions.
1077 - Thus "/foo" would match a file called "foo" at the transfer-root
1078 - (see above for how this is different from the filesystem-root).
1079 - On the other hand, "foo" would match any file called "foo"
1080 + it() if the pattern starts with a / then it is anchored to a
1081 + particular spot in the hierarchy of files, otherwise it is matched
1082 + against the end of the pathname. This is similar to a leading ^ in
1083 + regular expressions.
1084 + Thus "/foo" would match a file called "foo" at either the "root of the
1085 + transfer" (for a global rule) or in the current directory (for a
1086 + per-directory rule).
1087 + An unqualified "foo" would match any file or directory named "foo"
1088 anywhere in the tree because the algorithm is applied recursively from
1090 top down; it behaves as if each path component gets a turn at being the
1091 - end of the file name.
1092 + end of the file name. Even the unanchored "sub/foo" would match at
1093 + any point in the hierarchy where a "foo" was found within a directory
1096 it() if the pattern ends with a / then it will only match a
1097 directory, not a file, link, or device.
1098 @@ -1072,22 +1086,31 @@ itemize(
1099 single asterisk pattern "*" will stop at slashes.
1101 it() if the pattern contains a / (not counting a trailing /) or a "**"
1102 - then it is matched against the full filename, including any leading
1103 - directory. If the pattern doesn't contain a / or a "**", then it is
1104 + then it is matched against the full pathname, including any leading
1105 + directories. If the pattern doesn't contain a / or a "**", then it is
1106 matched only against the final component of the filename. Again,
1107 remember that the algorithm is applied recursively so "full filename" can
1108 actually be any portion of a path below the starting directory.
1110 it() if the pattern starts with "+ " (a plus followed by a space)
1111 then it is always considered an include pattern, even if specified as
1112 - part of an exclude option. The prefix is discarded before matching.
1113 + part of an exclude option. (The prefix is discarded before matching.)
1115 it() if the pattern starts with "- " (a minus followed by a space)
1116 then it is always considered an exclude pattern, even if specified as
1117 - part of an include option. The prefix is discarded before matching.
1118 + part of an include option. (The prefix is discarded before matching.)
1120 + it() if the pattern starts with ". " (a dot followed by a space) then its
1121 + pattern is taken to be a merge-file that is read in to supplement the
1122 + current rules. See the section on MERGED EXCLUDE FILES for more
1125 it() if the pattern is a single exclamation mark ! then the current
1126 include/exclude list is reset, removing all previously defined patterns.
1127 + The "current" list is either the global list of rules (which are
1128 + specified via options) or a set of per-directory rules (which are
1129 + inherited in their own sub-list, so a subdirectory can use this to
1130 + clear out the parent's rules).
1133 The +/- rules are most useful in a list that was read from a file, allowing
1134 @@ -1134,10 +1157,109 @@ itemize(
1135 it() --include "*/" --include "*.c" --exclude "*" would include all
1136 directories and C source files
1137 it() --include "foo/" --include "foo/bar.c" --exclude "*" would include
1138 - only foo/bar.c (the foo/ directory must be explicitly included or
1139 - it would be excluded by the "*")
1140 + only the foo directory and foo/bar.c (the foo directory must be
1141 + explicitly included or it would be excluded by the "*")
1144 +manpagesection(MERGED EXCLUDE FILES)
1146 +You can merge whole files into an exclude file by specifying a rule that
1147 +starts with a ". " (a dot followed by a space) and putting a filename in
1148 +place of the pattern. There are two kinds of merged exclude files --
1149 +single-instance and per-directory. You select which one you want by
1150 +optionally including an option prior to the merge-file name:
1154 +dit(bf(-p)) Make the file a per-directory merge-file. Rsync will scan
1155 +every directory that it traverses for the named file, merging its contents
1156 +when the file exists.
1158 +Per-directory rules are inherited in all subdirectories of the directory
1159 +where the merge-file was found. Each subdirectory's rules are prefixed
1160 +to the inherited rules from the parent directories, which gives the
1161 +newest rules a higher priority than the inherited rules. If you don't
1162 +want a rule to be inherited, anchor it with a leading slash. Anchored
1163 +rules in a per-directory merge-file are relative to the current
1164 +directory, so a rule "/foo" would only exclude the file "foo" in the
1165 +directory where the per-dir exclude file was found.
1167 +dit(bf(--)) End the scanning of options. Useful if you want to specify a
1168 +filename that begins with a dash.
1172 +Here's an example exclude file (which you'd specify via the normal
1173 +--exclude-from=FILE option):
1176 + . /home/user/.global_excludes
1183 +This will merge the contents of the /home/user/.global_excludes file at the
1184 +start of the list and also turns the ".excl" filename into a per-directory
1185 +exclude file. All the merged rules default to being exclude rules because
1186 +an exclude statement was used to specify them. Rules read in from the
1187 +.global_excludes file are anchored just like all other global rules
1188 +(only per-directory rules that are read in once the transfer begins get
1189 +the current-dir anchoring).
1191 +If a per-directory merge-file is specified with a path that is a parent
1192 +directory of the first transfer directory, rsync will scan all the parent
1193 +dirs from that starting point to the transfer directory for the indicated
1194 +per-directory file. For instance, the -E option is an abbreviation for
1198 + --exclude='. -p /.rsync-excludes'
1201 +That exclude tells rsync to scan for the file .rsync-excludes in all
1202 +directories from the root up through the source of the transfer. (For an
1203 +rsync daemon, the "root dir" is always the module's "path" setting.)
1205 +Some examples of this pre-scanning for per-directory files:
1208 + rsync -avE /src/path/ /dest/dir
1209 + rsync -av --exclude='. -p ../../.rsync-excludes' /src/path/ /dest/dir
1210 + rsync -av --exclude='. -p .rsync-excludes' /src/path/ /dest/dir
1213 +The first two commands above will look for ".rsync-excludes" in "/" and
1214 +"/src" before the normal scan begins looking for the file in "/src/path"
1215 +and its subdirectories. The last command avoids the parent-dir scan
1216 +and only looks for the ".rsync-excludes" files in each directory that is
1217 +a part of the transfer.
1219 +Finally, note that the parsing of any merge-file named ".cvsignore" is
1220 +always done in a CVS-compatible manner, even if -C wasn't specified. This
1221 +means that its rules are always excludes (even if an include option
1222 +specified the file), patterns are split on whitespace, the rules are never
1223 +inherited, and no special characters are honored (e.g. no comments, no "!",
1226 +Additionally, you can affect where the --cvs-exclude (-C) option's
1227 +inclusion of the per-directory .cvsignore file gets placed into your rules
1228 +by adding your own explicit per-directory merge rule for ".cvsignore".
1229 +Without this rsync would add its this rule at the end of all your other
1230 +rules (giving it a lower priority than your command-line rules). For
1234 + rsync -avC --exclude='. -p .cvsignore' --exclude-from=foo a/ b
1237 +The above will merge all the per-directory .cvsignore rules at the start of
1238 +your list rather than at the end. This allows their dir-specific rules to
1239 +supersede your rules instead of being subservient to them. (The global
1240 +rules taken from the $HOME/.cvsignore file and from $CVSIGNORE are not
1241 +repositioned by this.)
1243 manpagesection(BATCH MODE)
1245 bf(Note:) Batch mode should be considered experimental in this version
1246 --- orig/testsuite/exclude.test 2004-05-29 21:25:45
1247 +++ testsuite/exclude.test 2004-08-08 06:35:15
1248 @@ -23,19 +23,47 @@ export HOME CVSIGNORE
1249 makepath "$fromdir/foo/down/to/you"
1250 makepath "$fromdir/bar/down/to/foo/too"
1251 makepath "$fromdir/mid/for/foo/and/that/is/who"
1252 +cat >"$fromdir/.excl" <<EOF
1258 echo kept >"$fromdir/foo/file1"
1259 echo removed >"$fromdir/foo/file2"
1260 echo cvsout >"$fromdir/foo/file2.old"
1261 +cat >"$fromdir/foo/.excl" <<EOF
1265 +cat >"$fromdir/bar/.excl" <<EOF
1270 echo cvsout >"$fromdir/bar/down/to/home-cvs-exclude"
1271 +cat >"$fromdir/bar/down/to/.excl2" <<EOF
1274 echo keeper >"$fromdir/bar/down/to/foo/file1"
1275 echo cvsout >"$fromdir/bar/down/to/foo/file1.bak"
1276 echo gone >"$fromdir/bar/down/to/foo/file3"
1277 echo lost >"$fromdir/bar/down/to/foo/file4"
1278 echo cvsout >"$fromdir/bar/down/to/foo/file4.junk"
1279 echo smashed >"$fromdir/bar/down/to/foo/to"
1280 +cat >"$fromdir/bar/down/to/foo/.excl2" <<EOF
1283 +# This one should be ineffectual
1284 +cat >"$fromdir/mid/.excl2" <<EOF
1287 echo cvsout >"$fromdir/mid/one-in-one-out"
1288 echo one-in-one-out >"$fromdir/mid/.cvsignore"
1289 echo cvsin >"$fromdir/mid/one-for-all"
1290 +cat >"$fromdir/mid/.excl" <<EOF
1293 echo cvsin >"$fromdir/mid/for/one-in-one-out"
1294 echo expunged >"$fromdir/mid/for/foo/extra"
1295 echo retained >"$fromdir/mid/for/foo/keep"
1296 @@ -100,5 +128,24 @@ $RSYNC -av --existing --include='*/' --e
1297 checkit "$RSYNC -avvC --exclude-from=\"$excl\" \
1298 --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1300 +# Modify the chk dir for our merge-exclude test and then tweak the dir times.
1303 +rm "$chkdir"/foo/file1
1304 +rm "$chkdir"/bar/.excl
1305 +rm "$chkdir"/bar/down/to/.excl2
1306 +rm "$chkdir"/bar/down/to/foo/.excl2
1307 +rm "$chkdir"/mid/.excl
1308 +cp -p "$fromdir"/bar/down/to/foo/*.junk "$chkdir"/bar/down/to/foo
1309 +cp -p "$fromdir"/bar/down/to/foo/to "$chkdir"/bar/down/to/foo
1311 +$RSYNC -av --existing --include='*/' --exclude='*' "$fromdir/" "$chkdir/"
1313 +# Now, test if rsync excludes the same files, this time with a merge-exclude
1316 +checkit "$RSYNC -avv --exclude='. -p .excl' --exclude-from=\"$excl\" \
1317 + --delete-excluded \"$fromdir/\" \"$todir/\"" "$chkdir" "$todir"
1319 # The script would have aborted on error, so getting here means we've won.
1321 --- orig/util.c 2004-08-09 21:07:10
1322 +++ util.c 2004-08-09 21:07:25
1323 @@ -524,7 +524,7 @@ static void glob_expand_one(char *s, cha
1327 - s = sanitize_path(NULL, s, NULL);
1328 + s = sanitize_path(NULL, s, "", NULL);
1332 @@ -706,18 +706,16 @@ unsigned int clean_fname(char *name)
1333 * "/" (either removing it or expanding it) and any leading or embedded
1334 * ".." components that attempt to escape past the module's top dir.
1336 - * If dest is NULL, a buffer is allocated to hold the result. If dest is
1337 - * the same buffer as p (the path) OR if reldir is NULL, a leading slash
1338 - * is dropped instead of being expanded to be the module's top dir.
1339 + * If dest is NULL, a buffer is allocated to hold the result. It is legal
1340 + * to call with the dest and the path (p) pointing to the same buffer, but
1341 + * rootdir is ignored to avoid expansion of the string.
1343 + * The rootdir string contains a value to use in place of a leading slash.
1344 + * Specify NULL to get the default of lp_path(module_id).
1346 * If reldir is non-NULL (and non-empty), it is a sanitized directory that
1347 * the path will be relative to, so allow as many '..'s at the beginning of
1348 - * the path as there are components in reldir. This is used for symbolic
1349 - * link targets. If reldir is non-null and the path began with "/", to be
1350 - * completely like a chroot we should add in depth levels of ".." at the
1351 - * beginning of the path, but that would blow the assumption that the path
1352 - * doesn't grow and it is not likely to end up being a valid symlink
1353 - * anyway, so just do the normal removal of the leading "/" instead.
1354 + * the path as there are components in reldir.
1356 * While we're at it, remove double slashes and "." components like
1357 * clean_fname() does, but DON'T remove a trailing slash because that is
1358 @@ -725,7 +723,8 @@ unsigned int clean_fname(char *name)
1360 * If the resulting path would be empty, change it into ".".
1362 -char *sanitize_path(char *dest, const char *p, const char *reldir)
1363 +char *sanitize_path(char *dest, const char *p, const char *rootdir,
1364 + const char *reldir)
1368 @@ -734,8 +733,10 @@ char *sanitize_path(char *dest, const ch
1371 int plen = strlen(p);
1372 - if (*p == '/' && reldir) {
1373 - rlen = strlen(lp_path(module_id));
1376 + rootdir = lp_path(module_id);
1377 + rlen = strlen(rootdir);
1381 @@ -745,7 +746,7 @@ char *sanitize_path(char *dest, const ch
1382 } else if (!(dest = new_array(char, rlen + plen + 1)))
1383 out_of_memory("sanitize_path");
1385 - memcpy(dest, lp_path(module_id), rlen);
1386 + memcpy(dest, rootdir, rlen);