archive_write_set_format_pax.c 56 KB

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