archive_write_set_format_pax.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  4. * Copyright (c) 2016 Martin Matuska
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_pax.c 201162 2009-12-29 05:47:46Z kientzle $");
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_entry_locale.h"
  41. #include "archive_private.h"
  42. #include "archive_write_private.h"
  43. struct sparse_block {
  44. struct sparse_block *next;
  45. int is_hole;
  46. uint64_t offset;
  47. uint64_t remaining;
  48. };
  49. struct pax {
  50. uint64_t entry_bytes_remaining;
  51. uint64_t entry_padding;
  52. struct archive_string l_url_encoded_name;
  53. struct archive_string pax_header;
  54. struct archive_string sparse_map;
  55. size_t sparse_map_padding;
  56. struct sparse_block *sparse_list;
  57. struct sparse_block *sparse_tail;
  58. struct archive_string_conv *sconv_utf8;
  59. int opt_binary;
  60. unsigned flags;
  61. #define WRITE_SCHILY_XATTR (1 << 0)
  62. #define WRITE_LIBARCHIVE_XATTR (1 << 1)
  63. };
  64. static void add_pax_attr(struct archive_string *, const char *key,
  65. const char *value);
  66. static void add_pax_attr_binary(struct archive_string *,
  67. const char *key,
  68. const char *value, size_t value_len);
  69. static void add_pax_attr_int(struct archive_string *,
  70. const char *key, int64_t value);
  71. static void add_pax_attr_time(struct archive_string *,
  72. const char *key, int64_t sec,
  73. unsigned long nanos);
  74. static int add_pax_acl(struct archive_write *,
  75. struct archive_entry *, struct pax *, int);
  76. static ssize_t archive_write_pax_data(struct archive_write *,
  77. const void *, size_t);
  78. static int archive_write_pax_close(struct archive_write *);
  79. static int archive_write_pax_free(struct archive_write *);
  80. static int archive_write_pax_finish_entry(struct archive_write *);
  81. static int archive_write_pax_header(struct archive_write *,
  82. struct archive_entry *);
  83. static int archive_write_pax_options(struct archive_write *,
  84. const char *, const char *);
  85. static char *base64_encode(const char *src, size_t len);
  86. static char *build_gnu_sparse_name(char *dest, const char *src);
  87. static char *build_pax_attribute_name(char *dest, const char *src);
  88. static char *build_ustar_entry_name(char *dest, const char *src,
  89. size_t src_length, const char *insert);
  90. static char *format_int(char *dest, int64_t);
  91. static int has_non_ASCII(const char *);
  92. static void sparse_list_clear(struct pax *);
  93. static int sparse_list_add(struct pax *, int64_t, int64_t);
  94. static char *url_encode(const char *in);
  95. /*
  96. * Set output format to 'restricted pax' format.
  97. *
  98. * This is the same as normal 'pax', but tries to suppress
  99. * the pax header whenever possible. This is the default for
  100. * bsdtar, for instance.
  101. */
  102. int
  103. archive_write_set_format_pax_restricted(struct archive *_a)
  104. {
  105. struct archive_write *a = (struct archive_write *)_a;
  106. int r;
  107. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  108. ARCHIVE_STATE_NEW, "archive_write_set_format_pax_restricted");
  109. r = archive_write_set_format_pax(&a->archive);
  110. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
  111. a->archive.archive_format_name = "restricted POSIX pax interchange";
  112. return (r);
  113. }
  114. /*
  115. * Set output format to 'pax' format.
  116. */
  117. int
  118. archive_write_set_format_pax(struct archive *_a)
  119. {
  120. struct archive_write *a = (struct archive_write *)_a;
  121. struct pax *pax;
  122. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  123. ARCHIVE_STATE_NEW, "archive_write_set_format_pax");
  124. if (a->format_free != NULL)
  125. (a->format_free)(a);
  126. pax = (struct pax *)calloc(1, sizeof(*pax));
  127. if (pax == NULL) {
  128. archive_set_error(&a->archive, ENOMEM,
  129. "Can't allocate pax data");
  130. return (ARCHIVE_FATAL);
  131. }
  132. pax->flags = WRITE_LIBARCHIVE_XATTR | WRITE_SCHILY_XATTR;
  133. a->format_data = pax;
  134. a->format_name = "pax";
  135. a->format_options = archive_write_pax_options;
  136. a->format_write_header = archive_write_pax_header;
  137. a->format_write_data = archive_write_pax_data;
  138. a->format_close = archive_write_pax_close;
  139. a->format_free = archive_write_pax_free;
  140. a->format_finish_entry = archive_write_pax_finish_entry;
  141. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  142. a->archive.archive_format_name = "POSIX pax interchange";
  143. return (ARCHIVE_OK);
  144. }
  145. static int
  146. archive_write_pax_options(struct archive_write *a, const char *key,
  147. const char *val)
  148. {
  149. struct pax *pax = (struct pax *)a->format_data;
  150. int ret = ARCHIVE_FAILED;
  151. if (strcmp(key, "hdrcharset") == 0) {
  152. /*
  153. * The character-set we can use are defined in
  154. * IEEE Std 1003.1-2001
  155. */
  156. if (val == NULL || val[0] == 0)
  157. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  158. "pax: hdrcharset option needs a character-set name");
  159. else if (strcmp(val, "BINARY") == 0 ||
  160. strcmp(val, "binary") == 0) {
  161. /*
  162. * Specify binary mode. We will not convert
  163. * filenames, uname and gname to any charsets.
  164. */
  165. pax->opt_binary = 1;
  166. ret = ARCHIVE_OK;
  167. } else if (strcmp(val, "UTF-8") == 0) {
  168. /*
  169. * Specify UTF-8 character-set to be used for
  170. * filenames. This is almost the test that
  171. * running platform supports the string conversion.
  172. * Especially libarchive_test needs this trick for
  173. * its test.
  174. */
  175. pax->sconv_utf8 = archive_string_conversion_to_charset(
  176. &(a->archive), "UTF-8", 0);
  177. if (pax->sconv_utf8 == NULL)
  178. ret = ARCHIVE_FATAL;
  179. else
  180. ret = ARCHIVE_OK;
  181. } else
  182. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  183. "pax: invalid charset name");
  184. return (ret);
  185. }
  186. /* Note: The "warn" return is just to inform the options
  187. * supervisor that we didn't handle it. It will generate
  188. * a suitable error if no one used this option. */
  189. return (ARCHIVE_WARN);
  190. }
  191. /*
  192. * Note: This code assumes that 'nanos' has the same sign as 'sec',
  193. * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
  194. * and not -0.8 seconds. This is a pretty pedantic point, as we're
  195. * unlikely to encounter many real files created before Jan 1, 1970,
  196. * much less ones with timestamps recorded to sub-second resolution.
  197. */
  198. static void
  199. add_pax_attr_time(struct archive_string *as, const char *key,
  200. int64_t sec, unsigned long nanos)
  201. {
  202. int digit, i;
  203. char *t;
  204. /*
  205. * Note that each byte contributes fewer than 3 base-10
  206. * digits, so this will always be big enough.
  207. */
  208. char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];
  209. tmp[sizeof(tmp) - 1] = 0;
  210. t = tmp + sizeof(tmp) - 1;
  211. /* Skip trailing zeros in the fractional part. */
  212. for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
  213. digit = nanos % 10;
  214. nanos /= 10;
  215. }
  216. /* Only format the fraction if it's non-zero. */
  217. if (i > 0) {
  218. while (i > 0) {
  219. *--t = "0123456789"[digit];
  220. digit = nanos % 10;
  221. nanos /= 10;
  222. i--;
  223. }
  224. *--t = '.';
  225. }
  226. t = format_int(t, sec);
  227. add_pax_attr(as, key, t);
  228. }
  229. static char *
  230. format_int(char *t, int64_t i)
  231. {
  232. uint64_t ui;
  233. if (i < 0)
  234. ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : (uint64_t)(-i);
  235. else
  236. ui = i;
  237. do {
  238. *--t = "0123456789"[ui % 10];
  239. } while (ui /= 10);
  240. if (i < 0)
  241. *--t = '-';
  242. return (t);
  243. }
  244. static void
  245. add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
  246. {
  247. char tmp[1 + 3 * sizeof(value)];
  248. tmp[sizeof(tmp) - 1] = 0;
  249. add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
  250. }
  251. /*
  252. * Add a key/value attribute to the pax header. This function handles
  253. * the length field and various other syntactic requirements.
  254. */
  255. static void
  256. add_pax_attr(struct archive_string *as, const char *key, const char *value)
  257. {
  258. add_pax_attr_binary(as, key, value, strlen(value));
  259. }
  260. /*
  261. * Add a key/value attribute to the pax header. This function handles
  262. * binary values.
  263. */
  264. static void
  265. add_pax_attr_binary(struct archive_string *as, const char *key,
  266. const char *value, size_t value_len)
  267. {
  268. int digits, i, len, next_ten;
  269. char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */
  270. /*-
  271. * PAX attributes have the following layout:
  272. * <len> <space> <key> <=> <value> <nl>
  273. */
  274. len = 1 + (int)strlen(key) + 1 + (int)value_len + 1;
  275. /*
  276. * The <len> field includes the length of the <len> field, so
  277. * computing the correct length is tricky. I start by
  278. * counting the number of base-10 digits in 'len' and
  279. * computing the next higher power of 10.
  280. */
  281. next_ten = 1;
  282. digits = 0;
  283. i = len;
  284. while (i > 0) {
  285. i = i / 10;
  286. digits++;
  287. next_ten = next_ten * 10;
  288. }
  289. /*
  290. * For example, if string without the length field is 99
  291. * chars, then adding the 2 digit length "99" will force the
  292. * total length past 100, requiring an extra digit. The next
  293. * statement adjusts for this effect.
  294. */
  295. if (len + digits >= next_ten)
  296. digits++;
  297. /* Now, we have the right length so we can build the line. */
  298. tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */
  299. archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits));
  300. archive_strappend_char(as, ' ');
  301. archive_strcat(as, key);
  302. archive_strappend_char(as, '=');
  303. archive_array_append(as, value, value_len);
  304. archive_strappend_char(as, '\n');
  305. }
  306. static void
  307. archive_write_pax_header_xattr(struct pax *pax, const char *encoded_name,
  308. const void *value, size_t value_len)
  309. {
  310. struct archive_string s;
  311. char *encoded_value;
  312. if (pax->flags & WRITE_LIBARCHIVE_XATTR) {
  313. encoded_value = base64_encode((const char *)value, value_len);
  314. if (encoded_name != NULL && encoded_value != NULL) {
  315. archive_string_init(&s);
  316. archive_strcpy(&s, "LIBARCHIVE.xattr.");
  317. archive_strcat(&s, encoded_name);
  318. add_pax_attr(&(pax->pax_header), s.s, encoded_value);
  319. archive_string_free(&s);
  320. }
  321. free(encoded_value);
  322. }
  323. if (pax->flags & WRITE_SCHILY_XATTR) {
  324. archive_string_init(&s);
  325. archive_strcpy(&s, "SCHILY.xattr.");
  326. archive_strcat(&s, encoded_name);
  327. add_pax_attr_binary(&(pax->pax_header), s.s, value, value_len);
  328. archive_string_free(&s);
  329. }
  330. }
  331. static int
  332. archive_write_pax_header_xattrs(struct archive_write *a,
  333. struct pax *pax, struct archive_entry *entry)
  334. {
  335. int i = archive_entry_xattr_reset(entry);
  336. while (i--) {
  337. const char *name;
  338. const void *value;
  339. char *url_encoded_name = NULL, *encoded_name = NULL;
  340. size_t size;
  341. int r;
  342. archive_entry_xattr_next(entry, &name, &value, &size);
  343. url_encoded_name = url_encode(name);
  344. if (url_encoded_name != NULL) {
  345. /* Convert narrow-character to UTF-8. */
  346. r = archive_strcpy_l(&(pax->l_url_encoded_name),
  347. url_encoded_name, pax->sconv_utf8);
  348. free(url_encoded_name); /* Done with this. */
  349. if (r == 0)
  350. encoded_name = pax->l_url_encoded_name.s;
  351. else if (errno == ENOMEM) {
  352. archive_set_error(&a->archive, ENOMEM,
  353. "Can't allocate memory for Linkname");
  354. return (ARCHIVE_FATAL);
  355. }
  356. }
  357. archive_write_pax_header_xattr(pax, encoded_name,
  358. value, size);
  359. }
  360. return (ARCHIVE_OK);
  361. }
  362. static int
  363. get_entry_hardlink(struct archive_write *a, struct archive_entry *entry,
  364. const char **name, size_t *length, struct archive_string_conv *sc)
  365. {
  366. int r;
  367. r = archive_entry_hardlink_l(entry, name, length, sc);
  368. if (r != 0) {
  369. if (errno == ENOMEM) {
  370. archive_set_error(&a->archive, ENOMEM,
  371. "Can't allocate memory for Linkname");
  372. return (ARCHIVE_FATAL);
  373. }
  374. return (ARCHIVE_WARN);
  375. }
  376. return (ARCHIVE_OK);
  377. }
  378. static int
  379. get_entry_pathname(struct archive_write *a, struct archive_entry *entry,
  380. const char **name, size_t *length, struct archive_string_conv *sc)
  381. {
  382. int r;
  383. r = archive_entry_pathname_l(entry, name, length, sc);
  384. if (r != 0) {
  385. if (errno == ENOMEM) {
  386. archive_set_error(&a->archive, ENOMEM,
  387. "Can't allocate memory for Pathname");
  388. return (ARCHIVE_FATAL);
  389. }
  390. return (ARCHIVE_WARN);
  391. }
  392. return (ARCHIVE_OK);
  393. }
  394. static int
  395. get_entry_uname(struct archive_write *a, struct archive_entry *entry,
  396. const char **name, size_t *length, struct archive_string_conv *sc)
  397. {
  398. int r;
  399. r = archive_entry_uname_l(entry, name, length, sc);
  400. if (r != 0) {
  401. if (errno == ENOMEM) {
  402. archive_set_error(&a->archive, ENOMEM,
  403. "Can't allocate memory for Uname");
  404. return (ARCHIVE_FATAL);
  405. }
  406. return (ARCHIVE_WARN);
  407. }
  408. return (ARCHIVE_OK);
  409. }
  410. static int
  411. get_entry_gname(struct archive_write *a, struct archive_entry *entry,
  412. const char **name, size_t *length, struct archive_string_conv *sc)
  413. {
  414. int r;
  415. r = archive_entry_gname_l(entry, name, length, sc);
  416. if (r != 0) {
  417. if (errno == ENOMEM) {
  418. archive_set_error(&a->archive, ENOMEM,
  419. "Can't allocate memory for Gname");
  420. return (ARCHIVE_FATAL);
  421. }
  422. return (ARCHIVE_WARN);
  423. }
  424. return (ARCHIVE_OK);
  425. }
  426. static int
  427. get_entry_symlink(struct archive_write *a, struct archive_entry *entry,
  428. const char **name, size_t *length, struct archive_string_conv *sc)
  429. {
  430. int r;
  431. r = archive_entry_symlink_l(entry, name, length, sc);
  432. if (r != 0) {
  433. if (errno == ENOMEM) {
  434. archive_set_error(&a->archive, ENOMEM,
  435. "Can't allocate memory for Linkname");
  436. return (ARCHIVE_FATAL);
  437. }
  438. return (ARCHIVE_WARN);
  439. }
  440. return (ARCHIVE_OK);
  441. }
  442. /* Add ACL to pax header */
  443. static int
  444. add_pax_acl(struct archive_write *a,
  445. struct archive_entry *entry, struct pax *pax, int flags)
  446. {
  447. char *p;
  448. const char *attr;
  449. int acl_types;
  450. acl_types = archive_entry_acl_types(entry);
  451. if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0)
  452. attr = "SCHILY.acl.ace";
  453. else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
  454. attr = "SCHILY.acl.access";
  455. else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0)
  456. attr = "SCHILY.acl.default";
  457. else
  458. return (ARCHIVE_FATAL);
  459. p = archive_entry_acl_to_text_l(entry, NULL, flags, pax->sconv_utf8);
  460. if (p == NULL) {
  461. if (errno == ENOMEM) {
  462. archive_set_error(&a->archive, ENOMEM, "%s %s",
  463. "Can't allocate memory for ", attr);
  464. return (ARCHIVE_FATAL);
  465. }
  466. archive_set_error(&a->archive,
  467. ARCHIVE_ERRNO_FILE_FORMAT, "%s %s %s",
  468. "Can't translate ", attr, " to UTF-8");
  469. return(ARCHIVE_WARN);
  470. } else if (*p != '\0') {
  471. add_pax_attr(&(pax->pax_header),
  472. attr, p);
  473. free(p);
  474. }
  475. return(ARCHIVE_OK);
  476. }
  477. /*
  478. * TODO: Consider adding 'comment' and 'charset' fields to
  479. * archive_entry so that clients can specify them. Also, consider
  480. * adding generic key/value tags so clients can add arbitrary
  481. * key/value data.
  482. *
  483. * TODO: Break up this 700-line function!!!! Yowza!
  484. */
  485. static int
  486. archive_write_pax_header(struct archive_write *a,
  487. struct archive_entry *entry_original)
  488. {
  489. struct archive_entry *entry_main;
  490. const char *p;
  491. const char *suffix;
  492. int need_extension, r, ret;
  493. int acl_types;
  494. int sparse_count;
  495. uint64_t sparse_total, real_size;
  496. struct pax *pax;
  497. const char *hardlink;
  498. const char *path = NULL, *linkpath = NULL;
  499. const char *uname = NULL, *gname = NULL;
  500. const void *mac_metadata;
  501. size_t mac_metadata_size;
  502. struct archive_string_conv *sconv;
  503. size_t hardlink_length, path_length, linkpath_length;
  504. size_t uname_length, gname_length;
  505. char paxbuff[512];
  506. char ustarbuff[512];
  507. char ustar_entry_name[256];
  508. char pax_entry_name[256];
  509. char gnu_sparse_name[256];
  510. struct archive_string entry_name;
  511. ret = ARCHIVE_OK;
  512. need_extension = 0;
  513. pax = (struct pax *)a->format_data;
  514. /* Sanity check. */
  515. if (archive_entry_pathname(entry_original) == NULL) {
  516. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  517. "Can't record entry in tar file without pathname");
  518. return (ARCHIVE_FAILED);
  519. }
  520. /*
  521. * Choose a header encoding.
  522. */
  523. if (pax->opt_binary)
  524. sconv = NULL;/* Binary mode. */
  525. else {
  526. /* Header encoding is UTF-8. */
  527. if (pax->sconv_utf8 == NULL) {
  528. /* Initialize the string conversion object
  529. * we must need */
  530. pax->sconv_utf8 = archive_string_conversion_to_charset(
  531. &(a->archive), "UTF-8", 1);
  532. if (pax->sconv_utf8 == NULL)
  533. /* Couldn't allocate memory */
  534. return (ARCHIVE_FAILED);
  535. }
  536. sconv = pax->sconv_utf8;
  537. }
  538. r = get_entry_hardlink(a, entry_original, &hardlink,
  539. &hardlink_length, sconv);
  540. if (r == ARCHIVE_FATAL)
  541. return (r);
  542. else if (r != ARCHIVE_OK) {
  543. r = get_entry_hardlink(a, entry_original, &hardlink,
  544. &hardlink_length, NULL);
  545. if (r == ARCHIVE_FATAL)
  546. return (r);
  547. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  548. "Can't translate linkname '%s' to %s", hardlink,
  549. archive_string_conversion_charset_name(sconv));
  550. ret = ARCHIVE_WARN;
  551. sconv = NULL;/* The header charset switches to binary mode. */
  552. }
  553. /* Make sure this is a type of entry that we can handle here */
  554. if (hardlink == NULL) {
  555. switch (archive_entry_filetype(entry_original)) {
  556. case AE_IFBLK:
  557. case AE_IFCHR:
  558. case AE_IFIFO:
  559. case AE_IFLNK:
  560. case AE_IFREG:
  561. break;
  562. case AE_IFDIR:
  563. {
  564. /*
  565. * Ensure a trailing '/'. Modify the original
  566. * entry so the client sees the change.
  567. */
  568. #if defined(_WIN32) && !defined(__CYGWIN__)
  569. const wchar_t *wp;
  570. wp = archive_entry_pathname_w(entry_original);
  571. if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
  572. struct archive_wstring ws;
  573. archive_string_init(&ws);
  574. path_length = wcslen(wp);
  575. if (archive_wstring_ensure(&ws,
  576. path_length + 2) == NULL) {
  577. archive_set_error(&a->archive, ENOMEM,
  578. "Can't allocate pax data");
  579. archive_wstring_free(&ws);
  580. return(ARCHIVE_FATAL);
  581. }
  582. /* Should we keep '\' ? */
  583. if (wp[path_length -1] == L'\\')
  584. path_length--;
  585. archive_wstrncpy(&ws, wp, path_length);
  586. archive_wstrappend_wchar(&ws, L'/');
  587. archive_entry_copy_pathname_w(
  588. entry_original, ws.s);
  589. archive_wstring_free(&ws);
  590. p = NULL;
  591. } else
  592. #endif
  593. p = archive_entry_pathname(entry_original);
  594. /*
  595. * On Windows, this is a backup operation just in
  596. * case getting WCS failed. On POSIX, this is a
  597. * normal operation.
  598. */
  599. if (p != NULL && p[strlen(p) - 1] != '/') {
  600. struct archive_string as;
  601. archive_string_init(&as);
  602. path_length = strlen(p);
  603. if (archive_string_ensure(&as,
  604. path_length + 2) == NULL) {
  605. archive_set_error(&a->archive, ENOMEM,
  606. "Can't allocate pax data");
  607. archive_string_free(&as);
  608. return(ARCHIVE_FATAL);
  609. }
  610. #if defined(_WIN32) && !defined(__CYGWIN__)
  611. /* NOTE: This might break the pathname
  612. * if the current code page is CP932 and
  613. * the pathname includes a character '\'
  614. * as a part of its multibyte pathname. */
  615. if (p[strlen(p) -1] == '\\')
  616. path_length--;
  617. else
  618. #endif
  619. archive_strncpy(&as, p, path_length);
  620. archive_strappend_char(&as, '/');
  621. archive_entry_copy_pathname(
  622. entry_original, as.s);
  623. archive_string_free(&as);
  624. }
  625. break;
  626. }
  627. case AE_IFSOCK:
  628. archive_set_error(&a->archive,
  629. ARCHIVE_ERRNO_FILE_FORMAT,
  630. "tar format cannot archive socket");
  631. return (ARCHIVE_FAILED);
  632. default:
  633. archive_set_error(&a->archive,
  634. ARCHIVE_ERRNO_FILE_FORMAT,
  635. "tar format cannot archive this (type=0%lo)",
  636. (unsigned long)
  637. archive_entry_filetype(entry_original));
  638. return (ARCHIVE_FAILED);
  639. }
  640. }
  641. /*
  642. * If Mac OS metadata blob is here, recurse to write that
  643. * as a separate entry. This is really a pretty poor design:
  644. * In particular, it doubles the overhead for long filenames.
  645. * TODO: Help Apple folks design something better and figure
  646. * out how to transition from this legacy format.
  647. *
  648. * Note that this code is present on every platform; clients
  649. * on non-Mac are unlikely to ever provide this data, but
  650. * applications that copy entries from one archive to another
  651. * should not lose data just because the local filesystem
  652. * can't store it.
  653. */
  654. mac_metadata =
  655. archive_entry_mac_metadata(entry_original, &mac_metadata_size);
  656. if (mac_metadata != NULL) {
  657. const char *oname;
  658. char *name, *bname;
  659. size_t name_length;
  660. struct archive_entry *extra = archive_entry_new2(&a->archive);
  661. oname = archive_entry_pathname(entry_original);
  662. name_length = strlen(oname);
  663. name = malloc(name_length + 3);
  664. if (name == NULL || extra == NULL) {
  665. /* XXX error message */
  666. archive_entry_free(extra);
  667. free(name);
  668. return (ARCHIVE_FAILED);
  669. }
  670. strcpy(name, oname);
  671. /* Find last '/'; strip trailing '/' characters */
  672. bname = strrchr(name, '/');
  673. while (bname != NULL && bname[1] == '\0') {
  674. *bname = '\0';
  675. bname = strrchr(name, '/');
  676. }
  677. if (bname == NULL) {
  678. memmove(name + 2, name, name_length + 1);
  679. memmove(name, "._", 2);
  680. } else {
  681. bname += 1;
  682. memmove(bname + 2, bname, strlen(bname) + 1);
  683. memmove(bname, "._", 2);
  684. }
  685. archive_entry_copy_pathname(extra, name);
  686. free(name);
  687. archive_entry_set_size(extra, mac_metadata_size);
  688. archive_entry_set_filetype(extra, AE_IFREG);
  689. archive_entry_set_perm(extra,
  690. archive_entry_perm(entry_original));
  691. archive_entry_set_mtime(extra,
  692. archive_entry_mtime(entry_original),
  693. archive_entry_mtime_nsec(entry_original));
  694. archive_entry_set_gid(extra,
  695. archive_entry_gid(entry_original));
  696. archive_entry_set_gname(extra,
  697. archive_entry_gname(entry_original));
  698. archive_entry_set_uid(extra,
  699. archive_entry_uid(entry_original));
  700. archive_entry_set_uname(extra,
  701. archive_entry_uname(entry_original));
  702. /* Recurse to write the special copyfile entry. */
  703. r = archive_write_pax_header(a, extra);
  704. archive_entry_free(extra);
  705. if (r < ARCHIVE_WARN)
  706. return (r);
  707. if (r < ret)
  708. ret = r;
  709. r = (int)archive_write_pax_data(a, mac_metadata,
  710. mac_metadata_size);
  711. if (r < ARCHIVE_WARN)
  712. return (r);
  713. if (r < ret)
  714. ret = r;
  715. r = archive_write_pax_finish_entry(a);
  716. if (r < ARCHIVE_WARN)
  717. return (r);
  718. if (r < ret)
  719. ret = r;
  720. }
  721. /* Copy entry so we can modify it as needed. */
  722. #if defined(_WIN32) && !defined(__CYGWIN__)
  723. /* Make sure the path separators in pathname, hardlink and symlink
  724. * are all slash '/', not the Windows path separator '\'. */
  725. entry_main = __la_win_entry_in_posix_pathseparator(entry_original);
  726. if (entry_main == entry_original)
  727. entry_main = archive_entry_clone(entry_original);
  728. #else
  729. entry_main = archive_entry_clone(entry_original);
  730. #endif
  731. if (entry_main == NULL) {
  732. archive_set_error(&a->archive, ENOMEM,
  733. "Can't allocate pax data");
  734. return(ARCHIVE_FATAL);
  735. }
  736. archive_string_empty(&(pax->pax_header)); /* Blank our work area. */
  737. archive_string_empty(&(pax->sparse_map));
  738. sparse_total = 0;
  739. sparse_list_clear(pax);
  740. if (hardlink == NULL &&
  741. archive_entry_filetype(entry_main) == AE_IFREG)
  742. sparse_count = archive_entry_sparse_reset(entry_main);
  743. else
  744. sparse_count = 0;
  745. if (sparse_count) {
  746. int64_t offset, length, last_offset = 0;
  747. /* Get the last entry of sparse block. */
  748. while (archive_entry_sparse_next(
  749. entry_main, &offset, &length) == ARCHIVE_OK)
  750. last_offset = offset + length;
  751. /* If the last sparse block does not reach the end of file,
  752. * We have to add a empty sparse block as the last entry to
  753. * manage storing file data. */
  754. if (last_offset < archive_entry_size(entry_main))
  755. archive_entry_sparse_add_entry(entry_main,
  756. archive_entry_size(entry_main), 0);
  757. sparse_count = archive_entry_sparse_reset(entry_main);
  758. }
  759. /*
  760. * First, check the name fields and see if any of them
  761. * require binary coding. If any of them does, then all of
  762. * them do.
  763. */
  764. r = get_entry_pathname(a, entry_main, &path, &path_length, sconv);
  765. if (r == ARCHIVE_FATAL)
  766. return (r);
  767. else if (r != ARCHIVE_OK) {
  768. r = get_entry_pathname(a, entry_main, &path,
  769. &path_length, NULL);
  770. if (r == ARCHIVE_FATAL)
  771. return (r);
  772. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  773. "Can't translate pathname '%s' to %s", path,
  774. archive_string_conversion_charset_name(sconv));
  775. ret = ARCHIVE_WARN;
  776. sconv = NULL;/* The header charset switches to binary mode. */
  777. }
  778. r = get_entry_uname(a, entry_main, &uname, &uname_length, sconv);
  779. if (r == ARCHIVE_FATAL)
  780. return (r);
  781. else if (r != ARCHIVE_OK) {
  782. r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
  783. if (r == ARCHIVE_FATAL)
  784. return (r);
  785. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  786. "Can't translate uname '%s' to %s", uname,
  787. archive_string_conversion_charset_name(sconv));
  788. ret = ARCHIVE_WARN;
  789. sconv = NULL;/* The header charset switches to binary mode. */
  790. }
  791. r = get_entry_gname(a, entry_main, &gname, &gname_length, sconv);
  792. if (r == ARCHIVE_FATAL)
  793. return (r);
  794. else if (r != ARCHIVE_OK) {
  795. r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
  796. if (r == ARCHIVE_FATAL)
  797. return (r);
  798. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  799. "Can't translate gname '%s' to %s", gname,
  800. archive_string_conversion_charset_name(sconv));
  801. ret = ARCHIVE_WARN;
  802. sconv = NULL;/* The header charset switches to binary mode. */
  803. }
  804. linkpath = hardlink;
  805. linkpath_length = hardlink_length;
  806. if (linkpath == NULL) {
  807. r = get_entry_symlink(a, entry_main, &linkpath,
  808. &linkpath_length, sconv);
  809. if (r == ARCHIVE_FATAL)
  810. return (r);
  811. else if (r != ARCHIVE_OK) {
  812. r = get_entry_symlink(a, entry_main, &linkpath,
  813. &linkpath_length, NULL);
  814. if (r == ARCHIVE_FATAL)
  815. return (r);
  816. archive_set_error(&a->archive,
  817. ARCHIVE_ERRNO_FILE_FORMAT,
  818. "Can't translate linkname '%s' to %s", linkpath,
  819. archive_string_conversion_charset_name(sconv));
  820. ret = ARCHIVE_WARN;
  821. sconv = NULL;
  822. }
  823. }
  824. /* If any string conversions failed, get all attributes
  825. * in binary-mode. */
  826. if (sconv == NULL && !pax->opt_binary) {
  827. if (hardlink != NULL) {
  828. r = get_entry_hardlink(a, entry_main, &hardlink,
  829. &hardlink_length, NULL);
  830. if (r == ARCHIVE_FATAL)
  831. return (r);
  832. linkpath = hardlink;
  833. linkpath_length = hardlink_length;
  834. }
  835. r = get_entry_pathname(a, entry_main, &path,
  836. &path_length, NULL);
  837. if (r == ARCHIVE_FATAL)
  838. return (r);
  839. r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
  840. if (r == ARCHIVE_FATAL)
  841. return (r);
  842. r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
  843. if (r == ARCHIVE_FATAL)
  844. return (r);
  845. }
  846. /* Store the header encoding first, to be nice to readers. */
  847. if (sconv == NULL)
  848. add_pax_attr(&(pax->pax_header), "hdrcharset", "BINARY");
  849. /*
  850. * If name is too long, or has non-ASCII characters, add
  851. * 'path' to pax extended attrs. (Note that an unconvertible
  852. * name must have non-ASCII characters.)
  853. */
  854. if (has_non_ASCII(path)) {
  855. /* We have non-ASCII characters. */
  856. add_pax_attr(&(pax->pax_header), "path", path);
  857. archive_entry_set_pathname(entry_main,
  858. build_ustar_entry_name(ustar_entry_name,
  859. path, path_length, NULL));
  860. need_extension = 1;
  861. } else {
  862. /* We have an all-ASCII path; we'd like to just store
  863. * it in the ustar header if it will fit. Yes, this
  864. * duplicates some of the logic in
  865. * archive_write_set_format_ustar.c
  866. */
  867. if (path_length <= 100) {
  868. /* Fits in the old 100-char tar name field. */
  869. } else {
  870. /* Find largest suffix that will fit. */
  871. /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */
  872. suffix = strchr(path + path_length - 100 - 1, '/');
  873. /* Don't attempt an empty prefix. */
  874. if (suffix == path)
  875. suffix = strchr(suffix + 1, '/');
  876. /* We can put it in the ustar header if it's
  877. * all ASCII and it's either <= 100 characters
  878. * or can be split at a '/' into a prefix <=
  879. * 155 chars and a suffix <= 100 chars. (Note
  880. * the strchr() above will return NULL exactly
  881. * when the path can't be split.)
  882. */
  883. if (suffix == NULL /* Suffix > 100 chars. */
  884. || suffix[1] == '\0' /* empty suffix */
  885. || suffix - path > 155) /* Prefix > 155 chars */
  886. {
  887. add_pax_attr(&(pax->pax_header), "path", path);
  888. archive_entry_set_pathname(entry_main,
  889. build_ustar_entry_name(ustar_entry_name,
  890. path, path_length, NULL));
  891. need_extension = 1;
  892. }
  893. }
  894. }
  895. if (linkpath != NULL) {
  896. /* If link name is too long or has non-ASCII characters, add
  897. * 'linkpath' to pax extended attrs. */
  898. if (linkpath_length > 100 || has_non_ASCII(linkpath)) {
  899. add_pax_attr(&(pax->pax_header), "linkpath", linkpath);
  900. if (linkpath_length > 100) {
  901. if (hardlink != NULL)
  902. archive_entry_set_hardlink(entry_main,
  903. "././@LongHardLink");
  904. else
  905. archive_entry_set_symlink(entry_main,
  906. "././@LongSymLink");
  907. }
  908. need_extension = 1;
  909. }
  910. }
  911. /* Save a pathname since it will be renamed if `entry_main` has
  912. * sparse blocks. */
  913. archive_string_init(&entry_name);
  914. archive_strcpy(&entry_name, archive_entry_pathname(entry_main));
  915. /* If file size is too large, add 'size' to pax extended attrs. */
  916. if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
  917. add_pax_attr_int(&(pax->pax_header), "size",
  918. archive_entry_size(entry_main));
  919. need_extension = 1;
  920. }
  921. /* If numeric GID is too large, add 'gid' to pax extended attrs. */
  922. if ((unsigned int)archive_entry_gid(entry_main) >= (1 << 18)) {
  923. add_pax_attr_int(&(pax->pax_header), "gid",
  924. archive_entry_gid(entry_main));
  925. need_extension = 1;
  926. }
  927. /* If group name is too large or has non-ASCII characters, add
  928. * 'gname' to pax extended attrs. */
  929. if (gname != NULL) {
  930. if (gname_length > 31 || has_non_ASCII(gname)) {
  931. add_pax_attr(&(pax->pax_header), "gname", gname);
  932. need_extension = 1;
  933. }
  934. }
  935. /* If numeric UID is too large, add 'uid' to pax extended attrs. */
  936. if ((unsigned int)archive_entry_uid(entry_main) >= (1 << 18)) {
  937. add_pax_attr_int(&(pax->pax_header), "uid",
  938. archive_entry_uid(entry_main));
  939. need_extension = 1;
  940. }
  941. /* Add 'uname' to pax extended attrs if necessary. */
  942. if (uname != NULL) {
  943. if (uname_length > 31 || has_non_ASCII(uname)) {
  944. add_pax_attr(&(pax->pax_header), "uname", uname);
  945. need_extension = 1;
  946. }
  947. }
  948. /*
  949. * POSIX/SUSv3 doesn't provide a standard key for large device
  950. * numbers. I use the same keys here that Joerg Schilling
  951. * used for 'star.' (Which, somewhat confusingly, are called
  952. * "devXXX" even though they code "rdev" values.) No doubt,
  953. * other implementations use other keys. Note that there's no
  954. * reason we can't write the same information into a number of
  955. * different keys.
  956. *
  957. * Of course, this is only needed for block or char device entries.
  958. */
  959. if (archive_entry_filetype(entry_main) == AE_IFBLK
  960. || archive_entry_filetype(entry_main) == AE_IFCHR) {
  961. /*
  962. * If rdevmajor is too large, add 'SCHILY.devmajor' to
  963. * extended attributes.
  964. */
  965. int rdevmajor, rdevminor;
  966. rdevmajor = archive_entry_rdevmajor(entry_main);
  967. rdevminor = archive_entry_rdevminor(entry_main);
  968. if (rdevmajor >= (1 << 18)) {
  969. add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor",
  970. rdevmajor);
  971. /*
  972. * Non-strict formatting below means we don't
  973. * have to truncate here. Not truncating improves
  974. * the chance that some more modern tar archivers
  975. * (such as GNU tar 1.13) can restore the full
  976. * value even if they don't understand the pax
  977. * extended attributes. See my rant below about
  978. * file size fields for additional details.
  979. */
  980. /* archive_entry_set_rdevmajor(entry_main,
  981. rdevmajor & ((1 << 18) - 1)); */
  982. need_extension = 1;
  983. }
  984. /*
  985. * If devminor is too large, add 'SCHILY.devminor' to
  986. * extended attributes.
  987. */
  988. if (rdevminor >= (1 << 18)) {
  989. add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor",
  990. rdevminor);
  991. /* Truncation is not necessary here, either. */
  992. /* archive_entry_set_rdevminor(entry_main,
  993. rdevminor & ((1 << 18) - 1)); */
  994. need_extension = 1;
  995. }
  996. }
  997. /*
  998. * Technically, the mtime field in the ustar header can
  999. * support 33 bits, but many platforms use signed 32-bit time
  1000. * values. The cutoff of 0x7fffffff here is a compromise.
  1001. * Yes, this check is duplicated just below; this helps to
  1002. * avoid writing an mtime attribute just to handle a
  1003. * high-resolution timestamp in "restricted pax" mode.
  1004. */
  1005. if (!need_extension &&
  1006. ((archive_entry_mtime(entry_main) < 0)
  1007. || (archive_entry_mtime(entry_main) >= 0x7fffffff)))
  1008. need_extension = 1;
  1009. /* I use a star-compatible file flag attribute. */
  1010. p = archive_entry_fflags_text(entry_main);
  1011. if (!need_extension && p != NULL && *p != '\0')
  1012. need_extension = 1;
  1013. /* If there are extended attributes, we need an extension */
  1014. if (!need_extension && archive_entry_xattr_count(entry_original) > 0)
  1015. need_extension = 1;
  1016. /* If there are sparse info, we need an extension */
  1017. if (!need_extension && sparse_count > 0)
  1018. need_extension = 1;
  1019. acl_types = archive_entry_acl_types(entry_original);
  1020. /* If there are any ACL entries, we need an extension */
  1021. if (!need_extension && acl_types != 0)
  1022. need_extension = 1;
  1023. /*
  1024. * Libarchive used to include these in extended headers for
  1025. * restricted pax format, but that confused people who
  1026. * expected ustar-like time semantics. So now we only include
  1027. * them in full pax format.
  1028. */
  1029. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED) {
  1030. if (archive_entry_ctime(entry_main) != 0 ||
  1031. archive_entry_ctime_nsec(entry_main) != 0)
  1032. add_pax_attr_time(&(pax->pax_header), "ctime",
  1033. archive_entry_ctime(entry_main),
  1034. archive_entry_ctime_nsec(entry_main));
  1035. if (archive_entry_atime(entry_main) != 0 ||
  1036. archive_entry_atime_nsec(entry_main) != 0)
  1037. add_pax_attr_time(&(pax->pax_header), "atime",
  1038. archive_entry_atime(entry_main),
  1039. archive_entry_atime_nsec(entry_main));
  1040. /* Store birth/creationtime only if it's earlier than mtime */
  1041. if (archive_entry_birthtime_is_set(entry_main) &&
  1042. archive_entry_birthtime(entry_main)
  1043. < archive_entry_mtime(entry_main))
  1044. add_pax_attr_time(&(pax->pax_header),
  1045. "LIBARCHIVE.creationtime",
  1046. archive_entry_birthtime(entry_main),
  1047. archive_entry_birthtime_nsec(entry_main));
  1048. }
  1049. /*
  1050. * The following items are handled differently in "pax
  1051. * restricted" format. In particular, in "pax restricted"
  1052. * format they won't be added unless need_extension is
  1053. * already set (we're already generating an extended header, so
  1054. * may as well include these).
  1055. */
  1056. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED ||
  1057. need_extension) {
  1058. if (archive_entry_mtime(entry_main) < 0 ||
  1059. archive_entry_mtime(entry_main) >= 0x7fffffff ||
  1060. archive_entry_mtime_nsec(entry_main) != 0)
  1061. add_pax_attr_time(&(pax->pax_header), "mtime",
  1062. archive_entry_mtime(entry_main),
  1063. archive_entry_mtime_nsec(entry_main));
  1064. /* I use a star-compatible file flag attribute. */
  1065. p = archive_entry_fflags_text(entry_main);
  1066. if (p != NULL && *p != '\0')
  1067. add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p);
  1068. /* I use star-compatible ACL attributes. */
  1069. if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) {
  1070. ret = add_pax_acl(a, entry_original, pax,
  1071. ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
  1072. ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA |
  1073. ARCHIVE_ENTRY_ACL_STYLE_COMPACT);
  1074. if (ret == ARCHIVE_FATAL)
  1075. return (ARCHIVE_FATAL);
  1076. }
  1077. if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
  1078. ret = add_pax_acl(a, entry_original, pax,
  1079. ARCHIVE_ENTRY_ACL_TYPE_ACCESS |
  1080. ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
  1081. ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA);
  1082. if (ret == ARCHIVE_FATAL)
  1083. return (ARCHIVE_FATAL);
  1084. }
  1085. if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) {
  1086. ret = add_pax_acl(a, entry_original, pax,
  1087. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT |
  1088. ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID |
  1089. ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA);
  1090. if (ret == ARCHIVE_FATAL)
  1091. return (ARCHIVE_FATAL);
  1092. }
  1093. /* We use GNU-tar-compatible sparse attributes. */
  1094. if (sparse_count > 0) {
  1095. int64_t soffset, slength;
  1096. add_pax_attr_int(&(pax->pax_header),
  1097. "GNU.sparse.major", 1);
  1098. add_pax_attr_int(&(pax->pax_header),
  1099. "GNU.sparse.minor", 0);
  1100. /*
  1101. * Make sure to store the original path, since
  1102. * truncation to ustar limit happened already.
  1103. */
  1104. add_pax_attr(&(pax->pax_header),
  1105. "GNU.sparse.name", path);
  1106. add_pax_attr_int(&(pax->pax_header),
  1107. "GNU.sparse.realsize",
  1108. archive_entry_size(entry_main));
  1109. /* Rename the file name which will be used for
  1110. * ustar header to a special name, which GNU
  1111. * PAX Format 1.0 requires */
  1112. archive_entry_set_pathname(entry_main,
  1113. build_gnu_sparse_name(gnu_sparse_name,
  1114. entry_name.s));
  1115. /*
  1116. * - Make a sparse map, which will precede a file data.
  1117. * - Get the total size of available data of sparse.
  1118. */
  1119. archive_string_sprintf(&(pax->sparse_map), "%d\n",
  1120. sparse_count);
  1121. while (archive_entry_sparse_next(entry_main,
  1122. &soffset, &slength) == ARCHIVE_OK) {
  1123. archive_string_sprintf(&(pax->sparse_map),
  1124. "%jd\n%jd\n",
  1125. (intmax_t)soffset,
  1126. (intmax_t)slength);
  1127. sparse_total += slength;
  1128. if (sparse_list_add(pax, soffset, slength)
  1129. != ARCHIVE_OK) {
  1130. archive_set_error(&a->archive,
  1131. ENOMEM,
  1132. "Can't allocate memory");
  1133. archive_entry_free(entry_main);
  1134. archive_string_free(&entry_name);
  1135. return (ARCHIVE_FATAL);
  1136. }
  1137. }
  1138. }
  1139. /* Store extended attributes */
  1140. if (archive_write_pax_header_xattrs(a, pax, entry_original)
  1141. == ARCHIVE_FATAL) {
  1142. archive_entry_free(entry_main);
  1143. archive_string_free(&entry_name);
  1144. return (ARCHIVE_FATAL);
  1145. }
  1146. }
  1147. /* Only regular files have data. */
  1148. if (archive_entry_filetype(entry_main) != AE_IFREG)
  1149. archive_entry_set_size(entry_main, 0);
  1150. /*
  1151. * Pax-restricted does not store data for hardlinks, in order
  1152. * to improve compatibility with ustar.
  1153. */
  1154. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
  1155. hardlink != NULL)
  1156. archive_entry_set_size(entry_main, 0);
  1157. /*
  1158. * XXX Full pax interchange format does permit a hardlink
  1159. * entry to have data associated with it. I'm not supporting
  1160. * that here because the client expects me to tell them whether
  1161. * or not this format expects data for hardlinks. If I
  1162. * don't check here, then every pax archive will end up with
  1163. * duplicated data for hardlinks. Someday, there may be
  1164. * need to select this behavior, in which case the following
  1165. * will need to be revisited. XXX
  1166. */
  1167. if (hardlink != NULL)
  1168. archive_entry_set_size(entry_main, 0);
  1169. /* Save a real file size. */
  1170. real_size = archive_entry_size(entry_main);
  1171. /*
  1172. * Overwrite a file size by the total size of sparse blocks and
  1173. * the size of sparse map info. That file size is the length of
  1174. * the data, which we will exactly store into an archive file.
  1175. */
  1176. if (archive_strlen(&(pax->sparse_map))) {
  1177. size_t mapsize = archive_strlen(&(pax->sparse_map));
  1178. pax->sparse_map_padding = 0x1ff & (-(ssize_t)mapsize);
  1179. archive_entry_set_size(entry_main,
  1180. mapsize + pax->sparse_map_padding + sparse_total);
  1181. }
  1182. /* Format 'ustar' header for main entry.
  1183. *
  1184. * The trouble with file size: If the reader can't understand
  1185. * the file size, they may not be able to locate the next
  1186. * entry and the rest of the archive is toast. Pax-compliant
  1187. * readers are supposed to ignore the file size in the main
  1188. * header, so the question becomes how to maximize portability
  1189. * for readers that don't support pax attribute extensions.
  1190. * For maximum compatibility, I permit numeric extensions in
  1191. * the main header so that the file size stored will always be
  1192. * correct, even if it's in a format that only some
  1193. * implementations understand. The technique used here is:
  1194. *
  1195. * a) If possible, follow the standard exactly. This handles
  1196. * files up to 8 gigabytes minus 1.
  1197. *
  1198. * b) If that fails, try octal but omit the field terminator.
  1199. * That handles files up to 64 gigabytes minus 1.
  1200. *
  1201. * c) Otherwise, use base-256 extensions. That handles files
  1202. * up to 2^63 in this implementation, with the potential to
  1203. * go up to 2^94. That should hold us for a while. ;-)
  1204. *
  1205. * The non-strict formatter uses similar logic for other
  1206. * numeric fields, though they're less critical.
  1207. */
  1208. if (__archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0,
  1209. NULL) == ARCHIVE_FATAL)
  1210. return (ARCHIVE_FATAL);
  1211. /* If we built any extended attributes, write that entry first. */
  1212. if (archive_strlen(&(pax->pax_header)) > 0) {
  1213. struct archive_entry *pax_attr_entry;
  1214. time_t s;
  1215. int64_t uid, gid;
  1216. int mode;
  1217. pax_attr_entry = archive_entry_new2(&a->archive);
  1218. p = entry_name.s;
  1219. archive_entry_set_pathname(pax_attr_entry,
  1220. build_pax_attribute_name(pax_entry_name, p));
  1221. archive_entry_set_size(pax_attr_entry,
  1222. archive_strlen(&(pax->pax_header)));
  1223. /* Copy uid/gid (but clip to ustar limits). */
  1224. uid = archive_entry_uid(entry_main);
  1225. if (uid >= 1 << 18)
  1226. uid = (1 << 18) - 1;
  1227. archive_entry_set_uid(pax_attr_entry, uid);
  1228. gid = archive_entry_gid(entry_main);
  1229. if (gid >= 1 << 18)
  1230. gid = (1 << 18) - 1;
  1231. archive_entry_set_gid(pax_attr_entry, gid);
  1232. /* Copy mode over (but not setuid/setgid bits) */
  1233. mode = archive_entry_mode(entry_main);
  1234. #ifdef S_ISUID
  1235. mode &= ~S_ISUID;
  1236. #endif
  1237. #ifdef S_ISGID
  1238. mode &= ~S_ISGID;
  1239. #endif
  1240. #ifdef S_ISVTX
  1241. mode &= ~S_ISVTX;
  1242. #endif
  1243. archive_entry_set_mode(pax_attr_entry, mode);
  1244. /* Copy uname/gname. */
  1245. archive_entry_set_uname(pax_attr_entry,
  1246. archive_entry_uname(entry_main));
  1247. archive_entry_set_gname(pax_attr_entry,
  1248. archive_entry_gname(entry_main));
  1249. /* Copy mtime, but clip to ustar limits. */
  1250. s = archive_entry_mtime(entry_main);
  1251. if (s < 0) { s = 0; }
  1252. if (s >= 0x7fffffff) { s = 0x7fffffff; }
  1253. archive_entry_set_mtime(pax_attr_entry, s, 0);
  1254. /* Standard ustar doesn't support atime. */
  1255. archive_entry_set_atime(pax_attr_entry, 0, 0);
  1256. /* Standard ustar doesn't support ctime. */
  1257. archive_entry_set_ctime(pax_attr_entry, 0, 0);
  1258. r = __archive_write_format_header_ustar(a, paxbuff,
  1259. pax_attr_entry, 'x', 1, NULL);
  1260. archive_entry_free(pax_attr_entry);
  1261. /* Note that the 'x' header shouldn't ever fail to format */
  1262. if (r < ARCHIVE_WARN) {
  1263. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1264. "archive_write_pax_header: "
  1265. "'x' header failed?! This can't happen.\n");
  1266. return (ARCHIVE_FATAL);
  1267. } else if (r < ret)
  1268. ret = r;
  1269. r = __archive_write_output(a, paxbuff, 512);
  1270. if (r != ARCHIVE_OK) {
  1271. sparse_list_clear(pax);
  1272. pax->entry_bytes_remaining = 0;
  1273. pax->entry_padding = 0;
  1274. return (ARCHIVE_FATAL);
  1275. }
  1276. pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header));
  1277. pax->entry_padding =
  1278. 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
  1279. r = __archive_write_output(a, pax->pax_header.s,
  1280. archive_strlen(&(pax->pax_header)));
  1281. if (r != ARCHIVE_OK) {
  1282. /* If a write fails, we're pretty much toast. */
  1283. return (ARCHIVE_FATAL);
  1284. }
  1285. /* Pad out the end of the entry. */
  1286. r = __archive_write_nulls(a, (size_t)pax->entry_padding);
  1287. if (r != ARCHIVE_OK) {
  1288. /* If a write fails, we're pretty much toast. */
  1289. return (ARCHIVE_FATAL);
  1290. }
  1291. pax->entry_bytes_remaining = pax->entry_padding = 0;
  1292. }
  1293. /* Write the header for main entry. */
  1294. r = __archive_write_output(a, ustarbuff, 512);
  1295. if (r != ARCHIVE_OK)
  1296. return (r);
  1297. /*
  1298. * Inform the client of the on-disk size we're using, so
  1299. * they can avoid unnecessarily writing a body for something
  1300. * that we're just going to ignore.
  1301. */
  1302. archive_entry_set_size(entry_original, real_size);
  1303. if (pax->sparse_list == NULL && real_size > 0) {
  1304. /* This is not a sparse file but we handle its data as
  1305. * a sparse block. */
  1306. sparse_list_add(pax, 0, real_size);
  1307. sparse_total = real_size;
  1308. }
  1309. pax->entry_padding = 0x1ff & (-(int64_t)sparse_total);
  1310. archive_entry_free(entry_main);
  1311. archive_string_free(&entry_name);
  1312. return (ret);
  1313. }
  1314. /*
  1315. * We need a valid name for the regular 'ustar' entry. This routine
  1316. * tries to hack something more-or-less reasonable.
  1317. *
  1318. * The approach here tries to preserve leading dir names. We do so by
  1319. * working with four sections:
  1320. * 1) "prefix" directory names,
  1321. * 2) "suffix" directory names,
  1322. * 3) inserted dir name (optional),
  1323. * 4) filename.
  1324. *
  1325. * These sections must satisfy the following requirements:
  1326. * * Parts 1 & 2 together form an initial portion of the dir name.
  1327. * * Part 3 is specified by the caller. (It should not contain a leading
  1328. * or trailing '/'.)
  1329. * * Part 4 forms an initial portion of the base filename.
  1330. * * The filename must be <= 99 chars to fit the ustar 'name' field.
  1331. * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
  1332. * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
  1333. * * If the original name ends in a '/', the new name must also end in a '/'
  1334. * * Trailing '/.' sequences may be stripped.
  1335. *
  1336. * Note: Recall that the ustar format does not store the '/' separating
  1337. * parts 1 & 2, but does store the '/' separating parts 2 & 3.
  1338. */
  1339. static char *
  1340. build_ustar_entry_name(char *dest, const char *src, size_t src_length,
  1341. const char *insert)
  1342. {
  1343. const char *prefix, *prefix_end;
  1344. const char *suffix, *suffix_end;
  1345. const char *filename, *filename_end;
  1346. char *p;
  1347. int need_slash = 0; /* Was there a trailing slash? */
  1348. size_t suffix_length = 99;
  1349. size_t insert_length;
  1350. /* Length of additional dir element to be added. */
  1351. if (insert == NULL)
  1352. insert_length = 0;
  1353. else
  1354. /* +2 here allows for '/' before and after the insert. */
  1355. insert_length = strlen(insert) + 2;
  1356. /* Step 0: Quick bailout in a common case. */
  1357. if (src_length < 100 && insert == NULL) {
  1358. strncpy(dest, src, src_length);
  1359. dest[src_length] = '\0';
  1360. return (dest);
  1361. }
  1362. /* Step 1: Locate filename and enforce the length restriction. */
  1363. filename_end = src + src_length;
  1364. /* Remove trailing '/' chars and '/.' pairs. */
  1365. for (;;) {
  1366. if (filename_end > src && filename_end[-1] == '/') {
  1367. filename_end --;
  1368. need_slash = 1; /* Remember to restore trailing '/'. */
  1369. continue;
  1370. }
  1371. if (filename_end > src + 1 && filename_end[-1] == '.'
  1372. && filename_end[-2] == '/') {
  1373. filename_end -= 2;
  1374. need_slash = 1; /* "foo/." will become "foo/" */
  1375. continue;
  1376. }
  1377. break;
  1378. }
  1379. if (need_slash)
  1380. suffix_length--;
  1381. /* Find start of filename. */
  1382. filename = filename_end - 1;
  1383. while ((filename > src) && (*filename != '/'))
  1384. filename --;
  1385. if ((*filename == '/') && (filename < filename_end - 1))
  1386. filename ++;
  1387. /* Adjust filename_end so that filename + insert fits in 99 chars. */
  1388. suffix_length -= insert_length;
  1389. if (filename_end > filename + suffix_length)
  1390. filename_end = filename + suffix_length;
  1391. /* Calculate max size for "suffix" section (#3 above). */
  1392. suffix_length -= filename_end - filename;
  1393. /* Step 2: Locate the "prefix" section of the dirname, including
  1394. * trailing '/'. */
  1395. prefix = src;
  1396. prefix_end = prefix + 155;
  1397. if (prefix_end > filename)
  1398. prefix_end = filename;
  1399. while (prefix_end > prefix && *prefix_end != '/')
  1400. prefix_end--;
  1401. if ((prefix_end < filename) && (*prefix_end == '/'))
  1402. prefix_end++;
  1403. /* Step 3: Locate the "suffix" section of the dirname,
  1404. * including trailing '/'. */
  1405. suffix = prefix_end;
  1406. suffix_end = suffix + suffix_length; /* Enforce limit. */
  1407. if (suffix_end > filename)
  1408. suffix_end = filename;
  1409. if (suffix_end < suffix)
  1410. suffix_end = suffix;
  1411. while (suffix_end > suffix && *suffix_end != '/')
  1412. suffix_end--;
  1413. if ((suffix_end < filename) && (*suffix_end == '/'))
  1414. suffix_end++;
  1415. /* Step 4: Build the new name. */
  1416. /* The OpenBSD strlcpy function is safer, but less portable. */
  1417. /* Rather than maintain two versions, just use the strncpy version. */
  1418. p = dest;
  1419. if (prefix_end > prefix) {
  1420. strncpy(p, prefix, prefix_end - prefix);
  1421. p += prefix_end - prefix;
  1422. }
  1423. if (suffix_end > suffix) {
  1424. strncpy(p, suffix, suffix_end - suffix);
  1425. p += suffix_end - suffix;
  1426. }
  1427. if (insert != NULL) {
  1428. /* Note: assume insert does not have leading or trailing '/' */
  1429. strcpy(p, insert);
  1430. p += strlen(insert);
  1431. *p++ = '/';
  1432. }
  1433. strncpy(p, filename, filename_end - filename);
  1434. p += filename_end - filename;
  1435. if (need_slash)
  1436. *p++ = '/';
  1437. *p = '\0';
  1438. return (dest);
  1439. }
  1440. /*
  1441. * The ustar header for the pax extended attributes must have a
  1442. * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
  1443. * where 'pid' is the PID of the archiving process. Unfortunately,
  1444. * that makes testing a pain since the output varies for each run,
  1445. * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
  1446. * for now. (Someday, I'll make this settable. Then I can use the
  1447. * SUS recommendation as default and test harnesses can override it
  1448. * to get predictable results.)
  1449. *
  1450. * Joerg Schilling has argued that this is unnecessary because, in
  1451. * practice, if the pax extended attributes get extracted as regular
  1452. * files, no one is going to bother reading those attributes to
  1453. * manually restore them. Based on this, 'star' uses
  1454. * /tmp/PaxHeader/'basename' as the ustar header name. This is a
  1455. * tempting argument, in part because it's simpler than the SUSv3
  1456. * recommendation, but I'm not entirely convinced. I'm also
  1457. * uncomfortable with the fact that "/tmp" is a Unix-ism.
  1458. *
  1459. * The following routine leverages build_ustar_entry_name() above and
  1460. * so is simpler than you might think. It just needs to provide the
  1461. * additional path element and handle a few pathological cases).
  1462. */
  1463. static char *
  1464. build_pax_attribute_name(char *dest, const char *src)
  1465. {
  1466. char buff[64];
  1467. const char *p;
  1468. /* Handle the null filename case. */
  1469. if (src == NULL || *src == '\0') {
  1470. strcpy(dest, "PaxHeader/blank");
  1471. return (dest);
  1472. }
  1473. /* Prune final '/' and other unwanted final elements. */
  1474. p = src + strlen(src);
  1475. for (;;) {
  1476. /* Ends in "/", remove the '/' */
  1477. if (p > src && p[-1] == '/') {
  1478. --p;
  1479. continue;
  1480. }
  1481. /* Ends in "/.", remove the '.' */
  1482. if (p > src + 1 && p[-1] == '.'
  1483. && p[-2] == '/') {
  1484. --p;
  1485. continue;
  1486. }
  1487. break;
  1488. }
  1489. /* Pathological case: After above, there was nothing left.
  1490. * This includes "/." "/./." "/.//./." etc. */
  1491. if (p == src) {
  1492. strcpy(dest, "/PaxHeader/rootdir");
  1493. return (dest);
  1494. }
  1495. /* Convert unadorned "." into a suitable filename. */
  1496. if (*src == '.' && p == src + 1) {
  1497. strcpy(dest, "PaxHeader/currentdir");
  1498. return (dest);
  1499. }
  1500. /*
  1501. * TODO: Push this string into the 'pax' structure to avoid
  1502. * recomputing it every time. That will also open the door
  1503. * to having clients override it.
  1504. */
  1505. #if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
  1506. sprintf(buff, "PaxHeader.%d", getpid());
  1507. #else
  1508. /* If the platform can't fetch the pid, don't include it. */
  1509. strcpy(buff, "PaxHeader");
  1510. #endif
  1511. /* General case: build a ustar-compatible name adding
  1512. * "/PaxHeader/". */
  1513. build_ustar_entry_name(dest, src, p - src, buff);
  1514. return (dest);
  1515. }
  1516. /*
  1517. * GNU PAX Format 1.0 requires the special name, which pattern is:
  1518. * <dir>/GNUSparseFile.<pid>/<original file name>
  1519. *
  1520. * Since reproducable archives are more important, use 0 as pid.
  1521. *
  1522. * This function is used for only Sparse file, a file type of which
  1523. * is regular file.
  1524. */
  1525. static char *
  1526. build_gnu_sparse_name(char *dest, const char *src)
  1527. {
  1528. const char *p;
  1529. /* Handle the null filename case. */
  1530. if (src == NULL || *src == '\0') {
  1531. strcpy(dest, "GNUSparseFile/blank");
  1532. return (dest);
  1533. }
  1534. /* Prune final '/' and other unwanted final elements. */
  1535. p = src + strlen(src);
  1536. for (;;) {
  1537. /* Ends in "/", remove the '/' */
  1538. if (p > src && p[-1] == '/') {
  1539. --p;
  1540. continue;
  1541. }
  1542. /* Ends in "/.", remove the '.' */
  1543. if (p > src + 1 && p[-1] == '.'
  1544. && p[-2] == '/') {
  1545. --p;
  1546. continue;
  1547. }
  1548. break;
  1549. }
  1550. /* General case: build a ustar-compatible name adding
  1551. * "/GNUSparseFile/". */
  1552. build_ustar_entry_name(dest, src, p - src, "GNUSparseFile.0");
  1553. return (dest);
  1554. }
  1555. /* Write two null blocks for the end of archive */
  1556. static int
  1557. archive_write_pax_close(struct archive_write *a)
  1558. {
  1559. return (__archive_write_nulls(a, 512 * 2));
  1560. }
  1561. static int
  1562. archive_write_pax_free(struct archive_write *a)
  1563. {
  1564. struct pax *pax;
  1565. pax = (struct pax *)a->format_data;
  1566. if (pax == NULL)
  1567. return (ARCHIVE_OK);
  1568. archive_string_free(&pax->pax_header);
  1569. archive_string_free(&pax->sparse_map);
  1570. archive_string_free(&pax->l_url_encoded_name);
  1571. sparse_list_clear(pax);
  1572. free(pax);
  1573. a->format_data = NULL;
  1574. return (ARCHIVE_OK);
  1575. }
  1576. static int
  1577. archive_write_pax_finish_entry(struct archive_write *a)
  1578. {
  1579. struct pax *pax;
  1580. uint64_t remaining;
  1581. int ret;
  1582. pax = (struct pax *)a->format_data;
  1583. remaining = pax->entry_bytes_remaining;
  1584. if (remaining == 0) {
  1585. while (pax->sparse_list) {
  1586. struct sparse_block *sb;
  1587. if (!pax->sparse_list->is_hole)
  1588. remaining += pax->sparse_list->remaining;
  1589. sb = pax->sparse_list->next;
  1590. free(pax->sparse_list);
  1591. pax->sparse_list = sb;
  1592. }
  1593. }
  1594. ret = __archive_write_nulls(a, (size_t)(remaining + pax->entry_padding));
  1595. pax->entry_bytes_remaining = pax->entry_padding = 0;
  1596. return (ret);
  1597. }
  1598. static ssize_t
  1599. archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
  1600. {
  1601. struct pax *pax;
  1602. size_t ws;
  1603. size_t total;
  1604. int ret;
  1605. pax = (struct pax *)a->format_data;
  1606. /*
  1607. * According to GNU PAX format 1.0, write a sparse map
  1608. * before the body.
  1609. */
  1610. if (archive_strlen(&(pax->sparse_map))) {
  1611. ret = __archive_write_output(a, pax->sparse_map.s,
  1612. archive_strlen(&(pax->sparse_map)));
  1613. if (ret != ARCHIVE_OK)
  1614. return (ret);
  1615. ret = __archive_write_nulls(a, pax->sparse_map_padding);
  1616. if (ret != ARCHIVE_OK)
  1617. return (ret);
  1618. archive_string_empty(&(pax->sparse_map));
  1619. }
  1620. total = 0;
  1621. while (total < s) {
  1622. const unsigned char *p;
  1623. while (pax->sparse_list != NULL &&
  1624. pax->sparse_list->remaining == 0) {
  1625. struct sparse_block *sb = pax->sparse_list->next;
  1626. free(pax->sparse_list);
  1627. pax->sparse_list = sb;
  1628. }
  1629. if (pax->sparse_list == NULL)
  1630. return (total);
  1631. p = ((const unsigned char *)buff) + total;
  1632. ws = s - total;
  1633. if (ws > pax->sparse_list->remaining)
  1634. ws = (size_t)pax->sparse_list->remaining;
  1635. if (pax->sparse_list->is_hole) {
  1636. /* Current block is hole thus we do not write
  1637. * the body. */
  1638. pax->sparse_list->remaining -= ws;
  1639. total += ws;
  1640. continue;
  1641. }
  1642. ret = __archive_write_output(a, p, ws);
  1643. pax->sparse_list->remaining -= ws;
  1644. total += ws;
  1645. if (ret != ARCHIVE_OK)
  1646. return (ret);
  1647. }
  1648. return (total);
  1649. }
  1650. static int
  1651. has_non_ASCII(const char *_p)
  1652. {
  1653. const unsigned char *p = (const unsigned char *)_p;
  1654. if (p == NULL)
  1655. return (1);
  1656. while (*p != '\0' && *p < 128)
  1657. p++;
  1658. return (*p != '\0');
  1659. }
  1660. /*
  1661. * Used by extended attribute support; encodes the name
  1662. * so that there will be no '=' characters in the result.
  1663. */
  1664. static char *
  1665. url_encode(const char *in)
  1666. {
  1667. const char *s;
  1668. char *d;
  1669. int out_len = 0;
  1670. char *out;
  1671. for (s = in; *s != '\0'; s++) {
  1672. if (*s < 33 || *s > 126 || *s == '%' || *s == '=')
  1673. out_len += 3;
  1674. else
  1675. out_len++;
  1676. }
  1677. out = (char *)malloc(out_len + 1);
  1678. if (out == NULL)
  1679. return (NULL);
  1680. for (s = in, d = out; *s != '\0'; s++) {
  1681. /* encode any non-printable ASCII character or '%' or '=' */
  1682. if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
  1683. /* URL encoding is '%' followed by two hex digits */
  1684. *d++ = '%';
  1685. *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
  1686. *d++ = "0123456789ABCDEF"[0x0f & *s];
  1687. } else {
  1688. *d++ = *s;
  1689. }
  1690. }
  1691. *d = '\0';
  1692. return (out);
  1693. }
  1694. /*
  1695. * Encode a sequence of bytes into a C string using base-64 encoding.
  1696. *
  1697. * Returns a null-terminated C string allocated with malloc(); caller
  1698. * is responsible for freeing the result.
  1699. */
  1700. static char *
  1701. base64_encode(const char *s, size_t len)
  1702. {
  1703. static const char digits[64] =
  1704. { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
  1705. 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
  1706. 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
  1707. 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  1708. '8','9','+','/' };
  1709. int v;
  1710. char *d, *out;
  1711. /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
  1712. out = (char *)malloc((len * 4 + 2) / 3 + 1);
  1713. if (out == NULL)
  1714. return (NULL);
  1715. d = out;
  1716. /* Convert each group of 3 bytes into 4 characters. */
  1717. while (len >= 3) {
  1718. v = (((int)s[0] << 16) & 0xff0000)
  1719. | (((int)s[1] << 8) & 0xff00)
  1720. | (((int)s[2]) & 0x00ff);
  1721. s += 3;
  1722. len -= 3;
  1723. *d++ = digits[(v >> 18) & 0x3f];
  1724. *d++ = digits[(v >> 12) & 0x3f];
  1725. *d++ = digits[(v >> 6) & 0x3f];
  1726. *d++ = digits[(v) & 0x3f];
  1727. }
  1728. /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
  1729. switch (len) {
  1730. case 0: break;
  1731. case 1:
  1732. v = (((int)s[0] << 16) & 0xff0000);
  1733. *d++ = digits[(v >> 18) & 0x3f];
  1734. *d++ = digits[(v >> 12) & 0x3f];
  1735. break;
  1736. case 2:
  1737. v = (((int)s[0] << 16) & 0xff0000)
  1738. | (((int)s[1] << 8) & 0xff00);
  1739. *d++ = digits[(v >> 18) & 0x3f];
  1740. *d++ = digits[(v >> 12) & 0x3f];
  1741. *d++ = digits[(v >> 6) & 0x3f];
  1742. break;
  1743. }
  1744. /* Add trailing NUL character so output is a valid C string. */
  1745. *d = '\0';
  1746. return (out);
  1747. }
  1748. static void
  1749. sparse_list_clear(struct pax *pax)
  1750. {
  1751. while (pax->sparse_list != NULL) {
  1752. struct sparse_block *sb = pax->sparse_list;
  1753. pax->sparse_list = sb->next;
  1754. free(sb);
  1755. }
  1756. pax->sparse_tail = NULL;
  1757. }
  1758. static int
  1759. _sparse_list_add_block(struct pax *pax, int64_t offset, int64_t length,
  1760. int is_hole)
  1761. {
  1762. struct sparse_block *sb;
  1763. sb = (struct sparse_block *)malloc(sizeof(*sb));
  1764. if (sb == NULL)
  1765. return (ARCHIVE_FATAL);
  1766. sb->next = NULL;
  1767. sb->is_hole = is_hole;
  1768. sb->offset = offset;
  1769. sb->remaining = length;
  1770. if (pax->sparse_list == NULL || pax->sparse_tail == NULL)
  1771. pax->sparse_list = pax->sparse_tail = sb;
  1772. else {
  1773. pax->sparse_tail->next = sb;
  1774. pax->sparse_tail = sb;
  1775. }
  1776. return (ARCHIVE_OK);
  1777. }
  1778. static int
  1779. sparse_list_add(struct pax *pax, int64_t offset, int64_t length)
  1780. {
  1781. int64_t last_offset;
  1782. int r;
  1783. if (pax->sparse_tail == NULL)
  1784. last_offset = 0;
  1785. else {
  1786. last_offset = pax->sparse_tail->offset +
  1787. pax->sparse_tail->remaining;
  1788. }
  1789. if (last_offset < offset) {
  1790. /* Add a hole block. */
  1791. r = _sparse_list_add_block(pax, last_offset,
  1792. offset - last_offset, 1);
  1793. if (r != ARCHIVE_OK)
  1794. return (r);
  1795. }
  1796. /* Add data block. */
  1797. return (_sparse_list_add_block(pax, offset, length, 0));
  1798. }