archive_write_set_format_xar.c 80 KB

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