archive_write_set_format_ustar.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: 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. #include "archive_write_set_format_private.h"
  44. struct ustar {
  45. uint64_t entry_bytes_remaining;
  46. uint64_t entry_padding;
  47. struct archive_string_conv *opt_sconv;
  48. struct archive_string_conv *sconv_default;
  49. int init_default_conversion;
  50. };
  51. /*
  52. * Define structure of POSIX 'ustar' tar header.
  53. */
  54. #define USTAR_name_offset 0
  55. #define USTAR_name_size 100
  56. #define USTAR_mode_offset 100
  57. #define USTAR_mode_size 6
  58. #define USTAR_mode_max_size 8
  59. #define USTAR_uid_offset 108
  60. #define USTAR_uid_size 6
  61. #define USTAR_uid_max_size 8
  62. #define USTAR_gid_offset 116
  63. #define USTAR_gid_size 6
  64. #define USTAR_gid_max_size 8
  65. #define USTAR_size_offset 124
  66. #define USTAR_size_size 11
  67. #define USTAR_size_max_size 12
  68. #define USTAR_mtime_offset 136
  69. #define USTAR_mtime_size 11
  70. #define USTAR_mtime_max_size 11
  71. #define USTAR_checksum_offset 148
  72. #define USTAR_checksum_size 8
  73. #define USTAR_typeflag_offset 156
  74. #define USTAR_typeflag_size 1
  75. #define USTAR_linkname_offset 157
  76. #define USTAR_linkname_size 100
  77. #define USTAR_magic_offset 257
  78. #define USTAR_magic_size 6
  79. #define USTAR_version_offset 263
  80. #define USTAR_version_size 2
  81. #define USTAR_uname_offset 265
  82. #define USTAR_uname_size 32
  83. #define USTAR_gname_offset 297
  84. #define USTAR_gname_size 32
  85. #define USTAR_rdevmajor_offset 329
  86. #define USTAR_rdevmajor_size 6
  87. #define USTAR_rdevmajor_max_size 8
  88. #define USTAR_rdevminor_offset 337
  89. #define USTAR_rdevminor_size 6
  90. #define USTAR_rdevminor_max_size 8
  91. #define USTAR_prefix_offset 345
  92. #define USTAR_prefix_size 155
  93. #define USTAR_padding_offset 500
  94. #define USTAR_padding_size 12
  95. /*
  96. * A filled-in copy of the header for initialization.
  97. */
  98. static const char template_header[] = {
  99. /* name: 100 bytes */
  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,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,
  103. 0,0,0,0,
  104. /* Mode, space-null termination: 8 bytes */
  105. '0','0','0','0','0','0', ' ','\0',
  106. /* uid, space-null termination: 8 bytes */
  107. '0','0','0','0','0','0', ' ','\0',
  108. /* gid, space-null termination: 8 bytes */
  109. '0','0','0','0','0','0', ' ','\0',
  110. /* size, space termination: 12 bytes */
  111. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  112. /* mtime, space termination: 12 bytes */
  113. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  114. /* Initial checksum value: 8 spaces */
  115. ' ',' ',' ',' ',' ',' ',' ',' ',
  116. /* Typeflag: 1 byte */
  117. '0', /* '0' = regular file */
  118. /* Linkname: 100 bytes */
  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,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,
  122. 0,0,0,0,
  123. /* Magic: 6 bytes, Version: 2 bytes */
  124. 'u','s','t','a','r','\0', '0','0',
  125. /* Uname: 32 bytes */
  126. 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,
  127. /* Gname: 32 bytes */
  128. 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,
  129. /* rdevmajor + space/null padding: 8 bytes */
  130. '0','0','0','0','0','0', ' ','\0',
  131. /* rdevminor + space/null padding: 8 bytes */
  132. '0','0','0','0','0','0', ' ','\0',
  133. /* Prefix: 155 bytes */
  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,0,0,0,0,0,
  138. 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,
  139. /* Padding: 12 bytes */
  140. 0,0,0,0,0,0,0,0, 0,0,0,0
  141. };
  142. static ssize_t archive_write_ustar_data(struct archive_write *a, const void *buff,
  143. size_t s);
  144. static int archive_write_ustar_free(struct archive_write *);
  145. static int archive_write_ustar_close(struct archive_write *);
  146. static int archive_write_ustar_finish_entry(struct archive_write *);
  147. static int archive_write_ustar_header(struct archive_write *,
  148. struct archive_entry *entry);
  149. static int archive_write_ustar_options(struct archive_write *,
  150. const char *, const char *);
  151. static int format_256(int64_t, char *, int);
  152. static int format_number(int64_t, char *, int size, int max, int strict);
  153. static int format_octal(int64_t, char *, int);
  154. /*
  155. * Set output format to 'ustar' format.
  156. */
  157. int
  158. archive_write_set_format_ustar(struct archive *_a)
  159. {
  160. struct archive_write *a = (struct archive_write *)_a;
  161. struct ustar *ustar;
  162. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  163. ARCHIVE_STATE_NEW, "archive_write_set_format_ustar");
  164. /* If someone else was already registered, unregister them. */
  165. if (a->format_free != NULL)
  166. (a->format_free)(a);
  167. /* Basic internal sanity test. */
  168. if (sizeof(template_header) != 512) {
  169. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  170. "Internal: template_header wrong size: %zu should be 512",
  171. sizeof(template_header));
  172. return (ARCHIVE_FATAL);
  173. }
  174. ustar = (struct ustar *)calloc(1, sizeof(*ustar));
  175. if (ustar == NULL) {
  176. archive_set_error(&a->archive, ENOMEM,
  177. "Can't allocate ustar data");
  178. return (ARCHIVE_FATAL);
  179. }
  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. return (ret);
  212. }
  213. /* Note: The "warn" return is just to inform the options
  214. * supervisor that we didn't handle it. It will generate
  215. * a suitable error if no one used this option. */
  216. return (ARCHIVE_WARN);
  217. }
  218. static int
  219. archive_write_ustar_header(struct archive_write *a, struct archive_entry *entry)
  220. {
  221. char buff[512];
  222. int ret, ret2;
  223. struct ustar *ustar;
  224. struct archive_entry *entry_main;
  225. struct archive_string_conv *sconv;
  226. ustar = (struct ustar *)a->format_data;
  227. /* Setup default string conversion. */
  228. if (ustar->opt_sconv == NULL) {
  229. if (!ustar->init_default_conversion) {
  230. ustar->sconv_default =
  231. archive_string_default_conversion_for_write(&(a->archive));
  232. ustar->init_default_conversion = 1;
  233. }
  234. sconv = ustar->sconv_default;
  235. } else
  236. sconv = ustar->opt_sconv;
  237. /* Sanity check. */
  238. if (archive_entry_pathname(entry) == NULL) {
  239. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  240. "Can't record entry in tar file without pathname");
  241. return (ARCHIVE_FAILED);
  242. }
  243. /* Only regular files (not hardlinks) have data. */
  244. if (archive_entry_hardlink(entry) != NULL ||
  245. archive_entry_symlink(entry) != NULL ||
  246. !(archive_entry_filetype(entry) == AE_IFREG))
  247. archive_entry_set_size(entry, 0);
  248. if (AE_IFDIR == archive_entry_filetype(entry)) {
  249. const char *p;
  250. size_t path_length;
  251. /*
  252. * Ensure a trailing '/'. Modify the entry so
  253. * the client sees the change.
  254. */
  255. #if defined(_WIN32) && !defined(__CYGWIN__)
  256. const wchar_t *wp;
  257. wp = archive_entry_pathname_w(entry);
  258. if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
  259. struct archive_wstring ws;
  260. archive_string_init(&ws);
  261. path_length = wcslen(wp);
  262. if (archive_wstring_ensure(&ws,
  263. path_length + 2) == NULL) {
  264. archive_set_error(&a->archive, ENOMEM,
  265. "Can't allocate ustar data");
  266. archive_wstring_free(&ws);
  267. return(ARCHIVE_FATAL);
  268. }
  269. /* Should we keep '\' ? */
  270. if (wp[path_length -1] == L'\\')
  271. path_length--;
  272. archive_wstrncpy(&ws, wp, path_length);
  273. archive_wstrappend_wchar(&ws, L'/');
  274. archive_entry_copy_pathname_w(entry, ws.s);
  275. archive_wstring_free(&ws);
  276. p = NULL;
  277. } else
  278. #endif
  279. p = archive_entry_pathname(entry);
  280. /*
  281. * On Windows, this is a backup operation just in
  282. * case getting WCS failed. On POSIX, this is a
  283. * normal operation.
  284. */
  285. if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
  286. struct archive_string as;
  287. archive_string_init(&as);
  288. path_length = strlen(p);
  289. if (archive_string_ensure(&as,
  290. path_length + 2) == NULL) {
  291. archive_set_error(&a->archive, ENOMEM,
  292. "Can't allocate ustar data");
  293. archive_string_free(&as);
  294. return(ARCHIVE_FATAL);
  295. }
  296. #if defined(_WIN32) && !defined(__CYGWIN__)
  297. /* NOTE: This might break the pathname
  298. * if the current code page is CP932 and
  299. * the pathname includes a character '\'
  300. * as a part of its multibyte pathname. */
  301. if (p[strlen(p) -1] == '\\')
  302. path_length--;
  303. else
  304. #endif
  305. archive_strncpy(&as, p, path_length);
  306. archive_strappend_char(&as, '/');
  307. archive_entry_copy_pathname(entry, as.s);
  308. archive_string_free(&as);
  309. }
  310. }
  311. #if defined(_WIN32) && !defined(__CYGWIN__)
  312. /* Make sure the path separators in pathname, hardlink and symlink
  313. * are all slash '/', not the Windows path separator '\'. */
  314. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  315. if (entry_main == NULL) {
  316. archive_set_error(&a->archive, ENOMEM,
  317. "Can't allocate ustar data");
  318. return(ARCHIVE_FATAL);
  319. }
  320. if (entry != entry_main)
  321. entry = entry_main;
  322. else
  323. entry_main = NULL;
  324. #else
  325. entry_main = NULL;
  326. #endif
  327. ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1, sconv);
  328. if (ret < ARCHIVE_WARN) {
  329. archive_entry_free(entry_main);
  330. return (ret);
  331. }
  332. ret2 = __archive_write_output(a, buff, 512);
  333. if (ret2 < ARCHIVE_WARN) {
  334. archive_entry_free(entry_main);
  335. return (ret2);
  336. }
  337. if (ret2 < ret)
  338. ret = ret2;
  339. ustar->entry_bytes_remaining = archive_entry_size(entry);
  340. ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
  341. archive_entry_free(entry_main);
  342. return (ret);
  343. }
  344. /*
  345. * Format a basic 512-byte "ustar" header.
  346. *
  347. * Returns -1 if format failed (due to field overflow).
  348. * Note that this always formats as much of the header as possible.
  349. * If "strict" is set to zero, it will extend numeric fields as
  350. * necessary (overwriting terminators or using base-256 extensions).
  351. *
  352. * This is exported so that other 'tar' formats can use it.
  353. */
  354. int
  355. __archive_write_format_header_ustar(struct archive_write *a, char h[512],
  356. struct archive_entry *entry, int tartype, int strict,
  357. struct archive_string_conv *sconv)
  358. {
  359. unsigned int checksum;
  360. int i, r, ret;
  361. size_t copy_length;
  362. const char *p, *pp;
  363. int mytartype;
  364. ret = 0;
  365. mytartype = -1;
  366. /*
  367. * The "template header" already includes the "ustar"
  368. * signature, various end-of-field markers and other required
  369. * elements.
  370. */
  371. memcpy(h, &template_header, 512);
  372. /*
  373. * Because the block is already null-filled, and strings
  374. * are allowed to exactly fill their destination (without null),
  375. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  376. */
  377. r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
  378. if (r != 0) {
  379. if (errno == ENOMEM) {
  380. archive_set_error(&a->archive, ENOMEM,
  381. "Can't allocate memory for Pathname");
  382. return (ARCHIVE_FATAL);
  383. }
  384. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  385. "Can't translate pathname '%s' to %s",
  386. pp, archive_string_conversion_charset_name(sconv));
  387. ret = ARCHIVE_WARN;
  388. }
  389. if (copy_length <= USTAR_name_size)
  390. memcpy(h + USTAR_name_offset, pp, copy_length);
  391. else {
  392. /* Store in two pieces, splitting at a '/'. */
  393. p = strchr(pp + copy_length - USTAR_name_size - 1, '/');
  394. /*
  395. * Look for the next '/' if we chose the first character
  396. * as the separator. (ustar format doesn't permit
  397. * an empty prefix.)
  398. */
  399. if (p == pp)
  400. p = strchr(p + 1, '/');
  401. /* Fail if the name won't fit. */
  402. if (!p) {
  403. /* No separator. */
  404. archive_set_error(&a->archive, ENAMETOOLONG,
  405. "Pathname too long");
  406. ret = ARCHIVE_FAILED;
  407. } else if (p[1] == '\0') {
  408. /*
  409. * The only feasible separator is a final '/';
  410. * this would result in a non-empty prefix and
  411. * an empty name, which POSIX doesn't
  412. * explicitly forbid, but it just feels wrong.
  413. */
  414. archive_set_error(&a->archive, ENAMETOOLONG,
  415. "Pathname too long");
  416. ret = ARCHIVE_FAILED;
  417. } else if (p > pp + USTAR_prefix_size) {
  418. /* Prefix is too long. */
  419. archive_set_error(&a->archive, ENAMETOOLONG,
  420. "Pathname too long");
  421. ret = ARCHIVE_FAILED;
  422. } else {
  423. /* Copy prefix and remainder to appropriate places */
  424. memcpy(h + USTAR_prefix_offset, pp, p - pp);
  425. memcpy(h + USTAR_name_offset, p + 1,
  426. pp + copy_length - p - 1);
  427. }
  428. }
  429. r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
  430. if (r != 0) {
  431. if (errno == ENOMEM) {
  432. archive_set_error(&a->archive, ENOMEM,
  433. "Can't allocate memory for Linkname");
  434. return (ARCHIVE_FATAL);
  435. }
  436. archive_set_error(&a->archive,
  437. ARCHIVE_ERRNO_FILE_FORMAT,
  438. "Can't translate linkname '%s' to %s",
  439. p, archive_string_conversion_charset_name(sconv));
  440. ret = ARCHIVE_WARN;
  441. }
  442. if (copy_length > 0)
  443. mytartype = '1';
  444. else {
  445. r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
  446. if (r != 0) {
  447. if (errno == ENOMEM) {
  448. archive_set_error(&a->archive, ENOMEM,
  449. "Can't allocate memory for Linkname");
  450. return (ARCHIVE_FATAL);
  451. }
  452. archive_set_error(&a->archive,
  453. ARCHIVE_ERRNO_FILE_FORMAT,
  454. "Can't translate linkname '%s' to %s",
  455. p, archive_string_conversion_charset_name(sconv));
  456. ret = ARCHIVE_WARN;
  457. }
  458. }
  459. if (copy_length > 0) {
  460. if (copy_length > USTAR_linkname_size) {
  461. archive_set_error(&a->archive, ENAMETOOLONG,
  462. "Link contents too long");
  463. ret = ARCHIVE_FAILED;
  464. copy_length = USTAR_linkname_size;
  465. }
  466. memcpy(h + USTAR_linkname_offset, p, copy_length);
  467. }
  468. r = archive_entry_uname_l(entry, &p, &copy_length, sconv);
  469. if (r != 0) {
  470. if (errno == ENOMEM) {
  471. archive_set_error(&a->archive, ENOMEM,
  472. "Can't allocate memory for Uname");
  473. return (ARCHIVE_FATAL);
  474. }
  475. archive_set_error(&a->archive,
  476. ARCHIVE_ERRNO_FILE_FORMAT,
  477. "Can't translate uname '%s' to %s",
  478. p, archive_string_conversion_charset_name(sconv));
  479. ret = ARCHIVE_WARN;
  480. }
  481. if (copy_length > 0) {
  482. if (copy_length > USTAR_uname_size) {
  483. if (tartype != 'x') {
  484. archive_set_error(&a->archive,
  485. ARCHIVE_ERRNO_MISC, "Username too long");
  486. ret = ARCHIVE_FAILED;
  487. }
  488. copy_length = USTAR_uname_size;
  489. }
  490. memcpy(h + USTAR_uname_offset, p, copy_length);
  491. }
  492. r = archive_entry_gname_l(entry, &p, &copy_length, sconv);
  493. if (r != 0) {
  494. if (errno == ENOMEM) {
  495. archive_set_error(&a->archive, ENOMEM,
  496. "Can't allocate memory for Gname");
  497. return (ARCHIVE_FATAL);
  498. }
  499. archive_set_error(&a->archive,
  500. ARCHIVE_ERRNO_FILE_FORMAT,
  501. "Can't translate gname '%s' to %s",
  502. p, archive_string_conversion_charset_name(sconv));
  503. ret = ARCHIVE_WARN;
  504. }
  505. if (copy_length > 0) {
  506. if (strlen(p) > USTAR_gname_size) {
  507. if (tartype != 'x') {
  508. archive_set_error(&a->archive,
  509. ARCHIVE_ERRNO_MISC, "Group name too long");
  510. ret = ARCHIVE_FAILED;
  511. }
  512. copy_length = USTAR_gname_size;
  513. }
  514. memcpy(h + USTAR_gname_offset, p, copy_length);
  515. }
  516. if (format_number(archive_entry_mode(entry) & 07777,
  517. h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
  518. archive_set_error(&a->archive, ERANGE,
  519. "Numeric mode too large");
  520. ret = ARCHIVE_FAILED;
  521. }
  522. if (format_number(archive_entry_uid(entry),
  523. h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
  524. archive_set_error(&a->archive, ERANGE,
  525. "Numeric user ID too large");
  526. ret = ARCHIVE_FAILED;
  527. }
  528. if (format_number(archive_entry_gid(entry),
  529. h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
  530. archive_set_error(&a->archive, ERANGE,
  531. "Numeric group ID too large");
  532. ret = ARCHIVE_FAILED;
  533. }
  534. if (format_number(archive_entry_size(entry),
  535. h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
  536. archive_set_error(&a->archive, ERANGE,
  537. "File size out of range");
  538. ret = ARCHIVE_FAILED;
  539. }
  540. if (format_number(archive_entry_mtime(entry),
  541. h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
  542. archive_set_error(&a->archive, ERANGE,
  543. "File modification time too large");
  544. ret = ARCHIVE_FAILED;
  545. }
  546. if (archive_entry_filetype(entry) == AE_IFBLK
  547. || archive_entry_filetype(entry) == AE_IFCHR) {
  548. if (format_number(archive_entry_rdevmajor(entry),
  549. h + USTAR_rdevmajor_offset, USTAR_rdevmajor_size,
  550. USTAR_rdevmajor_max_size, strict)) {
  551. archive_set_error(&a->archive, ERANGE,
  552. "Major device number too large");
  553. ret = ARCHIVE_FAILED;
  554. }
  555. if (format_number(archive_entry_rdevminor(entry),
  556. h + USTAR_rdevminor_offset, USTAR_rdevminor_size,
  557. USTAR_rdevminor_max_size, strict)) {
  558. archive_set_error(&a->archive, ERANGE,
  559. "Minor device number too large");
  560. ret = ARCHIVE_FAILED;
  561. }
  562. }
  563. if (tartype >= 0) {
  564. h[USTAR_typeflag_offset] = tartype;
  565. } else if (mytartype >= 0) {
  566. h[USTAR_typeflag_offset] = mytartype;
  567. } else {
  568. switch (archive_entry_filetype(entry)) {
  569. case AE_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
  570. case AE_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
  571. case AE_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
  572. case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
  573. case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
  574. case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
  575. default: /* AE_IFSOCK and unknown */
  576. __archive_write_entry_filetype_unsupported(
  577. &a->archive, entry, "ustar");
  578. ret = ARCHIVE_FAILED;
  579. }
  580. }
  581. checksum = 0;
  582. for (i = 0; i < 512; i++)
  583. checksum += 255 & (unsigned int)h[i];
  584. h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
  585. /* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
  586. format_octal(checksum, h + USTAR_checksum_offset, 6);
  587. return (ret);
  588. }
  589. /*
  590. * Format a number into a field, with some intelligence.
  591. */
  592. static int
  593. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  594. {
  595. int64_t limit;
  596. limit = ((int64_t)1 << (s*3));
  597. /* "Strict" only permits octal values with proper termination. */
  598. if (strict)
  599. return (format_octal(v, p, s));
  600. /*
  601. * In non-strict mode, we allow the number to overwrite one or
  602. * more bytes of the field termination. Even old tar
  603. * implementations should be able to handle this with no
  604. * problem.
  605. */
  606. if (v >= 0) {
  607. while (s <= maxsize) {
  608. if (v < limit)
  609. return (format_octal(v, p, s));
  610. s++;
  611. limit <<= 3;
  612. }
  613. }
  614. /* Base-256 can handle any number, positive or negative. */
  615. return (format_256(v, p, maxsize));
  616. }
  617. /*
  618. * Format a number into the specified field using base-256.
  619. */
  620. static int
  621. format_256(int64_t v, char *p, int s)
  622. {
  623. p += s;
  624. while (s-- > 0) {
  625. *--p = (char)(v & 0xff);
  626. v >>= 8;
  627. }
  628. *p |= 0x80; /* Set the base-256 marker bit. */
  629. return (0);
  630. }
  631. /*
  632. * Format a number into the specified field.
  633. */
  634. static int
  635. format_octal(int64_t v, char *p, int s)
  636. {
  637. int len;
  638. len = s;
  639. /* Octal values can't be negative, so use 0. */
  640. if (v < 0) {
  641. while (len-- > 0)
  642. *p++ = '0';
  643. return (-1);
  644. }
  645. p += s; /* Start at the end and work backwards. */
  646. while (s-- > 0) {
  647. *--p = (char)('0' + (v & 7));
  648. v >>= 3;
  649. }
  650. if (v == 0)
  651. return (0);
  652. /* If it overflowed, fill field with max value. */
  653. while (len-- > 0)
  654. *p++ = '7';
  655. return (-1);
  656. }
  657. static int
  658. archive_write_ustar_close(struct archive_write *a)
  659. {
  660. return (__archive_write_nulls(a, 512*2));
  661. }
  662. static int
  663. archive_write_ustar_free(struct archive_write *a)
  664. {
  665. struct ustar *ustar;
  666. ustar = (struct ustar *)a->format_data;
  667. free(ustar);
  668. a->format_data = NULL;
  669. return (ARCHIVE_OK);
  670. }
  671. static int
  672. archive_write_ustar_finish_entry(struct archive_write *a)
  673. {
  674. struct ustar *ustar;
  675. int ret;
  676. ustar = (struct ustar *)a->format_data;
  677. ret = __archive_write_nulls(a,
  678. (size_t)(ustar->entry_bytes_remaining + ustar->entry_padding));
  679. ustar->entry_bytes_remaining = ustar->entry_padding = 0;
  680. return (ret);
  681. }
  682. static ssize_t
  683. archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
  684. {
  685. struct ustar *ustar;
  686. int ret;
  687. ustar = (struct ustar *)a->format_data;
  688. if (s > ustar->entry_bytes_remaining)
  689. s = (size_t)ustar->entry_bytes_remaining;
  690. ret = __archive_write_output(a, buff, s);
  691. ustar->entry_bytes_remaining -= s;
  692. if (ret != ARCHIVE_OK)
  693. return (ret);
  694. return (s);
  695. }