| 1 | /* -*- c-file-style: "linux" -*- |
| 2 | * |
| 3 | * Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org> |
| 4 | * Copyright (C) 1996 by Paul Mackerras |
| 5 | * Copyright (C) 2002 by Martin Pool |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | /* a lot of this stuff was originally derived from GNU tar, although |
| 23 | it has now changed so much that it is hard to tell :) */ |
| 24 | |
| 25 | /* include/exclude cluestick added by Martin Pool <mbp@samba.org> */ |
| 26 | |
| 27 | #include "rsync.h" |
| 28 | |
| 29 | extern int verbose; |
| 30 | extern int am_server; |
| 31 | extern int eol_nulls; |
| 32 | extern int list_only; |
| 33 | extern int recurse; |
| 34 | extern int io_error; |
| 35 | extern int sanitize_paths; |
| 36 | extern int protocol_version; |
| 37 | extern int module_id; |
| 38 | |
| 39 | extern char curr_dir[]; |
| 40 | extern unsigned int curr_dir_len; |
| 41 | extern unsigned int module_dirlen; |
| 42 | |
| 43 | struct filter_list_struct filter_list = { 0, 0, "" }; |
| 44 | struct filter_list_struct server_filter_list = { 0, 0, "server " }; |
| 45 | |
| 46 | /* Need room enough for ":MODS " prefix plus some room to grow. */ |
| 47 | #define MAX_RULE_PREFIX (16) |
| 48 | |
| 49 | /* The dirbuf is set by push_local_filters() to the current subdirectory |
| 50 | * relative to curr_dir that is being processed. The path always has a |
| 51 | * trailing slash appended, and the variable dirbuf_len contains the length |
| 52 | * of this path prefix. The path is always absolute. */ |
| 53 | static char dirbuf[MAXPATHLEN+1]; |
| 54 | static unsigned int dirbuf_len = 0; |
| 55 | static int dirbuf_depth; |
| 56 | |
| 57 | /* This is True when we're scanning parent dirs for per-dir merge-files. */ |
| 58 | static BOOL parent_dirscan = False; |
| 59 | |
| 60 | /* This array contains a list of all the currently active per-dir merge |
| 61 | * files. This makes it easier to save the appropriate values when we |
| 62 | * "push" down into each subdirectory. */ |
| 63 | static struct filter_struct **mergelist_parents; |
| 64 | static int mergelist_cnt = 0; |
| 65 | static int mergelist_size = 0; |
| 66 | |
| 67 | /* Each filter_list_struct describes a singly-linked list by keeping track |
| 68 | * of both the head and tail pointers. The list is slightly unusual in that |
| 69 | * a parent-dir's content can be appended to the end of the local list in a |
| 70 | * special way: the last item in the local list has its "next" pointer set |
| 71 | * to point to the inherited list, but the local list's tail pointer points |
| 72 | * at the end of the local list. Thus, if the local list is empty, the head |
| 73 | * will be pointing at the inherited content but the tail will be NULL. To |
| 74 | * help you visualize this, here are the possible list arrangements: |
| 75 | * |
| 76 | * Completely Empty Local Content Only |
| 77 | * ================================== ==================================== |
| 78 | * head -> NULL head -> Local1 -> Local2 -> NULL |
| 79 | * tail -> NULL tail -------------^ |
| 80 | * |
| 81 | * Inherited Content Only Both Local and Inherited Content |
| 82 | * ================================== ==================================== |
| 83 | * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL |
| 84 | * tail -> NULL tail ---------^ |
| 85 | * |
| 86 | * This means that anyone wanting to traverse the whole list to use it just |
| 87 | * needs to start at the head and use the "next" pointers until it goes |
| 88 | * NULL. To add new local content, we insert the item after the tail item |
| 89 | * and update the tail (obviously, if "tail" was NULL, we insert it at the |
| 90 | * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT |
| 91 | * because it is shared between the current list and our parent list(s). |
| 92 | * The easiest way to handle this is to simply truncate the list after the |
| 93 | * tail item and then free the local list from the head. When inheriting |
| 94 | * the list for a new local dir, we just save off the filter_list_struct |
| 95 | * values (so we can pop back to them later) and set the tail to NULL. |
| 96 | */ |
| 97 | |
| 98 | static void free_filter(struct filter_struct *ex) |
| 99 | { |
| 100 | if (ex->match_flags & MATCHFLG_PERDIR_MERGE) { |
| 101 | free(ex->u.mergelist->debug_type); |
| 102 | free(ex->u.mergelist); |
| 103 | mergelist_cnt--; |
| 104 | } |
| 105 | free(ex->pattern); |
| 106 | free(ex); |
| 107 | } |
| 108 | |
| 109 | /* Build a filter structure given a filter pattern. The value in "pat" |
| 110 | * is not null-terminated. */ |
| 111 | static void filter_rule(struct filter_list_struct *listp, const char *pat, |
| 112 | unsigned int pat_len, unsigned int mflags, int xflags) |
| 113 | { |
| 114 | struct filter_struct *ret; |
| 115 | const char *cp; |
| 116 | unsigned int ex_len; |
| 117 | |
| 118 | if (verbose > 2) { |
| 119 | rprintf(FINFO, "[%s] filter_rule(%.*s, %s%s)\n", |
| 120 | who_am_i(), (int)pat_len, pat, |
| 121 | mflags & MATCHFLG_PERDIR_MERGE ? "per-dir-merge" |
| 122 | : mflags & MATCHFLG_INCLUDE ? "include" : "exclude", |
| 123 | listp->debug_type); |
| 124 | } |
| 125 | |
| 126 | ret = new(struct filter_struct); |
| 127 | if (!ret) |
| 128 | out_of_memory("filter_rule"); |
| 129 | memset(ret, 0, sizeof ret[0]); |
| 130 | |
| 131 | if (xflags & XFLG_ANCHORED2ABS && *pat == '/' |
| 132 | && !(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))) { |
| 133 | mflags |= MATCHFLG_ABS_PATH; |
| 134 | ex_len = dirbuf_len - module_dirlen - 1; |
| 135 | } else |
| 136 | ex_len = 0; |
| 137 | ret->pattern = new_array(char, ex_len + pat_len + 1); |
| 138 | if (!ret->pattern) |
| 139 | out_of_memory("filter_rule"); |
| 140 | if (ex_len) |
| 141 | memcpy(ret->pattern, dirbuf + module_dirlen, ex_len); |
| 142 | strlcpy(ret->pattern + ex_len, pat, pat_len + 1); |
| 143 | pat_len += ex_len; |
| 144 | |
| 145 | if (strpbrk(ret->pattern, "*[?")) { |
| 146 | mflags |= MATCHFLG_WILD; |
| 147 | if ((cp = strstr(ret->pattern, "**")) != NULL) { |
| 148 | mflags |= MATCHFLG_WILD2; |
| 149 | /* If the pattern starts with **, note that. */ |
| 150 | if (cp == ret->pattern) |
| 151 | mflags |= MATCHFLG_WILD2_PREFIX; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (pat_len > 1 && ret->pattern[pat_len-1] == '/') { |
| 156 | ret->pattern[pat_len-1] = 0; |
| 157 | mflags |= MATCHFLG_DIRECTORY; |
| 158 | } |
| 159 | |
| 160 | if (mflags & MATCHFLG_PERDIR_MERGE) { |
| 161 | struct filter_list_struct *lp; |
| 162 | unsigned int len; |
| 163 | int i; |
| 164 | |
| 165 | if ((cp = strrchr(ret->pattern, '/')) != NULL) |
| 166 | cp++; |
| 167 | else |
| 168 | cp = ret->pattern; |
| 169 | |
| 170 | /* If the local merge file was already mentioned, don't |
| 171 | * add it again. */ |
| 172 | for (i = 0; i < mergelist_cnt; i++) { |
| 173 | struct filter_struct *ex = mergelist_parents[i]; |
| 174 | const char *s = strrchr(ex->pattern, '/'); |
| 175 | if (s) |
| 176 | s++; |
| 177 | else |
| 178 | s = ex->pattern; |
| 179 | len = strlen(s); |
| 180 | if (len == pat_len - (cp - ret->pattern) |
| 181 | && memcmp(s, cp, len) == 0) { |
| 182 | free_filter(ret); |
| 183 | return; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (!(lp = new_array(struct filter_list_struct, 1))) |
| 188 | out_of_memory("filter_rule"); |
| 189 | lp->head = lp->tail = NULL; |
| 190 | if (asprintf(&lp->debug_type, " (per-dir %s)", cp) < 0) |
| 191 | out_of_memory("filter_rule"); |
| 192 | ret->u.mergelist = lp; |
| 193 | |
| 194 | if (mergelist_cnt == mergelist_size) { |
| 195 | mergelist_size += 5; |
| 196 | mergelist_parents = realloc_array(mergelist_parents, |
| 197 | struct filter_struct *, |
| 198 | mergelist_size); |
| 199 | if (!mergelist_parents) |
| 200 | out_of_memory("filter_rule"); |
| 201 | } |
| 202 | mergelist_parents[mergelist_cnt++] = ret; |
| 203 | } else { |
| 204 | for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++) |
| 205 | ret->u.slash_cnt++; |
| 206 | } |
| 207 | |
| 208 | ret->match_flags = mflags; |
| 209 | |
| 210 | if (!listp->tail) { |
| 211 | ret->next = listp->head; |
| 212 | listp->head = listp->tail = ret; |
| 213 | } else { |
| 214 | ret->next = listp->tail->next; |
| 215 | listp->tail->next = ret; |
| 216 | listp->tail = ret; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static void clear_filter_list(struct filter_list_struct *listp) |
| 221 | { |
| 222 | if (listp->tail) { |
| 223 | struct filter_struct *ent, *next; |
| 224 | /* Truncate any inherited items from the local list. */ |
| 225 | listp->tail->next = NULL; |
| 226 | /* Now free everything that is left. */ |
| 227 | for (ent = listp->head; ent; ent = next) { |
| 228 | next = ent->next; |
| 229 | free_filter(ent); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | listp->head = listp->tail = NULL; |
| 234 | } |
| 235 | |
| 236 | /* This returns an expanded (absolute) filename for the merge-file name if |
| 237 | * the name has any slashes in it OR if the parent_dirscan var is True; |
| 238 | * otherwise it returns the original merge_file name. If the len_ptr value |
| 239 | * is non-NULL the merge_file name is limited by the referenced length |
| 240 | * value and will be updated with the length of the resulting name. We |
| 241 | * always return a name that is null terminated, even if the merge_file |
| 242 | * name was not. */ |
| 243 | static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr, |
| 244 | unsigned int prefix_skip) |
| 245 | { |
| 246 | static char buf[MAXPATHLEN]; |
| 247 | char *fn, tmpbuf[MAXPATHLEN]; |
| 248 | unsigned int fn_len; |
| 249 | |
| 250 | if (!parent_dirscan && *merge_file != '/') { |
| 251 | /* Return the name unchanged it doesn't have any slashes. */ |
| 252 | if (len_ptr) { |
| 253 | const char *p = merge_file + *len_ptr; |
| 254 | while (--p > merge_file && *p != '/') {} |
| 255 | if (p == merge_file) { |
| 256 | strlcpy(buf, merge_file, *len_ptr + 1); |
| 257 | return buf; |
| 258 | } |
| 259 | } else if (strchr(merge_file, '/') == NULL) |
| 260 | return (char *)merge_file; |
| 261 | } |
| 262 | |
| 263 | fn = *merge_file == '/' ? buf : tmpbuf; |
| 264 | if (sanitize_paths) { |
| 265 | const char *r = prefix_skip ? "/" : NULL; |
| 266 | /* null-terminate the name if it isn't already */ |
| 267 | if (len_ptr && merge_file[*len_ptr]) { |
| 268 | char *to = fn == buf ? tmpbuf : buf; |
| 269 | strlcpy(to, merge_file, *len_ptr + 1); |
| 270 | merge_file = to; |
| 271 | } |
| 272 | if (!sanitize_path(fn, merge_file, r, dirbuf_depth)) { |
| 273 | rprintf(FERROR, "merge-file name overflows: %s\n", |
| 274 | merge_file); |
| 275 | return NULL; |
| 276 | } |
| 277 | } else { |
| 278 | strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN); |
| 279 | clean_fname(fn, 1); |
| 280 | } |
| 281 | |
| 282 | fn_len = strlen(fn); |
| 283 | if (fn == buf) |
| 284 | goto done; |
| 285 | |
| 286 | if (dirbuf_len + fn_len >= MAXPATHLEN) { |
| 287 | rprintf(FERROR, "merge-file name overflows: %s\n", fn); |
| 288 | return NULL; |
| 289 | } |
| 290 | memcpy(buf, dirbuf + prefix_skip, dirbuf_len - prefix_skip); |
| 291 | memcpy(buf + dirbuf_len - prefix_skip, fn, fn_len + 1); |
| 292 | fn_len = clean_fname(buf, 1); |
| 293 | |
| 294 | done: |
| 295 | if (len_ptr) |
| 296 | *len_ptr = fn_len; |
| 297 | return buf; |
| 298 | } |
| 299 | |
| 300 | /* Sets the dirbuf and dirbuf_len values. */ |
| 301 | void set_filter_dir(const char *dir, unsigned int dirlen) |
| 302 | { |
| 303 | unsigned int len; |
| 304 | if (*dir != '/') { |
| 305 | memcpy(dirbuf, curr_dir, curr_dir_len); |
| 306 | dirbuf[curr_dir_len] = '/'; |
| 307 | len = curr_dir_len + 1; |
| 308 | if (len + dirlen >= MAXPATHLEN) |
| 309 | dirlen = 0; |
| 310 | } else |
| 311 | len = 0; |
| 312 | memcpy(dirbuf + len, dir, dirlen); |
| 313 | dirbuf[dirlen + len] = '\0'; |
| 314 | dirbuf_len = clean_fname(dirbuf, 1); |
| 315 | if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.' |
| 316 | && dirbuf[dirbuf_len-2] == '/') |
| 317 | dirbuf_len -= 2; |
| 318 | if (dirbuf_len != 1) |
| 319 | dirbuf[dirbuf_len++] = '/'; |
| 320 | dirbuf[dirbuf_len] = '\0'; |
| 321 | if (sanitize_paths) |
| 322 | dirbuf_depth = count_dir_elements(dirbuf + module_dirlen); |
| 323 | } |
| 324 | |
| 325 | /* This routine takes a per-dir merge-file entry and finishes its setup. |
| 326 | * If the name has a path portion then we check to see if it refers to a |
| 327 | * parent directory of the first transfer dir. If it does, we scan all the |
| 328 | * dirs from that point through the parent dir of the transfer dir looking |
| 329 | * for the per-dir merge-file in each one. */ |
| 330 | static BOOL setup_merge_file(struct filter_struct *ex, |
| 331 | struct filter_list_struct *lp, int flags) |
| 332 | { |
| 333 | char buf[MAXPATHLEN]; |
| 334 | char *x, *y, *pat = ex->pattern; |
| 335 | unsigned int len; |
| 336 | |
| 337 | if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/') |
| 338 | return 0; |
| 339 | |
| 340 | y = strrchr(x, '/'); |
| 341 | *y = '\0'; |
| 342 | ex->pattern = strdup(y+1); |
| 343 | if (!*x) |
| 344 | x = "/"; |
| 345 | if (*x == '/') |
| 346 | strlcpy(buf, x, MAXPATHLEN); |
| 347 | else |
| 348 | pathjoin(buf, MAXPATHLEN, dirbuf, x); |
| 349 | |
| 350 | len = clean_fname(buf, 1); |
| 351 | if (len != 1 && len < MAXPATHLEN-1) { |
| 352 | buf[len++] = '/'; |
| 353 | buf[len] = '\0'; |
| 354 | } |
| 355 | /* This ensures that the specified dir is a parent of the transfer. */ |
| 356 | for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {} |
| 357 | if (*x) |
| 358 | y += strlen(y); /* nope -- skip the scan */ |
| 359 | |
| 360 | parent_dirscan = True; |
| 361 | while (*y) { |
| 362 | char save[MAXPATHLEN]; |
| 363 | strlcpy(save, y, MAXPATHLEN); |
| 364 | *y = '\0'; |
| 365 | dirbuf_len = y - dirbuf; |
| 366 | strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf)); |
| 367 | add_filter_file(lp, buf, flags | XFLG_ANCHORED2ABS); |
| 368 | if (ex->match_flags & MATCHFLG_NO_INHERIT) |
| 369 | lp->head = NULL; |
| 370 | lp->tail = NULL; |
| 371 | strlcpy(y, save, MAXPATHLEN); |
| 372 | while ((*x++ = *y++) != '/') {} |
| 373 | } |
| 374 | parent_dirscan = False; |
| 375 | free(pat); |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | /* Each time rsync changes to a new directory it call this function to |
| 380 | * handle all the per-dir merge-files. The "dir" value is the current path |
| 381 | * relative to curr_dir (which might not be null-terminated). We copy it |
| 382 | * into dirbuf so that we can easily append a file name on the end. */ |
| 383 | void *push_local_filters(const char *dir, unsigned int dirlen) |
| 384 | { |
| 385 | struct filter_list_struct *ap, *push; |
| 386 | int i; |
| 387 | |
| 388 | set_filter_dir(dir, dirlen); |
| 389 | |
| 390 | if (!mergelist_cnt) |
| 391 | return NULL; |
| 392 | |
| 393 | push = new_array(struct filter_list_struct, mergelist_cnt); |
| 394 | if (!push) |
| 395 | out_of_memory("push_local_filters"); |
| 396 | |
| 397 | for (i = 0, ap = push; i < mergelist_cnt; i++) { |
| 398 | memcpy(ap++, mergelist_parents[i]->u.mergelist, |
| 399 | sizeof (struct filter_list_struct)); |
| 400 | } |
| 401 | |
| 402 | /* Note: add_filter_file() might increase mergelist_cnt, so keep |
| 403 | * this loop separate from the above loop. */ |
| 404 | for (i = 0; i < mergelist_cnt; i++) { |
| 405 | struct filter_struct *ex = mergelist_parents[i]; |
| 406 | struct filter_list_struct *lp = ex->u.mergelist; |
| 407 | int flags = 0; |
| 408 | |
| 409 | if (verbose > 2) { |
| 410 | rprintf(FINFO, "[%s] pushing filter list%s\n", |
| 411 | who_am_i(), lp->debug_type); |
| 412 | } |
| 413 | |
| 414 | lp->tail = NULL; /* Switch any local rules to inherited. */ |
| 415 | if (ex->match_flags & MATCHFLG_NO_INHERIT) |
| 416 | lp->head = NULL; |
| 417 | if (ex->match_flags & MATCHFLG_WORD_SPLIT) |
| 418 | flags |= XFLG_WORD_SPLIT; |
| 419 | if (ex->match_flags & MATCHFLG_NO_PREFIXES) |
| 420 | flags |= XFLG_NO_PREFIXES; |
| 421 | if (ex->match_flags & MATCHFLG_INCLUDE) |
| 422 | flags |= XFLG_DEF_INCLUDE; |
| 423 | else if (ex->match_flags & MATCHFLG_NO_PREFIXES) |
| 424 | flags |= XFLG_DEF_EXCLUDE; |
| 425 | |
| 426 | if (ex->match_flags & MATCHFLG_FINISH_SETUP) { |
| 427 | ex->match_flags &= ~MATCHFLG_FINISH_SETUP; |
| 428 | if (setup_merge_file(ex, lp, flags)) |
| 429 | set_filter_dir(dir, dirlen); |
| 430 | } |
| 431 | |
| 432 | if (strlcpy(dirbuf + dirbuf_len, ex->pattern, |
| 433 | MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) |
| 434 | add_filter_file(lp, dirbuf, flags | XFLG_ANCHORED2ABS); |
| 435 | else { |
| 436 | io_error |= IOERR_GENERAL; |
| 437 | rprintf(FINFO, |
| 438 | "cannot add local filter rules in long-named directory: %s\n", |
| 439 | full_fname(dirbuf)); |
| 440 | } |
| 441 | dirbuf[dirbuf_len] = '\0'; |
| 442 | } |
| 443 | |
| 444 | return (void*)push; |
| 445 | } |
| 446 | |
| 447 | void pop_local_filters(void *mem) |
| 448 | { |
| 449 | struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem; |
| 450 | int i; |
| 451 | |
| 452 | for (i = mergelist_cnt; i-- > 0; ) { |
| 453 | struct filter_struct *ex = mergelist_parents[i]; |
| 454 | struct filter_list_struct *lp = ex->u.mergelist; |
| 455 | |
| 456 | if (verbose > 2) { |
| 457 | rprintf(FINFO, "[%s] popping filter list%s\n", |
| 458 | who_am_i(), lp->debug_type); |
| 459 | } |
| 460 | |
| 461 | clear_filter_list(lp); |
| 462 | } |
| 463 | |
| 464 | if (!pop) |
| 465 | return; |
| 466 | |
| 467 | for (i = 0, ap = pop; i < mergelist_cnt; i++) { |
| 468 | memcpy(mergelist_parents[i]->u.mergelist, ap++, |
| 469 | sizeof (struct filter_list_struct)); |
| 470 | } |
| 471 | |
| 472 | free(pop); |
| 473 | } |
| 474 | |
| 475 | static int rule_matches(char *name, struct filter_struct *ex, int name_is_dir) |
| 476 | { |
| 477 | char *p, full_name[MAXPATHLEN]; |
| 478 | int match_start = 0; |
| 479 | char *pattern = ex->pattern; |
| 480 | |
| 481 | if (!*name) |
| 482 | return 0; |
| 483 | |
| 484 | /* If the pattern does not have any slashes AND it does not have |
| 485 | * a "**" (which could match a slash), then we just match the |
| 486 | * name portion of the path. */ |
| 487 | if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) { |
| 488 | if ((p = strrchr(name,'/')) != NULL) |
| 489 | name = p+1; |
| 490 | } |
| 491 | else if (ex->match_flags & MATCHFLG_ABS_PATH && *name != '/' |
| 492 | && curr_dir_len > module_dirlen + 1) { |
| 493 | pathjoin(full_name, sizeof full_name, |
| 494 | curr_dir + module_dirlen + 1, name); |
| 495 | name = full_name; |
| 496 | } |
| 497 | |
| 498 | if (ex->match_flags & MATCHFLG_DIRECTORY && !name_is_dir) |
| 499 | return 0; |
| 500 | |
| 501 | if (*pattern == '/') { |
| 502 | match_start = 1; |
| 503 | pattern++; |
| 504 | if (*name == '/') |
| 505 | name++; |
| 506 | } |
| 507 | |
| 508 | if (ex->match_flags & MATCHFLG_WILD) { |
| 509 | /* A non-anchored match with an infix slash and no "**" |
| 510 | * needs to match the last slash_cnt+1 name elements. */ |
| 511 | if (!match_start && ex->u.slash_cnt |
| 512 | && !(ex->match_flags & MATCHFLG_WILD2)) { |
| 513 | int cnt = ex->u.slash_cnt + 1; |
| 514 | for (p = name + strlen(name) - 1; p >= name; p--) { |
| 515 | if (*p == '/' && !--cnt) |
| 516 | break; |
| 517 | } |
| 518 | name = p+1; |
| 519 | } |
| 520 | if (wildmatch(pattern, name)) |
| 521 | return 1; |
| 522 | if (ex->match_flags & MATCHFLG_WILD2_PREFIX) { |
| 523 | /* If the **-prefixed pattern has a '/' as the next |
| 524 | * character, then try to match the rest of the |
| 525 | * pattern at the root. */ |
| 526 | if (pattern[2] == '/' && wildmatch(pattern+3, name)) |
| 527 | return 1; |
| 528 | } |
| 529 | else if (!match_start && ex->match_flags & MATCHFLG_WILD2) { |
| 530 | /* A non-anchored match with an infix or trailing "**" |
| 531 | * (but not a prefixed "**") needs to try matching |
| 532 | * after every slash. */ |
| 533 | while ((name = strchr(name, '/')) != NULL) { |
| 534 | name++; |
| 535 | if (wildmatch(pattern, name)) |
| 536 | return 1; |
| 537 | } |
| 538 | } |
| 539 | } else if (match_start) { |
| 540 | if (strcmp(name,pattern) == 0) |
| 541 | return 1; |
| 542 | } else { |
| 543 | int l1 = strlen(name); |
| 544 | int l2 = strlen(pattern); |
| 545 | if (l2 <= l1 && |
| 546 | strcmp(name+(l1-l2),pattern) == 0 && |
| 547 | (l1==l2 || name[l1-(l2+1)] == '/')) { |
| 548 | return 1; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | static void report_filter_result(char const *name, |
| 557 | struct filter_struct const *ent, |
| 558 | int name_is_dir, const char *type) |
| 559 | { |
| 560 | /* If a trailing slash is present to match only directories, |
| 561 | * then it is stripped out by filter_rule. So as a special |
| 562 | * case we add it back in here. */ |
| 563 | |
| 564 | if (verbose >= 2) { |
| 565 | rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s%s\n", |
| 566 | who_am_i(), |
| 567 | ent->match_flags & MATCHFLG_INCLUDE ? "in" : "ex", |
| 568 | name_is_dir ? "directory" : "file", name, ent->pattern, |
| 569 | ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | * Return -1 if file "name" is defined to be excluded by the specified |
| 576 | * exclude list, 1 if it is included, and 0 if it was not matched. |
| 577 | */ |
| 578 | int check_filter(struct filter_list_struct *listp, char *name, int name_is_dir) |
| 579 | { |
| 580 | struct filter_struct *ent; |
| 581 | |
| 582 | for (ent = listp->head; ent; ent = ent->next) { |
| 583 | if (ent->match_flags & MATCHFLG_PERDIR_MERGE) { |
| 584 | int rc = check_filter(ent->u.mergelist, name, |
| 585 | name_is_dir); |
| 586 | if (rc) |
| 587 | return rc; |
| 588 | continue; |
| 589 | } |
| 590 | if (rule_matches(name, ent, name_is_dir)) { |
| 591 | report_filter_result(name, ent, name_is_dir, |
| 592 | listp->debug_type); |
| 593 | return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /* Get the next include/exclude arg from the string. The token will not |
| 602 | * be '\0' terminated, so use the returned length to limit the string. |
| 603 | * Also, be sure to add this length to the returned pointer before passing |
| 604 | * it back to ask for the next token. This routine parses the "!" (list- |
| 605 | * clearing) token and (if xflags does NOT contain XFLG_NO_PREFIXES) the |
| 606 | * +/- prefixes for overriding the include/exclude mode. The *flag_ptr |
| 607 | * value will also be set to the MATCHFLG_* bits for the current token. |
| 608 | */ |
| 609 | static const char *get_filter_tok(const char *p, int xflags, |
| 610 | unsigned int *len_ptr, unsigned int *flag_ptr) |
| 611 | { |
| 612 | const unsigned char *s = (const unsigned char *)p; |
| 613 | unsigned int len, mflags = 0; |
| 614 | int empty_pat_is_OK = 0; |
| 615 | |
| 616 | if (xflags & XFLG_WORD_SPLIT) { |
| 617 | /* Skip over any initial whitespace. */ |
| 618 | while (isspace(*s)) |
| 619 | s++; |
| 620 | /* Update to point to real start of rule. */ |
| 621 | p = (const char *)s; |
| 622 | } |
| 623 | if (!*s) |
| 624 | return NULL; |
| 625 | |
| 626 | /* Figure out what kind of a filter rule "s" is pointing at. */ |
| 627 | if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE))) { |
| 628 | char *mods = ""; |
| 629 | switch (*s) { |
| 630 | case ':': |
| 631 | mflags |= MATCHFLG_PERDIR_MERGE |
| 632 | | MATCHFLG_FINISH_SETUP; |
| 633 | /* FALL THROUGH */ |
| 634 | case '.': |
| 635 | mflags |= MATCHFLG_MERGE_FILE; |
| 636 | mods = "-+Cens"; |
| 637 | break; |
| 638 | case '+': |
| 639 | mflags |= MATCHFLG_INCLUDE; |
| 640 | /* FALL THROUGH */ |
| 641 | case '-': |
| 642 | mods = "/"; |
| 643 | break; |
| 644 | case '!': |
| 645 | mflags |= MATCHFLG_CLEAR_LIST; |
| 646 | mods = NULL; |
| 647 | break; |
| 648 | default: |
| 649 | rprintf(FERROR, "Unknown filter rule: %s\n", p); |
| 650 | exit_cleanup(RERR_SYNTAX); |
| 651 | } |
| 652 | while (mods && *++s && *s != ' ' && *s != '_') { |
| 653 | if (strchr(mods, *s) == NULL) { |
| 654 | if (xflags & XFLG_WORD_SPLIT && isspace(*s)) { |
| 655 | s--; |
| 656 | break; |
| 657 | } |
| 658 | rprintf(FERROR, |
| 659 | "unknown modifier '%c' in filter rule: %s\n", |
| 660 | *s, p); |
| 661 | exit_cleanup(RERR_SYNTAX); |
| 662 | } |
| 663 | switch (*s) { |
| 664 | case '-': |
| 665 | mflags |= MATCHFLG_NO_PREFIXES; |
| 666 | break; |
| 667 | case '+': |
| 668 | mflags |= MATCHFLG_NO_PREFIXES |
| 669 | | MATCHFLG_INCLUDE; |
| 670 | break; |
| 671 | case '/': |
| 672 | mflags |= MATCHFLG_ABS_PATH; |
| 673 | break; |
| 674 | case 'C': |
| 675 | empty_pat_is_OK = 1; |
| 676 | mflags |= MATCHFLG_NO_PREFIXES |
| 677 | | MATCHFLG_WORD_SPLIT |
| 678 | | MATCHFLG_NO_INHERIT; |
| 679 | break; |
| 680 | case 'e': |
| 681 | mflags |= MATCHFLG_EXCLUDE_SELF; |
| 682 | break; |
| 683 | case 'n': |
| 684 | mflags |= MATCHFLG_NO_INHERIT; |
| 685 | break; |
| 686 | case 's': |
| 687 | mflags |= MATCHFLG_WORD_SPLIT; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | if (*s) |
| 692 | s++; |
| 693 | } else if (!(xflags & XFLG_NO_PREFIXES) |
| 694 | && (*s == '-' || *s == '+') && s[1] == ' ') { |
| 695 | if (*s == '+') |
| 696 | mflags |= MATCHFLG_INCLUDE; |
| 697 | s += 2; |
| 698 | } else { |
| 699 | if (xflags & XFLG_DEF_INCLUDE) |
| 700 | mflags |= MATCHFLG_INCLUDE; |
| 701 | if (*s == '!') |
| 702 | mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */ |
| 703 | } |
| 704 | |
| 705 | if (xflags & XFLG_DIRECTORY) |
| 706 | mflags |= MATCHFLG_DIRECTORY; |
| 707 | |
| 708 | if (xflags & XFLG_WORD_SPLIT) { |
| 709 | const unsigned char *cp = s; |
| 710 | /* Token ends at whitespace or the end of the string. */ |
| 711 | while (!isspace(*cp) && *cp != '\0') |
| 712 | cp++; |
| 713 | len = cp - s; |
| 714 | } else |
| 715 | len = strlen((char*)s); |
| 716 | |
| 717 | if (mflags & MATCHFLG_CLEAR_LIST) { |
| 718 | if (!(xflags & (XFLG_DEF_INCLUDE | XFLG_DEF_EXCLUDE)) && len) { |
| 719 | rprintf(FERROR, |
| 720 | "'!' rule has trailing characters: %s\n", p); |
| 721 | exit_cleanup(RERR_SYNTAX); |
| 722 | } |
| 723 | if (len > 1) |
| 724 | mflags &= ~MATCHFLG_CLEAR_LIST; |
| 725 | } else if (!len && !empty_pat_is_OK) { |
| 726 | rprintf(FERROR, "unexpected end of filter rule: %s\n", p); |
| 727 | exit_cleanup(RERR_SYNTAX); |
| 728 | } |
| 729 | |
| 730 | *len_ptr = len; |
| 731 | *flag_ptr = mflags; |
| 732 | return (const char *)s; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | void add_filter(struct filter_list_struct *listp, const char *pattern, |
| 737 | int xflags) |
| 738 | { |
| 739 | unsigned int pat_len, mflags; |
| 740 | const char *cp, *p; |
| 741 | |
| 742 | if (!pattern) |
| 743 | return; |
| 744 | |
| 745 | while (1) { |
| 746 | /* Remember that the returned string is NOT '\0' terminated! */ |
| 747 | cp = get_filter_tok(pattern, xflags, &pat_len, &mflags); |
| 748 | if (!cp) |
| 749 | break; |
| 750 | if (pat_len >= MAXPATHLEN) { |
| 751 | rprintf(FERROR, "discarding over-long filter: %s\n", |
| 752 | cp); |
| 753 | continue; |
| 754 | } |
| 755 | pattern = cp + pat_len; |
| 756 | |
| 757 | if (mflags & MATCHFLG_CLEAR_LIST) { |
| 758 | if (verbose > 2) { |
| 759 | rprintf(FINFO, |
| 760 | "[%s] clearing filter list%s\n", |
| 761 | who_am_i(), listp->debug_type); |
| 762 | } |
| 763 | clear_filter_list(listp); |
| 764 | continue; |
| 765 | } |
| 766 | |
| 767 | if (!pat_len) { |
| 768 | cp = ".cvsignore"; |
| 769 | pat_len = 10; |
| 770 | } |
| 771 | |
| 772 | if (mflags & MATCHFLG_MERGE_FILE) { |
| 773 | unsigned int len = pat_len; |
| 774 | if (mflags & MATCHFLG_EXCLUDE_SELF) { |
| 775 | const char *name = strrchr(cp, '/'); |
| 776 | if (name) |
| 777 | len -= ++name - cp; |
| 778 | else |
| 779 | name = cp; |
| 780 | filter_rule(listp, name, len, 0, 0); |
| 781 | mflags &= ~MATCHFLG_EXCLUDE_SELF; |
| 782 | len = pat_len; |
| 783 | } |
| 784 | if (mflags & MATCHFLG_PERDIR_MERGE) { |
| 785 | if (parent_dirscan) { |
| 786 | if (!(p = parse_merge_name(cp, &len, module_dirlen))) |
| 787 | continue; |
| 788 | filter_rule(listp, p, len, mflags, 0); |
| 789 | continue; |
| 790 | } |
| 791 | } else { |
| 792 | int flgs = XFLG_FATAL_ERRORS; |
| 793 | if (!(p = parse_merge_name(cp, &len, 0))) |
| 794 | continue; |
| 795 | if (mflags & MATCHFLG_INCLUDE) |
| 796 | flgs |= XFLG_DEF_INCLUDE; |
| 797 | else if (mflags & MATCHFLG_NO_PREFIXES) |
| 798 | flgs |= XFLG_DEF_EXCLUDE; |
| 799 | add_filter_file(listp, p, flgs); |
| 800 | continue; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | filter_rule(listp, cp, pat_len, mflags, xflags); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
| 809 | void add_filter_file(struct filter_list_struct *listp, const char *fname, |
| 810 | int xflags) |
| 811 | { |
| 812 | FILE *fp; |
| 813 | char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */ |
| 814 | char *eob = line + sizeof line - 1; |
| 815 | int word_split = xflags & XFLG_WORD_SPLIT; |
| 816 | |
| 817 | if (!fname || !*fname) |
| 818 | return; |
| 819 | |
| 820 | if (*fname != '-' || fname[1] || am_server) { |
| 821 | if (server_filter_list.head) { |
| 822 | strlcpy(line, fname, sizeof line); |
| 823 | clean_fname(line, 1); |
| 824 | if (check_filter(&server_filter_list, line, 0) < 0) |
| 825 | fp = NULL; |
| 826 | else |
| 827 | fp = fopen(line, "rb"); |
| 828 | } else |
| 829 | fp = fopen(fname, "rb"); |
| 830 | } else |
| 831 | fp = stdin; |
| 832 | |
| 833 | if (verbose > 2) { |
| 834 | rprintf(FINFO, "[%s] add_filter_file(%s,%d)%s\n", |
| 835 | who_am_i(), safe_fname(fname), xflags, |
| 836 | fp ? "" : " [not found]"); |
| 837 | } |
| 838 | |
| 839 | if (!fp) { |
| 840 | if (xflags & XFLG_FATAL_ERRORS) { |
| 841 | rsyserr(FERROR, errno, |
| 842 | "failed to open %sclude file %s", |
| 843 | xflags & XFLG_DEF_INCLUDE ? "in" : "ex", |
| 844 | safe_fname(fname)); |
| 845 | exit_cleanup(RERR_FILEIO); |
| 846 | } |
| 847 | return; |
| 848 | } |
| 849 | dirbuf[dirbuf_len] = '\0'; |
| 850 | |
| 851 | while (1) { |
| 852 | char *s = line; |
| 853 | int ch, overflow = 0; |
| 854 | while (1) { |
| 855 | if ((ch = getc(fp)) == EOF) { |
| 856 | if (ferror(fp) && errno == EINTR) |
| 857 | continue; |
| 858 | break; |
| 859 | } |
| 860 | if (word_split && isspace(ch)) |
| 861 | break; |
| 862 | if (eol_nulls? !ch : (ch == '\n' || ch == '\r')) |
| 863 | break; |
| 864 | if (s < eob) |
| 865 | *s++ = ch; |
| 866 | else |
| 867 | overflow = 1; |
| 868 | } |
| 869 | if (overflow) { |
| 870 | rprintf(FERROR, "discarding over-long filter: %s...\n", line); |
| 871 | s = line; |
| 872 | } |
| 873 | *s = '\0'; |
| 874 | /* Skip an empty token and (when line parsing) comments. */ |
| 875 | if (*line && (word_split || (*line != ';' && *line != '#'))) |
| 876 | add_filter(listp, line, xflags); |
| 877 | if (ch == EOF) |
| 878 | break; |
| 879 | } |
| 880 | fclose(fp); |
| 881 | } |
| 882 | |
| 883 | char *get_rule_prefix(int match_flags, const char *pat, unsigned int *plen_ptr) |
| 884 | { |
| 885 | static char buf[MAX_RULE_PREFIX+1]; |
| 886 | char *op = buf; |
| 887 | |
| 888 | if (match_flags & MATCHFLG_PERDIR_MERGE) { |
| 889 | *op++ = ':'; |
| 890 | if (match_flags & MATCHFLG_WORD_SPLIT) |
| 891 | *op++ = 's'; |
| 892 | if (match_flags & MATCHFLG_NO_INHERIT) |
| 893 | *op++ = 'n'; |
| 894 | if (match_flags & MATCHFLG_EXCLUDE_SELF) |
| 895 | *op++ = 'e'; |
| 896 | if (match_flags & MATCHFLG_NO_PREFIXES) { |
| 897 | if (match_flags & MATCHFLG_INCLUDE) |
| 898 | *op++ = '+'; |
| 899 | else |
| 900 | *op++ = '-'; |
| 901 | } |
| 902 | *op++ = ' '; |
| 903 | } else if (match_flags & MATCHFLG_INCLUDE) { |
| 904 | *op++ = '+'; |
| 905 | *op++ = ' '; |
| 906 | } else if (protocol_version >= 29 |
| 907 | || ((*pat == '-' || *pat == '+') && pat[1] == ' ')) { |
| 908 | *op++ = '-'; |
| 909 | *op++ = ' '; |
| 910 | } |
| 911 | *op = '\0'; |
| 912 | if (plen_ptr) |
| 913 | *plen_ptr = op - buf; |
| 914 | if (op - buf > MAX_RULE_PREFIX) |
| 915 | overflow("get_rule_prefix"); |
| 916 | return buf; |
| 917 | } |
| 918 | |
| 919 | void send_filter_list(int f) |
| 920 | { |
| 921 | struct filter_struct *ent; |
| 922 | |
| 923 | /* This is a complete hack - blame Rusty. FIXME! |
| 924 | * Remove this hack when older rsyncs (below 2.6.4) are gone. */ |
| 925 | if (list_only == 1 && !recurse) |
| 926 | add_filter(&filter_list, "/*/*", XFLG_DEF_EXCLUDE); |
| 927 | |
| 928 | for (ent = filter_list.head; ent; ent = ent->next) { |
| 929 | unsigned int len, plen, dlen; |
| 930 | char *p; |
| 931 | |
| 932 | len = strlen(ent->pattern); |
| 933 | if (len == 0 || len >= MAXPATHLEN) |
| 934 | continue; |
| 935 | p = get_rule_prefix(ent->match_flags, ent->pattern, &plen); |
| 936 | if (protocol_version < 29 && *p == ':') { |
| 937 | if (strcmp(p, ":sn- ") == 0 |
| 938 | && strcmp(ent->pattern, ".cvsignore") == 0) |
| 939 | continue; |
| 940 | rprintf(FERROR, |
| 941 | "remote rsync is too old to understand per-directory merge files.\n"); |
| 942 | exit_cleanup(RERR_SYNTAX); |
| 943 | } |
| 944 | dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0; |
| 945 | write_int(f, plen + len + dlen); |
| 946 | if (plen) |
| 947 | write_buf(f, p, plen); |
| 948 | write_buf(f, ent->pattern, len); |
| 949 | if (dlen) |
| 950 | write_byte(f, '/'); |
| 951 | } |
| 952 | |
| 953 | write_int(f, 0); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | void recv_filter_list(int f) |
| 958 | { |
| 959 | char line[MAXPATHLEN+MAX_RULE_PREFIX+1]; /* +1 for trailing slash. */ |
| 960 | unsigned int xflags = protocol_version >= 29 ? 0 : XFLG_DEF_EXCLUDE; |
| 961 | unsigned int l; |
| 962 | |
| 963 | while ((l = read_int(f)) != 0) { |
| 964 | if (l >= sizeof line) |
| 965 | overflow("recv_filter_list"); |
| 966 | read_sbuf(f, line, l); |
| 967 | add_filter(&filter_list, line, xflags); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | |
| 972 | static char default_cvsignore[] = |
| 973 | /* These default ignored items come from the CVS manual. */ |
| 974 | "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS" |
| 975 | " .make.state .nse_depinfo *~ #* .#* ,* _$* *$" |
| 976 | " *.old *.bak *.BAK *.orig *.rej .del-*" |
| 977 | " *.a *.olb *.o *.obj *.so *.exe" |
| 978 | " *.Z *.elc *.ln core" |
| 979 | /* The rest we added to suit ourself. */ |
| 980 | " .svn/"; |
| 981 | |
| 982 | void add_cvs_excludes(void) |
| 983 | { |
| 984 | static unsigned int cvs_flags = XFLG_WORD_SPLIT | XFLG_NO_PREFIXES |
| 985 | | XFLG_DEF_EXCLUDE; |
| 986 | char fname[MAXPATHLEN]; |
| 987 | char *p = module_id >= 0 && lp_use_chroot(module_id) |
| 988 | ? "/" : getenv("HOME"); |
| 989 | |
| 990 | add_filter(&filter_list, ":C", 0); |
| 991 | add_filter(&filter_list, default_cvsignore, cvs_flags); |
| 992 | |
| 993 | if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN) { |
| 994 | add_filter_file(&filter_list, fname, cvs_flags); |
| 995 | } |
| 996 | |
| 997 | add_filter(&filter_list, getenv("CVSIGNORE"), cvs_flags); |
| 998 | } |