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. __FBSDID("$FreeBSD$");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include "archive.h"
  38. #include "archive_private.h"
  39. #include "archive_entry.h"
  40. #include "archive_getdate.h"
  41. #include "archive_pathmatch.h"
  42. #include "archive_rb.h"
  43. #include "archive_string.h"
  44. struct match {
  45. struct match *next;
  46. int matches;
  47. struct archive_mstring pattern;
  48. };
  49. struct match_list {
  50. struct match *first;
  51. struct match **last;
  52. int count;
  53. int unmatched_count;
  54. struct match *unmatched_next;
  55. int unmatched_eof;
  56. };
  57. struct match_file {
  58. struct archive_rb_node node;
  59. struct match_file *next;
  60. struct archive_mstring pathname;
  61. int flag;
  62. time_t mtime_sec;
  63. long mtime_nsec;
  64. time_t ctime_sec;
  65. long ctime_nsec;
  66. };
  67. struct entry_list {
  68. struct match_file *first;
  69. struct match_file **last;
  70. int count;
  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. /*
  85. * Matching filename patterns.
  86. */
  87. struct match_list exclusions;
  88. struct match_list inclusions;
  89. /*
  90. * Matching time stamps.
  91. */
  92. time_t now;
  93. int newer_mtime_filter;
  94. time_t newer_mtime_sec;
  95. long newer_mtime_nsec;
  96. int newer_ctime_filter;
  97. time_t newer_ctime_sec;
  98. long newer_ctime_nsec;
  99. int older_mtime_filter;
  100. time_t older_mtime_sec;
  101. long older_mtime_nsec;
  102. int older_ctime_filter;
  103. time_t older_ctime_sec;
  104. long older_ctime_nsec;
  105. /*
  106. * Matching time stamps with its filename.
  107. */
  108. struct archive_rb_tree exclusion_tree;
  109. struct entry_list exclusion_entry_list;
  110. /*
  111. * Matching file owners.
  112. */
  113. struct id_array inclusion_uids;
  114. struct id_array inclusion_gids;
  115. struct match_list inclusion_unames;
  116. struct match_list inclusion_gnames;
  117. };
  118. static int add_pattern_from_file(struct archive_match *,
  119. struct match_list *, int, const void *, int);
  120. static int add_entry(struct archive_match *, int,
  121. struct archive_entry *);
  122. static int add_owner_id(struct archive_match *, struct id_array *,
  123. int64_t);
  124. static int add_owner_name(struct archive_match *, struct match_list *,
  125. int, const void *);
  126. static int add_pattern_mbs(struct archive_match *, struct match_list *,
  127. const char *);
  128. static int add_pattern_wcs(struct archive_match *, struct match_list *,
  129. const wchar_t *);
  130. static int cmp_key_mbs(const struct archive_rb_node *, const void *);
  131. static int cmp_key_wcs(const struct archive_rb_node *, const void *);
  132. static int cmp_node_mbs(const struct archive_rb_node *,
  133. const struct archive_rb_node *);
  134. static int cmp_node_wcs(const struct archive_rb_node *,
  135. const struct archive_rb_node *);
  136. static void entry_list_add(struct entry_list *, struct match_file *);
  137. static void entry_list_free(struct entry_list *);
  138. static void entry_list_init(struct entry_list *);
  139. static int error_nomem(struct archive_match *);
  140. static void match_list_add(struct match_list *, struct match *);
  141. static void match_list_free(struct match_list *);
  142. static void match_list_init(struct match_list *);
  143. static int match_list_unmatched_inclusions_next(struct archive_match *,
  144. struct match_list *, int, const void **);
  145. static int match_owner_id(struct id_array *, int64_t);
  146. #if !defined(_WIN32) || defined(__CYGWIN__)
  147. static int match_owner_name_mbs(struct archive_match *,
  148. struct match_list *, const char *);
  149. #else
  150. static int match_owner_name_wcs(struct archive_match *,
  151. struct match_list *, const wchar_t *);
  152. #endif
  153. static int match_path_exclusion(struct archive_match *,
  154. struct match *, int, const void *);
  155. static int match_path_inclusion(struct archive_match *,
  156. struct match *, int, const void *);
  157. static int owner_excluded(struct archive_match *,
  158. struct archive_entry *);
  159. static int path_excluded(struct archive_match *, int, const void *);
  160. static int set_timefilter(struct archive_match *, int, time_t, long,
  161. time_t, long);
  162. static int set_timefilter_pathname_mbs(struct archive_match *,
  163. int, const char *);
  164. static int set_timefilter_pathname_wcs(struct archive_match *,
  165. int, const wchar_t *);
  166. static int set_timefilter_date(struct archive_match *, int, const char *);
  167. static int set_timefilter_date_w(struct archive_match *, int,
  168. const wchar_t *);
  169. static int time_excluded(struct archive_match *,
  170. struct archive_entry *);
  171. static int validate_time_flag(struct archive *, int, const char *);
  172. #define get_date __archive_get_date
  173. static const struct archive_rb_tree_ops rb_ops_mbs = {
  174. cmp_node_mbs, cmp_key_mbs
  175. };
  176. static const struct archive_rb_tree_ops rb_ops_wcs = {
  177. cmp_node_wcs, cmp_key_wcs
  178. };
  179. /*
  180. * The matching logic here needs to be re-thought. I started out to
  181. * try to mimic gtar's matching logic, but it's not entirely
  182. * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
  183. * on the command line as anchored, but --exclude doesn't.
  184. */
  185. static int
  186. error_nomem(struct archive_match *a)
  187. {
  188. archive_set_error(&(a->archive), ENOMEM, "No memory");
  189. a->archive.state = ARCHIVE_STATE_FATAL;
  190. return (ARCHIVE_FATAL);
  191. }
  192. /*
  193. * Create an ARCHIVE_MATCH object.
  194. */
  195. struct archive *
  196. archive_match_new(void)
  197. {
  198. struct archive_match *a;
  199. a = (struct archive_match *)calloc(1, sizeof(*a));
  200. if (a == NULL)
  201. return (NULL);
  202. a->archive.magic = ARCHIVE_MATCH_MAGIC;
  203. a->archive.state = ARCHIVE_STATE_NEW;
  204. match_list_init(&(a->inclusions));
  205. match_list_init(&(a->exclusions));
  206. __archive_rb_tree_init(&(a->exclusion_tree), &rb_ops_mbs);
  207. entry_list_init(&(a->exclusion_entry_list));
  208. match_list_init(&(a->inclusion_unames));
  209. match_list_init(&(a->inclusion_gnames));
  210. time(&a->now);
  211. return (&(a->archive));
  212. }
  213. /*
  214. * Free an ARCHIVE_MATCH object.
  215. */
  216. int
  217. archive_match_free(struct archive *_a)
  218. {
  219. struct archive_match *a;
  220. if (_a == NULL)
  221. return (ARCHIVE_OK);
  222. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  223. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_match_free");
  224. a = (struct archive_match *)_a;
  225. match_list_free(&(a->inclusions));
  226. match_list_free(&(a->exclusions));
  227. entry_list_free(&(a->exclusion_entry_list));
  228. free(a->inclusion_uids.ids);
  229. free(a->inclusion_gids.ids);
  230. match_list_free(&(a->inclusion_unames));
  231. match_list_free(&(a->inclusion_gnames));
  232. free(a);
  233. return (ARCHIVE_OK);
  234. }
  235. /*
  236. * Convenience function to perform all exclusion tests.
  237. *
  238. * Returns 1 if archive entry is excluded.
  239. * Returns 0 if archive entry is not excluded.
  240. * Returns <0 if something error happened.
  241. */
  242. int
  243. archive_match_excluded(struct archive *_a, struct archive_entry *entry)
  244. {
  245. struct archive_match *a;
  246. int r;
  247. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  248. ARCHIVE_STATE_NEW, "archive_match_excluded_ae");
  249. a = (struct archive_match *)_a;
  250. if (entry == NULL) {
  251. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  252. return (ARCHIVE_FAILED);
  253. }
  254. r = 0;
  255. if (a->setflag & PATTERN_IS_SET) {
  256. #if defined(_WIN32) && !defined(__CYGWIN__)
  257. r = path_excluded(a, 0, archive_entry_pathname_w(entry));
  258. #else
  259. r = path_excluded(a, 1, archive_entry_pathname(entry));
  260. #endif
  261. if (r != 0)
  262. return (r);
  263. }
  264. if (a->setflag & TIME_IS_SET) {
  265. r = time_excluded(a, entry);
  266. if (r != 0)
  267. return (r);
  268. }
  269. if (a->setflag & ID_IS_SET)
  270. r = owner_excluded(a, entry);
  271. return (r);
  272. }
  273. /*
  274. * Utility functions to manage exclusion/inclusion patterns
  275. */
  276. int
  277. archive_match_exclude_pattern(struct archive *_a, const char *pattern)
  278. {
  279. struct archive_match *a;
  280. int r;
  281. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  282. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern");
  283. a = (struct archive_match *)_a;
  284. if (pattern == NULL || *pattern == '\0') {
  285. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  286. return (ARCHIVE_FAILED);
  287. }
  288. if ((r = add_pattern_mbs(a, &(a->exclusions), pattern)) != ARCHIVE_OK)
  289. return (r);
  290. return (ARCHIVE_OK);
  291. }
  292. int
  293. archive_match_exclude_pattern_w(struct archive *_a, const wchar_t *pattern)
  294. {
  295. struct archive_match *a;
  296. int r;
  297. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  298. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_w");
  299. a = (struct archive_match *)_a;
  300. if (pattern == NULL || *pattern == L'\0') {
  301. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  302. return (ARCHIVE_FAILED);
  303. }
  304. if ((r = add_pattern_wcs(a, &(a->exclusions), pattern)) != ARCHIVE_OK)
  305. return (r);
  306. return (ARCHIVE_OK);
  307. }
  308. int
  309. archive_match_exclude_pattern_from_file(struct archive *_a,
  310. const char *pathname, int nullSeparator)
  311. {
  312. struct archive_match *a;
  313. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  314. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file");
  315. a = (struct archive_match *)_a;
  316. return add_pattern_from_file(a, &(a->exclusions), 1, pathname,
  317. nullSeparator);
  318. }
  319. int
  320. archive_match_exclude_pattern_from_file_w(struct archive *_a,
  321. const wchar_t *pathname, int nullSeparator)
  322. {
  323. struct archive_match *a;
  324. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  325. ARCHIVE_STATE_NEW, "archive_match_exclude_pattern_from_file_w");
  326. a = (struct archive_match *)_a;
  327. return add_pattern_from_file(a, &(a->exclusions), 0, pathname,
  328. nullSeparator);
  329. }
  330. int
  331. archive_match_include_pattern(struct archive *_a, const char *pattern)
  332. {
  333. struct archive_match *a;
  334. int r;
  335. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  336. ARCHIVE_STATE_NEW, "archive_match_include_pattern");
  337. a = (struct archive_match *)_a;
  338. if (pattern == NULL || *pattern == '\0') {
  339. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  340. return (ARCHIVE_FAILED);
  341. }
  342. if ((r = add_pattern_mbs(a, &(a->inclusions), pattern)) != ARCHIVE_OK)
  343. return (r);
  344. return (ARCHIVE_OK);
  345. }
  346. int
  347. archive_match_include_pattern_w(struct archive *_a, const wchar_t *pattern)
  348. {
  349. struct archive_match *a;
  350. int r;
  351. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  352. ARCHIVE_STATE_NEW, "archive_match_include_pattern_w");
  353. a = (struct archive_match *)_a;
  354. if (pattern == NULL || *pattern == L'\0') {
  355. archive_set_error(&(a->archive), EINVAL, "pattern is empty");
  356. return (ARCHIVE_FAILED);
  357. }
  358. if ((r = add_pattern_wcs(a, &(a->inclusions), pattern)) != ARCHIVE_OK)
  359. return (r);
  360. return (ARCHIVE_OK);
  361. }
  362. int
  363. archive_match_include_pattern_from_file(struct archive *_a,
  364. const char *pathname, int nullSeparator)
  365. {
  366. struct archive_match *a;
  367. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  368. ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file");
  369. a = (struct archive_match *)_a;
  370. return add_pattern_from_file(a, &(a->inclusions), 1, pathname,
  371. nullSeparator);
  372. }
  373. int
  374. archive_match_include_pattern_from_file_w(struct archive *_a,
  375. const wchar_t *pathname, int nullSeparator)
  376. {
  377. struct archive_match *a;
  378. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  379. ARCHIVE_STATE_NEW, "archive_match_include_pattern_from_file_w");
  380. a = (struct archive_match *)_a;
  381. return add_pattern_from_file(a, &(a->inclusions), 0, pathname,
  382. nullSeparator);
  383. }
  384. /*
  385. * Test functions for pathname patterns.
  386. *
  387. * Returns 1 if archive entry is excluded.
  388. * Returns 0 if archive entry is not excluded.
  389. * Returns <0 if something error happened.
  390. */
  391. int
  392. archive_match_path_excluded(struct archive *_a,
  393. struct archive_entry *entry)
  394. {
  395. struct archive_match *a;
  396. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  397. ARCHIVE_STATE_NEW, "archive_match_path_excluded");
  398. a = (struct archive_match *)_a;
  399. if (entry == NULL) {
  400. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  401. return (ARCHIVE_FAILED);
  402. }
  403. /* If we don't have exclusion/inclusion pattern set at all,
  404. * the entry is always not excluded. */
  405. if ((a->setflag & PATTERN_IS_SET) == 0)
  406. return (0);
  407. #if defined(_WIN32) && !defined(__CYGWIN__)
  408. return (path_excluded(a, 0, archive_entry_pathname_w(entry)));
  409. #else
  410. return (path_excluded(a, 1, archive_entry_pathname(entry)));
  411. #endif
  412. }
  413. /*
  414. * Utility functions to get statistic information for inclusion patterns.
  415. */
  416. int
  417. archive_match_path_unmatched_inclusions(struct archive *_a)
  418. {
  419. struct archive_match *a;
  420. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  421. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions");
  422. a = (struct archive_match *)_a;
  423. return (a->inclusions.unmatched_count);
  424. }
  425. int
  426. archive_match_path_unmatched_inclusions_next(struct archive *_a,
  427. const char **_p)
  428. {
  429. struct archive_match *a;
  430. const void *v;
  431. int r;
  432. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  433. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions_next");
  434. a = (struct archive_match *)_a;
  435. r = match_list_unmatched_inclusions_next(a, &(a->inclusions), 1, &v);
  436. *_p = (const char *)v;
  437. return (r);
  438. }
  439. int
  440. archive_match_path_unmatched_inclusions_next_w(struct archive *_a,
  441. const wchar_t **_p)
  442. {
  443. struct archive_match *a;
  444. const void *v;
  445. int r;
  446. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  447. ARCHIVE_STATE_NEW, "archive_match_unmatched_inclusions_next_w");
  448. a = (struct archive_match *)_a;
  449. r = match_list_unmatched_inclusions_next(a, &(a->inclusions), 0, &v);
  450. *_p = (const wchar_t *)v;
  451. return (r);
  452. }
  453. /*
  454. * Add inclusion/exclusion patterns.
  455. */
  456. static int
  457. add_pattern_mbs(struct archive_match *a, struct match_list *list,
  458. const char *pattern)
  459. {
  460. struct match *match;
  461. size_t len;
  462. match = calloc(1, sizeof(*match));
  463. if (match == NULL)
  464. return (error_nomem(a));
  465. /* Both "foo/" and "foo" should match "foo/bar". */
  466. len = strlen(pattern);
  467. if (len && pattern[len - 1] == '/')
  468. --len;
  469. archive_mstring_copy_mbs_len(&(match->pattern), pattern, len);
  470. match_list_add(list, match);
  471. a->setflag |= PATTERN_IS_SET;
  472. return (ARCHIVE_OK);
  473. }
  474. static int
  475. add_pattern_wcs(struct archive_match *a, struct match_list *list,
  476. const wchar_t *pattern)
  477. {
  478. struct match *match;
  479. size_t len;
  480. match = calloc(1, sizeof(*match));
  481. if (match == NULL)
  482. return (error_nomem(a));
  483. /* Both "foo/" and "foo" should match "foo/bar". */
  484. len = wcslen(pattern);
  485. if (len && pattern[len - 1] == L'/')
  486. --len;
  487. archive_mstring_copy_wcs_len(&(match->pattern), pattern, len);
  488. match_list_add(list, match);
  489. a->setflag |= PATTERN_IS_SET;
  490. return (ARCHIVE_OK);
  491. }
  492. static int
  493. add_pattern_from_file(struct archive_match *a, struct match_list *mlist,
  494. int mbs, const void *pathname, int nullSeparator)
  495. {
  496. struct archive *ar;
  497. struct archive_entry *ae;
  498. struct archive_string as;
  499. const void *buff;
  500. size_t size;
  501. int64_t offset;
  502. int r;
  503. ar = archive_read_new();
  504. if (ar == NULL) {
  505. archive_set_error(&(a->archive), ENOMEM, "No memory");
  506. return (ARCHIVE_FATAL);
  507. }
  508. r = archive_read_support_format_raw(ar);
  509. r = archive_read_support_format_empty(ar);
  510. if (r != ARCHIVE_OK) {
  511. archive_copy_error(&(a->archive), ar);
  512. archive_read_free(ar);
  513. return (r);
  514. }
  515. if (mbs)
  516. r = archive_read_open_filename(ar, pathname, 512*20);
  517. else
  518. r = archive_read_open_filename_w(ar, pathname, 512*20);
  519. if (r != ARCHIVE_OK) {
  520. archive_copy_error(&(a->archive), ar);
  521. archive_read_free(ar);
  522. return (r);
  523. }
  524. r = archive_read_next_header(ar, &ae);
  525. if (r != ARCHIVE_OK) {
  526. archive_read_free(ar);
  527. if (r == ARCHIVE_EOF) {
  528. return (ARCHIVE_OK);
  529. } else {
  530. archive_copy_error(&(a->archive), ar);
  531. return (r);
  532. }
  533. }
  534. archive_string_init(&as);
  535. while ((r = archive_read_data_block(ar, &buff, &size, &offset))
  536. == ARCHIVE_OK) {
  537. const char *b = (const char *)buff;
  538. while (size) {
  539. const char *s = (const char *)b;
  540. size_t length = 0;
  541. int found_separator = 0;
  542. while (length < size) {
  543. if (nullSeparator) {
  544. if (*b == '\0') {
  545. found_separator = 1;
  546. break;
  547. }
  548. } else {
  549. if (*b == 0x0d || *b == 0x0a) {
  550. found_separator = 1;
  551. break;
  552. }
  553. }
  554. b++;
  555. length++;
  556. }
  557. if (!found_separator) {
  558. archive_strncat(&as, s, length);
  559. /* Read next data block. */
  560. break;
  561. }
  562. b++;
  563. size -= length + 1;
  564. archive_strncat(&as, s, length);
  565. /* If the line is not empty, add the pattern. */
  566. if (archive_strlen(&as) > 0) {
  567. /* Add pattern. */
  568. r = add_pattern_mbs(a, mlist, as.s);
  569. if (r != ARCHIVE_OK) {
  570. archive_read_free(ar);
  571. archive_string_free(&as);
  572. return (r);
  573. }
  574. archive_string_empty(&as);
  575. }
  576. }
  577. }
  578. /* If an error occurred, report it immediately. */
  579. if (r < ARCHIVE_OK) {
  580. archive_copy_error(&(a->archive), ar);
  581. archive_read_free(ar);
  582. archive_string_free(&as);
  583. return (r);
  584. }
  585. /* If the line is not empty, add the pattern. */
  586. if (r == ARCHIVE_EOF && archive_strlen(&as) > 0) {
  587. /* Add pattern. */
  588. r = add_pattern_mbs(a, mlist, as.s);
  589. if (r != ARCHIVE_OK) {
  590. archive_read_free(ar);
  591. archive_string_free(&as);
  592. return (r);
  593. }
  594. }
  595. archive_read_free(ar);
  596. archive_string_free(&as);
  597. return (ARCHIVE_OK);
  598. }
  599. /*
  600. * Test if pathname is excluded by inclusion/exclusion patterns.
  601. */
  602. static int
  603. path_excluded(struct archive_match *a, int mbs, const void *pathname)
  604. {
  605. struct match *match;
  606. struct match *matched;
  607. int r;
  608. if (a == NULL)
  609. return (0);
  610. /* Mark off any unmatched inclusions. */
  611. /* In particular, if a filename does appear in the archive and
  612. * is explicitly included and excluded, then we don't report
  613. * it as missing even though we don't extract it.
  614. */
  615. matched = NULL;
  616. for (match = a->inclusions.first; match != NULL;
  617. match = match->next){
  618. if (match->matches == 0 &&
  619. (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
  620. if (r < 0)
  621. return (r);
  622. a->inclusions.unmatched_count--;
  623. match->matches++;
  624. matched = match;
  625. }
  626. }
  627. /* Exclusions take priority */
  628. for (match = a->exclusions.first; match != NULL;
  629. match = match->next){
  630. r = match_path_exclusion(a, match, mbs, pathname);
  631. if (r)
  632. return (r);
  633. }
  634. /* It's not excluded and we found an inclusion above, so it's
  635. * included. */
  636. if (matched != NULL)
  637. return (0);
  638. /* We didn't find an unmatched inclusion, check the remaining ones. */
  639. for (match = a->inclusions.first; match != NULL;
  640. match = match->next){
  641. /* We looked at previously-unmatched inclusions already. */
  642. if (match->matches > 0 &&
  643. (r = match_path_inclusion(a, match, mbs, pathname)) != 0) {
  644. if (r < 0)
  645. return (r);
  646. match->matches++;
  647. return (0);
  648. }
  649. }
  650. /* If there were inclusions, default is to exclude. */
  651. if (a->inclusions.first != NULL)
  652. return (1);
  653. /* No explicit inclusions, default is to match. */
  654. return (0);
  655. }
  656. /*
  657. * This is a little odd, but it matches the default behavior of
  658. * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
  659. *
  660. */
  661. static int
  662. match_path_exclusion(struct archive_match *a, struct match *m,
  663. int mbs, const void *pn)
  664. {
  665. int flag = PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END;
  666. int r;
  667. if (mbs) {
  668. const char *p;
  669. r = archive_mstring_get_mbs(&(a->archive), &(m->pattern), &p);
  670. if (r == 0)
  671. return (archive_pathmatch(p, (const char *)pn, flag));
  672. } else {
  673. const wchar_t *p;
  674. r = archive_mstring_get_wcs(&(a->archive), &(m->pattern), &p);
  675. if (r == 0)
  676. return (archive_pathmatch_w(p, (const wchar_t *)pn,
  677. flag));
  678. }
  679. if (errno == ENOMEM)
  680. return (error_nomem(a));
  681. return (0);
  682. }
  683. /*
  684. * Again, mimic gtar: inclusions are always anchored (have to match
  685. * the beginning of the path) even though exclusions are not anchored.
  686. */
  687. static int
  688. match_path_inclusion(struct archive_match *a, struct match *m,
  689. int mbs, const void *pn)
  690. {
  691. int flag = PATHMATCH_NO_ANCHOR_END;
  692. int r;
  693. if (mbs) {
  694. const char *p;
  695. r = archive_mstring_get_mbs(&(a->archive), &(m->pattern), &p);
  696. if (r == 0)
  697. return (archive_pathmatch(p, (const char *)pn, flag));
  698. } else {
  699. const wchar_t *p;
  700. r = archive_mstring_get_wcs(&(a->archive), &(m->pattern), &p);
  701. if (r == 0)
  702. return (archive_pathmatch_w(p, (const wchar_t *)pn,
  703. flag));
  704. }
  705. if (errno == ENOMEM)
  706. return (error_nomem(a));
  707. return (0);
  708. }
  709. static void
  710. match_list_init(struct match_list *list)
  711. {
  712. list->first = NULL;
  713. list->last = &(list->first);
  714. list->count = 0;
  715. }
  716. static void
  717. match_list_free(struct match_list *list)
  718. {
  719. struct match *p, *q;
  720. for (p = list->first; p != NULL; ) {
  721. q = p;
  722. p = p->next;
  723. archive_mstring_clean(&(q->pattern));
  724. free(q);
  725. }
  726. }
  727. static void
  728. match_list_add(struct match_list *list, struct match *m)
  729. {
  730. *list->last = m;
  731. list->last = &(m->next);
  732. list->count++;
  733. list->unmatched_count++;
  734. }
  735. static int
  736. match_list_unmatched_inclusions_next(struct archive_match *a,
  737. struct match_list *list, int mbs, const void **vp)
  738. {
  739. struct match *m;
  740. *vp = NULL;
  741. if (list->unmatched_eof) {
  742. list->unmatched_eof = 0;
  743. return (ARCHIVE_EOF);
  744. }
  745. if (list->unmatched_next == NULL) {
  746. if (list->unmatched_count == 0)
  747. return (ARCHIVE_EOF);
  748. list->unmatched_next = list->first;
  749. }
  750. for (m = list->unmatched_next; m != NULL; m = m->next) {
  751. int r;
  752. if (m->matches)
  753. continue;
  754. if (mbs) {
  755. const char *p;
  756. r = archive_mstring_get_mbs(&(a->archive),
  757. &(m->pattern), &p);
  758. if (r < 0 && errno == ENOMEM)
  759. return (error_nomem(a));
  760. if (p == NULL)
  761. p = "";
  762. *vp = p;
  763. } else {
  764. const wchar_t *p;
  765. r = archive_mstring_get_wcs(&(a->archive),
  766. &(m->pattern), &p);
  767. if (r < 0 && errno == ENOMEM)
  768. return (error_nomem(a));
  769. if (p == NULL)
  770. p = L"";
  771. *vp = p;
  772. }
  773. list->unmatched_next = m->next;
  774. if (list->unmatched_next == NULL)
  775. /* To return EOF next time. */
  776. list->unmatched_eof = 1;
  777. return (ARCHIVE_OK);
  778. }
  779. list->unmatched_next = NULL;
  780. return (ARCHIVE_EOF);
  781. }
  782. /*
  783. * Utility functions to manage inclusion timestamps.
  784. */
  785. int
  786. archive_match_include_time(struct archive *_a, int flag, time_t sec,
  787. long nsec)
  788. {
  789. int r;
  790. r = validate_time_flag(_a, flag, "archive_match_include_time");
  791. if (r != ARCHIVE_OK)
  792. return (r);
  793. return set_timefilter((struct archive_match *)_a, flag,
  794. sec, nsec, sec, nsec);
  795. }
  796. int
  797. archive_match_include_date(struct archive *_a, int flag,
  798. const char *datestr)
  799. {
  800. int r;
  801. r = validate_time_flag(_a, flag, "archive_match_include_date");
  802. if (r != ARCHIVE_OK)
  803. return (r);
  804. return set_timefilter_date((struct archive_match *)_a, flag, datestr);
  805. }
  806. int
  807. archive_match_include_date_w(struct archive *_a, int flag,
  808. const wchar_t *datestr)
  809. {
  810. int r;
  811. r = validate_time_flag(_a, flag, "archive_match_include_date_w");
  812. if (r != ARCHIVE_OK)
  813. return (r);
  814. return set_timefilter_date_w((struct archive_match *)_a, flag, datestr);
  815. }
  816. int
  817. archive_match_include_file_time(struct archive *_a, int flag,
  818. const char *pathname)
  819. {
  820. int r;
  821. r = validate_time_flag(_a, flag, "archive_match_include_file_time");
  822. if (r != ARCHIVE_OK)
  823. return (r);
  824. return set_timefilter_pathname_mbs((struct archive_match *)_a,
  825. flag, pathname);
  826. }
  827. int
  828. archive_match_include_file_time_w(struct archive *_a, int flag,
  829. const wchar_t *pathname)
  830. {
  831. int r;
  832. r = validate_time_flag(_a, flag, "archive_match_include_file_time_w");
  833. if (r != ARCHIVE_OK)
  834. return (r);
  835. return set_timefilter_pathname_wcs((struct archive_match *)_a,
  836. flag, pathname);
  837. }
  838. int
  839. archive_match_exclude_entry(struct archive *_a, int flag,
  840. struct archive_entry *entry)
  841. {
  842. struct archive_match *a;
  843. int r;
  844. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  845. ARCHIVE_STATE_NEW, "archive_match_time_include_entry");
  846. a = (struct archive_match *)_a;
  847. if (entry == NULL) {
  848. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  849. return (ARCHIVE_FAILED);
  850. }
  851. r = validate_time_flag(_a, flag, "archive_match_exclude_entry");
  852. if (r != ARCHIVE_OK)
  853. return (r);
  854. return (add_entry(a, flag, entry));
  855. }
  856. /*
  857. * Test function for time stamps.
  858. *
  859. * Returns 1 if archive entry is excluded.
  860. * Returns 0 if archive entry is not excluded.
  861. * Returns <0 if something error happened.
  862. */
  863. int
  864. archive_match_time_excluded(struct archive *_a,
  865. struct archive_entry *entry)
  866. {
  867. struct archive_match *a;
  868. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  869. ARCHIVE_STATE_NEW, "archive_match_time_excluded_ae");
  870. a = (struct archive_match *)_a;
  871. if (entry == NULL) {
  872. archive_set_error(&(a->archive), EINVAL, "entry is NULL");
  873. return (ARCHIVE_FAILED);
  874. }
  875. /* If we don't have inclusion time set at all, the entry is always
  876. * not excluded. */
  877. if ((a->setflag & TIME_IS_SET) == 0)
  878. return (0);
  879. return (time_excluded(a, entry));
  880. }
  881. static int
  882. validate_time_flag(struct archive *_a, int flag, const char *_fn)
  883. {
  884. archive_check_magic(_a, ARCHIVE_MATCH_MAGIC,
  885. ARCHIVE_STATE_NEW, _fn);
  886. /* Check a type of time. */
  887. if (flag &
  888. ((~(ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME)) & 0xff00)) {
  889. archive_set_error(_a, EINVAL, "Invalid time flag");
  890. return (ARCHIVE_FAILED);
  891. }
  892. if ((flag & (ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_CTIME)) == 0) {
  893. archive_set_error(_a, EINVAL, "No time flag");
  894. return (ARCHIVE_FAILED);
  895. }
  896. /* Check a type of comparison. */
  897. if (flag &
  898. ((~(ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER
  899. | ARCHIVE_MATCH_EQUAL)) & 0x00ff)) {
  900. archive_set_error(_a, EINVAL, "Invalid comparison flag");
  901. return (ARCHIVE_FAILED);
  902. }
  903. if ((flag & (ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER
  904. | ARCHIVE_MATCH_EQUAL)) == 0) {
  905. archive_set_error(_a, EINVAL, "No comparison flag");
  906. return (ARCHIVE_FAILED);
  907. }
  908. return (ARCHIVE_OK);
  909. }
  910. #define JUST_EQUAL(t) (((t) & (ARCHIVE_MATCH_EQUAL |\
  911. ARCHIVE_MATCH_NEWER | ARCHIVE_MATCH_OLDER)) == ARCHIVE_MATCH_EQUAL)
  912. static int
  913. set_timefilter(struct archive_match *a, int timetype,
  914. time_t mtime_sec, long mtime_nsec, time_t ctime_sec, long ctime_nsec)
  915. {
  916. if (timetype & ARCHIVE_MATCH_MTIME) {
  917. if ((timetype & ARCHIVE_MATCH_NEWER) || JUST_EQUAL(timetype)) {
  918. a->newer_mtime_filter = timetype;
  919. a->newer_mtime_sec = mtime_sec;
  920. a->newer_mtime_nsec = mtime_nsec;
  921. a->setflag |= TIME_IS_SET;
  922. }
  923. if ((timetype & ARCHIVE_MATCH_OLDER) || JUST_EQUAL(timetype)) {
  924. a->older_mtime_filter = timetype;
  925. a->older_mtime_sec = mtime_sec;
  926. a->older_mtime_nsec = mtime_nsec;
  927. a->setflag |= TIME_IS_SET;
  928. }
  929. }
  930. if (timetype & ARCHIVE_MATCH_CTIME) {
  931. if ((timetype & ARCHIVE_MATCH_NEWER) || JUST_EQUAL(timetype)) {
  932. a->newer_ctime_filter = timetype;
  933. a->newer_ctime_sec = ctime_sec;
  934. a->newer_ctime_nsec = ctime_nsec;
  935. a->setflag |= TIME_IS_SET;
  936. }
  937. if ((timetype & ARCHIVE_MATCH_OLDER) || JUST_EQUAL(timetype)) {
  938. a->older_ctime_filter = timetype;
  939. a->older_ctime_sec = ctime_sec;
  940. a->older_ctime_nsec = ctime_nsec;
  941. a->setflag |= TIME_IS_SET;
  942. }
  943. }
  944. return (ARCHIVE_OK);
  945. }
  946. static int
  947. set_timefilter_date(struct archive_match *a, int timetype, const char *datestr)
  948. {
  949. time_t t;
  950. if (datestr == NULL || *datestr == '\0') {
  951. archive_set_error(&(a->archive), EINVAL, "date is empty");
  952. return (ARCHIVE_FAILED);
  953. }
  954. t = get_date(a->now, datestr);
  955. if (t == (time_t)-1) {
  956. archive_set_error(&(a->archive), EINVAL, "invalid date string");
  957. return (ARCHIVE_FAILED);
  958. }
  959. return set_timefilter(a, timetype, t, 0, t, 0);
  960. }
  961. static int
  962. set_timefilter_date_w(struct archive_match *a, int timetype,
  963. const wchar_t *datestr)
  964. {
  965. struct archive_string as;
  966. time_t t;
  967. if (datestr == NULL || *datestr == L'\0') {
  968. archive_set_error(&(a->archive), EINVAL, "date is empty");
  969. return (ARCHIVE_FAILED);
  970. }
  971. archive_string_init(&as);
  972. if (archive_string_append_from_wcs(&as, datestr, wcslen(datestr)) < 0) {
  973. archive_string_free(&as);
  974. if (errno == ENOMEM)
  975. return (error_nomem(a));
  976. archive_set_error(&(a->archive), -1,
  977. "Failed to convert WCS to MBS");
  978. return (ARCHIVE_FAILED);
  979. }
  980. t = get_date(a->now, as.s);
  981. archive_string_free(&as);
  982. if (t == (time_t)-1) {
  983. archive_set_error(&(a->archive), EINVAL, "invalid date string");
  984. return (ARCHIVE_FAILED);
  985. }
  986. return set_timefilter(a, timetype, t, 0, t, 0);
  987. }
  988. #if defined(_WIN32) && !defined(__CYGWIN__)
  989. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  990. static int
  991. set_timefilter_find_data(struct archive_match *a, int timetype,
  992. DWORD ftLastWriteTime_dwHighDateTime, DWORD ftLastWriteTime_dwLowDateTime,
  993. DWORD ftCreationTime_dwHighDateTime, DWORD ftCreationTime_dwLowDateTime)
  994. {
  995. ULARGE_INTEGER utc;
  996. time_t ctime_sec, mtime_sec;
  997. long ctime_ns, mtime_ns;
  998. utc.HighPart = ftCreationTime_dwHighDateTime;
  999. utc.LowPart = ftCreationTime_dwLowDateTime;
  1000. if (utc.QuadPart >= EPOC_TIME) {
  1001. utc.QuadPart -= EPOC_TIME;
  1002. ctime_sec = (time_t)(utc.QuadPart / 10000000);
  1003. ctime_ns = (long)(utc.QuadPart % 10000000) * 100;
  1004. } else {
  1005. ctime_sec = 0;
  1006. ctime_ns = 0;
  1007. }
  1008. utc.HighPart = ftLastWriteTime_dwHighDateTime;
  1009. utc.LowPart = ftLastWriteTime_dwLowDateTime;
  1010. if (utc.QuadPart >= EPOC_TIME) {
  1011. utc.QuadPart -= EPOC_TIME;
  1012. mtime_sec = (time_t)(utc.QuadPart / 10000000);
  1013. mtime_ns = (long)(utc.QuadPart % 10000000) * 100;
  1014. } else {
  1015. mtime_sec = 0;
  1016. mtime_ns = 0;
  1017. }
  1018. return set_timefilter(a, timetype,
  1019. mtime_sec, mtime_ns, ctime_sec, ctime_ns);
  1020. }
  1021. static int
  1022. set_timefilter_pathname_mbs(struct archive_match *a, int timetype,
  1023. const char *path)
  1024. {
  1025. /* NOTE: stat() on Windows cannot handle nano seconds. */
  1026. HANDLE h;
  1027. WIN32_FIND_DATAA d;
  1028. if (path == NULL || *path == '\0') {
  1029. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1030. return (ARCHIVE_FAILED);
  1031. }
  1032. h = FindFirstFileA(path, &d);
  1033. if (h == INVALID_HANDLE_VALUE) {
  1034. la_dosmaperr(GetLastError());
  1035. archive_set_error(&(a->archive), errno,
  1036. "Failed to FindFirstFileA");
  1037. return (ARCHIVE_FAILED);
  1038. }
  1039. FindClose(h);
  1040. return set_timefilter_find_data(a, timetype,
  1041. d.ftLastWriteTime.dwHighDateTime, d.ftLastWriteTime.dwLowDateTime,
  1042. d.ftCreationTime.dwHighDateTime, d.ftCreationTime.dwLowDateTime);
  1043. }
  1044. static int
  1045. set_timefilter_pathname_wcs(struct archive_match *a, int timetype,
  1046. const wchar_t *path)
  1047. {
  1048. HANDLE h;
  1049. WIN32_FIND_DATAW d;
  1050. if (path == NULL || *path == L'\0') {
  1051. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1052. return (ARCHIVE_FAILED);
  1053. }
  1054. h = FindFirstFileW(path, &d);
  1055. if (h == INVALID_HANDLE_VALUE) {
  1056. la_dosmaperr(GetLastError());
  1057. archive_set_error(&(a->archive), errno,
  1058. "Failed to FindFirstFile");
  1059. return (ARCHIVE_FAILED);
  1060. }
  1061. FindClose(h);
  1062. return set_timefilter_find_data(a, timetype,
  1063. d.ftLastWriteTime.dwHighDateTime, d.ftLastWriteTime.dwLowDateTime,
  1064. d.ftCreationTime.dwHighDateTime, d.ftCreationTime.dwLowDateTime);
  1065. }
  1066. #else /* _WIN32 && !__CYGWIN__ */
  1067. static int
  1068. set_timefilter_stat(struct archive_match *a, int timetype, struct stat *st)
  1069. {
  1070. struct archive_entry *ae;
  1071. time_t ctime_sec, mtime_sec;
  1072. long ctime_ns, mtime_ns;
  1073. ae = archive_entry_new();
  1074. if (ae == NULL)
  1075. return (error_nomem(a));
  1076. archive_entry_copy_stat(ae, st);
  1077. ctime_sec = archive_entry_ctime(ae);
  1078. ctime_ns = archive_entry_ctime_nsec(ae);
  1079. mtime_sec = archive_entry_mtime(ae);
  1080. mtime_ns = archive_entry_mtime_nsec(ae);
  1081. archive_entry_free(ae);
  1082. return set_timefilter(a, timetype, mtime_sec, mtime_ns,
  1083. ctime_sec, ctime_ns);
  1084. }
  1085. static int
  1086. set_timefilter_pathname_mbs(struct archive_match *a, int timetype,
  1087. const char *path)
  1088. {
  1089. struct stat st;
  1090. if (path == NULL || *path == '\0') {
  1091. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1092. return (ARCHIVE_FAILED);
  1093. }
  1094. if (stat(path, &st) != 0) {
  1095. archive_set_error(&(a->archive), errno, "Failed to stat()");
  1096. return (ARCHIVE_FAILED);
  1097. }
  1098. return (set_timefilter_stat(a, timetype, &st));
  1099. }
  1100. static int
  1101. set_timefilter_pathname_wcs(struct archive_match *a, int timetype,
  1102. const wchar_t *path)
  1103. {
  1104. struct archive_string as;
  1105. int r;
  1106. if (path == NULL || *path == L'\0') {
  1107. archive_set_error(&(a->archive), EINVAL, "pathname is empty");
  1108. return (ARCHIVE_FAILED);
  1109. }
  1110. /* Convert WCS filename to MBS filename. */
  1111. archive_string_init(&as);
  1112. if (archive_string_append_from_wcs(&as, path, wcslen(path)) < 0) {
  1113. archive_string_free(&as);
  1114. if (errno == ENOMEM)
  1115. return (error_nomem(a));
  1116. archive_set_error(&(a->archive), -1,
  1117. "Failed to convert WCS to MBS");
  1118. return (ARCHIVE_FAILED);
  1119. }
  1120. r = set_timefilter_pathname_mbs(a, timetype, as.s);
  1121. archive_string_free(&as);
  1122. return (r);
  1123. }
  1124. #endif /* _WIN32 && !__CYGWIN__ */
  1125. /*
  1126. * Call back functions for archive_rb.
  1127. */
  1128. static int
  1129. cmp_node_mbs(const struct archive_rb_node *n1,
  1130. const struct archive_rb_node *n2)
  1131. {
  1132. struct match_file *f1 = (struct match_file *)(uintptr_t)n1;
  1133. struct match_file *f2 = (struct match_file *)(uintptr_t)n2;
  1134. const char *p1, *p2;
  1135. archive_mstring_get_mbs(NULL, &(f1->pathname), &p1);
  1136. archive_mstring_get_mbs(NULL, &(f2->pathname), &p2);
  1137. if (p1 == NULL)
  1138. return (1);
  1139. if (p2 == NULL)
  1140. return (-1);
  1141. return (strcmp(p1, p2));
  1142. }
  1143. static int
  1144. cmp_key_mbs(const struct archive_rb_node *n, const void *key)
  1145. {
  1146. struct match_file *f = (struct match_file *)(uintptr_t)n;
  1147. const char *p;
  1148. archive_mstring_get_mbs(NULL, &(f->pathname), &p);
  1149. if (p == NULL)
  1150. return (-1);
  1151. return (strcmp(p, (const char *)key));
  1152. }
  1153. static int
  1154. cmp_node_wcs(const struct archive_rb_node *n1,
  1155. const struct archive_rb_node *n2)
  1156. {
  1157. struct match_file *f1 = (struct match_file *)(uintptr_t)n1;
  1158. struct match_file *f2 = (struct match_file *)(uintptr_t)n2;
  1159. const wchar_t *p1, *p2;
  1160. archive_mstring_get_wcs(NULL, &(f1->pathname), &p1);
  1161. archive_mstring_get_wcs(NULL, &(f2->pathname), &p2);
  1162. if (p1 == NULL)
  1163. return (1);
  1164. if (p2 == NULL)
  1165. return (-1);
  1166. return (wcscmp(p1, p2));
  1167. }
  1168. static int
  1169. cmp_key_wcs(const struct archive_rb_node *n, const void *key)
  1170. {
  1171. struct match_file *f = (struct match_file *)(uintptr_t)n;
  1172. const wchar_t *p;
  1173. archive_mstring_get_wcs(NULL, &(f->pathname), &p);
  1174. if (p == NULL)
  1175. return (-1);
  1176. return (wcscmp(p, (const wchar_t *)key));
  1177. }
  1178. static void
  1179. entry_list_init(struct entry_list *list)
  1180. {
  1181. list->first = NULL;
  1182. list->last = &(list->first);
  1183. list->count = 0;
  1184. }
  1185. static void
  1186. entry_list_free(struct entry_list *list)
  1187. {
  1188. struct match_file *p, *q;
  1189. for (p = list->first; p != NULL; ) {
  1190. q = p;
  1191. p = p->next;
  1192. archive_mstring_clean(&(q->pathname));
  1193. free(q);
  1194. }
  1195. }
  1196. static void
  1197. entry_list_add(struct entry_list *list, struct match_file *file)
  1198. {
  1199. *list->last = file;
  1200. list->last = &(file->next);
  1201. list->count++;
  1202. }
  1203. static int
  1204. add_entry(struct archive_match *a, int flag,
  1205. struct archive_entry *entry)
  1206. {
  1207. struct match_file *f;
  1208. const void *pathname;
  1209. int r;
  1210. f = calloc(1, sizeof(*f));
  1211. if (f == NULL)
  1212. return (error_nomem(a));
  1213. #if defined(_WIN32) && !defined(__CYGWIN__)
  1214. pathname = archive_entry_pathname_w(entry);
  1215. if (pathname == NULL) {
  1216. free(f);
  1217. archive_set_error(&(a->archive), EINVAL, "pathname is NULL");
  1218. return (ARCHIVE_FAILED);
  1219. }
  1220. archive_mstring_copy_wcs(&(f->pathname), pathname);
  1221. a->exclusion_tree.rbt_ops = &rb_ops_wcs;
  1222. #else
  1223. (void)rb_ops_wcs;
  1224. pathname = archive_entry_pathname(entry);
  1225. if (pathname == NULL) {
  1226. free(f);
  1227. archive_set_error(&(a->archive), EINVAL, "pathname is NULL");
  1228. return (ARCHIVE_FAILED);
  1229. }
  1230. archive_mstring_copy_mbs(&(f->pathname), pathname);
  1231. a->exclusion_tree.rbt_ops = &rb_ops_mbs;
  1232. #endif
  1233. f->flag = flag;
  1234. f->mtime_sec = archive_entry_mtime(entry);
  1235. f->mtime_nsec = archive_entry_mtime_nsec(entry);
  1236. f->ctime_sec = archive_entry_ctime(entry);
  1237. f->ctime_nsec = archive_entry_ctime_nsec(entry);
  1238. r = __archive_rb_tree_insert_node(&(a->exclusion_tree), &(f->node));
  1239. if (!r) {
  1240. struct match_file *f2;
  1241. /* Get the duplicated file. */
  1242. f2 = (struct match_file *)__archive_rb_tree_find_node(
  1243. &(a->exclusion_tree), pathname);
  1244. /*
  1245. * We always overwrite comparison condition.
  1246. * If you do not want to overwrite it, you should not
  1247. * call archive_match_exclude_entry(). We cannot know
  1248. * what behavior you really expect since overwriting
  1249. * condition might be different with the flag.
  1250. */
  1251. if (f2 != NULL) {
  1252. f2->flag = f->flag;
  1253. f2->mtime_sec = f->mtime_sec;
  1254. f2->mtime_nsec = f->mtime_nsec;
  1255. f2->ctime_sec = f->ctime_sec;
  1256. f2->ctime_nsec = f->ctime_nsec;
  1257. }
  1258. /* Release the duplicated file. */
  1259. archive_mstring_clean(&(f->pathname));
  1260. free(f);
  1261. return (ARCHIVE_OK);
  1262. }
  1263. entry_list_add(&(a->exclusion_entry_list), f);
  1264. a->setflag |= TIME_IS_SET;
  1265. return (ARCHIVE_OK);
  1266. }
  1267. /*
  1268. * Test if entry is excluded by its timestamp.
  1269. */
  1270. static int
  1271. time_excluded(struct archive_match *a, struct archive_entry *entry)
  1272. {
  1273. struct match_file *f;
  1274. const void *pathname;
  1275. time_t sec;
  1276. long nsec;
  1277. /*
  1278. * If this file/dir is excluded by a time comparison, skip it.
  1279. */
  1280. if (a->newer_ctime_filter) {
  1281. /* If ctime is not set, use mtime instead. */
  1282. if (archive_entry_ctime_is_set(entry))
  1283. sec = archive_entry_ctime(entry);
  1284. else
  1285. sec = archive_entry_mtime(entry);
  1286. if (sec < a->newer_ctime_sec)
  1287. return (1); /* Too old, skip it. */
  1288. if (sec == a->newer_ctime_sec) {
  1289. if (archive_entry_ctime_is_set(entry))
  1290. nsec = archive_entry_ctime_nsec(entry);
  1291. else
  1292. nsec = archive_entry_mtime_nsec(entry);
  1293. if (nsec < a->newer_ctime_nsec)
  1294. return (1); /* Too old, skip it. */
  1295. if (nsec == a->newer_ctime_nsec &&
  1296. (a->newer_ctime_filter & ARCHIVE_MATCH_EQUAL)
  1297. == 0)
  1298. return (1); /* Equal, skip it. */
  1299. }
  1300. }
  1301. if (a->older_ctime_filter) {
  1302. /* If ctime is not set, use mtime instead. */
  1303. if (archive_entry_ctime_is_set(entry))
  1304. sec = archive_entry_ctime(entry);
  1305. else
  1306. sec = archive_entry_mtime(entry);
  1307. if (sec > a->older_ctime_sec)
  1308. return (1); /* Too new, skip it. */
  1309. if (sec == a->older_ctime_sec) {
  1310. if (archive_entry_ctime_is_set(entry))
  1311. nsec = archive_entry_ctime_nsec(entry);
  1312. else
  1313. nsec = archive_entry_mtime_nsec(entry);
  1314. if (nsec > a->older_ctime_nsec)
  1315. return (1); /* Too new, skip it. */
  1316. if (nsec == a->older_ctime_nsec &&
  1317. (a->older_ctime_filter & ARCHIVE_MATCH_EQUAL)
  1318. == 0)
  1319. return (1); /* Equal, skip it. */
  1320. }
  1321. }
  1322. if (a->newer_mtime_filter) {
  1323. sec = archive_entry_mtime(entry);
  1324. if (sec < a->newer_mtime_sec)
  1325. return (1); /* Too old, skip it. */
  1326. if (sec == a->newer_mtime_sec) {
  1327. nsec = archive_entry_mtime_nsec(entry);
  1328. if (nsec < a->newer_mtime_nsec)
  1329. return (1); /* Too old, skip it. */
  1330. if (nsec == a->newer_mtime_nsec &&
  1331. (a->newer_mtime_filter & ARCHIVE_MATCH_EQUAL)
  1332. == 0)
  1333. return (1); /* Equal, skip it. */
  1334. }
  1335. }
  1336. if (a->older_mtime_filter) {
  1337. sec = archive_entry_mtime(entry);
  1338. if (sec > a->older_mtime_sec)
  1339. return (1); /* Too new, skip it. */
  1340. nsec = archive_entry_mtime_nsec(entry);
  1341. if (sec == a->older_mtime_sec) {
  1342. if (nsec > a->older_mtime_nsec)
  1343. return (1); /* Too new, skip it. */
  1344. if (nsec == a->older_mtime_nsec &&
  1345. (a->older_mtime_filter & ARCHIVE_MATCH_EQUAL)
  1346. == 0)
  1347. return (1); /* Equal, skip it. */
  1348. }
  1349. }
  1350. /* If there is no exclusion list, include the file. */
  1351. if (a->exclusion_entry_list.count == 0)
  1352. return (0);
  1353. #if defined(_WIN32) && !defined(__CYGWIN__)
  1354. pathname = archive_entry_pathname_w(entry);
  1355. a->exclusion_tree.rbt_ops = &rb_ops_wcs;
  1356. #else
  1357. (void)rb_ops_wcs;
  1358. pathname = archive_entry_pathname(entry);
  1359. a->exclusion_tree.rbt_ops = &rb_ops_mbs;
  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, 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, 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. unsigned 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. unsigned b, m, t;
  1528. t = 0;
  1529. b = (unsigned)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->matches++;
  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->matches++;
  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.count) {
  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.count) {
  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. }