archive_write_set_format_gnutar.c 23 KB

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