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