archive_write_set_format_gnutar.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*-
  2. * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
  3. * Author: Jonas Gastal <[email protected]>
  4. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "archive_platform.h"
  29. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_gnu_tar.c 191579 2009-04-27 18:35:03Z gastal $");
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #include <stdio.h>
  34. #ifdef HAVE_STDLIB_H
  35. #include <stdlib.h>
  36. #endif
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40. #include "archive.h"
  41. #include "archive_entry.h"
  42. #include "archive_entry_locale.h"
  43. #include "archive_private.h"
  44. #include "archive_write_private.h"
  45. #include "archive_write_set_format_private.h"
  46. struct gnutar {
  47. uint64_t entry_bytes_remaining;
  48. uint64_t entry_padding;
  49. const char * linkname;
  50. size_t linkname_length;
  51. const char * pathname;
  52. size_t pathname_length;
  53. const char * uname;
  54. size_t uname_length;
  55. const char * gname;
  56. size_t gname_length;
  57. struct archive_string_conv *opt_sconv;
  58. struct archive_string_conv *sconv_default;
  59. int init_default_conversion;
  60. };
  61. /*
  62. * Define structure of GNU tar header.
  63. */
  64. #define GNUTAR_name_offset 0
  65. #define GNUTAR_name_size 100
  66. #define GNUTAR_mode_offset 100
  67. #define GNUTAR_mode_size 7
  68. #define GNUTAR_mode_max_size 8
  69. #define GNUTAR_uid_offset 108
  70. #define GNUTAR_uid_size 7
  71. #define GNUTAR_uid_max_size 8
  72. #define GNUTAR_gid_offset 116
  73. #define GNUTAR_gid_size 7
  74. #define GNUTAR_gid_max_size 8
  75. #define GNUTAR_size_offset 124
  76. #define GNUTAR_size_size 11
  77. #define GNUTAR_size_max_size 12
  78. #define GNUTAR_mtime_offset 136
  79. #define GNUTAR_mtime_size 11
  80. #define GNUTAR_mtime_max_size 11
  81. #define GNUTAR_checksum_offset 148
  82. #define GNUTAR_checksum_size 8
  83. #define GNUTAR_typeflag_offset 156
  84. #define GNUTAR_typeflag_size 1
  85. #define GNUTAR_linkname_offset 157
  86. #define GNUTAR_linkname_size 100
  87. #define GNUTAR_magic_offset 257
  88. #define GNUTAR_magic_size 6
  89. #define GNUTAR_version_offset 263
  90. #define GNUTAR_version_size 2
  91. #define GNUTAR_uname_offset 265
  92. #define GNUTAR_uname_size 32
  93. #define GNUTAR_gname_offset 297
  94. #define GNUTAR_gname_size 32
  95. #define GNUTAR_rdevmajor_offset 329
  96. #define GNUTAR_rdevmajor_size 6
  97. #define GNUTAR_rdevmajor_max_size 8
  98. #define GNUTAR_rdevminor_offset 337
  99. #define GNUTAR_rdevminor_size 6
  100. #define GNUTAR_rdevminor_max_size 8
  101. /*
  102. * A filled-in copy of the header for initialization.
  103. */
  104. static const char template_header[] = {
  105. /* name: 100 bytes */
  106. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  107. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  108. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  109. 0,0,0,0,
  110. /* Mode, null termination: 8 bytes */
  111. '0','0','0','0','0','0', '0','\0',
  112. /* uid, null termination: 8 bytes */
  113. '0','0','0','0','0','0', '0','\0',
  114. /* gid, null termination: 8 bytes */
  115. '0','0','0','0','0','0', '0','\0',
  116. /* size, space termination: 12 bytes */
  117. '0','0','0','0','0','0','0','0','0','0','0', '\0',
  118. /* mtime, space termination: 12 bytes */
  119. '0','0','0','0','0','0','0','0','0','0','0', '\0',
  120. /* Initial checksum value: 8 spaces */
  121. ' ',' ',' ',' ',' ',' ',' ',' ',
  122. /* Typeflag: 1 byte */
  123. '0', /* '0' = regular file */
  124. /* Linkname: 100 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. 0,0,0,0,0,0,0,0, 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. 0,0,0,0,0,0,0,0, 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. 0,0,0,0,
  129. /* Magic: 8 bytes */
  130. 'u','s','t','a','r',' ', ' ','\0',
  131. /* Uname: 32 bytes */
  132. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  133. /* Gname: 32 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. /* rdevmajor + null padding: 8 bytes */
  136. '\0','\0','\0','\0','\0','\0', '\0','\0',
  137. /* rdevminor + null padding: 8 bytes */
  138. '\0','\0','\0','\0','\0','\0', '\0','\0',
  139. /* Padding: 167 bytes */
  140. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  141. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  142. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  143. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  144. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  145. 0,0,0,0,0,0,0
  146. };
  147. static int archive_write_gnutar_options(struct archive_write *,
  148. const char *, const char *);
  149. static int archive_format_gnutar_header(struct archive_write *, char h[512],
  150. struct archive_entry *, int tartype);
  151. static int archive_write_gnutar_header(struct archive_write *,
  152. struct archive_entry *entry);
  153. static ssize_t archive_write_gnutar_data(struct archive_write *a, const void *buff,
  154. size_t s);
  155. static int archive_write_gnutar_free(struct archive_write *);
  156. static int archive_write_gnutar_close(struct archive_write *);
  157. static int archive_write_gnutar_finish_entry(struct archive_write *);
  158. static int format_256(int64_t, char *, int);
  159. static int format_number(int64_t, char *, int size, int maxsize);
  160. static int format_octal(int64_t, char *, int);
  161. /*
  162. * Set output format to 'GNU tar' format.
  163. */
  164. int
  165. archive_write_set_format_gnutar(struct archive *_a)
  166. {
  167. struct archive_write *a = (struct archive_write *)_a;
  168. struct gnutar *gnutar;
  169. gnutar = (struct gnutar *)calloc(1, sizeof(*gnutar));
  170. if (gnutar == NULL) {
  171. archive_set_error(&a->archive, ENOMEM,
  172. "Can't allocate gnutar data");
  173. return (ARCHIVE_FATAL);
  174. }
  175. a->format_data = gnutar;
  176. a->format_name = "gnutar";
  177. a->format_options = archive_write_gnutar_options;
  178. a->format_write_header = archive_write_gnutar_header;
  179. a->format_write_data = archive_write_gnutar_data;
  180. a->format_close = archive_write_gnutar_close;
  181. a->format_free = archive_write_gnutar_free;
  182. a->format_finish_entry = archive_write_gnutar_finish_entry;
  183. a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
  184. a->archive.archive_format_name = "GNU tar";
  185. return (ARCHIVE_OK);
  186. }
  187. static int
  188. archive_write_gnutar_options(struct archive_write *a, const char *key,
  189. const char *val)
  190. {
  191. struct gnutar *gnutar = (struct gnutar *)a->format_data;
  192. int ret = ARCHIVE_FAILED;
  193. if (strcmp(key, "hdrcharset") == 0) {
  194. if (val == NULL || val[0] == 0)
  195. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  196. "%s: hdrcharset option needs a character-set name",
  197. a->format_name);
  198. else {
  199. gnutar->opt_sconv = archive_string_conversion_to_charset(
  200. &a->archive, val, 0);
  201. if (gnutar->opt_sconv != NULL)
  202. ret = ARCHIVE_OK;
  203. else
  204. ret = ARCHIVE_FATAL;
  205. }
  206. return (ret);
  207. }
  208. /* Note: The "warn" return is just to inform the options
  209. * supervisor that we didn't handle it. It will generate
  210. * a suitable error if no one used this option. */
  211. return (ARCHIVE_WARN);
  212. }
  213. static int
  214. archive_write_gnutar_close(struct archive_write *a)
  215. {
  216. return (__archive_write_nulls(a, 512*2));
  217. }
  218. static int
  219. archive_write_gnutar_free(struct archive_write *a)
  220. {
  221. struct gnutar *gnutar;
  222. gnutar = (struct gnutar *)a->format_data;
  223. free(gnutar);
  224. a->format_data = NULL;
  225. return (ARCHIVE_OK);
  226. }
  227. static int
  228. archive_write_gnutar_finish_entry(struct archive_write *a)
  229. {
  230. struct gnutar *gnutar;
  231. int ret;
  232. gnutar = (struct gnutar *)a->format_data;
  233. ret = __archive_write_nulls(a, (size_t)
  234. (gnutar->entry_bytes_remaining + gnutar->entry_padding));
  235. gnutar->entry_bytes_remaining = gnutar->entry_padding = 0;
  236. return (ret);
  237. }
  238. static ssize_t
  239. archive_write_gnutar_data(struct archive_write *a, const void *buff, size_t s)
  240. {
  241. struct gnutar *gnutar;
  242. int ret;
  243. gnutar = (struct gnutar *)a->format_data;
  244. if (s > gnutar->entry_bytes_remaining)
  245. s = (size_t)gnutar->entry_bytes_remaining;
  246. ret = __archive_write_output(a, buff, s);
  247. gnutar->entry_bytes_remaining -= s;
  248. if (ret != ARCHIVE_OK)
  249. return (ret);
  250. return (s);
  251. }
  252. static int
  253. archive_write_gnutar_header(struct archive_write *a,
  254. struct archive_entry *entry)
  255. {
  256. char buff[512];
  257. int r, ret, ret2 = ARCHIVE_OK;
  258. int tartype;
  259. struct gnutar *gnutar;
  260. struct archive_string_conv *sconv;
  261. struct archive_entry *entry_main;
  262. gnutar = (struct gnutar *)a->format_data;
  263. /* Setup default string conversion. */
  264. if (gnutar->opt_sconv == NULL) {
  265. if (!gnutar->init_default_conversion) {
  266. gnutar->sconv_default =
  267. archive_string_default_conversion_for_write(
  268. &(a->archive));
  269. gnutar->init_default_conversion = 1;
  270. }
  271. sconv = gnutar->sconv_default;
  272. } else
  273. sconv = gnutar->opt_sconv;
  274. /* Only regular files (not hardlinks) have data. */
  275. if (archive_entry_hardlink(entry) != NULL ||
  276. archive_entry_symlink(entry) != NULL ||
  277. !(archive_entry_filetype(entry) == AE_IFREG))
  278. archive_entry_set_size(entry, 0);
  279. if (AE_IFDIR == archive_entry_filetype(entry)) {
  280. const char *p;
  281. size_t path_length;
  282. /*
  283. * Ensure a trailing '/'. Modify the entry so
  284. * the client sees the change.
  285. */
  286. #if defined(_WIN32) && !defined(__CYGWIN__)
  287. const wchar_t *wp;
  288. wp = archive_entry_pathname_w(entry);
  289. if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
  290. struct archive_wstring ws;
  291. archive_string_init(&ws);
  292. path_length = wcslen(wp);
  293. if (archive_wstring_ensure(&ws,
  294. path_length + 2) == NULL) {
  295. archive_set_error(&a->archive, ENOMEM,
  296. "Can't allocate ustar data");
  297. archive_wstring_free(&ws);
  298. return(ARCHIVE_FATAL);
  299. }
  300. /* Should we keep '\' ? */
  301. if (wp[path_length -1] == L'\\')
  302. path_length--;
  303. archive_wstrncpy(&ws, wp, path_length);
  304. archive_wstrappend_wchar(&ws, L'/');
  305. archive_entry_copy_pathname_w(entry, ws.s);
  306. archive_wstring_free(&ws);
  307. p = NULL;
  308. } else
  309. #endif
  310. p = archive_entry_pathname(entry);
  311. /*
  312. * On Windows, this is a backup operation just in
  313. * case getting WCS failed. On POSIX, this is a
  314. * normal operation.
  315. */
  316. if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
  317. struct archive_string as;
  318. archive_string_init(&as);
  319. path_length = strlen(p);
  320. if (archive_string_ensure(&as,
  321. path_length + 2) == NULL) {
  322. archive_set_error(&a->archive, ENOMEM,
  323. "Can't allocate ustar data");
  324. archive_string_free(&as);
  325. return(ARCHIVE_FATAL);
  326. }
  327. #if defined(_WIN32) && !defined(__CYGWIN__)
  328. /* NOTE: This might break the pathname
  329. * if the current code page is CP932 and
  330. * the pathname includes a character '\'
  331. * as a part of its multibyte pathname. */
  332. if (p[strlen(p) -1] == '\\')
  333. path_length--;
  334. else
  335. #endif
  336. archive_strncpy(&as, p, path_length);
  337. archive_strappend_char(&as, '/');
  338. archive_entry_copy_pathname(entry, as.s);
  339. archive_string_free(&as);
  340. }
  341. }
  342. #if defined(_WIN32) && !defined(__CYGWIN__)
  343. /* Make sure the path separators in pathname, hardlink and symlink
  344. * are all slash '/', not the Windows path separator '\'. */
  345. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  346. if (entry_main == NULL) {
  347. archive_set_error(&a->archive, ENOMEM,
  348. "Can't allocate ustar data");
  349. return(ARCHIVE_FATAL);
  350. }
  351. if (entry != entry_main)
  352. entry = entry_main;
  353. else
  354. entry_main = NULL;
  355. #else
  356. entry_main = NULL;
  357. #endif
  358. r = archive_entry_pathname_l(entry, &(gnutar->pathname),
  359. &(gnutar->pathname_length), sconv);
  360. if (r != 0) {
  361. if (errno == ENOMEM) {
  362. archive_set_error(&a->archive, ENOMEM,
  363. "Can't allocate memory for Pathame");
  364. ret = ARCHIVE_FATAL;
  365. goto exit_write_header;
  366. }
  367. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  368. "Can't translate pathname '%s' to %s",
  369. archive_entry_pathname(entry),
  370. archive_string_conversion_charset_name(sconv));
  371. ret2 = ARCHIVE_WARN;
  372. }
  373. r = archive_entry_uname_l(entry, &(gnutar->uname),
  374. &(gnutar->uname_length), sconv);
  375. if (r != 0) {
  376. if (errno == ENOMEM) {
  377. archive_set_error(&a->archive, ENOMEM,
  378. "Can't allocate memory for Uname");
  379. ret = ARCHIVE_FATAL;
  380. goto exit_write_header;
  381. }
  382. archive_set_error(&a->archive,
  383. ARCHIVE_ERRNO_FILE_FORMAT,
  384. "Can't translate uname '%s' to %s",
  385. archive_entry_uname(entry),
  386. archive_string_conversion_charset_name(sconv));
  387. ret2 = ARCHIVE_WARN;
  388. }
  389. r = archive_entry_gname_l(entry, &(gnutar->gname),
  390. &(gnutar->gname_length), sconv);
  391. if (r != 0) {
  392. if (errno == ENOMEM) {
  393. archive_set_error(&a->archive, ENOMEM,
  394. "Can't allocate memory for Gname");
  395. ret = ARCHIVE_FATAL;
  396. goto exit_write_header;
  397. }
  398. archive_set_error(&a->archive,
  399. ARCHIVE_ERRNO_FILE_FORMAT,
  400. "Can't translate gname '%s' to %s",
  401. archive_entry_gname(entry),
  402. archive_string_conversion_charset_name(sconv));
  403. ret2 = ARCHIVE_WARN;
  404. }
  405. /* If linkname is longer than 100 chars we need to add a 'K' header. */
  406. r = archive_entry_hardlink_l(entry, &(gnutar->linkname),
  407. &(gnutar->linkname_length), sconv);
  408. if (r != 0) {
  409. if (errno == ENOMEM) {
  410. archive_set_error(&a->archive, ENOMEM,
  411. "Can't allocate memory for Linkname");
  412. ret = ARCHIVE_FATAL;
  413. goto exit_write_header;
  414. }
  415. archive_set_error(&a->archive,
  416. ARCHIVE_ERRNO_FILE_FORMAT,
  417. "Can't translate linkname '%s' to %s",
  418. archive_entry_hardlink(entry),
  419. archive_string_conversion_charset_name(sconv));
  420. ret2 = ARCHIVE_WARN;
  421. }
  422. if (gnutar->linkname_length == 0) {
  423. r = archive_entry_symlink_l(entry, &(gnutar->linkname),
  424. &(gnutar->linkname_length), sconv);
  425. if (r != 0) {
  426. if (errno == ENOMEM) {
  427. archive_set_error(&a->archive, ENOMEM,
  428. "Can't allocate memory for Linkname");
  429. ret = ARCHIVE_FATAL;
  430. goto exit_write_header;
  431. }
  432. archive_set_error(&a->archive,
  433. ARCHIVE_ERRNO_FILE_FORMAT,
  434. "Can't translate linkname '%s' to %s",
  435. archive_entry_hardlink(entry),
  436. archive_string_conversion_charset_name(sconv));
  437. ret2 = ARCHIVE_WARN;
  438. }
  439. }
  440. if (gnutar->linkname_length > GNUTAR_linkname_size) {
  441. size_t length = gnutar->linkname_length + 1;
  442. struct archive_entry *temp = archive_entry_new2(&a->archive);
  443. /* Uname/gname here don't really matter since no one reads them;
  444. * these are the values that GNU tar happens to use on FreeBSD. */
  445. archive_entry_set_uname(temp, "root");
  446. archive_entry_set_gname(temp, "wheel");
  447. archive_entry_set_pathname(temp, "././@LongLink");
  448. archive_entry_set_size(temp, length);
  449. ret = archive_format_gnutar_header(a, buff, temp, 'K');
  450. archive_entry_free(temp);
  451. if (ret < ARCHIVE_WARN)
  452. goto exit_write_header;
  453. ret = __archive_write_output(a, buff, 512);
  454. if (ret < ARCHIVE_WARN)
  455. goto exit_write_header;
  456. /* Write name and trailing null byte. */
  457. ret = __archive_write_output(a, gnutar->linkname, length);
  458. if (ret < ARCHIVE_WARN)
  459. goto exit_write_header;
  460. /* Pad to 512 bytes */
  461. ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
  462. if (ret < ARCHIVE_WARN)
  463. goto exit_write_header;
  464. }
  465. /* If pathname is longer than 100 chars we need to add an 'L' header. */
  466. if (gnutar->pathname_length > GNUTAR_name_size) {
  467. const char *pathname = gnutar->pathname;
  468. size_t length = gnutar->pathname_length + 1;
  469. struct archive_entry *temp = archive_entry_new2(&a->archive);
  470. /* Uname/gname here don't really matter since no one reads them;
  471. * these are the values that GNU tar happens to use on FreeBSD. */
  472. archive_entry_set_uname(temp, "root");
  473. archive_entry_set_gname(temp, "wheel");
  474. archive_entry_set_pathname(temp, "././@LongLink");
  475. archive_entry_set_size(temp, length);
  476. ret = archive_format_gnutar_header(a, buff, temp, 'L');
  477. archive_entry_free(temp);
  478. if (ret < ARCHIVE_WARN)
  479. goto exit_write_header;
  480. ret = __archive_write_output(a, buff, 512);
  481. if(ret < ARCHIVE_WARN)
  482. goto exit_write_header;
  483. /* Write pathname + trailing null byte. */
  484. ret = __archive_write_output(a, pathname, length);
  485. if(ret < ARCHIVE_WARN)
  486. goto exit_write_header;
  487. /* Pad to multiple of 512 bytes. */
  488. ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
  489. if (ret < ARCHIVE_WARN)
  490. goto exit_write_header;
  491. }
  492. if (archive_entry_hardlink(entry) != NULL) {
  493. tartype = '1';
  494. } else
  495. switch (archive_entry_filetype(entry)) {
  496. case AE_IFREG: tartype = '0' ; break;
  497. case AE_IFLNK: tartype = '2' ; break;
  498. case AE_IFCHR: tartype = '3' ; break;
  499. case AE_IFBLK: tartype = '4' ; break;
  500. case AE_IFDIR: tartype = '5' ; break;
  501. case AE_IFIFO: tartype = '6' ; break;
  502. default: /* AE_IFSOCK and unknown */
  503. __archive_write_entry_filetype_unsupported(
  504. &a->archive, entry, "gnutar");
  505. ret = ARCHIVE_FAILED;
  506. goto exit_write_header;
  507. }
  508. ret = archive_format_gnutar_header(a, buff, entry, tartype);
  509. if (ret < ARCHIVE_WARN)
  510. goto exit_write_header;
  511. if (ret2 < ret)
  512. ret = ret2;
  513. ret2 = __archive_write_output(a, buff, 512);
  514. if (ret2 < ARCHIVE_WARN) {
  515. ret = ret2;
  516. goto exit_write_header;
  517. }
  518. if (ret2 < ret)
  519. ret = ret2;
  520. gnutar->entry_bytes_remaining = archive_entry_size(entry);
  521. gnutar->entry_padding = 0x1ff & (-(int64_t)gnutar->entry_bytes_remaining);
  522. exit_write_header:
  523. archive_entry_free(entry_main);
  524. return (ret);
  525. }
  526. static int
  527. archive_format_gnutar_header(struct archive_write *a, char h[512],
  528. struct archive_entry *entry, int tartype)
  529. {
  530. unsigned int checksum;
  531. int i, ret;
  532. size_t copy_length;
  533. const char *p;
  534. struct gnutar *gnutar;
  535. gnutar = (struct gnutar *)a->format_data;
  536. ret = 0;
  537. /*
  538. * The "template header" already includes the signature,
  539. * various end-of-field markers, and other required elements.
  540. */
  541. memcpy(h, &template_header, 512);
  542. /*
  543. * Because the block is already null-filled, and strings
  544. * are allowed to exactly fill their destination (without null),
  545. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  546. */
  547. if (tartype == 'K' || tartype == 'L') {
  548. p = archive_entry_pathname(entry);
  549. copy_length = strlen(p);
  550. } else {
  551. p = gnutar->pathname;
  552. copy_length = gnutar->pathname_length;
  553. }
  554. if (copy_length > GNUTAR_name_size)
  555. copy_length = GNUTAR_name_size;
  556. memcpy(h + GNUTAR_name_offset, p, copy_length);
  557. if ((copy_length = gnutar->linkname_length) > 0) {
  558. if (copy_length > GNUTAR_linkname_size)
  559. copy_length = GNUTAR_linkname_size;
  560. memcpy(h + GNUTAR_linkname_offset, gnutar->linkname,
  561. copy_length);
  562. }
  563. /* TODO: How does GNU tar handle unames longer than GNUTAR_uname_size? */
  564. if (tartype == 'K' || tartype == 'L') {
  565. p = archive_entry_uname(entry);
  566. copy_length = strlen(p);
  567. } else {
  568. p = gnutar->uname;
  569. copy_length = gnutar->uname_length;
  570. }
  571. if (copy_length > 0) {
  572. if (copy_length > GNUTAR_uname_size)
  573. copy_length = GNUTAR_uname_size;
  574. memcpy(h + GNUTAR_uname_offset, p, copy_length);
  575. }
  576. /* TODO: How does GNU tar handle gnames longer than GNUTAR_gname_size? */
  577. if (tartype == 'K' || tartype == 'L') {
  578. p = archive_entry_gname(entry);
  579. copy_length = strlen(p);
  580. } else {
  581. p = gnutar->gname;
  582. copy_length = gnutar->gname_length;
  583. }
  584. if (copy_length > 0) {
  585. if (strlen(p) > GNUTAR_gname_size)
  586. copy_length = GNUTAR_gname_size;
  587. memcpy(h + GNUTAR_gname_offset, p, copy_length);
  588. }
  589. /* By truncating the mode here, we ensure it always fits. */
  590. format_octal(archive_entry_mode(entry) & 07777,
  591. h + GNUTAR_mode_offset, GNUTAR_mode_size);
  592. /* GNU tar supports base-256 here, so should never overflow. */
  593. if (format_number(archive_entry_uid(entry), h + GNUTAR_uid_offset,
  594. GNUTAR_uid_size, GNUTAR_uid_max_size)) {
  595. archive_set_error(&a->archive, ERANGE,
  596. "Numeric user ID %jd too large",
  597. (intmax_t)archive_entry_uid(entry));
  598. ret = ARCHIVE_FAILED;
  599. }
  600. /* GNU tar supports base-256 here, so should never overflow. */
  601. if (format_number(archive_entry_gid(entry), h + GNUTAR_gid_offset,
  602. GNUTAR_gid_size, GNUTAR_gid_max_size)) {
  603. archive_set_error(&a->archive, ERANGE,
  604. "Numeric group ID %jd too large",
  605. (intmax_t)archive_entry_gid(entry));
  606. ret = ARCHIVE_FAILED;
  607. }
  608. /* GNU tar supports base-256 here, so should never overflow. */
  609. if (format_number(archive_entry_size(entry), h + GNUTAR_size_offset,
  610. GNUTAR_size_size, GNUTAR_size_max_size)) {
  611. archive_set_error(&a->archive, ERANGE,
  612. "File size out of range");
  613. ret = ARCHIVE_FAILED;
  614. }
  615. /* Shouldn't overflow before 2106, since mtime field is 33 bits. */
  616. format_octal(archive_entry_mtime(entry),
  617. h + GNUTAR_mtime_offset, GNUTAR_mtime_size);
  618. if (archive_entry_filetype(entry) == AE_IFBLK
  619. || archive_entry_filetype(entry) == AE_IFCHR) {
  620. if (format_octal(archive_entry_rdevmajor(entry),
  621. h + GNUTAR_rdevmajor_offset,
  622. GNUTAR_rdevmajor_size)) {
  623. archive_set_error(&a->archive, ERANGE,
  624. "Major device number too large");
  625. ret = ARCHIVE_FAILED;
  626. }
  627. if (format_octal(archive_entry_rdevminor(entry),
  628. h + GNUTAR_rdevminor_offset,
  629. GNUTAR_rdevminor_size)) {
  630. archive_set_error(&a->archive, ERANGE,
  631. "Minor device number too large");
  632. ret = ARCHIVE_FAILED;
  633. }
  634. }
  635. h[GNUTAR_typeflag_offset] = tartype;
  636. checksum = 0;
  637. for (i = 0; i < 512; i++)
  638. checksum += 255 & (unsigned int)h[i];
  639. h[GNUTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
  640. /* h[GNUTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
  641. format_octal(checksum, h + GNUTAR_checksum_offset, 6);
  642. return (ret);
  643. }
  644. /*
  645. * Format a number into a field, falling back to base-256 if necessary.
  646. */
  647. static int
  648. format_number(int64_t v, char *p, int s, int maxsize)
  649. {
  650. int64_t limit = ((int64_t)1 << (s*3));
  651. if (v < limit)
  652. return (format_octal(v, p, s));
  653. return (format_256(v, p, maxsize));
  654. }
  655. /*
  656. * Format a number into the specified field using base-256.
  657. */
  658. static int
  659. format_256(int64_t v, char *p, int s)
  660. {
  661. p += s;
  662. while (s-- > 0) {
  663. *--p = (char)(v & 0xff);
  664. v >>= 8;
  665. }
  666. *p |= 0x80; /* Set the base-256 marker bit. */
  667. return (0);
  668. }
  669. /*
  670. * Format a number into the specified field using octal.
  671. */
  672. static int
  673. format_octal(int64_t v, char *p, int s)
  674. {
  675. int len = s;
  676. /* Octal values can't be negative, so use 0. */
  677. if (v < 0)
  678. v = 0;
  679. p += s; /* Start at the end and work backwards. */
  680. while (s-- > 0) {
  681. *--p = (char)('0' + (v & 7));
  682. v >>= 3;
  683. }
  684. if (v == 0)
  685. return (0);
  686. /* If it overflowed, fill field with max value. */
  687. while (len-- > 0)
  688. *p++ = '7';
  689. return (-1);
  690. }