archive_write_set_format_ustar.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. 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. 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[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 pahtname, 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. if (entry_main)
  330. archive_entry_free(entry_main);
  331. return (ret);
  332. }
  333. ret2 = __archive_write_output(a, buff, 512);
  334. if (ret2 < ARCHIVE_WARN) {
  335. if (entry_main)
  336. archive_entry_free(entry_main);
  337. return (ret2);
  338. }
  339. if (ret2 < ret)
  340. ret = ret2;
  341. ustar->entry_bytes_remaining = archive_entry_size(entry);
  342. ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
  343. if (entry_main)
  344. archive_entry_free(entry_main);
  345. return (ret);
  346. }
  347. /*
  348. * Format a basic 512-byte "ustar" header.
  349. *
  350. * Returns -1 if format failed (due to field overflow).
  351. * Note that this always formats as much of the header as possible.
  352. * If "strict" is set to zero, it will extend numeric fields as
  353. * necessary (overwriting terminators or using base-256 extensions).
  354. *
  355. * This is exported so that other 'tar' formats can use it.
  356. */
  357. int
  358. __archive_write_format_header_ustar(struct archive_write *a, char h[512],
  359. struct archive_entry *entry, int tartype, int strict,
  360. struct archive_string_conv *sconv)
  361. {
  362. unsigned int checksum;
  363. int i, r, ret;
  364. size_t copy_length;
  365. const char *p, *pp;
  366. int mytartype;
  367. ret = 0;
  368. mytartype = -1;
  369. /*
  370. * The "template header" already includes the "ustar"
  371. * signature, various end-of-field markers and other required
  372. * elements.
  373. */
  374. memcpy(h, &template_header, 512);
  375. /*
  376. * Because the block is already null-filled, and strings
  377. * are allowed to exactly fill their destination (without null),
  378. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  379. */
  380. r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
  381. if (r != 0) {
  382. if (errno == ENOMEM) {
  383. archive_set_error(&a->archive, ENOMEM,
  384. "Can't allocate memory for Pathname");
  385. return (ARCHIVE_FATAL);
  386. }
  387. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  388. "Can't translate pathname '%s' to %s",
  389. pp, archive_string_conversion_charset_name(sconv));
  390. ret = ARCHIVE_WARN;
  391. }
  392. if (copy_length <= USTAR_name_size)
  393. memcpy(h + USTAR_name_offset, pp, copy_length);
  394. else {
  395. /* Store in two pieces, splitting at a '/'. */
  396. p = strchr(pp + copy_length - USTAR_name_size - 1, '/');
  397. /*
  398. * Look for the next '/' if we chose the first character
  399. * as the separator. (ustar format doesn't permit
  400. * an empty prefix.)
  401. */
  402. if (p == pp)
  403. p = strchr(p + 1, '/');
  404. /* Fail if the name won't fit. */
  405. if (!p) {
  406. /* No separator. */
  407. archive_set_error(&a->archive, ENAMETOOLONG,
  408. "Pathname too long");
  409. ret = ARCHIVE_FAILED;
  410. } else if (p[1] == '\0') {
  411. /*
  412. * The only feasible separator is a final '/';
  413. * this would result in a non-empty prefix and
  414. * an empty name, which POSIX doesn't
  415. * explicitly forbid, but it just feels wrong.
  416. */
  417. archive_set_error(&a->archive, ENAMETOOLONG,
  418. "Pathname too long");
  419. ret = ARCHIVE_FAILED;
  420. } else if (p > pp + USTAR_prefix_size) {
  421. /* Prefix is too long. */
  422. archive_set_error(&a->archive, ENAMETOOLONG,
  423. "Pathname too long");
  424. ret = ARCHIVE_FAILED;
  425. } else {
  426. /* Copy prefix and remainder to appropriate places */
  427. memcpy(h + USTAR_prefix_offset, pp, p - pp);
  428. memcpy(h + USTAR_name_offset, p + 1,
  429. pp + copy_length - p - 1);
  430. }
  431. }
  432. r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
  433. if (r != 0) {
  434. if (errno == ENOMEM) {
  435. archive_set_error(&a->archive, ENOMEM,
  436. "Can't allocate memory for Linkname");
  437. return (ARCHIVE_FATAL);
  438. }
  439. archive_set_error(&a->archive,
  440. ARCHIVE_ERRNO_FILE_FORMAT,
  441. "Can't translate linkname '%s' to %s",
  442. p, archive_string_conversion_charset_name(sconv));
  443. ret = ARCHIVE_WARN;
  444. }
  445. if (copy_length > 0)
  446. mytartype = '1';
  447. else {
  448. r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
  449. if (r != 0) {
  450. if (errno == ENOMEM) {
  451. archive_set_error(&a->archive, ENOMEM,
  452. "Can't allocate memory for Linkname");
  453. return (ARCHIVE_FATAL);
  454. }
  455. archive_set_error(&a->archive,
  456. ARCHIVE_ERRNO_FILE_FORMAT,
  457. "Can't translate linkname '%s' to %s",
  458. p, archive_string_conversion_charset_name(sconv));
  459. ret = ARCHIVE_WARN;
  460. }
  461. }
  462. if (copy_length > 0) {
  463. if (copy_length > USTAR_linkname_size) {
  464. archive_set_error(&a->archive, ENAMETOOLONG,
  465. "Link contents too long");
  466. ret = ARCHIVE_FAILED;
  467. copy_length = USTAR_linkname_size;
  468. }
  469. memcpy(h + USTAR_linkname_offset, p, copy_length);
  470. }
  471. r = archive_entry_uname_l(entry, &p, &copy_length, sconv);
  472. if (r != 0) {
  473. if (errno == ENOMEM) {
  474. archive_set_error(&a->archive, ENOMEM,
  475. "Can't allocate memory for Uname");
  476. return (ARCHIVE_FATAL);
  477. }
  478. archive_set_error(&a->archive,
  479. ARCHIVE_ERRNO_FILE_FORMAT,
  480. "Can't translate uname '%s' to %s",
  481. p, archive_string_conversion_charset_name(sconv));
  482. ret = ARCHIVE_WARN;
  483. }
  484. if (copy_length > 0) {
  485. if (copy_length > USTAR_uname_size) {
  486. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  487. "Username too long");
  488. ret = ARCHIVE_FAILED;
  489. copy_length = USTAR_uname_size;
  490. }
  491. memcpy(h + USTAR_uname_offset, p, copy_length);
  492. }
  493. r = archive_entry_gname_l(entry, &p, &copy_length, sconv);
  494. if (r != 0) {
  495. if (errno == ENOMEM) {
  496. archive_set_error(&a->archive, ENOMEM,
  497. "Can't allocate memory for Gname");
  498. return (ARCHIVE_FATAL);
  499. }
  500. archive_set_error(&a->archive,
  501. ARCHIVE_ERRNO_FILE_FORMAT,
  502. "Can't translate gname '%s' to %s",
  503. p, archive_string_conversion_charset_name(sconv));
  504. ret = ARCHIVE_WARN;
  505. }
  506. if (copy_length > 0) {
  507. if (strlen(p) > USTAR_gname_size) {
  508. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  509. "Group name too long");
  510. ret = ARCHIVE_FAILED;
  511. copy_length = USTAR_gname_size;
  512. }
  513. memcpy(h + USTAR_gname_offset, p, copy_length);
  514. }
  515. if (format_number(archive_entry_mode(entry) & 07777,
  516. h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
  517. archive_set_error(&a->archive, ERANGE,
  518. "Numeric mode too large");
  519. ret = ARCHIVE_FAILED;
  520. }
  521. if (format_number(archive_entry_uid(entry),
  522. h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
  523. archive_set_error(&a->archive, ERANGE,
  524. "Numeric user ID too large");
  525. ret = ARCHIVE_FAILED;
  526. }
  527. if (format_number(archive_entry_gid(entry),
  528. h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
  529. archive_set_error(&a->archive, ERANGE,
  530. "Numeric group ID too large");
  531. ret = ARCHIVE_FAILED;
  532. }
  533. if (format_number(archive_entry_size(entry),
  534. h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
  535. archive_set_error(&a->archive, ERANGE,
  536. "File size out of range");
  537. ret = ARCHIVE_FAILED;
  538. }
  539. if (format_number(archive_entry_mtime(entry),
  540. h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
  541. archive_set_error(&a->archive, ERANGE,
  542. "File modification time too large");
  543. ret = ARCHIVE_FAILED;
  544. }
  545. if (archive_entry_filetype(entry) == AE_IFBLK
  546. || archive_entry_filetype(entry) == AE_IFCHR) {
  547. if (format_number(archive_entry_rdevmajor(entry),
  548. h + USTAR_rdevmajor_offset, USTAR_rdevmajor_size,
  549. USTAR_rdevmajor_max_size, strict)) {
  550. archive_set_error(&a->archive, ERANGE,
  551. "Major device number too large");
  552. ret = ARCHIVE_FAILED;
  553. }
  554. if (format_number(archive_entry_rdevminor(entry),
  555. h + USTAR_rdevminor_offset, USTAR_rdevminor_size,
  556. USTAR_rdevminor_max_size, strict)) {
  557. archive_set_error(&a->archive, ERANGE,
  558. "Minor device number too large");
  559. ret = ARCHIVE_FAILED;
  560. }
  561. }
  562. if (tartype >= 0) {
  563. h[USTAR_typeflag_offset] = tartype;
  564. } else if (mytartype >= 0) {
  565. h[USTAR_typeflag_offset] = mytartype;
  566. } else {
  567. switch (archive_entry_filetype(entry)) {
  568. case AE_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
  569. case AE_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
  570. case AE_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
  571. case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
  572. case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
  573. case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
  574. case AE_IFSOCK:
  575. archive_set_error(&a->archive,
  576. ARCHIVE_ERRNO_FILE_FORMAT,
  577. "tar format cannot archive socket");
  578. return (ARCHIVE_FAILED);
  579. default:
  580. archive_set_error(&a->archive,
  581. ARCHIVE_ERRNO_FILE_FORMAT,
  582. "tar format cannot archive this (mode=0%lo)",
  583. (unsigned long)archive_entry_mode(entry));
  584. ret = ARCHIVE_FAILED;
  585. }
  586. }
  587. checksum = 0;
  588. for (i = 0; i < 512; i++)
  589. checksum += 255 & (unsigned int)h[i];
  590. h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
  591. /* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
  592. format_octal(checksum, h + USTAR_checksum_offset, 6);
  593. return (ret);
  594. }
  595. /*
  596. * Format a number into a field, with some intelligence.
  597. */
  598. static int
  599. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  600. {
  601. int64_t limit;
  602. limit = ((int64_t)1 << (s*3));
  603. /* "Strict" only permits octal values with proper termination. */
  604. if (strict)
  605. return (format_octal(v, p, s));
  606. /*
  607. * In non-strict mode, we allow the number to overwrite one or
  608. * more bytes of the field termination. Even old tar
  609. * implementations should be able to handle this with no
  610. * problem.
  611. */
  612. if (v >= 0) {
  613. while (s <= maxsize) {
  614. if (v < limit)
  615. return (format_octal(v, p, s));
  616. s++;
  617. limit <<= 3;
  618. }
  619. }
  620. /* Base-256 can handle any number, positive or negative. */
  621. return (format_256(v, p, maxsize));
  622. }
  623. /*
  624. * Format a number into the specified field using base-256.
  625. */
  626. static int
  627. format_256(int64_t v, char *p, int s)
  628. {
  629. p += s;
  630. while (s-- > 0) {
  631. *--p = (char)(v & 0xff);
  632. v >>= 8;
  633. }
  634. *p |= 0x80; /* Set the base-256 marker bit. */
  635. return (0);
  636. }
  637. /*
  638. * Format a number into the specified field.
  639. */
  640. static int
  641. format_octal(int64_t v, char *p, int s)
  642. {
  643. int len;
  644. len = s;
  645. /* Octal values can't be negative, so use 0. */
  646. if (v < 0) {
  647. while (len-- > 0)
  648. *p++ = '0';
  649. return (-1);
  650. }
  651. p += s; /* Start at the end and work backwards. */
  652. while (s-- > 0) {
  653. *--p = (char)('0' + (v & 7));
  654. v >>= 3;
  655. }
  656. if (v == 0)
  657. return (0);
  658. /* If it overflowed, fill field with max value. */
  659. while (len-- > 0)
  660. *p++ = '7';
  661. return (-1);
  662. }
  663. static int
  664. archive_write_ustar_close(struct archive_write *a)
  665. {
  666. return (__archive_write_nulls(a, 512*2));
  667. }
  668. static int
  669. archive_write_ustar_free(struct archive_write *a)
  670. {
  671. struct ustar *ustar;
  672. ustar = (struct ustar *)a->format_data;
  673. free(ustar);
  674. a->format_data = NULL;
  675. return (ARCHIVE_OK);
  676. }
  677. static int
  678. archive_write_ustar_finish_entry(struct archive_write *a)
  679. {
  680. struct ustar *ustar;
  681. int ret;
  682. ustar = (struct ustar *)a->format_data;
  683. ret = __archive_write_nulls(a,
  684. (size_t)(ustar->entry_bytes_remaining + ustar->entry_padding));
  685. ustar->entry_bytes_remaining = ustar->entry_padding = 0;
  686. return (ret);
  687. }
  688. static ssize_t
  689. archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
  690. {
  691. struct ustar *ustar;
  692. int ret;
  693. ustar = (struct ustar *)a->format_data;
  694. if (s > ustar->entry_bytes_remaining)
  695. s = (size_t)ustar->entry_bytes_remaining;
  696. ret = __archive_write_output(a, buff, s);
  697. ustar->entry_bytes_remaining -= s;
  698. if (ret != ARCHIVE_OK)
  699. return (ret);
  700. return (s);
  701. }