exclude.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 sender-side rule. */
  817. if (delete_excluded
  818. && !(new_mflags & (MATCHFLG_RECEIVER_SIDE|MATCHFLG_SENDER_SIDE|MATCHFLG_MERGE_FILE|MATCHFLG_PERDIR_MERGE)))
  819. new_mflags |= MATCHFLG_SENDER_SIDE;
  820. *len_ptr = len;
  821. *mflags_ptr = new_mflags;
  822. return (const char *)s;
  823. }
  824. static char default_cvsignore[] =
  825. /* These default ignored items come from the CVS manual. */
  826. "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
  827. " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
  828. " *.old *.bak *.BAK *.orig *.rej .del-*"
  829. " *.a *.olb *.o *.obj *.so *.exe"
  830. " *.Z *.elc *.ln core"
  831. /* The rest we added to suit ourself. */
  832. " .svn/ .git/ .hg/ .bzr/";
  833. static void get_cvs_excludes(uint32 mflags)
  834. {
  835. static int initialized = 0;
  836. char *p, fname[MAXPATHLEN];
  837. if (initialized)
  838. return;
  839. initialized = 1;
  840. parse_rule(&cvs_filter_list, default_cvsignore,
  841. mflags | (protocol_version >= 30 ? MATCHFLG_PERISHABLE : 0),
  842. 0);
  843. p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
  844. if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
  845. parse_filter_file(&cvs_filter_list, fname, mflags, 0);
  846. parse_rule(&cvs_filter_list, getenv("CVSIGNORE"), mflags, 0);
  847. }
  848. void parse_rule(struct filter_list_struct *listp, const char *pattern,
  849. uint32 mflags, int xflags)
  850. {
  851. unsigned int pat_len;
  852. uint32 new_mflags;
  853. const char *cp, *p;
  854. if (!pattern)
  855. return;
  856. while (1) {
  857. /* Remember that the returned string is NOT '\0' terminated! */
  858. cp = parse_rule_tok(pattern, mflags, xflags,
  859. &pat_len, &new_mflags);
  860. if (!cp)
  861. break;
  862. pattern = cp + pat_len;
  863. if (pat_len >= MAXPATHLEN) {
  864. rprintf(FERROR, "discarding over-long filter: %.*s\n",
  865. (int)pat_len, cp);
  866. continue;
  867. }
  868. if (new_mflags & MATCHFLG_CLEAR_LIST) {
  869. if (verbose > 2) {
  870. rprintf(FINFO,
  871. "[%s] clearing filter list%s\n",
  872. who_am_i(), listp->debug_type);
  873. }
  874. clear_filter_list(listp);
  875. continue;
  876. }
  877. if (new_mflags & MATCHFLG_MERGE_FILE) {
  878. unsigned int len;
  879. if (!pat_len) {
  880. cp = ".cvsignore";
  881. pat_len = 10;
  882. }
  883. len = pat_len;
  884. if (new_mflags & MATCHFLG_EXCLUDE_SELF) {
  885. const char *name = cp + len;
  886. while (name > cp && name[-1] != '/') name--;
  887. len -= name - cp;
  888. add_rule(listp, name, len, 0, 0);
  889. new_mflags &= ~MATCHFLG_EXCLUDE_SELF;
  890. len = pat_len;
  891. }
  892. if (new_mflags & MATCHFLG_PERDIR_MERGE) {
  893. if (parent_dirscan) {
  894. if (!(p = parse_merge_name(cp, &len,
  895. module_dirlen)))
  896. continue;
  897. add_rule(listp, p, len, new_mflags, 0);
  898. continue;
  899. }
  900. } else {
  901. if (!(p = parse_merge_name(cp, &len, 0)))
  902. continue;
  903. parse_filter_file(listp, p, new_mflags,
  904. XFLG_FATAL_ERRORS);
  905. continue;
  906. }
  907. }
  908. add_rule(listp, cp, pat_len, new_mflags, xflags);
  909. if (new_mflags & MATCHFLG_CVS_IGNORE
  910. && !(new_mflags & MATCHFLG_MERGE_FILE))
  911. get_cvs_excludes(new_mflags);
  912. }
  913. }
  914. void parse_filter_file(struct filter_list_struct *listp, const char *fname,
  915. uint32 mflags, int xflags)
  916. {
  917. FILE *fp;
  918. char line[BIGPATHBUFLEN];
  919. char *eob = line + sizeof line - 1;
  920. int word_split = mflags & MATCHFLG_WORD_SPLIT;
  921. if (!fname || !*fname)
  922. return;
  923. if (*fname != '-' || fname[1] || am_server) {
  924. if (daemon_filter_list.head) {
  925. strlcpy(line, fname, sizeof line);
  926. clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
  927. if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
  928. fp = NULL;
  929. else
  930. fp = fopen(line, "rb");
  931. } else
  932. fp = fopen(fname, "rb");
  933. } else
  934. fp = stdin;
  935. if (verbose > 2) {
  936. rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
  937. who_am_i(), fname, mflags, xflags,
  938. fp ? "" : " [not found]");
  939. }
  940. if (!fp) {
  941. if (xflags & XFLG_FATAL_ERRORS) {
  942. rsyserr(FERROR, errno,
  943. "failed to open %sclude file %s",
  944. mflags & MATCHFLG_INCLUDE ? "in" : "ex",
  945. fname);
  946. exit_cleanup(RERR_FILEIO);
  947. }
  948. return;
  949. }
  950. dirbuf[dirbuf_len] = '\0';
  951. while (1) {
  952. char *s = line;
  953. int ch, overflow = 0;
  954. while (1) {
  955. if ((ch = getc(fp)) == EOF) {
  956. if (ferror(fp) && errno == EINTR) {
  957. clearerr(fp);
  958. continue;
  959. }
  960. break;
  961. }
  962. if (word_split && isspace(ch))
  963. break;
  964. if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
  965. break;
  966. if (s < eob)
  967. *s++ = ch;
  968. else
  969. overflow = 1;
  970. }
  971. if (overflow) {
  972. rprintf(FERROR, "discarding over-long filter: %s...\n", line);
  973. s = line;
  974. }
  975. *s = '\0';
  976. /* Skip an empty token and (when line parsing) comments. */
  977. if (*line && (word_split || (*line != ';' && *line != '#')))
  978. parse_rule(listp, line, mflags, xflags);
  979. if (ch == EOF)
  980. break;
  981. }
  982. fclose(fp);
  983. }
  984. /* If the "for_xfer" flag is set, the prefix is made compatible with the
  985. * current protocol_version (if possible) or a NULL is returned (if not
  986. * possible). */
  987. char *get_rule_prefix(int match_flags, const char *pat, int for_xfer,
  988. unsigned int *plen_ptr)
  989. {
  990. static char buf[MAX_RULE_PREFIX+1];
  991. char *op = buf;
  992. int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
  993. if (match_flags & MATCHFLG_PERDIR_MERGE) {
  994. if (legal_len == 1)
  995. return NULL;
  996. *op++ = ':';
  997. } else if (match_flags & MATCHFLG_INCLUDE)
  998. *op++ = '+';
  999. else if (legal_len != 1
  1000. || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
  1001. *op++ = '-';
  1002. else
  1003. legal_len = 0;
  1004. if (match_flags & MATCHFLG_ABS_PATH)
  1005. *op++ = '/';
  1006. if (match_flags & MATCHFLG_NEGATE)
  1007. *op++ = '!';
  1008. if (match_flags & MATCHFLG_CVS_IGNORE)
  1009. *op++ = 'C';
  1010. else {
  1011. if (match_flags & MATCHFLG_NO_INHERIT)
  1012. *op++ = 'n';
  1013. if (match_flags & MATCHFLG_WORD_SPLIT)
  1014. *op++ = 'w';
  1015. if (match_flags & MATCHFLG_NO_PREFIXES) {
  1016. if (match_flags & MATCHFLG_INCLUDE)
  1017. *op++ = '+';
  1018. else
  1019. *op++ = '-';
  1020. }
  1021. }
  1022. if (match_flags & MATCHFLG_EXCLUDE_SELF)
  1023. *op++ = 'e';
  1024. if (match_flags & MATCHFLG_SENDER_SIDE
  1025. && (!for_xfer || protocol_version >= 29))
  1026. *op++ = 's';
  1027. if (match_flags & MATCHFLG_RECEIVER_SIDE
  1028. && (!for_xfer || protocol_version >= 29
  1029. || (delete_excluded && am_sender)))
  1030. *op++ = 'r';
  1031. if (match_flags & MATCHFLG_PERISHABLE) {
  1032. if (!for_xfer || protocol_version >= 30)
  1033. *op++ = 'p';
  1034. else if (am_sender)
  1035. return NULL;
  1036. }
  1037. if (op - buf > legal_len)
  1038. return NULL;
  1039. if (legal_len)
  1040. *op++ = ' ';
  1041. *op = '\0';
  1042. if (plen_ptr)
  1043. *plen_ptr = op - buf;
  1044. return buf;
  1045. }
  1046. static void send_rules(int f_out, struct filter_list_struct *flp)
  1047. {
  1048. struct filter_struct *ent, *prev = NULL;
  1049. for (ent = flp->head; ent; ent = ent->next) {
  1050. unsigned int len, plen, dlen;
  1051. int elide = 0;
  1052. char *p;
  1053. /* Note we need to check delete_excluded here in addition to
  1054. * the code in parse_rule_tok() because some rules may have
  1055. * been added before we found the --delete-excluded option.
  1056. * We must also elide any CVS merge-file rules to avoid a
  1057. * backward compatibility problem, and we elide any no-prefix
  1058. * merge files as an optimization (since they can only have
  1059. * include/exclude rules). */
  1060. if (ent->match_flags & MATCHFLG_SENDER_SIDE)
  1061. elide = am_sender ? 1 : -1;
  1062. if (ent->match_flags & MATCHFLG_RECEIVER_SIDE)
  1063. elide = elide ? 0 : am_sender ? -1 : 1;
  1064. else if (delete_excluded && !elide
  1065. && (!(ent->match_flags & MATCHFLG_PERDIR_MERGE)
  1066. || ent->match_flags & MATCHFLG_NO_PREFIXES))
  1067. elide = am_sender ? 1 : -1;
  1068. if (elide < 0) {
  1069. if (prev)
  1070. prev->next = ent->next;
  1071. else
  1072. flp->head = ent->next;
  1073. } else
  1074. prev = ent;
  1075. if (elide > 0)
  1076. continue;
  1077. if (ent->match_flags & MATCHFLG_CVS_IGNORE
  1078. && !(ent->match_flags & MATCHFLG_MERGE_FILE)) {
  1079. int f = am_sender || protocol_version < 29 ? f_out : -2;
  1080. send_rules(f, &cvs_filter_list);
  1081. if (f == f_out)
  1082. continue;
  1083. }
  1084. p = get_rule_prefix(ent->match_flags, ent->pattern, 1, &plen);
  1085. if (!p) {
  1086. rprintf(FERROR,
  1087. "filter rules are too modern for remote rsync.\n");
  1088. exit_cleanup(RERR_PROTOCOL);
  1089. }
  1090. if (f_out < 0)
  1091. continue;
  1092. len = strlen(ent->pattern);
  1093. dlen = ent->match_flags & MATCHFLG_DIRECTORY ? 1 : 0;
  1094. if (!(plen + len + dlen))
  1095. continue;
  1096. write_int(f_out, plen + len + dlen);
  1097. if (plen)
  1098. write_buf(f_out, p, plen);
  1099. write_buf(f_out, ent->pattern, len);
  1100. if (dlen)
  1101. write_byte(f_out, '/');
  1102. }
  1103. flp->tail = prev;
  1104. }
  1105. /* This is only called by the client. */
  1106. void send_filter_list(int f_out)
  1107. {
  1108. int receiver_wants_list = prune_empty_dirs
  1109. || (delete_mode && (!delete_excluded || protocol_version >= 29));
  1110. if (local_server || (am_sender && !receiver_wants_list))
  1111. f_out = -1;
  1112. if (cvs_exclude && am_sender) {
  1113. if (protocol_version >= 29)
  1114. parse_rule(&filter_list, ":C", 0, 0);
  1115. parse_rule(&filter_list, "-C", 0, 0);
  1116. }
  1117. send_rules(f_out, &filter_list);
  1118. if (f_out >= 0)
  1119. write_int(f_out, 0);
  1120. if (cvs_exclude) {
  1121. if (!am_sender || protocol_version < 29)
  1122. parse_rule(&filter_list, ":C", 0, 0);
  1123. if (!am_sender)
  1124. parse_rule(&filter_list, "-C", 0, 0);
  1125. }
  1126. }
  1127. /* This is only called by the server. */
  1128. void recv_filter_list(int f_in)
  1129. {
  1130. char line[BIGPATHBUFLEN];
  1131. int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
  1132. int receiver_wants_list = prune_empty_dirs
  1133. || (delete_mode
  1134. && (!delete_excluded || protocol_version >= 29));
  1135. unsigned int len;
  1136. if (!local_server && (am_sender || receiver_wants_list)) {
  1137. while ((len = read_int(f_in)) != 0) {
  1138. if (len >= sizeof line)
  1139. overflow_exit("recv_rules");
  1140. read_sbuf(f_in, line, len);
  1141. parse_rule(&filter_list, line, 0, xflags);
  1142. }
  1143. }
  1144. if (cvs_exclude) {
  1145. if (local_server || am_sender || protocol_version < 29)
  1146. parse_rule(&filter_list, ":C", 0, 0);
  1147. if (local_server || am_sender)
  1148. parse_rule(&filter_list, "-C", 0, 0);
  1149. }
  1150. if (local_server) /* filter out any rules that aren't for us. */
  1151. send_rules(-1, &filter_list);
  1152. }