exclude.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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
  7. * Copyright (C) 2003-2009 Wayne Davison
  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. #include "rsync.h"
  23. extern int verbose;
  24. extern int am_server;
  25. extern int am_sender;
  26. extern int eol_nulls;
  27. extern int io_error;
  28. extern int local_server;
  29. extern int prune_empty_dirs;
  30. extern int ignore_perishable;
  31. extern int delete_mode;
  32. extern int delete_excluded;
  33. extern int cvs_exclude;
  34. extern int sanitize_paths;
  35. extern int protocol_version;
  36. extern int module_id;
  37. extern char curr_dir[MAXPATHLEN];
  38. extern unsigned int curr_dir_len;
  39. extern unsigned int module_dirlen;
  40. struct filter_list_struct filter_list = { 0, 0, "" };
  41. struct filter_list_struct cvs_filter_list = { 0, 0, " [global CVS]" };
  42. struct filter_list_struct daemon_filter_list = { 0, 0, " [daemon]" };
  43. /* Need room enough for ":MODS " prefix plus some room to grow. */
  44. #define MAX_RULE_PREFIX (16)
  45. #define MODIFIERS_MERGE_FILE "-+Cenw"
  46. #define MODIFIERS_INCL_EXCL "/!Crsp"
  47. #define MODIFIERS_HIDE_PROTECT "/!p"
  48. #define SLASH_WILD3_SUFFIX "/***"
  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. /* This is True when we're scanning parent dirs for per-dir merge-files. */
  57. static BOOL parent_dirscan = False;
  58. /* This array contains a list of all the currently active per-dir merge
  59. * files. This makes it easier to save the appropriate values when we
  60. * "push" down into each subdirectory. */
  61. static struct filter_struct **mergelist_parents;
  62. static int mergelist_cnt = 0;
  63. static int mergelist_size = 0;
  64. /* Each filter_list_struct describes a singly-linked list by keeping track
  65. * of both the head and tail pointers. The list is slightly unusual in that
  66. * a parent-dir's content can be appended to the end of the local list in a
  67. * special way: the last item in the local list has its "next" pointer set
  68. * to point to the inherited list, but the local list's tail pointer points
  69. * at the end of the local list. Thus, if the local list is empty, the head
  70. * will be pointing at the inherited content but the tail will be NULL. To
  71. * help you visualize this, here are the possible list arrangements:
  72. *
  73. * Completely Empty Local Content Only
  74. * ================================== ====================================
  75. * head -> NULL head -> Local1 -> Local2 -> NULL
  76. * tail -> NULL tail -------------^
  77. *
  78. * Inherited Content Only Both Local and Inherited Content
  79. * ================================== ====================================
  80. * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
  81. * tail -> NULL tail ---------^
  82. *
  83. * This means that anyone wanting to traverse the whole list to use it just
  84. * needs to start at the head and use the "next" pointers until it goes
  85. * NULL. To add new local content, we insert the item after the tail item
  86. * and update the tail (obviously, if "tail" was NULL, we insert it at the
  87. * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
  88. * because it is shared between the current list and our parent list(s).
  89. * The easiest way to handle this is to simply truncate the list after the
  90. * tail item and then free the local list from the head. When inheriting
  91. * the list for a new local dir, we just save off the filter_list_struct
  92. * values (so we can pop back to them later) and set the tail to NULL.
  93. */
  94. static void free_filter(struct filter_struct *ex)
  95. {
  96. if (ex->match_flags & MATCHFLG_PERDIR_MERGE) {
  97. free(ex->u.mergelist->debug_type);
  98. free(ex->u.mergelist);
  99. mergelist_cnt--;
  100. }
  101. free(ex->pattern);
  102. free(ex);
  103. }
  104. /* Build a filter structure given a filter pattern. The value in "pat"
  105. * is not null-terminated. */
  106. static void add_rule(struct filter_list_struct *listp, const char *pat,
  107. unsigned int pat_len, uint32 mflags, int xflags)
  108. {
  109. struct filter_struct *ret;
  110. const char *cp;
  111. unsigned int pre_len, suf_len, slash_cnt = 0;
  112. if (verbose > 2) {
  113. rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
  114. who_am_i(), get_rule_prefix(mflags, pat, 0, NULL),
  115. (int)pat_len, pat,
  116. (mflags & MATCHFLG_DIRECTORY) ? "/" : "",
  117. listp->debug_type);
  118. }
  119. /* These flags also indicate that we're reading a list that
  120. * needs to be filtered now, not post-filtered later. */
  121. if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)) {
  122. uint32 mf = mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE);
  123. if (am_sender) {
  124. if (mf == MATCHFLG_RECEIVER_SIDE)
  125. return;
  126. } else {
  127. if (mf == MATCHFLG_SENDER_SIDE)
  128. return;
  129. }
  130. }
  131. if (!(ret = new0(struct filter_struct)))
  132. out_of_memory("add_rule");
  133. if (pat_len > 1 && pat[pat_len-1] == '/') {
  134. pat_len--;
  135. mflags |= MATCHFLG_DIRECTORY;
  136. }
  137. for (cp = pat; cp < pat + pat_len; cp++) {
  138. if (*cp == '/')
  139. slash_cnt++;
  140. }
  141. if (!(mflags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE))
  142. && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
  143. || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
  144. mflags |= MATCHFLG_ABS_PATH;
  145. if (*pat == '/')
  146. pre_len = dirbuf_len - module_dirlen - 1;
  147. else
  148. pre_len = 0;
  149. } else
  150. pre_len = 0;
  151. /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
  152. if (xflags & XFLG_DIR2WILD3
  153. && BITS_SETnUNSET(mflags, MATCHFLG_DIRECTORY, MATCHFLG_INCLUDE)) {
  154. mflags &= ~MATCHFLG_DIRECTORY;
  155. suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
  156. } else
  157. suf_len = 0;
  158. if (!(ret->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
  159. out_of_memory("add_rule");
  160. if (pre_len) {
  161. memcpy(ret->pattern, dirbuf + module_dirlen, pre_len);
  162. for (cp = ret->pattern; cp < ret->pattern + pre_len; cp++) {
  163. if (*cp == '/')
  164. slash_cnt++;
  165. }
  166. }
  167. strlcpy(ret->pattern + pre_len, pat, pat_len + 1);
  168. pat_len += pre_len;
  169. if (suf_len) {
  170. memcpy(ret->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
  171. pat_len += suf_len;
  172. slash_cnt++;
  173. }
  174. if (strpbrk(ret->pattern, "*[?")) {
  175. mflags |= MATCHFLG_WILD;
  176. if ((cp = strstr(ret->pattern, "**")) != NULL) {
  177. mflags |= MATCHFLG_WILD2;
  178. /* If the pattern starts with **, note that. */
  179. if (cp == ret->pattern)
  180. mflags |= MATCHFLG_WILD2_PREFIX;
  181. /* If the pattern ends with ***, note that. */
  182. if (pat_len >= 3
  183. && ret->pattern[pat_len-3] == '*'
  184. && ret->pattern[pat_len-2] == '*'
  185. && ret->pattern[pat_len-1] == '*')
  186. mflags |= MATCHFLG_WILD3_SUFFIX;
  187. }
  188. }
  189. if (mflags & MATCHFLG_PERDIR_MERGE) {
  190. struct filter_list_struct *lp;
  191. unsigned int len;
  192. int i;
  193. if ((cp = strrchr(ret->pattern, '/')) != NULL)
  194. cp++;
  195. else
  196. cp = ret->pattern;
  197. /* If the local merge file was already mentioned, don't
  198. * add it again. */
  199. for (i = 0; i < mergelist_cnt; i++) {
  200. struct filter_struct *ex = mergelist_parents[i];
  201. const char *s = strrchr(ex->pattern, '/');
  202. if (s)
  203. s++;
  204. else
  205. s = ex->pattern;
  206. len = strlen(s);
  207. if (len == pat_len - (cp - ret->pattern)
  208. && memcmp(s, cp, len) == 0) {
  209. free_filter(ret);
  210. return;
  211. }
  212. }
  213. if (!(lp = new_array(struct filter_list_struct, 1)))
  214. out_of_memory("add_rule");
  215. lp->head = lp->tail = NULL;
  216. if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
  217. out_of_memory("add_rule");
  218. ret->u.mergelist = lp;
  219. if (mergelist_cnt == mergelist_size) {
  220. mergelist_size += 5;
  221. mergelist_parents = realloc_array(mergelist_parents,
  222. struct filter_struct *,
  223. mergelist_size);
  224. if (!mergelist_parents)
  225. out_of_memory("add_rule");
  226. }
  227. mergelist_parents[mergelist_cnt++] = ret;
  228. } else
  229. ret->u.slash_cnt = slash_cnt;
  230. ret->match_flags = mflags;
  231. if (!listp->tail) {
  232. ret->next = listp->head;
  233. listp->head = listp->tail = ret;
  234. } else {
  235. ret->next = listp->tail->next;
  236. listp->tail->next = ret;
  237. listp->tail = ret;
  238. }
  239. }
  240. static void clear_filter_list(struct filter_list_struct *listp)
  241. {
  242. if (listp->tail) {
  243. struct filter_struct *ent, *next;
  244. /* Truncate any inherited items from the local list. */
  245. listp->tail->next = NULL;
  246. /* Now free everything that is left. */
  247. for (ent = listp->head; ent; ent = next) {
  248. next = ent->next;
  249. free_filter(ent);
  250. }
  251. }
  252. listp->head = listp->tail = NULL;
  253. }
  254. /* This returns an expanded (absolute) filename for the merge-file name if
  255. * the name has any slashes in it OR if the parent_dirscan var is True;
  256. * otherwise it returns the original merge_file name. If the len_ptr value
  257. * is non-NULL the merge_file name is limited by the referenced length
  258. * value and will be updated with the length of the resulting name. We
  259. * always return a name that is null terminated, even if the merge_file
  260. * name was not. */
  261. static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
  262. unsigned int prefix_skip)
  263. {
  264. static char buf[MAXPATHLEN];
  265. char *fn, tmpbuf[MAXPATHLEN];
  266. unsigned int fn_len;
  267. if (!parent_dirscan && *merge_file != '/') {
  268. /* Return the name unchanged it doesn't have any slashes. */
  269. if (len_ptr) {
  270. const char *p = merge_file + *len_ptr;
  271. while (--p > merge_file && *p != '/') {}
  272. if (p == merge_file) {
  273. strlcpy(buf, merge_file, *len_ptr + 1);
  274. return buf;
  275. }
  276. } else if (strchr(merge_file, '/') == NULL)
  277. return (char *)merge_file;
  278. }
  279. fn = *merge_file == '/' ? buf : tmpbuf;
  280. if (sanitize_paths) {
  281. const char *r = prefix_skip ? "/" : NULL;
  282. /* null-terminate the name if it isn't already */
  283. if (len_ptr && merge_file[*len_ptr]) {
  284. char *to = fn == buf ? tmpbuf : buf;
  285. strlcpy(to, merge_file, *len_ptr + 1);
  286. merge_file = to;
  287. }
  288. if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
  289. rprintf(FERROR, "merge-file name overflows: %s\n",
  290. merge_file);
  291. return NULL;
  292. }
  293. fn_len = strlen(fn);
  294. } else {
  295. strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
  296. fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
  297. }
  298. /* If the name isn't in buf yet, it's wasn't absolute. */
  299. if (fn != buf) {
  300. int d_len = dirbuf_len - prefix_skip;
  301. if (d_len + fn_len >= MAXPATHLEN) {
  302. rprintf(FERROR, "merge-file name overflows: %s\n", fn);
  303. return NULL;
  304. }
  305. memcpy(buf, dirbuf + prefix_skip, d_len);
  306. memcpy(buf + d_len, fn, fn_len + 1);
  307. fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
  308. }
  309. if (len_ptr)
  310. *len_ptr = fn_len;
  311. return buf;
  312. }
  313. /* Sets the dirbuf and dirbuf_len values. */
  314. void set_filter_dir(const char *dir, unsigned int dirlen)
  315. {
  316. unsigned int len;
  317. if (*dir != '/') {
  318. memcpy(dirbuf, curr_dir, curr_dir_len);
  319. dirbuf[curr_dir_len] = '/';
  320. len = curr_dir_len + 1;
  321. if (len + dirlen >= MAXPATHLEN)
  322. dirlen = 0;
  323. } else
  324. len = 0;
  325. memcpy(dirbuf + len, dir, dirlen);
  326. dirbuf[dirlen + len] = '\0';
  327. dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
  328. if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
  329. && dirbuf[dirbuf_len-2] == '/')
  330. dirbuf_len -= 2;
  331. if (dirbuf_len != 1)
  332. dirbuf[dirbuf_len++] = '/';
  333. dirbuf[dirbuf_len] = '\0';
  334. if (sanitize_paths)
  335. dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
  336. }
  337. /* This routine takes a per-dir merge-file entry and finishes its setup.
  338. * If the name has a path portion then we check to see if it refers to a
  339. * parent directory of the first transfer dir. If it does, we scan all the
  340. * dirs from that point through the parent dir of the transfer dir looking
  341. * for the per-dir merge-file in each one. */
  342. static BOOL setup_merge_file(struct filter_struct *ex,
  343. struct filter_list_struct *lp)
  344. {
  345. char buf[MAXPATHLEN];
  346. char *x, *y, *pat = ex->pattern;
  347. unsigned int len;
  348. if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
  349. return 0;
  350. y = strrchr(x, '/');
  351. *y = '\0';
  352. ex->pattern = strdup(y+1);
  353. if (!*x)
  354. x = "/";
  355. if (*x == '/')
  356. strlcpy(buf, x, MAXPATHLEN);
  357. else
  358. pathjoin(buf, MAXPATHLEN, dirbuf, x);
  359. len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
  360. if (len != 1 && len < MAXPATHLEN-1) {
  361. buf[len++] = '/';
  362. buf[len] = '\0';
  363. }
  364. /* This ensures that the specified dir is a parent of the transfer. */
  365. for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
  366. if (*x)
  367. y += strlen(y); /* nope -- skip the scan */
  368. parent_dirscan = True;
  369. while (*y) {
  370. char save[MAXPATHLEN];
  371. strlcpy(save, y, MAXPATHLEN);
  372. *y = '\0';
  373. dirbuf_len = y - dirbuf;
  374. strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
  375. parse_filter_file(lp, buf, ex->match_flags, XFLG_ANCHORED2ABS);
  376. if (ex->match_flags & MATCHFLG_NO_INHERIT)
  377. lp->head = NULL;
  378. lp->tail = NULL;
  379. strlcpy(y, save, MAXPATHLEN);
  380. while ((*x++ = *y++) != '/') {}
  381. }
  382. parent_dirscan = False;
  383. free(pat);
  384. return 1;
  385. }
  386. /* Each time rsync changes to a new directory it call this function to
  387. * handle all the per-dir merge-files. The "dir" value is the current path
  388. * relative to curr_dir (which might not be null-terminated). We copy it
  389. * into dirbuf so that we can easily append a file name on the end. */
  390. void *push_local_filters(const char *dir, unsigned int dirlen)
  391. {
  392. struct filter_list_struct *ap, *push;
  393. int i;
  394. set_filter_dir(dir, dirlen);
  395. if (!mergelist_cnt)
  396. return NULL;
  397. push = new_array(struct filter_list_struct, mergelist_cnt);
  398. if (!push)
  399. out_of_memory("push_local_filters");
  400. for (i = 0, ap = push; i < mergelist_cnt; i++) {
  401. memcpy(ap++, mergelist_parents[i]->u.mergelist,
  402. sizeof (struct filter_list_struct));
  403. }
  404. /* Note: parse_filter_file() might increase mergelist_cnt, so keep
  405. * this loop separate from the above loop. */
  406. for (i = 0; i < mergelist_cnt; i++) {
  407. struct filter_struct *ex = mergelist_parents[i];
  408. struct filter_list_struct *lp = ex->u.mergelist;
  409. if (verbose > 2) {
  410. rprintf(FINFO, "[%s] pushing filter list%s\n",
  411. who_am_i(), lp->debug_type);
  412. }
  413. lp->tail = NULL; /* Switch any local rules to inherited. */
  414. if (ex->match_flags & MATCHFLG_NO_INHERIT)
  415. lp->head = NULL;
  416. if (ex->match_flags & MATCHFLG_FINISH_SETUP) {
  417. ex->match_flags &= ~MATCHFLG_FINISH_SETUP;
  418. if (setup_merge_file(ex, lp))
  419. set_filter_dir(dir, dirlen);
  420. }
  421. if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
  422. MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
  423. parse_filter_file(lp, dirbuf, ex->match_flags,
  424. XFLG_ANCHORED2ABS);
  425. } else {
  426. io_error |= IOERR_GENERAL;
  427. rprintf(FERROR,
  428. "cannot add local filter rules in long-named directory: %s\n",
  429. full_fname(dirbuf));
  430. }
  431. dirbuf[dirbuf_len] = '\0';
  432. }
  433. return (void*)push;
  434. }
  435. void pop_local_filters(void *mem)
  436. {
  437. struct filter_list_struct *ap, *pop = (struct filter_list_struct*)mem;
  438. int i;
  439. for (i = mergelist_cnt; i-- > 0; ) {
  440. struct filter_struct *ex = mergelist_parents[i];
  441. struct filter_list_struct *lp = ex->u.mergelist;
  442. if (verbose > 2) {
  443. rprintf(FINFO, "[%s] popping filter list%s\n",
  444. who_am_i(), lp->debug_type);
  445. }
  446. clear_filter_list(lp);
  447. }
  448. if (!pop)
  449. return;
  450. for (i = 0, ap = pop; i < mergelist_cnt; i++) {
  451. memcpy(mergelist_parents[i]->u.mergelist, ap++,
  452. sizeof (struct filter_list_struct));
  453. }
  454. free(pop);
  455. }
  456. void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
  457. {
  458. static int cur_depth = -1;
  459. static void *filt_array[MAXPATHLEN/2+1];
  460. if (!dname) {
  461. for ( ; cur_depth >= 0; cur_depth--) {
  462. if (filt_array[cur_depth]) {
  463. pop_local_filters(filt_array[cur_depth]);
  464. filt_array[cur_depth] = NULL;
  465. }
  466. }
  467. return;
  468. }
  469. assert(dir_depth < MAXPATHLEN/2+1);
  470. for ( ; cur_depth >= dir_depth; cur_depth--) {
  471. if (filt_array[cur_depth]) {
  472. pop_local_filters(filt_array[cur_depth]);
  473. filt_array[cur_depth] = NULL;
  474. }
  475. }
  476. cur_depth = dir_depth;
  477. filt_array[cur_depth] = push_local_filters(dname, dlen);
  478. }
  479. static int rule_matches(const char *fname, struct filter_struct *ex, int name_is_dir)
  480. {
  481. int slash_handling, str_cnt = 0, anchored_match = 0;
  482. int ret_match = ex->match_flags & MATCHFLG_NEGATE ? 0 : 1;
  483. char *p, *pattern = ex->pattern;
  484. const char *strings[16]; /* more than enough */
  485. const char *name = fname + (*fname == '/');
  486. if (!*name)
  487. return 0;
  488. if (!ex->u.slash_cnt && !(ex->match_flags & MATCHFLG_WILD2)) {
  489. /* If the pattern does not have any slashes AND it does
  490. * not have a "**" (which could match a slash), then we
  491. * just match the name portion of the path. */
  492. if ((p = strrchr(name,'/')) != NULL)
  493. name = p+1;
  494. } else if (ex->match_flags & MATCHFLG_ABS_PATH && *fname != '/'
  495. && curr_dir_len > module_dirlen + 1) {
  496. /* If we're matching against an absolute-path pattern,
  497. * we need to prepend our full path info. */
  498. strings[str_cnt++] = curr_dir + module_dirlen + 1;
  499. strings[str_cnt++] = "/";
  500. } else if (ex->match_flags & MATCHFLG_WILD2_PREFIX && *fname != '/') {
  501. /* Allow "**"+"/" to match at the start of the string. */
  502. strings[str_cnt++] = "/";
  503. }
  504. strings[str_cnt++] = name;
  505. if (name_is_dir) {
  506. /* Allow a trailing "/"+"***" to match the directory. */
  507. if (ex->match_flags & MATCHFLG_WILD3_SUFFIX)
  508. strings[str_cnt++] = "/";
  509. } else if (ex->match_flags & MATCHFLG_DIRECTORY)
  510. return !ret_match;
  511. strings[str_cnt] = NULL;
  512. if (*pattern == '/') {
  513. anchored_match = 1;
  514. pattern++;
  515. }
  516. if (!anchored_match && ex->u.slash_cnt
  517. && !(ex->match_flags & MATCHFLG_WILD2)) {
  518. /* A non-anchored match with an infix slash and no "**"
  519. * needs to match the last slash_cnt+1 name elements. */
  520. slash_handling = ex->u.slash_cnt + 1;
  521. } else if (!anchored_match && !(ex->match_flags & MATCHFLG_WILD2_PREFIX)
  522. && ex->match_flags & MATCHFLG_WILD2) {
  523. /* A non-anchored match with an infix or trailing "**" (but not
  524. * a prefixed "**") needs to try matching after every slash. */
  525. slash_handling = -1;
  526. } else {
  527. /* The pattern matches only at the start of the path or name. */
  528. slash_handling = 0;
  529. }
  530. if (ex->match_flags & MATCHFLG_WILD) {
  531. if (wildmatch_array(pattern, strings, slash_handling))
  532. return ret_match;
  533. } else if (str_cnt > 1) {
  534. if (litmatch_array(pattern, strings, slash_handling))
  535. return ret_match;
  536. } else if (anchored_match) {
  537. if (strcmp(name, pattern) == 0)
  538. return ret_match;
  539. } else {
  540. int l1 = strlen(name);
  541. int l2 = strlen(pattern);
  542. if (l2 <= l1 &&
  543. strcmp(name+(l1-l2),pattern) == 0 &&
  544. (l1==l2 || name[l1-(l2+1)] == '/')) {
  545. return ret_match;
  546. }
  547. }
  548. return !ret_match;
  549. }
  550. static void report_filter_result(enum logcode code, char const *name,
  551. struct filter_struct const *ent,
  552. int name_is_dir, const char *type)
  553. {
  554. /* If a trailing slash is present to match only directories,
  555. * then it is stripped out by add_rule(). So as a special
  556. * case we add it back in here. */
  557. if (verbose >= 2) {
  558. static char *actions[2][2]
  559. = { {"show", "hid"}, {"risk", "protect"} };
  560. const char *w = who_am_i();
  561. rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
  562. w, actions[*w!='s'][!(ent->match_flags&MATCHFLG_INCLUDE)],
  563. name_is_dir ? "directory" : "file", name, ent->pattern,
  564. ent->match_flags & MATCHFLG_DIRECTORY ? "/" : "", type);
  565. }
  566. }
  567. /*
  568. * Return -1 if file "name" is defined to be excluded by the specified
  569. * exclude list, 1 if it is included, and 0 if it was not matched.
  570. */
  571. int check_filter(struct filter_list_struct *listp, enum logcode code,
  572. const char *name, int name_is_dir)
  573. {
  574. struct filter_struct *ent;
  575. for (ent = listp->head; ent; ent = ent->next) {
  576. if (ignore_perishable && ent->match_flags & MATCHFLG_PERISHABLE)
  577. continue;
  578. if (ent->match_flags & MATCHFLG_PERDIR_MERGE) {
  579. int rc = check_filter(ent->u.mergelist, code, name,
  580. name_is_dir);
  581. if (rc)
  582. return rc;
  583. continue;
  584. }
  585. if (ent->match_flags & MATCHFLG_CVS_IGNORE) {
  586. int rc = check_filter(&cvs_filter_list, code, name,
  587. name_is_dir);
  588. if (rc)
  589. return rc;
  590. continue;
  591. }
  592. if (rule_matches(name, ent, name_is_dir)) {
  593. report_filter_result(code, name, ent, name_is_dir,
  594. listp->debug_type);
  595. return ent->match_flags & MATCHFLG_INCLUDE ? 1 : -1;
  596. }
  597. }
  598. return 0;
  599. }
  600. #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
  601. static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
  602. {
  603. if (strncmp((char*)str, rule, rule_len) != 0)
  604. return NULL;
  605. if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
  606. return str + rule_len - 1;
  607. if (str[rule_len] == ',')
  608. return str + rule_len;
  609. return NULL;
  610. }
  611. /* Get the next include/exclude arg from the string. The token will not
  612. * be '\0' terminated, so use the returned length to limit the string.
  613. * Also, be sure to add this length to the returned pointer before passing
  614. * it back to ask for the next token. This routine parses the "!" (list-
  615. * clearing) token and (depending on the mflags) the various prefixes.
  616. * The *mflags_ptr value will be set on exit to the new MATCHFLG_* bits
  617. * for the current token. */
  618. static const char *parse_rule_tok(const char *p, uint32 mflags, int xflags,
  619. unsigned int *len_ptr, uint32 *mflags_ptr)
  620. {
  621. const uchar *s = (const uchar *)p;
  622. uint32 new_mflags;
  623. unsigned int len;
  624. if (mflags & MATCHFLG_WORD_SPLIT) {
  625. /* Skip over any initial whitespace. */
  626. while (isspace(*s))
  627. s++;
  628. /* Update to point to real start of rule. */
  629. p = (const char *)s;
  630. }
  631. if (!*s)
  632. return NULL;
  633. new_mflags = mflags & MATCHFLGS_FROM_CONTAINER;
  634. /* Figure out what kind of a filter rule "s" is pointing at. Note
  635. * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include
  636. * or an exclude based on the inheritance of the MATCHFLG_INCLUDE
  637. * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
  638. * for old include/exclude patterns where just "+ " and "- " are
  639. * allowed as optional prefixes. */
  640. if (mflags & MATCHFLG_NO_PREFIXES) {
  641. if (*s == '!' && mflags & MATCHFLG_CVS_IGNORE)
  642. new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
  643. } else if (xflags & XFLG_OLD_PREFIXES) {
  644. if (*s == '-' && s[1] == ' ') {
  645. new_mflags &= ~MATCHFLG_INCLUDE;
  646. s += 2;
  647. } else if (*s == '+' && s[1] == ' ') {
  648. new_mflags |= MATCHFLG_INCLUDE;
  649. s += 2;
  650. } else if (*s == '!')
  651. new_mflags |= MATCHFLG_CLEAR_LIST; /* Tentative! */
  652. } else {
  653. char ch = 0, *mods = "";
  654. switch (*s) {
  655. case 'c':
  656. if ((s = RULE_STRCMP(s, "clear")) != NULL)
  657. ch = '!';
  658. break;
  659. case 'd':
  660. if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
  661. ch = ':';
  662. break;
  663. case 'e':
  664. if ((s = RULE_STRCMP(s, "exclude")) != NULL)
  665. ch = '-';
  666. break;
  667. case 'h':
  668. if ((s = RULE_STRCMP(s, "hide")) != NULL)
  669. ch = 'H';
  670. break;
  671. case 'i':
  672. if ((s = RULE_STRCMP(s, "include")) != NULL)
  673. ch = '+';
  674. break;
  675. case 'm':
  676. if ((s = RULE_STRCMP(s, "merge")) != NULL)
  677. ch = '.';
  678. break;
  679. case 'p':
  680. if ((s = RULE_STRCMP(s, "protect")) != NULL)
  681. ch = 'P';
  682. break;
  683. case 'r':
  684. if ((s = RULE_STRCMP(s, "risk")) != NULL)
  685. ch = 'R';
  686. break;
  687. case 's':
  688. if ((s = RULE_STRCMP(s, "show")) != NULL)
  689. ch = 'S';
  690. break;
  691. default:
  692. ch = *s;
  693. if (s[1] == ',')
  694. s++;
  695. break;
  696. }
  697. switch (ch) {
  698. case ':':
  699. new_mflags |= MATCHFLG_PERDIR_MERGE
  700. | MATCHFLG_FINISH_SETUP;
  701. /* FALL THROUGH */
  702. case '.':
  703. new_mflags |= MATCHFLG_MERGE_FILE;
  704. mods = MODIFIERS_INCL_EXCL MODIFIERS_MERGE_FILE;
  705. break;
  706. case '+':
  707. new_mflags |= MATCHFLG_INCLUDE;
  708. /* FALL THROUGH */
  709. case '-':
  710. mods = MODIFIERS_INCL_EXCL;
  711. break;
  712. case 'S':
  713. new_mflags |= MATCHFLG_INCLUDE;
  714. /* FALL THROUGH */
  715. case 'H':
  716. new_mflags |= MATCHFLG_SENDER_SIDE;
  717. mods = MODIFIERS_HIDE_PROTECT;
  718. break;
  719. case 'R':
  720. new_mflags |= MATCHFLG_INCLUDE;
  721. /* FALL THROUGH */
  722. case 'P':
  723. new_mflags |= MATCHFLG_RECEIVER_SIDE;
  724. mods = MODIFIERS_HIDE_PROTECT;
  725. break;
  726. case '!':
  727. new_mflags |= MATCHFLG_CLEAR_LIST;
  728. mods = NULL;
  729. break;
  730. default:
  731. rprintf(FERROR, "Unknown filter rule: `%s'\n", p);
  732. exit_cleanup(RERR_SYNTAX);
  733. }
  734. while (mods && *++s && *s != ' ' && *s != '_') {
  735. if (strchr(mods, *s) == NULL) {
  736. if (mflags & MATCHFLG_WORD_SPLIT && isspace(*s)) {
  737. s--;
  738. break;
  739. }
  740. invalid:
  741. rprintf(FERROR,
  742. "invalid modifier sequence at '%c' in filter rule: %s\n",
  743. *s, p);
  744. exit_cleanup(RERR_SYNTAX);
  745. }
  746. switch (*s) {
  747. case '-':
  748. if (new_mflags & MATCHFLG_NO_PREFIXES)
  749. goto invalid;
  750. new_mflags |= MATCHFLG_NO_PREFIXES;
  751. break;
  752. case '+':
  753. if (new_mflags & MATCHFLG_NO_PREFIXES)
  754. goto invalid;
  755. new_mflags |= MATCHFLG_NO_PREFIXES
  756. | MATCHFLG_INCLUDE;
  757. break;
  758. case '/':
  759. new_mflags |= MATCHFLG_ABS_PATH;
  760. break;
  761. case '!':
  762. new_mflags |= MATCHFLG_NEGATE;
  763. break;
  764. case 'C':
  765. if (new_mflags & MATCHFLG_NO_PREFIXES)
  766. goto invalid;
  767. new_mflags |= MATCHFLG_NO_PREFIXES
  768. | MATCHFLG_WORD_SPLIT
  769. | MATCHFLG_NO_INHERIT
  770. | MATCHFLG_CVS_IGNORE;
  771. break;
  772. case 'e':
  773. new_mflags |= MATCHFLG_EXCLUDE_SELF;
  774. break;
  775. case 'n':
  776. new_mflags |= MATCHFLG_NO_INHERIT;
  777. break;
  778. case 'p':
  779. new_mflags |= MATCHFLG_PERISHABLE;
  780. break;
  781. case 'r':
  782. new_mflags |= MATCHFLG_RECEIVER_SIDE;
  783. break;
  784. case 's':
  785. new_mflags |= MATCHFLG_SENDER_SIDE;
  786. break;
  787. case 'w':
  788. new_mflags |= MATCHFLG_WORD_SPLIT;
  789. break;
  790. }
  791. }
  792. if (*s)
  793. s++;
  794. }
  795. if (mflags & MATCHFLG_WORD_SPLIT) {
  796. const uchar *cp = s;
  797. /* Token ends at whitespace or the end of the string. */
  798. while (!isspace(*cp) && *cp != '\0')
  799. cp++;
  800. len = cp - s;
  801. } else
  802. len = strlen((char*)s);
  803. if (new_mflags & MATCHFLG_CLEAR_LIST) {
  804. if (!(mflags & MATCHFLG_NO_PREFIXES)
  805. && !(xflags & XFLG_OLD_PREFIXES) && len) {
  806. rprintf(FERROR,
  807. "'!' rule has trailing characters: %s\n", p);
  808. exit_cleanup(RERR_SYNTAX);
  809. }
  810. if (len > 1)
  811. new_mflags &= ~MATCHFLG_CLEAR_LIST;
  812. } else if (!len && !(new_mflags & MATCHFLG_CVS_IGNORE)) {
  813. rprintf(FERROR, "unexpected end of filter rule: %s\n", p);
  814. exit_cleanup(RERR_SYNTAX);
  815. }
  816. /* --delete-excluded turns an un-modified include/exclude into a
  817. * sender-side rule. We also affect per-dir merge files that take
  818. * no prefixes as a simple optimization. */
  819. if (delete_excluded
  820. && !(new_mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE))
  821. && (!(new_mflags & MATCHFLG_PERDIR_MERGE)
  822. || new_mflags & MATCHFLG_NO_PREFIXES))
  823. new_mflags |= MATCHFLG_SENDER_SIDE;
  824. *len_ptr = len;
  825. *mflags_ptr = new_mflags;
  826. return (const char *)s;
  827. }
  828. static char default_cvsignore[] =
  829. /* These default ignored items come from the CVS manual. */
  830. "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
  831. " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
  832. " *.old *.bak *.BAK *.orig *.rej .del-*"
  833. " *.a *.olb *.o *.obj *.so *.exe"
  834. " *.Z *.elc *.ln core"
  835. /* The rest we added to suit ourself. */
  836. " .svn/ .git/ .hg/ .bzr/";
  837. static void get_cvs_excludes(uint32 mflags)
  838. {
  839. static int initialized = 0;
  840. char *p, fname[MAXPATHLEN];
  841. if (initialized)
  842. return;
  843. initialized = 1;
  844. parse_rule(&cvs_filter_list, default_cvsignore,
  845. mflags | (protocol_version >= 30 ? MATCHFLG_PERISHABLE : 0),
  846. 0);
  847. p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
  848. if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
  849. parse_filter_file(&cvs_filter_list, fname, mflags, 0);
  850. parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
  851. }
  852. void parse_rule(struct filter_list_struct *listp, const char *pattern,
  853. uint32 mflags, int xflags)
  854. {
  855. unsigned int pat_len;
  856. uint32 new_mflags;
  857. const char *cp, *p;
  858. if (!pattern)
  859. return;
  860. while (1) {
  861. /* Remember that the returned string is NOT '\0' terminated! */
  862. cp = parse_rule_tok(pattern, mflags, xflags,
  863. &pat_len, &new_mflags);
  864. if (!cp)
  865. break;
  866. pattern = cp + pat_len;
  867. if (pat_len >= MAXPATHLEN) {
  868. rprintf(FERROR, "discarding over-long filter: %.*s\n",
  869. (int)pat_len, cp);
  870. continue;
  871. }
  872. if (new_mflags & MATCHFLG_CLEAR_LIST) {
  873. if (verbose > 2) {
  874. rprintf(FINFO,
  875. "[%s] clearing filter list%s\n",
  876. who_am_i(), listp->debug_type);
  877. }
  878. clear_filter_list(listp);
  879. continue;
  880. }
  881. if (new_mflags & MATCHFLG_MERGE_FILE) {
  882. unsigned int len;
  883. if (!pat_len) {
  884. cp = ".cvsignore";
  885. pat_len = 10;
  886. }
  887. len = pat_len;
  888. if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
  889. const char *name = cp + len;
  890. while (name > cp && name[-1] != '/') name--;
  891. len -= name - cp;
  892. add_rule(listp, name, len, 0, 0);
  893. new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
  894. len = pat_len;
  895. }
  896. if (new_mflags & MATCHFLG_PERDIR_MERGE) {
  897. if (parent_dirscan) {
  898. if (!(p = parse_merge_name(cp, &len,
  899. module_dirlen)))
  900. continue;
  901. add_rule(listp, p, len, new_mflags, 0);
  902. continue;
  903. }
  904. } else {
  905. if (!(p = parse_merge_name(cp, &len, 0)))
  906. continue;
  907. parse_filter_file(listp, p, new_mflags,
  908. XFLG_FATAL_ERRORS);
  909. continue;
  910. }
  911. }
  912. add_rule(listp, cp, pat_len, new_mflags, xflags);
  913. if (new_mflags & MATCHFLG_CVS_IGNORE
  914. && !(new_mflags & MATCHFLG_MERGE_FILE))
  915. get_cvs_excludes(new_mflags);
  916. }
  917. }
  918. void parse_filter_file(struct filter_list_struct *listp, const char *fname,
  919. uint32 mflags, int xflags)
  920. {
  921. FILE *fp;
  922. char line[BIGPATHBUFLEN];
  923. char *eob = line + sizeof line - 1;
  924. int word_split = mflags & MATCHFLG_WORD_SPLIT;
  925. if (!fname || !*fname)
  926. return;
  927. if (*fname != '-' || fname[1] || am_server) {
  928. if (daemon_filter_list.head) {
  929. strlcpy(line, fname, sizeof line);
  930. clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
  931. if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
  932. fp = NULL;
  933. else
  934. fp = fopen(line, "rb");
  935. } else
  936. fp = fopen(fname, "rb");
  937. } else
  938. fp = stdin;
  939. if (verbose > 2) {
  940. rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
  941. who_am_i(), fname, mflags, xflags,
  942. fp ? "" : " [not found]");
  943. }
  944. if (!fp) {
  945. if (xflags & XFLG_FATAL_ERRORS) {
  946. rsyserr(FERROR, errno,
  947. "failed to open %sclude file %s",
  948. mflags & MATCHFLG_INCLUDE ? "in" : "ex",
  949. fname);
  950. exit_cleanup(RERR_FILEIO);
  951. }
  952. return;
  953. }
  954. dirbuf[dirbuf_len] = '\0';
  955. while (1) {
  956. char *s = line;
  957. int ch, overflow = 0;
  958. while (1) {
  959. if ((ch = getc(fp)) == EOF) {
  960. if (ferror(fp) && errno == EINTR) {
  961. clearerr(fp);
  962. continue;
  963. }
  964. break;
  965. }
  966. if (word_split && isspace(ch))
  967. break;
  968. if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
  969. break;
  970. if (s < eob)
  971. *s++ = ch;
  972. else
  973. overflow = 1;
  974. }
  975. if (overflow) {
  976. rprintf(FERROR, "discarding over-long filter: %s...\n", line);
  977. s = line;
  978. }
  979. *s = '\0';
  980. /* Skip an empty token and (when line parsing) comments. */
  981. if (*line && (word_split || (*line != ';' && *line != '#')))
  982. parse_rule(listp, line, mflags, xflags);
  983. if (ch == EOF)
  984. break;
  985. }
  986. fclose(fp);
  987. }
  988. /* If the "for_xfer" flag is set, the prefix is made compatible with the
  989. * current protocol_version (if possible) or a NULL is returned (if not
  990. * possible). */
  991. char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
  992. unsigned int *plen_ptr)
  993. {
  994. static char buf[MAX_RULE_PREFIX+1];
  995. char *op = buf;
  996. int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
  997. if (match_flags & MATCHFLG_PERDIR_MERGE) {
  998. if (legal_len == 1)
  999. return NULL;
  1000. *op++ = ':';
  1001. } else if (match_flags & MATCHFLG_INCLUDE)
  1002. *op++ = '+';
  1003. else if (legal_len != 1
  1004. || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
  1005. *op++ = '-';
  1006. else
  1007. legal_len = 0;
  1008. if (match_flags & MATCHFLG_ABS_PATH)
  1009. *op++ = '/';
  1010. if (match_flags & MATCHFLG_NEGATE)
  1011. *op++ = '!';
  1012. if (match_flags & MATCHFLG_CVS_IGNORE)
  1013. *op++ = 'C';
  1014. else {
  1015. if (match_flags & MATCHFLG_NO_INHERIT)
  1016. *op++ = 'n';
  1017. if (match_flags & MATCHFLG_WORD_SPLIT)
  1018. *op++ = 'w';
  1019. if (match_flags & MATCHFLG_NO_PREFIXES) {
  1020. if (match_flags & MATCHFLG_INCLUDE)
  1021. *op++ = '+';
  1022. else
  1023. *op++ = '-';
  1024. }
  1025. }
  1026. if (match_flags & MATCHFLG_EXCLUDE_SELF)
  1027. *op++ = 'e';
  1028. if (match_flags & MATCHFLG_SENDER_SIDE
  1029. && (!for_xfer || protocol_version >= 29))
  1030. *op++ = 's';
  1031. if (match_flags & MATCHFLG_RECEIVER_SIDE
  1032. && (!for_xfer || protocol_version >= 29
  1033. || (delete_excluded && am_sender)))
  1034. *op++ = 'r';
  1035. if (match_flags & MATCHFLG_PERISHABLE) {
  1036. if (!for_xfer || protocol_version >= 30)
  1037. *op++ = 'p';
  1038. else if (am_sender)
  1039. return NULL;
  1040. }
  1041. if (op - buf > legal_len)
  1042. return NULL;
  1043. if (legal_len)
  1044. *op++ = ' ';
  1045. *op = '\0';
  1046. if (plen_ptr)
  1047. *plen_ptr = op - buf;
  1048. return buf;
  1049. }
  1050. static void send_rules(int f_out, struct filter_list_struct *flp)
  1051. {
  1052. struct filter_struct *ent, *prev = NULL;
  1053. for (ent = flp->head; ent; ent = ent->next) {
  1054. unsigned int len, plen, dlen;
  1055. int elide = 0;
  1056. char *p;
  1057. /* Note we need to check delete_excluded here in addition to
  1058. * the code in parse_rule_tok() because some rules may have
  1059. * been added before we found the --delete-excluded option.
  1060. * We must also elide any CVS merge-file rules to avoid a
  1061. * backward compatibility problem, and we elide any no-prefix
  1062. * merge files as an optimization (since they can only have
  1063. * include/exclude rules). */
  1064. if (ent->match_flags & MATCHFLG_SENDER_SIDE)
  1065. elide = am_sender ? 1 : -1;
  1066. if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
  1067. elide = elide ? 0 : am_sender ? -1 : 1;
  1068. else if (delete_excluded && !elide
  1069. && (!(ent->match_flags & MATCHFLG_PERDIR_MERGE)
  1070. || ent->match_flags & MATCHFLG_NO_PREFIXES))
  1071. elide = am_sender ? 1 : -1;
  1072. if (elide < 0) {
  1073. if (prev)
  1074. prev->next = ent->next;
  1075. else
  1076. flp->head = ent->next;
  1077. } else
  1078. prev = ent;
  1079. if (elide > 0)
  1080. continue;
  1081. if (ent->match_flags & MATCHFLG_CVS_IGNORE
  1082. && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
  1083. int f = am_sender || protocol_version < 29 ? f_out : -2;
  1084. send_rules(f, &cvs_filter_list);
  1085. if (f == f_out)
  1086. continue;
  1087. }
  1088. p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
  1089. if (!p) {
  1090. rprintf(FERROR,
  1091. "filter rules are too modern for remote rsync.\n");
  1092. exit_cleanup(RERR_PROTOCOL);
  1093. }
  1094. if (f_out < 0)
  1095. continue;
  1096. len = strlen(ent->pattern);
  1097. dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
  1098. if (!(plen + len + dlen))
  1099. continue;
  1100. write_int(f_out, plen + len + dlen);
  1101. if (plen)
  1102. write_buf(f_out, p, plen);
  1103. write_buf(f_out, ent->pattern, len);
  1104. if (dlen)
  1105. write_byte(f_out, '/');
  1106. }
  1107. flp->tail = prev;
  1108. }
  1109. /* This is only called by the client. */
  1110. void send_filter_list(int f_out)
  1111. {
  1112. int receiver_wants_list = prune_empty_dirs
  1113. || (delete_mode && (!delete_excluded || protocol_version >= 29));
  1114. if (local_server || (am_sender && !receiver_wants_list))
  1115. f_out = -1;
  1116. if (cvs_exclude && am_sender) {
  1117. if (protocol_version >= 29)
  1118. parse_rule(&filter_list, ":C", 0, 0);
  1119. parse_rule(&filter_list, "-C", 0, 0);
  1120. }
  1121. send_rules(f_out, &filter_list);
  1122. if (f_out >= 0)
  1123. write_int(f_out, 0);
  1124. if (cvs_exclude) {
  1125. if (!am_sender || protocol_version < 29)
  1126. parse_rule(&filter_list, ":C", 0, 0);
  1127. if (!am_sender)
  1128. parse_rule(&filter_list, "-C", 0, 0);
  1129. }
  1130. }
  1131. /* This is only called by the server. */
  1132. void recv_filter_list(int f_in)
  1133. {
  1134. char line[BIGPATHBUFLEN];
  1135. int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
  1136. int receiver_wants_list = prune_empty_dirs
  1137. || (delete_mode
  1138. && (!delete_excluded || protocol_version >= 29));
  1139. unsigned int len;
  1140. if (!local_server && (am_sender || receiver_wants_list)) {
  1141. while ((len = read_int(f_in)) != 0) {
  1142. if (len >= sizeof line)
  1143. overflow_exit("recv_rules");
  1144. read_sbuf(f_in, line, len);
  1145. parse_rule(&filter_list, line, 0, xflags);
  1146. }
  1147. }
  1148. if (cvs_exclude) {
  1149. if (local_server || am_sender || protocol_version < 29)
  1150. parse_rule(&filter_list, ":C", 0, 0);
  1151. if (local_server || am_sender)
  1152. parse_rule(&filter_list, "-C", 0, 0);
  1153. }
  1154. if (local_server) /* filter out any rules that aren't for us. */
  1155. send_rules(-1, &filter_list);
  1156. }