archive_write_set_format_xar.c 79 KB

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