archive_write_set_format_ustar.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011 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: head/lib/libarchive/archive_write_set_format_ustar.c 191579 2009-04-27 18:35:03Z kientzle $");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stdio.h>
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_write_private.h"
  43. struct ustar {
  44. uint64_t entry_bytes_remaining;
  45. uint64_t entry_padding;
  46. struct archive_string_conv *opt_sconv;
  47. struct archive_string_conv *sconv_default;
  48. int init_default_conversion;
  49. };
  50. /*
  51. * Define structure of POSIX 'ustar' tar header.
  52. */
  53. #define USTAR_name_offset 0
  54. #define USTAR_name_size 100
  55. #define USTAR_mode_offset 100
  56. #define USTAR_mode_size 6
  57. #define USTAR_mode_max_size 8
  58. #define USTAR_uid_offset 108
  59. #define USTAR_uid_size 6
  60. #define USTAR_uid_max_size 8
  61. #define USTAR_gid_offset 116
  62. #define USTAR_gid_size 6
  63. #define USTAR_gid_max_size 8
  64. #define USTAR_size_offset 124
  65. #define USTAR_size_size 11
  66. #define USTAR_size_max_size 12
  67. #define USTAR_mtime_offset 136
  68. #define USTAR_mtime_size 11
  69. #define USTAR_mtime_max_size 11
  70. #define USTAR_checksum_offset 148
  71. #define USTAR_checksum_size 8
  72. #define USTAR_typeflag_offset 156
  73. #define USTAR_typeflag_size 1
  74. #define USTAR_linkname_offset 157
  75. #define USTAR_linkname_size 100
  76. #define USTAR_magic_offset 257
  77. #define USTAR_magic_size 6
  78. #define USTAR_version_offset 263
  79. #define USTAR_version_size 2
  80. #define USTAR_uname_offset 265
  81. #define USTAR_uname_size 32
  82. #define USTAR_gname_offset 297
  83. #define USTAR_gname_size 32
  84. #define USTAR_rdevmajor_offset 329
  85. #define USTAR_rdevmajor_size 6
  86. #define USTAR_rdevmajor_max_size 8
  87. #define USTAR_rdevminor_offset 337
  88. #define USTAR_rdevminor_size 6
  89. #define USTAR_rdevminor_max_size 8
  90. #define USTAR_prefix_offset 345
  91. #define USTAR_prefix_size 155
  92. #define USTAR_padding_offset 500
  93. #define USTAR_padding_size 12
  94. /*
  95. * A filled-in copy of the header for initialization.
  96. */
  97. static const char template_header[] = {
  98. /* name: 100 bytes */
  99. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  100. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  101. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  102. 0,0,0,0,
  103. /* Mode, space-null termination: 8 bytes */
  104. '0','0','0','0','0','0', ' ','\0',
  105. /* uid, space-null termination: 8 bytes */
  106. '0','0','0','0','0','0', ' ','\0',
  107. /* gid, space-null termination: 8 bytes */
  108. '0','0','0','0','0','0', ' ','\0',
  109. /* size, space termation: 12 bytes */
  110. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  111. /* mtime, space termation: 12 bytes */
  112. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  113. /* Initial checksum value: 8 spaces */
  114. ' ',' ',' ',' ',' ',' ',' ',' ',
  115. /* Typeflag: 1 byte */
  116. '0', /* '0' = regular file */
  117. /* Linkname: 100 bytes */
  118. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  119. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  120. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  121. 0,0,0,0,
  122. /* Magic: 6 bytes, Version: 2 bytes */
  123. 'u','s','t','a','r','\0', '0','0',
  124. /* Uname: 32 bytes */
  125. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  126. /* Gname: 32 bytes */
  127. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  128. /* rdevmajor + space/null padding: 8 bytes */
  129. '0','0','0','0','0','0', ' ','\0',
  130. /* rdevminor + space/null padding: 8 bytes */
  131. '0','0','0','0','0','0', ' ','\0',
  132. /* Prefix: 155 bytes */
  133. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  134. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  135. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  136. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  137. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,
  138. /* Padding: 12 bytes */
  139. 0,0,0,0,0,0,0,0, 0,0,0,0
  140. };
  141. static ssize_t archive_write_ustar_data(struct archive_write *a, const void *buff,
  142. size_t s);
  143. static int archive_write_ustar_free(struct archive_write *);
  144. static int archive_write_ustar_close(struct archive_write *);
  145. static int archive_write_ustar_finish_entry(struct archive_write *);
  146. static int archive_write_ustar_header(struct archive_write *,
  147. struct archive_entry *entry);
  148. static int archive_write_ustar_options(struct archive_write *,
  149. const char *, const char *);
  150. static int format_256(int64_t, char *, int);
  151. static int format_number(int64_t, char *, int size, int max, int strict);
  152. static int format_octal(int64_t, char *, int);
  153. /*
  154. * Set output format to 'ustar' format.
  155. */
  156. int
  157. archive_write_set_format_ustar(struct archive *_a)
  158. {
  159. struct archive_write *a = (struct archive_write *)_a;
  160. struct ustar *ustar;
  161. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  162. ARCHIVE_STATE_NEW, "archive_write_set_format_ustar");
  163. /* If someone else was already registered, unregister them. */
  164. if (a->format_free != NULL)
  165. (a->format_free)(a);
  166. /* Basic internal sanity test. */
  167. if (sizeof(template_header) != 512) {
  168. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  169. "Internal: template_header wrong size: %zu should be 512",
  170. sizeof(template_header));
  171. return (ARCHIVE_FATAL);
  172. }
  173. ustar = (struct ustar *)malloc(sizeof(*ustar));
  174. if (ustar == NULL) {
  175. archive_set_error(&a->archive, ENOMEM,
  176. "Can't allocate ustar data");
  177. return (ARCHIVE_FATAL);
  178. }
  179. memset(ustar, 0, sizeof(*ustar));
  180. a->format_data = ustar;
  181. a->format_name = "ustar";
  182. a->format_options = archive_write_ustar_options;
  183. a->format_write_header = archive_write_ustar_header;
  184. a->format_write_data = archive_write_ustar_data;
  185. a->format_close = archive_write_ustar_close;
  186. a->format_free = archive_write_ustar_free;
  187. a->format_finish_entry = archive_write_ustar_finish_entry;
  188. a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
  189. a->archive.archive_format_name = "POSIX ustar";
  190. return (ARCHIVE_OK);
  191. }
  192. static int
  193. archive_write_ustar_options(struct archive_write *a, const char *key,
  194. const char *val)
  195. {
  196. struct ustar *ustar = (struct ustar *)a->format_data;
  197. int ret = ARCHIVE_FAILED;
  198. if (strcmp(key, "hdrcharset") == 0) {
  199. if (val == NULL || val[0] == 0)
  200. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  201. "%s: hdrcharset option needs a character-set name",
  202. a->format_name);
  203. else {
  204. ustar->opt_sconv = archive_string_conversion_to_charset(
  205. &a->archive, val, 0);
  206. if (ustar->opt_sconv != NULL)
  207. ret = ARCHIVE_OK;
  208. else
  209. ret = ARCHIVE_FATAL;
  210. }
  211. } else
  212. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  213. "%s: unknown keyword ``%s''", a->format_name, key);
  214. return (ret);
  215. }
  216. static int
  217. archive_write_ustar_header(struct archive_write *a, struct archive_entry *entry)
  218. {
  219. char buff[512];
  220. int ret, ret2;
  221. struct ustar *ustar;
  222. struct archive_string_conv *sconv;
  223. ustar = (struct ustar *)a->format_data;
  224. /* Setup default string conversion. */
  225. if (ustar->opt_sconv == NULL) {
  226. if (!ustar->init_default_conversion) {
  227. ustar->sconv_default =
  228. archive_string_default_conversion_for_write(&(a->archive));
  229. ustar->init_default_conversion = 1;
  230. }
  231. sconv = ustar->sconv_default;
  232. } else
  233. sconv = ustar->opt_sconv;
  234. /* Sanity check. */
  235. if (archive_entry_pathname(entry) == NULL) {
  236. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  237. "Can't record entry in tar file without pathname");
  238. return (ARCHIVE_FAILED);
  239. }
  240. /* Only regular files (not hardlinks) have data. */
  241. if (archive_entry_hardlink(entry) != NULL ||
  242. archive_entry_symlink(entry) != NULL ||
  243. !(archive_entry_filetype(entry) == AE_IFREG))
  244. archive_entry_set_size(entry, 0);
  245. if (AE_IFDIR == archive_entry_filetype(entry)) {
  246. const char *p;
  247. char *t;
  248. /*
  249. * Ensure a trailing '/'. Modify the entry so
  250. * the client sees the change.
  251. */
  252. p = archive_entry_pathname(entry);
  253. if (p[strlen(p) - 1] != '/') {
  254. t = (char *)malloc(strlen(p) + 2);
  255. if (t == NULL) {
  256. archive_set_error(&a->archive, ENOMEM,
  257. "Can't allocate ustar data");
  258. return(ARCHIVE_FATAL);
  259. }
  260. strcpy(t, p);
  261. strcat(t, "/");
  262. archive_entry_copy_pathname(entry, t);
  263. free(t);
  264. }
  265. }
  266. ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1, sconv);
  267. if (ret < ARCHIVE_WARN)
  268. return (ret);
  269. ret2 = __archive_write_output(a, buff, 512);
  270. if (ret2 < ARCHIVE_WARN)
  271. return (ret2);
  272. if (ret2 < ret)
  273. ret = ret2;
  274. ustar->entry_bytes_remaining = archive_entry_size(entry);
  275. ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
  276. return (ret);
  277. }
  278. /*
  279. * Format a basic 512-byte "ustar" header.
  280. *
  281. * Returns -1 if format failed (due to field overflow).
  282. * Note that this always formats as much of the header as possible.
  283. * If "strict" is set to zero, it will extend numeric fields as
  284. * necessary (overwriting terminators or using base-256 extensions).
  285. *
  286. * This is exported so that other 'tar' formats can use it.
  287. */
  288. int
  289. __archive_write_format_header_ustar(struct archive_write *a, char h[512],
  290. struct archive_entry *entry, int tartype, int strict,
  291. struct archive_string_conv *sconv)
  292. {
  293. unsigned int checksum;
  294. int i, r, ret;
  295. size_t copy_length;
  296. const char *p, *pp;
  297. int mytartype;
  298. ret = 0;
  299. mytartype = -1;
  300. /*
  301. * The "template header" already includes the "ustar"
  302. * signature, various end-of-field markers and other required
  303. * elements.
  304. */
  305. memcpy(h, &template_header, 512);
  306. /*
  307. * Because the block is already null-filled, and strings
  308. * are allowed to exactly fill their destination (without null),
  309. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  310. */
  311. r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
  312. if (r != 0) {
  313. if (errno == ENOMEM) {
  314. archive_set_error(&a->archive, ENOMEM,
  315. "Can't allocate memory for Pathname");
  316. return (ARCHIVE_FATAL);
  317. }
  318. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  319. "Can't translate pathname '%s' to %s",
  320. pp, archive_string_conversion_charset_name(sconv));
  321. ret = ARCHIVE_WARN;
  322. }
  323. if (copy_length <= USTAR_name_size)
  324. memcpy(h + USTAR_name_offset, pp, copy_length);
  325. else {
  326. /* Store in two pieces, splitting at a '/'. */
  327. p = strchr(pp + copy_length - USTAR_name_size - 1, '/');
  328. /*
  329. * Look for the next '/' if we chose the first character
  330. * as the separator. (ustar format doesn't permit
  331. * an empty prefix.)
  332. */
  333. if (p == pp)
  334. p = strchr(p + 1, '/');
  335. /* Fail if the name won't fit. */
  336. if (!p) {
  337. /* No separator. */
  338. archive_set_error(&a->archive, ENAMETOOLONG,
  339. "Pathname too long");
  340. ret = ARCHIVE_FAILED;
  341. } else if (p[1] == '\0') {
  342. /*
  343. * The only feasible separator is a final '/';
  344. * this would result in a non-empty prefix and
  345. * an empty name, which POSIX doesn't
  346. * explicitly forbid, but it just feels wrong.
  347. */
  348. archive_set_error(&a->archive, ENAMETOOLONG,
  349. "Pathname too long");
  350. ret = ARCHIVE_FAILED;
  351. } else if (p > pp + USTAR_prefix_size) {
  352. /* Prefix is too long. */
  353. archive_set_error(&a->archive, ENAMETOOLONG,
  354. "Pathname too long");
  355. ret = ARCHIVE_FAILED;
  356. } else {
  357. /* Copy prefix and remainder to appropriate places */
  358. memcpy(h + USTAR_prefix_offset, pp, p - pp);
  359. memcpy(h + USTAR_name_offset, p + 1,
  360. pp + copy_length - p - 1);
  361. }
  362. }
  363. r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
  364. if (r != 0) {
  365. if (errno == ENOMEM) {
  366. archive_set_error(&a->archive, ENOMEM,
  367. "Can't allocate memory for Linkname");
  368. return (ARCHIVE_FATAL);
  369. }
  370. archive_set_error(&a->archive,
  371. ARCHIVE_ERRNO_FILE_FORMAT,
  372. "Can't translate linkname '%s' to %s",
  373. p, archive_string_conversion_charset_name(sconv));
  374. ret = ARCHIVE_WARN;
  375. }
  376. if (copy_length > 0)
  377. mytartype = '1';
  378. else {
  379. r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
  380. if (r != 0) {
  381. if (errno == ENOMEM) {
  382. archive_set_error(&a->archive, ENOMEM,
  383. "Can't allocate memory for Linkname");
  384. return (ARCHIVE_FATAL);
  385. }
  386. archive_set_error(&a->archive,
  387. ARCHIVE_ERRNO_FILE_FORMAT,
  388. "Can't translate linkname '%s' to %s",
  389. p, archive_string_conversion_charset_name(sconv));
  390. ret = ARCHIVE_WARN;
  391. }
  392. }
  393. if (copy_length > 0) {
  394. if (copy_length > USTAR_linkname_size) {
  395. archive_set_error(&a->archive, ENAMETOOLONG,
  396. "Link contents too long");
  397. ret = ARCHIVE_FAILED;
  398. copy_length = USTAR_linkname_size;
  399. }
  400. memcpy(h + USTAR_linkname_offset, p, copy_length);
  401. }
  402. r = archive_entry_uname_l(entry, &p, &copy_length, sconv);
  403. if (r != 0) {
  404. if (errno == ENOMEM) {
  405. archive_set_error(&a->archive, ENOMEM,
  406. "Can't allocate memory for Uname");
  407. return (ARCHIVE_FATAL);
  408. }
  409. archive_set_error(&a->archive,
  410. ARCHIVE_ERRNO_FILE_FORMAT,
  411. "Can't translate uname '%s' to %s",
  412. p, archive_string_conversion_charset_name(sconv));
  413. ret = ARCHIVE_WARN;
  414. }
  415. if (copy_length > 0) {
  416. if (copy_length > USTAR_uname_size) {
  417. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  418. "Username too long");
  419. ret = ARCHIVE_FAILED;
  420. copy_length = USTAR_uname_size;
  421. }
  422. memcpy(h + USTAR_uname_offset, p, copy_length);
  423. }
  424. r = archive_entry_gname_l(entry, &p, &copy_length, sconv);
  425. if (r != 0) {
  426. if (errno == ENOMEM) {
  427. archive_set_error(&a->archive, ENOMEM,
  428. "Can't allocate memory for Gname");
  429. return (ARCHIVE_FATAL);
  430. }
  431. archive_set_error(&a->archive,
  432. ARCHIVE_ERRNO_FILE_FORMAT,
  433. "Can't translate gname '%s' to %s",
  434. p, archive_string_conversion_charset_name(sconv));
  435. ret = ARCHIVE_WARN;
  436. }
  437. if (copy_length > 0) {
  438. if (strlen(p) > USTAR_gname_size) {
  439. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  440. "Group name too long");
  441. ret = ARCHIVE_FAILED;
  442. copy_length = USTAR_gname_size;
  443. }
  444. memcpy(h + USTAR_gname_offset, p, copy_length);
  445. }
  446. if (format_number(archive_entry_mode(entry) & 07777,
  447. h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
  448. archive_set_error(&a->archive, ERANGE,
  449. "Numeric mode too large");
  450. ret = ARCHIVE_FAILED;
  451. }
  452. if (format_number(archive_entry_uid(entry),
  453. h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
  454. archive_set_error(&a->archive, ERANGE,
  455. "Numeric user ID too large");
  456. ret = ARCHIVE_FAILED;
  457. }
  458. if (format_number(archive_entry_gid(entry),
  459. h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
  460. archive_set_error(&a->archive, ERANGE,
  461. "Numeric group ID too large");
  462. ret = ARCHIVE_FAILED;
  463. }
  464. if (format_number(archive_entry_size(entry),
  465. h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
  466. archive_set_error(&a->archive, ERANGE,
  467. "File size out of range");
  468. ret = ARCHIVE_FAILED;
  469. }
  470. if (format_number(archive_entry_mtime(entry),
  471. h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
  472. archive_set_error(&a->archive, ERANGE,
  473. "File modification time too large");
  474. ret = ARCHIVE_FAILED;
  475. }
  476. if (archive_entry_filetype(entry) == AE_IFBLK
  477. || archive_entry_filetype(entry) == AE_IFCHR) {
  478. if (format_number(archive_entry_rdevmajor(entry),
  479. h + USTAR_rdevmajor_offset, USTAR_rdevmajor_size,
  480. USTAR_rdevmajor_max_size, strict)) {
  481. archive_set_error(&a->archive, ERANGE,
  482. "Major device number too large");
  483. ret = ARCHIVE_FAILED;
  484. }
  485. if (format_number(archive_entry_rdevminor(entry),
  486. h + USTAR_rdevminor_offset, USTAR_rdevminor_size,
  487. USTAR_rdevminor_max_size, strict)) {
  488. archive_set_error(&a->archive, ERANGE,
  489. "Minor device number too large");
  490. ret = ARCHIVE_FAILED;
  491. }
  492. }
  493. if (tartype >= 0) {
  494. h[USTAR_typeflag_offset] = tartype;
  495. } else if (mytartype >= 0) {
  496. h[USTAR_typeflag_offset] = mytartype;
  497. } else {
  498. switch (archive_entry_filetype(entry)) {
  499. case AE_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
  500. case AE_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
  501. case AE_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
  502. case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
  503. case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
  504. case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
  505. case AE_IFSOCK:
  506. archive_set_error(&a->archive,
  507. ARCHIVE_ERRNO_FILE_FORMAT,
  508. "tar format cannot archive socket");
  509. return (ARCHIVE_FAILED);
  510. default:
  511. archive_set_error(&a->archive,
  512. ARCHIVE_ERRNO_FILE_FORMAT,
  513. "tar format cannot archive this (mode=0%lo)",
  514. (unsigned long)archive_entry_mode(entry));
  515. ret = ARCHIVE_FAILED;
  516. }
  517. }
  518. checksum = 0;
  519. for (i = 0; i < 512; i++)
  520. checksum += 255 & (unsigned int)h[i];
  521. h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
  522. /* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
  523. format_octal(checksum, h + USTAR_checksum_offset, 6);
  524. return (ret);
  525. }
  526. /*
  527. * Format a number into a field, with some intelligence.
  528. */
  529. static int
  530. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  531. {
  532. int64_t limit;
  533. limit = ((int64_t)1 << (s*3));
  534. /* "Strict" only permits octal values with proper termination. */
  535. if (strict)
  536. return (format_octal(v, p, s));
  537. /*
  538. * In non-strict mode, we allow the number to overwrite one or
  539. * more bytes of the field termination. Even old tar
  540. * implementations should be able to handle this with no
  541. * problem.
  542. */
  543. if (v >= 0) {
  544. while (s <= maxsize) {
  545. if (v < limit)
  546. return (format_octal(v, p, s));
  547. s++;
  548. limit <<= 3;
  549. }
  550. }
  551. /* Base-256 can handle any number, positive or negative. */
  552. return (format_256(v, p, maxsize));
  553. }
  554. /*
  555. * Format a number into the specified field using base-256.
  556. */
  557. static int
  558. format_256(int64_t v, char *p, int s)
  559. {
  560. p += s;
  561. while (s-- > 0) {
  562. *--p = (char)(v & 0xff);
  563. v >>= 8;
  564. }
  565. *p |= 0x80; /* Set the base-256 marker bit. */
  566. return (0);
  567. }
  568. /*
  569. * Format a number into the specified field.
  570. */
  571. static int
  572. format_octal(int64_t v, char *p, int s)
  573. {
  574. int len;
  575. len = s;
  576. /* Octal values can't be negative, so use 0. */
  577. if (v < 0) {
  578. while (len-- > 0)
  579. *p++ = '0';
  580. return (-1);
  581. }
  582. p += s; /* Start at the end and work backwards. */
  583. while (s-- > 0) {
  584. *--p = (char)('0' + (v & 7));
  585. v >>= 3;
  586. }
  587. if (v == 0)
  588. return (0);
  589. /* If it overflowed, fill field with max value. */
  590. while (len-- > 0)
  591. *p++ = '7';
  592. return (-1);
  593. }
  594. static int
  595. archive_write_ustar_close(struct archive_write *a)
  596. {
  597. return (__archive_write_nulls(a, 512*2));
  598. }
  599. static int
  600. archive_write_ustar_free(struct archive_write *a)
  601. {
  602. struct ustar *ustar;
  603. ustar = (struct ustar *)a->format_data;
  604. free(ustar);
  605. a->format_data = NULL;
  606. return (ARCHIVE_OK);
  607. }
  608. static int
  609. archive_write_ustar_finish_entry(struct archive_write *a)
  610. {
  611. struct ustar *ustar;
  612. int ret;
  613. ustar = (struct ustar *)a->format_data;
  614. ret = __archive_write_nulls(a,
  615. ustar->entry_bytes_remaining + ustar->entry_padding);
  616. ustar->entry_bytes_remaining = ustar->entry_padding = 0;
  617. return (ret);
  618. }
  619. static ssize_t
  620. archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
  621. {
  622. struct ustar *ustar;
  623. int ret;
  624. ustar = (struct ustar *)a->format_data;
  625. if (s > ustar->entry_bytes_remaining)
  626. s = ustar->entry_bytes_remaining;
  627. ret = __archive_write_output(a, buff, s);
  628. ustar->entry_bytes_remaining -= s;
  629. if (ret != ARCHIVE_OK)
  630. return (ret);
  631. return (s);
  632. }