archive_write_set_format_xar.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224
  1. /*-
  2. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_LIMITS_H
  31. #include <limits.h>
  32. #endif
  33. #include <stdlib.h>
  34. #if HAVE_LIBXML_XMLWRITER_H
  35. #include <libxml/xmlwriter.h>
  36. #endif
  37. #ifdef HAVE_BZLIB_H
  38. #include <cm_bzlib.h>
  39. #endif
  40. #if HAVE_LZMA_H
  41. #include <cm_lzma.h>
  42. #endif
  43. #ifdef HAVE_ZLIB_H
  44. #include <cm_zlib.h>
  45. #endif
  46. #include "archive.h"
  47. #include "archive_digest_private.h"
  48. #include "archive_endian.h"
  49. #include "archive_entry.h"
  50. #include "archive_entry_locale.h"
  51. #include "archive_private.h"
  52. #include "archive_rb.h"
  53. #include "archive_string.h"
  54. #include "archive_write_private.h"
  55. /*
  56. * Differences to xar utility.
  57. * - Subdocument is not supported yet.
  58. * - ACL is not supported yet.
  59. * - When writing an XML element <link type="<file-type>">, <file-type>
  60. * which is a file type a symbolic link is referencing is always marked
  61. * as "broken". Xar utility uses stat(2) to get the file type, but, in
  62. * libarchive format writer, we should not use it; if it is needed, we
  63. * should get about it at archive_read_disk.c.
  64. * - It is possible to appear both <flags> and <ext2> elements.
  65. * Xar utility generates <flags> on BSD platform and <ext2> on Linux
  66. * platform.
  67. *
  68. */
  69. #if !(defined(HAVE_LIBXML_XMLWRITER_H) && defined(LIBXML_VERSION) &&\
  70. LIBXML_VERSION >= 20703) ||\
  71. !defined(HAVE_ZLIB_H) || \
  72. !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
  73. /*
  74. * xar needs several external libraries.
  75. * o libxml2
  76. * o openssl or MD5/SHA1 hash function
  77. * o zlib
  78. * o bzlib2 (option)
  79. * o liblzma (option)
  80. */
  81. int
  82. archive_write_set_format_xar(struct archive *_a)
  83. {
  84. struct archive_write *a = (struct archive_write *)_a;
  85. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  86. "Xar not supported on this platform");
  87. return (ARCHIVE_WARN);
  88. }
  89. #else /* Support xar format */
  90. /*#define DEBUG_PRINT_TOC 1 */
  91. #define BAD_CAST_CONST (const xmlChar *)
  92. #define HEADER_MAGIC 0x78617221
  93. #define HEADER_SIZE 28
  94. #define HEADER_VERSION 1
  95. enum sumalg {
  96. CKSUM_NONE = 0,
  97. CKSUM_SHA1 = 1,
  98. CKSUM_MD5 = 2
  99. };
  100. #define MD5_SIZE 16
  101. #define SHA1_SIZE 20
  102. #define MAX_SUM_SIZE 20
  103. #define MD5_NAME "md5"
  104. #define SHA1_NAME "sha1"
  105. enum enctype {
  106. NONE,
  107. GZIP,
  108. BZIP2,
  109. LZMA,
  110. XZ,
  111. };
  112. struct chksumwork {
  113. enum sumalg alg;
  114. #ifdef ARCHIVE_HAS_MD5
  115. archive_md5_ctx md5ctx;
  116. #endif
  117. #ifdef ARCHIVE_HAS_SHA1
  118. archive_sha1_ctx sha1ctx;
  119. #endif
  120. };
  121. enum la_zaction {
  122. ARCHIVE_Z_FINISH,
  123. ARCHIVE_Z_RUN
  124. };
  125. /*
  126. * Universal zstream.
  127. */
  128. struct la_zstream {
  129. const unsigned char *next_in;
  130. size_t avail_in;
  131. uint64_t total_in;
  132. unsigned char *next_out;
  133. size_t avail_out;
  134. uint64_t total_out;
  135. int valid;
  136. void *real_stream;
  137. int (*code) (struct archive *a,
  138. struct la_zstream *lastrm,
  139. enum la_zaction action);
  140. int (*end)(struct archive *a,
  141. struct la_zstream *lastrm);
  142. };
  143. struct chksumval {
  144. enum sumalg alg;
  145. size_t len;
  146. unsigned char val[MAX_SUM_SIZE];
  147. };
  148. struct heap_data {
  149. int id;
  150. struct heap_data *next;
  151. uint64_t temp_offset;
  152. uint64_t length; /* archived size. */
  153. uint64_t size; /* extracted size. */
  154. enum enctype compression;
  155. struct chksumval a_sum; /* archived checksum. */
  156. struct chksumval e_sum; /* extracted checksum. */
  157. };
  158. struct file {
  159. struct archive_rb_node rbnode;
  160. int id;
  161. struct archive_entry *entry;
  162. struct archive_rb_tree rbtree;
  163. struct file *next;
  164. struct file *chnext;
  165. struct file *hlnext;
  166. /* For hardlinked files.
  167. * Use only when archive_entry_nlink() > 1 */
  168. struct file *hardlink_target;
  169. struct file *parent; /* parent directory entry */
  170. /*
  171. * To manage sub directory files.
  172. * We use 'chnext' (a member of struct file) to chain.
  173. */
  174. struct {
  175. struct file *first;
  176. struct file **last;
  177. } children;
  178. /* For making a directory tree. */
  179. struct archive_string parentdir;
  180. struct archive_string basename;
  181. struct archive_string symlink;
  182. int ea_idx;
  183. struct {
  184. struct heap_data *first;
  185. struct heap_data **last;
  186. } xattr;
  187. struct heap_data data;
  188. struct archive_string script;
  189. int virtual:1;
  190. int dir:1;
  191. };
  192. struct hardlink {
  193. struct archive_rb_node rbnode;
  194. int nlink;
  195. struct {
  196. struct file *first;
  197. struct file **last;
  198. } file_list;
  199. };
  200. struct xar {
  201. int temp_fd;
  202. uint64_t temp_offset;
  203. int file_idx;
  204. struct file *root;
  205. struct file *cur_dirent;
  206. struct archive_string cur_dirstr;
  207. struct file *cur_file;
  208. uint64_t bytes_remaining;
  209. struct archive_string tstr;
  210. struct archive_string vstr;
  211. enum sumalg opt_toc_sumalg;
  212. enum sumalg opt_sumalg;
  213. enum enctype opt_compression;
  214. int opt_compression_level;
  215. uint32_t opt_threads;
  216. struct chksumwork a_sumwrk; /* archived checksum. */
  217. struct chksumwork e_sumwrk; /* extracted checksum. */
  218. struct la_zstream stream;
  219. struct archive_string_conv *sconv;
  220. /*
  221. * Compressed data buffer.
  222. */
  223. unsigned char wbuff[1024 * 64];
  224. size_t wbuff_remaining;
  225. struct heap_data toc;
  226. /*
  227. * The list of all file entries is used to manage struct file
  228. * objects.
  229. * We use 'next' (a member of struct file) to chain.
  230. */
  231. struct {
  232. struct file *first;
  233. struct file **last;
  234. } file_list;
  235. /*
  236. * The list of hard-linked file entries.
  237. * We use 'hlnext' (a member of struct file) to chain.
  238. */
  239. struct archive_rb_tree hardlink_rbtree;
  240. };
  241. static int xar_options(struct archive_write *,
  242. const char *, const char *);
  243. static int xar_write_header(struct archive_write *,
  244. struct archive_entry *);
  245. static ssize_t xar_write_data(struct archive_write *,
  246. const void *, size_t);
  247. static int xar_finish_entry(struct archive_write *);
  248. static int xar_close(struct archive_write *);
  249. static int xar_free(struct archive_write *);
  250. static struct file *file_new(struct archive_write *a, struct archive_entry *);
  251. static void file_free(struct file *);
  252. static struct file *file_create_virtual_dir(struct archive_write *a, struct xar *,
  253. const char *);
  254. static int file_add_child_tail(struct file *, struct file *);
  255. static struct file *file_find_child(struct file *, const char *);
  256. static int file_gen_utility_names(struct archive_write *,
  257. struct file *);
  258. static int get_path_component(char *, int, const char *);
  259. static int file_tree(struct archive_write *, struct file **);
  260. static void file_register(struct xar *, struct file *);
  261. static void file_init_register(struct xar *);
  262. static void file_free_register(struct xar *);
  263. static int file_register_hardlink(struct archive_write *,
  264. struct file *);
  265. static void file_connect_hardlink_files(struct xar *);
  266. static void file_init_hardlinks(struct xar *);
  267. static void file_free_hardlinks(struct xar *);
  268. static void checksum_init(struct chksumwork *, enum sumalg);
  269. static void checksum_update(struct chksumwork *, const void *, size_t);
  270. static void checksum_final(struct chksumwork *, struct chksumval *);
  271. static int compression_init_encoder_gzip(struct archive *,
  272. struct la_zstream *, int, int);
  273. static int compression_code_gzip(struct archive *,
  274. struct la_zstream *, enum la_zaction);
  275. static int compression_end_gzip(struct archive *, struct la_zstream *);
  276. static int compression_init_encoder_bzip2(struct archive *,
  277. struct la_zstream *, int);
  278. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  279. static int compression_code_bzip2(struct archive *,
  280. struct la_zstream *, enum la_zaction);
  281. static int compression_end_bzip2(struct archive *, struct la_zstream *);
  282. #endif
  283. static int compression_init_encoder_lzma(struct archive *,
  284. struct la_zstream *, int);
  285. static int compression_init_encoder_xz(struct archive *,
  286. struct la_zstream *, int, int);
  287. #if defined(HAVE_LZMA_H)
  288. static int compression_code_lzma(struct archive *,
  289. struct la_zstream *, enum la_zaction);
  290. static int compression_end_lzma(struct archive *, struct la_zstream *);
  291. #endif
  292. static int xar_compression_init_encoder(struct archive_write *);
  293. static int compression_code(struct archive *,
  294. struct la_zstream *, enum la_zaction);
  295. static int compression_end(struct archive *,
  296. struct la_zstream *);
  297. static int save_xattrs(struct archive_write *, struct file *);
  298. static int getalgsize(enum sumalg);
  299. static const char *getalgname(enum sumalg);
  300. int
  301. archive_write_set_format_xar(struct archive *_a)
  302. {
  303. struct archive_write *a = (struct archive_write *)_a;
  304. struct xar *xar;
  305. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  306. ARCHIVE_STATE_NEW, "archive_write_set_format_xar");
  307. /* If another format was already registered, unregister it. */
  308. if (a->format_free != NULL)
  309. (a->format_free)(a);
  310. xar = calloc(1, sizeof(*xar));
  311. if (xar == NULL) {
  312. archive_set_error(&a->archive, ENOMEM,
  313. "Can't allocate xar data");
  314. return (ARCHIVE_FATAL);
  315. }
  316. xar->temp_fd = -1;
  317. file_init_register(xar);
  318. file_init_hardlinks(xar);
  319. archive_string_init(&(xar->tstr));
  320. archive_string_init(&(xar->vstr));
  321. /*
  322. * Create the root directory.
  323. */
  324. xar->root = file_create_virtual_dir(a, xar, "");
  325. if (xar->root == NULL) {
  326. free(xar);
  327. archive_set_error(&a->archive, ENOMEM,
  328. "Can't allocate xar data");
  329. return (ARCHIVE_FATAL);
  330. }
  331. xar->root->parent = xar->root;
  332. file_register(xar, xar->root);
  333. xar->cur_dirent = xar->root;
  334. archive_string_init(&(xar->cur_dirstr));
  335. archive_string_ensure(&(xar->cur_dirstr), 1);
  336. xar->cur_dirstr.s[0] = 0;
  337. /*
  338. * Initialize option.
  339. */
  340. /* Set default checksum type. */
  341. xar->opt_toc_sumalg = CKSUM_SHA1;
  342. xar->opt_sumalg = CKSUM_SHA1;
  343. /* Set default compression type, level, and number of threads. */
  344. xar->opt_compression = GZIP;
  345. xar->opt_compression_level = 6;
  346. xar->opt_threads = 1;
  347. a->format_data = xar;
  348. a->format_name = "xar";
  349. a->format_options = xar_options;
  350. a->format_write_header = xar_write_header;
  351. a->format_write_data = xar_write_data;
  352. a->format_finish_entry = xar_finish_entry;
  353. a->format_close = xar_close;
  354. a->format_free = xar_free;
  355. a->archive.archive_format = ARCHIVE_FORMAT_XAR;
  356. a->archive.archive_format_name = "xar";
  357. return (ARCHIVE_OK);
  358. }
  359. static int
  360. xar_options(struct archive_write *a, const char *key, const char *value)
  361. {
  362. struct xar *xar;
  363. xar = (struct xar *)a->format_data;
  364. if (strcmp(key, "checksum") == 0) {
  365. if (value == NULL)
  366. xar->opt_sumalg = CKSUM_NONE;
  367. else if (strcmp(value, "sha1") == 0)
  368. xar->opt_sumalg = CKSUM_SHA1;
  369. else if (strcmp(value, "md5") == 0)
  370. xar->opt_sumalg = CKSUM_MD5;
  371. else {
  372. archive_set_error(&(a->archive),
  373. ARCHIVE_ERRNO_MISC,
  374. "Unknown checksum name: `%s'",
  375. value);
  376. return (ARCHIVE_FAILED);
  377. }
  378. return (ARCHIVE_OK);
  379. }
  380. if (strcmp(key, "compression") == 0) {
  381. const char *name = NULL;
  382. if (value == NULL)
  383. xar->opt_compression = NONE;
  384. else if (strcmp(value, "gzip") == 0)
  385. xar->opt_compression = GZIP;
  386. else if (strcmp(value, "bzip2") == 0)
  387. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  388. xar->opt_compression = BZIP2;
  389. #else
  390. name = "bzip2";
  391. #endif
  392. else if (strcmp(value, "lzma") == 0)
  393. #if HAVE_LZMA_H
  394. xar->opt_compression = LZMA;
  395. #else
  396. name = "lzma";
  397. #endif
  398. else if (strcmp(value, "xz") == 0)
  399. #if HAVE_LZMA_H
  400. xar->opt_compression = XZ;
  401. #else
  402. name = "xz";
  403. #endif
  404. else {
  405. archive_set_error(&(a->archive),
  406. ARCHIVE_ERRNO_MISC,
  407. "Unknown compression name: `%s'",
  408. value);
  409. return (ARCHIVE_FAILED);
  410. }
  411. if (name != NULL) {
  412. archive_set_error(&(a->archive),
  413. ARCHIVE_ERRNO_MISC,
  414. "`%s' compression not supported "
  415. "on this platform",
  416. name);
  417. return (ARCHIVE_FAILED);
  418. }
  419. return (ARCHIVE_OK);
  420. }
  421. if (strcmp(key, "compression-level") == 0) {
  422. if (value == NULL ||
  423. !(value[0] >= '0' && value[0] <= '9') ||
  424. value[1] != '\0') {
  425. archive_set_error(&(a->archive),
  426. ARCHIVE_ERRNO_MISC,
  427. "Illegal value `%s'",
  428. value);
  429. return (ARCHIVE_FAILED);
  430. }
  431. xar->opt_compression_level = value[0] - '0';
  432. return (ARCHIVE_OK);
  433. }
  434. if (strcmp(key, "toc-checksum") == 0) {
  435. if (value == NULL)
  436. xar->opt_toc_sumalg = CKSUM_NONE;
  437. else if (strcmp(value, "sha1") == 0)
  438. xar->opt_toc_sumalg = CKSUM_SHA1;
  439. else if (strcmp(value, "md5") == 0)
  440. xar->opt_toc_sumalg = CKSUM_MD5;
  441. else {
  442. archive_set_error(&(a->archive),
  443. ARCHIVE_ERRNO_MISC,
  444. "Unknown checksum name: `%s'",
  445. value);
  446. return (ARCHIVE_FAILED);
  447. }
  448. return (ARCHIVE_OK);
  449. }
  450. if (strcmp(key, "threads") == 0) {
  451. if (value == NULL)
  452. return (ARCHIVE_FAILED);
  453. xar->opt_threads = (int)strtoul(value, NULL, 10);
  454. if (xar->opt_threads == 0 && errno != 0) {
  455. xar->opt_threads = 1;
  456. archive_set_error(&(a->archive),
  457. ARCHIVE_ERRNO_MISC,
  458. "Illegal value `%s'",
  459. value);
  460. return (ARCHIVE_FAILED);
  461. }
  462. if (xar->opt_threads == 0) {
  463. #ifdef HAVE_LZMA_STREAM_ENCODER_MT
  464. xar->opt_threads = lzma_cputhreads();
  465. #else
  466. xar->opt_threads = 1;
  467. #endif
  468. }
  469. }
  470. /* Note: The "warn" return is just to inform the options
  471. * supervisor that we didn't handle it. It will generate
  472. * a suitable error if no one used this option. */
  473. return (ARCHIVE_WARN);
  474. }
  475. static int
  476. xar_write_header(struct archive_write *a, struct archive_entry *entry)
  477. {
  478. struct xar *xar;
  479. struct file *file;
  480. struct archive_entry *file_entry;
  481. int r, r2;
  482. xar = (struct xar *)a->format_data;
  483. xar->cur_file = NULL;
  484. xar->bytes_remaining = 0;
  485. if (xar->sconv == NULL) {
  486. xar->sconv = archive_string_conversion_to_charset(
  487. &a->archive, "UTF-8", 1);
  488. if (xar->sconv == NULL)
  489. return (ARCHIVE_FATAL);
  490. }
  491. file = file_new(a, entry);
  492. if (file == NULL) {
  493. archive_set_error(&a->archive, ENOMEM,
  494. "Can't allocate data");
  495. return (ARCHIVE_FATAL);
  496. }
  497. r2 = file_gen_utility_names(a, file);
  498. if (r2 < ARCHIVE_WARN)
  499. return (r2);
  500. /*
  501. * Ignore a path which looks like the top of directory name
  502. * since we have already made the root directory of an Xar archive.
  503. */
  504. if (archive_strlen(&(file->parentdir)) == 0 &&
  505. archive_strlen(&(file->basename)) == 0) {
  506. file_free(file);
  507. return (r2);
  508. }
  509. /* Add entry into tree */
  510. file_entry = file->entry;
  511. r = file_tree(a, &file);
  512. if (r != ARCHIVE_OK)
  513. return (r);
  514. /* There is the same file in tree and
  515. * the current file is older than the file in tree.
  516. * So we don't need the current file data anymore. */
  517. if (file->entry != file_entry)
  518. return (r2);
  519. if (file->id == 0)
  520. file_register(xar, file);
  521. /* A virtual file, which is a directory, does not have
  522. * any contents and we won't store it into a archive
  523. * file other than its name. */
  524. if (file->virtual)
  525. return (r2);
  526. /*
  527. * Prepare to save the contents of the file.
  528. */
  529. if (xar->temp_fd == -1) {
  530. int algsize;
  531. xar->temp_offset = 0;
  532. xar->temp_fd = __archive_mktemp(NULL);
  533. if (xar->temp_fd < 0) {
  534. archive_set_error(&a->archive, errno,
  535. "Couldn't create temporary file");
  536. return (ARCHIVE_FATAL);
  537. }
  538. algsize = getalgsize(xar->opt_toc_sumalg);
  539. if (algsize > 0) {
  540. if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
  541. archive_set_error(&(a->archive), errno,
  542. "lseek failed");
  543. return (ARCHIVE_FATAL);
  544. }
  545. xar->temp_offset = algsize;
  546. }
  547. }
  548. if (archive_entry_hardlink(file->entry) == NULL) {
  549. r = save_xattrs(a, file);
  550. if (r != ARCHIVE_OK)
  551. return (ARCHIVE_FATAL);
  552. }
  553. /* Non regular files contents are unneeded to be saved to
  554. * a temporary file. */
  555. if (archive_entry_filetype(file->entry) != AE_IFREG)
  556. return (r2);
  557. /*
  558. * Set the current file to cur_file to read its contents.
  559. */
  560. xar->cur_file = file;
  561. if (archive_entry_nlink(file->entry) > 1) {
  562. r = file_register_hardlink(a, file);
  563. if (r != ARCHIVE_OK)
  564. return (r);
  565. if (archive_entry_hardlink(file->entry) != NULL) {
  566. archive_entry_unset_size(file->entry);
  567. return (r2);
  568. }
  569. }
  570. /* Save a offset of current file in temporary file. */
  571. file->data.temp_offset = xar->temp_offset;
  572. file->data.size = archive_entry_size(file->entry);
  573. file->data.compression = xar->opt_compression;
  574. xar->bytes_remaining = archive_entry_size(file->entry);
  575. checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
  576. checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
  577. r = xar_compression_init_encoder(a);
  578. if (r != ARCHIVE_OK)
  579. return (r);
  580. else
  581. return (r2);
  582. }
  583. static int
  584. write_to_temp(struct archive_write *a, const void *buff, size_t s)
  585. {
  586. struct xar *xar;
  587. const unsigned char *p;
  588. ssize_t ws;
  589. xar = (struct xar *)a->format_data;
  590. p = (const unsigned char *)buff;
  591. while (s) {
  592. ws = write(xar->temp_fd, p, s);
  593. if (ws < 0) {
  594. archive_set_error(&(a->archive), errno,
  595. "fwrite function failed");
  596. return (ARCHIVE_FATAL);
  597. }
  598. s -= ws;
  599. p += ws;
  600. xar->temp_offset += ws;
  601. }
  602. return (ARCHIVE_OK);
  603. }
  604. static ssize_t
  605. xar_write_data(struct archive_write *a, const void *buff, size_t s)
  606. {
  607. struct xar *xar;
  608. enum la_zaction run;
  609. size_t size, rsize;
  610. int r;
  611. xar = (struct xar *)a->format_data;
  612. if (s > xar->bytes_remaining)
  613. s = (size_t)xar->bytes_remaining;
  614. if (s == 0 || xar->cur_file == NULL)
  615. return (0);
  616. if (xar->cur_file->data.compression == NONE) {
  617. checksum_update(&(xar->e_sumwrk), buff, s);
  618. checksum_update(&(xar->a_sumwrk), buff, s);
  619. size = rsize = s;
  620. } else {
  621. xar->stream.next_in = (const unsigned char *)buff;
  622. xar->stream.avail_in = s;
  623. if (xar->bytes_remaining > s)
  624. run = ARCHIVE_Z_RUN;
  625. else
  626. run = ARCHIVE_Z_FINISH;
  627. /* Compress file data. */
  628. r = compression_code(&(a->archive), &(xar->stream), run);
  629. if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
  630. return (ARCHIVE_FATAL);
  631. rsize = s - xar->stream.avail_in;
  632. checksum_update(&(xar->e_sumwrk), buff, rsize);
  633. size = sizeof(xar->wbuff) - xar->stream.avail_out;
  634. checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
  635. }
  636. #if !defined(_WIN32) || defined(__CYGWIN__)
  637. if (xar->bytes_remaining ==
  638. (uint64_t)archive_entry_size(xar->cur_file->entry)) {
  639. /*
  640. * Get the path of a shell script if so.
  641. */
  642. const unsigned char *b = (const unsigned char *)buff;
  643. archive_string_empty(&(xar->cur_file->script));
  644. if (rsize > 2 && b[0] == '#' && b[1] == '!') {
  645. size_t i, end, off;
  646. off = 2;
  647. if (b[off] == ' ')
  648. off++;
  649. #ifdef PATH_MAX
  650. if ((rsize - off) > PATH_MAX)
  651. end = off + PATH_MAX;
  652. else
  653. #endif
  654. end = rsize;
  655. /* Find the end of a script path. */
  656. for (i = off; i < end && b[i] != '\0' &&
  657. b[i] != '\n' && b[i] != '\r' &&
  658. b[i] != ' ' && b[i] != '\t'; i++)
  659. ;
  660. archive_strncpy(&(xar->cur_file->script), b + off,
  661. i - off);
  662. }
  663. }
  664. #endif
  665. if (xar->cur_file->data.compression == NONE) {
  666. if (write_to_temp(a, buff, size) != ARCHIVE_OK)
  667. return (ARCHIVE_FATAL);
  668. } else {
  669. if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
  670. return (ARCHIVE_FATAL);
  671. }
  672. xar->bytes_remaining -= rsize;
  673. xar->cur_file->data.length += size;
  674. return (rsize);
  675. }
  676. static int
  677. xar_finish_entry(struct archive_write *a)
  678. {
  679. struct xar *xar;
  680. struct file *file;
  681. size_t s;
  682. ssize_t w;
  683. xar = (struct xar *)a->format_data;
  684. if (xar->cur_file == NULL)
  685. return (ARCHIVE_OK);
  686. while (xar->bytes_remaining > 0) {
  687. s = (size_t)xar->bytes_remaining;
  688. if (s > a->null_length)
  689. s = a->null_length;
  690. w = xar_write_data(a, a->nulls, s);
  691. if (w > 0)
  692. xar->bytes_remaining -= w;
  693. else
  694. return (w);
  695. }
  696. file = xar->cur_file;
  697. checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
  698. checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
  699. xar->cur_file = NULL;
  700. return (ARCHIVE_OK);
  701. }
  702. static int
  703. xmlwrite_string_attr(struct archive_write *a, xmlTextWriterPtr writer,
  704. const char *key, const char *value,
  705. const char *attrkey, const char *attrvalue)
  706. {
  707. int r;
  708. r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
  709. if (r < 0) {
  710. archive_set_error(&a->archive,
  711. ARCHIVE_ERRNO_MISC,
  712. "xmlTextWriterStartElement() failed: %d", r);
  713. return (ARCHIVE_FATAL);
  714. }
  715. if (attrkey != NULL && attrvalue != NULL) {
  716. r = xmlTextWriterWriteAttribute(writer,
  717. BAD_CAST_CONST(attrkey), BAD_CAST_CONST(attrvalue));
  718. if (r < 0) {
  719. archive_set_error(&a->archive,
  720. ARCHIVE_ERRNO_MISC,
  721. "xmlTextWriterWriteAttribute() failed: %d", r);
  722. return (ARCHIVE_FATAL);
  723. }
  724. }
  725. if (value != NULL) {
  726. r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
  727. if (r < 0) {
  728. archive_set_error(&a->archive,
  729. ARCHIVE_ERRNO_MISC,
  730. "xmlTextWriterWriteString() failed: %d", r);
  731. return (ARCHIVE_FATAL);
  732. }
  733. }
  734. r = xmlTextWriterEndElement(writer);
  735. if (r < 0) {
  736. archive_set_error(&a->archive,
  737. ARCHIVE_ERRNO_MISC,
  738. "xmlTextWriterEndElement() failed: %d", r);
  739. return (ARCHIVE_FATAL);
  740. }
  741. return (ARCHIVE_OK);
  742. }
  743. static int
  744. xmlwrite_string(struct archive_write *a, xmlTextWriterPtr writer,
  745. const char *key, const char *value)
  746. {
  747. int r;
  748. if (value == NULL)
  749. return (ARCHIVE_OK);
  750. r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
  751. if (r < 0) {
  752. archive_set_error(&a->archive,
  753. ARCHIVE_ERRNO_MISC,
  754. "xmlTextWriterStartElement() failed: %d", r);
  755. return (ARCHIVE_FATAL);
  756. }
  757. if (value != NULL) {
  758. r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
  759. if (r < 0) {
  760. archive_set_error(&a->archive,
  761. ARCHIVE_ERRNO_MISC,
  762. "xmlTextWriterWriteString() failed: %d", r);
  763. return (ARCHIVE_FATAL);
  764. }
  765. }
  766. r = xmlTextWriterEndElement(writer);
  767. if (r < 0) {
  768. archive_set_error(&a->archive,
  769. ARCHIVE_ERRNO_MISC,
  770. "xmlTextWriterEndElement() failed: %d", r);
  771. return (ARCHIVE_FATAL);
  772. }
  773. return (ARCHIVE_OK);
  774. }
  775. static int
  776. xmlwrite_fstring(struct archive_write *a, xmlTextWriterPtr writer,
  777. const char *key, const char *fmt, ...)
  778. {
  779. struct xar *xar;
  780. va_list ap;
  781. xar = (struct xar *)a->format_data;
  782. va_start(ap, fmt);
  783. archive_string_empty(&xar->vstr);
  784. archive_string_vsprintf(&xar->vstr, fmt, ap);
  785. va_end(ap);
  786. return (xmlwrite_string(a, writer, key, xar->vstr.s));
  787. }
  788. static int
  789. xmlwrite_time(struct archive_write *a, xmlTextWriterPtr writer,
  790. const char *key, time_t t, int z)
  791. {
  792. char timestr[100];
  793. struct tm tm;
  794. #if defined(HAVE_GMTIME_R)
  795. gmtime_r(&t, &tm);
  796. #elif defined(HAVE__GMTIME64_S)
  797. _gmtime64_s(&tm, &t);
  798. #else
  799. memcpy(&tm, gmtime(&t), sizeof(tm));
  800. #endif
  801. memset(&timestr, 0, sizeof(timestr));
  802. /* Do not use %F and %T for portability. */
  803. strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
  804. if (z)
  805. strcat(timestr, "Z");
  806. return (xmlwrite_string(a, writer, key, timestr));
  807. }
  808. static int
  809. xmlwrite_mode(struct archive_write *a, xmlTextWriterPtr writer,
  810. const char *key, mode_t mode)
  811. {
  812. char ms[5];
  813. ms[0] = '0';
  814. ms[1] = '0' + ((mode >> 6) & 07);
  815. ms[2] = '0' + ((mode >> 3) & 07);
  816. ms[3] = '0' + (mode & 07);
  817. ms[4] = '\0';
  818. return (xmlwrite_string(a, writer, key, ms));
  819. }
  820. static int
  821. xmlwrite_sum(struct archive_write *a, xmlTextWriterPtr writer,
  822. const char *key, struct chksumval *sum)
  823. {
  824. const char *algname;
  825. int algsize;
  826. char buff[MAX_SUM_SIZE*2 + 1];
  827. char *p;
  828. unsigned char *s;
  829. int i, r;
  830. if (sum->len > 0) {
  831. algname = getalgname(sum->alg);
  832. algsize = getalgsize(sum->alg);
  833. if (algname != NULL) {
  834. const char *hex = "0123456789abcdef";
  835. p = buff;
  836. s = sum->val;
  837. for (i = 0; i < algsize; i++) {
  838. *p++ = hex[(*s >> 4)];
  839. *p++ = hex[(*s & 0x0f)];
  840. s++;
  841. }
  842. *p = '\0';
  843. r = xmlwrite_string_attr(a, writer,
  844. key, buff,
  845. "style", algname);
  846. if (r < 0)
  847. return (ARCHIVE_FATAL);
  848. }
  849. }
  850. return (ARCHIVE_OK);
  851. }
  852. static int
  853. xmlwrite_heap(struct archive_write *a, xmlTextWriterPtr writer,
  854. struct heap_data *heap)
  855. {
  856. const char *encname;
  857. int r;
  858. r = xmlwrite_fstring(a, writer, "length", "%ju", heap->length);
  859. if (r < 0)
  860. return (ARCHIVE_FATAL);
  861. r = xmlwrite_fstring(a, writer, "offset", "%ju", heap->temp_offset);
  862. if (r < 0)
  863. return (ARCHIVE_FATAL);
  864. r = xmlwrite_fstring(a, writer, "size", "%ju", heap->size);
  865. if (r < 0)
  866. return (ARCHIVE_FATAL);
  867. switch (heap->compression) {
  868. case GZIP:
  869. encname = "application/x-gzip"; break;
  870. case BZIP2:
  871. encname = "application/x-bzip2"; break;
  872. case LZMA:
  873. encname = "application/x-lzma"; break;
  874. case XZ:
  875. encname = "application/x-xz"; break;
  876. default:
  877. encname = "application/octet-stream"; break;
  878. }
  879. r = xmlwrite_string_attr(a, writer, "encoding", NULL,
  880. "style", encname);
  881. if (r < 0)
  882. return (ARCHIVE_FATAL);
  883. r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
  884. if (r < 0)
  885. return (ARCHIVE_FATAL);
  886. r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
  887. if (r < 0)
  888. return (ARCHIVE_FATAL);
  889. return (ARCHIVE_OK);
  890. }
  891. /*
  892. * xar utility records fflags as following xml elements:
  893. * <flags>
  894. * <UserNoDump/>
  895. * .....
  896. * </flags>
  897. * or
  898. * <ext2>
  899. * <NoDump/>
  900. * .....
  901. * </ext2>
  902. * If xar is running on BSD platform, records <flags>..</flags>;
  903. * if xar is running on linux platform, records <ext2>..</ext2>;
  904. * otherwise does not record.
  905. *
  906. * Our implements records both <flags> and <ext2> if it's necessary.
  907. */
  908. static int
  909. make_fflags_entry(struct archive_write *a, xmlTextWriterPtr writer,
  910. const char *element, const char *fflags_text)
  911. {
  912. static const struct flagentry {
  913. const char *name;
  914. const char *xarname;
  915. }
  916. flagbsd[] = {
  917. { "sappnd", "SystemAppend"},
  918. { "sappend", "SystemAppend"},
  919. { "arch", "SystemArchived"},
  920. { "archived", "SystemArchived"},
  921. { "schg", "SystemImmutable"},
  922. { "schange", "SystemImmutable"},
  923. { "simmutable", "SystemImmutable"},
  924. { "nosunlnk", "SystemNoUnlink"},
  925. { "nosunlink", "SystemNoUnlink"},
  926. { "snapshot", "SystemSnapshot"},
  927. { "uappnd", "UserAppend"},
  928. { "uappend", "UserAppend"},
  929. { "uchg", "UserImmutable"},
  930. { "uchange", "UserImmutable"},
  931. { "uimmutable", "UserImmutable"},
  932. { "nodump", "UserNoDump"},
  933. { "noopaque", "UserOpaque"},
  934. { "nouunlnk", "UserNoUnlink"},
  935. { "nouunlink", "UserNoUnlink"},
  936. { NULL, NULL}
  937. },
  938. flagext2[] = {
  939. { "sappnd", "AppendOnly"},
  940. { "sappend", "AppendOnly"},
  941. { "schg", "Immutable"},
  942. { "schange", "Immutable"},
  943. { "simmutable", "Immutable"},
  944. { "nodump", "NoDump"},
  945. { "nouunlnk", "Undelete"},
  946. { "nouunlink", "Undelete"},
  947. { "btree", "BTree"},
  948. { "comperr", "CompError"},
  949. { "compress", "Compress"},
  950. { "noatime", "NoAtime"},
  951. { "compdirty", "CompDirty"},
  952. { "comprblk", "CompBlock"},
  953. { "dirsync", "DirSync"},
  954. { "hashidx", "HashIndexed"},
  955. { "imagic", "iMagic"},
  956. { "journal", "Journaled"},
  957. { "securedeletion", "SecureDeletion"},
  958. { "sync", "Synchronous"},
  959. { "notail", "NoTail"},
  960. { "topdir", "TopDir"},
  961. { "reserved", "Reserved"},
  962. { NULL, NULL}
  963. };
  964. const struct flagentry *fe, *flagentry;
  965. #define FLAGENTRY_MAXSIZE ((sizeof(flagbsd)+sizeof(flagext2))/sizeof(flagbsd))
  966. const struct flagentry *avail[FLAGENTRY_MAXSIZE];
  967. const char *p;
  968. int i, n, r;
  969. if (strcmp(element, "ext2") == 0)
  970. flagentry = flagext2;
  971. else
  972. flagentry = flagbsd;
  973. n = 0;
  974. p = fflags_text;
  975. do {
  976. const char *cp;
  977. cp = strchr(p, ',');
  978. if (cp == NULL)
  979. cp = p + strlen(p);
  980. for (fe = flagentry; fe->name != NULL; fe++) {
  981. if (fe->name[cp - p] != '\0'
  982. || p[0] != fe->name[0])
  983. continue;
  984. if (strncmp(p, fe->name, cp - p) == 0) {
  985. avail[n++] = fe;
  986. break;
  987. }
  988. }
  989. if (*cp == ',')
  990. p = cp + 1;
  991. else
  992. p = NULL;
  993. } while (p != NULL);
  994. if (n > 0) {
  995. r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(element));
  996. if (r < 0) {
  997. archive_set_error(&a->archive,
  998. ARCHIVE_ERRNO_MISC,
  999. "xmlTextWriterStartElement() failed: %d", r);
  1000. return (ARCHIVE_FATAL);
  1001. }
  1002. for (i = 0; i < n; i++) {
  1003. r = xmlwrite_string(a, writer,
  1004. avail[i]->xarname, NULL);
  1005. if (r != ARCHIVE_OK)
  1006. return (r);
  1007. }
  1008. r = xmlTextWriterEndElement(writer);
  1009. if (r < 0) {
  1010. archive_set_error(&a->archive,
  1011. ARCHIVE_ERRNO_MISC,
  1012. "xmlTextWriterEndElement() failed: %d", r);
  1013. return (ARCHIVE_FATAL);
  1014. }
  1015. }
  1016. return (ARCHIVE_OK);
  1017. }
  1018. static int
  1019. make_file_entry(struct archive_write *a, xmlTextWriterPtr writer,
  1020. struct file *file)
  1021. {
  1022. struct xar *xar;
  1023. const char *filetype, *filelink, *fflags;
  1024. struct archive_string linkto;
  1025. struct heap_data *heap;
  1026. unsigned char *tmp;
  1027. const char *p;
  1028. size_t len;
  1029. int r, r2, l, ll;
  1030. xar = (struct xar *)a->format_data;
  1031. r2 = ARCHIVE_OK;
  1032. /*
  1033. * Make a file name entry, "<name>".
  1034. */
  1035. l = ll = archive_strlen(&(file->basename));
  1036. tmp = malloc(l);
  1037. if (tmp == NULL) {
  1038. archive_set_error(&a->archive, ENOMEM,
  1039. "Can't allocate memory");
  1040. return (ARCHIVE_FATAL);
  1041. }
  1042. r = UTF8Toisolat1(tmp, &l, BAD_CAST(file->basename.s), &ll);
  1043. free(tmp);
  1044. if (r < 0) {
  1045. r = xmlTextWriterStartElement(writer, BAD_CAST("name"));
  1046. if (r < 0) {
  1047. archive_set_error(&a->archive,
  1048. ARCHIVE_ERRNO_MISC,
  1049. "xmlTextWriterStartElement() failed: %d", r);
  1050. return (ARCHIVE_FATAL);
  1051. }
  1052. r = xmlTextWriterWriteAttribute(writer,
  1053. BAD_CAST("enctype"), BAD_CAST("base64"));
  1054. if (r < 0) {
  1055. archive_set_error(&a->archive,
  1056. ARCHIVE_ERRNO_MISC,
  1057. "xmlTextWriterWriteAttribute() failed: %d", r);
  1058. return (ARCHIVE_FATAL);
  1059. }
  1060. r = xmlTextWriterWriteBase64(writer, file->basename.s,
  1061. 0, archive_strlen(&(file->basename)));
  1062. if (r < 0) {
  1063. archive_set_error(&a->archive,
  1064. ARCHIVE_ERRNO_MISC,
  1065. "xmlTextWriterWriteBase64() failed: %d", r);
  1066. return (ARCHIVE_FATAL);
  1067. }
  1068. r = xmlTextWriterEndElement(writer);
  1069. if (r < 0) {
  1070. archive_set_error(&a->archive,
  1071. ARCHIVE_ERRNO_MISC,
  1072. "xmlTextWriterEndElement() failed: %d", r);
  1073. return (ARCHIVE_FATAL);
  1074. }
  1075. } else {
  1076. r = xmlwrite_string(a, writer, "name", file->basename.s);
  1077. if (r < 0)
  1078. return (ARCHIVE_FATAL);
  1079. }
  1080. /*
  1081. * Make a file type entry, "<type>".
  1082. */
  1083. filelink = NULL;
  1084. archive_string_init(&linkto);
  1085. switch (archive_entry_filetype(file->entry)) {
  1086. case AE_IFDIR:
  1087. filetype = "directory"; break;
  1088. case AE_IFLNK:
  1089. filetype = "symlink"; break;
  1090. case AE_IFCHR:
  1091. filetype = "character special"; break;
  1092. case AE_IFBLK:
  1093. filetype = "block special"; break;
  1094. case AE_IFSOCK:
  1095. filetype = "socket"; break;
  1096. case AE_IFIFO:
  1097. filetype = "fifo"; break;
  1098. case AE_IFREG:
  1099. default:
  1100. if (file->hardlink_target != NULL) {
  1101. filetype = "hardlink";
  1102. filelink = "link";
  1103. if (file->hardlink_target == file)
  1104. archive_strcpy(&linkto, "original");
  1105. else
  1106. archive_string_sprintf(&linkto, "%d",
  1107. file->hardlink_target->id);
  1108. } else
  1109. filetype = "file";
  1110. break;
  1111. }
  1112. r = xmlwrite_string_attr(a, writer, "type", filetype,
  1113. filelink, linkto.s);
  1114. archive_string_free(&linkto);
  1115. if (r < 0)
  1116. return (ARCHIVE_FATAL);
  1117. /*
  1118. * On a virtual directory, we record "name" and "type" only.
  1119. */
  1120. if (file->virtual)
  1121. return (ARCHIVE_OK);
  1122. switch (archive_entry_filetype(file->entry)) {
  1123. case AE_IFLNK:
  1124. /*
  1125. * xar utility has checked a file type, which
  1126. * a symbolic-link file has referenced.
  1127. * For example:
  1128. * <link type="directory">../ref/</link>
  1129. * The symlink target file is "../ref/" and its
  1130. * file type is a directory.
  1131. *
  1132. * <link type="file">../f</link>
  1133. * The symlink target file is "../f" and its
  1134. * file type is a regular file.
  1135. *
  1136. * But our implementation cannot do it, and then we
  1137. * always record that a attribute "type" is "broken",
  1138. * for example:
  1139. * <link type="broken">foo/bar</link>
  1140. * It means "foo/bar" is not reachable.
  1141. */
  1142. r = xmlwrite_string_attr(a, writer, "link",
  1143. file->symlink.s,
  1144. "type", "broken");
  1145. if (r < 0)
  1146. return (ARCHIVE_FATAL);
  1147. break;
  1148. case AE_IFCHR:
  1149. case AE_IFBLK:
  1150. r = xmlTextWriterStartElement(writer, BAD_CAST("device"));
  1151. if (r < 0) {
  1152. archive_set_error(&a->archive,
  1153. ARCHIVE_ERRNO_MISC,
  1154. "xmlTextWriterStartElement() failed: %d", r);
  1155. return (ARCHIVE_FATAL);
  1156. }
  1157. r = xmlwrite_fstring(a, writer, "major",
  1158. "%d", archive_entry_rdevmajor(file->entry));
  1159. if (r < 0)
  1160. return (ARCHIVE_FATAL);
  1161. r = xmlwrite_fstring(a, writer, "minor",
  1162. "%d", archive_entry_rdevminor(file->entry));
  1163. if (r < 0)
  1164. return (ARCHIVE_FATAL);
  1165. r = xmlTextWriterEndElement(writer);
  1166. if (r < 0) {
  1167. archive_set_error(&a->archive,
  1168. ARCHIVE_ERRNO_MISC,
  1169. "xmlTextWriterEndElement() failed: %d", r);
  1170. return (ARCHIVE_FATAL);
  1171. }
  1172. break;
  1173. default:
  1174. break;
  1175. }
  1176. /*
  1177. * Make a inode entry, "<inode>".
  1178. */
  1179. r = xmlwrite_fstring(a, writer, "inode",
  1180. "%jd", archive_entry_ino64(file->entry));
  1181. if (r < 0)
  1182. return (ARCHIVE_FATAL);
  1183. if (archive_entry_dev(file->entry) != 0) {
  1184. r = xmlwrite_fstring(a, writer, "deviceno",
  1185. "%d", archive_entry_dev(file->entry));
  1186. if (r < 0)
  1187. return (ARCHIVE_FATAL);
  1188. }
  1189. /*
  1190. * Make a file mode entry, "<mode>".
  1191. */
  1192. r = xmlwrite_mode(a, writer, "mode",
  1193. archive_entry_mode(file->entry));
  1194. if (r < 0)
  1195. return (ARCHIVE_FATAL);
  1196. /*
  1197. * Make a user entry, "<uid>" and "<user>.
  1198. */
  1199. r = xmlwrite_fstring(a, writer, "uid",
  1200. "%d", archive_entry_uid(file->entry));
  1201. if (r < 0)
  1202. return (ARCHIVE_FATAL);
  1203. r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
  1204. if (r != 0) {
  1205. if (errno == ENOMEM) {
  1206. archive_set_error(&a->archive, ENOMEM,
  1207. "Can't allocate memory for Uname");
  1208. return (ARCHIVE_FATAL);
  1209. }
  1210. archive_set_error(&a->archive,
  1211. ARCHIVE_ERRNO_FILE_FORMAT,
  1212. "Can't translate uname '%s' to UTF-8",
  1213. archive_entry_uname(file->entry));
  1214. r2 = ARCHIVE_WARN;
  1215. }
  1216. if (len > 0) {
  1217. r = xmlwrite_string(a, writer, "user", p);
  1218. if (r < 0)
  1219. return (ARCHIVE_FATAL);
  1220. }
  1221. /*
  1222. * Make a group entry, "<gid>" and "<group>.
  1223. */
  1224. r = xmlwrite_fstring(a, writer, "gid",
  1225. "%d", archive_entry_gid(file->entry));
  1226. if (r < 0)
  1227. return (ARCHIVE_FATAL);
  1228. r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
  1229. if (r != 0) {
  1230. if (errno == ENOMEM) {
  1231. archive_set_error(&a->archive, ENOMEM,
  1232. "Can't allocate memory for Gname");
  1233. return (ARCHIVE_FATAL);
  1234. }
  1235. archive_set_error(&a->archive,
  1236. ARCHIVE_ERRNO_FILE_FORMAT,
  1237. "Can't translate gname '%s' to UTF-8",
  1238. archive_entry_gname(file->entry));
  1239. r2 = ARCHIVE_WARN;
  1240. }
  1241. if (len > 0) {
  1242. r = xmlwrite_string(a, writer, "group", p);
  1243. if (r < 0)
  1244. return (ARCHIVE_FATAL);
  1245. }
  1246. /*
  1247. * Make a ctime entry, "<ctime>".
  1248. */
  1249. if (archive_entry_ctime_is_set(file->entry)) {
  1250. r = xmlwrite_time(a, writer, "ctime",
  1251. archive_entry_ctime(file->entry), 1);
  1252. if (r < 0)
  1253. return (ARCHIVE_FATAL);
  1254. }
  1255. /*
  1256. * Make a mtime entry, "<mtime>".
  1257. */
  1258. if (archive_entry_mtime_is_set(file->entry)) {
  1259. r = xmlwrite_time(a, writer, "mtime",
  1260. archive_entry_mtime(file->entry), 1);
  1261. if (r < 0)
  1262. return (ARCHIVE_FATAL);
  1263. }
  1264. /*
  1265. * Make a atime entry, "<atime>".
  1266. */
  1267. if (archive_entry_atime_is_set(file->entry)) {
  1268. r = xmlwrite_time(a, writer, "atime",
  1269. archive_entry_atime(file->entry), 1);
  1270. if (r < 0)
  1271. return (ARCHIVE_FATAL);
  1272. }
  1273. /*
  1274. * Make fflags entries, "<flags>" and "<ext2>".
  1275. */
  1276. fflags = archive_entry_fflags_text(file->entry);
  1277. if (fflags != NULL) {
  1278. r = make_fflags_entry(a, writer, "flags", fflags);
  1279. if (r < 0)
  1280. return (r);
  1281. r = make_fflags_entry(a, writer, "ext2", fflags);
  1282. if (r < 0)
  1283. return (r);
  1284. }
  1285. /*
  1286. * Make extended attribute entries, "<ea>".
  1287. */
  1288. archive_entry_xattr_reset(file->entry);
  1289. for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
  1290. const char *name;
  1291. const void *value;
  1292. size_t size;
  1293. archive_entry_xattr_next(file->entry,
  1294. &name, &value, &size);
  1295. r = xmlTextWriterStartElement(writer, BAD_CAST("ea"));
  1296. if (r < 0) {
  1297. archive_set_error(&a->archive,
  1298. ARCHIVE_ERRNO_MISC,
  1299. "xmlTextWriterStartElement() failed: %d", r);
  1300. return (ARCHIVE_FATAL);
  1301. }
  1302. r = xmlTextWriterWriteFormatAttribute(writer,
  1303. BAD_CAST("id"), "%d", heap->id);
  1304. if (r < 0) {
  1305. archive_set_error(&a->archive,
  1306. ARCHIVE_ERRNO_MISC,
  1307. "xmlTextWriterWriteAttribute() failed: %d", r);
  1308. return (ARCHIVE_FATAL);
  1309. }
  1310. r = xmlwrite_heap(a, writer, heap);
  1311. if (r < 0)
  1312. return (ARCHIVE_FATAL);
  1313. r = xmlwrite_string(a, writer, "name", name);
  1314. if (r < 0)
  1315. return (ARCHIVE_FATAL);
  1316. r = xmlTextWriterEndElement(writer);
  1317. if (r < 0) {
  1318. archive_set_error(&a->archive,
  1319. ARCHIVE_ERRNO_MISC,
  1320. "xmlTextWriterEndElement() failed: %d", r);
  1321. return (ARCHIVE_FATAL);
  1322. }
  1323. }
  1324. /*
  1325. * Make a file data entry, "<data>".
  1326. */
  1327. if (file->data.length > 0) {
  1328. r = xmlTextWriterStartElement(writer, BAD_CAST("data"));
  1329. if (r < 0) {
  1330. archive_set_error(&a->archive,
  1331. ARCHIVE_ERRNO_MISC,
  1332. "xmlTextWriterStartElement() failed: %d", r);
  1333. return (ARCHIVE_FATAL);
  1334. }
  1335. r = xmlwrite_heap(a, writer, &(file->data));
  1336. if (r < 0)
  1337. return (ARCHIVE_FATAL);
  1338. r = xmlTextWriterEndElement(writer);
  1339. if (r < 0) {
  1340. archive_set_error(&a->archive,
  1341. ARCHIVE_ERRNO_MISC,
  1342. "xmlTextWriterEndElement() failed: %d", r);
  1343. return (ARCHIVE_FATAL);
  1344. }
  1345. }
  1346. if (archive_strlen(&file->script) > 0) {
  1347. r = xmlTextWriterStartElement(writer, BAD_CAST("content"));
  1348. if (r < 0) {
  1349. archive_set_error(&a->archive,
  1350. ARCHIVE_ERRNO_MISC,
  1351. "xmlTextWriterStartElement() failed: %d", r);
  1352. return (ARCHIVE_FATAL);
  1353. }
  1354. r = xmlwrite_string(a, writer,
  1355. "interpreter", file->script.s);
  1356. if (r < 0)
  1357. return (ARCHIVE_FATAL);
  1358. r = xmlwrite_string(a, writer, "type", "script");
  1359. if (r < 0)
  1360. return (ARCHIVE_FATAL);
  1361. r = xmlTextWriterEndElement(writer);
  1362. if (r < 0) {
  1363. archive_set_error(&a->archive,
  1364. ARCHIVE_ERRNO_MISC,
  1365. "xmlTextWriterEndElement() failed: %d", r);
  1366. return (ARCHIVE_FATAL);
  1367. }
  1368. }
  1369. return (r2);
  1370. }
  1371. /*
  1372. * Make the TOC
  1373. */
  1374. static int
  1375. make_toc(struct archive_write *a)
  1376. {
  1377. struct xar *xar;
  1378. struct file *np;
  1379. xmlBufferPtr bp;
  1380. xmlTextWriterPtr writer;
  1381. int algsize;
  1382. int r, ret;
  1383. xar = (struct xar *)a->format_data;
  1384. ret = ARCHIVE_FATAL;
  1385. /*
  1386. * Initialize xml writer.
  1387. */
  1388. writer = NULL;
  1389. bp = xmlBufferCreate();
  1390. if (bp == NULL) {
  1391. archive_set_error(&a->archive, ENOMEM,
  1392. "xmlBufferCreate() "
  1393. "couldn't create xml buffer");
  1394. goto exit_toc;
  1395. }
  1396. writer = xmlNewTextWriterMemory(bp, 0);
  1397. if (writer == NULL) {
  1398. archive_set_error(&a->archive,
  1399. ARCHIVE_ERRNO_MISC,
  1400. "xmlNewTextWriterMemory() "
  1401. "couldn't create xml writer");
  1402. goto exit_toc;
  1403. }
  1404. r = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
  1405. if (r < 0) {
  1406. archive_set_error(&a->archive,
  1407. ARCHIVE_ERRNO_MISC,
  1408. "xmlTextWriterStartDocument() failed: %d", r);
  1409. goto exit_toc;
  1410. }
  1411. r = xmlTextWriterSetIndent(writer, 4);
  1412. if (r < 0) {
  1413. archive_set_error(&a->archive,
  1414. ARCHIVE_ERRNO_MISC,
  1415. "xmlTextWriterSetIndent() failed: %d", r);
  1416. goto exit_toc;
  1417. }
  1418. /*
  1419. * Start recording TOC
  1420. */
  1421. r = xmlTextWriterStartElement(writer, BAD_CAST("xar"));
  1422. if (r < 0) {
  1423. archive_set_error(&a->archive,
  1424. ARCHIVE_ERRNO_MISC,
  1425. "xmlTextWriterStartElement() failed: %d", r);
  1426. goto exit_toc;
  1427. }
  1428. r = xmlTextWriterStartElement(writer, BAD_CAST("toc"));
  1429. if (r < 0) {
  1430. archive_set_error(&a->archive,
  1431. ARCHIVE_ERRNO_MISC,
  1432. "xmlTextWriterStartDocument() failed: %d", r);
  1433. goto exit_toc;
  1434. }
  1435. /*
  1436. * Record the creation time of the archive file.
  1437. */
  1438. r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
  1439. if (r < 0)
  1440. goto exit_toc;
  1441. /*
  1442. * Record the checksum value of TOC
  1443. */
  1444. algsize = getalgsize(xar->opt_toc_sumalg);
  1445. if (algsize) {
  1446. /*
  1447. * Record TOC checksum
  1448. */
  1449. r = xmlTextWriterStartElement(writer, BAD_CAST("checksum"));
  1450. if (r < 0) {
  1451. archive_set_error(&a->archive,
  1452. ARCHIVE_ERRNO_MISC,
  1453. "xmlTextWriterStartElement() failed: %d", r);
  1454. goto exit_toc;
  1455. }
  1456. r = xmlTextWriterWriteAttribute(writer, BAD_CAST("style"),
  1457. BAD_CAST_CONST(getalgname(xar->opt_toc_sumalg)));
  1458. if (r < 0) {
  1459. archive_set_error(&a->archive,
  1460. ARCHIVE_ERRNO_MISC,
  1461. "xmlTextWriterWriteAttribute() failed: %d", r);
  1462. goto exit_toc;
  1463. }
  1464. /*
  1465. * Record the offset of the value of checksum of TOC
  1466. */
  1467. r = xmlwrite_string(a, writer, "offset", "0");
  1468. if (r < 0)
  1469. goto exit_toc;
  1470. /*
  1471. * Record the size of the value of checksum of TOC
  1472. */
  1473. r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
  1474. if (r < 0)
  1475. goto exit_toc;
  1476. r = xmlTextWriterEndElement(writer);
  1477. if (r < 0) {
  1478. archive_set_error(&a->archive,
  1479. ARCHIVE_ERRNO_MISC,
  1480. "xmlTextWriterEndElement() failed: %d", r);
  1481. goto exit_toc;
  1482. }
  1483. }
  1484. np = xar->root;
  1485. do {
  1486. if (np != np->parent) {
  1487. r = make_file_entry(a, writer, np);
  1488. if (r != ARCHIVE_OK)
  1489. goto exit_toc;
  1490. }
  1491. if (np->dir && np->children.first != NULL) {
  1492. /* Enter to sub directories. */
  1493. np = np->children.first;
  1494. r = xmlTextWriterStartElement(writer,
  1495. BAD_CAST("file"));
  1496. if (r < 0) {
  1497. archive_set_error(&a->archive,
  1498. ARCHIVE_ERRNO_MISC,
  1499. "xmlTextWriterStartElement() "
  1500. "failed: %d", r);
  1501. goto exit_toc;
  1502. }
  1503. r = xmlTextWriterWriteFormatAttribute(
  1504. writer, BAD_CAST("id"), "%d", np->id);
  1505. if (r < 0) {
  1506. archive_set_error(&a->archive,
  1507. ARCHIVE_ERRNO_MISC,
  1508. "xmlTextWriterWriteAttribute() "
  1509. "failed: %d", r);
  1510. goto exit_toc;
  1511. }
  1512. continue;
  1513. }
  1514. while (np != np->parent) {
  1515. r = xmlTextWriterEndElement(writer);
  1516. if (r < 0) {
  1517. archive_set_error(&a->archive,
  1518. ARCHIVE_ERRNO_MISC,
  1519. "xmlTextWriterEndElement() "
  1520. "failed: %d", r);
  1521. goto exit_toc;
  1522. }
  1523. if (np->chnext == NULL) {
  1524. /* Return to the parent directory. */
  1525. np = np->parent;
  1526. } else {
  1527. np = np->chnext;
  1528. r = xmlTextWriterStartElement(writer,
  1529. BAD_CAST("file"));
  1530. if (r < 0) {
  1531. archive_set_error(&a->archive,
  1532. ARCHIVE_ERRNO_MISC,
  1533. "xmlTextWriterStartElement() "
  1534. "failed: %d", r);
  1535. goto exit_toc;
  1536. }
  1537. r = xmlTextWriterWriteFormatAttribute(
  1538. writer, BAD_CAST("id"), "%d", np->id);
  1539. if (r < 0) {
  1540. archive_set_error(&a->archive,
  1541. ARCHIVE_ERRNO_MISC,
  1542. "xmlTextWriterWriteAttribute() "
  1543. "failed: %d", r);
  1544. goto exit_toc;
  1545. }
  1546. break;
  1547. }
  1548. }
  1549. } while (np != np->parent);
  1550. r = xmlTextWriterEndDocument(writer);
  1551. if (r < 0) {
  1552. archive_set_error(&a->archive,
  1553. ARCHIVE_ERRNO_MISC,
  1554. "xmlTextWriterEndDocument() failed: %d", r);
  1555. goto exit_toc;
  1556. }
  1557. #if DEBUG_PRINT_TOC
  1558. fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
  1559. strlen((const char *)bp->content), bp->content);
  1560. #endif
  1561. /*
  1562. * Compress the TOC and calculate the sum of the TOC.
  1563. */
  1564. xar->toc.temp_offset = xar->temp_offset;
  1565. xar->toc.size = bp->use;
  1566. checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
  1567. r = compression_init_encoder_gzip(&(a->archive),
  1568. &(xar->stream), 6, 1);
  1569. if (r != ARCHIVE_OK)
  1570. goto exit_toc;
  1571. xar->stream.next_in = bp->content;
  1572. xar->stream.avail_in = bp->use;
  1573. xar->stream.total_in = 0;
  1574. xar->stream.next_out = xar->wbuff;
  1575. xar->stream.avail_out = sizeof(xar->wbuff);
  1576. xar->stream.total_out = 0;
  1577. for (;;) {
  1578. size_t size;
  1579. r = compression_code(&(a->archive),
  1580. &(xar->stream), ARCHIVE_Z_FINISH);
  1581. if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
  1582. goto exit_toc;
  1583. size = sizeof(xar->wbuff) - xar->stream.avail_out;
  1584. checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
  1585. if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
  1586. goto exit_toc;
  1587. if (r == ARCHIVE_EOF)
  1588. break;
  1589. xar->stream.next_out = xar->wbuff;
  1590. xar->stream.avail_out = sizeof(xar->wbuff);
  1591. }
  1592. r = compression_end(&(a->archive), &(xar->stream));
  1593. if (r != ARCHIVE_OK)
  1594. goto exit_toc;
  1595. xar->toc.length = xar->stream.total_out;
  1596. xar->toc.compression = GZIP;
  1597. checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
  1598. ret = ARCHIVE_OK;
  1599. exit_toc:
  1600. if (writer)
  1601. xmlFreeTextWriter(writer);
  1602. if (bp)
  1603. xmlBufferFree(bp);
  1604. return (ret);
  1605. }
  1606. static int
  1607. flush_wbuff(struct archive_write *a)
  1608. {
  1609. struct xar *xar;
  1610. int r;
  1611. size_t s;
  1612. xar = (struct xar *)a->format_data;
  1613. s = sizeof(xar->wbuff) - xar->wbuff_remaining;
  1614. r = __archive_write_output(a, xar->wbuff, s);
  1615. if (r != ARCHIVE_OK)
  1616. return (r);
  1617. xar->wbuff_remaining = sizeof(xar->wbuff);
  1618. return (r);
  1619. }
  1620. static int
  1621. copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
  1622. {
  1623. struct xar *xar;
  1624. int r;
  1625. xar = (struct xar *)a->format_data;
  1626. if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
  1627. archive_set_error(&(a->archive), errno, "lseek failed");
  1628. return (ARCHIVE_FATAL);
  1629. }
  1630. while (length) {
  1631. size_t rsize;
  1632. ssize_t rs;
  1633. unsigned char *wb;
  1634. if (length > xar->wbuff_remaining)
  1635. rsize = xar->wbuff_remaining;
  1636. else
  1637. rsize = (size_t)length;
  1638. wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
  1639. rs = read(xar->temp_fd, wb, rsize);
  1640. if (rs < 0) {
  1641. archive_set_error(&(a->archive), errno,
  1642. "Can't read temporary file(%jd)",
  1643. (intmax_t)rs);
  1644. return (ARCHIVE_FATAL);
  1645. }
  1646. if (rs == 0) {
  1647. archive_set_error(&(a->archive), 0,
  1648. "Truncated xar archive");
  1649. return (ARCHIVE_FATAL);
  1650. }
  1651. xar->wbuff_remaining -= rs;
  1652. length -= rs;
  1653. if (xar->wbuff_remaining == 0) {
  1654. r = flush_wbuff(a);
  1655. if (r != ARCHIVE_OK)
  1656. return (r);
  1657. }
  1658. }
  1659. return (ARCHIVE_OK);
  1660. }
  1661. static int
  1662. xar_close(struct archive_write *a)
  1663. {
  1664. struct xar *xar;
  1665. unsigned char *wb;
  1666. uint64_t length;
  1667. int r;
  1668. xar = (struct xar *)a->format_data;
  1669. /* Empty! */
  1670. if (xar->root->children.first == NULL)
  1671. return (ARCHIVE_OK);
  1672. /* Save the length of all file extended attributes and contents. */
  1673. length = xar->temp_offset;
  1674. /* Connect hardlinked files */
  1675. file_connect_hardlink_files(xar);
  1676. /* Make the TOC */
  1677. r = make_toc(a);
  1678. if (r != ARCHIVE_OK)
  1679. return (r);
  1680. /*
  1681. * Make the xar header on wbuff(write buffer).
  1682. */
  1683. wb = xar->wbuff;
  1684. xar->wbuff_remaining = sizeof(xar->wbuff);
  1685. archive_be32enc(&wb[0], HEADER_MAGIC);
  1686. archive_be16enc(&wb[4], HEADER_SIZE);
  1687. archive_be16enc(&wb[6], HEADER_VERSION);
  1688. archive_be64enc(&wb[8], xar->toc.length);
  1689. archive_be64enc(&wb[16], xar->toc.size);
  1690. archive_be32enc(&wb[24], xar->toc.a_sum.alg);
  1691. xar->wbuff_remaining -= HEADER_SIZE;
  1692. /*
  1693. * Write the TOC
  1694. */
  1695. r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
  1696. if (r != ARCHIVE_OK)
  1697. return (r);
  1698. /* Write the checksum value of the TOC. */
  1699. if (xar->toc.a_sum.len) {
  1700. if (xar->wbuff_remaining < xar->toc.a_sum.len) {
  1701. r = flush_wbuff(a);
  1702. if (r != ARCHIVE_OK)
  1703. return (r);
  1704. }
  1705. wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
  1706. memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
  1707. xar->wbuff_remaining -= xar->toc.a_sum.len;
  1708. }
  1709. /*
  1710. * Write all file extended attributes and contents.
  1711. */
  1712. r = copy_out(a, xar->toc.a_sum.len, length);
  1713. if (r != ARCHIVE_OK)
  1714. return (r);
  1715. r = flush_wbuff(a);
  1716. return (r);
  1717. }
  1718. static int
  1719. xar_free(struct archive_write *a)
  1720. {
  1721. struct xar *xar;
  1722. xar = (struct xar *)a->format_data;
  1723. /* Close the temporary file. */
  1724. if (xar->temp_fd >= 0)
  1725. close(xar->temp_fd);
  1726. archive_string_free(&(xar->cur_dirstr));
  1727. archive_string_free(&(xar->tstr));
  1728. archive_string_free(&(xar->vstr));
  1729. file_free_hardlinks(xar);
  1730. file_free_register(xar);
  1731. compression_end(&(a->archive), &(xar->stream));
  1732. free(xar);
  1733. return (ARCHIVE_OK);
  1734. }
  1735. static int
  1736. file_cmp_node(const struct archive_rb_node *n1,
  1737. const struct archive_rb_node *n2)
  1738. {
  1739. const struct file *f1 = (const struct file *)n1;
  1740. const struct file *f2 = (const struct file *)n2;
  1741. return (strcmp(f1->basename.s, f2->basename.s));
  1742. }
  1743. static int
  1744. file_cmp_key(const struct archive_rb_node *n, const void *key)
  1745. {
  1746. const struct file *f = (const struct file *)n;
  1747. return (strcmp(f->basename.s, (const char *)key));
  1748. }
  1749. static struct file *
  1750. file_new(struct archive_write *a, struct archive_entry *entry)
  1751. {
  1752. struct file *file;
  1753. static const struct archive_rb_tree_ops rb_ops = {
  1754. file_cmp_node, file_cmp_key
  1755. };
  1756. file = calloc(1, sizeof(*file));
  1757. if (file == NULL)
  1758. return (NULL);
  1759. if (entry != NULL)
  1760. file->entry = archive_entry_clone(entry);
  1761. else
  1762. file->entry = archive_entry_new2(&a->archive);
  1763. if (file->entry == NULL) {
  1764. free(file);
  1765. return (NULL);
  1766. }
  1767. __archive_rb_tree_init(&(file->rbtree), &rb_ops);
  1768. file->children.first = NULL;
  1769. file->children.last = &(file->children.first);
  1770. file->xattr.first = NULL;
  1771. file->xattr.last = &(file->xattr.first);
  1772. archive_string_init(&(file->parentdir));
  1773. archive_string_init(&(file->basename));
  1774. archive_string_init(&(file->symlink));
  1775. archive_string_init(&(file->script));
  1776. if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
  1777. file->dir = 1;
  1778. return (file);
  1779. }
  1780. static void
  1781. file_free(struct file *file)
  1782. {
  1783. struct heap_data *heap, *next_heap;
  1784. heap = file->xattr.first;
  1785. while (heap != NULL) {
  1786. next_heap = heap->next;
  1787. free(heap);
  1788. heap = next_heap;
  1789. }
  1790. archive_string_free(&(file->parentdir));
  1791. archive_string_free(&(file->basename));
  1792. archive_string_free(&(file->symlink));
  1793. archive_string_free(&(file->script));
  1794. archive_entry_free(file->entry);
  1795. free(file);
  1796. }
  1797. static struct file *
  1798. file_create_virtual_dir(struct archive_write *a, struct xar *xar,
  1799. const char *pathname)
  1800. {
  1801. struct file *file;
  1802. (void)xar; /* UNUSED */
  1803. file = file_new(a, NULL);
  1804. if (file == NULL)
  1805. return (NULL);
  1806. archive_entry_set_pathname(file->entry, pathname);
  1807. archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
  1808. file->dir = 1;
  1809. file->virtual = 1;
  1810. return (file);
  1811. }
  1812. static int
  1813. file_add_child_tail(struct file *parent, struct file *child)
  1814. {
  1815. if (!__archive_rb_tree_insert_node(
  1816. &(parent->rbtree), (struct archive_rb_node *)child))
  1817. return (0);
  1818. child->chnext = NULL;
  1819. *parent->children.last = child;
  1820. parent->children.last = &(child->chnext);
  1821. child->parent = parent;
  1822. return (1);
  1823. }
  1824. /*
  1825. * Find a entry from `parent'
  1826. */
  1827. static struct file *
  1828. file_find_child(struct file *parent, const char *child_name)
  1829. {
  1830. struct file *np;
  1831. np = (struct file *)__archive_rb_tree_find_node(
  1832. &(parent->rbtree), child_name);
  1833. return (np);
  1834. }
  1835. #if defined(_WIN32) || defined(__CYGWIN__)
  1836. static void
  1837. cleanup_backslash(char *utf8, size_t len)
  1838. {
  1839. /* Convert a path-separator from '\' to '/' */
  1840. while (*utf8 != '\0' && len) {
  1841. if (*utf8 == '\\')
  1842. *utf8 = '/';
  1843. ++utf8;
  1844. --len;
  1845. }
  1846. }
  1847. #else
  1848. #define cleanup_backslash(p, len) /* nop */
  1849. #endif
  1850. /*
  1851. * Generate a parent directory name and a base name from a pathname.
  1852. */
  1853. static int
  1854. file_gen_utility_names(struct archive_write *a, struct file *file)
  1855. {
  1856. struct xar *xar;
  1857. const char *pp;
  1858. char *p, *dirname, *slash;
  1859. size_t len;
  1860. int r = ARCHIVE_OK;
  1861. xar = (struct xar *)a->format_data;
  1862. archive_string_empty(&(file->parentdir));
  1863. archive_string_empty(&(file->basename));
  1864. archive_string_empty(&(file->symlink));
  1865. if (file->parent == file)/* virtual root */
  1866. return (ARCHIVE_OK);
  1867. if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
  1868. != 0) {
  1869. if (errno == ENOMEM) {
  1870. archive_set_error(&a->archive, ENOMEM,
  1871. "Can't allocate memory for Pathname");
  1872. return (ARCHIVE_FATAL);
  1873. }
  1874. archive_set_error(&a->archive,
  1875. ARCHIVE_ERRNO_FILE_FORMAT,
  1876. "Can't translate pathname '%s' to UTF-8",
  1877. archive_entry_pathname(file->entry));
  1878. r = ARCHIVE_WARN;
  1879. }
  1880. archive_strncpy(&(file->parentdir), pp, len);
  1881. len = file->parentdir.length;
  1882. p = dirname = file->parentdir.s;
  1883. /*
  1884. * Convert a path-separator from '\' to '/'
  1885. */
  1886. cleanup_backslash(p, len);
  1887. /*
  1888. * Remove leading '/', '../' and './' elements
  1889. */
  1890. while (*p) {
  1891. if (p[0] == '/') {
  1892. p++;
  1893. len--;
  1894. } else if (p[0] != '.')
  1895. break;
  1896. else if (p[1] == '.' && p[2] == '/') {
  1897. p += 3;
  1898. len -= 3;
  1899. } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
  1900. p += 2;
  1901. len -= 2;
  1902. } else if (p[1] == '\0') {
  1903. p++;
  1904. len--;
  1905. } else
  1906. break;
  1907. }
  1908. if (p != dirname) {
  1909. memmove(dirname, p, len+1);
  1910. p = dirname;
  1911. }
  1912. /*
  1913. * Remove "/","/." and "/.." elements from tail.
  1914. */
  1915. while (len > 0) {
  1916. size_t ll = len;
  1917. if (len > 0 && p[len-1] == '/') {
  1918. p[len-1] = '\0';
  1919. len--;
  1920. }
  1921. if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
  1922. p[len-2] = '\0';
  1923. len -= 2;
  1924. }
  1925. if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
  1926. p[len-1] == '.') {
  1927. p[len-3] = '\0';
  1928. len -= 3;
  1929. }
  1930. if (ll == len)
  1931. break;
  1932. }
  1933. while (*p) {
  1934. if (p[0] == '/') {
  1935. if (p[1] == '/')
  1936. /* Convert '//' --> '/' */
  1937. strcpy(p, p+1);
  1938. else if (p[1] == '.' && p[2] == '/')
  1939. /* Convert '/./' --> '/' */
  1940. strcpy(p, p+2);
  1941. else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
  1942. /* Convert 'dir/dir1/../dir2/'
  1943. * --> 'dir/dir2/'
  1944. */
  1945. char *rp = p -1;
  1946. while (rp >= dirname) {
  1947. if (*rp == '/')
  1948. break;
  1949. --rp;
  1950. }
  1951. if (rp > dirname) {
  1952. strcpy(rp, p+3);
  1953. p = rp;
  1954. } else {
  1955. strcpy(dirname, p+4);
  1956. p = dirname;
  1957. }
  1958. } else
  1959. p++;
  1960. } else
  1961. p++;
  1962. }
  1963. p = dirname;
  1964. len = strlen(p);
  1965. if (archive_entry_filetype(file->entry) == AE_IFLNK) {
  1966. size_t len2;
  1967. /* Convert symlink name too. */
  1968. if (archive_entry_symlink_l(file->entry, &pp, &len2,
  1969. xar->sconv) != 0) {
  1970. if (errno == ENOMEM) {
  1971. archive_set_error(&a->archive, ENOMEM,
  1972. "Can't allocate memory for Linkname");
  1973. return (ARCHIVE_FATAL);
  1974. }
  1975. archive_set_error(&a->archive,
  1976. ARCHIVE_ERRNO_FILE_FORMAT,
  1977. "Can't translate symlink '%s' to UTF-8",
  1978. archive_entry_symlink(file->entry));
  1979. r = ARCHIVE_WARN;
  1980. }
  1981. archive_strncpy(&(file->symlink), pp, len2);
  1982. cleanup_backslash(file->symlink.s, file->symlink.length);
  1983. }
  1984. /*
  1985. * - Count up directory elements.
  1986. * - Find out the position which points the last position of
  1987. * path separator('/').
  1988. */
  1989. slash = NULL;
  1990. for (; *p != '\0'; p++)
  1991. if (*p == '/')
  1992. slash = p;
  1993. if (slash == NULL) {
  1994. /* The pathname doesn't have a parent directory. */
  1995. file->parentdir.length = len;
  1996. archive_string_copy(&(file->basename), &(file->parentdir));
  1997. archive_string_empty(&(file->parentdir));
  1998. *file->parentdir.s = '\0';
  1999. return (r);
  2000. }
  2001. /* Make a basename from dirname and slash */
  2002. *slash = '\0';
  2003. file->parentdir.length = slash - dirname;
  2004. archive_strcpy(&(file->basename), slash + 1);
  2005. return (r);
  2006. }
  2007. static int
  2008. get_path_component(char *name, int n, const char *fn)
  2009. {
  2010. char *p;
  2011. int l;
  2012. p = strchr(fn, '/');
  2013. if (p == NULL) {
  2014. if ((l = strlen(fn)) == 0)
  2015. return (0);
  2016. } else
  2017. l = p - fn;
  2018. if (l > n -1)
  2019. return (-1);
  2020. memcpy(name, fn, l);
  2021. name[l] = '\0';
  2022. return (l);
  2023. }
  2024. /*
  2025. * Add a new entry into the tree.
  2026. */
  2027. static int
  2028. file_tree(struct archive_write *a, struct file **filepp)
  2029. {
  2030. #if defined(_WIN32) && !defined(__CYGWIN__)
  2031. char name[_MAX_FNAME];/* Included null terminator size. */
  2032. #elif defined(NAME_MAX) && NAME_MAX >= 255
  2033. char name[NAME_MAX+1];
  2034. #else
  2035. char name[256];
  2036. #endif
  2037. struct xar *xar = (struct xar *)a->format_data;
  2038. struct file *dent, *file, *np;
  2039. struct archive_entry *ent;
  2040. const char *fn, *p;
  2041. int l;
  2042. file = *filepp;
  2043. dent = xar->root;
  2044. if (file->parentdir.length > 0)
  2045. fn = p = file->parentdir.s;
  2046. else
  2047. fn = p = "";
  2048. /*
  2049. * If the path of the parent directory of `file' entry is
  2050. * the same as the path of `cur_dirent', add isoent to
  2051. * `cur_dirent'.
  2052. */
  2053. if (archive_strlen(&(xar->cur_dirstr))
  2054. == archive_strlen(&(file->parentdir)) &&
  2055. strcmp(xar->cur_dirstr.s, fn) == 0) {
  2056. if (!file_add_child_tail(xar->cur_dirent, file)) {
  2057. np = (struct file *)__archive_rb_tree_find_node(
  2058. &(xar->cur_dirent->rbtree),
  2059. file->basename.s);
  2060. goto same_entry;
  2061. }
  2062. return (ARCHIVE_OK);
  2063. }
  2064. for (;;) {
  2065. l = get_path_component(name, sizeof(name), fn);
  2066. if (l == 0) {
  2067. np = NULL;
  2068. break;
  2069. }
  2070. if (l < 0) {
  2071. archive_set_error(&a->archive,
  2072. ARCHIVE_ERRNO_MISC,
  2073. "A name buffer is too small");
  2074. file_free(file);
  2075. *filepp = NULL;
  2076. return (ARCHIVE_FATAL);
  2077. }
  2078. np = file_find_child(dent, name);
  2079. if (np == NULL || fn[0] == '\0')
  2080. break;
  2081. /* Find next subdirectory. */
  2082. if (!np->dir) {
  2083. /* NOT Directory! */
  2084. archive_set_error(&a->archive,
  2085. ARCHIVE_ERRNO_MISC,
  2086. "`%s' is not directory, we cannot insert `%s' ",
  2087. archive_entry_pathname(np->entry),
  2088. archive_entry_pathname(file->entry));
  2089. file_free(file);
  2090. *filepp = NULL;
  2091. return (ARCHIVE_FAILED);
  2092. }
  2093. fn += l;
  2094. if (fn[0] == '/')
  2095. fn++;
  2096. dent = np;
  2097. }
  2098. if (np == NULL) {
  2099. /*
  2100. * Create virtual parent directories.
  2101. */
  2102. while (fn[0] != '\0') {
  2103. struct file *vp;
  2104. struct archive_string as;
  2105. archive_string_init(&as);
  2106. archive_strncat(&as, p, fn - p + l);
  2107. if (as.s[as.length-1] == '/') {
  2108. as.s[as.length-1] = '\0';
  2109. as.length--;
  2110. }
  2111. vp = file_create_virtual_dir(a, xar, as.s);
  2112. if (vp == NULL) {
  2113. archive_string_free(&as);
  2114. archive_set_error(&a->archive, ENOMEM,
  2115. "Can't allocate memory");
  2116. file_free(file);
  2117. *filepp = NULL;
  2118. return (ARCHIVE_FATAL);
  2119. }
  2120. archive_string_free(&as);
  2121. if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
  2122. return (ARCHIVE_FATAL);
  2123. file_add_child_tail(dent, vp);
  2124. file_register(xar, vp);
  2125. np = vp;
  2126. fn += l;
  2127. if (fn[0] == '/')
  2128. fn++;
  2129. l = get_path_component(name, sizeof(name), fn);
  2130. if (l < 0) {
  2131. archive_string_free(&as);
  2132. archive_set_error(&a->archive,
  2133. ARCHIVE_ERRNO_MISC,
  2134. "A name buffer is too small");
  2135. file_free(file);
  2136. *filepp = NULL;
  2137. return (ARCHIVE_FATAL);
  2138. }
  2139. dent = np;
  2140. }
  2141. /* Found out the parent directory where isoent can be
  2142. * inserted. */
  2143. xar->cur_dirent = dent;
  2144. archive_string_empty(&(xar->cur_dirstr));
  2145. archive_string_ensure(&(xar->cur_dirstr),
  2146. archive_strlen(&(dent->parentdir)) +
  2147. archive_strlen(&(dent->basename)) + 2);
  2148. if (archive_strlen(&(dent->parentdir)) +
  2149. archive_strlen(&(dent->basename)) == 0)
  2150. xar->cur_dirstr.s[0] = 0;
  2151. else {
  2152. if (archive_strlen(&(dent->parentdir)) > 0) {
  2153. archive_string_copy(&(xar->cur_dirstr),
  2154. &(dent->parentdir));
  2155. archive_strappend_char(&(xar->cur_dirstr), '/');
  2156. }
  2157. archive_string_concat(&(xar->cur_dirstr),
  2158. &(dent->basename));
  2159. }
  2160. if (!file_add_child_tail(dent, file)) {
  2161. np = (struct file *)__archive_rb_tree_find_node(
  2162. &(dent->rbtree), file->basename.s);
  2163. goto same_entry;
  2164. }
  2165. return (ARCHIVE_OK);
  2166. }
  2167. same_entry:
  2168. /*
  2169. * We have already has the entry the filename of which is
  2170. * the same.
  2171. */
  2172. if (archive_entry_filetype(np->entry) !=
  2173. archive_entry_filetype(file->entry)) {
  2174. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2175. "Found duplicate entries `%s' and its file type is "
  2176. "different",
  2177. archive_entry_pathname(np->entry));
  2178. file_free(file);
  2179. *filepp = NULL;
  2180. return (ARCHIVE_FAILED);
  2181. }
  2182. /* Swap files. */
  2183. ent = np->entry;
  2184. np->entry = file->entry;
  2185. file->entry = ent;
  2186. np->virtual = 0;
  2187. file_free(file);
  2188. *filepp = np;
  2189. return (ARCHIVE_OK);
  2190. }
  2191. static void
  2192. file_register(struct xar *xar, struct file *file)
  2193. {
  2194. file->id = xar->file_idx++;
  2195. file->next = NULL;
  2196. *xar->file_list.last = file;
  2197. xar->file_list.last = &(file->next);
  2198. }
  2199. static void
  2200. file_init_register(struct xar *xar)
  2201. {
  2202. xar->file_list.first = NULL;
  2203. xar->file_list.last = &(xar->file_list.first);
  2204. }
  2205. static void
  2206. file_free_register(struct xar *xar)
  2207. {
  2208. struct file *file, *file_next;
  2209. file = xar->file_list.first;
  2210. while (file != NULL) {
  2211. file_next = file->next;
  2212. file_free(file);
  2213. file = file_next;
  2214. }
  2215. }
  2216. /*
  2217. * Register entry to get a hardlink target.
  2218. */
  2219. static int
  2220. file_register_hardlink(struct archive_write *a, struct file *file)
  2221. {
  2222. struct xar *xar = (struct xar *)a->format_data;
  2223. struct hardlink *hl;
  2224. const char *pathname;
  2225. archive_entry_set_nlink(file->entry, 1);
  2226. pathname = archive_entry_hardlink(file->entry);
  2227. if (pathname == NULL) {
  2228. /* This `file` is a hardlink target. */
  2229. hl = malloc(sizeof(*hl));
  2230. if (hl == NULL) {
  2231. archive_set_error(&a->archive, ENOMEM,
  2232. "Can't allocate memory");
  2233. return (ARCHIVE_FATAL);
  2234. }
  2235. hl->nlink = 1;
  2236. /* A hardlink target must be the first position. */
  2237. file->hlnext = NULL;
  2238. hl->file_list.first = file;
  2239. hl->file_list.last = &(file->hlnext);
  2240. __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
  2241. (struct archive_rb_node *)hl);
  2242. } else {
  2243. hl = (struct hardlink *)__archive_rb_tree_find_node(
  2244. &(xar->hardlink_rbtree), pathname);
  2245. if (hl != NULL) {
  2246. /* Insert `file` entry into the tail. */
  2247. file->hlnext = NULL;
  2248. *hl->file_list.last = file;
  2249. hl->file_list.last = &(file->hlnext);
  2250. hl->nlink++;
  2251. }
  2252. archive_entry_unset_size(file->entry);
  2253. }
  2254. return (ARCHIVE_OK);
  2255. }
  2256. /*
  2257. * Hardlinked files have to have the same location of extent.
  2258. * We have to find out hardlink target entries for entries which
  2259. * have a hardlink target name.
  2260. */
  2261. static void
  2262. file_connect_hardlink_files(struct xar *xar)
  2263. {
  2264. struct archive_rb_node *n;
  2265. struct hardlink *hl;
  2266. struct file *target, *nf;
  2267. ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
  2268. hl = (struct hardlink *)n;
  2269. /* The first entry must be a hardlink target. */
  2270. target = hl->file_list.first;
  2271. archive_entry_set_nlink(target->entry, hl->nlink);
  2272. if (hl->nlink > 1)
  2273. /* It means this file is a hardlink
  2274. * target itself. */
  2275. target->hardlink_target = target;
  2276. for (nf = target->hlnext;
  2277. nf != NULL; nf = nf->hlnext) {
  2278. nf->hardlink_target = target;
  2279. archive_entry_set_nlink(nf->entry, hl->nlink);
  2280. }
  2281. }
  2282. }
  2283. static int
  2284. file_hd_cmp_node(const struct archive_rb_node *n1,
  2285. const struct archive_rb_node *n2)
  2286. {
  2287. const struct hardlink *h1 = (const struct hardlink *)n1;
  2288. const struct hardlink *h2 = (const struct hardlink *)n2;
  2289. return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
  2290. archive_entry_pathname(h2->file_list.first->entry)));
  2291. }
  2292. static int
  2293. file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
  2294. {
  2295. const struct hardlink *h = (const struct hardlink *)n;
  2296. return (strcmp(archive_entry_pathname(h->file_list.first->entry),
  2297. (const char *)key));
  2298. }
  2299. static void
  2300. file_init_hardlinks(struct xar *xar)
  2301. {
  2302. static const struct archive_rb_tree_ops rb_ops = {
  2303. file_hd_cmp_node, file_hd_cmp_key,
  2304. };
  2305. __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
  2306. }
  2307. static void
  2308. file_free_hardlinks(struct xar *xar)
  2309. {
  2310. struct archive_rb_node *n, *next;
  2311. for (n = ARCHIVE_RB_TREE_MIN(&(xar->hardlink_rbtree)); n;) {
  2312. next = __archive_rb_tree_iterate(&(xar->hardlink_rbtree),
  2313. n, ARCHIVE_RB_DIR_RIGHT);
  2314. free(n);
  2315. n = next;
  2316. }
  2317. }
  2318. static void
  2319. checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
  2320. {
  2321. sumwrk->alg = sum_alg;
  2322. switch (sum_alg) {
  2323. case CKSUM_NONE:
  2324. break;
  2325. case CKSUM_SHA1:
  2326. archive_sha1_init(&(sumwrk->sha1ctx));
  2327. break;
  2328. case CKSUM_MD5:
  2329. archive_md5_init(&(sumwrk->md5ctx));
  2330. break;
  2331. }
  2332. }
  2333. static void
  2334. checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
  2335. {
  2336. switch (sumwrk->alg) {
  2337. case CKSUM_NONE:
  2338. break;
  2339. case CKSUM_SHA1:
  2340. archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
  2341. break;
  2342. case CKSUM_MD5:
  2343. archive_md5_update(&(sumwrk->md5ctx), buff, size);
  2344. break;
  2345. }
  2346. }
  2347. static void
  2348. checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
  2349. {
  2350. switch (sumwrk->alg) {
  2351. case CKSUM_NONE:
  2352. sumval->len = 0;
  2353. break;
  2354. case CKSUM_SHA1:
  2355. archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
  2356. sumval->len = SHA1_SIZE;
  2357. break;
  2358. case CKSUM_MD5:
  2359. archive_md5_final(&(sumwrk->md5ctx), sumval->val);
  2360. sumval->len = MD5_SIZE;
  2361. break;
  2362. }
  2363. sumval->alg = sumwrk->alg;
  2364. }
  2365. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
  2366. static int
  2367. compression_unsupported_encoder(struct archive *a,
  2368. struct la_zstream *lastrm, const char *name)
  2369. {
  2370. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2371. "%s compression not supported on this platform", name);
  2372. lastrm->valid = 0;
  2373. lastrm->real_stream = NULL;
  2374. return (ARCHIVE_FAILED);
  2375. }
  2376. #endif
  2377. static int
  2378. compression_init_encoder_gzip(struct archive *a,
  2379. struct la_zstream *lastrm, int level, int withheader)
  2380. {
  2381. z_stream *strm;
  2382. if (lastrm->valid)
  2383. compression_end(a, lastrm);
  2384. strm = calloc(1, sizeof(*strm));
  2385. if (strm == NULL) {
  2386. archive_set_error(a, ENOMEM,
  2387. "Can't allocate memory for gzip stream");
  2388. return (ARCHIVE_FATAL);
  2389. }
  2390. /* zlib.h is not const-correct, so we need this one bit
  2391. * of ugly hackery to convert a const * pointer to
  2392. * a non-const pointer. */
  2393. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  2394. strm->avail_in = lastrm->avail_in;
  2395. strm->total_in = (uLong)lastrm->total_in;
  2396. strm->next_out = lastrm->next_out;
  2397. strm->avail_out = lastrm->avail_out;
  2398. strm->total_out = (uLong)lastrm->total_out;
  2399. if (deflateInit2(strm, level, Z_DEFLATED,
  2400. (withheader)?15:-15,
  2401. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  2402. free(strm);
  2403. lastrm->real_stream = NULL;
  2404. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2405. "Internal error initializing compression library");
  2406. return (ARCHIVE_FATAL);
  2407. }
  2408. lastrm->real_stream = strm;
  2409. lastrm->valid = 1;
  2410. lastrm->code = compression_code_gzip;
  2411. lastrm->end = compression_end_gzip;
  2412. return (ARCHIVE_OK);
  2413. }
  2414. static int
  2415. compression_code_gzip(struct archive *a,
  2416. struct la_zstream *lastrm, enum la_zaction action)
  2417. {
  2418. z_stream *strm;
  2419. int r;
  2420. strm = (z_stream *)lastrm->real_stream;
  2421. /* zlib.h is not const-correct, so we need this one bit
  2422. * of ugly hackery to convert a const * pointer to
  2423. * a non-const pointer. */
  2424. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  2425. strm->avail_in = lastrm->avail_in;
  2426. strm->total_in = (uLong)lastrm->total_in;
  2427. strm->next_out = lastrm->next_out;
  2428. strm->avail_out = lastrm->avail_out;
  2429. strm->total_out = (uLong)lastrm->total_out;
  2430. r = deflate(strm,
  2431. (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
  2432. lastrm->next_in = strm->next_in;
  2433. lastrm->avail_in = strm->avail_in;
  2434. lastrm->total_in = strm->total_in;
  2435. lastrm->next_out = strm->next_out;
  2436. lastrm->avail_out = strm->avail_out;
  2437. lastrm->total_out = strm->total_out;
  2438. switch (r) {
  2439. case Z_OK:
  2440. return (ARCHIVE_OK);
  2441. case Z_STREAM_END:
  2442. return (ARCHIVE_EOF);
  2443. default:
  2444. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2445. "GZip compression failed:"
  2446. " deflate() call returned status %d", r);
  2447. return (ARCHIVE_FATAL);
  2448. }
  2449. }
  2450. static int
  2451. compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
  2452. {
  2453. z_stream *strm;
  2454. int r;
  2455. strm = (z_stream *)lastrm->real_stream;
  2456. r = deflateEnd(strm);
  2457. free(strm);
  2458. lastrm->real_stream = NULL;
  2459. lastrm->valid = 0;
  2460. if (r != Z_OK) {
  2461. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2462. "Failed to clean up compressor");
  2463. return (ARCHIVE_FATAL);
  2464. }
  2465. return (ARCHIVE_OK);
  2466. }
  2467. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  2468. static int
  2469. compression_init_encoder_bzip2(struct archive *a,
  2470. struct la_zstream *lastrm, int level)
  2471. {
  2472. bz_stream *strm;
  2473. if (lastrm->valid)
  2474. compression_end(a, lastrm);
  2475. strm = calloc(1, sizeof(*strm));
  2476. if (strm == NULL) {
  2477. archive_set_error(a, ENOMEM,
  2478. "Can't allocate memory for bzip2 stream");
  2479. return (ARCHIVE_FATAL);
  2480. }
  2481. /* bzlib.h is not const-correct, so we need this one bit
  2482. * of ugly hackery to convert a const * pointer to
  2483. * a non-const pointer. */
  2484. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  2485. strm->avail_in = lastrm->avail_in;
  2486. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  2487. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  2488. strm->next_out = (char *)lastrm->next_out;
  2489. strm->avail_out = lastrm->avail_out;
  2490. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  2491. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  2492. if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
  2493. free(strm);
  2494. lastrm->real_stream = NULL;
  2495. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2496. "Internal error initializing compression library");
  2497. return (ARCHIVE_FATAL);
  2498. }
  2499. lastrm->real_stream = strm;
  2500. lastrm->valid = 1;
  2501. lastrm->code = compression_code_bzip2;
  2502. lastrm->end = compression_end_bzip2;
  2503. return (ARCHIVE_OK);
  2504. }
  2505. static int
  2506. compression_code_bzip2(struct archive *a,
  2507. struct la_zstream *lastrm, enum la_zaction action)
  2508. {
  2509. bz_stream *strm;
  2510. int r;
  2511. strm = (bz_stream *)lastrm->real_stream;
  2512. /* bzlib.h is not const-correct, so we need this one bit
  2513. * of ugly hackery to convert a const * pointer to
  2514. * a non-const pointer. */
  2515. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  2516. strm->avail_in = lastrm->avail_in;
  2517. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  2518. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  2519. strm->next_out = (char *)lastrm->next_out;
  2520. strm->avail_out = lastrm->avail_out;
  2521. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  2522. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  2523. r = BZ2_bzCompress(strm,
  2524. (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
  2525. lastrm->next_in = (const unsigned char *)strm->next_in;
  2526. lastrm->avail_in = strm->avail_in;
  2527. lastrm->total_in =
  2528. (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
  2529. + (uint64_t)(uint32_t)strm->total_in_lo32;
  2530. lastrm->next_out = (unsigned char *)strm->next_out;
  2531. lastrm->avail_out = strm->avail_out;
  2532. lastrm->total_out =
  2533. (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
  2534. + (uint64_t)(uint32_t)strm->total_out_lo32;
  2535. switch (r) {
  2536. case BZ_RUN_OK: /* Non-finishing */
  2537. case BZ_FINISH_OK: /* Finishing: There's more work to do */
  2538. return (ARCHIVE_OK);
  2539. case BZ_STREAM_END: /* Finishing: all done */
  2540. /* Only occurs in finishing case */
  2541. return (ARCHIVE_EOF);
  2542. default:
  2543. /* Any other return value indicates an error */
  2544. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2545. "Bzip2 compression failed:"
  2546. " BZ2_bzCompress() call returned status %d", r);
  2547. return (ARCHIVE_FATAL);
  2548. }
  2549. }
  2550. static int
  2551. compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
  2552. {
  2553. bz_stream *strm;
  2554. int r;
  2555. strm = (bz_stream *)lastrm->real_stream;
  2556. r = BZ2_bzCompressEnd(strm);
  2557. free(strm);
  2558. lastrm->real_stream = NULL;
  2559. lastrm->valid = 0;
  2560. if (r != BZ_OK) {
  2561. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2562. "Failed to clean up compressor");
  2563. return (ARCHIVE_FATAL);
  2564. }
  2565. return (ARCHIVE_OK);
  2566. }
  2567. #else
  2568. static int
  2569. compression_init_encoder_bzip2(struct archive *a,
  2570. struct la_zstream *lastrm, int level)
  2571. {
  2572. (void) level; /* UNUSED */
  2573. if (lastrm->valid)
  2574. compression_end(a, lastrm);
  2575. return (compression_unsupported_encoder(a, lastrm, "bzip2"));
  2576. }
  2577. #endif
  2578. #if defined(HAVE_LZMA_H)
  2579. static int
  2580. compression_init_encoder_lzma(struct archive *a,
  2581. struct la_zstream *lastrm, int level)
  2582. {
  2583. static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
  2584. lzma_stream *strm;
  2585. lzma_options_lzma lzma_opt;
  2586. int r;
  2587. if (lastrm->valid)
  2588. compression_end(a, lastrm);
  2589. if (lzma_lzma_preset(&lzma_opt, level)) {
  2590. lastrm->real_stream = NULL;
  2591. archive_set_error(a, ENOMEM,
  2592. "Internal error initializing compression library");
  2593. return (ARCHIVE_FATAL);
  2594. }
  2595. strm = calloc(1, sizeof(*strm));
  2596. if (strm == NULL) {
  2597. archive_set_error(a, ENOMEM,
  2598. "Can't allocate memory for lzma stream");
  2599. return (ARCHIVE_FATAL);
  2600. }
  2601. *strm = lzma_init_data;
  2602. r = lzma_alone_encoder(strm, &lzma_opt);
  2603. switch (r) {
  2604. case LZMA_OK:
  2605. lastrm->real_stream = strm;
  2606. lastrm->valid = 1;
  2607. lastrm->code = compression_code_lzma;
  2608. lastrm->end = compression_end_lzma;
  2609. r = ARCHIVE_OK;
  2610. break;
  2611. case LZMA_MEM_ERROR:
  2612. free(strm);
  2613. lastrm->real_stream = NULL;
  2614. archive_set_error(a, ENOMEM,
  2615. "Internal error initializing compression library: "
  2616. "Cannot allocate memory");
  2617. r = ARCHIVE_FATAL;
  2618. break;
  2619. default:
  2620. free(strm);
  2621. lastrm->real_stream = NULL;
  2622. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2623. "Internal error initializing compression library: "
  2624. "It's a bug in liblzma");
  2625. r = ARCHIVE_FATAL;
  2626. break;
  2627. }
  2628. return (r);
  2629. }
  2630. static int
  2631. compression_init_encoder_xz(struct archive *a,
  2632. struct la_zstream *lastrm, int level, int threads)
  2633. {
  2634. static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
  2635. lzma_stream *strm;
  2636. lzma_filter *lzmafilters;
  2637. lzma_options_lzma lzma_opt;
  2638. int r;
  2639. #ifdef HAVE_LZMA_STREAM_ENCODER_MT
  2640. lzma_mt mt_options;
  2641. #endif
  2642. (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
  2643. if (lastrm->valid)
  2644. compression_end(a, lastrm);
  2645. strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
  2646. if (strm == NULL) {
  2647. archive_set_error(a, ENOMEM,
  2648. "Can't allocate memory for xz stream");
  2649. return (ARCHIVE_FATAL);
  2650. }
  2651. lzmafilters = (lzma_filter *)(strm+1);
  2652. if (level > 6)
  2653. level = 6;
  2654. if (lzma_lzma_preset(&lzma_opt, level)) {
  2655. free(strm);
  2656. lastrm->real_stream = NULL;
  2657. archive_set_error(a, ENOMEM,
  2658. "Internal error initializing compression library");
  2659. return (ARCHIVE_FATAL);
  2660. }
  2661. lzmafilters[0].id = LZMA_FILTER_LZMA2;
  2662. lzmafilters[0].options = &lzma_opt;
  2663. lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
  2664. *strm = lzma_init_data;
  2665. #ifdef HAVE_LZMA_STREAM_ENCODER_MT
  2666. if (threads > 1) {
  2667. memset(&mt_options, 0, sizeof(mt_options));
  2668. mt_options.threads = threads;
  2669. mt_options.timeout = 300;
  2670. mt_options.filters = lzmafilters;
  2671. mt_options.check = LZMA_CHECK_CRC64;
  2672. r = lzma_stream_encoder_mt(strm, &mt_options);
  2673. } else
  2674. #endif
  2675. r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
  2676. switch (r) {
  2677. case LZMA_OK:
  2678. lastrm->real_stream = strm;
  2679. lastrm->valid = 1;
  2680. lastrm->code = compression_code_lzma;
  2681. lastrm->end = compression_end_lzma;
  2682. r = ARCHIVE_OK;
  2683. break;
  2684. case LZMA_MEM_ERROR:
  2685. free(strm);
  2686. lastrm->real_stream = NULL;
  2687. archive_set_error(a, ENOMEM,
  2688. "Internal error initializing compression library: "
  2689. "Cannot allocate memory");
  2690. r = ARCHIVE_FATAL;
  2691. break;
  2692. default:
  2693. free(strm);
  2694. lastrm->real_stream = NULL;
  2695. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2696. "Internal error initializing compression library: "
  2697. "It's a bug in liblzma");
  2698. r = ARCHIVE_FATAL;
  2699. break;
  2700. }
  2701. return (r);
  2702. }
  2703. static int
  2704. compression_code_lzma(struct archive *a,
  2705. struct la_zstream *lastrm, enum la_zaction action)
  2706. {
  2707. lzma_stream *strm;
  2708. int r;
  2709. strm = (lzma_stream *)lastrm->real_stream;
  2710. strm->next_in = lastrm->next_in;
  2711. strm->avail_in = lastrm->avail_in;
  2712. strm->total_in = lastrm->total_in;
  2713. strm->next_out = lastrm->next_out;
  2714. strm->avail_out = lastrm->avail_out;
  2715. strm->total_out = lastrm->total_out;
  2716. r = lzma_code(strm,
  2717. (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
  2718. lastrm->next_in = strm->next_in;
  2719. lastrm->avail_in = strm->avail_in;
  2720. lastrm->total_in = strm->total_in;
  2721. lastrm->next_out = strm->next_out;
  2722. lastrm->avail_out = strm->avail_out;
  2723. lastrm->total_out = strm->total_out;
  2724. switch (r) {
  2725. case LZMA_OK:
  2726. /* Non-finishing case */
  2727. return (ARCHIVE_OK);
  2728. case LZMA_STREAM_END:
  2729. /* This return can only occur in finishing case. */
  2730. return (ARCHIVE_EOF);
  2731. case LZMA_MEMLIMIT_ERROR:
  2732. archive_set_error(a, ENOMEM,
  2733. "lzma compression error:"
  2734. " %ju MiB would have been needed",
  2735. (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
  2736. / (1024 * 1024)));
  2737. return (ARCHIVE_FATAL);
  2738. default:
  2739. /* Any other return value indicates an error */
  2740. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  2741. "lzma compression failed:"
  2742. " lzma_code() call returned status %d", r);
  2743. return (ARCHIVE_FATAL);
  2744. }
  2745. }
  2746. static int
  2747. compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
  2748. {
  2749. lzma_stream *strm;
  2750. (void)a; /* UNUSED */
  2751. strm = (lzma_stream *)lastrm->real_stream;
  2752. lzma_end(strm);
  2753. free(strm);
  2754. lastrm->valid = 0;
  2755. lastrm->real_stream = NULL;
  2756. return (ARCHIVE_OK);
  2757. }
  2758. #else
  2759. static int
  2760. compression_init_encoder_lzma(struct archive *a,
  2761. struct la_zstream *lastrm, int level)
  2762. {
  2763. (void) level; /* UNUSED */
  2764. if (lastrm->valid)
  2765. compression_end(a, lastrm);
  2766. return (compression_unsupported_encoder(a, lastrm, "lzma"));
  2767. }
  2768. static int
  2769. compression_init_encoder_xz(struct archive *a,
  2770. struct la_zstream *lastrm, int level, int threads)
  2771. {
  2772. (void) level; /* UNUSED */
  2773. (void) threads; /* UNUSED */
  2774. if (lastrm->valid)
  2775. compression_end(a, lastrm);
  2776. return (compression_unsupported_encoder(a, lastrm, "xz"));
  2777. }
  2778. #endif
  2779. static int
  2780. xar_compression_init_encoder(struct archive_write *a)
  2781. {
  2782. struct xar *xar;
  2783. int r;
  2784. xar = (struct xar *)a->format_data;
  2785. switch (xar->opt_compression) {
  2786. case GZIP:
  2787. r = compression_init_encoder_gzip(
  2788. &(a->archive), &(xar->stream),
  2789. xar->opt_compression_level, 1);
  2790. break;
  2791. case BZIP2:
  2792. r = compression_init_encoder_bzip2(
  2793. &(a->archive), &(xar->stream),
  2794. xar->opt_compression_level);
  2795. break;
  2796. case LZMA:
  2797. r = compression_init_encoder_lzma(
  2798. &(a->archive), &(xar->stream),
  2799. xar->opt_compression_level);
  2800. break;
  2801. case XZ:
  2802. r = compression_init_encoder_xz(
  2803. &(a->archive), &(xar->stream),
  2804. xar->opt_compression_level, xar->opt_threads);
  2805. break;
  2806. default:
  2807. r = ARCHIVE_OK;
  2808. break;
  2809. }
  2810. if (r == ARCHIVE_OK) {
  2811. xar->stream.total_in = 0;
  2812. xar->stream.next_out = xar->wbuff;
  2813. xar->stream.avail_out = sizeof(xar->wbuff);
  2814. xar->stream.total_out = 0;
  2815. }
  2816. return (r);
  2817. }
  2818. static int
  2819. compression_code(struct archive *a, struct la_zstream *lastrm,
  2820. enum la_zaction action)
  2821. {
  2822. if (lastrm->valid)
  2823. return (lastrm->code(a, lastrm, action));
  2824. return (ARCHIVE_OK);
  2825. }
  2826. static int
  2827. compression_end(struct archive *a, struct la_zstream *lastrm)
  2828. {
  2829. if (lastrm->valid)
  2830. return (lastrm->end(a, lastrm));
  2831. return (ARCHIVE_OK);
  2832. }
  2833. static int
  2834. save_xattrs(struct archive_write *a, struct file *file)
  2835. {
  2836. struct xar *xar;
  2837. const char *name;
  2838. const void *value;
  2839. struct heap_data *heap;
  2840. size_t size;
  2841. int count, r;
  2842. xar = (struct xar *)a->format_data;
  2843. count = archive_entry_xattr_reset(file->entry);
  2844. if (count == 0)
  2845. return (ARCHIVE_OK);
  2846. while (count--) {
  2847. archive_entry_xattr_next(file->entry,
  2848. &name, &value, &size);
  2849. checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
  2850. checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
  2851. heap = calloc(1, sizeof(*heap));
  2852. if (heap == NULL) {
  2853. archive_set_error(&a->archive, ENOMEM,
  2854. "Can't allocate memory for xattr");
  2855. return (ARCHIVE_FATAL);
  2856. }
  2857. heap->id = file->ea_idx++;
  2858. heap->temp_offset = xar->temp_offset;
  2859. heap->size = size;/* save a extracted size */
  2860. heap->compression = xar->opt_compression;
  2861. /* Get a extracted sumcheck value. */
  2862. checksum_update(&(xar->e_sumwrk), value, size);
  2863. checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
  2864. /*
  2865. * Not compression to xattr is simple way.
  2866. */
  2867. if (heap->compression == NONE) {
  2868. checksum_update(&(xar->a_sumwrk), value, size);
  2869. checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
  2870. if (write_to_temp(a, value, size)
  2871. != ARCHIVE_OK) {
  2872. free(heap);
  2873. return (ARCHIVE_FATAL);
  2874. }
  2875. heap->length = size;
  2876. /* Add heap to the tail of file->xattr. */
  2877. heap->next = NULL;
  2878. *file->xattr.last = heap;
  2879. file->xattr.last = &(heap->next);
  2880. /* Next xattr */
  2881. continue;
  2882. }
  2883. /*
  2884. * Init compression library.
  2885. */
  2886. r = xar_compression_init_encoder(a);
  2887. if (r != ARCHIVE_OK) {
  2888. free(heap);
  2889. return (ARCHIVE_FATAL);
  2890. }
  2891. xar->stream.next_in = (const unsigned char *)value;
  2892. xar->stream.avail_in = size;
  2893. for (;;) {
  2894. r = compression_code(&(a->archive),
  2895. &(xar->stream), ARCHIVE_Z_FINISH);
  2896. if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
  2897. free(heap);
  2898. return (ARCHIVE_FATAL);
  2899. }
  2900. size = sizeof(xar->wbuff) - xar->stream.avail_out;
  2901. checksum_update(&(xar->a_sumwrk),
  2902. xar->wbuff, size);
  2903. if (write_to_temp(a, xar->wbuff, size)
  2904. != ARCHIVE_OK)
  2905. return (ARCHIVE_FATAL);
  2906. if (r == ARCHIVE_OK) {
  2907. xar->stream.next_out = xar->wbuff;
  2908. xar->stream.avail_out = sizeof(xar->wbuff);
  2909. } else {
  2910. checksum_final(&(xar->a_sumwrk),
  2911. &(heap->a_sum));
  2912. heap->length = xar->stream.total_out;
  2913. /* Add heap to the tail of file->xattr. */
  2914. heap->next = NULL;
  2915. *file->xattr.last = heap;
  2916. file->xattr.last = &(heap->next);
  2917. break;
  2918. }
  2919. }
  2920. /* Clean up compression library. */
  2921. r = compression_end(&(a->archive), &(xar->stream));
  2922. if (r != ARCHIVE_OK)
  2923. return (ARCHIVE_FATAL);
  2924. }
  2925. return (ARCHIVE_OK);
  2926. }
  2927. static int
  2928. getalgsize(enum sumalg sumalg)
  2929. {
  2930. switch (sumalg) {
  2931. default:
  2932. case CKSUM_NONE:
  2933. return (0);
  2934. case CKSUM_SHA1:
  2935. return (SHA1_SIZE);
  2936. case CKSUM_MD5:
  2937. return (MD5_SIZE);
  2938. }
  2939. }
  2940. static const char *
  2941. getalgname(enum sumalg sumalg)
  2942. {
  2943. switch (sumalg) {
  2944. default:
  2945. case CKSUM_NONE:
  2946. return (NULL);
  2947. case CKSUM_SHA1:
  2948. return (SHA1_NAME);
  2949. case CKSUM_MD5:
  2950. return (MD5_NAME);
  2951. }
  2952. }
  2953. #endif /* Support xar format */