archive_read_support_format_mtree.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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;
  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, NULL, NULL, 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. for (counter = 1; ; ++counter) {
  352. len = readline(a, mtree, &p, 256);
  353. if (len == 0) {
  354. mtree->this_entry = mtree->entries;
  355. free_options(global);
  356. return (ARCHIVE_OK);
  357. }
  358. if (len < 0) {
  359. free_options(global);
  360. return (len);
  361. }
  362. /* Leading whitespace is never significant, ignore it. */
  363. while (*p == ' ' || *p == '\t') {
  364. ++p;
  365. --len;
  366. }
  367. /* Skip content lines and blank lines. */
  368. if (*p == '#')
  369. continue;
  370. if (*p == '\r' || *p == '\n' || *p == '\0')
  371. continue;
  372. if (*p != '/') {
  373. r = process_add_entry(a, mtree, &global, p,
  374. &last_entry);
  375. } else if (strncmp(p, "/set", 4) == 0) {
  376. if (p[4] != ' ' && p[4] != '\t')
  377. break;
  378. r = process_global_set(a, &global, p);
  379. } else if (strncmp(p, "/unset", 6) == 0) {
  380. if (p[6] != ' ' && p[6] != '\t')
  381. break;
  382. r = process_global_unset(a, &global, p);
  383. } else
  384. break;
  385. if (r != ARCHIVE_OK) {
  386. free_options(global);
  387. return r;
  388. }
  389. }
  390. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  391. "Can't parse line %ju", counter);
  392. free_options(global);
  393. return (ARCHIVE_FATAL);
  394. }
  395. /*
  396. * Read in the entire mtree file into memory on the first request.
  397. * Then use the next unused file to satisfy each header request.
  398. */
  399. static int
  400. read_header(struct archive_read *a, struct archive_entry *entry)
  401. {
  402. struct mtree *mtree;
  403. char *p;
  404. int r, use_next;
  405. mtree = (struct mtree *)(a->format->data);
  406. if (mtree->fd >= 0) {
  407. close(mtree->fd);
  408. mtree->fd = -1;
  409. }
  410. if (mtree->entries == NULL) {
  411. mtree->resolver = archive_entry_linkresolver_new();
  412. if (mtree->resolver == NULL)
  413. return ARCHIVE_FATAL;
  414. archive_entry_linkresolver_set_strategy(mtree->resolver,
  415. ARCHIVE_FORMAT_MTREE);
  416. r = read_mtree(a, mtree);
  417. if (r != ARCHIVE_OK)
  418. return (r);
  419. }
  420. a->archive.archive_format = mtree->archive_format;
  421. a->archive.archive_format_name = mtree->archive_format_name;
  422. for (;;) {
  423. if (mtree->this_entry == NULL)
  424. return (ARCHIVE_EOF);
  425. if (strcmp(mtree->this_entry->name, "..") == 0) {
  426. mtree->this_entry->used = 1;
  427. if (archive_strlen(&mtree->current_dir) > 0) {
  428. /* Roll back current path. */
  429. p = mtree->current_dir.s
  430. + mtree->current_dir.length - 1;
  431. while (p >= mtree->current_dir.s && *p != '/')
  432. --p;
  433. if (p >= mtree->current_dir.s)
  434. --p;
  435. mtree->current_dir.length
  436. = p - mtree->current_dir.s + 1;
  437. }
  438. }
  439. if (!mtree->this_entry->used) {
  440. use_next = 0;
  441. r = parse_file(a, entry, mtree, mtree->this_entry, &use_next);
  442. if (use_next == 0)
  443. return (r);
  444. }
  445. mtree->this_entry = mtree->this_entry->next;
  446. }
  447. }
  448. /*
  449. * A single file can have multiple lines contribute specifications.
  450. * Parse as many lines as necessary, then pull additional information
  451. * from a backing file on disk as necessary.
  452. */
  453. static int
  454. parse_file(struct archive_read *a, struct archive_entry *entry,
  455. struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
  456. {
  457. const char *path;
  458. struct stat st_storage, *st;
  459. struct mtree_entry *mp;
  460. struct archive_entry *sparse_entry;
  461. int r = ARCHIVE_OK, r1, parsed_kws, mismatched_type;
  462. mentry->used = 1;
  463. /* Initialize reasonable defaults. */
  464. mtree->filetype = AE_IFREG;
  465. archive_entry_set_size(entry, 0);
  466. /* Parse options from this line. */
  467. parsed_kws = 0;
  468. r = parse_line(a, entry, mtree, mentry, &parsed_kws);
  469. if (mentry->full) {
  470. archive_entry_copy_pathname(entry, mentry->name);
  471. /*
  472. * "Full" entries are allowed to have multiple lines
  473. * and those lines aren't required to be adjacent. We
  474. * don't support multiple lines for "relative" entries
  475. * nor do we make any attempt to merge data from
  476. * separate "relative" and "full" entries. (Merging
  477. * "relative" and "full" entries would require dealing
  478. * with pathname canonicalization, which is a very
  479. * tricky subject.)
  480. */
  481. for (mp = mentry->next; mp != NULL; mp = mp->next) {
  482. if (mp->full && !mp->used
  483. && strcmp(mentry->name, mp->name) == 0) {
  484. /* Later lines override earlier ones. */
  485. mp->used = 1;
  486. r1 = parse_line(a, entry, mtree, mp,
  487. &parsed_kws);
  488. if (r1 < r)
  489. r = r1;
  490. }
  491. }
  492. } else {
  493. /*
  494. * Relative entries require us to construct
  495. * the full path and possibly update the
  496. * current directory.
  497. */
  498. size_t n = archive_strlen(&mtree->current_dir);
  499. if (n > 0)
  500. archive_strcat(&mtree->current_dir, "/");
  501. archive_strcat(&mtree->current_dir, mentry->name);
  502. archive_entry_copy_pathname(entry, mtree->current_dir.s);
  503. if (archive_entry_filetype(entry) != AE_IFDIR)
  504. mtree->current_dir.length = n;
  505. }
  506. /*
  507. * Try to open and stat the file to get the real size
  508. * and other file info. It would be nice to avoid
  509. * this here so that getting a listing of an mtree
  510. * wouldn't require opening every referenced contents
  511. * file. But then we wouldn't know the actual
  512. * contents size, so I don't see a really viable way
  513. * around this. (Also, we may want to someday pull
  514. * other unspecified info from the contents file on
  515. * disk.)
  516. */
  517. mtree->fd = -1;
  518. if (archive_strlen(&mtree->contents_name) > 0)
  519. path = mtree->contents_name.s;
  520. else
  521. path = archive_entry_pathname(entry);
  522. if (archive_entry_filetype(entry) == AE_IFREG ||
  523. archive_entry_filetype(entry) == AE_IFDIR) {
  524. mtree->fd = open(path,
  525. 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. }
  906. return (ARCHIVE_FATAL);
  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. if (mentry != NULL && strcmp(src, ".") == 0)
  949. mentry->full = 1;
  950. while (*src != '\0') {
  951. c = *src++;
  952. if (c == '/' && mentry != NULL)
  953. mentry->full = 1;
  954. if (c == '\\') {
  955. switch (src[0]) {
  956. case '0':
  957. if (src[1] < '0' || src[1] > '7') {
  958. c = 0;
  959. ++src;
  960. break;
  961. }
  962. /* FALLTHROUGH */
  963. case '1':
  964. case '2':
  965. case '3':
  966. if (src[1] >= '0' && src[1] <= '7' &&
  967. src[2] >= '0' && src[2] <= '7') {
  968. c = (src[0] - '0') << 6;
  969. c |= (src[1] - '0') << 3;
  970. c |= (src[2] - '0');
  971. src += 3;
  972. }
  973. break;
  974. case 'a':
  975. c = '\a';
  976. ++src;
  977. break;
  978. case 'b':
  979. c = '\b';
  980. ++src;
  981. break;
  982. case 'f':
  983. c = '\f';
  984. ++src;
  985. break;
  986. case 'n':
  987. c = '\n';
  988. ++src;
  989. break;
  990. case 'r':
  991. c = '\r';
  992. ++src;
  993. break;
  994. case 's':
  995. c = ' ';
  996. ++src;
  997. break;
  998. case 't':
  999. c = '\t';
  1000. ++src;
  1001. break;
  1002. case 'v':
  1003. c = '\v';
  1004. ++src;
  1005. break;
  1006. }
  1007. }
  1008. *dest++ = c;
  1009. }
  1010. *dest = '\0';
  1011. }
  1012. /*
  1013. * Note that this implementation does not (and should not!) obey
  1014. * locale settings; you cannot simply substitute strtol here, since
  1015. * it does obey locale.
  1016. */
  1017. static int64_t
  1018. mtree_atol8(char **p)
  1019. {
  1020. int64_t l, limit, last_digit_limit;
  1021. int digit, base;
  1022. base = 8;
  1023. limit = INT64_MAX / base;
  1024. last_digit_limit = INT64_MAX % base;
  1025. l = 0;
  1026. digit = **p - '0';
  1027. while (digit >= 0 && digit < base) {
  1028. if (l>limit || (l == limit && digit > last_digit_limit)) {
  1029. l = INT64_MAX; /* Truncate on overflow. */
  1030. break;
  1031. }
  1032. l = (l * base) + digit;
  1033. digit = *++(*p) - '0';
  1034. }
  1035. return (l);
  1036. }
  1037. /*
  1038. * Note that this implementation does not (and should not!) obey
  1039. * locale settings; you cannot simply substitute strtol here, since
  1040. * it does obey locale.
  1041. */
  1042. static int64_t
  1043. mtree_atol10(char **p)
  1044. {
  1045. int64_t l, limit, last_digit_limit;
  1046. int base, digit, sign;
  1047. base = 10;
  1048. if (**p == '-') {
  1049. sign = -1;
  1050. limit = ((uint64_t)(INT64_MAX) + 1) / base;
  1051. last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
  1052. ++(*p);
  1053. } else {
  1054. sign = 1;
  1055. limit = INT64_MAX / base;
  1056. last_digit_limit = INT64_MAX % base;
  1057. }
  1058. l = 0;
  1059. digit = **p - '0';
  1060. while (digit >= 0 && digit < base) {
  1061. if (l > limit || (l == limit && digit > last_digit_limit))
  1062. return (sign < 0) ? INT64_MIN : INT64_MAX;
  1063. l = (l * base) + digit;
  1064. digit = *++(*p) - '0';
  1065. }
  1066. return (sign < 0) ? -l : l;
  1067. }
  1068. /* Parse a hex digit. */
  1069. static int
  1070. parsehex(char c)
  1071. {
  1072. if (c >= '0' && c <= '9')
  1073. return c - '0';
  1074. else if (c >= 'a' && c <= 'f')
  1075. return c - 'a';
  1076. else if (c >= 'A' && c <= 'F')
  1077. return c - 'A';
  1078. else
  1079. return -1;
  1080. }
  1081. /*
  1082. * Note that this implementation does not (and should not!) obey
  1083. * locale settings; you cannot simply substitute strtol here, since
  1084. * it does obey locale.
  1085. */
  1086. static int64_t
  1087. mtree_atol16(char **p)
  1088. {
  1089. int64_t l, limit, last_digit_limit;
  1090. int base, digit, sign;
  1091. base = 16;
  1092. if (**p == '-') {
  1093. sign = -1;
  1094. limit = ((uint64_t)(INT64_MAX) + 1) / base;
  1095. last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
  1096. ++(*p);
  1097. } else {
  1098. sign = 1;
  1099. limit = INT64_MAX / base;
  1100. last_digit_limit = INT64_MAX % base;
  1101. }
  1102. l = 0;
  1103. digit = parsehex(**p);
  1104. while (digit >= 0 && digit < base) {
  1105. if (l > limit || (l == limit && digit > last_digit_limit))
  1106. return (sign < 0) ? INT64_MIN : INT64_MAX;
  1107. l = (l * base) + digit;
  1108. digit = parsehex(*++(*p));
  1109. }
  1110. return (sign < 0) ? -l : l;
  1111. }
  1112. static int64_t
  1113. mtree_atol(char **p)
  1114. {
  1115. if (**p != '0')
  1116. return mtree_atol10(p);
  1117. if ((*p)[1] == 'x' || (*p)[1] == 'X') {
  1118. *p += 2;
  1119. return mtree_atol16(p);
  1120. }
  1121. return mtree_atol8(p);
  1122. }
  1123. /*
  1124. * Returns length of line (including trailing newline)
  1125. * or negative on error. 'start' argument is updated to
  1126. * point to first character of line.
  1127. */
  1128. static ssize_t
  1129. readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
  1130. {
  1131. ssize_t bytes_read;
  1132. ssize_t total_size = 0;
  1133. ssize_t find_off = 0;
  1134. const void *t;
  1135. const char *s;
  1136. void *p;
  1137. char *u;
  1138. /* Accumulate line in a line buffer. */
  1139. for (;;) {
  1140. /* Read some more. */
  1141. t = __archive_read_ahead(a, 1, &bytes_read);
  1142. if (t == NULL)
  1143. return (0);
  1144. if (bytes_read < 0)
  1145. return (ARCHIVE_FATAL);
  1146. s = t; /* Start of line? */
  1147. p = memchr(t, '\n', bytes_read);
  1148. /* If we found '\n', trim the read. */
  1149. if (p != NULL) {
  1150. bytes_read = 1 + ((const char *)p) - s;
  1151. }
  1152. if (total_size + bytes_read + 1 > limit) {
  1153. archive_set_error(&a->archive,
  1154. ARCHIVE_ERRNO_FILE_FORMAT,
  1155. "Line too long");
  1156. return (ARCHIVE_FATAL);
  1157. }
  1158. if (archive_string_ensure(&mtree->line,
  1159. total_size + bytes_read + 1) == NULL) {
  1160. archive_set_error(&a->archive, ENOMEM,
  1161. "Can't allocate working buffer");
  1162. return (ARCHIVE_FATAL);
  1163. }
  1164. memcpy(mtree->line.s + total_size, t, bytes_read);
  1165. __archive_read_consume(a, bytes_read);
  1166. total_size += bytes_read;
  1167. /* Null terminate. */
  1168. mtree->line.s[total_size] = '\0';
  1169. /* If we found an unescaped '\n', clean up and return. */
  1170. for (u = mtree->line.s + find_off; *u; ++u) {
  1171. if (u[0] == '\n') {
  1172. *start = mtree->line.s;
  1173. return total_size;
  1174. }
  1175. if (u[0] == '#') {
  1176. if (p == NULL)
  1177. break;
  1178. *start = mtree->line.s;
  1179. return total_size;
  1180. }
  1181. if (u[0] != '\\')
  1182. continue;
  1183. if (u[1] == '\\') {
  1184. ++u;
  1185. continue;
  1186. }
  1187. if (u[1] == '\n') {
  1188. memmove(u, u + 1,
  1189. total_size - (u - mtree->line.s) + 1);
  1190. --total_size;
  1191. ++u;
  1192. break;
  1193. }
  1194. if (u[1] == '\0')
  1195. break;
  1196. }
  1197. find_off = u - mtree->line.s;
  1198. }
  1199. }