archive_write_set_format_pax.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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. /* Include star-compatible metadata info. */
  750. /* Note: "SCHILY.dev{major,minor}" are NOT the
  751. * major/minor portions of "SCHILY.dev". */
  752. add_pax_attr_int(&(pax->pax_header), "SCHILY.dev",
  753. archive_entry_dev(entry_main));
  754. add_pax_attr_int(&(pax->pax_header), "SCHILY.ino",
  755. archive_entry_ino64(entry_main));
  756. add_pax_attr_int(&(pax->pax_header), "SCHILY.nlink",
  757. archive_entry_nlink(entry_main));
  758. /* Store extended attributes */
  759. archive_write_pax_header_xattrs(pax, entry_original);
  760. }
  761. /* Only regular files have data. */
  762. if (archive_entry_filetype(entry_main) != AE_IFREG)
  763. archive_entry_set_size(entry_main, 0);
  764. /*
  765. * Pax-restricted does not store data for hardlinks, in order
  766. * to improve compatibility with ustar.
  767. */
  768. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
  769. hardlink != NULL)
  770. archive_entry_set_size(entry_main, 0);
  771. /*
  772. * XXX Full pax interchange format does permit a hardlink
  773. * entry to have data associated with it. I'm not supporting
  774. * that here because the client expects me to tell them whether
  775. * or not this format expects data for hardlinks. If I
  776. * don't check here, then every pax archive will end up with
  777. * duplicated data for hardlinks. Someday, there may be
  778. * need to select this behavior, in which case the following
  779. * will need to be revisited. XXX
  780. */
  781. if (hardlink != NULL)
  782. archive_entry_set_size(entry_main, 0);
  783. /* Format 'ustar' header for main entry.
  784. *
  785. * The trouble with file size: If the reader can't understand
  786. * the file size, they may not be able to locate the next
  787. * entry and the rest of the archive is toast. Pax-compliant
  788. * readers are supposed to ignore the file size in the main
  789. * header, so the question becomes how to maximize portability
  790. * for readers that don't support pax attribute extensions.
  791. * For maximum compatibility, I permit numeric extensions in
  792. * the main header so that the file size stored will always be
  793. * correct, even if it's in a format that only some
  794. * implementations understand. The technique used here is:
  795. *
  796. * a) If possible, follow the standard exactly. This handles
  797. * files up to 8 gigabytes minus 1.
  798. *
  799. * b) If that fails, try octal but omit the field terminator.
  800. * That handles files up to 64 gigabytes minus 1.
  801. *
  802. * c) Otherwise, use base-256 extensions. That handles files
  803. * up to 2^63 in this implementation, with the potential to
  804. * go up to 2^94. That should hold us for a while. ;-)
  805. *
  806. * The non-strict formatter uses similar logic for other
  807. * numeric fields, though they're less critical.
  808. */
  809. __archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0);
  810. /* If we built any extended attributes, write that entry first. */
  811. if (archive_strlen(&(pax->pax_header)) > 0) {
  812. struct archive_entry *pax_attr_entry;
  813. time_t s;
  814. uid_t uid;
  815. gid_t gid;
  816. mode_t mode;
  817. long ns;
  818. pax_attr_entry = archive_entry_new();
  819. p = archive_entry_pathname(entry_main);
  820. archive_entry_set_pathname(pax_attr_entry,
  821. build_pax_attribute_name(pax_entry_name, p));
  822. archive_entry_set_size(pax_attr_entry,
  823. archive_strlen(&(pax->pax_header)));
  824. /* Copy uid/gid (but clip to ustar limits). */
  825. uid = archive_entry_uid(entry_main);
  826. if ((unsigned int)uid >= 1 << 18)
  827. uid = (uid_t)(1 << 18) - 1;
  828. archive_entry_set_uid(pax_attr_entry, uid);
  829. gid = archive_entry_gid(entry_main);
  830. if ((unsigned int)gid >= 1 << 18)
  831. gid = (gid_t)(1 << 18) - 1;
  832. archive_entry_set_gid(pax_attr_entry, gid);
  833. /* Copy mode over (but not setuid/setgid bits) */
  834. mode = archive_entry_mode(entry_main);
  835. #ifdef S_ISUID
  836. mode &= ~S_ISUID;
  837. #endif
  838. #ifdef S_ISGID
  839. mode &= ~S_ISGID;
  840. #endif
  841. #ifdef S_ISVTX
  842. mode &= ~S_ISVTX;
  843. #endif
  844. archive_entry_set_mode(pax_attr_entry, mode);
  845. /* Copy uname/gname. */
  846. archive_entry_set_uname(pax_attr_entry,
  847. archive_entry_uname(entry_main));
  848. archive_entry_set_gname(pax_attr_entry,
  849. archive_entry_gname(entry_main));
  850. /* Copy mtime, but clip to ustar limits. */
  851. s = archive_entry_mtime(entry_main);
  852. ns = archive_entry_mtime_nsec(entry_main);
  853. if (s < 0) { s = 0; ns = 0; }
  854. if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
  855. archive_entry_set_mtime(pax_attr_entry, s, ns);
  856. /* Ditto for atime. */
  857. s = archive_entry_atime(entry_main);
  858. ns = archive_entry_atime_nsec(entry_main);
  859. if (s < 0) { s = 0; ns = 0; }
  860. if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
  861. archive_entry_set_atime(pax_attr_entry, s, ns);
  862. /* Standard ustar doesn't support ctime. */
  863. archive_entry_set_ctime(pax_attr_entry, 0, 0);
  864. r = __archive_write_format_header_ustar(a, paxbuff,
  865. pax_attr_entry, 'x', 1);
  866. archive_entry_free(pax_attr_entry);
  867. /* Note that the 'x' header shouldn't ever fail to format */
  868. if (r != 0) {
  869. const char *msg = "archive_write_pax_header: "
  870. "'x' header failed?! This can't happen.\n";
  871. size_t u = write(2, msg, strlen(msg));
  872. (void)u; /* UNUSED */
  873. exit(1);
  874. }
  875. r = (a->compressor.write)(a, paxbuff, 512);
  876. if (r != ARCHIVE_OK) {
  877. pax->entry_bytes_remaining = 0;
  878. pax->entry_padding = 0;
  879. return (ARCHIVE_FATAL);
  880. }
  881. pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header));
  882. pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
  883. r = (a->compressor.write)(a, pax->pax_header.s,
  884. archive_strlen(&(pax->pax_header)));
  885. if (r != ARCHIVE_OK) {
  886. /* If a write fails, we're pretty much toast. */
  887. return (ARCHIVE_FATAL);
  888. }
  889. /* Pad out the end of the entry. */
  890. r = write_nulls(a, pax->entry_padding);
  891. if (r != ARCHIVE_OK) {
  892. /* If a write fails, we're pretty much toast. */
  893. return (ARCHIVE_FATAL);
  894. }
  895. pax->entry_bytes_remaining = pax->entry_padding = 0;
  896. }
  897. /* Write the header for main entry. */
  898. r = (a->compressor.write)(a, ustarbuff, 512);
  899. if (r != ARCHIVE_OK)
  900. return (r);
  901. /*
  902. * Inform the client of the on-disk size we're using, so
  903. * they can avoid unnecessarily writing a body for something
  904. * that we're just going to ignore.
  905. */
  906. archive_entry_set_size(entry_original, archive_entry_size(entry_main));
  907. pax->entry_bytes_remaining = archive_entry_size(entry_main);
  908. pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
  909. archive_entry_free(entry_main);
  910. return (ret);
  911. }
  912. /*
  913. * We need a valid name for the regular 'ustar' entry. This routine
  914. * tries to hack something more-or-less reasonable.
  915. *
  916. * The approach here tries to preserve leading dir names. We do so by
  917. * working with four sections:
  918. * 1) "prefix" directory names,
  919. * 2) "suffix" directory names,
  920. * 3) inserted dir name (optional),
  921. * 4) filename.
  922. *
  923. * These sections must satisfy the following requirements:
  924. * * Parts 1 & 2 together form an initial portion of the dir name.
  925. * * Part 3 is specified by the caller. (It should not contain a leading
  926. * or trailing '/'.)
  927. * * Part 4 forms an initial portion of the base filename.
  928. * * The filename must be <= 99 chars to fit the ustar 'name' field.
  929. * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
  930. * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
  931. * * If the original name ends in a '/', the new name must also end in a '/'
  932. * * Trailing '/.' sequences may be stripped.
  933. *
  934. * Note: Recall that the ustar format does not store the '/' separating
  935. * parts 1 & 2, but does store the '/' separating parts 2 & 3.
  936. */
  937. static char *
  938. build_ustar_entry_name(char *dest, const char *src, size_t src_length,
  939. const char *insert)
  940. {
  941. const char *prefix, *prefix_end;
  942. const char *suffix, *suffix_end;
  943. const char *filename, *filename_end;
  944. char *p;
  945. int need_slash = 0; /* Was there a trailing slash? */
  946. size_t suffix_length = 99;
  947. int insert_length;
  948. /* Length of additional dir element to be added. */
  949. if (insert == NULL)
  950. insert_length = 0;
  951. else
  952. /* +2 here allows for '/' before and after the insert. */
  953. insert_length = strlen(insert) + 2;
  954. /* Step 0: Quick bailout in a common case. */
  955. if (src_length < 100 && insert == NULL) {
  956. strncpy(dest, src, src_length);
  957. dest[src_length] = '\0';
  958. return (dest);
  959. }
  960. /* Step 1: Locate filename and enforce the length restriction. */
  961. filename_end = src + src_length;
  962. /* Remove trailing '/' chars and '/.' pairs. */
  963. for (;;) {
  964. if (filename_end > src && filename_end[-1] == '/') {
  965. filename_end --;
  966. need_slash = 1; /* Remember to restore trailing '/'. */
  967. continue;
  968. }
  969. if (filename_end > src + 1 && filename_end[-1] == '.'
  970. && filename_end[-2] == '/') {
  971. filename_end -= 2;
  972. need_slash = 1; /* "foo/." will become "foo/" */
  973. continue;
  974. }
  975. break;
  976. }
  977. if (need_slash)
  978. suffix_length--;
  979. /* Find start of filename. */
  980. filename = filename_end - 1;
  981. while ((filename > src) && (*filename != '/'))
  982. filename --;
  983. if ((*filename == '/') && (filename < filename_end - 1))
  984. filename ++;
  985. /* Adjust filename_end so that filename + insert fits in 99 chars. */
  986. suffix_length -= insert_length;
  987. if (filename_end > filename + suffix_length)
  988. filename_end = filename + suffix_length;
  989. /* Calculate max size for "suffix" section (#3 above). */
  990. suffix_length -= filename_end - filename;
  991. /* Step 2: Locate the "prefix" section of the dirname, including
  992. * trailing '/'. */
  993. prefix = src;
  994. prefix_end = prefix + 155;
  995. if (prefix_end > filename)
  996. prefix_end = filename;
  997. while (prefix_end > prefix && *prefix_end != '/')
  998. prefix_end--;
  999. if ((prefix_end < filename) && (*prefix_end == '/'))
  1000. prefix_end++;
  1001. /* Step 3: Locate the "suffix" section of the dirname,
  1002. * including trailing '/'. */
  1003. suffix = prefix_end;
  1004. suffix_end = suffix + suffix_length; /* Enforce limit. */
  1005. if (suffix_end > filename)
  1006. suffix_end = filename;
  1007. if (suffix_end < suffix)
  1008. suffix_end = suffix;
  1009. while (suffix_end > suffix && *suffix_end != '/')
  1010. suffix_end--;
  1011. if ((suffix_end < filename) && (*suffix_end == '/'))
  1012. suffix_end++;
  1013. /* Step 4: Build the new name. */
  1014. /* The OpenBSD strlcpy function is safer, but less portable. */
  1015. /* Rather than maintain two versions, just use the strncpy version. */
  1016. p = dest;
  1017. if (prefix_end > prefix) {
  1018. strncpy(p, prefix, prefix_end - prefix);
  1019. p += prefix_end - prefix;
  1020. }
  1021. if (suffix_end > suffix) {
  1022. strncpy(p, suffix, suffix_end - suffix);
  1023. p += suffix_end - suffix;
  1024. }
  1025. if (insert != NULL) {
  1026. /* Note: assume insert does not have leading or trailing '/' */
  1027. strcpy(p, insert);
  1028. p += strlen(insert);
  1029. *p++ = '/';
  1030. }
  1031. strncpy(p, filename, filename_end - filename);
  1032. p += filename_end - filename;
  1033. if (need_slash)
  1034. *p++ = '/';
  1035. *p++ = '\0';
  1036. return (dest);
  1037. }
  1038. /*
  1039. * The ustar header for the pax extended attributes must have a
  1040. * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
  1041. * where 'pid' is the PID of the archiving process. Unfortunately,
  1042. * that makes testing a pain since the output varies for each run,
  1043. * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
  1044. * for now. (Someday, I'll make this settable. Then I can use the
  1045. * SUS recommendation as default and test harnesses can override it
  1046. * to get predictable results.)
  1047. *
  1048. * Joerg Schilling has argued that this is unnecessary because, in
  1049. * practice, if the pax extended attributes get extracted as regular
  1050. * files, noone is going to bother reading those attributes to
  1051. * manually restore them. Based on this, 'star' uses
  1052. * /tmp/PaxHeader/'basename' as the ustar header name. This is a
  1053. * tempting argument, in part because it's simpler than the SUSv3
  1054. * recommendation, but I'm not entirely convinced. I'm also
  1055. * uncomfortable with the fact that "/tmp" is a Unix-ism.
  1056. *
  1057. * The following routine leverages build_ustar_entry_name() above and
  1058. * so is simpler than you might think. It just needs to provide the
  1059. * additional path element and handle a few pathological cases).
  1060. */
  1061. static char *
  1062. build_pax_attribute_name(char *dest, const char *src)
  1063. {
  1064. char buff[64];
  1065. const char *p;
  1066. /* Handle the null filename case. */
  1067. if (src == NULL || *src == '\0') {
  1068. strcpy(dest, "PaxHeader/blank");
  1069. return (dest);
  1070. }
  1071. /* Prune final '/' and other unwanted final elements. */
  1072. p = src + strlen(src);
  1073. for (;;) {
  1074. /* Ends in "/", remove the '/' */
  1075. if (p > src && p[-1] == '/') {
  1076. --p;
  1077. continue;
  1078. }
  1079. /* Ends in "/.", remove the '.' */
  1080. if (p > src + 1 && p[-1] == '.'
  1081. && p[-2] == '/') {
  1082. --p;
  1083. continue;
  1084. }
  1085. break;
  1086. }
  1087. /* Pathological case: After above, there was nothing left.
  1088. * This includes "/." "/./." "/.//./." etc. */
  1089. if (p == src) {
  1090. strcpy(dest, "/PaxHeader/rootdir");
  1091. return (dest);
  1092. }
  1093. /* Convert unadorned "." into a suitable filename. */
  1094. if (*src == '.' && p == src + 1) {
  1095. strcpy(dest, "PaxHeader/currentdir");
  1096. return (dest);
  1097. }
  1098. /*
  1099. * TODO: Push this string into the 'pax' structure to avoid
  1100. * recomputing it every time. That will also open the door
  1101. * to having clients override it.
  1102. */
  1103. #if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
  1104. sprintf(buff, "PaxHeader.%d", getpid());
  1105. #else
  1106. /* If the platform can't fetch the pid, don't include it. */
  1107. strcpy(buff, "PaxHeader");
  1108. #endif
  1109. /* General case: build a ustar-compatible name adding "/PaxHeader/". */
  1110. build_ustar_entry_name(dest, src, p - src, buff);
  1111. return (dest);
  1112. }
  1113. /* Write two null blocks for the end of archive */
  1114. static int
  1115. archive_write_pax_finish(struct archive_write *a)
  1116. {
  1117. struct pax *pax;
  1118. int r;
  1119. if (a->compressor.write == NULL)
  1120. return (ARCHIVE_OK);
  1121. pax = (struct pax *)a->format_data;
  1122. r = write_nulls(a, 512 * 2);
  1123. return (r);
  1124. }
  1125. static int
  1126. archive_write_pax_destroy(struct archive_write *a)
  1127. {
  1128. struct pax *pax;
  1129. pax = (struct pax *)a->format_data;
  1130. if (pax == NULL)
  1131. return (ARCHIVE_OK);
  1132. archive_string_free(&pax->pax_header);
  1133. free(pax);
  1134. a->format_data = NULL;
  1135. return (ARCHIVE_OK);
  1136. }
  1137. static int
  1138. archive_write_pax_finish_entry(struct archive_write *a)
  1139. {
  1140. struct pax *pax;
  1141. int ret;
  1142. pax = (struct pax *)a->format_data;
  1143. ret = write_nulls(a, pax->entry_bytes_remaining + pax->entry_padding);
  1144. pax->entry_bytes_remaining = pax->entry_padding = 0;
  1145. return (ret);
  1146. }
  1147. static int
  1148. write_nulls(struct archive_write *a, size_t padding)
  1149. {
  1150. int ret, to_write;
  1151. while (padding > 0) {
  1152. to_write = padding < a->null_length ? padding : a->null_length;
  1153. ret = (a->compressor.write)(a, a->nulls, to_write);
  1154. if (ret != ARCHIVE_OK)
  1155. return (ret);
  1156. padding -= to_write;
  1157. }
  1158. return (ARCHIVE_OK);
  1159. }
  1160. static ssize_t
  1161. archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
  1162. {
  1163. struct pax *pax;
  1164. int ret;
  1165. pax = (struct pax *)a->format_data;
  1166. if (s > pax->entry_bytes_remaining)
  1167. s = pax->entry_bytes_remaining;
  1168. ret = (a->compressor.write)(a, buff, s);
  1169. pax->entry_bytes_remaining -= s;
  1170. if (ret == ARCHIVE_OK)
  1171. return (s);
  1172. else
  1173. return (ret);
  1174. }
  1175. static int
  1176. has_non_ASCII(const wchar_t *wp)
  1177. {
  1178. if (wp == NULL)
  1179. return (1);
  1180. while (*wp != L'\0' && *wp < 128)
  1181. wp++;
  1182. return (*wp != L'\0');
  1183. }
  1184. /*
  1185. * Used by extended attribute support; encodes the name
  1186. * so that there will be no '=' characters in the result.
  1187. */
  1188. static char *
  1189. url_encode(const char *in)
  1190. {
  1191. const char *s;
  1192. char *d;
  1193. int out_len = 0;
  1194. char *out;
  1195. for (s = in; *s != '\0'; s++) {
  1196. if (*s < 33 || *s > 126 || *s == '%' || *s == '=')
  1197. out_len += 3;
  1198. else
  1199. out_len++;
  1200. }
  1201. out = (char *)malloc(out_len + 1);
  1202. if (out == NULL)
  1203. return (NULL);
  1204. for (s = in, d = out; *s != '\0'; s++) {
  1205. /* encode any non-printable ASCII character or '%' or '=' */
  1206. if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
  1207. /* URL encoding is '%' followed by two hex digits */
  1208. *d++ = '%';
  1209. *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
  1210. *d++ = "0123456789ABCDEF"[0x0f & *s];
  1211. } else {
  1212. *d++ = *s;
  1213. }
  1214. }
  1215. *d = '\0';
  1216. return (out);
  1217. }
  1218. /*
  1219. * Encode a sequence of bytes into a C string using base-64 encoding.
  1220. *
  1221. * Returns a null-terminated C string allocated with malloc(); caller
  1222. * is responsible for freeing the result.
  1223. */
  1224. static char *
  1225. base64_encode(const char *s, size_t len)
  1226. {
  1227. static const char digits[64] =
  1228. { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
  1229. 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
  1230. 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
  1231. 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  1232. '8','9','+','/' };
  1233. int v;
  1234. char *d, *out;
  1235. /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
  1236. out = (char *)malloc((len * 4 + 2) / 3 + 1);
  1237. if (out == NULL)
  1238. return (NULL);
  1239. d = out;
  1240. /* Convert each group of 3 bytes into 4 characters. */
  1241. while (len >= 3) {
  1242. v = (((int)s[0] << 16) & 0xff0000)
  1243. | (((int)s[1] << 8) & 0xff00)
  1244. | (((int)s[2]) & 0x00ff);
  1245. s += 3;
  1246. len -= 3;
  1247. *d++ = digits[(v >> 18) & 0x3f];
  1248. *d++ = digits[(v >> 12) & 0x3f];
  1249. *d++ = digits[(v >> 6) & 0x3f];
  1250. *d++ = digits[(v) & 0x3f];
  1251. }
  1252. /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
  1253. switch (len) {
  1254. case 0: break;
  1255. case 1:
  1256. v = (((int)s[0] << 16) & 0xff0000);
  1257. *d++ = digits[(v >> 18) & 0x3f];
  1258. *d++ = digits[(v >> 12) & 0x3f];
  1259. break;
  1260. case 2:
  1261. v = (((int)s[0] << 16) & 0xff0000)
  1262. | (((int)s[1] << 8) & 0xff00);
  1263. *d++ = digits[(v >> 18) & 0x3f];
  1264. *d++ = digits[(v >> 12) & 0x3f];
  1265. *d++ = digits[(v >> 6) & 0x3f];
  1266. break;
  1267. }
  1268. /* Add trailing NUL character so output is a valid C string. */
  1269. *d++ = '\0';
  1270. return (out);
  1271. }