archive_match.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #ifdef HAVE_LIMITS_H
  37. #include <limits.h>
  38. #endif
  39. #include "archive.h"
  40. #include "archive_private.h"
  41. #include "archive_entry.h"
  42. #include "archive_pathmatch.h"
  43. #include "archive_rb.h"
  44. #include "archive_string.h"
  45. #include "archive_time_private.h"
  46. struct match {
  47. struct match *next;
  48. int matched;
  49. struct archive_mstring pattern;
  50. };
  51. struct match_list {
  52. struct match *first;
  53. struct match **last;
  54. size_t unmatched_count;
  55. struct match *unmatched_next;
  56. int unmatched_eof;
  57. };
  58. struct match_file {
  59. struct archive_rb_node node;
  60. struct match_file *next;
  61. struct archive_mstring pathname;
  62. int flag;
  63. time_t mtime_sec;
  64. long mtime_nsec;
  65. time_t ctime_sec;
  66. long ctime_nsec;
  67. };
  68. struct entry_list {
  69. struct match_file *first;
  70. struct match_file **last;
  71. };
  72. struct id_array {
  73. size_t size;/* Allocated size */
  74. size_t count;
  75. int64_t *ids;
  76. };
  77. #define PATTERN_IS_SET 1
  78. #define TIME_IS_SET 2
  79. #define ID_IS_SET 4
  80. struct archive_match {
  81. struct archive archive;
  82. /* exclusion/inclusion set flag. */
  83. int setflag;
  84. /* Recursively include directory content? */
  85. int recursive_include;
  86. /*
  87. * Matching filename patterns.
  88. */
  89. struct match_list exclusions;
  90. struct match_list inclusions;
  91. /*
  92. * Matching time stamps.
  93. */
  94. time_t now;
  95. int newer_mtime_filter;
  96. time_t newer_mtime_sec;
  97. long newer_mtime_nsec;
  98. int newer_ctime_filter;
  99. time_t newer_ctime_sec;
  100. long newer_ctime_nsec;
  101. int older_mtime_filter;
  102. time_t older_mtime_sec;
  103. long older_mtime_nsec;
  104. int older_ctime_filter;
  105. time_t older_ctime_sec;
  106. long older_ctime_nsec;
  107. /*
  108. * Matching time stamps with its filename.
  109. */
  110. struct archive_rb_tree exclusion_tree;
  111. struct entry_list exclusion_entry_list;
  112. /*
  113. * Matching file owners.
  114. */
  115. struct id_array inclusion_uids;
  116. struct id_array inclusion_gids;
  117. struct match_list inclusion_unames;
  118. struct match_list inclusion_gnames;
  119. };
  120. static int add_pattern_from_file(struct archive_match *,
  121. struct match_list *, int, const void *, int);
  122. static int add_entry(struct archive_match *, int,
  123. struct archive_entry *);
  124. static int add_owner_id(struct archive_match *, struct id_array *,
  125. int64_t);
  126. static int add_owner_name(struct archive_match *, struct match_list *,
  127. int, const void *);
  128. static int add_pattern_mbs(struct archive_match *, struct match_list *,
  129. const char *);
  130. static int add_pattern_wcs(struct archive_match *, struct match_list *,
  131. const wchar_t *);
  132. #if !defined(_WIN32) || defined(__CYGWIN__)
  133. static int cmp_key_mbs(const struct archive_rb_node *, const void *);
  134. static int cmp_node_mbs(const struct archive_rb_node *,
  135. const struct archive_rb_node *);
  136. #else
  137. static int cmp_key_wcs(const struct archive_rb_node *, const void *);
  138. static int cmp_node_wcs(const struct archive_rb_node *,
  139. const struct archive_rb_node *);
  140. #endif
  141. static void entry_list_add(struct entry_list *, struct match_file *);
  142. static void entry_list_free(struct entry_list *);
  143. static void entry_list_init(struct entry_list *);
  144. static int error_nomem(struct archive_match *);
  145. static void match_list_add(struct match_list *, struct match *);
  146. static void match_list_free(struct match_list *);
  147. static void match_list_init(struct match_list *);
  148. static int match_list_unmatched_inclusions_next(struct archive_match *,
  149. struct match_list *, int, const void **);
  150. static int match_owner_id(struct id_array *, int64_t);
  151. #if !defined(_WIN32) || defined(__CYGWIN__)
  152. static int match_owner_name_mbs(struct archive_match *,
  153. struct match_list *, const char *);
  154. #else
  155. static int match_owner_name_wcs(struct archive_match *,
  156. struct match_list *, const wchar_t *);
  157. #endif
  158. static int match_path_exclusion(struct archive_match *,
  159. struct match *, int, const void *);
  160. static int match_path_inclusion(struct archive_match *,
  161. struct match *, int, const void *);
  162. static int owner_excluded(struct archive_match *,
  163. struct archive_entry *);
  164. static int path_excluded(struct archive_match *, int, const void *);
  165. static int set_timefilter(struct archive_match *, int, time_t, long,
  166. time_t, long);
  167. static int set_timefilter_pathname_mbs(struct archive_match *,
  168. int, const char *);
  169. static int set_timefilter_pathname_wcs(struct archive_match *,
  170. int, const wchar_t *);
  171. static int set_timefilter_date(struct archive_match *, int, const char *);
  172. static int set_timefilter_date_w(struct archive_match *, int,
  173. const wchar_t *);
  174. static int time_excluded(struct archive_match *,
  175. struct archive_entry *);
  176. static int validate_time_flag(struct archive *, int, const char *);
  177. #define get_date archive_parse_date
  178. static const struct archive_rb_tree_ops rb_ops = {
  179. #if !defined(_WIN32) || defined(__CYGWIN__)
  180. cmp_node_mbs, cmp_key_mbs
  181. #else
  182. cmp_node_wcs, cmp_key_wcs
  183. #endif
  184. };
  185. /*
  186. * The matching logic here needs to be re-thought. I started out to
  187. * try to mimic gtar's matching logic, but it's not entirely
  188. * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
  189. * on the command line as anchored, but --exclude doesn't.
  190. */
  191. static int
  192. error_nomem(struct archive_match *a)
  193. {
  194. archive_set_error(&(a->archive), ENOMEM, "No memory");
  195. a->archive.state = ARCHIVE_STATE_FATAL;
  196. return (ARCHIVE_FATAL);
  197. }
  198. /*
  199. * Create an ARCHIVE_MATCH object.
  200. */
  201. struct archive *
  202. archive_match_new(void)
  203. {
  204. struct archive_match *a;
  205. a = calloc(1, sizeof(*a));
  206. if (a == NULL)
  207. return (NULL);
  208. a->archive.magic = ARCHIVE_MATCH_MAGIC;
  209. a->archive.state = ARCHIVE_STATE_NEW;
  210. a->recursive_include = 1;
  211. match_list_init(&(a->inclusions));
  212. match_list_init(&(a->exclusions));
  213. __archive_rb_tree_init(&(a->exclusion_tree), &rb_ops);
  214. entry_list_init(&(a->exclusion_entry_list));
  215. match_list_init(&(a->inclusion_unames));
  216. match_list_init(&(a->inclusion_gnames));
  217. time(&a->now);
  218. return (&(a->archive));
  219. }
  220. /*
  221. * Free an ARCHIVE_MATCH object.
  222. */
  223. int
  224. archive_match_free(struct archive *_a)
  225. {
  226. struct archive_match *a;
  227. if (_a == NULL)
  228. return (ARCHIVE_OK);
  229. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  230. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_match_free");
  231. a = (struct archive_match *)_a;
  232. match_list_free(&(a->inclusions));
  233. match_list_free(&(a->exclusions));
  234. entry_list_free(&(a->exclusion_entry_list));
  235. free(a->inclusion_uids.ids);
  236. free(a->inclusion_gids.ids);
  237. match_list_free(&(a->inclusion_unames));
  238. match_list_free(&(a->inclusion_gnames));
  239. free(a);
  240. return (ARCHIVE_OK);
  241. }
  242. /*
  243. * Convenience function to perform all exclusion tests.
  244. *
  245. * Returns 1 if archive entry is excluded.
  246. * Returns 0 if archive entry is not excluded.
  247. * Returns <0 if something error happened.
  248. */
  249. int
  250. archive_match_excluded(struct archive *_a, struct archive_entry *entry)
  251. {
  252. struct archive_match *a;
  253. int r;
  254. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  255. ARCHIVE_STATE_NEW, "archive_match_excluded_ae");
  256. a = (struct archive_match *)_a;
  257. if (entry == NULL) {
  258. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  259. return (ARCHIVE_FAILED);
  260. }
  261. r = 0;
  262. if (a->setflag & PATTERN_IS_SET) {
  263. #if defined(_WIN32) && !defined(__CYGWIN__)
  264. r = path_excluded(a, 0, archive_entry_pathname_w(entry));
  265. #else
  266. r = path_excluded(a, 1, archive_entry_pathname(entry));
  267. #endif
  268. if (r != 0)
  269. return (r);
  270. }
  271. if (a->setflag & TIME_IS_SET) {
  272. r = time_excluded(a, entry);
  273. if (r != 0)
  274. return (r);
  275. }
  276. if (a->setflag & ID_IS_SET)
  277. r = owner_excluded(a, entry);
  278. return (r);
  279. }
  280. /*
  281. * Utility functions to manage exclusion/inclusion patterns
  282. */
  283. int
  284. archive_match_exclude_pattern(struct archive *_a, const char *pattern)
  285. {
  286. struct archive_match *a;
  287. int r;
  288. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  289. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern");
  290. a = (struct archive_match *)_a;
  291. if (pattern == NULL || *pattern == '\0') {
  292. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  293. return (ARCHIVE_FAILED);
  294. }
  295. if ((r = add_pattern_mbs(a, &(a->exclusions), pattern)) != ARCHIVE_OK)
  296. return (r);
  297. return (ARCHIVE_OK);
  298. }
  299. int
  300. archive_match_exclude_pattern_w(struct archive *_a, const wchar_t *pattern)
  301. {
  302. struct archive_match *a;
  303. int r;
  304. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  305. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_w");
  306. a = (struct archive_match *)_a;
  307. if (pattern == NULL || *pattern == L'\0') {
  308. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  309. return (ARCHIVE_FAILED);
  310. }
  311. if ((r = add_pattern_wcs(a, &(a->exclusions), pattern)) != ARCHIVE_OK)
  312. return (r);
  313. return (ARCHIVE_OK);
  314. }
  315. int
  316. archive_match_exclude_pattern_from_file(struct archive *_a,
  317. const char *pathname, int nullSeparator)
  318. {
  319. struct archive_match *a;
  320. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  321. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file");
  322. a = (struct archive_match *)_a;
  323. return add_pattern_from_file(a, &(a->exclusions), 1, pathname,
  324. nullSeparator);
  325. }
  326. int
  327. archive_match_exclude_pattern_from_file_w(struct archive *_a,
  328. const wchar_t *pathname, int nullSeparator)
  329. {
  330. struct archive_match *a;
  331. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  332. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file_w");
  333. a = (struct archive_match *)_a;
  334. return add_pattern_from_file(a, &(a->exclusions), 0, pathname,
  335. nullSeparator);
  336. }
  337. int
  338. archive_match_include_pattern(struct archive *_a, const char *pattern)
  339. {
  340. struct archive_match *a;
  341. int r;
  342. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  343. ARCHIVE_STATE_NEW, "archive_match_include_pattern");
  344. a = (struct archive_match *)_a;
  345. if (pattern == NULL || *pattern == '\0') {
  346. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  347. return (ARCHIVE_FAILED);
  348. }
  349. if ((r = add_pattern_mbs(a, &(a->inclusions), pattern)) != ARCHIVE_OK)
  350. return (r);
  351. return (ARCHIVE_OK);
  352. }
  353. int
  354. archive_match_include_pattern_w(struct archive *_a, const wchar_t *pattern)
  355. {
  356. struct archive_match *a;
  357. int r;
  358. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  359. ARCHIVE_STATE_NEW, "archive_match_include_pattern_w");
  360. a = (struct archive_match *)_a;
  361. if (pattern == NULL || *pattern == L'\0') {
  362. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  363. return (ARCHIVE_FAILED);
  364. }
  365. if ((r = add_pattern_wcs(a, &(a->inclusions), pattern)) != ARCHIVE_OK)
  366. return (r);
  367. return (ARCHIVE_OK);
  368. }
  369. int
  370. archive_match_include_pattern_from_file(struct archive *_a,
  371. const char *pathname, int nullSeparator)
  372. {
  373. struct archive_match *a;
  374. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  375. ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file");
  376. a = (struct archive_match *)_a;
  377. return add_pattern_from_file(a, &(a->inclusions), 1, pathname,
  378. nullSeparator);
  379. }
  380. int
  381. archive_match_include_pattern_from_file_w(struct archive *_a,
  382. const wchar_t *pathname, int nullSeparator)
  383. {
  384. struct archive_match *a;
  385. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  386. ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file_w");
  387. a = (struct archive_match *)_a;
  388. return add_pattern_from_file(a, &(a->inclusions), 0, pathname,
  389. nullSeparator);
  390. }
  391. /*
  392. * Test functions for pathname patterns.
  393. *
  394. * Returns 1 if archive entry is excluded.
  395. * Returns 0 if archive entry is not excluded.
  396. * Returns <0 if something error happened.
  397. */
  398. int
  399. archive_match_path_excluded(struct archive *_a,
  400. struct archive_entry *entry)
  401. {
  402. struct archive_match *a;
  403. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  404. ARCHIVE_STATE_NEW, "archive_match_path_excluded");
  405. a = (struct archive_match *)_a;
  406. if (entry == NULL) {
  407. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  408. return (ARCHIVE_FAILED);
  409. }
  410. /* If we don't have exclusion/inclusion pattern set at all,
  411. * the entry is always not excluded. */
  412. if ((a->setflag & PATTERN_IS_SET) == 0)
  413. return (0);
  414. #if defined(_WIN32) && !defined(__CYGWIN__)
  415. return (path_excluded(a, 0, archive_entry_pathname_w(entry)));
  416. #else
  417. return (path_excluded(a, 1, archive_entry_pathname(entry)));
  418. #endif
  419. }
  420. /*
  421. * When recursive inclusion of directory content is enabled,
  422. * an inclusion pattern that matches a directory will also
  423. * include everything beneath that directory. Enabled by default.
  424. *
  425. * For compatibility with GNU tar, exclusion patterns always
  426. * match if a subset of the full patch matches (i.e., they are
  427. * are not rooted at the beginning of the path) and thus there
  428. * is no corresponding non-recursive exclusion mode.
  429. */
  430. int
  431. archive_match_set_inclusion_recursion(struct archive *_a, int enabled)
  432. {
  433. struct archive_match *a;
  434. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  435. ARCHIVE_STATE_NEW, "archive_match_set_inclusion_recursion");
  436. a = (struct archive_match *)_a;
  437. a->recursive_include = enabled;
  438. return (ARCHIVE_OK);
  439. }
  440. /*
  441. * Utility functions to get statistic information for inclusion patterns.
  442. */
  443. int
  444. archive_match_path_unmatched_inclusions(struct archive *_a)
  445. {
  446. struct archive_match *a;
  447. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  448. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions");
  449. a = (struct archive_match *)_a;
  450. if (a->inclusions.unmatched_count > (size_t)INT_MAX)
  451. return INT_MAX;
  452. return (int)(a->inclusions.unmatched_count);
  453. }
  454. int
  455. archive_match_path_unmatched_inclusions_next(struct archive *_a,
  456. const char **_p)
  457. {
  458. struct archive_match *a;
  459. const void *v;
  460. int r;
  461. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  462. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions_next");
  463. a = (struct archive_match *)_a;
  464. r = match_list_unmatched_inclusions_next(a, &(a->inclusions), 1, &v);
  465. *_p = (const char *)v;
  466. return (r);
  467. }
  468. int
  469. archive_match_path_unmatched_inclusions_next_w(struct archive *_a,
  470. const wchar_t **_p)
  471. {
  472. struct archive_match *a;
  473. const void *v;
  474. int r;
  475. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  476. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions_next_w");
  477. a = (struct archive_match *)_a;
  478. r = match_list_unmatched_inclusions_next(a, &(a->inclusions), 0, &v);
  479. *_p = (const wchar_t *)v;
  480. return (r);
  481. }
  482. /*
  483. * Add inclusion/exclusion patterns.
  484. */
  485. static int
  486. add_pattern_mbs(struct archive_match *a, struct match_list *list,
  487. const char *pattern)
  488. {
  489. struct match *match;
  490. size_t len;
  491. match = calloc(1, sizeof(*match));
  492. if (match == NULL)
  493. return (error_nomem(a));
  494. /* Both "foo/" and "foo" should match "foo/bar". */
  495. len = strlen(pattern);
  496. if (len && pattern[len - 1] == '/')
  497. --len;
  498. archive_mstring_copy_mbs_len(&(match->pattern), pattern, len);
  499. match_list_add(list, match);
  500. a->setflag |= PATTERN_IS_SET;
  501. return (ARCHIVE_OK);
  502. }
  503. static int
  504. add_pattern_wcs(struct archive_match *a, struct match_list *list,
  505. const wchar_t *pattern)
  506. {
  507. struct match *match;
  508. size_t len;
  509. match = calloc(1, sizeof(*match));
  510. if (match == NULL)
  511. return (error_nomem(a));
  512. /* Both "foo/" and "foo" should match "foo/bar". */
  513. len = wcslen(pattern);
  514. if (len && pattern[len - 1] == L'/')
  515. --len;
  516. archive_mstring_copy_wcs_len(&(match->pattern), pattern, len);
  517. match_list_add(list, match);
  518. a->setflag |= PATTERN_IS_SET;
  519. return (ARCHIVE_OK);
  520. }
  521. static int
  522. add_pattern_from_file(struct archive_match *a, struct match_list *mlist,
  523. int mbs, const void *pathname, int nullSeparator)
  524. {
  525. struct archive *ar;
  526. struct archive_entry *ae;
  527. struct archive_string as;
  528. const void *buff;
  529. size_t size;
  530. int64_t offset;
  531. int r;
  532. ar = archive_read_new();
  533. if (ar == NULL) {
  534. archive_set_error(&(a->archive), ENOMEM, "No memory");
  535. return (ARCHIVE_FATAL);
  536. }
  537. r = archive_read_support_format_raw(ar);
  538. if (r == ARCHIVE_OK)
  539. r = archive_read_support_format_empty(ar);
  540. if (r != ARCHIVE_OK) {
  541. archive_copy_error(&(a->archive), ar);
  542. archive_read_free(ar);
  543. return (r);
  544. }
  545. if (mbs)
  546. r = archive_read_open_filename(ar, pathname, 512*20);
  547. else
  548. r = archive_read_open_filename_w(ar, pathname, 512*20);
  549. if (r != ARCHIVE_OK) {
  550. archive_copy_error(&(a->archive), ar);
  551. archive_read_free(ar);
  552. return (r);
  553. }
  554. r = archive_read_next_header(ar, &ae);
  555. if (r != ARCHIVE_OK) {
  556. archive_read_free(ar);
  557. if (r == ARCHIVE_EOF) {
  558. return (ARCHIVE_OK);
  559. } else {
  560. archive_copy_error(&(a->archive), ar);
  561. return (r);
  562. }
  563. }
  564. archive_string_init(&as);
  565. while ((r = archive_read_data_block(ar, &buff, &size, &offset))
  566. == ARCHIVE_OK) {
  567. const char *b = (const char *)buff;
  568. while (size) {
  569. const char *s = (const char *)b;
  570. size_t length = 0;
  571. int found_separator = 0;
  572. while (length < size) {
  573. if (nullSeparator) {
  574. if (*b == '\0') {
  575. found_separator = 1;
  576. break;
  577. }
  578. } else {
  579. if (*b == 0x0d || *b == 0x0a) {
  580. found_separator = 1;
  581. break;
  582. }
  583. }
  584. b++;
  585. length++;
  586. }
  587. if (!found_separator) {
  588. archive_strncat(&as, s, length);
  589. /* Read next data block. */
  590. break;
  591. }
  592. b++;
  593. size -= length + 1;
  594. archive_strncat(&as, s, length);
  595. /* If the line is not empty, add the pattern. */
  596. if (archive_strlen(&as) > 0) {
  597. /* Add pattern. */
  598. r = add_pattern_mbs(a, mlist, as.s);
  599. if (r != ARCHIVE_OK) {
  600. archive_read_free(ar);
  601. archive_string_free(&as);
  602. return (r);
  603. }
  604. archive_string_empty(&as);
  605. }
  606. }
  607. }
  608. /* If an error occurred, report it immediately. */
  609. if (r < ARCHIVE_OK) {
  610. archive_copy_error(&(a->archive), ar);
  611. archive_read_free(ar);
  612. archive_string_free(&as);
  613. return (r);
  614. }
  615. /* If the line is not empty, add the pattern. */
  616. if (r == ARCHIVE_EOF && archive_strlen(&as) > 0) {
  617. /* Add pattern. */
  618. r = add_pattern_mbs(a, mlist, as.s);
  619. if (r != ARCHIVE_OK) {
  620. archive_read_free(ar);
  621. archive_string_free(&as);
  622. return (r);
  623. }
  624. }
  625. archive_read_free(ar);
  626. archive_string_free(&as);
  627. return (ARCHIVE_OK);
  628. }
  629. /*
  630. * Test if pathname is excluded by inclusion/exclusion patterns.
  631. */
  632. static int
  633. path_excluded(struct archive_match *a, int mbs, const void *pathname)
  634. {
  635. struct match *match;
  636. struct match *matched;
  637. int r;
  638. if (a == NULL)
  639. return (0);
  640. /* Mark off any unmatched inclusions. */
  641. /* In particular, if a filename does appear in the archive and
  642. * is explicitly included and excluded, then we don't report
  643. * it as missing even though we don't extract it.
  644. */
  645. matched = NULL;
  646. for (match = a->inclusions.first; match != NULL;
  647. match = match->next){
  648. if (!match->matched &&
  649. (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
  650. if (r < 0)
  651. return (r);
  652. a->inclusions.unmatched_count--;
  653. match->matched = 1;
  654. matched = match;
  655. }
  656. }
  657. /* Exclusions take priority. */
  658. for (match = a->exclusions.first; match != NULL;
  659. match = match->next){
  660. r = match_path_exclusion(a, match, mbs, pathname);
  661. if (r)
  662. return (r);
  663. }
  664. /* It's not excluded and we found an inclusion above, so it's
  665. * included. */
  666. if (matched != NULL)
  667. return (0);
  668. /* We didn't find an unmatched inclusion, check the remaining ones. */
  669. for (match = a->inclusions.first; match != NULL;
  670. match = match->next){
  671. /* We looked at previously-unmatched inclusions already. */
  672. if (match->matched &&
  673. (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
  674. if (r < 0)
  675. return (r);
  676. return (0);
  677. }
  678. }
  679. /* If there were inclusions, default is to exclude. */
  680. if (a->inclusions.first != NULL)
  681. return (1);
  682. /* No explicit inclusions, default is to match. */
  683. return (0);
  684. }
  685. /*
  686. * This is a little odd, but it matches the default behavior of
  687. * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
  688. *
  689. */
  690. static int
  691. match_path_exclusion(struct archive_match *a, struct match *m,
  692. int mbs, const void *pn)
  693. {
  694. int flag = PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END;
  695. int r;
  696. if (mbs) {
  697. const char *p;
  698. r = archive_mstring_get_mbs(&(a->archive), &(m->pattern), &p);
  699. if (r == 0)
  700. return (archive_pathmatch(p, (const char *)pn, flag));
  701. } else {
  702. const wchar_t *p;
  703. r = archive_mstring_get_wcs(&(a->archive), &(m->pattern), &p);
  704. if (r == 0)
  705. return (archive_pathmatch_w(p, (const wchar_t *)pn,
  706. flag));
  707. }
  708. if (errno == ENOMEM)
  709. return (error_nomem(a));
  710. return (0);
  711. }
  712. /*
  713. * Again, mimic gtar: inclusions are always anchored (have to match
  714. * the beginning of the path) even though exclusions are not anchored.
  715. */
  716. static int
  717. match_path_inclusion(struct archive_match *a, struct match *m,
  718. int mbs, const void *pn)
  719. {
  720. /* Recursive operation requires only a prefix match. */
  721. int flag = a->recursive_include ?
  722. PATHMATCH_NO_ANCHOR_END :
  723. 0;
  724. int r;
  725. if (mbs) {
  726. const char *p;
  727. r = archive_mstring_get_mbs(&(a->archive), &(m->pattern), &p);
  728. if (r == 0)
  729. return (archive_pathmatch(p, (const char *)pn, flag));
  730. } else {
  731. const wchar_t *p;
  732. r = archive_mstring_get_wcs(&(a->archive), &(m->pattern), &p);
  733. if (r == 0)
  734. return (archive_pathmatch_w(p, (const wchar_t *)pn,
  735. flag));
  736. }
  737. if (errno == ENOMEM)
  738. return (error_nomem(a));
  739. return (0);
  740. }
  741. static void
  742. match_list_init(struct match_list *list)
  743. {
  744. list->first = NULL;
  745. list->last = &(list->first);
  746. }
  747. static void
  748. match_list_free(struct match_list *list)
  749. {
  750. struct match *p, *q;
  751. for (p = list->first; p != NULL; ) {
  752. q = p;
  753. p = p->next;
  754. archive_mstring_clean(&(q->pattern));
  755. free(q);
  756. }
  757. }
  758. static void
  759. match_list_add(struct match_list *list, struct match *m)
  760. {
  761. *list->last = m;
  762. list->last = &(m->next);
  763. list->unmatched_count++;
  764. }
  765. static int
  766. match_list_unmatched_inclusions_next(struct archive_match *a,
  767. struct match_list *list, int mbs, const void **vp)
  768. {
  769. struct match *m;
  770. *vp = NULL;
  771. if (list->unmatched_eof) {
  772. list->unmatched_eof = 0;
  773. return (ARCHIVE_EOF);
  774. }
  775. if (list->unmatched_next == NULL) {
  776. if (list->unmatched_count == 0)
  777. return (ARCHIVE_EOF);
  778. list->unmatched_next = list->first;
  779. }
  780. for (m = list->unmatched_next; m != NULL; m = m->next) {
  781. int r;
  782. if (m->matched)
  783. continue;
  784. if (mbs) {
  785. const char *p;
  786. r = archive_mstring_get_mbs(&(a->archive),
  787. &(m->pattern), &p);
  788. if (r < 0 && errno == ENOMEM)
  789. return (error_nomem(a));
  790. if (p == NULL)
  791. p = "";
  792. *vp = p;
  793. } else {
  794. const wchar_t *p;
  795. r = archive_mstring_get_wcs(&(a->archive),
  796. &(m->pattern), &p);
  797. if (r < 0 && errno == ENOMEM)
  798. return (error_nomem(a));
  799. if (p == NULL)
  800. p = L"";
  801. *vp = p;
  802. }
  803. list->unmatched_next = m->next;
  804. if (list->unmatched_next == NULL)
  805. /* To return EOF next time. */
  806. list->unmatched_eof = 1;
  807. return (ARCHIVE_OK);
  808. }
  809. list->unmatched_next = NULL;
  810. return (ARCHIVE_EOF);
  811. }
  812. /*
  813. * Utility functions to manage inclusion timestamps.
  814. */
  815. int
  816. archive_match_include_time(struct archive *_a, int flag, time_t sec,
  817. long nsec)
  818. {
  819. int r;
  820. r = validate_time_flag(_a, flag, "archive_match_include_time");
  821. if (r != ARCHIVE_OK)
  822. return (r);
  823. return set_timefilter((struct archive_match *)_a, flag,
  824. sec, nsec, sec, nsec);
  825. }
  826. int
  827. archive_match_include_date(struct archive *_a, int flag,
  828. const char *datestr)
  829. {
  830. int r;
  831. r = validate_time_flag(_a, flag, "archive_match_include_date");
  832. if (r != ARCHIVE_OK)
  833. return (r);
  834. return set_timefilter_date((struct archive_match *)_a, flag, datestr);
  835. }
  836. int
  837. archive_match_include_date_w(struct archive *_a, int flag,
  838. const wchar_t *datestr)
  839. {
  840. int r;
  841. r = validate_time_flag(_a, flag, "archive_match_include_date_w");
  842. if (r != ARCHIVE_OK)
  843. return (r);
  844. return set_timefilter_date_w((struct archive_match *)_a, flag, datestr);
  845. }
  846. int
  847. archive_match_include_file_time(struct archive *_a, int flag,
  848. const char *pathname)
  849. {
  850. int r;
  851. r = validate_time_flag(_a, flag, "archive_match_include_file_time");
  852. if (r != ARCHIVE_OK)
  853. return (r);
  854. return set_timefilter_pathname_mbs((struct archive_match *)_a,
  855. flag, pathname);
  856. }
  857. int
  858. archive_match_include_file_time_w(struct archive *_a, int flag,
  859. const wchar_t *pathname)
  860. {
  861. int r;
  862. r = validate_time_flag(_a, flag, "archive_match_include_file_time_w");
  863. if (r != ARCHIVE_OK)
  864. return (r);
  865. return set_timefilter_pathname_wcs((struct archive_match *)_a,
  866. flag, pathname);
  867. }
  868. int
  869. archive_match_exclude_entry(struct archive *_a, int flag,
  870. struct archive_entry *entry)
  871. {
  872. struct archive_match *a;
  873. int r;
  874. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  875. ARCHIVE_STATE_NEW, "archive_match_time_include_entry");
  876. a = (struct archive_match *)_a;
  877. if (entry == NULL) {
  878. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  879. return (ARCHIVE_FAILED);
  880. }
  881. r = validate_time_flag(_a, flag, "archive_match_exclude_entry");
  882. if (r != ARCHIVE_OK)
  883. return (r);
  884. return (add_entry(a, flag, entry));
  885. }
  886. /*
  887. * Test function for time stamps.
  888. *
  889. * Returns 1 if archive entry is excluded.
  890. * Returns 0 if archive entry is not excluded.
  891. * Returns <0 if something error happened.
  892. */
  893. int
  894. archive_match_time_excluded(struct archive *_a,
  895. struct archive_entry *entry)
  896. {
  897. struct archive_match *a;
  898. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  899. ARCHIVE_STATE_NEW, "archive_match_time_excluded_ae");
  900. a = (struct archive_match *)_a;
  901. if (entry == NULL) {
  902. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  903. return (ARCHIVE_FAILED);
  904. }
  905. /* If we don't have inclusion time set at all, the entry is always
  906. * not excluded. */
  907. if ((a->setflag & TIME_IS_SET) == 0)
  908. return (0);
  909. return (time_excluded(a, entry));
  910. }
  911. static int
  912. validate_time_flag(struct archive *_a, int flag, const char *_fn)
  913. {
  914. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  915. ARCHIVE_STATE_NEW, _fn);
  916. /* Check a type of time. */
  917. if (flag &
  918. ((~(ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME)) & 0xff00)) {
  919. archive_set_error(_a, EINVAL, "Invalid time flag");
  920. return (ARCHIVE_FAILED);
  921. }
  922. if ((flag & (ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME)) == 0) {
  923. archive_set_error(_a, EINVAL, "No time flag");
  924. return (ARCHIVE_FAILED);
  925. }
  926. /* Check a type of comparison. */
  927. if (flag &
  928. ((~(ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER
  929. | ARCHIVE_MATCH_EQUAL)) & 0x00ff)) {
  930. archive_set_error(_a, EINVAL, "Invalid comparison flag");
  931. return (ARCHIVE_FAILED);
  932. }
  933. if ((flag & (ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER
  934. | ARCHIVE_MATCH_EQUAL)) == 0) {
  935. archive_set_error(_a, EINVAL, "No comparison flag");
  936. return (ARCHIVE_FAILED);
  937. }
  938. return (ARCHIVE_OK);
  939. }
  940. #define JUST_EQUAL(t) (((t) & (ARCHIVE_MATCH_EQUAL |\
  941. ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER)) == ARCHIVE_MATCH_EQUAL)
  942. static int
  943. set_timefilter(struct archive_match *a, int timetype,
  944. time_t mtime_sec, long mtime_nsec, time_t ctime_sec, long ctime_nsec)
  945. {
  946. if (timetype & ARCHIVE_MATCH_MTIME) {
  947. if ((timetype & ARCHIVE_MATCH_NEWER) || JUST_EQUAL(timetype)) {
  948. a->newer_mtime_filter = timetype;
  949. a->newer_mtime_sec = mtime_sec;
  950. a->newer_mtime_nsec = mtime_nsec;
  951. a->setflag |= TIME_IS_SET;
  952. }
  953. if ((timetype & ARCHIVE_MATCH_OLDER) || JUST_EQUAL(timetype)) {
  954. a->older_mtime_filter = timetype;
  955. a->older_mtime_sec = mtime_sec;
  956. a->older_mtime_nsec = mtime_nsec;
  957. a->setflag |= TIME_IS_SET;
  958. }
  959. }
  960. if (timetype & ARCHIVE_MATCH_CTIME) {
  961. if ((timetype & ARCHIVE_MATCH_NEWER) || JUST_EQUAL(timetype)) {
  962. a->newer_ctime_filter = timetype;
  963. a->newer_ctime_sec = ctime_sec;
  964. a->newer_ctime_nsec = ctime_nsec;
  965. a->setflag |= TIME_IS_SET;
  966. }
  967. if ((timetype & ARCHIVE_MATCH_OLDER) || JUST_EQUAL(timetype)) {
  968. a->older_ctime_filter = timetype;
  969. a->older_ctime_sec = ctime_sec;
  970. a->older_ctime_nsec = ctime_nsec;
  971. a->setflag |= TIME_IS_SET;
  972. }
  973. }
  974. return (ARCHIVE_OK);
  975. }
  976. static int
  977. set_timefilter_date(struct archive_match *a, int timetype, const char *datestr)
  978. {
  979. time_t t;
  980. if (datestr == NULL || *datestr == '\0') {
  981. archive_set_error(&(a->archive), EINVAL, "date is empty");
  982. return (ARCHIVE_FAILED);
  983. }
  984. t = get_date(a->now, datestr);
  985. if (t == (time_t)-1) {
  986. archive_set_error(&(a->archive), EINVAL, "invalid date string");
  987. return (ARCHIVE_FAILED);
  988. }
  989. return set_timefilter(a, timetype, t, 0, t, 0);
  990. }
  991. static int
  992. set_timefilter_date_w(struct archive_match *a, int timetype,
  993. const wchar_t *datestr)
  994. {
  995. struct archive_string as;
  996. time_t t;
  997. if (datestr == NULL || *datestr == L'\0') {
  998. archive_set_error(&(a->archive), EINVAL, "date is empty");
  999. return (ARCHIVE_FAILED);
  1000. }
  1001. archive_string_init(&as);
  1002. if (archive_string_append_from_wcs(&as, datestr, wcslen(datestr)) < 0) {
  1003. archive_string_free(&as);
  1004. if (errno == ENOMEM)
  1005. return (error_nomem(a));
  1006. archive_set_error(&(a->archive), -1,
  1007. "Failed to convert WCS to MBS");
  1008. return (ARCHIVE_FAILED);
  1009. }
  1010. t = get_date(a->now, as.s);
  1011. archive_string_free(&as);
  1012. if (t == (time_t)-1) {
  1013. archive_set_error(&(a->archive), EINVAL, "invalid date string");
  1014. return (ARCHIVE_FAILED);
  1015. }
  1016. return set_timefilter(a, timetype, t, 0, t, 0);
  1017. }
  1018. #if defined(_WIN32) && !defined(__CYGWIN__)
  1019. static int
  1020. set_timefilter_find_data(struct archive_match *a, int timetype,
  1021. const FILETIME* ftLastWriteTime, const FILETIME* ftCreationTime)
  1022. {
  1023. time_t ctime_sec, mtime_sec;
  1024. uint32_t ctime_ns, mtime_ns;
  1025. ntfs_to_unix(FILETIME_to_ntfs(ftLastWriteTime), &mtime_sec, &mtime_ns);
  1026. ntfs_to_unix(FILETIME_to_ntfs(ftCreationTime), &ctime_sec, &ctime_ns);
  1027. return set_timefilter(a, timetype,
  1028. mtime_sec, mtime_ns, ctime_sec, ctime_ns);
  1029. }
  1030. static int
  1031. set_timefilter_pathname_mbs(struct archive_match *a, int timetype,
  1032. const char *path)
  1033. {
  1034. /* NOTE: stat() on Windows cannot handle nano seconds. */
  1035. HANDLE h;
  1036. WIN32_FIND_DATAA d;
  1037. if (path == NULL || *path == '\0') {
  1038. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1039. return (ARCHIVE_FAILED);
  1040. }
  1041. h = FindFirstFileA(path, &d);
  1042. if (h == INVALID_HANDLE_VALUE) {
  1043. la_dosmaperr(GetLastError());
  1044. archive_set_error(&(a->archive), errno,
  1045. "Failed to FindFirstFileA");
  1046. return (ARCHIVE_FAILED);
  1047. }
  1048. FindClose(h);
  1049. return set_timefilter_find_data(a, timetype, &d.ftLastWriteTime, &d.ftCreationTime);
  1050. }
  1051. static int
  1052. set_timefilter_pathname_wcs(struct archive_match *a, int timetype,
  1053. const wchar_t *path)
  1054. {
  1055. HANDLE h;
  1056. WIN32_FIND_DATAW d;
  1057. if (path == NULL || *path == L'\0') {
  1058. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1059. return (ARCHIVE_FAILED);
  1060. }
  1061. h = FindFirstFileW(path, &d);
  1062. if (h == INVALID_HANDLE_VALUE) {
  1063. la_dosmaperr(GetLastError());
  1064. archive_set_error(&(a->archive), errno,
  1065. "Failed to FindFirstFile");
  1066. return (ARCHIVE_FAILED);
  1067. }
  1068. FindClose(h);
  1069. return set_timefilter_find_data(a, timetype, &d.ftLastWriteTime, &d.ftCreationTime);
  1070. }
  1071. #else /* _WIN32 && !__CYGWIN__ */
  1072. static int
  1073. set_timefilter_stat(struct archive_match *a, int timetype, struct stat *st)
  1074. {
  1075. struct archive_entry *ae;
  1076. time_t ctime_sec, mtime_sec;
  1077. long ctime_ns, mtime_ns;
  1078. ae = archive_entry_new();
  1079. if (ae == NULL)
  1080. return (error_nomem(a));
  1081. archive_entry_copy_stat(ae, st);
  1082. ctime_sec = archive_entry_ctime(ae);
  1083. ctime_ns = archive_entry_ctime_nsec(ae);
  1084. mtime_sec = archive_entry_mtime(ae);
  1085. mtime_ns = archive_entry_mtime_nsec(ae);
  1086. archive_entry_free(ae);
  1087. return set_timefilter(a, timetype, mtime_sec, mtime_ns,
  1088. ctime_sec, ctime_ns);
  1089. }
  1090. static int
  1091. set_timefilter_pathname_mbs(struct archive_match *a, int timetype,
  1092. const char *path)
  1093. {
  1094. struct stat st;
  1095. if (path == NULL || *path == '\0') {
  1096. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1097. return (ARCHIVE_FAILED);
  1098. }
  1099. if (la_stat(path, &st) != 0) {
  1100. archive_set_error(&(a->archive), errno, "Failed to stat()");
  1101. return (ARCHIVE_FAILED);
  1102. }
  1103. return (set_timefilter_stat(a, timetype, &st));
  1104. }
  1105. static int
  1106. set_timefilter_pathname_wcs(struct archive_match *a, int timetype,
  1107. const wchar_t *path)
  1108. {
  1109. struct archive_string as;
  1110. int r;
  1111. if (path == NULL || *path == L'\0') {
  1112. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1113. return (ARCHIVE_FAILED);
  1114. }
  1115. /* Convert WCS filename to MBS filename. */
  1116. archive_string_init(&as);
  1117. if (archive_string_append_from_wcs(&as, path, wcslen(path)) < 0) {
  1118. archive_string_free(&as);
  1119. if (errno == ENOMEM)
  1120. return (error_nomem(a));
  1121. archive_set_error(&(a->archive), -1,
  1122. "Failed to convert WCS to MBS");
  1123. return (ARCHIVE_FAILED);
  1124. }
  1125. r = set_timefilter_pathname_mbs(a, timetype, as.s);
  1126. archive_string_free(&as);
  1127. return (r);
  1128. }
  1129. #endif /* _WIN32 && !__CYGWIN__ */
  1130. /*
  1131. * Call back functions for archive_rb.
  1132. */
  1133. #if !defined(_WIN32) || defined(__CYGWIN__)
  1134. static int
  1135. cmp_node_mbs(const struct archive_rb_node *n1,
  1136. const struct archive_rb_node *n2)
  1137. {
  1138. struct match_file *f1 = (struct match_file *)(uintptr_t)n1;
  1139. struct match_file *f2 = (struct match_file *)(uintptr_t)n2;
  1140. const char *p1, *p2;
  1141. archive_mstring_get_mbs(NULL, &(f1->pathname), &p1);
  1142. archive_mstring_get_mbs(NULL, &(f2->pathname), &p2);
  1143. if (p1 == NULL)
  1144. return (1);
  1145. if (p2 == NULL)
  1146. return (-1);
  1147. return (strcmp(p1, p2));
  1148. }
  1149. static int
  1150. cmp_key_mbs(const struct archive_rb_node *n, const void *key)
  1151. {
  1152. struct match_file *f = (struct match_file *)(uintptr_t)n;
  1153. const char *p;
  1154. archive_mstring_get_mbs(NULL, &(f->pathname), &p);
  1155. if (p == NULL)
  1156. return (-1);
  1157. return (strcmp(p, (const char *)key));
  1158. }
  1159. #else
  1160. static int
  1161. cmp_node_wcs(const struct archive_rb_node *n1,
  1162. const struct archive_rb_node *n2)
  1163. {
  1164. struct match_file *f1 = (struct match_file *)(uintptr_t)n1;
  1165. struct match_file *f2 = (struct match_file *)(uintptr_t)n2;
  1166. const wchar_t *p1, *p2;
  1167. archive_mstring_get_wcs(NULL, &(f1->pathname), &p1);
  1168. archive_mstring_get_wcs(NULL, &(f2->pathname), &p2);
  1169. if (p1 == NULL)
  1170. return (1);
  1171. if (p2 == NULL)
  1172. return (-1);
  1173. return (wcscmp(p1, p2));
  1174. }
  1175. static int
  1176. cmp_key_wcs(const struct archive_rb_node *n, const void *key)
  1177. {
  1178. struct match_file *f = (struct match_file *)(uintptr_t)n;
  1179. const wchar_t *p;
  1180. archive_mstring_get_wcs(NULL, &(f->pathname), &p);
  1181. if (p == NULL)
  1182. return (-1);
  1183. return (wcscmp(p, (const wchar_t *)key));
  1184. }
  1185. #endif
  1186. static void
  1187. entry_list_init(struct entry_list *list)
  1188. {
  1189. list->first = NULL;
  1190. list->last = &(list->first);
  1191. }
  1192. static void
  1193. entry_list_free(struct entry_list *list)
  1194. {
  1195. struct match_file *p, *q;
  1196. for (p = list->first; p != NULL; ) {
  1197. q = p;
  1198. p = p->next;
  1199. archive_mstring_clean(&(q->pathname));
  1200. free(q);
  1201. }
  1202. }
  1203. static void
  1204. entry_list_add(struct entry_list *list, struct match_file *file)
  1205. {
  1206. *list->last = file;
  1207. list->last = &(file->next);
  1208. }
  1209. static int
  1210. add_entry(struct archive_match *a, int flag,
  1211. struct archive_entry *entry)
  1212. {
  1213. struct match_file *f;
  1214. const void *pathname;
  1215. int r;
  1216. f = calloc(1, sizeof(*f));
  1217. if (f == NULL)
  1218. return (error_nomem(a));
  1219. #if defined(_WIN32) && !defined(__CYGWIN__)
  1220. pathname = archive_entry_pathname_w(entry);
  1221. if (pathname == NULL) {
  1222. free(f);
  1223. archive_set_error(&(a->archive), EINVAL, "pathname is NULL");
  1224. return (ARCHIVE_FAILED);
  1225. }
  1226. archive_mstring_copy_wcs(&(f->pathname), pathname);
  1227. #else
  1228. pathname = archive_entry_pathname(entry);
  1229. if (pathname == NULL) {
  1230. free(f);
  1231. archive_set_error(&(a->archive), EINVAL, "pathname is NULL");
  1232. return (ARCHIVE_FAILED);
  1233. }
  1234. archive_mstring_copy_mbs(&(f->pathname), pathname);
  1235. #endif
  1236. f->flag = flag;
  1237. f->mtime_sec = archive_entry_mtime(entry);
  1238. f->mtime_nsec = archive_entry_mtime_nsec(entry);
  1239. f->ctime_sec = archive_entry_ctime(entry);
  1240. f->ctime_nsec = archive_entry_ctime_nsec(entry);
  1241. r = __archive_rb_tree_insert_node(&(a->exclusion_tree), &(f->node));
  1242. if (!r) {
  1243. struct match_file *f2;
  1244. /* Get the duplicated file. */
  1245. f2 = (struct match_file *)__archive_rb_tree_find_node(
  1246. &(a->exclusion_tree), pathname);
  1247. /*
  1248. * We always overwrite comparison condition.
  1249. * If you do not want to overwrite it, you should not
  1250. * call archive_match_exclude_entry(). We cannot know
  1251. * what behavior you really expect since overwriting
  1252. * condition might be different with the flag.
  1253. */
  1254. if (f2 != NULL) {
  1255. f2->flag = f->flag;
  1256. f2->mtime_sec = f->mtime_sec;
  1257. f2->mtime_nsec = f->mtime_nsec;
  1258. f2->ctime_sec = f->ctime_sec;
  1259. f2->ctime_nsec = f->ctime_nsec;
  1260. }
  1261. /* Release the duplicated file. */
  1262. archive_mstring_clean(&(f->pathname));
  1263. free(f);
  1264. return (ARCHIVE_OK);
  1265. }
  1266. entry_list_add(&(a->exclusion_entry_list), f);
  1267. a->setflag |= TIME_IS_SET;
  1268. return (ARCHIVE_OK);
  1269. }
  1270. /*
  1271. * Test if entry is excluded by its timestamp.
  1272. */
  1273. static int
  1274. time_excluded(struct archive_match *a, struct archive_entry *entry)
  1275. {
  1276. struct match_file *f;
  1277. const void *pathname;
  1278. time_t sec;
  1279. long nsec;
  1280. /*
  1281. * If this file/dir is excluded by a time comparison, skip it.
  1282. */
  1283. if (a->newer_ctime_filter) {
  1284. /* If ctime is not set, use mtime instead. */
  1285. if (archive_entry_ctime_is_set(entry))
  1286. sec = archive_entry_ctime(entry);
  1287. else
  1288. sec = archive_entry_mtime(entry);
  1289. if (sec < a->newer_ctime_sec)
  1290. return (1); /* Too old, skip it. */
  1291. if (sec == a->newer_ctime_sec) {
  1292. if (archive_entry_ctime_is_set(entry))
  1293. nsec = archive_entry_ctime_nsec(entry);
  1294. else
  1295. nsec = archive_entry_mtime_nsec(entry);
  1296. if (nsec < a->newer_ctime_nsec)
  1297. return (1); /* Too old, skip it. */
  1298. if (nsec == a->newer_ctime_nsec &&
  1299. (a->newer_ctime_filter & ARCHIVE_MATCH_EQUAL)
  1300. == 0)
  1301. return (1); /* Equal, skip it. */
  1302. }
  1303. }
  1304. if (a->older_ctime_filter) {
  1305. /* If ctime is not set, use mtime instead. */
  1306. if (archive_entry_ctime_is_set(entry))
  1307. sec = archive_entry_ctime(entry);
  1308. else
  1309. sec = archive_entry_mtime(entry);
  1310. if (sec > a->older_ctime_sec)
  1311. return (1); /* Too new, skip it. */
  1312. if (sec == a->older_ctime_sec) {
  1313. if (archive_entry_ctime_is_set(entry))
  1314. nsec = archive_entry_ctime_nsec(entry);
  1315. else
  1316. nsec = archive_entry_mtime_nsec(entry);
  1317. if (nsec > a->older_ctime_nsec)
  1318. return (1); /* Too new, skip it. */
  1319. if (nsec == a->older_ctime_nsec &&
  1320. (a->older_ctime_filter & ARCHIVE_MATCH_EQUAL)
  1321. == 0)
  1322. return (1); /* Equal, skip it. */
  1323. }
  1324. }
  1325. if (a->newer_mtime_filter) {
  1326. sec = archive_entry_mtime(entry);
  1327. if (sec < a->newer_mtime_sec)
  1328. return (1); /* Too old, skip it. */
  1329. if (sec == a->newer_mtime_sec) {
  1330. nsec = archive_entry_mtime_nsec(entry);
  1331. if (nsec < a->newer_mtime_nsec)
  1332. return (1); /* Too old, skip it. */
  1333. if (nsec == a->newer_mtime_nsec &&
  1334. (a->newer_mtime_filter & ARCHIVE_MATCH_EQUAL)
  1335. == 0)
  1336. return (1); /* Equal, skip it. */
  1337. }
  1338. }
  1339. if (a->older_mtime_filter) {
  1340. sec = archive_entry_mtime(entry);
  1341. if (sec > a->older_mtime_sec)
  1342. return (1); /* Too new, skip it. */
  1343. nsec = archive_entry_mtime_nsec(entry);
  1344. if (sec == a->older_mtime_sec) {
  1345. if (nsec > a->older_mtime_nsec)
  1346. return (1); /* Too new, skip it. */
  1347. if (nsec == a->older_mtime_nsec &&
  1348. (a->older_mtime_filter & ARCHIVE_MATCH_EQUAL)
  1349. == 0)
  1350. return (1); /* Equal, skip it. */
  1351. }
  1352. }
  1353. /* If there is no exclusion list, include the file. */
  1354. if (a->exclusion_entry_list.first == NULL)
  1355. return (0);
  1356. #if defined(_WIN32) && !defined(__CYGWIN__)
  1357. pathname = archive_entry_pathname_w(entry);
  1358. #else
  1359. pathname = archive_entry_pathname(entry);
  1360. #endif
  1361. if (pathname == NULL)
  1362. return (0);
  1363. f = (struct match_file *)__archive_rb_tree_find_node(
  1364. &(a->exclusion_tree), pathname);
  1365. /* If the file wasn't rejected, include it. */
  1366. if (f == NULL)
  1367. return (0);
  1368. if (f->flag & ARCHIVE_MATCH_CTIME) {
  1369. sec = archive_entry_ctime(entry);
  1370. if (f->ctime_sec > sec) {
  1371. if (f->flag & ARCHIVE_MATCH_OLDER)
  1372. return (1);
  1373. } else if (f->ctime_sec < sec) {
  1374. if (f->flag & ARCHIVE_MATCH_NEWER)
  1375. return (1);
  1376. } else {
  1377. nsec = archive_entry_ctime_nsec(entry);
  1378. if (f->ctime_nsec > nsec) {
  1379. if (f->flag & ARCHIVE_MATCH_OLDER)
  1380. return (1);
  1381. } else if (f->ctime_nsec < nsec) {
  1382. if (f->flag & ARCHIVE_MATCH_NEWER)
  1383. return (1);
  1384. } else if (f->flag & ARCHIVE_MATCH_EQUAL)
  1385. return (1);
  1386. }
  1387. }
  1388. if (f->flag & ARCHIVE_MATCH_MTIME) {
  1389. sec = archive_entry_mtime(entry);
  1390. if (f->mtime_sec > sec) {
  1391. if (f->flag & ARCHIVE_MATCH_OLDER)
  1392. return (1);
  1393. } else if (f->mtime_sec < sec) {
  1394. if (f->flag & ARCHIVE_MATCH_NEWER)
  1395. return (1);
  1396. } else {
  1397. nsec = archive_entry_mtime_nsec(entry);
  1398. if (f->mtime_nsec > nsec) {
  1399. if (f->flag & ARCHIVE_MATCH_OLDER)
  1400. return (1);
  1401. } else if (f->mtime_nsec < nsec) {
  1402. if (f->flag & ARCHIVE_MATCH_NEWER)
  1403. return (1);
  1404. } else if (f->flag & ARCHIVE_MATCH_EQUAL)
  1405. return (1);
  1406. }
  1407. }
  1408. return (0);
  1409. }
  1410. /*
  1411. * Utility functions to manage inclusion owners
  1412. */
  1413. int
  1414. archive_match_include_uid(struct archive *_a, la_int64_t uid)
  1415. {
  1416. struct archive_match *a;
  1417. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1418. ARCHIVE_STATE_NEW, "archive_match_include_uid");
  1419. a = (struct archive_match *)_a;
  1420. return (add_owner_id(a, &(a->inclusion_uids), uid));
  1421. }
  1422. int
  1423. archive_match_include_gid(struct archive *_a, la_int64_t gid)
  1424. {
  1425. struct archive_match *a;
  1426. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1427. ARCHIVE_STATE_NEW, "archive_match_include_gid");
  1428. a = (struct archive_match *)_a;
  1429. return (add_owner_id(a, &(a->inclusion_gids), gid));
  1430. }
  1431. int
  1432. archive_match_include_uname(struct archive *_a, const char *uname)
  1433. {
  1434. struct archive_match *a;
  1435. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1436. ARCHIVE_STATE_NEW, "archive_match_include_uname");
  1437. a = (struct archive_match *)_a;
  1438. return (add_owner_name(a, &(a->inclusion_unames), 1, uname));
  1439. }
  1440. int
  1441. archive_match_include_uname_w(struct archive *_a, const wchar_t *uname)
  1442. {
  1443. struct archive_match *a;
  1444. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1445. ARCHIVE_STATE_NEW, "archive_match_include_uname_w");
  1446. a = (struct archive_match *)_a;
  1447. return (add_owner_name(a, &(a->inclusion_unames), 0, uname));
  1448. }
  1449. int
  1450. archive_match_include_gname(struct archive *_a, const char *gname)
  1451. {
  1452. struct archive_match *a;
  1453. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1454. ARCHIVE_STATE_NEW, "archive_match_include_gname");
  1455. a = (struct archive_match *)_a;
  1456. return (add_owner_name(a, &(a->inclusion_gnames), 1, gname));
  1457. }
  1458. int
  1459. archive_match_include_gname_w(struct archive *_a, const wchar_t *gname)
  1460. {
  1461. struct archive_match *a;
  1462. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1463. ARCHIVE_STATE_NEW, "archive_match_include_gname_w");
  1464. a = (struct archive_match *)_a;
  1465. return (add_owner_name(a, &(a->inclusion_gnames), 0, gname));
  1466. }
  1467. /*
  1468. * Test function for owner(uid, gid, uname, gname).
  1469. *
  1470. * Returns 1 if archive entry is excluded.
  1471. * Returns 0 if archive entry is not excluded.
  1472. * Returns <0 if something error happened.
  1473. */
  1474. int
  1475. archive_match_owner_excluded(struct archive *_a,
  1476. struct archive_entry *entry)
  1477. {
  1478. struct archive_match *a;
  1479. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  1480. ARCHIVE_STATE_NEW, "archive_match_id_excluded_ae");
  1481. a = (struct archive_match *)_a;
  1482. if (entry == NULL) {
  1483. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  1484. return (ARCHIVE_FAILED);
  1485. }
  1486. /* If we don't have inclusion id set at all, the entry is always
  1487. * not excluded. */
  1488. if ((a->setflag & ID_IS_SET) == 0)
  1489. return (0);
  1490. return (owner_excluded(a, entry));
  1491. }
  1492. static int
  1493. add_owner_id(struct archive_match *a, struct id_array *ids, int64_t id)
  1494. {
  1495. size_t i;
  1496. if (ids->count + 1 >= ids->size) {
  1497. void *p;
  1498. if (ids->size == 0)
  1499. ids->size = 8;
  1500. else
  1501. ids->size *= 2;
  1502. p = realloc(ids->ids, sizeof(*ids->ids) * ids->size);
  1503. if (p == NULL)
  1504. return (error_nomem(a));
  1505. ids->ids = (int64_t *)p;
  1506. }
  1507. /* Find an insert point. */
  1508. for (i = 0; i < ids->count; i++) {
  1509. if (ids->ids[i] >= id)
  1510. break;
  1511. }
  1512. /* Add owner id. */
  1513. if (i == ids->count)
  1514. ids->ids[ids->count++] = id;
  1515. else if (ids->ids[i] != id) {
  1516. memmove(&(ids->ids[i+1]), &(ids->ids[i]),
  1517. (ids->count - i) * sizeof(ids->ids[0]));
  1518. ids->ids[i] = id;
  1519. ids->count++;
  1520. }
  1521. a->setflag |= ID_IS_SET;
  1522. return (ARCHIVE_OK);
  1523. }
  1524. static int
  1525. match_owner_id(struct id_array *ids, int64_t id)
  1526. {
  1527. size_t b, m, t;
  1528. t = 0;
  1529. b = ids->count;
  1530. while (t < b) {
  1531. m = (t + b)>>1;
  1532. if (ids->ids[m] == id)
  1533. return (1);
  1534. if (ids->ids[m] < id)
  1535. t = m + 1;
  1536. else
  1537. b = m;
  1538. }
  1539. return (0);
  1540. }
  1541. static int
  1542. add_owner_name(struct archive_match *a, struct match_list *list,
  1543. int mbs, const void *name)
  1544. {
  1545. struct match *match;
  1546. match = calloc(1, sizeof(*match));
  1547. if (match == NULL)
  1548. return (error_nomem(a));
  1549. if (mbs)
  1550. archive_mstring_copy_mbs(&(match->pattern), name);
  1551. else
  1552. archive_mstring_copy_wcs(&(match->pattern), name);
  1553. match_list_add(list, match);
  1554. a->setflag |= ID_IS_SET;
  1555. return (ARCHIVE_OK);
  1556. }
  1557. #if !defined(_WIN32) || defined(__CYGWIN__)
  1558. static int
  1559. match_owner_name_mbs(struct archive_match *a, struct match_list *list,
  1560. const char *name)
  1561. {
  1562. struct match *m;
  1563. const char *p;
  1564. if (name == NULL || *name == '\0')
  1565. return (0);
  1566. for (m = list->first; m; m = m->next) {
  1567. if (archive_mstring_get_mbs(&(a->archive), &(m->pattern), &p)
  1568. < 0 && errno == ENOMEM)
  1569. return (error_nomem(a));
  1570. if (p != NULL && strcmp(p, name) == 0) {
  1571. m->matched = 1;
  1572. return (1);
  1573. }
  1574. }
  1575. return (0);
  1576. }
  1577. #else
  1578. static int
  1579. match_owner_name_wcs(struct archive_match *a, struct match_list *list,
  1580. const wchar_t *name)
  1581. {
  1582. struct match *m;
  1583. const wchar_t *p;
  1584. if (name == NULL || *name == L'\0')
  1585. return (0);
  1586. for (m = list->first; m; m = m->next) {
  1587. if (archive_mstring_get_wcs(&(a->archive), &(m->pattern), &p)
  1588. < 0 && errno == ENOMEM)
  1589. return (error_nomem(a));
  1590. if (p != NULL && wcscmp(p, name) == 0) {
  1591. m->matched = 1;
  1592. return (1);
  1593. }
  1594. }
  1595. return (0);
  1596. }
  1597. #endif
  1598. /*
  1599. * Test if entry is excluded by uid, gid, uname or gname.
  1600. */
  1601. static int
  1602. owner_excluded(struct archive_match *a, struct archive_entry *entry)
  1603. {
  1604. int r;
  1605. if (a->inclusion_uids.count) {
  1606. if (!match_owner_id(&(a->inclusion_uids),
  1607. archive_entry_uid(entry)))
  1608. return (1);
  1609. }
  1610. if (a->inclusion_gids.count) {
  1611. if (!match_owner_id(&(a->inclusion_gids),
  1612. archive_entry_gid(entry)))
  1613. return (1);
  1614. }
  1615. if (a->inclusion_unames.first != NULL) {
  1616. #if defined(_WIN32) && !defined(__CYGWIN__)
  1617. r = match_owner_name_wcs(a, &(a->inclusion_unames),
  1618. archive_entry_uname_w(entry));
  1619. #else
  1620. r = match_owner_name_mbs(a, &(a->inclusion_unames),
  1621. archive_entry_uname(entry));
  1622. #endif
  1623. if (!r)
  1624. return (1);
  1625. else if (r < 0)
  1626. return (r);
  1627. }
  1628. if (a->inclusion_gnames.first != NULL) {
  1629. #if defined(_WIN32) && !defined(__CYGWIN__)
  1630. r = match_owner_name_wcs(a, &(a->inclusion_gnames),
  1631. archive_entry_gname_w(entry));
  1632. #else
  1633. r = match_owner_name_mbs(a, &(a->inclusion_gnames),
  1634. archive_entry_gname(entry));
  1635. #endif
  1636. if (!r)
  1637. return (1);
  1638. else if (r < 0)
  1639. return (r);
  1640. }
  1641. return (0);
  1642. }