archive_write_set_format_pax.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_pax.c,v 1.49 2008/09/30 03:57:07 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #include "archive.h"
  37. #include "archive_entry.h"
  38. #include "archive_private.h"
  39. #include "archive_write_private.h"
  40. struct pax {
  41. uint64_t entry_bytes_remaining;
  42. uint64_t entry_padding;
  43. struct archive_string pax_header;
  44. };
  45. static void add_pax_attr(struct archive_string *, const char *key,
  46. const char *value);
  47. static void add_pax_attr_int(struct archive_string *,
  48. const char *key, int64_t value);
  49. static void add_pax_attr_time(struct archive_string *,
  50. const char *key, int64_t sec,
  51. unsigned long nanos);
  52. static void add_pax_attr_w(struct archive_string *,
  53. const char *key, const wchar_t *wvalue);
  54. static ssize_t archive_write_pax_data(struct archive_write *,
  55. const void *, size_t);
  56. static int archive_write_pax_finish(struct archive_write *);
  57. static int archive_write_pax_destroy(struct archive_write *);
  58. static int archive_write_pax_finish_entry(struct archive_write *);
  59. static int archive_write_pax_header(struct archive_write *,
  60. struct archive_entry *);
  61. static char *base64_encode(const char *src, size_t len);
  62. static char *build_pax_attribute_name(char *dest, const char *src);
  63. static char *build_ustar_entry_name(char *dest, const char *src,
  64. size_t src_length, const char *insert);
  65. static char *format_int(char *dest, int64_t);
  66. static int has_non_ASCII(const wchar_t *);
  67. static char *url_encode(const char *in);
  68. static int write_nulls(struct archive_write *, size_t);
  69. /*
  70. * Set output format to 'restricted pax' format.
  71. *
  72. * This is the same as normal 'pax', but tries to suppress
  73. * the pax header whenever possible. This is the default for
  74. * bsdtar, for instance.
  75. */
  76. int
  77. archive_write_set_format_pax_restricted(struct archive *_a)
  78. {
  79. struct archive_write *a = (struct archive_write *)_a;
  80. int r;
  81. r = archive_write_set_format_pax(&a->archive);
  82. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
  83. a->archive.archive_format_name = "restricted POSIX pax interchange";
  84. return (r);
  85. }
  86. /*
  87. * Set output format to 'pax' format.
  88. */
  89. int
  90. archive_write_set_format_pax(struct archive *_a)
  91. {
  92. struct archive_write *a = (struct archive_write *)_a;
  93. struct pax *pax;
  94. if (a->format_destroy != NULL)
  95. (a->format_destroy)(a);
  96. pax = (struct pax *)malloc(sizeof(*pax));
  97. if (pax == NULL) {
  98. archive_set_error(&a->archive, ENOMEM, "Can't allocate pax data");
  99. return (ARCHIVE_FATAL);
  100. }
  101. memset(pax, 0, sizeof(*pax));
  102. a->format_data = pax;
  103. a->pad_uncompressed = 1;
  104. a->format_name = "pax";
  105. a->format_write_header = archive_write_pax_header;
  106. a->format_write_data = archive_write_pax_data;
  107. a->format_finish = archive_write_pax_finish;
  108. a->format_destroy = archive_write_pax_destroy;
  109. a->format_finish_entry = archive_write_pax_finish_entry;
  110. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  111. a->archive.archive_format_name = "POSIX pax interchange";
  112. return (ARCHIVE_OK);
  113. }
  114. /*
  115. * Note: This code assumes that 'nanos' has the same sign as 'sec',
  116. * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
  117. * and not -0.8 seconds. This is a pretty pedantic point, as we're
  118. * unlikely to encounter many real files created before Jan 1, 1970,
  119. * much less ones with timestamps recorded to sub-second resolution.
  120. */
  121. static void
  122. add_pax_attr_time(struct archive_string *as, const char *key,
  123. int64_t sec, unsigned long nanos)
  124. {
  125. int digit, i;
  126. char *t;
  127. /*
  128. * Note that each byte contributes fewer than 3 base-10
  129. * digits, so this will always be big enough.
  130. */
  131. char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];
  132. tmp[sizeof(tmp) - 1] = 0;
  133. t = tmp + sizeof(tmp) - 1;
  134. /* Skip trailing zeros in the fractional part. */
  135. for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
  136. digit = nanos % 10;
  137. nanos /= 10;
  138. }
  139. /* Only format the fraction if it's non-zero. */
  140. if (i > 0) {
  141. while (i > 0) {
  142. *--t = "0123456789"[digit];
  143. digit = nanos % 10;
  144. nanos /= 10;
  145. i--;
  146. }
  147. *--t = '.';
  148. }
  149. t = format_int(t, sec);
  150. add_pax_attr(as, key, t);
  151. }
  152. static char *
  153. format_int(char *t, int64_t i)
  154. {
  155. int sign;
  156. if (i < 0) {
  157. sign = -1;
  158. i = -i;
  159. } else
  160. sign = 1;
  161. do {
  162. *--t = "0123456789"[i % 10];
  163. } while (i /= 10);
  164. if (sign < 0)
  165. *--t = '-';
  166. return (t);
  167. }
  168. static void
  169. add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
  170. {
  171. char tmp[1 + 3 * sizeof(value)];
  172. tmp[sizeof(tmp) - 1] = 0;
  173. add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
  174. }
  175. static char *
  176. utf8_encode(const wchar_t *wval)
  177. {
  178. int utf8len;
  179. const wchar_t *wp;
  180. unsigned long wc;
  181. char *utf8_value, *p;
  182. utf8len = 0;
  183. for (wp = wval; *wp != L'\0'; ) {
  184. wc = *wp++;
  185. if (wc >= 0xd800 && wc <= 0xdbff
  186. && *wp >= 0xdc00 && *wp <= 0xdfff) {
  187. /* This is a surrogate pair. Combine into a
  188. * full Unicode value before encoding into
  189. * UTF-8. */
  190. wc = (wc - 0xd800) << 10; /* High 10 bits */
  191. wc += (*wp++ - 0xdc00); /* Low 10 bits */
  192. wc += 0x10000; /* Skip BMP */
  193. }
  194. if (wc <= 0x7f)
  195. utf8len++;
  196. else if (wc <= 0x7ff)
  197. utf8len += 2;
  198. else if (wc <= 0xffff)
  199. utf8len += 3;
  200. else if (wc <= 0x1fffff)
  201. utf8len += 4;
  202. else if (wc <= 0x3ffffff)
  203. utf8len += 5;
  204. else if (wc <= 0x7fffffff)
  205. utf8len += 6;
  206. /* Ignore larger values; UTF-8 can't encode them. */
  207. }
  208. utf8_value = (char *)malloc(utf8len + 1);
  209. if (utf8_value == NULL) {
  210. __archive_errx(1, "Not enough memory for attributes");
  211. return (NULL);
  212. }
  213. for (wp = wval, p = utf8_value; *wp != L'\0'; ) {
  214. wc = *wp++;
  215. if (wc >= 0xd800 && wc <= 0xdbff
  216. && *wp >= 0xdc00 && *wp <= 0xdfff) {
  217. /* Combine surrogate pair. */
  218. wc = (wc - 0xd800) << 10;
  219. wc += *wp++ - 0xdc00 + 0x10000;
  220. }
  221. if (wc <= 0x7f) {
  222. *p++ = (char)wc;
  223. } else if (wc <= 0x7ff) {
  224. p[0] = 0xc0 | ((wc >> 6) & 0x1f);
  225. p[1] = 0x80 | (wc & 0x3f);
  226. p += 2;
  227. } else if (wc <= 0xffff) {
  228. p[0] = 0xe0 | ((wc >> 12) & 0x0f);
  229. p[1] = 0x80 | ((wc >> 6) & 0x3f);
  230. p[2] = 0x80 | (wc & 0x3f);
  231. p += 3;
  232. } else if (wc <= 0x1fffff) {
  233. p[0] = 0xf0 | ((wc >> 18) & 0x07);
  234. p[1] = 0x80 | ((wc >> 12) & 0x3f);
  235. p[2] = 0x80 | ((wc >> 6) & 0x3f);
  236. p[3] = 0x80 | (wc & 0x3f);
  237. p += 4;
  238. } else if (wc <= 0x3ffffff) {
  239. p[0] = 0xf8 | ((wc >> 24) & 0x03);
  240. p[1] = 0x80 | ((wc >> 18) & 0x3f);
  241. p[2] = 0x80 | ((wc >> 12) & 0x3f);
  242. p[3] = 0x80 | ((wc >> 6) & 0x3f);
  243. p[4] = 0x80 | (wc & 0x3f);
  244. p += 5;
  245. } else if (wc <= 0x7fffffff) {
  246. p[0] = 0xfc | ((wc >> 30) & 0x01);
  247. p[1] = 0x80 | ((wc >> 24) & 0x3f);
  248. p[1] = 0x80 | ((wc >> 18) & 0x3f);
  249. p[2] = 0x80 | ((wc >> 12) & 0x3f);
  250. p[3] = 0x80 | ((wc >> 6) & 0x3f);
  251. p[4] = 0x80 | (wc & 0x3f);
  252. p += 6;
  253. }
  254. /* Ignore larger values; UTF-8 can't encode them. */
  255. }
  256. *p = '\0';
  257. return (utf8_value);
  258. }
  259. static void
  260. add_pax_attr_w(struct archive_string *as, const char *key, const wchar_t *wval)
  261. {
  262. char *utf8_value = utf8_encode(wval);
  263. if (utf8_value == NULL)
  264. return;
  265. add_pax_attr(as, key, utf8_value);
  266. free(utf8_value);
  267. }
  268. /*
  269. * Add a key/value attribute to the pax header. This function handles
  270. * the length field and various other syntactic requirements.
  271. */
  272. static void
  273. add_pax_attr(struct archive_string *as, const char *key, const char *value)
  274. {
  275. int digits, i, len, next_ten;
  276. char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */
  277. /*-
  278. * PAX attributes have the following layout:
  279. * <len> <space> <key> <=> <value> <nl>
  280. */
  281. len = 1 + strlen(key) + 1 + strlen(value) + 1;
  282. /*
  283. * The <len> field includes the length of the <len> field, so
  284. * computing the correct length is tricky. I start by
  285. * counting the number of base-10 digits in 'len' and
  286. * computing the next higher power of 10.
  287. */
  288. next_ten = 1;
  289. digits = 0;
  290. i = len;
  291. while (i > 0) {
  292. i = i / 10;
  293. digits++;
  294. next_ten = next_ten * 10;
  295. }
  296. /*
  297. * For example, if string without the length field is 99
  298. * chars, then adding the 2 digit length "99" will force the
  299. * total length past 100, requiring an extra digit. The next
  300. * statement adjusts for this effect.
  301. */
  302. if (len + digits >= next_ten)
  303. digits++;
  304. /* Now, we have the right length so we can build the line. */
  305. tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */
  306. archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits));
  307. archive_strappend_char(as, ' ');
  308. archive_strcat(as, key);
  309. archive_strappend_char(as, '=');
  310. archive_strcat(as, value);
  311. archive_strappend_char(as, '\n');
  312. }
  313. static void
  314. archive_write_pax_header_xattrs(struct pax *pax, struct archive_entry *entry)
  315. {
  316. struct archive_string s;
  317. int i = archive_entry_xattr_reset(entry);
  318. while (i--) {
  319. const char *name;
  320. const void *value;
  321. char *encoded_value;
  322. char *url_encoded_name = NULL, *encoded_name = NULL;
  323. wchar_t *wcs_name = NULL;
  324. size_t size;
  325. archive_entry_xattr_next(entry, &name, &value, &size);
  326. /* Name is URL-encoded, then converted to wchar_t,
  327. * then UTF-8 encoded. */
  328. url_encoded_name = url_encode(name);
  329. if (url_encoded_name != NULL) {
  330. /* Convert narrow-character to wide-character. */
  331. int wcs_length = strlen(url_encoded_name);
  332. wcs_name = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
  333. if (wcs_name == NULL)
  334. __archive_errx(1, "No memory for xattr conversion");
  335. mbstowcs(wcs_name, url_encoded_name, wcs_length);
  336. wcs_name[wcs_length] = 0;
  337. free(url_encoded_name); /* Done with this. */
  338. }
  339. if (wcs_name != NULL) {
  340. encoded_name = utf8_encode(wcs_name);
  341. free(wcs_name); /* Done with wchar_t name. */
  342. }
  343. encoded_value = base64_encode((const char *)value, size);
  344. if (encoded_name != NULL && encoded_value != NULL) {
  345. archive_string_init(&s);
  346. archive_strcpy(&s, "LIBARCHIVE.xattr.");
  347. archive_strcat(&s, encoded_name);
  348. add_pax_attr(&(pax->pax_header), s.s, encoded_value);
  349. archive_string_free(&s);
  350. }
  351. free(encoded_name);
  352. free(encoded_value);
  353. }
  354. }
  355. /*
  356. * TODO: Consider adding 'comment' and 'charset' fields to
  357. * archive_entry so that clients can specify them. Also, consider
  358. * adding generic key/value tags so clients can add arbitrary
  359. * key/value data.
  360. */
  361. static int
  362. archive_write_pax_header(struct archive_write *a,
  363. struct archive_entry *entry_original)
  364. {
  365. struct archive_entry *entry_main;
  366. const char *p;
  367. char *t;
  368. const wchar_t *wp;
  369. const char *suffix;
  370. int need_extension, r, ret;
  371. struct pax *pax;
  372. const char *hdrcharset = NULL;
  373. const char *hardlink;
  374. const char *path = NULL, *linkpath = NULL;
  375. const char *uname = NULL, *gname = NULL;
  376. const wchar_t *path_w = NULL, *linkpath_w = NULL;
  377. const wchar_t *uname_w = NULL, *gname_w = NULL;
  378. char paxbuff[512];
  379. char ustarbuff[512];
  380. char ustar_entry_name[256];
  381. char pax_entry_name[256];
  382. ret = ARCHIVE_OK;
  383. need_extension = 0;
  384. pax = (struct pax *)a->format_data;
  385. hardlink = archive_entry_hardlink(entry_original);
  386. /* Make sure this is a type of entry that we can handle here */
  387. if (hardlink == NULL) {
  388. switch (archive_entry_filetype(entry_original)) {
  389. case AE_IFBLK:
  390. case AE_IFCHR:
  391. case AE_IFIFO:
  392. case AE_IFLNK:
  393. case AE_IFREG:
  394. break;
  395. case AE_IFDIR:
  396. /*
  397. * Ensure a trailing '/'. Modify the original
  398. * entry so the client sees the change.
  399. */
  400. p = archive_entry_pathname(entry_original);
  401. if (p[strlen(p) - 1] != '/') {
  402. t = (char *)malloc(strlen(p) + 2);
  403. if (t == NULL) {
  404. archive_set_error(&a->archive, ENOMEM,
  405. "Can't allocate pax data");
  406. return(ARCHIVE_FATAL);
  407. }
  408. strcpy(t, p);
  409. strcat(t, "/");
  410. archive_entry_copy_pathname(entry_original, t);
  411. free(t);
  412. }
  413. break;
  414. case AE_IFSOCK:
  415. archive_set_error(&a->archive,
  416. ARCHIVE_ERRNO_FILE_FORMAT,
  417. "tar format cannot archive socket");
  418. return (ARCHIVE_WARN);
  419. default:
  420. archive_set_error(&a->archive,
  421. ARCHIVE_ERRNO_FILE_FORMAT,
  422. "tar format cannot archive this (type=0%lo)",
  423. (unsigned long)archive_entry_filetype(entry_original));
  424. return (ARCHIVE_WARN);
  425. }
  426. }
  427. /* Copy entry so we can modify it as needed. */
  428. entry_main = archive_entry_clone(entry_original);
  429. archive_string_empty(&(pax->pax_header)); /* Blank our work area. */
  430. /*
  431. * First, check the name fields and see if any of them
  432. * require binary coding. If any of them does, then all of
  433. * them do.
  434. */
  435. hdrcharset = NULL;
  436. path = archive_entry_pathname(entry_main);
  437. path_w = archive_entry_pathname_w(entry_main);
  438. if (path != NULL && path_w == NULL) {
  439. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  440. "Can't translate pathname '%s' to UTF-8", path);
  441. ret = ARCHIVE_WARN;
  442. hdrcharset = "BINARY";
  443. }
  444. uname = archive_entry_uname(entry_main);
  445. uname_w = archive_entry_uname_w(entry_main);
  446. if (uname != NULL && uname_w == NULL) {
  447. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  448. "Can't translate uname '%s' to UTF-8", uname);
  449. ret = ARCHIVE_WARN;
  450. hdrcharset = "BINARY";
  451. }
  452. gname = archive_entry_gname(entry_main);
  453. gname_w = archive_entry_gname_w(entry_main);
  454. if (gname != NULL && gname_w == NULL) {
  455. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  456. "Can't translate gname '%s' to UTF-8", gname);
  457. ret = ARCHIVE_WARN;
  458. hdrcharset = "BINARY";
  459. }
  460. linkpath = hardlink;
  461. if (linkpath != NULL) {
  462. linkpath_w = archive_entry_hardlink_w(entry_main);
  463. } else {
  464. linkpath = archive_entry_symlink(entry_main);
  465. if (linkpath != NULL)
  466. linkpath_w = archive_entry_symlink_w(entry_main);
  467. }
  468. if (linkpath != NULL && linkpath_w == NULL) {
  469. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  470. "Can't translate linkpath '%s' to UTF-8", linkpath);
  471. ret = ARCHIVE_WARN;
  472. hdrcharset = "BINARY";
  473. }
  474. /* Store the header encoding first, to be nice to readers. */
  475. if (hdrcharset != NULL)
  476. add_pax_attr(&(pax->pax_header), "hdrcharset", hdrcharset);
  477. /*
  478. * If name is too long, or has non-ASCII characters, add
  479. * 'path' to pax extended attrs. (Note that an unconvertible
  480. * name must have non-ASCII characters.)
  481. */
  482. if (path == NULL) {
  483. /* We don't have a narrow version, so we have to store
  484. * the wide version. */
  485. add_pax_attr_w(&(pax->pax_header), "path", path_w);
  486. archive_entry_set_pathname(entry_main, "@WidePath");
  487. need_extension = 1;
  488. } else if (has_non_ASCII(path_w)) {
  489. /* We have non-ASCII characters. */
  490. if (path_w == NULL || hdrcharset != NULL) {
  491. /* Can't do UTF-8, so store it raw. */
  492. add_pax_attr(&(pax->pax_header), "path", path);
  493. } else {
  494. /* Store UTF-8 */
  495. add_pax_attr_w(&(pax->pax_header),
  496. "path", path_w);
  497. }
  498. archive_entry_set_pathname(entry_main,
  499. build_ustar_entry_name(ustar_entry_name,
  500. path, strlen(path), NULL));
  501. need_extension = 1;
  502. } else {
  503. /* We have an all-ASCII path; we'd like to just store
  504. * it in the ustar header if it will fit. Yes, this
  505. * duplicates some of the logic in
  506. * write_set_format_ustar.c
  507. */
  508. if (strlen(path) <= 100) {
  509. /* Fits in the old 100-char tar name field. */
  510. } else {
  511. /* Find largest suffix that will fit. */
  512. /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */
  513. suffix = strchr(path + strlen(path) - 100 - 1, '/');
  514. /* Don't attempt an empty prefix. */
  515. if (suffix == path)
  516. suffix = strchr(suffix + 1, '/');
  517. /* We can put it in the ustar header if it's
  518. * all ASCII and it's either <= 100 characters
  519. * or can be split at a '/' into a prefix <=
  520. * 155 chars and a suffix <= 100 chars. (Note
  521. * the strchr() above will return NULL exactly
  522. * when the path can't be split.)
  523. */
  524. if (suffix == NULL /* Suffix > 100 chars. */
  525. || suffix[1] == '\0' /* empty suffix */
  526. || suffix - path > 155) /* Prefix > 155 chars */
  527. {
  528. if (path_w == NULL || hdrcharset != NULL) {
  529. /* Can't do UTF-8, so store it raw. */
  530. add_pax_attr(&(pax->pax_header),
  531. "path", path);
  532. } else {
  533. /* Store UTF-8 */
  534. add_pax_attr_w(&(pax->pax_header),
  535. "path", path_w);
  536. }
  537. archive_entry_set_pathname(entry_main,
  538. build_ustar_entry_name(ustar_entry_name,
  539. path, strlen(path), NULL));
  540. need_extension = 1;
  541. }
  542. }
  543. }
  544. if (linkpath != NULL) {
  545. /* If link name is too long or has non-ASCII characters, add
  546. * 'linkpath' to pax extended attrs. */
  547. if (strlen(linkpath) > 100 || linkpath_w == NULL
  548. || linkpath_w == NULL || has_non_ASCII(linkpath_w)) {
  549. if (linkpath_w == NULL || hdrcharset != NULL)
  550. /* If the linkpath is not convertible
  551. * to wide, or we're encoding in
  552. * binary anyway, store it raw. */
  553. add_pax_attr(&(pax->pax_header),
  554. "linkpath", linkpath);
  555. else
  556. /* If the link is long or has a
  557. * non-ASCII character, store it as a
  558. * pax extended attribute. */
  559. add_pax_attr_w(&(pax->pax_header),
  560. "linkpath", linkpath_w);
  561. if (strlen(linkpath) > 100) {
  562. if (hardlink != NULL)
  563. archive_entry_set_hardlink(entry_main,
  564. "././@LongHardLink");
  565. else
  566. archive_entry_set_symlink(entry_main,
  567. "././@LongSymLink");
  568. }
  569. need_extension = 1;
  570. }
  571. }
  572. /* If file size is too large, add 'size' to pax extended attrs. */
  573. if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
  574. add_pax_attr_int(&(pax->pax_header), "size",
  575. archive_entry_size(entry_main));
  576. need_extension = 1;
  577. }
  578. /* If numeric GID is too large, add 'gid' to pax extended attrs. */
  579. if ((unsigned int)archive_entry_gid(entry_main) >= (1 << 18)) {
  580. add_pax_attr_int(&(pax->pax_header), "gid",
  581. archive_entry_gid(entry_main));
  582. need_extension = 1;
  583. }
  584. /* If group name is too large or has non-ASCII characters, add
  585. * 'gname' to pax extended attrs. */
  586. if (gname != NULL) {
  587. if (strlen(gname) > 31
  588. || gname_w == NULL
  589. || has_non_ASCII(gname_w))
  590. {
  591. if (gname_w == NULL || hdrcharset != NULL) {
  592. add_pax_attr(&(pax->pax_header),
  593. "gname", gname);
  594. } else {
  595. add_pax_attr_w(&(pax->pax_header),
  596. "gname", gname_w);
  597. }
  598. need_extension = 1;
  599. }
  600. }
  601. /* If numeric UID is too large, add 'uid' to pax extended attrs. */
  602. if ((unsigned int)archive_entry_uid(entry_main) >= (1 << 18)) {
  603. add_pax_attr_int(&(pax->pax_header), "uid",
  604. archive_entry_uid(entry_main));
  605. need_extension = 1;
  606. }
  607. /* Add 'uname' to pax extended attrs if necessary. */
  608. if (uname != NULL) {
  609. if (strlen(uname) > 31
  610. || uname_w == NULL
  611. || has_non_ASCII(uname_w))
  612. {
  613. if (uname_w == NULL || hdrcharset != NULL) {
  614. add_pax_attr(&(pax->pax_header),
  615. "uname", uname);
  616. } else {
  617. add_pax_attr_w(&(pax->pax_header),
  618. "uname", uname_w);
  619. }
  620. need_extension = 1;
  621. }
  622. }
  623. /*
  624. * POSIX/SUSv3 doesn't provide a standard key for large device
  625. * numbers. I use the same keys here that Joerg Schilling
  626. * used for 'star.' (Which, somewhat confusingly, are called
  627. * "devXXX" even though they code "rdev" values.) No doubt,
  628. * other implementations use other keys. Note that there's no
  629. * reason we can't write the same information into a number of
  630. * different keys.
  631. *
  632. * Of course, this is only needed for block or char device entries.
  633. */
  634. if (archive_entry_filetype(entry_main) == AE_IFBLK
  635. || archive_entry_filetype(entry_main) == AE_IFCHR) {
  636. /*
  637. * If rdevmajor is too large, add 'SCHILY.devmajor' to
  638. * extended attributes.
  639. */
  640. dev_t rdevmajor, rdevminor;
  641. rdevmajor = archive_entry_rdevmajor(entry_main);
  642. rdevminor = archive_entry_rdevminor(entry_main);
  643. if (rdevmajor >= (1 << 18)) {
  644. add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor",
  645. rdevmajor);
  646. /*
  647. * Non-strict formatting below means we don't
  648. * have to truncate here. Not truncating improves
  649. * the chance that some more modern tar archivers
  650. * (such as GNU tar 1.13) can restore the full
  651. * value even if they don't understand the pax
  652. * extended attributes. See my rant below about
  653. * file size fields for additional details.
  654. */
  655. /* archive_entry_set_rdevmajor(entry_main,
  656. rdevmajor & ((1 << 18) - 1)); */
  657. need_extension = 1;
  658. }
  659. /*
  660. * If devminor is too large, add 'SCHILY.devminor' to
  661. * extended attributes.
  662. */
  663. if (rdevminor >= (1 << 18)) {
  664. add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor",
  665. rdevminor);
  666. /* Truncation is not necessary here, either. */
  667. /* archive_entry_set_rdevminor(entry_main,
  668. rdevminor & ((1 << 18) - 1)); */
  669. need_extension = 1;
  670. }
  671. }
  672. /*
  673. * Technically, the mtime field in the ustar header can
  674. * support 33 bits, but many platforms use signed 32-bit time
  675. * values. The cutoff of 0x7fffffff here is a compromise.
  676. * Yes, this check is duplicated just below; this helps to
  677. * avoid writing an mtime attribute just to handle a
  678. * high-resolution timestamp in "restricted pax" mode.
  679. */
  680. if (!need_extension &&
  681. ((archive_entry_mtime(entry_main) < 0)
  682. || (archive_entry_mtime(entry_main) >= 0x7fffffff)))
  683. need_extension = 1;
  684. /* I use a star-compatible file flag attribute. */
  685. p = archive_entry_fflags_text(entry_main);
  686. if (!need_extension && p != NULL && *p != '\0')
  687. need_extension = 1;
  688. /* If there are non-trivial ACL entries, we need an extension. */
  689. if (!need_extension && archive_entry_acl_count(entry_original,
  690. ARCHIVE_ENTRY_ACL_TYPE_ACCESS) > 0)
  691. need_extension = 1;
  692. /* If there are non-trivial ACL entries, we need an extension. */
  693. if (!need_extension && archive_entry_acl_count(entry_original,
  694. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) > 0)
  695. need_extension = 1;
  696. /* If there are extended attributes, we need an extension */
  697. if (!need_extension && archive_entry_xattr_count(entry_original) > 0)
  698. need_extension = 1;
  699. /*
  700. * The following items are handled differently in "pax
  701. * restricted" format. In particular, in "pax restricted"
  702. * format they won't be added unless need_extension is
  703. * already set (we're already generating an extended header, so
  704. * may as well include these).
  705. */
  706. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED ||
  707. need_extension) {
  708. if (archive_entry_mtime(entry_main) < 0 ||
  709. archive_entry_mtime(entry_main) >= 0x7fffffff ||
  710. archive_entry_mtime_nsec(entry_main) != 0)
  711. add_pax_attr_time(&(pax->pax_header), "mtime",
  712. archive_entry_mtime(entry_main),
  713. archive_entry_mtime_nsec(entry_main));
  714. if (archive_entry_ctime(entry_main) != 0 ||
  715. archive_entry_ctime_nsec(entry_main) != 0)
  716. add_pax_attr_time(&(pax->pax_header), "ctime",
  717. archive_entry_ctime(entry_main),
  718. archive_entry_ctime_nsec(entry_main));
  719. if (archive_entry_atime(entry_main) != 0 ||
  720. archive_entry_atime_nsec(entry_main) != 0)
  721. add_pax_attr_time(&(pax->pax_header), "atime",
  722. archive_entry_atime(entry_main),
  723. archive_entry_atime_nsec(entry_main));
  724. /* Store birth/creationtime only if it's earlier than mtime */
  725. if (archive_entry_birthtime_is_set(entry_main) &&
  726. archive_entry_birthtime(entry_main)
  727. < archive_entry_mtime(entry_main))
  728. add_pax_attr_time(&(pax->pax_header),
  729. "LIBARCHIVE.creationtime",
  730. archive_entry_birthtime(entry_main),
  731. archive_entry_birthtime_nsec(entry_main));
  732. /* I use a star-compatible file flag attribute. */
  733. p = archive_entry_fflags_text(entry_main);
  734. if (p != NULL && *p != '\0')
  735. add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p);
  736. /* I use star-compatible ACL attributes. */
  737. wp = archive_entry_acl_text_w(entry_original,
  738. ARCHIVE_ENTRY_ACL_TYPE_ACCESS |
  739. ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID);
  740. if (wp != NULL && *wp != L'\0')
  741. add_pax_attr_w(&(pax->pax_header),
  742. "SCHILY.acl.access", wp);
  743. wp = archive_entry_acl_text_w(entry_original,
  744. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT |
  745. ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID);
  746. if (wp != NULL && *wp != L'\0')
  747. add_pax_attr_w(&(pax->pax_header),
  748. "SCHILY.acl.default", wp);
  749. /* Store extended attributes */
  750. archive_write_pax_header_xattrs(pax, entry_original);
  751. }
  752. /* Only regular files have data. */
  753. if (archive_entry_filetype(entry_main) != AE_IFREG)
  754. archive_entry_set_size(entry_main, 0);
  755. /*
  756. * Pax-restricted does not store data for hardlinks, in order
  757. * to improve compatibility with ustar.
  758. */
  759. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
  760. hardlink != NULL)
  761. archive_entry_set_size(entry_main, 0);
  762. /*
  763. * XXX Full pax interchange format does permit a hardlink
  764. * entry to have data associated with it. I'm not supporting
  765. * that here because the client expects me to tell them whether
  766. * or not this format expects data for hardlinks. If I
  767. * don't check here, then every pax archive will end up with
  768. * duplicated data for hardlinks. Someday, there may be
  769. * need to select this behavior, in which case the following
  770. * will need to be revisited. XXX
  771. */
  772. if (hardlink != NULL)
  773. archive_entry_set_size(entry_main, 0);
  774. /* Format 'ustar' header for main entry.
  775. *
  776. * The trouble with file size: If the reader can't understand
  777. * the file size, they may not be able to locate the next
  778. * entry and the rest of the archive is toast. Pax-compliant
  779. * readers are supposed to ignore the file size in the main
  780. * header, so the question becomes how to maximize portability
  781. * for readers that don't support pax attribute extensions.
  782. * For maximum compatibility, I permit numeric extensions in
  783. * the main header so that the file size stored will always be
  784. * correct, even if it's in a format that only some
  785. * implementations understand. The technique used here is:
  786. *
  787. * a) If possible, follow the standard exactly. This handles
  788. * files up to 8 gigabytes minus 1.
  789. *
  790. * b) If that fails, try octal but omit the field terminator.
  791. * That handles files up to 64 gigabytes minus 1.
  792. *
  793. * c) Otherwise, use base-256 extensions. That handles files
  794. * up to 2^63 in this implementation, with the potential to
  795. * go up to 2^94. That should hold us for a while. ;-)
  796. *
  797. * The non-strict formatter uses similar logic for other
  798. * numeric fields, though they're less critical.
  799. */
  800. __archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0);
  801. /* If we built any extended attributes, write that entry first. */
  802. if (archive_strlen(&(pax->pax_header)) > 0) {
  803. struct archive_entry *pax_attr_entry;
  804. time_t s;
  805. uid_t uid;
  806. gid_t gid;
  807. mode_t mode;
  808. long ns;
  809. pax_attr_entry = archive_entry_new();
  810. p = archive_entry_pathname(entry_main);
  811. archive_entry_set_pathname(pax_attr_entry,
  812. build_pax_attribute_name(pax_entry_name, p));
  813. archive_entry_set_size(pax_attr_entry,
  814. archive_strlen(&(pax->pax_header)));
  815. /* Copy uid/gid (but clip to ustar limits). */
  816. uid = archive_entry_uid(entry_main);
  817. if ((unsigned int)uid >= 1 << 18)
  818. uid = (uid_t)(1 << 18) - 1;
  819. archive_entry_set_uid(pax_attr_entry, uid);
  820. gid = archive_entry_gid(entry_main);
  821. if ((unsigned int)gid >= 1 << 18)
  822. gid = (gid_t)(1 << 18) - 1;
  823. archive_entry_set_gid(pax_attr_entry, gid);
  824. /* Copy mode over (but not setuid/setgid bits) */
  825. mode = archive_entry_mode(entry_main);
  826. #ifdef S_ISUID
  827. mode &= ~S_ISUID;
  828. #endif
  829. #ifdef S_ISGID
  830. mode &= ~S_ISGID;
  831. #endif
  832. #ifdef S_ISVTX
  833. mode &= ~S_ISVTX;
  834. #endif
  835. archive_entry_set_mode(pax_attr_entry, mode);
  836. /* Copy uname/gname. */
  837. archive_entry_set_uname(pax_attr_entry,
  838. archive_entry_uname(entry_main));
  839. archive_entry_set_gname(pax_attr_entry,
  840. archive_entry_gname(entry_main));
  841. /* Copy mtime, but clip to ustar limits. */
  842. s = archive_entry_mtime(entry_main);
  843. ns = archive_entry_mtime_nsec(entry_main);
  844. if (s < 0) { s = 0; ns = 0; }
  845. if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
  846. archive_entry_set_mtime(pax_attr_entry, s, ns);
  847. /* Ditto for atime. */
  848. s = archive_entry_atime(entry_main);
  849. ns = archive_entry_atime_nsec(entry_main);
  850. if (s < 0) { s = 0; ns = 0; }
  851. if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
  852. archive_entry_set_atime(pax_attr_entry, s, ns);
  853. /* Standard ustar doesn't support ctime. */
  854. archive_entry_set_ctime(pax_attr_entry, 0, 0);
  855. r = __archive_write_format_header_ustar(a, paxbuff,
  856. pax_attr_entry, 'x', 1);
  857. archive_entry_free(pax_attr_entry);
  858. /* Note that the 'x' header shouldn't ever fail to format */
  859. if (r != 0) {
  860. const char *msg = "archive_write_pax_header: "
  861. "'x' header failed?! This can't happen.\n";
  862. size_t u = write(2, msg, strlen(msg));
  863. (void)u; /* UNUSED */
  864. exit(1);
  865. }
  866. r = (a->compressor.write)(a, paxbuff, 512);
  867. if (r != ARCHIVE_OK) {
  868. pax->entry_bytes_remaining = 0;
  869. pax->entry_padding = 0;
  870. return (ARCHIVE_FATAL);
  871. }
  872. pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header));
  873. pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
  874. r = (a->compressor.write)(a, pax->pax_header.s,
  875. archive_strlen(&(pax->pax_header)));
  876. if (r != ARCHIVE_OK) {
  877. /* If a write fails, we're pretty much toast. */
  878. return (ARCHIVE_FATAL);
  879. }
  880. /* Pad out the end of the entry. */
  881. r = write_nulls(a, pax->entry_padding);
  882. if (r != ARCHIVE_OK) {
  883. /* If a write fails, we're pretty much toast. */
  884. return (ARCHIVE_FATAL);
  885. }
  886. pax->entry_bytes_remaining = pax->entry_padding = 0;
  887. }
  888. /* Write the header for main entry. */
  889. r = (a->compressor.write)(a, ustarbuff, 512);
  890. if (r != ARCHIVE_OK)
  891. return (r);
  892. /*
  893. * Inform the client of the on-disk size we're using, so
  894. * they can avoid unnecessarily writing a body for something
  895. * that we're just going to ignore.
  896. */
  897. archive_entry_set_size(entry_original, archive_entry_size(entry_main));
  898. pax->entry_bytes_remaining = archive_entry_size(entry_main);
  899. pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
  900. archive_entry_free(entry_main);
  901. return (ret);
  902. }
  903. /*
  904. * We need a valid name for the regular 'ustar' entry. This routine
  905. * tries to hack something more-or-less reasonable.
  906. *
  907. * The approach here tries to preserve leading dir names. We do so by
  908. * working with four sections:
  909. * 1) "prefix" directory names,
  910. * 2) "suffix" directory names,
  911. * 3) inserted dir name (optional),
  912. * 4) filename.
  913. *
  914. * These sections must satisfy the following requirements:
  915. * * Parts 1 & 2 together form an initial portion of the dir name.
  916. * * Part 3 is specified by the caller. (It should not contain a leading
  917. * or trailing '/'.)
  918. * * Part 4 forms an initial portion of the base filename.
  919. * * The filename must be <= 99 chars to fit the ustar 'name' field.
  920. * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
  921. * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
  922. * * If the original name ends in a '/', the new name must also end in a '/'
  923. * * Trailing '/.' sequences may be stripped.
  924. *
  925. * Note: Recall that the ustar format does not store the '/' separating
  926. * parts 1 & 2, but does store the '/' separating parts 2 & 3.
  927. */
  928. static char *
  929. build_ustar_entry_name(char *dest, const char *src, size_t src_length,
  930. const char *insert)
  931. {
  932. const char *prefix, *prefix_end;
  933. const char *suffix, *suffix_end;
  934. const char *filename, *filename_end;
  935. char *p;
  936. int need_slash = 0; /* Was there a trailing slash? */
  937. size_t suffix_length = 99;
  938. int insert_length;
  939. /* Length of additional dir element to be added. */
  940. if (insert == NULL)
  941. insert_length = 0;
  942. else
  943. /* +2 here allows for '/' before and after the insert. */
  944. insert_length = strlen(insert) + 2;
  945. /* Step 0: Quick bailout in a common case. */
  946. if (src_length < 100 && insert == NULL) {
  947. strncpy(dest, src, src_length);
  948. dest[src_length] = '\0';
  949. return (dest);
  950. }
  951. /* Step 1: Locate filename and enforce the length restriction. */
  952. filename_end = src + src_length;
  953. /* Remove trailing '/' chars and '/.' pairs. */
  954. for (;;) {
  955. if (filename_end > src && filename_end[-1] == '/') {
  956. filename_end --;
  957. need_slash = 1; /* Remember to restore trailing '/'. */
  958. continue;
  959. }
  960. if (filename_end > src + 1 && filename_end[-1] == '.'
  961. && filename_end[-2] == '/') {
  962. filename_end -= 2;
  963. need_slash = 1; /* "foo/." will become "foo/" */
  964. continue;
  965. }
  966. break;
  967. }
  968. if (need_slash)
  969. suffix_length--;
  970. /* Find start of filename. */
  971. filename = filename_end - 1;
  972. while ((filename > src) && (*filename != '/'))
  973. filename --;
  974. if ((*filename == '/') && (filename < filename_end - 1))
  975. filename ++;
  976. /* Adjust filename_end so that filename + insert fits in 99 chars. */
  977. suffix_length -= insert_length;
  978. if (filename_end > filename + suffix_length)
  979. filename_end = filename + suffix_length;
  980. /* Calculate max size for "suffix" section (#3 above). */
  981. suffix_length -= filename_end - filename;
  982. /* Step 2: Locate the "prefix" section of the dirname, including
  983. * trailing '/'. */
  984. prefix = src;
  985. prefix_end = prefix + 155;
  986. if (prefix_end > filename)
  987. prefix_end = filename;
  988. while (prefix_end > prefix && *prefix_end != '/')
  989. prefix_end--;
  990. if ((prefix_end < filename) && (*prefix_end == '/'))
  991. prefix_end++;
  992. /* Step 3: Locate the "suffix" section of the dirname,
  993. * including trailing '/'. */
  994. suffix = prefix_end;
  995. suffix_end = suffix + suffix_length; /* Enforce limit. */
  996. if (suffix_end > filename)
  997. suffix_end = filename;
  998. if (suffix_end < suffix)
  999. suffix_end = suffix;
  1000. while (suffix_end > suffix && *suffix_end != '/')
  1001. suffix_end--;
  1002. if ((suffix_end < filename) && (*suffix_end == '/'))
  1003. suffix_end++;
  1004. /* Step 4: Build the new name. */
  1005. /* The OpenBSD strlcpy function is safer, but less portable. */
  1006. /* Rather than maintain two versions, just use the strncpy version. */
  1007. p = dest;
  1008. if (prefix_end > prefix) {
  1009. strncpy(p, prefix, prefix_end - prefix);
  1010. p += prefix_end - prefix;
  1011. }
  1012. if (suffix_end > suffix) {
  1013. strncpy(p, suffix, suffix_end - suffix);
  1014. p += suffix_end - suffix;
  1015. }
  1016. if (insert != NULL) {
  1017. /* Note: assume insert does not have leading or trailing '/' */
  1018. strcpy(p, insert);
  1019. p += strlen(insert);
  1020. *p++ = '/';
  1021. }
  1022. strncpy(p, filename, filename_end - filename);
  1023. p += filename_end - filename;
  1024. if (need_slash)
  1025. *p++ = '/';
  1026. *p++ = '\0';
  1027. return (dest);
  1028. }
  1029. /*
  1030. * The ustar header for the pax extended attributes must have a
  1031. * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
  1032. * where 'pid' is the PID of the archiving process. Unfortunately,
  1033. * that makes testing a pain since the output varies for each run,
  1034. * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
  1035. * for now. (Someday, I'll make this settable. Then I can use the
  1036. * SUS recommendation as default and test harnesses can override it
  1037. * to get predictable results.)
  1038. *
  1039. * Joerg Schilling has argued that this is unnecessary because, in
  1040. * practice, if the pax extended attributes get extracted as regular
  1041. * files, noone is going to bother reading those attributes to
  1042. * manually restore them. Based on this, 'star' uses
  1043. * /tmp/PaxHeader/'basename' as the ustar header name. This is a
  1044. * tempting argument, in part because it's simpler than the SUSv3
  1045. * recommendation, but I'm not entirely convinced. I'm also
  1046. * uncomfortable with the fact that "/tmp" is a Unix-ism.
  1047. *
  1048. * The following routine leverages build_ustar_entry_name() above and
  1049. * so is simpler than you might think. It just needs to provide the
  1050. * additional path element and handle a few pathological cases).
  1051. */
  1052. static char *
  1053. build_pax_attribute_name(char *dest, const char *src)
  1054. {
  1055. char buff[64];
  1056. const char *p;
  1057. /* Handle the null filename case. */
  1058. if (src == NULL || *src == '\0') {
  1059. strcpy(dest, "PaxHeader/blank");
  1060. return (dest);
  1061. }
  1062. /* Prune final '/' and other unwanted final elements. */
  1063. p = src + strlen(src);
  1064. for (;;) {
  1065. /* Ends in "/", remove the '/' */
  1066. if (p > src && p[-1] == '/') {
  1067. --p;
  1068. continue;
  1069. }
  1070. /* Ends in "/.", remove the '.' */
  1071. if (p > src + 1 && p[-1] == '.'
  1072. && p[-2] == '/') {
  1073. --p;
  1074. continue;
  1075. }
  1076. break;
  1077. }
  1078. /* Pathological case: After above, there was nothing left.
  1079. * This includes "/." "/./." "/.//./." etc. */
  1080. if (p == src) {
  1081. strcpy(dest, "/PaxHeader/rootdir");
  1082. return (dest);
  1083. }
  1084. /* Convert unadorned "." into a suitable filename. */
  1085. if (*src == '.' && p == src + 1) {
  1086. strcpy(dest, "PaxHeader/currentdir");
  1087. return (dest);
  1088. }
  1089. /*
  1090. * TODO: Push this string into the 'pax' structure to avoid
  1091. * recomputing it every time. That will also open the door
  1092. * to having clients override it.
  1093. */
  1094. #if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
  1095. sprintf(buff, "PaxHeader.%d", getpid());
  1096. #else
  1097. /* If the platform can't fetch the pid, don't include it. */
  1098. strcpy(buff, "PaxHeader");
  1099. #endif
  1100. /* General case: build a ustar-compatible name adding "/PaxHeader/". */
  1101. build_ustar_entry_name(dest, src, p - src, buff);
  1102. return (dest);
  1103. }
  1104. /* Write two null blocks for the end of archive */
  1105. static int
  1106. archive_write_pax_finish(struct archive_write *a)
  1107. {
  1108. struct pax *pax;
  1109. int r;
  1110. if (a->compressor.write == NULL)
  1111. return (ARCHIVE_OK);
  1112. pax = (struct pax *)a->format_data;
  1113. r = write_nulls(a, 512 * 2);
  1114. return (r);
  1115. }
  1116. static int
  1117. archive_write_pax_destroy(struct archive_write *a)
  1118. {
  1119. struct pax *pax;
  1120. pax = (struct pax *)a->format_data;
  1121. if (pax == NULL)
  1122. return (ARCHIVE_OK);
  1123. archive_string_free(&pax->pax_header);
  1124. free(pax);
  1125. a->format_data = NULL;
  1126. return (ARCHIVE_OK);
  1127. }
  1128. static int
  1129. archive_write_pax_finish_entry(struct archive_write *a)
  1130. {
  1131. struct pax *pax;
  1132. int ret;
  1133. pax = (struct pax *)a->format_data;
  1134. ret = write_nulls(a, pax->entry_bytes_remaining + pax->entry_padding);
  1135. pax->entry_bytes_remaining = pax->entry_padding = 0;
  1136. return (ret);
  1137. }
  1138. static int
  1139. write_nulls(struct archive_write *a, size_t padding)
  1140. {
  1141. int ret, to_write;
  1142. while (padding > 0) {
  1143. to_write = padding < a->null_length ? padding : a->null_length;
  1144. ret = (a->compressor.write)(a, a->nulls, to_write);
  1145. if (ret != ARCHIVE_OK)
  1146. return (ret);
  1147. padding -= to_write;
  1148. }
  1149. return (ARCHIVE_OK);
  1150. }
  1151. static ssize_t
  1152. archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
  1153. {
  1154. struct pax *pax;
  1155. int ret;
  1156. pax = (struct pax *)a->format_data;
  1157. if (s > pax->entry_bytes_remaining)
  1158. s = pax->entry_bytes_remaining;
  1159. ret = (a->compressor.write)(a, buff, s);
  1160. pax->entry_bytes_remaining -= s;
  1161. if (ret == ARCHIVE_OK)
  1162. return (s);
  1163. else
  1164. return (ret);
  1165. }
  1166. static int
  1167. has_non_ASCII(const wchar_t *wp)
  1168. {
  1169. if (wp == NULL)
  1170. return (1);
  1171. while (*wp != L'\0' && *wp < 128)
  1172. wp++;
  1173. return (*wp != L'\0');
  1174. }
  1175. /*
  1176. * Used by extended attribute support; encodes the name
  1177. * so that there will be no '=' characters in the result.
  1178. */
  1179. static char *
  1180. url_encode(const char *in)
  1181. {
  1182. const char *s;
  1183. char *d;
  1184. int out_len = 0;
  1185. char *out;
  1186. for (s = in; *s != '\0'; s++) {
  1187. if (*s < 33 || *s > 126 || *s == '%' || *s == '=')
  1188. out_len += 3;
  1189. else
  1190. out_len++;
  1191. }
  1192. out = (char *)malloc(out_len + 1);
  1193. if (out == NULL)
  1194. return (NULL);
  1195. for (s = in, d = out; *s != '\0'; s++) {
  1196. /* encode any non-printable ASCII character or '%' or '=' */
  1197. if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
  1198. /* URL encoding is '%' followed by two hex digits */
  1199. *d++ = '%';
  1200. *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
  1201. *d++ = "0123456789ABCDEF"[0x0f & *s];
  1202. } else {
  1203. *d++ = *s;
  1204. }
  1205. }
  1206. *d = '\0';
  1207. return (out);
  1208. }
  1209. /*
  1210. * Encode a sequence of bytes into a C string using base-64 encoding.
  1211. *
  1212. * Returns a null-terminated C string allocated with malloc(); caller
  1213. * is responsible for freeing the result.
  1214. */
  1215. static char *
  1216. base64_encode(const char *s, size_t len)
  1217. {
  1218. static const char digits[64] =
  1219. { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
  1220. 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
  1221. 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
  1222. 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  1223. '8','9','+','/' };
  1224. int v;
  1225. char *d, *out;
  1226. /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
  1227. out = (char *)malloc((len * 4 + 2) / 3 + 1);
  1228. if (out == NULL)
  1229. return (NULL);
  1230. d = out;
  1231. /* Convert each group of 3 bytes into 4 characters. */
  1232. while (len >= 3) {
  1233. v = (((int)s[0] << 16) & 0xff0000)
  1234. | (((int)s[1] << 8) & 0xff00)
  1235. | (((int)s[2]) & 0x00ff);
  1236. s += 3;
  1237. len -= 3;
  1238. *d++ = digits[(v >> 18) & 0x3f];
  1239. *d++ = digits[(v >> 12) & 0x3f];
  1240. *d++ = digits[(v >> 6) & 0x3f];
  1241. *d++ = digits[(v) & 0x3f];
  1242. }
  1243. /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
  1244. switch (len) {
  1245. case 0: break;
  1246. case 1:
  1247. v = (((int)s[0] << 16) & 0xff0000);
  1248. *d++ = digits[(v >> 18) & 0x3f];
  1249. *d++ = digits[(v >> 12) & 0x3f];
  1250. break;
  1251. case 2:
  1252. v = (((int)s[0] << 16) & 0xff0000)
  1253. | (((int)s[1] << 8) & 0xff00);
  1254. *d++ = digits[(v >> 18) & 0x3f];
  1255. *d++ = digits[(v >> 12) & 0x3f];
  1256. *d++ = digits[(v >> 6) & 0x3f];
  1257. break;
  1258. }
  1259. /* Add trailing NUL character so output is a valid C string. */
  1260. *d++ = '\0';
  1261. return (out);
  1262. }