archive_write_set_format_pax.c 61 KB

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