archive_write_set_format_pax.c 61 KB

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