Annotation of embedaddon/rsync/exclude.c, revision 1.1.1.3
1.1 misho 1: /*
2: * The filter include/exclude routines.
3: *
4: * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
5: * Copyright (C) 1996 Paul Mackerras
6: * Copyright (C) 2002 Martin Pool
1.1.1.3 ! misho 7: * Copyright (C) 2003-2015 Wayne Davison
1.1 misho 8: *
9: * This program is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation; either version 3 of the License, or
12: * (at your option) any later version.
13: *
14: * This program is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU General Public License for more details.
18: *
19: * You should have received a copy of the GNU General Public License along
20: * with this program; if not, visit the http://fsf.org website.
21: */
22:
23: #include "rsync.h"
24:
25: extern int am_server;
26: extern int am_sender;
27: extern int eol_nulls;
28: extern int io_error;
29: extern int local_server;
30: extern int prune_empty_dirs;
31: extern int ignore_perishable;
32: extern int delete_mode;
33: extern int delete_excluded;
34: extern int cvs_exclude;
35: extern int sanitize_paths;
36: extern int protocol_version;
37: extern int module_id;
38:
39: extern char curr_dir[MAXPATHLEN];
40: extern unsigned int curr_dir_len;
41: extern unsigned int module_dirlen;
42:
1.1.1.2 misho 43: filter_rule_list filter_list = { .debug_type = "" };
44: filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
45: filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
1.1 misho 46:
47: /* Need room enough for ":MODS " prefix plus some room to grow. */
48: #define MAX_RULE_PREFIX (16)
49:
50: #define SLASH_WILD3_SUFFIX "/***"
51:
52: /* The dirbuf is set by push_local_filters() to the current subdirectory
53: * relative to curr_dir that is being processed. The path always has a
54: * trailing slash appended, and the variable dirbuf_len contains the length
55: * of this path prefix. The path is always absolute. */
56: static char dirbuf[MAXPATHLEN+1];
57: static unsigned int dirbuf_len = 0;
58: static int dirbuf_depth;
59:
60: /* This is True when we're scanning parent dirs for per-dir merge-files. */
61: static BOOL parent_dirscan = False;
62:
63: /* This array contains a list of all the currently active per-dir merge
64: * files. This makes it easier to save the appropriate values when we
65: * "push" down into each subdirectory. */
1.1.1.2 misho 66: static filter_rule **mergelist_parents;
1.1 misho 67: static int mergelist_cnt = 0;
68: static int mergelist_size = 0;
69:
70: /* Each filter_list_struct describes a singly-linked list by keeping track
71: * of both the head and tail pointers. The list is slightly unusual in that
72: * a parent-dir's content can be appended to the end of the local list in a
73: * special way: the last item in the local list has its "next" pointer set
74: * to point to the inherited list, but the local list's tail pointer points
75: * at the end of the local list. Thus, if the local list is empty, the head
76: * will be pointing at the inherited content but the tail will be NULL. To
77: * help you visualize this, here are the possible list arrangements:
78: *
79: * Completely Empty Local Content Only
80: * ================================== ====================================
81: * head -> NULL head -> Local1 -> Local2 -> NULL
82: * tail -> NULL tail -------------^
83: *
84: * Inherited Content Only Both Local and Inherited Content
85: * ================================== ====================================
86: * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
87: * tail -> NULL tail ---------^
88: *
89: * This means that anyone wanting to traverse the whole list to use it just
90: * needs to start at the head and use the "next" pointers until it goes
91: * NULL. To add new local content, we insert the item after the tail item
92: * and update the tail (obviously, if "tail" was NULL, we insert it at the
93: * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
94: * because it is shared between the current list and our parent list(s).
95: * The easiest way to handle this is to simply truncate the list after the
96: * tail item and then free the local list from the head. When inheriting
97: * the list for a new local dir, we just save off the filter_list_struct
98: * values (so we can pop back to them later) and set the tail to NULL.
99: */
100:
1.1.1.2 misho 101: static void teardown_mergelist(filter_rule *ex)
1.1 misho 102: {
1.1.1.3 ! misho 103: int j;
! 104:
! 105: if (!ex->u.mergelist)
! 106: return;
! 107:
1.1.1.2 misho 108: if (DEBUG_GTE(FILTER, 2)) {
109: rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
110: who_am_i(), mergelist_cnt - 1,
111: ex->u.mergelist->debug_type);
1.1 misho 112: }
1.1.1.2 misho 113:
114: free(ex->u.mergelist->debug_type);
115: free(ex->u.mergelist);
1.1.1.3 ! misho 116:
! 117: for (j = 0; j < mergelist_cnt; j++) {
! 118: if (mergelist_parents[j] == ex) {
! 119: mergelist_parents[j] = NULL;
! 120: break;
! 121: }
! 122: }
! 123: while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
! 124: mergelist_cnt--;
1.1.1.2 misho 125: }
126:
127: static void free_filter(filter_rule *ex)
128: {
1.1.1.3 ! misho 129: if (ex->rflags & FILTRULE_PERDIR_MERGE)
! 130: teardown_mergelist(ex);
1.1 misho 131: free(ex->pattern);
132: free(ex);
133: }
134:
1.1.1.3 ! misho 135: static void free_filters(filter_rule *ent)
1.1.1.2 misho 136: {
1.1.1.3 ! misho 137: while (ent) {
! 138: filter_rule *next = ent->next;
! 139: free_filter(ent);
! 140: ent = next;
1.1.1.2 misho 141: }
142: }
143:
1.1 misho 144: /* Build a filter structure given a filter pattern. The value in "pat"
1.1.1.2 misho 145: * is not null-terminated. "rule" is either held or freed, so the
146: * caller should not free it. */
147: static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
148: filter_rule *rule, int xflags)
1.1 misho 149: {
150: const char *cp;
151: unsigned int pre_len, suf_len, slash_cnt = 0;
152:
1.1.1.2 misho 153: if (DEBUG_GTE(FILTER, 2)) {
1.1 misho 154: rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
1.1.1.2 misho 155: who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
1.1 misho 156: (int)pat_len, pat,
1.1.1.2 misho 157: (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
1.1 misho 158: listp->debug_type);
159: }
160:
161: /* These flags also indicate that we're reading a list that
162: * needs to be filtered now, not post-filtered later. */
1.1.1.2 misho 163: if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
164: && (rule->rflags & FILTRULES_SIDES)
165: == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
166: /* This filter applies only to the other side. Drop it. */
167: free_filter(rule);
168: return;
1.1 misho 169: }
170:
171: if (pat_len > 1 && pat[pat_len-1] == '/') {
172: pat_len--;
1.1.1.2 misho 173: rule->rflags |= FILTRULE_DIRECTORY;
1.1 misho 174: }
175:
176: for (cp = pat; cp < pat + pat_len; cp++) {
177: if (*cp == '/')
178: slash_cnt++;
179: }
180:
1.1.1.2 misho 181: if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
1.1 misho 182: && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
183: || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
1.1.1.2 misho 184: rule->rflags |= FILTRULE_ABS_PATH;
1.1 misho 185: if (*pat == '/')
186: pre_len = dirbuf_len - module_dirlen - 1;
187: else
188: pre_len = 0;
189: } else
190: pre_len = 0;
191:
192: /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
193: if (xflags & XFLG_DIR2WILD3
1.1.1.2 misho 194: && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
195: rule->rflags &= ~FILTRULE_DIRECTORY;
1.1 misho 196: suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
197: } else
198: suf_len = 0;
199:
1.1.1.2 misho 200: if (!(rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
1.1 misho 201: out_of_memory("add_rule");
202: if (pre_len) {
1.1.1.2 misho 203: memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
204: for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
1.1 misho 205: if (*cp == '/')
206: slash_cnt++;
207: }
208: }
1.1.1.2 misho 209: strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
1.1 misho 210: pat_len += pre_len;
211: if (suf_len) {
1.1.1.2 misho 212: memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
1.1 misho 213: pat_len += suf_len;
214: slash_cnt++;
215: }
216:
1.1.1.2 misho 217: if (strpbrk(rule->pattern, "*[?")) {
218: rule->rflags |= FILTRULE_WILD;
219: if ((cp = strstr(rule->pattern, "**")) != NULL) {
220: rule->rflags |= FILTRULE_WILD2;
1.1 misho 221: /* If the pattern starts with **, note that. */
1.1.1.2 misho 222: if (cp == rule->pattern)
223: rule->rflags |= FILTRULE_WILD2_PREFIX;
1.1 misho 224: /* If the pattern ends with ***, note that. */
225: if (pat_len >= 3
1.1.1.2 misho 226: && rule->pattern[pat_len-3] == '*'
227: && rule->pattern[pat_len-2] == '*'
228: && rule->pattern[pat_len-1] == '*')
229: rule->rflags |= FILTRULE_WILD3_SUFFIX;
1.1 misho 230: }
231: }
232:
1.1.1.2 misho 233: if (rule->rflags & FILTRULE_PERDIR_MERGE) {
234: filter_rule_list *lp;
1.1 misho 235: unsigned int len;
236: int i;
237:
1.1.1.2 misho 238: if ((cp = strrchr(rule->pattern, '/')) != NULL)
1.1 misho 239: cp++;
240: else
1.1.1.2 misho 241: cp = rule->pattern;
1.1 misho 242:
243: /* If the local merge file was already mentioned, don't
244: * add it again. */
245: for (i = 0; i < mergelist_cnt; i++) {
1.1.1.2 misho 246: filter_rule *ex = mergelist_parents[i];
1.1.1.3 ! misho 247: const char *s;
! 248: if (!ex)
! 249: continue;
! 250: s = strrchr(ex->pattern, '/');
1.1 misho 251: if (s)
252: s++;
253: else
254: s = ex->pattern;
255: len = strlen(s);
1.1.1.2 misho 256: if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
257: free_filter(rule);
1.1 misho 258: return;
259: }
260: }
261:
1.1.1.3 ! misho 262: if (!(lp = new_array0(filter_rule_list, 1)))
1.1 misho 263: out_of_memory("add_rule");
264: if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
265: out_of_memory("add_rule");
1.1.1.2 misho 266: rule->u.mergelist = lp;
1.1 misho 267:
268: if (mergelist_cnt == mergelist_size) {
269: mergelist_size += 5;
270: mergelist_parents = realloc_array(mergelist_parents,
1.1.1.2 misho 271: filter_rule *,
1.1 misho 272: mergelist_size);
273: if (!mergelist_parents)
274: out_of_memory("add_rule");
275: }
1.1.1.2 misho 276: if (DEBUG_GTE(FILTER, 2)) {
277: rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
278: who_am_i(), mergelist_cnt, lp->debug_type);
279: }
280: mergelist_parents[mergelist_cnt++] = rule;
1.1 misho 281: } else
1.1.1.2 misho 282: rule->u.slash_cnt = slash_cnt;
1.1 misho 283:
284: if (!listp->tail) {
1.1.1.2 misho 285: rule->next = listp->head;
286: listp->head = listp->tail = rule;
1.1 misho 287: } else {
1.1.1.2 misho 288: rule->next = listp->tail->next;
289: listp->tail->next = rule;
290: listp->tail = rule;
1.1 misho 291: }
292: }
293:
1.1.1.3 ! misho 294: /* This frees any non-inherited items, leaving just inherited items on the list. */
! 295: static void pop_filter_list(filter_rule_list *listp)
1.1 misho 296: {
1.1.1.3 ! misho 297: filter_rule *inherited;
! 298:
! 299: if (!listp->tail)
! 300: return;
! 301:
! 302: inherited = listp->tail->next;
1.1 misho 303:
1.1.1.3 ! misho 304: /* Truncate any inherited items from the local list. */
! 305: listp->tail->next = NULL;
! 306: /* Now free everything that is left. */
! 307: free_filters(listp->head);
! 308:
! 309: listp->head = inherited;
! 310: listp->tail = NULL;
1.1 misho 311: }
312:
313: /* This returns an expanded (absolute) filename for the merge-file name if
314: * the name has any slashes in it OR if the parent_dirscan var is True;
315: * otherwise it returns the original merge_file name. If the len_ptr value
316: * is non-NULL the merge_file name is limited by the referenced length
317: * value and will be updated with the length of the resulting name. We
318: * always return a name that is null terminated, even if the merge_file
319: * name was not. */
320: static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
321: unsigned int prefix_skip)
322: {
323: static char buf[MAXPATHLEN];
324: char *fn, tmpbuf[MAXPATHLEN];
325: unsigned int fn_len;
326:
327: if (!parent_dirscan && *merge_file != '/') {
328: /* Return the name unchanged it doesn't have any slashes. */
329: if (len_ptr) {
330: const char *p = merge_file + *len_ptr;
331: while (--p > merge_file && *p != '/') {}
332: if (p == merge_file) {
333: strlcpy(buf, merge_file, *len_ptr + 1);
334: return buf;
335: }
336: } else if (strchr(merge_file, '/') == NULL)
337: return (char *)merge_file;
338: }
339:
340: fn = *merge_file == '/' ? buf : tmpbuf;
341: if (sanitize_paths) {
342: const char *r = prefix_skip ? "/" : NULL;
343: /* null-terminate the name if it isn't already */
344: if (len_ptr && merge_file[*len_ptr]) {
345: char *to = fn == buf ? tmpbuf : buf;
346: strlcpy(to, merge_file, *len_ptr + 1);
347: merge_file = to;
348: }
349: if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
350: rprintf(FERROR, "merge-file name overflows: %s\n",
351: merge_file);
352: return NULL;
353: }
354: fn_len = strlen(fn);
355: } else {
356: strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
357: fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
358: }
359:
1.1.1.3 ! misho 360: /* If the name isn't in buf yet, it wasn't absolute. */
1.1 misho 361: if (fn != buf) {
362: int d_len = dirbuf_len - prefix_skip;
363: if (d_len + fn_len >= MAXPATHLEN) {
364: rprintf(FERROR, "merge-file name overflows: %s\n", fn);
365: return NULL;
366: }
367: memcpy(buf, dirbuf + prefix_skip, d_len);
368: memcpy(buf + d_len, fn, fn_len + 1);
369: fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
370: }
371:
372: if (len_ptr)
373: *len_ptr = fn_len;
374: return buf;
375: }
376:
377: /* Sets the dirbuf and dirbuf_len values. */
378: void set_filter_dir(const char *dir, unsigned int dirlen)
379: {
380: unsigned int len;
381: if (*dir != '/') {
382: memcpy(dirbuf, curr_dir, curr_dir_len);
383: dirbuf[curr_dir_len] = '/';
384: len = curr_dir_len + 1;
385: if (len + dirlen >= MAXPATHLEN)
386: dirlen = 0;
387: } else
388: len = 0;
389: memcpy(dirbuf + len, dir, dirlen);
390: dirbuf[dirlen + len] = '\0';
391: dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
392: if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
393: && dirbuf[dirbuf_len-2] == '/')
394: dirbuf_len -= 2;
395: if (dirbuf_len != 1)
396: dirbuf[dirbuf_len++] = '/';
397: dirbuf[dirbuf_len] = '\0';
398: if (sanitize_paths)
399: dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
400: }
401:
402: /* This routine takes a per-dir merge-file entry and finishes its setup.
403: * If the name has a path portion then we check to see if it refers to a
404: * parent directory of the first transfer dir. If it does, we scan all the
405: * dirs from that point through the parent dir of the transfer dir looking
406: * for the per-dir merge-file in each one. */
1.1.1.2 misho 407: static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
408: filter_rule_list *lp)
1.1 misho 409: {
410: char buf[MAXPATHLEN];
411: char *x, *y, *pat = ex->pattern;
412: unsigned int len;
413:
414: if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
415: return 0;
416:
1.1.1.2 misho 417: if (DEBUG_GTE(FILTER, 2)) {
418: rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
419: who_am_i(), mergelist_num, lp->debug_type);
420: }
1.1 misho 421: y = strrchr(x, '/');
422: *y = '\0';
423: ex->pattern = strdup(y+1);
424: if (!*x)
425: x = "/";
426: if (*x == '/')
427: strlcpy(buf, x, MAXPATHLEN);
428: else
429: pathjoin(buf, MAXPATHLEN, dirbuf, x);
430:
431: len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
432: if (len != 1 && len < MAXPATHLEN-1) {
433: buf[len++] = '/';
434: buf[len] = '\0';
435: }
436: /* This ensures that the specified dir is a parent of the transfer. */
437: for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
438: if (*x)
439: y += strlen(y); /* nope -- skip the scan */
440:
441: parent_dirscan = True;
442: while (*y) {
443: char save[MAXPATHLEN];
444: strlcpy(save, y, MAXPATHLEN);
445: *y = '\0';
446: dirbuf_len = y - dirbuf;
447: strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
1.1.1.2 misho 448: parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
449: if (ex->rflags & FILTRULE_NO_INHERIT) {
450: /* Free the undesired rules to clean up any per-dir
451: * mergelists they defined. Otherwise pop_local_filters
452: * may crash trying to restore nonexistent state for
453: * those mergelists. */
454: free_filters(lp->head);
1.1 misho 455: lp->head = NULL;
1.1.1.2 misho 456: }
1.1 misho 457: lp->tail = NULL;
458: strlcpy(y, save, MAXPATHLEN);
459: while ((*x++ = *y++) != '/') {}
460: }
461: parent_dirscan = False;
1.1.1.2 misho 462: if (DEBUG_GTE(FILTER, 2)) {
463: rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
464: who_am_i(), mergelist_num, lp->debug_type);
465: }
1.1 misho 466: free(pat);
467: return 1;
468: }
469:
1.1.1.2 misho 470: struct local_filter_state {
471: int mergelist_cnt;
472: filter_rule_list mergelists[1];
473: };
474:
1.1 misho 475: /* Each time rsync changes to a new directory it call this function to
476: * handle all the per-dir merge-files. The "dir" value is the current path
477: * relative to curr_dir (which might not be null-terminated). We copy it
478: * into dirbuf so that we can easily append a file name on the end. */
479: void *push_local_filters(const char *dir, unsigned int dirlen)
480: {
1.1.1.2 misho 481: struct local_filter_state *push;
1.1 misho 482: int i;
483:
484: set_filter_dir(dir, dirlen);
1.1.1.2 misho 485: if (DEBUG_GTE(FILTER, 2)) {
486: rprintf(FINFO, "[%s] pushing local filters for %s\n",
487: who_am_i(), dirbuf);
488: }
1.1 misho 489:
1.1.1.2 misho 490: if (!mergelist_cnt) {
491: /* No old state to save and no new merge files to push. */
1.1 misho 492: return NULL;
1.1.1.2 misho 493: }
1.1 misho 494:
1.1.1.2 misho 495: push = (struct local_filter_state *)new_array(char,
496: sizeof (struct local_filter_state)
497: + (mergelist_cnt-1) * sizeof (filter_rule_list));
1.1 misho 498: if (!push)
499: out_of_memory("push_local_filters");
500:
1.1.1.2 misho 501: push->mergelist_cnt = mergelist_cnt;
502: for (i = 0; i < mergelist_cnt; i++) {
1.1.1.3 ! misho 503: filter_rule *ex = mergelist_parents[i];
! 504: if (!ex)
! 505: continue;
! 506: memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
1.1 misho 507: }
508:
509: /* Note: parse_filter_file() might increase mergelist_cnt, so keep
510: * this loop separate from the above loop. */
511: for (i = 0; i < mergelist_cnt; i++) {
1.1.1.2 misho 512: filter_rule *ex = mergelist_parents[i];
1.1.1.3 ! misho 513: filter_rule_list *lp;
! 514: if (!ex)
! 515: continue;
! 516: lp = ex->u.mergelist;
1.1 misho 517:
1.1.1.2 misho 518: if (DEBUG_GTE(FILTER, 2)) {
519: rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
520: who_am_i(), i, lp->debug_type);
1.1 misho 521: }
522:
523: lp->tail = NULL; /* Switch any local rules to inherited. */
1.1.1.2 misho 524: if (ex->rflags & FILTRULE_NO_INHERIT)
1.1 misho 525: lp->head = NULL;
526:
1.1.1.2 misho 527: if (ex->rflags & FILTRULE_FINISH_SETUP) {
528: ex->rflags &= ~FILTRULE_FINISH_SETUP;
529: if (setup_merge_file(i, ex, lp))
1.1 misho 530: set_filter_dir(dir, dirlen);
531: }
532:
533: if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
534: MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
1.1.1.2 misho 535: parse_filter_file(lp, dirbuf, ex,
1.1 misho 536: XFLG_ANCHORED2ABS);
537: } else {
538: io_error |= IOERR_GENERAL;
539: rprintf(FERROR,
540: "cannot add local filter rules in long-named directory: %s\n",
541: full_fname(dirbuf));
542: }
543: dirbuf[dirbuf_len] = '\0';
544: }
545:
546: return (void*)push;
547: }
548:
549: void pop_local_filters(void *mem)
550: {
1.1.1.2 misho 551: struct local_filter_state *pop = (struct local_filter_state *)mem;
1.1 misho 552: int i;
1.1.1.2 misho 553: int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
554:
555: if (DEBUG_GTE(FILTER, 2))
556: rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
1.1 misho 557:
558: for (i = mergelist_cnt; i-- > 0; ) {
1.1.1.2 misho 559: filter_rule *ex = mergelist_parents[i];
1.1.1.3 ! misho 560: filter_rule_list *lp;
! 561: if (!ex)
! 562: continue;
! 563: lp = ex->u.mergelist;
1.1 misho 564:
1.1.1.2 misho 565: if (DEBUG_GTE(FILTER, 2)) {
566: rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
567: who_am_i(), i, lp->debug_type);
1.1 misho 568: }
569:
1.1.1.3 ! misho 570: pop_filter_list(lp);
! 571: if (i >= old_mergelist_cnt && lp->head) {
! 572: /* This mergelist does not exist in the state to be restored, but it
! 573: * still has inherited rules. This can sometimes happen if a per-dir
! 574: * merge file calls setup_merge_file() in push_local_filters() and that
! 575: * leaves some inherited rules that aren't in the pushed list state. */
1.1.1.2 misho 576: if (DEBUG_GTE(FILTER, 2)) {
577: rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
578: who_am_i(), i, ex->u.mergelist->debug_type);
579: }
1.1.1.3 ! misho 580: pop_filter_list(lp);
1.1.1.2 misho 581: }
1.1 misho 582: }
583:
1.1.1.3 ! misho 584: if (!pop)
! 585: return; /* No state to restore. */
1.1 misho 586:
1.1.1.3 ! misho 587: for (i = 0; i < old_mergelist_cnt; i++) {
! 588: filter_rule *ex = mergelist_parents[i];
! 589: if (!ex)
! 590: continue;
! 591: memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
1.1 misho 592: }
593:
594: free(pop);
595: }
596:
597: void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
598: {
599: static int cur_depth = -1;
600: static void *filt_array[MAXPATHLEN/2+1];
601:
602: if (!dname) {
603: for ( ; cur_depth >= 0; cur_depth--) {
604: if (filt_array[cur_depth]) {
605: pop_local_filters(filt_array[cur_depth]);
606: filt_array[cur_depth] = NULL;
607: }
608: }
609: return;
610: }
611:
612: assert(dir_depth < MAXPATHLEN/2+1);
613:
614: for ( ; cur_depth >= dir_depth; cur_depth--) {
615: if (filt_array[cur_depth]) {
616: pop_local_filters(filt_array[cur_depth]);
617: filt_array[cur_depth] = NULL;
618: }
619: }
620:
621: cur_depth = dir_depth;
622: filt_array[cur_depth] = push_local_filters(dname, dlen);
623: }
624:
1.1.1.2 misho 625: static int rule_matches(const char *fname, filter_rule *ex, int name_is_dir)
1.1 misho 626: {
627: int slash_handling, str_cnt = 0, anchored_match = 0;
1.1.1.2 misho 628: int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
1.1 misho 629: char *p, *pattern = ex->pattern;
630: const char *strings[16]; /* more than enough */
631: const char *name = fname + (*fname == '/');
632:
633: if (!*name)
634: return 0;
635:
1.1.1.2 misho 636: if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
1.1 misho 637: /* If the pattern does not have any slashes AND it does
638: * not have a "**" (which could match a slash), then we
639: * just match the name portion of the path. */
640: if ((p = strrchr(name,'/')) != NULL)
641: name = p+1;
1.1.1.2 misho 642: } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
1.1 misho 643: && curr_dir_len > module_dirlen + 1) {
644: /* If we're matching against an absolute-path pattern,
645: * we need to prepend our full path info. */
646: strings[str_cnt++] = curr_dir + module_dirlen + 1;
647: strings[str_cnt++] = "/";
1.1.1.2 misho 648: } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
1.1 misho 649: /* Allow "**"+"/" to match at the start of the string. */
650: strings[str_cnt++] = "/";
651: }
652: strings[str_cnt++] = name;
653: if (name_is_dir) {
654: /* Allow a trailing "/"+"***" to match the directory. */
1.1.1.2 misho 655: if (ex->rflags & FILTRULE_WILD3_SUFFIX)
1.1 misho 656: strings[str_cnt++] = "/";
1.1.1.2 misho 657: } else if (ex->rflags & FILTRULE_DIRECTORY)
1.1 misho 658: return !ret_match;
659: strings[str_cnt] = NULL;
660:
661: if (*pattern == '/') {
662: anchored_match = 1;
663: pattern++;
664: }
665:
666: if (!anchored_match && ex->u.slash_cnt
1.1.1.2 misho 667: && !(ex->rflags & FILTRULE_WILD2)) {
1.1 misho 668: /* A non-anchored match with an infix slash and no "**"
669: * needs to match the last slash_cnt+1 name elements. */
670: slash_handling = ex->u.slash_cnt + 1;
1.1.1.2 misho 671: } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
672: && ex->rflags & FILTRULE_WILD2) {
1.1 misho 673: /* A non-anchored match with an infix or trailing "**" (but not
674: * a prefixed "**") needs to try matching after every slash. */
675: slash_handling = -1;
676: } else {
677: /* The pattern matches only at the start of the path or name. */
678: slash_handling = 0;
679: }
680:
1.1.1.2 misho 681: if (ex->rflags & FILTRULE_WILD) {
1.1 misho 682: if (wildmatch_array(pattern, strings, slash_handling))
683: return ret_match;
684: } else if (str_cnt > 1) {
685: if (litmatch_array(pattern, strings, slash_handling))
686: return ret_match;
687: } else if (anchored_match) {
688: if (strcmp(name, pattern) == 0)
689: return ret_match;
690: } else {
691: int l1 = strlen(name);
692: int l2 = strlen(pattern);
693: if (l2 <= l1 &&
694: strcmp(name+(l1-l2),pattern) == 0 &&
695: (l1==l2 || name[l1-(l2+1)] == '/')) {
696: return ret_match;
697: }
698: }
699:
700: return !ret_match;
701: }
702:
703: static void report_filter_result(enum logcode code, char const *name,
1.1.1.2 misho 704: filter_rule const *ent,
705: int name_is_dir, const char *type)
1.1 misho 706: {
707: /* If a trailing slash is present to match only directories,
708: * then it is stripped out by add_rule(). So as a special
709: * case we add it back in here. */
710:
1.1.1.2 misho 711: if (DEBUG_GTE(FILTER, 1)) {
1.1 misho 712: static char *actions[2][2]
713: = { {"show", "hid"}, {"risk", "protect"} };
714: const char *w = who_am_i();
715: rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
1.1.1.2 misho 716: w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
1.1 misho 717: name_is_dir ? "directory" : "file", name, ent->pattern,
1.1.1.2 misho 718: ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
1.1 misho 719: }
720: }
721:
1.1.1.2 misho 722: /* Return -1 if file "name" is defined to be excluded by the specified
723: * exclude list, 1 if it is included, and 0 if it was not matched. */
724: int check_filter(filter_rule_list *listp, enum logcode code,
1.1 misho 725: const char *name, int name_is_dir)
726: {
1.1.1.2 misho 727: filter_rule *ent;
1.1 misho 728:
729: for (ent = listp->head; ent; ent = ent->next) {
1.1.1.2 misho 730: if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
1.1 misho 731: continue;
1.1.1.2 misho 732: if (ent->rflags & FILTRULE_PERDIR_MERGE) {
1.1 misho 733: int rc = check_filter(ent->u.mergelist, code, name,
734: name_is_dir);
735: if (rc)
736: return rc;
737: continue;
738: }
1.1.1.2 misho 739: if (ent->rflags & FILTRULE_CVS_IGNORE) {
1.1 misho 740: int rc = check_filter(&cvs_filter_list, code, name,
741: name_is_dir);
742: if (rc)
743: return rc;
744: continue;
745: }
746: if (rule_matches(name, ent, name_is_dir)) {
747: report_filter_result(code, name, ent, name_is_dir,
748: listp->debug_type);
1.1.1.2 misho 749: return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
1.1 misho 750: }
751: }
752:
753: return 0;
754: }
755:
756: #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
757:
758: static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
759: {
760: if (strncmp((char*)str, rule, rule_len) != 0)
761: return NULL;
762: if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
763: return str + rule_len - 1;
764: if (str[rule_len] == ',')
765: return str + rule_len;
766: return NULL;
767: }
768:
1.1.1.2 misho 769: #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
770: | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
771: | FILTRULE_PERISHABLE)
772:
773: /* Gets the next include/exclude rule from *rulestr_ptr and advances
774: * *rulestr_ptr to point beyond it. Stores the pattern's start (within
775: * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
776: * allocated filter_rule containing the rest of the information. Returns
777: * NULL if there are no more rules in the input.
778: *
779: * The template provides defaults for the new rule to inherit, and the
780: * template rflags and the xflags additionally affect parsing. */
781: static filter_rule *parse_rule_tok(const char **rulestr_ptr,
782: const filter_rule *template, int xflags,
783: const char **pat_ptr, unsigned int *pat_len_ptr)
1.1 misho 784: {
1.1.1.2 misho 785: const uchar *s = (const uchar *)*rulestr_ptr;
786: filter_rule *rule;
1.1 misho 787: unsigned int len;
788:
1.1.1.2 misho 789: if (template->rflags & FILTRULE_WORD_SPLIT) {
1.1 misho 790: /* Skip over any initial whitespace. */
791: while (isspace(*s))
792: s++;
793: /* Update to point to real start of rule. */
1.1.1.2 misho 794: *rulestr_ptr = (const char *)s;
1.1 misho 795: }
796: if (!*s)
797: return NULL;
798:
1.1.1.2 misho 799: if (!(rule = new0(filter_rule)))
800: out_of_memory("parse_rule_tok");
801:
802: /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
803: * that later. */
804: rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
1.1 misho 805:
806: /* Figure out what kind of a filter rule "s" is pointing at. Note
1.1.1.2 misho 807: * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
808: * or an exclude based on the inheritance of the FILTRULE_INCLUDE
1.1 misho 809: * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
810: * for old include/exclude patterns where just "+ " and "- " are
811: * allowed as optional prefixes. */
1.1.1.2 misho 812: if (template->rflags & FILTRULE_NO_PREFIXES) {
813: if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
814: rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1.1 misho 815: } else if (xflags & XFLG_OLD_PREFIXES) {
816: if (*s == '-' && s[1] == ' ') {
1.1.1.2 misho 817: rule->rflags &= ~FILTRULE_INCLUDE;
1.1 misho 818: s += 2;
819: } else if (*s == '+' && s[1] == ' ') {
1.1.1.2 misho 820: rule->rflags |= FILTRULE_INCLUDE;
1.1 misho 821: s += 2;
822: } else if (*s == '!')
1.1.1.2 misho 823: rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
1.1 misho 824: } else {
1.1.1.2 misho 825: char ch = 0;
826: BOOL prefix_specifies_side = False;
1.1 misho 827: switch (*s) {
828: case 'c':
829: if ((s = RULE_STRCMP(s, "clear")) != NULL)
830: ch = '!';
831: break;
832: case 'd':
833: if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
834: ch = ':';
835: break;
836: case 'e':
837: if ((s = RULE_STRCMP(s, "exclude")) != NULL)
838: ch = '-';
839: break;
840: case 'h':
841: if ((s = RULE_STRCMP(s, "hide")) != NULL)
842: ch = 'H';
843: break;
844: case 'i':
845: if ((s = RULE_STRCMP(s, "include")) != NULL)
846: ch = '+';
847: break;
848: case 'm':
849: if ((s = RULE_STRCMP(s, "merge")) != NULL)
850: ch = '.';
851: break;
852: case 'p':
853: if ((s = RULE_STRCMP(s, "protect")) != NULL)
854: ch = 'P';
855: break;
856: case 'r':
857: if ((s = RULE_STRCMP(s, "risk")) != NULL)
858: ch = 'R';
859: break;
860: case 's':
861: if ((s = RULE_STRCMP(s, "show")) != NULL)
862: ch = 'S';
863: break;
864: default:
865: ch = *s;
866: if (s[1] == ',')
867: s++;
868: break;
869: }
870: switch (ch) {
871: case ':':
1.1.1.2 misho 872: rule->rflags |= FILTRULE_PERDIR_MERGE
1.1.1.3 ! misho 873: | FILTRULE_FINISH_SETUP;
1.1 misho 874: /* FALL THROUGH */
875: case '.':
1.1.1.2 misho 876: rule->rflags |= FILTRULE_MERGE_FILE;
1.1 misho 877: break;
878: case '+':
1.1.1.2 misho 879: rule->rflags |= FILTRULE_INCLUDE;
880: break;
1.1 misho 881: case '-':
882: break;
883: case 'S':
1.1.1.2 misho 884: rule->rflags |= FILTRULE_INCLUDE;
1.1 misho 885: /* FALL THROUGH */
886: case 'H':
1.1.1.2 misho 887: rule->rflags |= FILTRULE_SENDER_SIDE;
888: prefix_specifies_side = True;
1.1 misho 889: break;
890: case 'R':
1.1.1.2 misho 891: rule->rflags |= FILTRULE_INCLUDE;
1.1 misho 892: /* FALL THROUGH */
893: case 'P':
1.1.1.2 misho 894: rule->rflags |= FILTRULE_RECEIVER_SIDE;
895: prefix_specifies_side = True;
1.1 misho 896: break;
897: case '!':
1.1.1.2 misho 898: rule->rflags |= FILTRULE_CLEAR_LIST;
1.1 misho 899: break;
900: default:
1.1.1.2 misho 901: rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
1.1 misho 902: exit_cleanup(RERR_SYNTAX);
903: }
1.1.1.2 misho 904: while (ch != '!' && *++s && *s != ' ' && *s != '_') {
905: if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
906: s--;
907: break;
908: }
909: switch (*s) {
910: default:
1.1 misho 911: invalid:
912: rprintf(FERROR,
1.1.1.2 misho 913: "invalid modifier '%c' at position %d in filter rule: %s\n",
914: *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
1.1 misho 915: exit_cleanup(RERR_SYNTAX);
916: case '-':
1.1.1.2 misho 917: if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
918: goto invalid;
919: rule->rflags |= FILTRULE_NO_PREFIXES;
1.1 misho 920: break;
921: case '+':
1.1.1.2 misho 922: if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
923: goto invalid;
924: rule->rflags |= FILTRULE_NO_PREFIXES
925: | FILTRULE_INCLUDE;
1.1 misho 926: break;
927: case '/':
1.1.1.2 misho 928: rule->rflags |= FILTRULE_ABS_PATH;
1.1 misho 929: break;
930: case '!':
1.1.1.2 misho 931: /* Negation really goes with the pattern, so it
932: * isn't useful as a merge-file default. */
933: if (rule->rflags & FILTRULE_MERGE_FILE)
934: goto invalid;
935: rule->rflags |= FILTRULE_NEGATE;
1.1 misho 936: break;
937: case 'C':
1.1.1.2 misho 938: if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
939: goto invalid;
940: rule->rflags |= FILTRULE_NO_PREFIXES
941: | FILTRULE_WORD_SPLIT
942: | FILTRULE_NO_INHERIT
943: | FILTRULE_CVS_IGNORE;
1.1 misho 944: break;
945: case 'e':
1.1.1.2 misho 946: if (!(rule->rflags & FILTRULE_MERGE_FILE))
947: goto invalid;
948: rule->rflags |= FILTRULE_EXCLUDE_SELF;
1.1 misho 949: break;
950: case 'n':
1.1.1.2 misho 951: if (!(rule->rflags & FILTRULE_MERGE_FILE))
952: goto invalid;
953: rule->rflags |= FILTRULE_NO_INHERIT;
1.1 misho 954: break;
955: case 'p':
1.1.1.2 misho 956: rule->rflags |= FILTRULE_PERISHABLE;
1.1 misho 957: break;
958: case 'r':
1.1.1.2 misho 959: if (prefix_specifies_side)
960: goto invalid;
961: rule->rflags |= FILTRULE_RECEIVER_SIDE;
1.1 misho 962: break;
963: case 's':
1.1.1.2 misho 964: if (prefix_specifies_side)
965: goto invalid;
966: rule->rflags |= FILTRULE_SENDER_SIDE;
1.1 misho 967: break;
968: case 'w':
1.1.1.2 misho 969: if (!(rule->rflags & FILTRULE_MERGE_FILE))
970: goto invalid;
971: rule->rflags |= FILTRULE_WORD_SPLIT;
1.1 misho 972: break;
973: }
974: }
975: if (*s)
976: s++;
977: }
1.1.1.2 misho 978: if (template->rflags & FILTRULES_SIDES) {
979: if (rule->rflags & FILTRULES_SIDES) {
980: /* The filter and template both specify side(s). This
981: * is dodgy (and won't work correctly if the template is
982: * a one-sided per-dir merge rule), so reject it. */
983: rprintf(FERROR,
984: "specified-side merge file contains specified-side filter: %s\n",
985: *rulestr_ptr);
986: exit_cleanup(RERR_SYNTAX);
987: }
988: rule->rflags |= template->rflags & FILTRULES_SIDES;
989: }
1.1 misho 990:
1.1.1.2 misho 991: if (template->rflags & FILTRULE_WORD_SPLIT) {
1.1 misho 992: const uchar *cp = s;
993: /* Token ends at whitespace or the end of the string. */
994: while (!isspace(*cp) && *cp != '\0')
995: cp++;
996: len = cp - s;
997: } else
998: len = strlen((char*)s);
999:
1.1.1.2 misho 1000: if (rule->rflags & FILTRULE_CLEAR_LIST) {
1001: if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1.1 misho 1002: && !(xflags & XFLG_OLD_PREFIXES) && len) {
1003: rprintf(FERROR,
1.1.1.2 misho 1004: "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1.1 misho 1005: exit_cleanup(RERR_SYNTAX);
1006: }
1007: if (len > 1)
1.1.1.2 misho 1008: rule->rflags &= ~FILTRULE_CLEAR_LIST;
1009: } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1010: rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1.1 misho 1011: exit_cleanup(RERR_SYNTAX);
1012: }
1013:
1.1.1.2 misho 1014: /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1.1 misho 1015: if (delete_excluded
1.1.1.2 misho 1016: && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
1017: rule->rflags |= FILTRULE_SENDER_SIDE;
1.1 misho 1018:
1.1.1.2 misho 1019: *pat_ptr = (const char *)s;
1020: *pat_len_ptr = len;
1021: *rulestr_ptr = *pat_ptr + len;
1022: return rule;
1.1 misho 1023: }
1024:
1025: static char default_cvsignore[] =
1026: /* These default ignored items come from the CVS manual. */
1027: "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1028: " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1029: " *.old *.bak *.BAK *.orig *.rej .del-*"
1030: " *.a *.olb *.o *.obj *.so *.exe"
1031: " *.Z *.elc *.ln core"
1032: /* The rest we added to suit ourself. */
1033: " .svn/ .git/ .hg/ .bzr/";
1034:
1.1.1.2 misho 1035: static void get_cvs_excludes(uint32 rflags)
1.1 misho 1036: {
1037: static int initialized = 0;
1038: char *p, fname[MAXPATHLEN];
1039:
1040: if (initialized)
1041: return;
1042: initialized = 1;
1043:
1.1.1.2 misho 1044: parse_filter_str(&cvs_filter_list, default_cvsignore,
1045: rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1046: 0);
1.1 misho 1047:
1048: p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1049: if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1.1.1.2 misho 1050: parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1.1 misho 1051:
1.1.1.2 misho 1052: parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1.1 misho 1053: }
1054:
1.1.1.2 misho 1055: const filter_rule *rule_template(uint32 rflags)
1056: {
1057: static filter_rule template; /* zero-initialized */
1058: template.rflags = rflags;
1059: return &template;
1060: }
1.1 misho 1061:
1.1.1.2 misho 1062: void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1063: const filter_rule *template, int xflags)
1.1 misho 1064: {
1.1.1.2 misho 1065: filter_rule *rule;
1066: const char *pat;
1.1 misho 1067: unsigned int pat_len;
1068:
1.1.1.2 misho 1069: if (!rulestr)
1.1 misho 1070: return;
1071:
1072: while (1) {
1.1.1.2 misho 1073: uint32 new_rflags;
1074:
1.1 misho 1075: /* Remember that the returned string is NOT '\0' terminated! */
1.1.1.2 misho 1076: if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1.1 misho 1077: break;
1078:
1079: if (pat_len >= MAXPATHLEN) {
1080: rprintf(FERROR, "discarding over-long filter: %.*s\n",
1.1.1.2 misho 1081: (int)pat_len, pat);
1082: free_continue:
1083: free_filter(rule);
1.1 misho 1084: continue;
1085: }
1086:
1.1.1.2 misho 1087: new_rflags = rule->rflags;
1088: if (new_rflags & FILTRULE_CLEAR_LIST) {
1089: if (DEBUG_GTE(FILTER, 2)) {
1.1 misho 1090: rprintf(FINFO,
1091: "[%s] clearing filter list%s\n",
1092: who_am_i(), listp->debug_type);
1093: }
1.1.1.3 ! misho 1094: pop_filter_list(listp);
! 1095: listp->head = NULL;
1.1.1.2 misho 1096: goto free_continue;
1.1 misho 1097: }
1098:
1.1.1.2 misho 1099: if (new_rflags & FILTRULE_MERGE_FILE) {
1.1 misho 1100: if (!pat_len) {
1.1.1.2 misho 1101: pat = ".cvsignore";
1.1 misho 1102: pat_len = 10;
1103: }
1.1.1.2 misho 1104: if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1105: const char *name;
1106: filter_rule *excl_self;
1107:
1108: if (!(excl_self = new0(filter_rule)))
1109: out_of_memory("parse_filter_str");
1110: /* Find the beginning of the basename and add an exclude for it. */
1111: for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1112: add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1113: rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1.1 misho 1114: }
1.1.1.2 misho 1115: if (new_rflags & FILTRULE_PERDIR_MERGE) {
1.1 misho 1116: if (parent_dirscan) {
1.1.1.2 misho 1117: const char *p;
1118: unsigned int len = pat_len;
1119: if ((p = parse_merge_name(pat, &len, module_dirlen)))
1120: add_rule(listp, p, len, rule, 0);
1121: else
1122: free_filter(rule);
1.1 misho 1123: continue;
1124: }
1125: } else {
1.1.1.2 misho 1126: const char *p;
1127: unsigned int len = pat_len;
1128: if ((p = parse_merge_name(pat, &len, 0)))
1129: parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1130: free_filter(rule);
1.1 misho 1131: continue;
1132: }
1133: }
1134:
1.1.1.2 misho 1135: add_rule(listp, pat, pat_len, rule, xflags);
1.1 misho 1136:
1.1.1.2 misho 1137: if (new_rflags & FILTRULE_CVS_IGNORE
1138: && !(new_rflags & FILTRULE_MERGE_FILE))
1139: get_cvs_excludes(new_rflags);
1.1 misho 1140: }
1141: }
1142:
1.1.1.2 misho 1143: void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1.1 misho 1144: {
1145: FILE *fp;
1146: char line[BIGPATHBUFLEN];
1147: char *eob = line + sizeof line - 1;
1.1.1.2 misho 1148: BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1.1 misho 1149:
1150: if (!fname || !*fname)
1151: return;
1152:
1153: if (*fname != '-' || fname[1] || am_server) {
1154: if (daemon_filter_list.head) {
1155: strlcpy(line, fname, sizeof line);
1156: clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1157: if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1158: fp = NULL;
1159: else
1160: fp = fopen(line, "rb");
1161: } else
1162: fp = fopen(fname, "rb");
1163: } else
1164: fp = stdin;
1165:
1.1.1.2 misho 1166: if (DEBUG_GTE(FILTER, 2)) {
1.1 misho 1167: rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1.1.1.2 misho 1168: who_am_i(), fname, template->rflags, xflags,
1.1 misho 1169: fp ? "" : " [not found]");
1170: }
1171:
1172: if (!fp) {
1173: if (xflags & XFLG_FATAL_ERRORS) {
1174: rsyserr(FERROR, errno,
1175: "failed to open %sclude file %s",
1.1.1.2 misho 1176: template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1.1 misho 1177: fname);
1178: exit_cleanup(RERR_FILEIO);
1179: }
1180: return;
1181: }
1182: dirbuf[dirbuf_len] = '\0';
1183:
1184: while (1) {
1185: char *s = line;
1186: int ch, overflow = 0;
1187: while (1) {
1188: if ((ch = getc(fp)) == EOF) {
1189: if (ferror(fp) && errno == EINTR) {
1190: clearerr(fp);
1191: continue;
1192: }
1193: break;
1194: }
1195: if (word_split && isspace(ch))
1196: break;
1197: if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1198: break;
1199: if (s < eob)
1200: *s++ = ch;
1201: else
1202: overflow = 1;
1203: }
1204: if (overflow) {
1205: rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1206: s = line;
1207: }
1208: *s = '\0';
1209: /* Skip an empty token and (when line parsing) comments. */
1210: if (*line && (word_split || (*line != ';' && *line != '#')))
1.1.1.2 misho 1211: parse_filter_str(listp, line, template, xflags);
1.1 misho 1212: if (ch == EOF)
1213: break;
1214: }
1215: fclose(fp);
1216: }
1217:
1218: /* If the "for_xfer" flag is set, the prefix is made compatible with the
1219: * current protocol_version (if possible) or a NULL is returned (if not
1220: * possible). */
1.1.1.2 misho 1221: char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1.1 misho 1222: unsigned int *plen_ptr)
1223: {
1224: static char buf[MAX_RULE_PREFIX+1];
1225: char *op = buf;
1226: int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1227:
1.1.1.2 misho 1228: if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1.1 misho 1229: if (legal_len == 1)
1230: return NULL;
1231: *op++ = ':';
1.1.1.2 misho 1232: } else if (rule->rflags & FILTRULE_INCLUDE)
1.1 misho 1233: *op++ = '+';
1234: else if (legal_len != 1
1235: || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1236: *op++ = '-';
1237: else
1238: legal_len = 0;
1239:
1.1.1.2 misho 1240: if (rule->rflags & FILTRULE_ABS_PATH)
1.1 misho 1241: *op++ = '/';
1.1.1.2 misho 1242: if (rule->rflags & FILTRULE_NEGATE)
1.1 misho 1243: *op++ = '!';
1.1.1.2 misho 1244: if (rule->rflags & FILTRULE_CVS_IGNORE)
1.1 misho 1245: *op++ = 'C';
1246: else {
1.1.1.2 misho 1247: if (rule->rflags & FILTRULE_NO_INHERIT)
1.1 misho 1248: *op++ = 'n';
1.1.1.2 misho 1249: if (rule->rflags & FILTRULE_WORD_SPLIT)
1.1 misho 1250: *op++ = 'w';
1.1.1.2 misho 1251: if (rule->rflags & FILTRULE_NO_PREFIXES) {
1252: if (rule->rflags & FILTRULE_INCLUDE)
1.1 misho 1253: *op++ = '+';
1254: else
1255: *op++ = '-';
1256: }
1257: }
1.1.1.2 misho 1258: if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1.1 misho 1259: *op++ = 'e';
1.1.1.2 misho 1260: if (rule->rflags & FILTRULE_SENDER_SIDE
1.1 misho 1261: && (!for_xfer || protocol_version >= 29))
1262: *op++ = 's';
1.1.1.2 misho 1263: if (rule->rflags & FILTRULE_RECEIVER_SIDE
1.1 misho 1264: && (!for_xfer || protocol_version >= 29
1265: || (delete_excluded && am_sender)))
1266: *op++ = 'r';
1.1.1.2 misho 1267: if (rule->rflags & FILTRULE_PERISHABLE) {
1.1 misho 1268: if (!for_xfer || protocol_version >= 30)
1269: *op++ = 'p';
1270: else if (am_sender)
1271: return NULL;
1272: }
1273: if (op - buf > legal_len)
1274: return NULL;
1275: if (legal_len)
1276: *op++ = ' ';
1277: *op = '\0';
1278: if (plen_ptr)
1279: *plen_ptr = op - buf;
1280: return buf;
1281: }
1282:
1.1.1.2 misho 1283: static void send_rules(int f_out, filter_rule_list *flp)
1.1 misho 1284: {
1.1.1.2 misho 1285: filter_rule *ent, *prev = NULL;
1.1 misho 1286:
1287: for (ent = flp->head; ent; ent = ent->next) {
1288: unsigned int len, plen, dlen;
1289: int elide = 0;
1290: char *p;
1291:
1292: /* Note we need to check delete_excluded here in addition to
1293: * the code in parse_rule_tok() because some rules may have
1294: * been added before we found the --delete-excluded option.
1295: * We must also elide any CVS merge-file rules to avoid a
1296: * backward compatibility problem, and we elide any no-prefix
1297: * merge files as an optimization (since they can only have
1298: * include/exclude rules). */
1.1.1.2 misho 1299: if (ent->rflags & FILTRULE_SENDER_SIDE)
1.1 misho 1300: elide = am_sender ? 1 : -1;
1.1.1.2 misho 1301: if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1.1 misho 1302: elide = elide ? 0 : am_sender ? -1 : 1;
1303: else if (delete_excluded && !elide
1.1.1.2 misho 1304: && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1305: || ent->rflags & FILTRULE_NO_PREFIXES))
1.1 misho 1306: elide = am_sender ? 1 : -1;
1307: if (elide < 0) {
1308: if (prev)
1309: prev->next = ent->next;
1310: else
1311: flp->head = ent->next;
1312: } else
1313: prev = ent;
1314: if (elide > 0)
1315: continue;
1.1.1.2 misho 1316: if (ent->rflags & FILTRULE_CVS_IGNORE
1317: && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1.1 misho 1318: int f = am_sender || protocol_version < 29 ? f_out : -2;
1319: send_rules(f, &cvs_filter_list);
1320: if (f == f_out)
1321: continue;
1322: }
1.1.1.2 misho 1323: p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1.1 misho 1324: if (!p) {
1325: rprintf(FERROR,
1326: "filter rules are too modern for remote rsync.\n");
1327: exit_cleanup(RERR_PROTOCOL);
1328: }
1329: if (f_out < 0)
1330: continue;
1331: len = strlen(ent->pattern);
1.1.1.2 misho 1332: dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1.1 misho 1333: if (!(plen + len + dlen))
1334: continue;
1335: write_int(f_out, plen + len + dlen);
1336: if (plen)
1337: write_buf(f_out, p, plen);
1338: write_buf(f_out, ent->pattern, len);
1339: if (dlen)
1340: write_byte(f_out, '/');
1341: }
1342: flp->tail = prev;
1343: }
1344:
1345: /* This is only called by the client. */
1346: void send_filter_list(int f_out)
1347: {
1348: int receiver_wants_list = prune_empty_dirs
1349: || (delete_mode && (!delete_excluded || protocol_version >= 29));
1350:
1351: if (local_server || (am_sender && !receiver_wants_list))
1352: f_out = -1;
1353: if (cvs_exclude && am_sender) {
1354: if (protocol_version >= 29)
1.1.1.2 misho 1355: parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1356: parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1.1 misho 1357: }
1358:
1359: send_rules(f_out, &filter_list);
1360:
1361: if (f_out >= 0)
1362: write_int(f_out, 0);
1363:
1364: if (cvs_exclude) {
1365: if (!am_sender || protocol_version < 29)
1.1.1.2 misho 1366: parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1.1 misho 1367: if (!am_sender)
1.1.1.2 misho 1368: parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1.1 misho 1369: }
1370: }
1371:
1372: /* This is only called by the server. */
1373: void recv_filter_list(int f_in)
1374: {
1375: char line[BIGPATHBUFLEN];
1376: int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1377: int receiver_wants_list = prune_empty_dirs
1378: || (delete_mode
1379: && (!delete_excluded || protocol_version >= 29));
1380: unsigned int len;
1381:
1382: if (!local_server && (am_sender || receiver_wants_list)) {
1383: while ((len = read_int(f_in)) != 0) {
1384: if (len >= sizeof line)
1385: overflow_exit("recv_rules");
1386: read_sbuf(f_in, line, len);
1.1.1.2 misho 1387: parse_filter_str(&filter_list, line, rule_template(0), xflags);
1.1 misho 1388: }
1389: }
1390:
1391: if (cvs_exclude) {
1392: if (local_server || am_sender || protocol_version < 29)
1.1.1.2 misho 1393: parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1.1 misho 1394: if (local_server || am_sender)
1.1.1.2 misho 1395: parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1.1 misho 1396: }
1397:
1398: if (local_server) /* filter out any rules that aren't for us. */
1399: send_rules(-1, &filter_list);
1400: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>