archive_write_set_format_xar.c 89 KB

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