archive_write_set_format_pax.c 60 KB

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