archive_read_support_format_mtree.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2008 Joerg Sonnenberger
  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: src/lib/libarchive/archive_read_support_format_mtree.c,v 1.11 2008/12/06 06:45:15 kientzle Exp $");
  28. #ifdef HAVE_SYS_STAT_H
  29. #include <sys/stat.h>
  30. #endif
  31. #ifdef HAVE_ERRNO_H
  32. #include <errno.h>
  33. #endif
  34. #ifdef HAVE_FCNTL_H
  35. #include <fcntl.h>
  36. #endif
  37. #include <stddef.h>
  38. /* #include <stdint.h> */ /* See archive_platform.h */
  39. #ifdef HAVE_STDLIB_H
  40. #include <stdlib.h>
  41. #endif
  42. #ifdef HAVE_STRING_H
  43. #include <string.h>
  44. #endif
  45. #include "archive.h"
  46. #include "archive_entry.h"
  47. #include "archive_private.h"
  48. #include "archive_read_private.h"
  49. #include "archive_string.h"
  50. #ifndef O_BINARY
  51. #define O_BINARY 0
  52. #endif
  53. #define MTREE_HAS_DEVICE 0x0001
  54. #define MTREE_HAS_FFLAGS 0x0002
  55. #define MTREE_HAS_GID 0x0004
  56. #define MTREE_HAS_GNAME 0x0008
  57. #define MTREE_HAS_MTIME 0x0010
  58. #define MTREE_HAS_NLINK 0x0020
  59. #define MTREE_HAS_PERM 0x0040
  60. #define MTREE_HAS_SIZE 0x0080
  61. #define MTREE_HAS_TYPE 0x0100
  62. #define MTREE_HAS_UID 0x0200
  63. #define MTREE_HAS_UNAME 0x0400
  64. #define MTREE_HAS_OPTIONAL 0x0800
  65. struct mtree_option {
  66. struct mtree_option *next;
  67. char *value;
  68. };
  69. struct mtree_entry {
  70. struct mtree_entry *next;
  71. struct mtree_option *options;
  72. char *name;
  73. char full;
  74. char used;
  75. };
  76. struct mtree {
  77. struct archive_string line;
  78. size_t buffsize;
  79. char *buff;
  80. off_t offset;
  81. int fd;
  82. int filetype;
  83. int archive_format;
  84. const char *archive_format_name;
  85. struct mtree_entry *entries;
  86. struct mtree_entry *this_entry;
  87. struct archive_string current_dir;
  88. struct archive_string contents_name;
  89. struct archive_entry_linkresolver *resolver;
  90. off_t cur_size, cur_offset;
  91. };
  92. static int cleanup(struct archive_read *);
  93. static int mtree_bid(struct archive_read *);
  94. static int parse_file(struct archive_read *, struct archive_entry *,
  95. struct mtree *, struct mtree_entry *, int *);
  96. static void parse_escapes(char *, struct mtree_entry *);
  97. static int parse_line(struct archive_read *, struct archive_entry *,
  98. struct mtree *, struct mtree_entry *, int *);
  99. static int parse_keyword(struct archive_read *, struct mtree *,
  100. struct archive_entry *, struct mtree_option *, int *);
  101. static int read_data(struct archive_read *a,
  102. const void **buff, size_t *size, off_t *offset);
  103. static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
  104. static int skip(struct archive_read *a);
  105. static int read_header(struct archive_read *,
  106. struct archive_entry *);
  107. static int64_t mtree_atol10(char **);
  108. static int64_t mtree_atol8(char **);
  109. static int64_t mtree_atol(char **);
  110. static void
  111. free_options(struct mtree_option *head)
  112. {
  113. struct mtree_option *next;
  114. for (; head != NULL; head = next) {
  115. next = head->next;
  116. free(head->value);
  117. free(head);
  118. }
  119. }
  120. int
  121. archive_read_support_format_mtree(struct archive *_a)
  122. {
  123. struct archive_read *a = (struct archive_read *)_a;
  124. struct mtree *mtree;
  125. int r;
  126. mtree = (struct mtree *)malloc(sizeof(*mtree));
  127. if (mtree == NULL) {
  128. archive_set_error(&a->archive, ENOMEM,
  129. "Can't allocate mtree data");
  130. return (ARCHIVE_FATAL);
  131. }
  132. memset(mtree, 0, sizeof(*mtree));
  133. mtree->fd = -1;
  134. r = __archive_read_register_format(a, mtree, "mtree",
  135. mtree_bid, NULL, read_header, read_data, skip, cleanup);
  136. if (r != ARCHIVE_OK)
  137. free(mtree);
  138. return (ARCHIVE_OK);
  139. }
  140. static int
  141. cleanup(struct archive_read *a)
  142. {
  143. struct mtree *mtree;
  144. struct mtree_entry *p, *q;
  145. mtree = (struct mtree *)(a->format->data);
  146. p = mtree->entries;
  147. while (p != NULL) {
  148. q = p->next;
  149. free(p->name);
  150. free_options(p->options);
  151. free(p);
  152. p = q;
  153. }
  154. archive_string_free(&mtree->line);
  155. archive_string_free(&mtree->current_dir);
  156. archive_string_free(&mtree->contents_name);
  157. archive_entry_linkresolver_free(mtree->resolver);
  158. free(mtree->buff);
  159. free(mtree);
  160. (a->format->data) = NULL;
  161. return (ARCHIVE_OK);
  162. }
  163. static int
  164. mtree_bid(struct archive_read *a)
  165. {
  166. const char *signature = "#mtree";
  167. const char *p;
  168. /* Now let's look at the actual header and see if it matches. */
  169. p = __archive_read_ahead(a, strlen(signature), NULL);
  170. if (p == NULL)
  171. return (-1);
  172. if (strncmp(p, signature, strlen(signature)) == 0)
  173. return (8 * strlen(signature));
  174. return (0);
  175. }
  176. /*
  177. * The extended mtree format permits multiple lines specifying
  178. * attributes for each file. For those entries, only the last line
  179. * is actually used. Practically speaking, that means we have
  180. * to read the entire mtree file into memory up front.
  181. *
  182. * The parsing is done in two steps. First, it is decided if a line
  183. * changes the global defaults and if it is, processed accordingly.
  184. * Otherwise, the options of the line are merged with the current
  185. * global options.
  186. */
  187. static int
  188. add_option(struct archive_read *a, struct mtree_option **global,
  189. const char *value, size_t len)
  190. {
  191. struct mtree_option *option;
  192. if ((option = malloc(sizeof(*option))) == NULL) {
  193. archive_set_error(&a->archive, errno, "Can't allocate memory");
  194. return (ARCHIVE_FATAL);
  195. }
  196. if ((option->value = malloc(len + 1)) == NULL) {
  197. free(option);
  198. archive_set_error(&a->archive, errno, "Can't allocate memory");
  199. return (ARCHIVE_FATAL);
  200. }
  201. memcpy(option->value, value, len);
  202. option->value[len] = '\0';
  203. option->next = *global;
  204. *global = option;
  205. return (ARCHIVE_OK);
  206. }
  207. static void
  208. remove_option(struct mtree_option **global, const char *value, size_t len)
  209. {
  210. struct mtree_option *iter, *last;
  211. last = NULL;
  212. for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
  213. if (strncmp(iter->value, value, len) == 0 &&
  214. (iter->value[len] == '\0' ||
  215. iter->value[len] == '='))
  216. break;
  217. }
  218. if (iter == NULL)
  219. return;
  220. if (last == NULL)
  221. *global = iter->next;
  222. else
  223. last->next = iter->next;
  224. free(iter->value);
  225. free(iter);
  226. }
  227. static int
  228. process_global_set(struct archive_read *a,
  229. struct mtree_option **global, const char *line)
  230. {
  231. const char *next, *eq;
  232. size_t len;
  233. int r;
  234. line += 4;
  235. for (;;) {
  236. next = line + strspn(line, " \t\r\n");
  237. if (*next == '\0')
  238. return (ARCHIVE_OK);
  239. line = next;
  240. next = line + strcspn(line, " \t\r\n");
  241. eq = strchr(line, '=');
  242. if (eq > next)
  243. len = next - line;
  244. else
  245. len = eq - line;
  246. remove_option(global, line, len);
  247. r = add_option(a, global, line, next - line);
  248. if (r != ARCHIVE_OK)
  249. return (r);
  250. line = next;
  251. }
  252. }
  253. static int
  254. process_global_unset(struct archive_read *a,
  255. struct mtree_option **global, const char *line)
  256. {
  257. const char *next;
  258. size_t len;
  259. line += 6;
  260. if (strchr(line, '=') != NULL) {
  261. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  262. "/unset shall not contain `='");
  263. return ARCHIVE_FATAL;
  264. }
  265. for (;;) {
  266. next = line + strspn(line, " \t\r\n");
  267. if (*next == '\0')
  268. return (ARCHIVE_OK);
  269. line = next;
  270. len = strcspn(line, " \t\r\n");
  271. if (len == 3 && strncmp(line, "all", 3) == 0) {
  272. free_options(*global);
  273. *global = NULL;
  274. } else {
  275. remove_option(global, line, len);
  276. }
  277. line += len;
  278. }
  279. }
  280. static int
  281. process_add_entry(struct archive_read *a, struct mtree *mtree,
  282. struct mtree_option **global, const char *line,
  283. struct mtree_entry **last_entry)
  284. {
  285. struct mtree_entry *entry;
  286. struct mtree_option *iter;
  287. const char *next, *eq;
  288. size_t len;
  289. int r;
  290. if ((entry = malloc(sizeof(*entry))) == NULL) {
  291. archive_set_error(&a->archive, errno, "Can't allocate memory");
  292. return (ARCHIVE_FATAL);
  293. }
  294. entry->next = NULL;
  295. entry->options = NULL;
  296. entry->name = NULL;
  297. entry->used = 0;
  298. entry->full = 0;
  299. /* Add this entry to list. */
  300. if (*last_entry == NULL)
  301. mtree->entries = entry;
  302. else
  303. (*last_entry)->next = entry;
  304. *last_entry = entry;
  305. len = strcspn(line, " \t\r\n");
  306. if ((entry->name = malloc(len + 1)) == NULL) {
  307. archive_set_error(&a->archive, errno, "Can't allocate memory");
  308. return (ARCHIVE_FATAL);
  309. }
  310. memcpy(entry->name, line, len);
  311. entry->name[len] = '\0';
  312. parse_escapes(entry->name, entry);
  313. line += len;
  314. for (iter = *global; iter != NULL; iter = iter->next) {
  315. r = add_option(a, &entry->options, iter->value,
  316. strlen(iter->value));
  317. if (r != ARCHIVE_OK)
  318. return (r);
  319. }
  320. for (;;) {
  321. next = line + strspn(line, " \t\r\n");
  322. if (*next == '\0')
  323. return (ARCHIVE_OK);
  324. line = next;
  325. next = line + strcspn(line, " \t\r\n");
  326. eq = strchr(line, '=');
  327. if (eq > next)
  328. len = next - line;
  329. else
  330. len = eq - line;
  331. remove_option(&entry->options, line, len);
  332. r = add_option(a, &entry->options, line, next - line);
  333. if (r != ARCHIVE_OK)
  334. return (r);
  335. line = next;
  336. }
  337. }
  338. static int
  339. read_mtree(struct archive_read *a, struct mtree *mtree)
  340. {
  341. ssize_t len;
  342. uintmax_t counter;
  343. char *p;
  344. struct mtree_option *global;
  345. struct mtree_entry *last_entry;
  346. int r;
  347. mtree->archive_format = ARCHIVE_FORMAT_MTREE;
  348. mtree->archive_format_name = "mtree";
  349. global = NULL;
  350. last_entry = NULL;
  351. r = ARCHIVE_OK;
  352. for (counter = 1; ; ++counter) {
  353. len = readline(a, mtree, &p, 256);
  354. if (len == 0) {
  355. mtree->this_entry = mtree->entries;
  356. free_options(global);
  357. return (ARCHIVE_OK);
  358. }
  359. if (len < 0) {
  360. free_options(global);
  361. return (len);
  362. }
  363. /* Leading whitespace is never significant, ignore it. */
  364. while (*p == ' ' || *p == '\t') {
  365. ++p;
  366. --len;
  367. }
  368. /* Skip content lines and blank lines. */
  369. if (*p == '#')
  370. continue;
  371. if (*p == '\r' || *p == '\n' || *p == '\0')
  372. continue;
  373. if (*p != '/') {
  374. r = process_add_entry(a, mtree, &global, p,
  375. &last_entry);
  376. } else if (strncmp(p, "/set", 4) == 0) {
  377. if (p[4] != ' ' && p[4] != '\t')
  378. break;
  379. r = process_global_set(a, &global, p);
  380. } else if (strncmp(p, "/unset", 6) == 0) {
  381. if (p[6] != ' ' && p[6] != '\t')
  382. break;
  383. r = process_global_unset(a, &global, p);
  384. } else
  385. break;
  386. if (r != ARCHIVE_OK) {
  387. free_options(global);
  388. return r;
  389. }
  390. }
  391. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  392. "Can't parse line %ju", counter);
  393. free_options(global);
  394. return (ARCHIVE_FATAL);
  395. }
  396. /*
  397. * Read in the entire mtree file into memory on the first request.
  398. * Then use the next unused file to satisfy each header request.
  399. */
  400. static int
  401. read_header(struct archive_read *a, struct archive_entry *entry)
  402. {
  403. struct mtree *mtree;
  404. char *p;
  405. int r, use_next;
  406. mtree = (struct mtree *)(a->format->data);
  407. if (mtree->fd >= 0) {
  408. close(mtree->fd);
  409. mtree->fd = -1;
  410. }
  411. if (mtree->entries == NULL) {
  412. mtree->resolver = archive_entry_linkresolver_new();
  413. if (mtree->resolver == NULL)
  414. return ARCHIVE_FATAL;
  415. archive_entry_linkresolver_set_strategy(mtree->resolver,
  416. ARCHIVE_FORMAT_MTREE);
  417. r = read_mtree(a, mtree);
  418. if (r != ARCHIVE_OK)
  419. return (r);
  420. }
  421. a->archive.archive_format = mtree->archive_format;
  422. a->archive.archive_format_name = mtree->archive_format_name;
  423. for (;;) {
  424. if (mtree->this_entry == NULL)
  425. return (ARCHIVE_EOF);
  426. if (strcmp(mtree->this_entry->name, "..") == 0) {
  427. mtree->this_entry->used = 1;
  428. if (archive_strlen(&mtree->current_dir) > 0) {
  429. /* Roll back current path. */
  430. p = mtree->current_dir.s
  431. + mtree->current_dir.length - 1;
  432. while (p >= mtree->current_dir.s && *p != '/')
  433. --p;
  434. if (p >= mtree->current_dir.s)
  435. --p;
  436. mtree->current_dir.length
  437. = p - mtree->current_dir.s + 1;
  438. }
  439. }
  440. if (!mtree->this_entry->used) {
  441. use_next = 0;
  442. r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
  443. if (use_next == 0)
  444. return (r);
  445. }
  446. mtree->this_entry = mtree->this_entry->next;
  447. }
  448. }
  449. /*
  450. * A single file can have multiple lines contribute specifications.
  451. * Parse as many lines as necessary, then pull additional information
  452. * from a backing file on disk as necessary.
  453. */
  454. static int
  455. parse_file(struct archive_read *a, struct archive_entry *entry,
  456. struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
  457. {
  458. const char *path;
  459. struct stat st_storage, *st;
  460. struct mtree_entry *mp;
  461. struct archive_entry *sparse_entry;
  462. int r = ARCHIVE_OK, r1, parsed_kws, mismatched_type;
  463. mentry->used = 1;
  464. /* Initialize reasonable defaults. */
  465. mtree->filetype = AE_IFREG;
  466. archive_entry_set_size(entry, 0);
  467. /* Parse options from this line. */
  468. parsed_kws = 0;
  469. r = parse_line(a, entry, mtree, mentry, &parsed_kws);
  470. if (mentry->full) {
  471. archive_entry_copy_pathname(entry, mentry->name);
  472. /*
  473. * "Full" entries are allowed to have multiple lines
  474. * and those lines aren't required to be adjacent. We
  475. * don't support multiple lines for "relative" entries
  476. * nor do we make any attempt to merge data from
  477. * separate "relative" and "full" entries. (Merging
  478. * "relative" and "full" entries would require dealing
  479. * with pathname canonicalization, which is a very
  480. * tricky subject.)
  481. */
  482. for (mp = mentry->next; mp != NULL; mp = mp->next) {
  483. if (mp->full && !mp->used
  484. && strcmp(mentry->name, mp->name) == 0) {
  485. /* Later lines override earlier ones. */
  486. mp->used = 1;
  487. r1 = parse_line(a, entry, mtree, mp,
  488. &parsed_kws);
  489. if (r1 < r)
  490. r = r1;
  491. }
  492. }
  493. } else {
  494. /*
  495. * Relative entries require us to construct
  496. * the full path and possibly update the
  497. * current directory.
  498. */
  499. size_t n = archive_strlen(&mtree->current_dir);
  500. if (n > 0)
  501. archive_strcat(&mtree->current_dir, "/");
  502. archive_strcat(&mtree->current_dir, mentry->name);
  503. archive_entry_copy_pathname(entry, mtree->current_dir.s);
  504. if (archive_entry_filetype(entry) != AE_IFDIR)
  505. mtree->current_dir.length = n;
  506. }
  507. /*
  508. * Try to open and stat the file to get the real size
  509. * and other file info. It would be nice to avoid
  510. * this here so that getting a listing of an mtree
  511. * wouldn't require opening every referenced contents
  512. * file. But then we wouldn't know the actual
  513. * contents size, so I don't see a really viable way
  514. * around this. (Also, we may want to someday pull
  515. * other unspecified info from the contents file on
  516. * disk.)
  517. */
  518. mtree->fd = -1;
  519. if (archive_strlen(&mtree->contents_name) > 0)
  520. path = mtree->contents_name.s;
  521. else
  522. path = archive_entry_pathname(entry);
  523. if (archive_entry_filetype(entry) == AE_IFREG ||
  524. archive_entry_filetype(entry) == AE_IFDIR) {
  525. mtree->fd = open(path, O_RDONLY | O_BINARY);
  526. if (mtree->fd == -1 &&
  527. (errno != ENOENT ||
  528. archive_strlen(&mtree->contents_name) > 0)) {
  529. archive_set_error(&a->archive, errno,
  530. "Can't open %s", path);
  531. r = ARCHIVE_WARN;
  532. }
  533. }
  534. st = &st_storage;
  535. if (mtree->fd >= 0) {
  536. if (fstat(mtree->fd, st) == -1) {
  537. archive_set_error(&a->archive, errno,
  538. "Could not fstat %s", path);
  539. r = ARCHIVE_WARN;
  540. /* If we can't stat it, don't keep it open. */
  541. close(mtree->fd);
  542. mtree->fd = -1;
  543. st = NULL;
  544. }
  545. } else if (lstat(path, st) == -1) {
  546. st = NULL;
  547. }
  548. /*
  549. * If there is a contents file on disk, use that size;
  550. * otherwise leave it as-is (it might have been set from
  551. * the mtree size= keyword).
  552. */
  553. if (st != NULL) {
  554. mismatched_type = 0;
  555. if ((st->st_mode & S_IFMT) == S_IFREG &&
  556. archive_entry_filetype(entry) != AE_IFREG)
  557. mismatched_type = 1;
  558. if ((st->st_mode & S_IFMT) == S_IFLNK &&
  559. archive_entry_filetype(entry) != AE_IFLNK)
  560. mismatched_type = 1;
  561. if ((st->st_mode & S_IFSOCK) == S_IFSOCK &&
  562. archive_entry_filetype(entry) != AE_IFSOCK)
  563. mismatched_type = 1;
  564. if ((st->st_mode & S_IFMT) == S_IFCHR &&
  565. archive_entry_filetype(entry) != AE_IFCHR)
  566. mismatched_type = 1;
  567. if ((st->st_mode & S_IFMT) == S_IFBLK &&
  568. archive_entry_filetype(entry) != AE_IFBLK)
  569. mismatched_type = 1;
  570. if ((st->st_mode & S_IFMT) == S_IFDIR &&
  571. archive_entry_filetype(entry) != AE_IFDIR)
  572. mismatched_type = 1;
  573. if ((st->st_mode & S_IFMT) == S_IFIFO &&
  574. archive_entry_filetype(entry) != AE_IFIFO)
  575. mismatched_type = 1;
  576. if (mismatched_type) {
  577. if ((parsed_kws & MTREE_HAS_OPTIONAL) == 0) {
  578. archive_set_error(&a->archive,
  579. ARCHIVE_ERRNO_MISC,
  580. "mtree specification has different type for %s",
  581. archive_entry_pathname(entry));
  582. r = ARCHIVE_WARN;
  583. } else {
  584. *use_next = 1;
  585. }
  586. /* Don't hold a non-regular file open. */
  587. if (mtree->fd >= 0)
  588. close(mtree->fd);
  589. mtree->fd = -1;
  590. st = NULL;
  591. return r;
  592. }
  593. }
  594. if (st != NULL) {
  595. if ((parsed_kws & MTREE_HAS_DEVICE) == 0 &&
  596. (archive_entry_filetype(entry) == AE_IFCHR ||
  597. archive_entry_filetype(entry) == AE_IFBLK))
  598. archive_entry_set_rdev(entry, st->st_rdev);
  599. if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME)) == 0)
  600. archive_entry_set_gid(entry, st->st_gid);
  601. if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME)) == 0)
  602. archive_entry_set_uid(entry, st->st_uid);
  603. if ((parsed_kws & MTREE_HAS_MTIME) == 0) {
  604. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  605. archive_entry_set_mtime(entry, st->st_mtime,
  606. st->st_mtimespec.tv_nsec);
  607. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  608. archive_entry_set_mtime(entry, st->st_mtime,
  609. st->st_mtim.tv_nsec);
  610. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  611. archive_entry_set_mtime(entry, st->st_mtime,
  612. st->st_mtime_n);
  613. #elif HAVE_STRUCT_STAT_ST_UMTIME
  614. archive_entry_set_mtime(entry, st->st_mtime,
  615. st->st_umtime*1000);
  616. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  617. archive_entry_set_mtime(entry, st->st_mtime,
  618. st->st_mtime_usec*1000);
  619. #else
  620. archive_entry_set_mtime(entry, st->st_mtime, 0);
  621. #endif
  622. }
  623. if ((parsed_kws & MTREE_HAS_NLINK) == 0)
  624. archive_entry_set_nlink(entry, st->st_nlink);
  625. if ((parsed_kws & MTREE_HAS_PERM) == 0)
  626. archive_entry_set_perm(entry, st->st_mode);
  627. if ((parsed_kws & MTREE_HAS_SIZE) == 0)
  628. archive_entry_set_size(entry, st->st_size);
  629. archive_entry_set_ino(entry, st->st_ino);
  630. archive_entry_set_dev(entry, st->st_dev);
  631. archive_entry_linkify(mtree->resolver, &entry, &sparse_entry);
  632. } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
  633. /*
  634. * Couldn't open the entry, stat it or the on-disk type
  635. * didn't match. If this entry is optional, just ignore it
  636. * and read the next header entry.
  637. */
  638. *use_next = 1;
  639. return ARCHIVE_OK;
  640. }
  641. mtree->cur_size = archive_entry_size(entry);
  642. mtree->offset = 0;
  643. return r;
  644. }
  645. /*
  646. * Each line contains a sequence of keywords.
  647. */
  648. static int
  649. parse_line(struct archive_read *a, struct archive_entry *entry,
  650. struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
  651. {
  652. struct mtree_option *iter;
  653. int r = ARCHIVE_OK, r1;
  654. for (iter = mp->options; iter != NULL; iter = iter->next) {
  655. r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
  656. if (r1 < r)
  657. r = r1;
  658. }
  659. if ((*parsed_kws & MTREE_HAS_TYPE) == 0) {
  660. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  661. "Missing type keyword in mtree specification");
  662. return (ARCHIVE_WARN);
  663. }
  664. return (r);
  665. }
  666. /*
  667. * Device entries have one of the following forms:
  668. * raw dev_t
  669. * format,major,minor[,subdevice]
  670. *
  671. * Just use major and minor, no translation etc is done
  672. * between formats.
  673. */
  674. static int
  675. parse_device(struct archive *a, struct archive_entry *entry, char *val)
  676. {
  677. char *comma1, *comma2;
  678. comma1 = strchr(val, ',');
  679. if (comma1 == NULL) {
  680. archive_entry_set_dev(entry, mtree_atol10(&val));
  681. return (ARCHIVE_OK);
  682. }
  683. ++comma1;
  684. comma2 = strchr(comma1, ',');
  685. if (comma2 == NULL) {
  686. archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
  687. "Malformed device attribute");
  688. return (ARCHIVE_WARN);
  689. }
  690. ++comma2;
  691. archive_entry_set_rdevmajor(entry, mtree_atol(&comma1));
  692. archive_entry_set_rdevminor(entry, mtree_atol(&comma2));
  693. return (ARCHIVE_OK);
  694. }
  695. /*
  696. * Parse a single keyword and its value.
  697. */
  698. static int
  699. parse_keyword(struct archive_read *a, struct mtree *mtree,
  700. struct archive_entry *entry, struct mtree_option *option, int *parsed_kws)
  701. {
  702. char *val, *key;
  703. key = option->value;
  704. if (*key == '\0')
  705. return (ARCHIVE_OK);
  706. if (strcmp(key, "optional") == 0) {
  707. *parsed_kws |= MTREE_HAS_OPTIONAL;
  708. return (ARCHIVE_OK);
  709. }
  710. if (strcmp(key, "ignore") == 0) {
  711. /*
  712. * The mtree processing is not recursive, so
  713. * recursion will only happen for explicitly listed
  714. * entries.
  715. */
  716. return (ARCHIVE_OK);
  717. }
  718. val = strchr(key, '=');
  719. if (val == NULL) {
  720. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  721. "Malformed attribute \"%s\" (%d)", key, key[0]);
  722. return (ARCHIVE_WARN);
  723. }
  724. *val = '\0';
  725. ++val;
  726. switch (key[0]) {
  727. case 'c':
  728. if (strcmp(key, "content") == 0
  729. || strcmp(key, "contents") == 0) {
  730. parse_escapes(val, NULL);
  731. archive_strcpy(&mtree->contents_name, val);
  732. break;
  733. }
  734. if (strcmp(key, "cksum") == 0)
  735. break;
  736. case 'd':
  737. if (strcmp(key, "device") == 0) {
  738. *parsed_kws |= MTREE_HAS_DEVICE;
  739. return parse_device(&a->archive, entry, val);
  740. }
  741. case 'f':
  742. if (strcmp(key, "flags") == 0) {
  743. *parsed_kws |= MTREE_HAS_FFLAGS;
  744. archive_entry_copy_fflags_text(entry, val);
  745. break;
  746. }
  747. case 'g':
  748. if (strcmp(key, "gid") == 0) {
  749. *parsed_kws |= MTREE_HAS_GID;
  750. archive_entry_set_gid(entry, mtree_atol10(&val));
  751. break;
  752. }
  753. if (strcmp(key, "gname") == 0) {
  754. *parsed_kws |= MTREE_HAS_GNAME;
  755. archive_entry_copy_gname(entry, val);
  756. break;
  757. }
  758. case 'l':
  759. if (strcmp(key, "link") == 0) {
  760. archive_entry_copy_symlink(entry, val);
  761. break;
  762. }
  763. case 'm':
  764. if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
  765. break;
  766. if (strcmp(key, "mode") == 0) {
  767. if (val[0] >= '0' && val[0] <= '9') {
  768. *parsed_kws |= MTREE_HAS_PERM;
  769. archive_entry_set_perm(entry,
  770. mtree_atol8(&val));
  771. } else {
  772. archive_set_error(&a->archive,
  773. ARCHIVE_ERRNO_FILE_FORMAT,
  774. "Symbolic mode \"%s\" unsupported", val);
  775. return ARCHIVE_WARN;
  776. }
  777. break;
  778. }
  779. case 'n':
  780. if (strcmp(key, "nlink") == 0) {
  781. *parsed_kws |= MTREE_HAS_NLINK;
  782. archive_entry_set_nlink(entry, mtree_atol10(&val));
  783. break;
  784. }
  785. case 'r':
  786. if (strcmp(key, "rmd160") == 0 ||
  787. strcmp(key, "rmd160digest") == 0)
  788. break;
  789. case 's':
  790. if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
  791. break;
  792. if (strcmp(key, "sha256") == 0 ||
  793. strcmp(key, "sha256digest") == 0)
  794. break;
  795. if (strcmp(key, "sha384") == 0 ||
  796. strcmp(key, "sha384digest") == 0)
  797. break;
  798. if (strcmp(key, "sha512") == 0 ||
  799. strcmp(key, "sha512digest") == 0)
  800. break;
  801. if (strcmp(key, "size") == 0) {
  802. archive_entry_set_size(entry, mtree_atol10(&val));
  803. break;
  804. }
  805. case 't':
  806. if (strcmp(key, "tags") == 0) {
  807. /*
  808. * Comma delimited list of tags.
  809. * Ignore the tags for now, but the interface
  810. * should be extended to allow inclusion/exclusion.
  811. */
  812. break;
  813. }
  814. if (strcmp(key, "time") == 0) {
  815. time_t m;
  816. long ns;
  817. *parsed_kws |= MTREE_HAS_MTIME;
  818. m = (time_t)mtree_atol10(&val);
  819. if (*val == '.') {
  820. ++val;
  821. ns = (long)mtree_atol10(&val);
  822. } else
  823. ns = 0;
  824. archive_entry_set_mtime(entry, m, ns);
  825. break;
  826. }
  827. if (strcmp(key, "type") == 0) {
  828. *parsed_kws |= MTREE_HAS_TYPE;
  829. switch (val[0]) {
  830. case 'b':
  831. if (strcmp(val, "block") == 0) {
  832. mtree->filetype = AE_IFBLK;
  833. break;
  834. }
  835. case 'c':
  836. if (strcmp(val, "char") == 0) {
  837. mtree->filetype = AE_IFCHR;
  838. break;
  839. }
  840. case 'd':
  841. if (strcmp(val, "dir") == 0) {
  842. mtree->filetype = AE_IFDIR;
  843. break;
  844. }
  845. case 'f':
  846. if (strcmp(val, "fifo") == 0) {
  847. mtree->filetype = AE_IFIFO;
  848. break;
  849. }
  850. if (strcmp(val, "file") == 0) {
  851. mtree->filetype = AE_IFREG;
  852. break;
  853. }
  854. case 'l':
  855. if (strcmp(val, "link") == 0) {
  856. mtree->filetype = AE_IFLNK;
  857. break;
  858. }
  859. default:
  860. archive_set_error(&a->archive,
  861. ARCHIVE_ERRNO_FILE_FORMAT,
  862. "Unrecognized file type \"%s\"", val);
  863. return (ARCHIVE_WARN);
  864. }
  865. archive_entry_set_filetype(entry, mtree->filetype);
  866. break;
  867. }
  868. case 'u':
  869. if (strcmp(key, "uid") == 0) {
  870. *parsed_kws |= MTREE_HAS_UID;
  871. archive_entry_set_uid(entry, mtree_atol10(&val));
  872. break;
  873. }
  874. if (strcmp(key, "uname") == 0) {
  875. *parsed_kws |= MTREE_HAS_UNAME;
  876. archive_entry_copy_uname(entry, val);
  877. break;
  878. }
  879. default:
  880. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  881. "Unrecognized key %s=%s", key, val);
  882. return (ARCHIVE_WARN);
  883. }
  884. return (ARCHIVE_OK);
  885. }
  886. static int
  887. read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset)
  888. {
  889. size_t bytes_to_read;
  890. ssize_t bytes_read;
  891. struct mtree *mtree;
  892. mtree = (struct mtree *)(a->format->data);
  893. if (mtree->fd < 0) {
  894. *buff = NULL;
  895. *offset = 0;
  896. *size = 0;
  897. return (ARCHIVE_EOF);
  898. }
  899. if (mtree->buff == NULL) {
  900. mtree->buffsize = 64 * 1024;
  901. mtree->buff = malloc(mtree->buffsize);
  902. if (mtree->buff == NULL) {
  903. archive_set_error(&a->archive, ENOMEM,
  904. "Can't allocate memory");
  905. return (ARCHIVE_FATAL);
  906. }
  907. }
  908. *buff = mtree->buff;
  909. *offset = mtree->offset;
  910. if ((off_t)mtree->buffsize > mtree->cur_size - mtree->offset)
  911. bytes_to_read = mtree->cur_size - mtree->offset;
  912. else
  913. bytes_to_read = mtree->buffsize;
  914. bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
  915. if (bytes_read < 0) {
  916. archive_set_error(&a->archive, errno, "Can't read");
  917. return (ARCHIVE_WARN);
  918. }
  919. if (bytes_read == 0) {
  920. *size = 0;
  921. return (ARCHIVE_EOF);
  922. }
  923. mtree->offset += bytes_read;
  924. *size = bytes_read;
  925. return (ARCHIVE_OK);
  926. }
  927. /* Skip does nothing except possibly close the contents file. */
  928. static int
  929. skip(struct archive_read *a)
  930. {
  931. struct mtree *mtree;
  932. mtree = (struct mtree *)(a->format->data);
  933. if (mtree->fd >= 0) {
  934. close(mtree->fd);
  935. mtree->fd = -1;
  936. }
  937. return (ARCHIVE_OK);
  938. }
  939. /*
  940. * Since parsing backslash sequences always makes strings shorter,
  941. * we can always do this conversion in-place.
  942. */
  943. static void
  944. parse_escapes(char *src, struct mtree_entry *mentry)
  945. {
  946. char *dest = src;
  947. char c;
  948. /*
  949. * The current directory is somewhat special, it should be archived
  950. * only once as it will confuse extraction otherwise.
  951. */
  952. if (strcmp(src, ".") == 0)
  953. mentry->full = 1;
  954. while (*src != '\0') {
  955. c = *src++;
  956. if (c == '/' && mentry != NULL)
  957. mentry->full = 1;
  958. if (c == '\\') {
  959. switch (src[0]) {
  960. case '0':
  961. if (src[1] < '0' || src[1] > '7') {
  962. c = 0;
  963. ++src;
  964. break;
  965. }
  966. /* FALLTHROUGH */
  967. case '1':
  968. case '2':
  969. case '3':
  970. if (src[1] >= '0' && src[1] <= '7' &&
  971. src[2] >= '0' && src[2] <= '7') {
  972. c = (src[0] - '0') << 6;
  973. c |= (src[1] - '0') << 3;
  974. c |= (src[2] - '0');
  975. src += 3;
  976. }
  977. break;
  978. case 'a':
  979. c = '\a';
  980. ++src;
  981. break;
  982. case 'b':
  983. c = '\b';
  984. ++src;
  985. break;
  986. case 'f':
  987. c = '\f';
  988. ++src;
  989. break;
  990. case 'n':
  991. c = '\n';
  992. ++src;
  993. break;
  994. case 'r':
  995. c = '\r';
  996. ++src;
  997. break;
  998. case 's':
  999. c = ' ';
  1000. ++src;
  1001. break;
  1002. case 't':
  1003. c = '\t';
  1004. ++src;
  1005. break;
  1006. case 'v':
  1007. c = '\v';
  1008. ++src;
  1009. break;
  1010. }
  1011. }
  1012. *dest++ = c;
  1013. }
  1014. *dest = '\0';
  1015. }
  1016. /*
  1017. * Note that this implementation does not (and should not!) obey
  1018. * locale settings; you cannot simply substitute strtol here, since
  1019. * it does obey locale.
  1020. */
  1021. static int64_t
  1022. mtree_atol8(char **p)
  1023. {
  1024. int64_t l, limit, last_digit_limit;
  1025. int digit, base;
  1026. base = 8;
  1027. limit = INT64_MAX / base;
  1028. last_digit_limit = INT64_MAX % base;
  1029. l = 0;
  1030. digit = **p - '0';
  1031. while (digit >= 0 && digit < base) {
  1032. if (l>limit || (l == limit && digit > last_digit_limit)) {
  1033. l = INT64_MAX; /* Truncate on overflow. */
  1034. break;
  1035. }
  1036. l = (l * base) + digit;
  1037. digit = *++(*p) - '0';
  1038. }
  1039. return (l);
  1040. }
  1041. /*
  1042. * Note that this implementation does not (and should not!) obey
  1043. * locale settings; you cannot simply substitute strtol here, since
  1044. * it does obey locale.
  1045. */
  1046. static int64_t
  1047. mtree_atol10(char **p)
  1048. {
  1049. int64_t l, limit, last_digit_limit;
  1050. int base, digit, sign;
  1051. base = 10;
  1052. limit = INT64_MAX / base;
  1053. last_digit_limit = INT64_MAX % base;
  1054. if (**p == '-') {
  1055. sign = -1;
  1056. ++(*p);
  1057. } else
  1058. sign = 1;
  1059. l = 0;
  1060. digit = **p - '0';
  1061. while (digit >= 0 && digit < base) {
  1062. if (l > limit || (l == limit && digit > last_digit_limit)) {
  1063. l = INT64_MAX; /* Truncate on overflow. */
  1064. break;
  1065. }
  1066. l = (l * base) + digit;
  1067. digit = *++(*p) - '0';
  1068. }
  1069. return (sign < 0) ? -l : l;
  1070. }
  1071. /*
  1072. * Note that this implementation does not (and should not!) obey
  1073. * locale settings; you cannot simply substitute strtol here, since
  1074. * it does obey locale.
  1075. */
  1076. static int64_t
  1077. mtree_atol16(char **p)
  1078. {
  1079. int64_t l, limit, last_digit_limit;
  1080. int base, digit, sign;
  1081. base = 16;
  1082. limit = INT64_MAX / base;
  1083. last_digit_limit = INT64_MAX % base;
  1084. if (**p == '-') {
  1085. sign = -1;
  1086. ++(*p);
  1087. } else
  1088. sign = 1;
  1089. l = 0;
  1090. if (**p >= '0' && **p <= '9')
  1091. digit = **p - '0';
  1092. else if (**p >= 'a' && **p <= 'f')
  1093. digit = **p - 'a' + 10;
  1094. else if (**p >= 'A' && **p <= 'F')
  1095. digit = **p - 'A' + 10;
  1096. else
  1097. digit = -1;
  1098. while (digit >= 0 && digit < base) {
  1099. if (l > limit || (l == limit && digit > last_digit_limit)) {
  1100. l = INT64_MAX; /* Truncate on overflow. */
  1101. break;
  1102. }
  1103. l = (l * base) + digit;
  1104. if (**p >= '0' && **p <= '9')
  1105. digit = **p - '0';
  1106. else if (**p >= 'a' && **p <= 'f')
  1107. digit = **p - 'a' + 10;
  1108. else if (**p >= 'A' && **p <= 'F')
  1109. digit = **p - 'A' + 10;
  1110. else
  1111. digit = -1;
  1112. }
  1113. return (sign < 0) ? -l : l;
  1114. }
  1115. static int64_t
  1116. mtree_atol(char **p)
  1117. {
  1118. if (**p != '0')
  1119. return mtree_atol10(p);
  1120. if ((*p)[1] == 'x' || (*p)[1] == 'X') {
  1121. *p += 2;
  1122. return mtree_atol16(p);
  1123. }
  1124. return mtree_atol8(p);
  1125. }
  1126. /*
  1127. * Returns length of line (including trailing newline)
  1128. * or negative on error. 'start' argument is updated to
  1129. * point to first character of line.
  1130. */
  1131. static ssize_t
  1132. readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
  1133. {
  1134. ssize_t bytes_read;
  1135. ssize_t total_size = 0;
  1136. ssize_t find_off = 0;
  1137. const void *t;
  1138. const char *s;
  1139. void *p;
  1140. char *u;
  1141. /* Accumulate line in a line buffer. */
  1142. for (;;) {
  1143. /* Read some more. */
  1144. t = __archive_read_ahead(a, 1, &bytes_read);
  1145. if (t == NULL)
  1146. return (0);
  1147. if (bytes_read < 0)
  1148. return (ARCHIVE_FATAL);
  1149. s = t; /* Start of line? */
  1150. p = memchr(t, '\n', bytes_read);
  1151. /* If we found '\n', trim the read. */
  1152. if (p != NULL) {
  1153. bytes_read = 1 + ((const char *)p) - s;
  1154. }
  1155. if (total_size + bytes_read + 1 > limit) {
  1156. archive_set_error(&a->archive,
  1157. ARCHIVE_ERRNO_FILE_FORMAT,
  1158. "Line too long");
  1159. return (ARCHIVE_FATAL);
  1160. }
  1161. if (archive_string_ensure(&mtree->line,
  1162. total_size + bytes_read + 1) == NULL) {
  1163. archive_set_error(&a->archive, ENOMEM,
  1164. "Can't allocate working buffer");
  1165. return (ARCHIVE_FATAL);
  1166. }
  1167. memcpy(mtree->line.s + total_size, t, bytes_read);
  1168. __archive_read_consume(a, bytes_read);
  1169. total_size += bytes_read;
  1170. /* Null terminate. */
  1171. mtree->line.s[total_size] = '\0';
  1172. /* If we found an unescaped '\n', clean up and return. */
  1173. for (u = mtree->line.s + find_off; *u; ++u) {
  1174. if (u[0] == '\n') {
  1175. *start = mtree->line.s;
  1176. return total_size;
  1177. }
  1178. if (u[0] == '#') {
  1179. if (p == NULL)
  1180. break;
  1181. *start = mtree->line.s;
  1182. return total_size;
  1183. }
  1184. if (u[0] != '\\')
  1185. continue;
  1186. if (u[1] == '\\') {
  1187. ++u;
  1188. continue;
  1189. }
  1190. if (u[1] == '\n') {
  1191. memmove(u, u + 1,
  1192. total_size - (u - mtree->line.s) + 1);
  1193. --total_size;
  1194. ++u;
  1195. break;
  1196. }
  1197. if (u[1] == '\0')
  1198. break;
  1199. }
  1200. find_off = u - mtree->line.s;
  1201. }
  1202. }