| 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 | |
| 31 | struct exclude_struct **exclude_list; |
| 32 | struct exclude_struct **local_exclude_list; |
| 33 | struct exclude_struct **server_exclude_list; |
| 34 | char *exclude_path_prefix = NULL; |
| 35 | |
| 36 | /** Build an exclude structure given a exclude pattern */ |
| 37 | static struct exclude_struct *make_exclude(const char *pattern, int pat_len, |
| 38 | int include) |
| 39 | { |
| 40 | struct exclude_struct *ret; |
| 41 | const char *cp; |
| 42 | int ex_len; |
| 43 | |
| 44 | ret = new(struct exclude_struct); |
| 45 | if (!ret) |
| 46 | out_of_memory("make_exclude"); |
| 47 | |
| 48 | memset(ret, 0, sizeof ret[0]); |
| 49 | ret->include = include; |
| 50 | |
| 51 | if (exclude_path_prefix) |
| 52 | ret->match_flags |= MATCHFLG_ABS_PATH; |
| 53 | if (exclude_path_prefix && *pattern == '/') |
| 54 | ex_len = strlen(exclude_path_prefix); |
| 55 | else |
| 56 | ex_len = 0; |
| 57 | ret->pattern = new_array(char, ex_len + pat_len + 1); |
| 58 | if (!ret->pattern) |
| 59 | out_of_memory("make_exclude"); |
| 60 | if (ex_len) |
| 61 | memcpy(ret->pattern, exclude_path_prefix, ex_len); |
| 62 | strlcpy(ret->pattern + ex_len, pattern, pat_len + 1); |
| 63 | pat_len += ex_len; |
| 64 | |
| 65 | if (strpbrk(ret->pattern, "*[?")) { |
| 66 | ret->match_flags |= MATCHFLG_WILD; |
| 67 | if ((cp = strstr(ret->pattern, "**")) != NULL) { |
| 68 | ret->match_flags |= MATCHFLG_WILD2; |
| 69 | /* If the pattern starts with **, note that. */ |
| 70 | if (cp == ret->pattern) |
| 71 | ret->match_flags |= MATCHFLG_WILD2_PREFIX; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (pat_len > 1 && ret->pattern[pat_len-1] == '/') { |
| 76 | ret->pattern[pat_len-1] = 0; |
| 77 | ret->directory = 1; |
| 78 | } |
| 79 | |
| 80 | for (cp = ret->pattern; (cp = strchr(cp, '/')) != NULL; cp++) |
| 81 | ret->slash_cnt++; |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static void free_exclude(struct exclude_struct *ex) |
| 87 | { |
| 88 | free(ex->pattern); |
| 89 | memset(ex, 0, sizeof ex[0]); |
| 90 | free(ex); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void free_exclude_list(struct exclude_struct ***listp) |
| 95 | { |
| 96 | struct exclude_struct **list = *listp; |
| 97 | |
| 98 | if (verbose > 2) |
| 99 | rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i()); |
| 100 | |
| 101 | if (!list) |
| 102 | return; |
| 103 | |
| 104 | while (*list) |
| 105 | free_exclude(*list++); |
| 106 | |
| 107 | free(*listp); |
| 108 | *listp = NULL; |
| 109 | } |
| 110 | |
| 111 | static int check_one_exclude(char *name, struct exclude_struct *ex, |
| 112 | int name_is_dir) |
| 113 | { |
| 114 | char *p; |
| 115 | int match_start = 0; |
| 116 | char *pattern = ex->pattern; |
| 117 | |
| 118 | /* If the pattern does not have any slashes AND it does not have |
| 119 | * a "**" (which could match a slash), then we just match the |
| 120 | * name portion of the path. */ |
| 121 | if (!ex->slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) { |
| 122 | if ((p = strrchr(name,'/')) != NULL) |
| 123 | name = p+1; |
| 124 | } |
| 125 | else if ((ex->match_flags & MATCHFLG_ABS_PATH) && *name != '/') { |
| 126 | static char full_name[MAXPATHLEN]; |
| 127 | extern char curr_dir[]; |
| 128 | int plus = curr_dir[1] == '\0'? 1 : 0; |
| 129 | pathjoin(full_name, sizeof full_name, curr_dir+plus, name); |
| 130 | name = full_name; |
| 131 | } |
| 132 | |
| 133 | if (!name[0]) return 0; |
| 134 | |
| 135 | if (ex->directory && !name_is_dir) return 0; |
| 136 | |
| 137 | if (*pattern == '/') { |
| 138 | match_start = 1; |
| 139 | pattern++; |
| 140 | if (*name == '/') |
| 141 | name++; |
| 142 | } |
| 143 | |
| 144 | if (ex->match_flags & MATCHFLG_WILD) { |
| 145 | /* A non-anchored match with an infix slash and no "**" |
| 146 | * needs to match the last slash_cnt+1 name elements. */ |
| 147 | if (!match_start && ex->slash_cnt && |
| 148 | !(ex->match_flags & MATCHFLG_WILD2)) { |
| 149 | int cnt = ex->slash_cnt + 1; |
| 150 | for (p = name + strlen(name) - 1; p >= name; p--) { |
| 151 | if (*p == '/' && !--cnt) |
| 152 | break; |
| 153 | } |
| 154 | name = p+1; |
| 155 | } |
| 156 | if (wildmatch(pattern, name)) |
| 157 | return 1; |
| 158 | if (ex->match_flags & MATCHFLG_WILD2_PREFIX) { |
| 159 | /* If the **-prefixed pattern has a '/' as the next |
| 160 | * character, then try to match the rest of the |
| 161 | * pattern at the root. */ |
| 162 | if (pattern[2] == '/' && wildmatch(pattern+3, name)) |
| 163 | return 1; |
| 164 | } |
| 165 | else if (!match_start && ex->match_flags & MATCHFLG_WILD2) { |
| 166 | /* A non-anchored match with an infix or trailing "**" |
| 167 | * (but not a prefixed "**") needs to try matching |
| 168 | * after every slash. */ |
| 169 | while ((name = strchr(name, '/')) != NULL) { |
| 170 | name++; |
| 171 | if (wildmatch(pattern, name)) |
| 172 | return 1; |
| 173 | } |
| 174 | } |
| 175 | } else if (match_start) { |
| 176 | if (strcmp(name,pattern) == 0) |
| 177 | return 1; |
| 178 | } else { |
| 179 | int l1 = strlen(name); |
| 180 | int l2 = strlen(pattern); |
| 181 | if (l2 <= l1 && |
| 182 | strcmp(name+(l1-l2),pattern) == 0 && |
| 183 | (l1==l2 || name[l1-(l2+1)] == '/')) { |
| 184 | return 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static void report_exclude_result(char const *name, |
| 193 | struct exclude_struct const *ent, |
| 194 | int name_is_dir) |
| 195 | { |
| 196 | /* If a trailing slash is present to match only directories, |
| 197 | * then it is stripped out by make_exclude. So as a special |
| 198 | * case we add it back in here. */ |
| 199 | |
| 200 | if (verbose >= 2) { |
| 201 | rprintf(FINFO, "[%s] %scluding %s %s because of pattern %s%s\n", |
| 202 | who_am_i(), ent->include ? "in" : "ex", |
| 203 | name_is_dir ? "directory" : "file", name, ent->pattern, |
| 204 | ent->directory ? "/" : ""); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /* |
| 210 | * Return true if file NAME is defined to be excluded by either |
| 211 | * LOCAL_EXCLUDE_LIST or the globals EXCLUDE_LIST. |
| 212 | */ |
| 213 | int check_exclude(struct exclude_struct **list, char *name, int name_is_dir) |
| 214 | { |
| 215 | struct exclude_struct *ent; |
| 216 | |
| 217 | while ((ent = *list++) != NULL) { |
| 218 | if (check_one_exclude(name, ent, name_is_dir)) { |
| 219 | report_exclude_result(name, ent, name_is_dir); |
| 220 | return !ent->include; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* Get the next include/exclude arg from the string. The token will not |
| 229 | * be '\0' terminated, so use the returned length to limit the string. |
| 230 | * Also, be sure to add this length to the returned pointer before passing |
| 231 | * it back to ask for the next token. This routine will not split off a |
| 232 | * prefix of "+ " or "- " unless xflags contains XFLG_NO_PREFIXES. The |
| 233 | * *incl_ptr value will be 1 for an include, 0 for an exclude, and -1 for |
| 234 | * the list-clearing "!" token. |
| 235 | */ |
| 236 | static const char *get_exclude_tok(const char *p, int *len_ptr, int *incl_ptr, |
| 237 | int xflags) |
| 238 | { |
| 239 | const unsigned char *s = (unsigned char *)p; |
| 240 | int len; |
| 241 | |
| 242 | if (xflags & XFLG_WORD_SPLIT) { |
| 243 | /* Skip over any initial whitespace. */ |
| 244 | while (isspace(*s)) |
| 245 | s++; |
| 246 | } |
| 247 | |
| 248 | /* Is this a '+' or '-' followed by a space (not whitespace)? */ |
| 249 | if (!(xflags & XFLG_NO_PREFIXES) |
| 250 | && (*s == '-' || *s == '+') && s[1] == ' ') { |
| 251 | *incl_ptr = *s == '+'; |
| 252 | s += 2; |
| 253 | } else |
| 254 | *incl_ptr = xflags & XFLG_DEF_INCLUDE; |
| 255 | |
| 256 | if (xflags & XFLG_WORD_SPLIT) { |
| 257 | const unsigned char *cp = s; |
| 258 | /* Token ends at whitespace or the end of the string. */ |
| 259 | while (!isspace(*cp) && *cp != '\0') |
| 260 | cp++; |
| 261 | len = cp - s; |
| 262 | } else |
| 263 | len = strlen(s); |
| 264 | |
| 265 | if (*s == '!' && len == 1 && !(xflags & XFLG_NO_PREFIXES) |
| 266 | && (const char *)s == p) |
| 267 | *incl_ptr = -1; |
| 268 | |
| 269 | *len_ptr = len; |
| 270 | return (const char *)s; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void add_exclude(struct exclude_struct ***listp, const char *pattern, int xflags) |
| 275 | { |
| 276 | struct exclude_struct **list = *listp; |
| 277 | int pat_len, list_len = 0; |
| 278 | int incl, add_cnt = 1; |
| 279 | const char *cp; |
| 280 | |
| 281 | if (!pattern) |
| 282 | return; |
| 283 | |
| 284 | if (xflags & XFLG_WORD_SPLIT) { |
| 285 | int add = 0; |
| 286 | /* Count maximum extra tokens we might encounter. */ |
| 287 | for (cp = pattern; *cp; cp++) { |
| 288 | if (isspace(*(unsigned char *)cp)) { |
| 289 | add_cnt += add; |
| 290 | add = 0; |
| 291 | } else |
| 292 | add = 1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | cp = get_exclude_tok(pattern, &pat_len, &incl, xflags); |
| 297 | if (!pat_len) |
| 298 | return; |
| 299 | |
| 300 | /* Check for the special "!" token that clears the list. Yes, we |
| 301 | * only honor it at the start of a XFLG_WORD_SPLIT string. */ |
| 302 | if (incl < 0) { |
| 303 | free_exclude_list(listp); |
| 304 | if (!--add_cnt) |
| 305 | return; |
| 306 | cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags); |
| 307 | if (!pat_len) |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | if (list) |
| 312 | for ( ; list[list_len]; list_len++) {} |
| 313 | |
| 314 | list = *listp = realloc_array(list, struct exclude_struct *, |
| 315 | list_len + add_cnt + 1); |
| 316 | if (!list) |
| 317 | out_of_memory("add_exclude"); |
| 318 | |
| 319 | while (pat_len) { |
| 320 | list[list_len++] = make_exclude(cp, pat_len, incl); |
| 321 | |
| 322 | if (verbose > 2) { |
| 323 | rprintf(FINFO, "[%s] add_exclude(%s,%s)\n", |
| 324 | who_am_i(), cp, |
| 325 | incl ? "include" : "exclude"); |
| 326 | } |
| 327 | cp = get_exclude_tok(cp + pat_len, &pat_len, &incl, xflags); |
| 328 | } |
| 329 | |
| 330 | list[list_len] = NULL; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void add_exclude_file(struct exclude_struct ***listp, const char *fname, |
| 335 | int xflags) |
| 336 | { |
| 337 | FILE *fp; |
| 338 | char line[MAXPATHLEN]; |
| 339 | char *eob = line + MAXPATHLEN - 1; |
| 340 | extern int eol_nulls; |
| 341 | |
| 342 | if (!fname || !*fname) |
| 343 | return; |
| 344 | |
| 345 | if (*fname != '-' || fname[1]) |
| 346 | fp = fopen(fname, "rb"); |
| 347 | else |
| 348 | fp = stdin; |
| 349 | if (!fp) { |
| 350 | if (xflags & XFLG_FATAL_ERRORS) { |
| 351 | rsyserr(FERROR, errno, |
| 352 | "failed to open %s file %s", |
| 353 | xflags & XFLG_DEF_INCLUDE ? "include" : "exclude", |
| 354 | fname); |
| 355 | exit_cleanup(RERR_FILEIO); |
| 356 | } |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | while (1) { |
| 361 | char *s = line; |
| 362 | int ch; |
| 363 | while (1) { |
| 364 | if ((ch = getc(fp)) == EOF) { |
| 365 | if (ferror(fp) && errno == EINTR) |
| 366 | continue; |
| 367 | break; |
| 368 | } |
| 369 | if (eol_nulls? !ch : (ch == '\n' || ch == '\r')) |
| 370 | break; |
| 371 | if (s < eob) |
| 372 | *s++ = ch; |
| 373 | } |
| 374 | *s = '\0'; |
| 375 | if (*line && *line != ';' && *line != '#') { |
| 376 | /* Skip lines starting with semicolon or pound. |
| 377 | * It probably wouldn't cause any harm to not skip |
| 378 | * them but there's no need to save them. */ |
| 379 | add_exclude(listp, line, xflags); |
| 380 | } |
| 381 | if (ch == EOF) |
| 382 | break; |
| 383 | } |
| 384 | fclose(fp); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | void send_exclude_list(int f) |
| 389 | { |
| 390 | int i; |
| 391 | extern int list_only, recurse; |
| 392 | |
| 393 | /* This is a complete hack - blame Rusty. |
| 394 | * |
| 395 | * FIXME: This pattern shows up in the output of |
| 396 | * report_exclude_result(), which is not ideal. */ |
| 397 | if (list_only && !recurse) |
| 398 | add_exclude(&exclude_list, "/*/*", 0); |
| 399 | |
| 400 | if (!exclude_list) { |
| 401 | write_int(f, 0); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | for (i = 0; exclude_list[i]; i++) { |
| 406 | unsigned int l; |
| 407 | char p[MAXPATHLEN+1]; |
| 408 | |
| 409 | l = strlcpy(p, exclude_list[i]->pattern, sizeof p); |
| 410 | if (l == 0 || l >= MAXPATHLEN) |
| 411 | continue; |
| 412 | if (exclude_list[i]->directory) { |
| 413 | p[l++] = '/'; |
| 414 | p[l] = '\0'; |
| 415 | } |
| 416 | |
| 417 | if (exclude_list[i]->include) { |
| 418 | write_int(f, l + 2); |
| 419 | write_buf(f, "+ ", 2); |
| 420 | } else if ((*p == '-' || *p == '+') && p[1] == ' ') { |
| 421 | write_int(f, l + 2); |
| 422 | write_buf(f, "- ", 2); |
| 423 | } else |
| 424 | write_int(f, l); |
| 425 | write_buf(f, p, l); |
| 426 | } |
| 427 | |
| 428 | write_int(f, 0); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void recv_exclude_list(int f) |
| 433 | { |
| 434 | char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */ |
| 435 | unsigned int l; |
| 436 | |
| 437 | while ((l = read_int(f)) != 0) { |
| 438 | if (l >= sizeof line) |
| 439 | overflow("recv_exclude_list"); |
| 440 | read_sbuf(f, line, l); |
| 441 | add_exclude(&exclude_list, line, 0); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
| 446 | static char default_cvsignore[] = |
| 447 | /* These default ignored items come from the CVS manual. */ |
| 448 | "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS" |
| 449 | " .make.state .nse_depinfo *~ #* .#* ,* _$* *$" |
| 450 | " *.old *.bak *.BAK *.orig *.rej .del-*" |
| 451 | " *.a *.olb *.o *.obj *.so *.exe" |
| 452 | " *.Z *.elc *.ln core" |
| 453 | /* The rest we added to suit ourself. */ |
| 454 | " .svn/"; |
| 455 | |
| 456 | void add_cvs_excludes(void) |
| 457 | { |
| 458 | char fname[MAXPATHLEN]; |
| 459 | char *p; |
| 460 | |
| 461 | add_exclude(&exclude_list, default_cvsignore, |
| 462 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); |
| 463 | |
| 464 | if ((p = getenv("HOME")) |
| 465 | && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname) { |
| 466 | add_exclude_file(&exclude_list, fname, |
| 467 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); |
| 468 | } |
| 469 | |
| 470 | add_exclude(&exclude_list, getenv("CVSIGNORE"), |
| 471 | XFLG_WORD_SPLIT | XFLG_NO_PREFIXES); |
| 472 | } |