cmCPackTGZGenerator.cxx 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackTGZGenerator.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmSystemTools.h"
  18. #include "cmMakefile.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include <cmsys/SystemTools.hxx>
  21. #include <cmzlib/zlib.h>
  22. #include <memory> // auto_ptr
  23. #include <fcntl.h> // O_WRONLY, O_CREAT
  24. //----------------------------------------------------------------------
  25. cmCPackTGZGenerator::cmCPackTGZGenerator()
  26. {
  27. }
  28. //----------------------------------------------------------------------
  29. cmCPackTGZGenerator::~cmCPackTGZGenerator()
  30. {
  31. }
  32. //----------------------------------------------------------------------
  33. int cmCPackTGZGenerator::ProcessGenerator()
  34. {
  35. return this->Superclass::ProcessGenerator();
  36. }
  37. //----------------------------------------------------------------------
  38. int cmCPackTGZGenerator::Initialize(const char* name)
  39. {
  40. return this->Superclass::Initialize(name);
  41. }
  42. //----------------------------------------------------------------------
  43. // The following code is modified version of bsdtar
  44. /*-
  45. * Copyright (c) 2003-2004 Tim Kientzle
  46. * All rights reserved.
  47. *
  48. * Redistribution and use in source and binary forms, with or without
  49. * modification, are permitted provided that the following conditions
  50. * are met:
  51. * 1. Redistributions of source code must retain the above copyright
  52. * notice, this list of conditions and the following disclaimer
  53. * in this position and unchanged.
  54. * 2. Redistributions in binary form must reproduce the above copyright
  55. * notice, this list of conditions and the following disclaimer in the
  56. * documentation and/or other materials provided with the distribution.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  59. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  60. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  61. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  62. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  63. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  64. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  65. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  66. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  67. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68. */
  69. #if defined(HAVE_IO_H)
  70. # include <io.h>
  71. #endif
  72. #if defined(HAVE_UNISTD_H)
  73. # include <unistd.h> // for geteuid
  74. #endif
  75. #if defined(HAVE_INTTYPES_H)
  76. # include <inttypes.h> // for uintptr_t
  77. #endif
  78. #include <sys/types.h>
  79. #include <errno.h> // for ENOMEM
  80. #include <sys/stat.h> // for struct stat
  81. #include <time.h> // for time
  82. #if defined(WIN32) && !defined(__CYGWIN__)
  83. # ifndef S_ISREG
  84. # define S_ISREG(x) ((x) & _S_IFREG)
  85. # endif
  86. # ifndef S_ISLNK
  87. # define S_ISLNK(x) (0)
  88. # endif
  89. #endif
  90. //--- archive_plarform.h
  91. //
  92. /* Set up defaults for internal error codes. */
  93. #ifndef ARCHIVE_ERRNO_FILE_FORMAT
  94. #if HAVE_EFTYPE
  95. #define ARCHIVE_ERRNO_FILE_FORMAT EFTYPE
  96. #else
  97. #if HAVE_EILSEQ
  98. #define ARCHIVE_ERRNO_FILE_FORMAT EILSEQ
  99. #else
  100. #define ARCHIVE_ERRNO_FILE_FORMAT EINVAL
  101. #endif
  102. #endif
  103. #endif
  104. #ifndef ARCHIVE_ERRNO_PROGRAMMER
  105. #define ARCHIVE_ERRNO_PROGRAMMER EINVAL
  106. #endif
  107. #ifndef ARCHIVE_ERRNO_MISC
  108. #define ARCHIVE_ERRNO_MISC (-1)
  109. #endif
  110. /* Select the best way to set/get hi-res timestamps. */
  111. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  112. /* FreeBSD uses "timespec" members. */
  113. #define ARCHIVE_STAT_ATIME_NANOS(st) (st)->st_atimespec.tv_nsec
  114. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctimespec.tv_nsec
  115. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtimespec.tv_nsec
  116. #define ARCHIVE_STAT_SET_ATIME_NANOS(st, n) (st)->st_atimespec.tv_nsec = (n)
  117. #define ARCHIVE_STAT_SET_CTIME_NANOS(st, n) (st)->st_ctimespec.tv_nsec = (n)
  118. #define ARCHIVE_STAT_SET_MTIME_NANOS(st, n) (st)->st_mtimespec.tv_nsec = (n)
  119. #else
  120. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  121. /* Linux uses "tim" members. */
  122. #define ARCHIVE_STAT_ATIME_NANOS(pstat) (pstat)->st_atim.tv_nsec
  123. #define ARCHIVE_STAT_CTIME_NANOS(pstat) (pstat)->st_ctim.tv_nsec
  124. #define ARCHIVE_STAT_MTIME_NANOS(pstat) (pstat)->st_mtim.tv_nsec
  125. #define ARCHIVE_STAT_SET_ATIME_NANOS(st, n) (st)->st_atim.tv_nsec = (n)
  126. #define ARCHIVE_STAT_SET_CTIME_NANOS(st, n) (st)->st_ctim.tv_nsec = (n)
  127. #define ARCHIVE_STAT_SET_MTIME_NANOS(st, n) (st)->st_mtim.tv_nsec = (n)
  128. #else
  129. /* If we can't find a better way, just use stubs. */
  130. #define ARCHIVE_STAT_ATIME_NANOS(pstat) 0
  131. #define ARCHIVE_STAT_CTIME_NANOS(pstat) 0
  132. #define ARCHIVE_STAT_MTIME_NANOS(pstat) 0
  133. #define ARCHIVE_STAT_SET_ATIME_NANOS(st, n)
  134. #define ARCHIVE_STAT_SET_CTIME_NANOS(st, n)
  135. #define ARCHIVE_STAT_SET_MTIME_NANOS(st, n)
  136. #endif
  137. #endif
  138. //--- archive.h
  139. #define ARCHIVE_BYTES_PER_RECORD 512
  140. #define ARCHIVE_DEFAULT_BYTES_PER_BLOCK 10240
  141. /* Declare our basic types. */
  142. struct archive;
  143. struct archive_entry;
  144. /*
  145. * Error codes: Use archive_errno() and archive_error_string()
  146. * to retrieve details. Unless specified otherwise, all functions
  147. * that return 'int' use these codes.
  148. */
  149. #define ARCHIVE_EOF 1 /* Found end of archive. */
  150. #define ARCHIVE_OK 0 /* Operation was successful. */
  151. #define ARCHIVE_RETRY (-10) /* Retry might succeed. */
  152. #define ARCHIVE_WARN (-20) /* Partial sucess. */
  153. #define ARCHIVE_FATAL (-30) /* No more operations are possible. */
  154. /*
  155. * As far as possible, archive_errno returns standard platform errno codes.
  156. * Of course, the details vary by platform, so the actual definitions
  157. * here are stored in "archive_platform.h". The symbols are listed here
  158. * for reference; as a rule, clients should not need to know the exact
  159. * platform-dependent error code.
  160. */
  161. /* Unrecognized or invalid file format. */
  162. /* #define ARCHIVE_ERRNO_FILE_FORMAT */
  163. /* Illegal usage of the library. */
  164. /* #define ARCHIVE_ERRNO_PROGRAMMER_ERROR */
  165. /* Unknown or unclassified error. */
  166. /* #define ARCHIVE_ERRNO_MISC */
  167. /*
  168. * Callbacks are invoked to automatically read/write/open/close the archive.
  169. * You can provide your own for complex tasks (like breaking archives
  170. * across multiple tapes) or use standard ones built into the library.
  171. */
  172. /* Returns pointer and size of next block of data from archive. */
  173. typedef ssize_t archive_read_callback(struct archive *, void *_client_data,
  174. const void **_buffer);
  175. /* Returns size actually written, zero on EOF, -1 on error. */
  176. typedef ssize_t archive_write_callback(struct archive *, void *_client_data,
  177. void *_buffer, size_t _length);
  178. typedef int archive_open_callback(struct archive *, void *_client_data);
  179. typedef int archive_close_callback(struct archive *, void *_client_data);
  180. /*
  181. * Codes for archive_compression.
  182. */
  183. #define ARCHIVE_COMPRESSION_NONE 0
  184. #define ARCHIVE_COMPRESSION_GZIP 1
  185. #define ARCHIVE_COMPRESSION_BZIP2 2
  186. #define ARCHIVE_COMPRESSION_COMPRESS 3
  187. /*
  188. * Codes returned by archive_format.
  189. *
  190. * Top 16 bits identifies the format family (e.g., "tar"); lower
  191. * 16 bits indicate the variant. This is updated by read_next_header.
  192. * Note that the lower 16 bits will often vary from entry to entry.
  193. */
  194. #define ARCHIVE_FORMAT_BASE_MASK 0xff0000U
  195. #define ARCHIVE_FORMAT_CPIO 0x10000
  196. #define ARCHIVE_FORMAT_CPIO_POSIX (ARCHIVE_FORMAT_CPIO | 1)
  197. #define ARCHIVE_FORMAT_SHAR 0x20000
  198. #define ARCHIVE_FORMAT_SHAR_BASE (ARCHIVE_FORMAT_SHAR | 1)
  199. #define ARCHIVE_FORMAT_SHAR_DUMP (ARCHIVE_FORMAT_SHAR | 2)
  200. #define ARCHIVE_FORMAT_TAR 0x30000
  201. #define ARCHIVE_FORMAT_TAR_USTAR (ARCHIVE_FORMAT_TAR | 1)
  202. #define ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE (ARCHIVE_FORMAT_TAR | 2)
  203. #define ARCHIVE_FORMAT_TAR_PAX_RESTRICTED (ARCHIVE_FORMAT_TAR | 3)
  204. #define ARCHIVE_FORMAT_TAR_GNUTAR (ARCHIVE_FORMAT_TAR | 4)
  205. #define ARCHIVE_FORMAT_ISO9660 0x40000
  206. #define ARCHIVE_FORMAT_ISO9660_ROCKRIDGE (ARCHIVE_FORMAT_ISO9660 | 1)
  207. #define ARCHIVE_FORMAT_ZIP 0x50000
  208. /*-
  209. * Basic outline for reading an archive:
  210. * 1) Ask archive_read_new for an archive reader object.
  211. * 2) Update any global properties as appropriate.
  212. * In particular, you'll certainly want to call appropriate
  213. * archive_read_support_XXX functions.
  214. * 3) Call archive_read_open_XXX to open the archive
  215. * 4) Repeatedly call archive_read_next_header to get information about
  216. * successive archive entries. Call archive_read_data to extract
  217. * data for entries of interest.
  218. * 5) Call archive_read_finish to end processing.
  219. */
  220. struct archive *archive_read_new(void);
  221. /*
  222. * The archive_read_support_XXX calls enable auto-detect for this
  223. * archive handle. They also link in the necessary support code.
  224. * For example, if you don't want bzlib linked in, don't invoke
  225. * support_compression_bzip2(). The "all" functions provide the
  226. * obvious shorthand.
  227. */
  228. int archive_read_support_compression_all(struct archive *);
  229. int archive_read_support_compression_bzip2(struct archive *);
  230. int archive_read_support_compression_compress(struct archive *);
  231. int archive_read_support_compression_gzip(struct archive *);
  232. int archive_read_support_compression_none(struct archive *);
  233. int archive_read_support_format_all(struct archive *);
  234. int archive_read_support_format_cpio(struct archive *);
  235. int archive_read_support_format_gnutar(struct archive *);
  236. int archive_read_support_format_iso9660(struct archive *);
  237. int archive_read_support_format_tar(struct archive *);
  238. int archive_read_support_format_tp(struct archive *a);
  239. int archive_read_support_format_zip(struct archive *);
  240. /* Open the archive using callbacks for archive I/O. */
  241. int archive_read_open(struct archive *, void *_client_data,
  242. archive_open_callback *, archive_read_callback *,
  243. archive_close_callback *);
  244. /*
  245. * The archive_read_open_file function is a convenience function built
  246. * on archive_read_open that uses a canned callback suitable for
  247. * common situations. Note that a NULL filename indicates stdin.
  248. */
  249. int archive_read_open_file(struct archive *, const char *_file,
  250. size_t _block_size);
  251. int archive_read_open_fd(struct archive *, int _fd,
  252. size_t _block_size);
  253. /* Parses and returns next entry header. */
  254. int archive_read_next_header(struct archive *,
  255. struct archive_entry **);
  256. /*
  257. * Retrieve the byte offset in UNCOMPRESSED data where last-read
  258. * header started.
  259. */
  260. int64_t archive_read_header_position(struct archive *);
  261. /* Read data from the body of an entry. Similar to read(2). */
  262. ssize_t archive_read_data(struct archive *, void *, size_t);
  263. /*
  264. * A zero-copy version of archive_read_data that also exposes the file offset
  265. * of each returned block. Note that the client has no way to specify
  266. * the desired size of the block. The API does gaurantee that offsets will
  267. * be strictly increasing and that returned blocks will not overlap.
  268. */
  269. int archive_read_data_block(struct archive *a,
  270. const void **buff, size_t *size, off_t *offset);
  271. /*-
  272. * Some convenience functions that are built on archive_read_data:
  273. * 'skip': skips entire entry
  274. * 'into_buffer': writes data into memory buffer that you provide
  275. * 'into_fd': writes data to specified filedes
  276. */
  277. int archive_read_data_skip(struct archive *);
  278. int archive_read_data_into_buffer(struct archive *, void *buffer,
  279. ssize_t len);
  280. int archive_read_data_into_fd(struct archive *, int fd);
  281. /*-
  282. * Convenience function to recreate the current entry (whose header
  283. * has just been read) on disk.
  284. *
  285. * This does quite a bit more than just copy data to disk. It also:
  286. * - Creates intermediate directories as required.
  287. * - Manages directory permissions: non-writable directories will
  288. * be initially created with write permission enabled; when the
  289. * archive is closed, dir permissions are edited to the values specified
  290. * in the archive.
  291. * - Checks hardlinks: hardlinks will not be extracted unless the
  292. * linked-to file was also extracted within the same session. (TODO)
  293. */
  294. /* The "flags" argument selects optional behavior, 'OR' the flags you want. */
  295. /* TODO: The 'Default' comments here are not quite correct; clean this up. */
  296. #define ARCHIVE_EXTRACT_OWNER (1) /* Default: owner/group not restored */
  297. #define ARCHIVE_EXTRACT_PERM (2) /* Default: restore perm only for reg file*/
  298. #define ARCHIVE_EXTRACT_TIME (4) /* Default: mod time not restored */
  299. #define ARCHIVE_EXTRACT_NO_OVERWRITE (8) /* Default: Replace files on disk */
  300. #define ARCHIVE_EXTRACT_UNLINK (16) /* Default: don't unlink existing files */
  301. #define ARCHIVE_EXTRACT_ACL (32) /* Default: don't restore ACLs */
  302. #define ARCHIVE_EXTRACT_FFLAGS (64) /* Default: don't restore fflags */
  303. int archive_read_extract(struct archive *, struct archive_entry *,
  304. int flags);
  305. void archive_read_extract_set_progress_callback(struct archive *,
  306. void (*_progress_func)(void *), void *_user_data);
  307. /* Close the file and release most resources. */
  308. int archive_read_close(struct archive *);
  309. /* Release all resources and destroy the object. */
  310. /* Note that archive_read_finish will call archive_read_close for you. */
  311. void archive_read_finish(struct archive *);
  312. /*-
  313. * To create an archive:
  314. * 1) Ask archive_write_new for a archive writer object.
  315. * 2) Set any global properties. In particular, you should set
  316. * the compression and format to use.
  317. * 3) Call archive_write_open to open the file (most people
  318. * will use archive_write_open_file or archive_write_open_fd,
  319. * which provide convenient canned I/O callbacks for you).
  320. * 4) For each entry:
  321. * - construct an appropriate struct archive_entry structure
  322. * - archive_write_header to write the header
  323. * - archive_write_data to write the entry data
  324. * 5) archive_write_close to close the output
  325. * 6) archive_write_finish to cleanup the writer and release resources
  326. */
  327. struct archive *archive_write_new(void);
  328. int archive_write_set_bytes_per_block(struct archive *,
  329. int bytes_per_block);
  330. /* XXX This is badly misnamed; suggestions appreciated. XXX */
  331. int archive_write_set_bytes_in_last_block(struct archive *,
  332. int bytes_in_last_block);
  333. int archive_write_set_compression_bzip2(struct archive *);
  334. int archive_write_set_compression_gzip(struct archive *);
  335. int archive_write_set_compression_none(struct archive *);
  336. /* A convenience function to set the format based on the code or name. */
  337. int archive_write_set_format(struct archive *, int format_code);
  338. int archive_write_set_format_by_name(struct archive *,
  339. const char *name);
  340. /* To minimize link pollution, use one or more of the following. */
  341. int archive_write_set_format_cpio(struct archive *);
  342. /* TODO: int archive_write_set_format_old_tar(struct archive *); */
  343. int archive_write_set_format_pax(struct archive *);
  344. int archive_write_set_format_pax_restricted(struct archive *);
  345. int archive_write_set_format_shar(struct archive *);
  346. int archive_write_set_format_shar_dump(struct archive *);
  347. int archive_write_set_format_ustar(struct archive *);
  348. int archive_write_open(struct archive *, void *,
  349. archive_open_callback *, archive_write_callback *,
  350. archive_close_callback *);
  351. int archive_write_open_fd(struct archive *, int _fd);
  352. int archive_write_open_file(struct archive *, const char *_file);
  353. /*
  354. * Note that the library will truncate writes beyond the size provided
  355. * to archive_write_header or pad if the provided data is short.
  356. */
  357. int archive_write_header(struct archive *,
  358. struct archive_entry *);
  359. /* TODO: should be ssize_t, but that might require .so version bump? */
  360. int archive_write_data(struct archive *, const void *, size_t);
  361. int archive_write_close(struct archive *);
  362. void archive_write_finish(struct archive *);
  363. /*
  364. * Accessor functions to read/set various information in
  365. * the struct archive object:
  366. */
  367. /* Bytes written after compression or read before decompression. */
  368. int64_t archive_position_compressed(struct archive *);
  369. /* Bytes written to compressor or read from decompressor. */
  370. int64_t archive_position_uncompressed(struct archive *);
  371. const char *archive_compression_name(struct archive *);
  372. int archive_compression(struct archive *);
  373. int archive_errno(struct archive *);
  374. const char *archive_error_string(struct archive *);
  375. const char *archive_format_name(struct archive *);
  376. int archive_format(struct archive *);
  377. void archive_set_error(struct archive *, int _err, const char *fmt, ...);
  378. //--- archive_string.h
  379. /*
  380. * Basic resizable/reusable string support a la Java's "StringBuffer."
  381. *
  382. * Unlike sbuf(9), the buffers here are fully reusable and track the
  383. * length throughout.
  384. *
  385. * Note that all visible symbols here begin with "__archive" as they
  386. * are internal symbols not intended for anyone outside of this library
  387. * to see or use.
  388. */
  389. struct archive_string {
  390. char *s; /* Pointer to the storage */
  391. size_t length; /* Length of 's' */
  392. size_t buffer_length; /* Length of malloc-ed storage */
  393. };
  394. /* Initialize an archive_string object on the stack or elsewhere. */
  395. #define archive_string_init(a) \
  396. do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
  397. /* Append a C char to an archive_string, resizing as necessary. */
  398. struct archive_string *
  399. __archive_strappend_char(struct archive_string *, char);
  400. #define archive_strappend_char __archive_strappend_char
  401. /* Append a char to an archive_string using UTF8. */
  402. struct archive_string *
  403. __archive_strappend_char_UTF8(struct archive_string *, int);
  404. #define archive_strappend_char_UTF8 __archive_strappend_char_UTF8
  405. /* Append an integer in the specified base (2 <= base <= 16). */
  406. struct archive_string *
  407. __archive_strappend_int(struct archive_string *as, int d, int base);
  408. #define archive_strappend_int __archive_strappend_int
  409. /* Basic append operation. */
  410. struct archive_string *
  411. __archive_string_append(struct archive_string *as, const char *p, size_t s);
  412. /* Ensure that the underlying buffer is at least as large as the request. */
  413. struct archive_string *
  414. __archive_string_ensure(struct archive_string *, size_t);
  415. #define archive_string_ensure __archive_string_ensure
  416. /* Append C string, which may lack trailing \0. */
  417. struct archive_string *
  418. __archive_strncat(struct archive_string *, const char *, size_t);
  419. #define archive_strncat __archive_strncat
  420. /* Append a C string to an archive_string, resizing as necessary. */
  421. #define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
  422. /* Copy a C string to an archive_string, resizing as necessary. */
  423. #define archive_strcpy(as,p) \
  424. ((as)->length = 0, __archive_string_append((as), (p), strlen(p)))
  425. /* Copy a C string to an archive_string with limit, resizing as necessary. */
  426. #define archive_strncpy(as,p,l) \
  427. ((as)->length=0, archive_strncat((as), (p), (l)))
  428. /* Return length of string. */
  429. #define archive_strlen(a) ((a)->length)
  430. /* Set string length to zero. */
  431. #define archive_string_empty(a) ((a)->length = 0)
  432. /* Release any allocated storage resources. */
  433. void __archive_string_free(struct archive_string *);
  434. #define archive_string_free __archive_string_free
  435. /* Like 'vsprintf', but resizes the underlying string as necessary. */
  436. void __archive_string_vsprintf(struct archive_string *, const char *,
  437. va_list);
  438. #define archive_string_vsprintf __archive_string_vsprintf
  439. //--- archive_private.h
  440. #define ARCHIVE_WRITE_MAGIC (0xb0c5c0deU)
  441. #define ARCHIVE_READ_MAGIC (0xdeb0c5U)
  442. struct archive {
  443. /*
  444. * The magic/state values are used to sanity-check the
  445. * client's usage. If an API function is called at a
  446. * rediculous time, or the client passes us an invalid
  447. * pointer, these values allow me to catch that.
  448. */
  449. unsigned magic;
  450. unsigned state;
  451. struct archive_entry *entry;
  452. uid_t user_uid; /* UID of current user. */
  453. /* Dev/ino of the archive being read/written. */
  454. dev_t skip_file_dev;
  455. ino_t skip_file_ino;
  456. /* Utility: Pointer to a block of nulls. */
  457. const unsigned char *nulls;
  458. size_t null_length;
  459. /*
  460. * Used by archive_read_data() to track blocks and copy
  461. * data to client buffers, filling gaps with zero bytes.
  462. */
  463. const char *read_data_block;
  464. off_t read_data_offset;
  465. off_t read_data_output_offset;
  466. size_t read_data_remaining;
  467. /* Callbacks to open/read/write/close archive stream. */
  468. archive_open_callback *client_opener;
  469. archive_read_callback *client_reader;
  470. archive_write_callback *client_writer;
  471. archive_close_callback *client_closer;
  472. void *client_data;
  473. /*
  474. * Blocking information. Note that bytes_in_last_block is
  475. * misleadingly named; I should find a better name. These
  476. * control the final output from all compressors, including
  477. * compression_none.
  478. */
  479. int bytes_per_block;
  480. int bytes_in_last_block;
  481. /*
  482. * These control whether data within a gzip/bzip2 compressed
  483. * stream gets padded or not. If pad_uncompressed is set,
  484. * the data will be padded to a full block before being
  485. * compressed. The pad_uncompressed_byte determines the value
  486. * that will be used for padding. Note that these have no
  487. * effect on compression "none."
  488. */
  489. int pad_uncompressed;
  490. int pad_uncompressed_byte; /* TODO: Support this. */
  491. /* Position in UNCOMPRESSED data stream. */
  492. off_t file_position;
  493. /* Position in COMPRESSED data stream. */
  494. off_t raw_position;
  495. /* File offset of beginning of most recently-read header. */
  496. off_t header_position;
  497. /*
  498. * Detection functions for decompression: bid functions are
  499. * given a block of data from the beginning of the stream and
  500. * can bid on whether or not they support the data stream.
  501. * General guideline: bid the number of bits that you actually
  502. * test, e.g., 16 if you test a 2-byte magic value. The
  503. * highest bidder will have their init function invoked, which
  504. * can set up pointers to specific handlers.
  505. *
  506. * On write, the client just invokes an archive_write_set function
  507. * which sets up the data here directly.
  508. */
  509. int compression_code; /* Currently active compression. */
  510. const char *compression_name;
  511. struct {
  512. int (*bid)(const void *buff, size_t);
  513. int (*init)(struct archive *, const void *buff, size_t);
  514. } decompressors[4];
  515. /* Read/write data stream (with compression). */
  516. void *compression_data; /* Data for (de)compressor. */
  517. int (*compression_init)(struct archive *); /* Initialize. */
  518. int (*compression_finish)(struct archive *);
  519. int (*compression_write)(struct archive *, const void *, size_t);
  520. /*
  521. * Read uses a peek/consume I/O model: the decompression code
  522. * returns a pointer to the requested block and advances the
  523. * file position only when requested by a consume call. This
  524. * reduces copying and also simplifies look-ahead for format
  525. * detection.
  526. */
  527. ssize_t (*compression_read_ahead)(struct archive *,
  528. const void **, size_t request);
  529. ssize_t (*compression_read_consume)(struct archive *, size_t);
  530. /*
  531. * Format detection is mostly the same as compression
  532. * detection, with two significant differences: The bidders
  533. * use the read_ahead calls above to examine the stream rather
  534. * than having the supervisor hand them a block of data to
  535. * examine, and the auction is repeated for every header.
  536. * Winning bidders should set the archive_format and
  537. * archive_format_name appropriately. Bid routines should
  538. * check archive_format and decline to bid if the format of
  539. * the last header was incompatible.
  540. *
  541. * Again, write support is considerably simpler because there's
  542. * no need for an auction.
  543. */
  544. int archive_format;
  545. const char *archive_format_name;
  546. struct archive_format_descriptor {
  547. int (*bid)(struct archive *);
  548. int (*read_header)(struct archive *, struct archive_entry *);
  549. int (*read_data)(struct archive *, const void **, size_t *, off_t *);
  550. int (*read_data_skip)(struct archive *);
  551. int (*cleanup)(struct archive *);
  552. void *format_data; /* Format-specific data for readers. */
  553. } formats[8];
  554. struct archive_format_descriptor *format; /* Active format. */
  555. /*
  556. * Storage for format-specific data. Note that there can be
  557. * multiple format readers active at one time, so we need to
  558. * allow for multiple format readers to have their data
  559. * available. The pformat_data slot here is the solution: on
  560. * read, it is gauranteed to always point to a void* variable
  561. * that the format can use.
  562. */
  563. void **pformat_data; /* Pointer to current format_data. */
  564. void *format_data; /* Used by writers. */
  565. /*
  566. * Pointers to format-specific functions for writing. They're
  567. * initialized by archive_write_set_format_XXX() calls.
  568. */
  569. int (*format_init)(struct archive *); /* Only used on write. */
  570. int (*format_finish)(struct archive *);
  571. int (*format_finish_entry)(struct archive *);
  572. int (*format_write_header)(struct archive *,
  573. struct archive_entry *);
  574. int (*format_write_data)(struct archive *,
  575. const void *buff, size_t);
  576. /*
  577. * Various information needed by archive_extract.
  578. */
  579. struct extract *extract;
  580. void (*extract_progress)(void *);
  581. void *extract_progress_user_data;
  582. void (*cleanup_archive_extract)(struct archive *);
  583. int archive_error_number;
  584. const char *error;
  585. struct archive_string error_string;
  586. };
  587. #define ARCHIVE_STATE_ANY 0xFFFFU
  588. #define ARCHIVE_STATE_NEW 1U
  589. #define ARCHIVE_STATE_HEADER 2U
  590. #define ARCHIVE_STATE_DATA 4U
  591. #define ARCHIVE_STATE_EOF 8U
  592. #define ARCHIVE_STATE_CLOSED 0x10U
  593. #define ARCHIVE_STATE_FATAL 0x8000U
  594. /* Check magic value and state; exit if it isn't valid. */
  595. void __archive_check_magic(struct archive *, unsigned magic,
  596. unsigned state, const char *func);
  597. int __archive_read_register_format(struct archive *a,
  598. void *format_data,
  599. int (*bid)(struct archive *),
  600. int (*read_header)(struct archive *, struct archive_entry *),
  601. int (*read_data)(struct archive *, const void **, size_t *, off_t *),
  602. int (*read_data_skip)(struct archive *),
  603. int (*cleanup)(struct archive *));
  604. int __archive_read_register_compression(struct archive *a,
  605. int (*bid)(const void *, size_t),
  606. int (*init)(struct archive *, const void *, size_t));
  607. void __archive_errx(int retvalue, const char *msg);
  608. #define err_combine(a,b) ((a) < (b) ? (a) : (b))
  609. /*
  610. * Utility function to format a USTAR header into a buffer. If
  611. * "strict" is set, this tries to create the absolutely most portable
  612. * version of a ustar header. If "strict" is set to 0, then it will
  613. * relax certain requirements.
  614. *
  615. * Generally, format-specific declarations don't belong in this
  616. * header; this is a rare example of a function that is shared by
  617. * two very similar formats (ustar and pax).
  618. */
  619. int
  620. __archive_write_format_header_ustar(struct archive *, char buff[512],
  621. struct archive_entry *, int tartype, int strict);
  622. //--- archive_entry.h
  623. /*
  624. * Description of an archive entry.
  625. *
  626. * Basically, a "struct stat" with a few text fields added in.
  627. *
  628. * TODO: Add "comment", "charset", and possibly other entries that are
  629. * supported by "pax interchange" format. However, GNU, ustar, cpio,
  630. * and other variants don't support these features, so they're not an
  631. * excruciatingly high priority right now.
  632. *
  633. * TODO: "pax interchange" format allows essentially arbitrary
  634. * key/value attributes to be attached to any entry. Supporting
  635. * such extensions may make this library useful for special
  636. * applications (e.g., a package manager could attach special
  637. * package-management attributes to each entry).
  638. */
  639. struct archive_entry;
  640. /*
  641. * Basic object manipulation
  642. */
  643. struct archive_entry *archive_entry_clear(struct archive_entry *);
  644. /* The 'clone' function does a deep copy; all of the strings are copied too. */
  645. struct archive_entry *archive_entry_clone(struct archive_entry *);
  646. void archive_entry_free(struct archive_entry *);
  647. struct archive_entry *archive_entry_new(void);
  648. /*
  649. * Retrieve fields from an archive_entry.
  650. */
  651. time_t archive_entry_atime(struct archive_entry *);
  652. long archive_entry_atime_nsec(struct archive_entry *);
  653. time_t archive_entry_ctime(struct archive_entry *);
  654. long archive_entry_ctime_nsec(struct archive_entry *);
  655. dev_t archive_entry_dev(struct archive_entry *);
  656. void archive_entry_fflags(struct archive_entry *,
  657. unsigned long *set, unsigned long *clear);
  658. const char *archive_entry_fflags_text(struct archive_entry *);
  659. gid_t archive_entry_gid(struct archive_entry *);
  660. const char *archive_entry_gname(struct archive_entry *);
  661. const wchar_t *archive_entry_gname_w(struct archive_entry *);
  662. const char *archive_entry_hardlink(struct archive_entry *);
  663. const wchar_t *archive_entry_hardlink_w(struct archive_entry *);
  664. ino_t archive_entry_ino(struct archive_entry *);
  665. mode_t archive_entry_mode(struct archive_entry *);
  666. time_t archive_entry_mtime(struct archive_entry *);
  667. long archive_entry_mtime_nsec(struct archive_entry *);
  668. const char *archive_entry_pathname(struct archive_entry *);
  669. const wchar_t *archive_entry_pathname_w(struct archive_entry *);
  670. dev_t archive_entry_rdev(struct archive_entry *);
  671. int64_t archive_entry_size(struct archive_entry *);
  672. const struct stat *archive_entry_stat(struct archive_entry *);
  673. const char *archive_entry_symlink(struct archive_entry *);
  674. const wchar_t *archive_entry_symlink_w(struct archive_entry *);
  675. uid_t archive_entry_uid(struct archive_entry *);
  676. const char *archive_entry_uname(struct archive_entry *);
  677. const wchar_t *archive_entry_uname_w(struct archive_entry *);
  678. /*
  679. * Set fields in an archive_entry.
  680. *
  681. * Note that string 'set' functions do not copy the string, only the pointer.
  682. * In contrast, 'copy' functions do copy the object pointed to.
  683. */
  684. void archive_entry_copy_stat(struct archive_entry *, const struct stat *);
  685. void archive_entry_set_atime(struct archive_entry *, time_t, long);
  686. void archive_entry_set_ctime(struct archive_entry *, time_t, long);
  687. void archive_entry_set_fflags(struct archive_entry *,
  688. unsigned long set, unsigned long clear);
  689. /* Returns pointer to start of first invalid token, or NULL if none. */
  690. /* Note that all recognized tokens are processed, regardless. */
  691. const wchar_t *archive_entry_copy_fflags_text_w(struct archive_entry *,
  692. const wchar_t *);
  693. void archive_entry_set_gid(struct archive_entry *, gid_t);
  694. void archive_entry_set_gname(struct archive_entry *, const char *);
  695. void archive_entry_copy_gname_w(struct archive_entry *, const wchar_t *);
  696. void archive_entry_set_hardlink(struct archive_entry *, const char *);
  697. void archive_entry_copy_hardlink(struct archive_entry *, const char *);
  698. void archive_entry_copy_hardlink_w(struct archive_entry *, const wchar_t *);
  699. void archive_entry_set_link(struct archive_entry *, const char *);
  700. void archive_entry_set_mode(struct archive_entry *, mode_t);
  701. void archive_entry_set_mtime(struct archive_entry *, time_t, long);
  702. void archive_entry_set_pathname(struct archive_entry *, const char *);
  703. void archive_entry_copy_pathname(struct archive_entry *, const char *);
  704. void archive_entry_copy_pathname_w(struct archive_entry *, const wchar_t *);
  705. void archive_entry_set_size(struct archive_entry *, int64_t);
  706. void archive_entry_set_symlink(struct archive_entry *, const char *);
  707. void archive_entry_copy_symlink_w(struct archive_entry *, const wchar_t *);
  708. void archive_entry_set_uid(struct archive_entry *, uid_t);
  709. void archive_entry_set_uname(struct archive_entry *, const char *);
  710. void archive_entry_copy_uname_w(struct archive_entry *, const wchar_t *);
  711. /*
  712. * ACL routines. This used to simply store and return text-format ACL
  713. * strings, but that proved insufficient for a number of reasons:
  714. * = clients need control over uname/uid and gname/gid mappings
  715. * = there are many different ACL text formats
  716. * = would like to be able to read/convert archives containing ACLs
  717. * on platforms that lack ACL libraries
  718. */
  719. /*
  720. * Permission bits mimic POSIX.1e. Note that I've not followed POSIX.1e's
  721. * "permset"/"perm" abstract type nonsense. A permset is just a simple
  722. * bitmap, following long-standing Unix tradition.
  723. */
  724. #define ARCHIVE_ENTRY_ACL_EXECUTE 1
  725. #define ARCHIVE_ENTRY_ACL_WRITE 2
  726. #define ARCHIVE_ENTRY_ACL_READ 4
  727. /* We need to be able to specify either or both of these. */
  728. #define ARCHIVE_ENTRY_ACL_TYPE_ACCESS 256
  729. #define ARCHIVE_ENTRY_ACL_TYPE_DEFAULT 512
  730. /* Tag values mimic POSIX.1e */
  731. #define ARCHIVE_ENTRY_ACL_USER 10001 /* Specified user. */
  732. #define ARCHIVE_ENTRY_ACL_USER_OBJ 10002 /* User who owns the file. */
  733. #define ARCHIVE_ENTRY_ACL_GROUP 10003 /* Specified group. */
  734. #define ARCHIVE_ENTRY_ACL_GROUP_OBJ 10004 /* Group who owns the file. */
  735. #define ARCHIVE_ENTRY_ACL_MASK 10005 /* Modify group access. */
  736. #define ARCHIVE_ENTRY_ACL_OTHER 10006 /* Public. */
  737. /*
  738. * Set the ACL by clearing it and adding entries one at a time.
  739. * Unlike the POSIX.1e ACL routines, you must specify the type
  740. * (access/default) for each entry. Internally, the ACL data is just
  741. * a soup of entries. API calls here allow you to retrieve just the
  742. * entries of interest. This design (which goes against the spirit of
  743. * POSIX.1e) is useful for handling archive formats that combine
  744. * default and access information in a single ACL list.
  745. */
  746. void archive_entry_acl_clear(struct archive_entry *);
  747. void archive_entry_acl_add_entry(struct archive_entry *,
  748. int type, int permset, int tag, int qual, const char *name);
  749. void archive_entry_acl_add_entry_w(struct archive_entry *,
  750. int type, int permset, int tag, int qual, const wchar_t *name);
  751. /*
  752. * To retrieve the ACL, first "reset", then repeatedly ask for the
  753. * "next" entry. The want_type parameter allows you to request only
  754. * access entries or only default entries.
  755. */
  756. int archive_entry_acl_reset(struct archive_entry *, int want_type);
  757. int archive_entry_acl_next(struct archive_entry *, int want_type,
  758. int *type, int *permset, int *tag, int *qual, const char **name);
  759. int archive_entry_acl_next_w(struct archive_entry *, int want_type,
  760. int *type, int *permset, int *tag, int *qual,
  761. const wchar_t **name);
  762. /*
  763. * Construct a text-format ACL. The flags argument is a bitmask that
  764. * can include any of the following:
  765. *
  766. * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - Include access entries.
  767. * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - Include default entries.
  768. * ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID - Include extra numeric ID field in
  769. * each ACL entry. (As used by 'star'.)
  770. * ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT - Include "default:" before each
  771. * default ACL entry.
  772. */
  773. #define ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID 1024
  774. #define ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT 2048
  775. const wchar_t *archive_entry_acl_text_w(struct archive_entry *, int flags);
  776. /* Return a count of entries matching 'want_type' */
  777. int archive_entry_acl_count(struct archive_entry *, int want_type);
  778. /*
  779. * Private ACL parser. This is private because it handles some
  780. * very weird formats that clients should not be messing with.
  781. * Clients should only deal with their platform-native formats.
  782. * Because of the need to support many formats cleanly, new arguments
  783. * are likely to get added on a regular basis. Clients who try to use
  784. * this interface are likely to be surprised when it changes.
  785. *
  786. * You were warned!
  787. */
  788. int __archive_entry_acl_parse_w(struct archive_entry *,
  789. const wchar_t *, int type);
  790. //--- archive_write.h
  791. /*
  792. * Allocate, initialize and return an archive object.
  793. */
  794. struct archive *
  795. archive_write_new(void)
  796. {
  797. struct archive *a;
  798. unsigned char *nulls;
  799. a = (struct archive*)malloc(sizeof(*a));
  800. if (a == NULL)
  801. return (NULL);
  802. memset(a, 0, sizeof(*a));
  803. a->magic = ARCHIVE_WRITE_MAGIC;
  804. #ifdef HAVE_GETEUID
  805. a->user_uid = geteuid();
  806. #endif
  807. a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
  808. a->bytes_in_last_block = -1; /* Default */
  809. a->state = ARCHIVE_STATE_NEW;
  810. a->pformat_data = &(a->format_data);
  811. /* Initialize a block of nulls for padding purposes. */
  812. a->null_length = 1024;
  813. nulls = (unsigned char*)malloc(a->null_length);
  814. if (nulls == NULL) {
  815. free(a);
  816. return (NULL);
  817. }
  818. memset(nulls, 0, a->null_length);
  819. a->nulls = nulls;
  820. /*
  821. * Set default compression, but don't set a default format.
  822. * Were we to set a default format here, we would force every
  823. * client to link in support for that format, even if they didn't
  824. * ever use it.
  825. */
  826. archive_write_set_compression_none(a);
  827. return (a);
  828. }
  829. /*
  830. * Set the block size. Returns 0 if successful.
  831. */
  832. int
  833. archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
  834. {
  835. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
  836. a->bytes_per_block = bytes_per_block;
  837. return (ARCHIVE_OK);
  838. }
  839. /*
  840. * Set the size for the last block.
  841. * Returns 0 if successful.
  842. */
  843. int
  844. archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
  845. {
  846. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
  847. a->bytes_in_last_block = bytes;
  848. return (ARCHIVE_OK);
  849. }
  850. /*
  851. * Open the archive using the current settings.
  852. */
  853. int
  854. archive_write_open(struct archive *a, void *client_data,
  855. archive_open_callback *opener, archive_write_callback *writer,
  856. archive_close_callback *closer)
  857. {
  858. int ret;
  859. ret = ARCHIVE_OK;
  860. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_open");
  861. archive_string_empty(&a->error_string);
  862. a->state = ARCHIVE_STATE_HEADER;
  863. a->client_data = client_data;
  864. a->client_writer = writer;
  865. a->client_opener = opener;
  866. a->client_closer = closer;
  867. ret = (a->compression_init)(a);
  868. if (a->format_init && ret == ARCHIVE_OK)
  869. ret = (a->format_init)(a);
  870. return (ret);
  871. }
  872. /*
  873. * Close out the archive.
  874. *
  875. * Be careful: user might just call write_new and then write_finish.
  876. * Don't assume we actually wrote anything or performed any non-trivial
  877. * initialization.
  878. */
  879. int
  880. archive_write_close(struct archive *a)
  881. {
  882. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_close");
  883. /* Finish the last entry. */
  884. if (a->state & ARCHIVE_STATE_DATA)
  885. ((a->format_finish_entry)(a));
  886. /* Finish off the archive. */
  887. if (a->format_finish != NULL)
  888. (a->format_finish)(a);
  889. /* Finish the compression and close the stream. */
  890. if (a->compression_finish != NULL)
  891. (a->compression_finish)(a);
  892. a->state = ARCHIVE_STATE_CLOSED;
  893. return (ARCHIVE_OK);
  894. }
  895. /*
  896. * Destroy the archive structure.
  897. */
  898. void
  899. archive_write_finish(struct archive *a)
  900. {
  901. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_finish");
  902. if (a->state != ARCHIVE_STATE_CLOSED)
  903. archive_write_close(a);
  904. /* Release various dynamic buffers. */
  905. free((void *)(uintptr_t)(const void *)a->nulls);
  906. archive_string_free(&a->error_string);
  907. a->magic = 0;
  908. free(a);
  909. }
  910. /*
  911. * Write the appropriate header.
  912. */
  913. int
  914. archive_write_header(struct archive *a, struct archive_entry *entry)
  915. {
  916. int ret;
  917. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
  918. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, "archive_write_header");
  919. archive_string_empty(&a->error_string);
  920. /* Finish last entry. */
  921. if (a->state & ARCHIVE_STATE_DATA)
  922. ((a->format_finish_entry)(a));
  923. if (archive_entry_dev(entry) == a->skip_file_dev &&
  924. archive_entry_ino(entry) == a->skip_file_ino) {
  925. archive_set_error(a, 0, "Can't add archive to itself");
  926. return (ARCHIVE_WARN);
  927. }
  928. /* Format and write header. */
  929. ret = ((a->format_write_header)(a, entry));
  930. a->state = ARCHIVE_STATE_DATA;
  931. return (ret);
  932. }
  933. /*
  934. * Note that the compressor is responsible for blocking.
  935. */
  936. /* Should be "ssize_t", but that breaks the ABI. <sigh> */
  937. int
  938. archive_write_data(struct archive *a, const void *buff, size_t s)
  939. {
  940. int ret;
  941. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA, "archive_write_data");
  942. archive_string_empty(&a->error_string);
  943. ret = (a->format_write_data)(a, buff, s);
  944. return (ret == ARCHIVE_OK ? (ssize_t)s : -1);
  945. }
  946. //--- archive_write_set_compression_none.c
  947. static int archive_compressor_none_finish(struct archive *a);
  948. static int archive_compressor_none_init(struct archive *);
  949. static int archive_compressor_none_write(struct archive *, const void *,
  950. size_t);
  951. struct archive_none {
  952. char *buffer;
  953. ssize_t buffer_size;
  954. char *next; /* Current insert location */
  955. ssize_t avail; /* Free space left in buffer */
  956. };
  957. int
  958. archive_write_set_compression_none(struct archive *a)
  959. {
  960. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_compression_none");
  961. a->compression_init = &archive_compressor_none_init;
  962. a->compression_code = ARCHIVE_COMPRESSION_NONE;
  963. a->compression_name = "none";
  964. return (0);
  965. }
  966. /*
  967. * Setup callback.
  968. */
  969. static int
  970. archive_compressor_none_init(struct archive *a)
  971. {
  972. int ret;
  973. struct archive_none *state;
  974. a->compression_code = ARCHIVE_COMPRESSION_NONE;
  975. a->compression_name = "none";
  976. if (a->client_opener != NULL) {
  977. ret = (a->client_opener)(a, a->client_data);
  978. if (ret != 0)
  979. return (ret);
  980. }
  981. state = (struct archive_none *)malloc(sizeof(*state));
  982. if (state == NULL) {
  983. archive_set_error(a, ENOMEM,
  984. "Can't allocate data for output buffering");
  985. return (ARCHIVE_FATAL);
  986. }
  987. memset(state, 0, sizeof(*state));
  988. state->buffer_size = a->bytes_per_block;
  989. state->buffer = (char*)malloc(state->buffer_size);
  990. if (state->buffer == NULL) {
  991. archive_set_error(a, ENOMEM,
  992. "Can't allocate output buffer");
  993. free(state);
  994. return (ARCHIVE_FATAL);
  995. }
  996. state->next = state->buffer;
  997. state->avail = state->buffer_size;
  998. a->compression_data = state;
  999. a->compression_write = archive_compressor_none_write;
  1000. a->compression_finish = archive_compressor_none_finish;
  1001. return (ARCHIVE_OK);
  1002. }
  1003. /*
  1004. * Write data to the stream.
  1005. */
  1006. static int
  1007. archive_compressor_none_write(struct archive *a, const void *vbuff,
  1008. size_t length)
  1009. {
  1010. const char *buff;
  1011. ssize_t remaining, to_copy;
  1012. ssize_t bytes_written;
  1013. struct archive_none *state;
  1014. state = (struct archive_none*)a->compression_data;
  1015. buff = (const char*)vbuff;
  1016. if (a->client_writer == NULL) {
  1017. archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
  1018. "No write callback is registered? "
  1019. "This is probably an internal programming error.");
  1020. return (ARCHIVE_FATAL);
  1021. }
  1022. remaining = length;
  1023. while (remaining > 0) {
  1024. /*
  1025. * If we have a full output block, write it and reset the
  1026. * output buffer.
  1027. */
  1028. if (state->avail == 0) {
  1029. bytes_written = (a->client_writer)(a, a->client_data,
  1030. state->buffer, state->buffer_size);
  1031. if (bytes_written <= 0)
  1032. return (ARCHIVE_FATAL);
  1033. /* XXX TODO: if bytes_written < state->buffer_size */
  1034. a->raw_position += bytes_written;
  1035. state->next = state->buffer;
  1036. state->avail = state->buffer_size;
  1037. }
  1038. /* Now we have space in the buffer; copy new data into it. */
  1039. to_copy = (remaining > state->avail) ?
  1040. state->avail : remaining;
  1041. memcpy(state->next, buff, to_copy);
  1042. state->next += to_copy;
  1043. state->avail -= to_copy;
  1044. buff += to_copy;
  1045. remaining -= to_copy;
  1046. }
  1047. a->file_position += length;
  1048. return (ARCHIVE_OK);
  1049. }
  1050. /*
  1051. * Finish the compression.
  1052. */
  1053. static int
  1054. archive_compressor_none_finish(struct archive *a)
  1055. {
  1056. ssize_t block_length;
  1057. ssize_t target_block_length;
  1058. ssize_t bytes_written;
  1059. int ret;
  1060. int ret2;
  1061. struct archive_none *state;
  1062. state = (struct archive_none*)a->compression_data;
  1063. ret = ret2 = ARCHIVE_OK;
  1064. if (a->client_writer == NULL) {
  1065. archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
  1066. "No write callback is registered? "
  1067. "This is probably an internal programming error.");
  1068. return (ARCHIVE_FATAL);
  1069. }
  1070. /* If there's pending data, pad and write the last block */
  1071. if (state->next != state->buffer) {
  1072. block_length = state->buffer_size - state->avail;
  1073. /* Tricky calculation to determine size of last block */
  1074. target_block_length = block_length;
  1075. if (a->bytes_in_last_block <= 0)
  1076. /* Default or Zero: pad to full block */
  1077. target_block_length = a->bytes_per_block;
  1078. else
  1079. /* Round to next multiple of bytes_in_last_block. */
  1080. target_block_length = a->bytes_in_last_block *
  1081. ( (block_length + a->bytes_in_last_block - 1) /
  1082. a->bytes_in_last_block);
  1083. if (target_block_length > a->bytes_per_block)
  1084. target_block_length = a->bytes_per_block;
  1085. if (block_length < target_block_length) {
  1086. memset(state->next, 0,
  1087. target_block_length - block_length);
  1088. block_length = target_block_length;
  1089. }
  1090. bytes_written = (a->client_writer)(a, a->client_data,
  1091. state->buffer, block_length);
  1092. if (bytes_written <= 0)
  1093. ret = ARCHIVE_FATAL;
  1094. else {
  1095. a->raw_position += bytes_written;
  1096. ret = ARCHIVE_OK;
  1097. }
  1098. }
  1099. /* Close the output */
  1100. if (a->client_closer != NULL)
  1101. ret2 = (a->client_closer)(a, a->client_data);
  1102. free(state->buffer);
  1103. free(state);
  1104. a->compression_data = NULL;
  1105. return (ret != ARCHIVE_OK ? ret : ret2);
  1106. }
  1107. //--- archive_check_magic.c
  1108. static void
  1109. errmsg(const char *m)
  1110. {
  1111. cmSystemTools::Error("CPack error: ", m);
  1112. }
  1113. static void
  1114. diediedie(void)
  1115. {
  1116. *(char *)0 = 1; /* Deliberately segfault and force a coredump. */
  1117. _exit(1); /* If that didn't work, just exit with an error. */
  1118. }
  1119. static const char *
  1120. state_name(unsigned s)
  1121. {
  1122. switch (s) {
  1123. case ARCHIVE_STATE_NEW: return ("new");
  1124. case ARCHIVE_STATE_HEADER: return ("header");
  1125. case ARCHIVE_STATE_DATA: return ("data");
  1126. case ARCHIVE_STATE_EOF: return ("eof");
  1127. case ARCHIVE_STATE_CLOSED: return ("closed");
  1128. case ARCHIVE_STATE_FATAL: return ("fatal");
  1129. default: return ("??");
  1130. }
  1131. }
  1132. static void
  1133. write_all_states(int states)
  1134. {
  1135. unsigned lowbit;
  1136. /* A trick for computing the lowest set bit. */
  1137. while ((lowbit = states & (-states)) != 0) {
  1138. states &= ~lowbit; /* Clear the low bit. */
  1139. errmsg(state_name(lowbit));
  1140. if (states != 0)
  1141. errmsg("/");
  1142. }
  1143. }
  1144. /*
  1145. * Check magic value and current state; bail if it isn't valid.
  1146. *
  1147. * This is designed to catch serious programming errors that violate
  1148. * the libarchive API.
  1149. */
  1150. void
  1151. __archive_check_magic(struct archive *a, unsigned magic, unsigned state,
  1152. const char *function)
  1153. {
  1154. if (a->magic != magic) {
  1155. errmsg("INTERNAL ERROR: Function ");
  1156. errmsg(function);
  1157. errmsg(" invoked with invalid struct archive structure.\n");
  1158. diediedie();
  1159. }
  1160. if (state == ARCHIVE_STATE_ANY)
  1161. return;
  1162. if ((a->state & state) == 0) {
  1163. errmsg("INTERNAL ERROR: Function '");
  1164. errmsg(function);
  1165. errmsg("' invoked with archive structure in state '");
  1166. write_all_states(a->state);
  1167. errmsg("', should be in state '");
  1168. write_all_states(state);
  1169. errmsg("'\n");
  1170. diediedie();
  1171. }
  1172. }
  1173. //--- archive_util.c
  1174. int
  1175. archive_errno(struct archive *a)
  1176. {
  1177. return (a->archive_error_number);
  1178. }
  1179. const char *
  1180. archive_error_string(struct archive *a)
  1181. {
  1182. if (a->error != NULL && *a->error != '\0')
  1183. return (a->error);
  1184. else
  1185. return ("(Empty error message)");
  1186. }
  1187. int
  1188. archive_format(struct archive *a)
  1189. {
  1190. return (a->archive_format);
  1191. }
  1192. const char *
  1193. archive_format_name(struct archive *a)
  1194. {
  1195. return (a->archive_format_name);
  1196. }
  1197. int
  1198. archive_compression(struct archive *a)
  1199. {
  1200. return (a->compression_code);
  1201. }
  1202. const char *
  1203. archive_compression_name(struct archive *a)
  1204. {
  1205. return (a->compression_name);
  1206. }
  1207. /*
  1208. * Return a count of the number of compressed bytes processed.
  1209. */
  1210. int64_t
  1211. archive_position_compressed(struct archive *a)
  1212. {
  1213. return (a->raw_position);
  1214. }
  1215. /*
  1216. * Return a count of the number of uncompressed bytes processed.
  1217. */
  1218. int64_t
  1219. archive_position_uncompressed(struct archive *a)
  1220. {
  1221. return (a->file_position);
  1222. }
  1223. void
  1224. archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
  1225. {
  1226. va_list ap;
  1227. #ifdef HAVE_STRERROR_R
  1228. char errbuff[512];
  1229. #endif
  1230. char *errp;
  1231. a->archive_error_number = error_number;
  1232. if (fmt == NULL) {
  1233. a->error = NULL;
  1234. return;
  1235. }
  1236. va_start(ap, fmt);
  1237. archive_string_vsprintf(&(a->error_string), fmt, ap);
  1238. if (error_number > 0) {
  1239. archive_strcat(&(a->error_string), ": ");
  1240. #ifdef HAVE_STRERROR_R
  1241. #ifdef STRERROR_R_CHAR_P
  1242. errp = strerror_r(error_number, errbuff, sizeof(errbuff));
  1243. #else
  1244. strerror_r(error_number, errbuff, sizeof(errbuff));
  1245. errp = errbuff;
  1246. #endif
  1247. #else
  1248. /* Note: this is not threadsafe! */
  1249. errp = strerror(error_number);
  1250. #endif
  1251. archive_strcat(&(a->error_string), errp);
  1252. }
  1253. a->error = a->error_string.s;
  1254. va_end(ap);
  1255. }
  1256. void
  1257. __archive_errx(int retvalue, const char *msg)
  1258. {
  1259. static const char *msg1 = "Fatal Internal Error in libarchive: ";
  1260. write(2, msg1, strlen(msg1));
  1261. write(2, msg, strlen(msg));
  1262. write(2, "\n", 1);
  1263. exit(retvalue);
  1264. }
  1265. //--- archive_string.c
  1266. struct archive_string *
  1267. __archive_string_append(struct archive_string *as, const char *p, size_t s)
  1268. {
  1269. __archive_string_ensure(as, as->length + s + 1);
  1270. memcpy(as->s + as->length, p, s);
  1271. as->s[as->length + s] = 0;
  1272. as->length += s;
  1273. return (as);
  1274. }
  1275. void
  1276. __archive_string_free(struct archive_string *as)
  1277. {
  1278. as->length = 0;
  1279. as->buffer_length = 0;
  1280. if (as->s != NULL)
  1281. free(as->s);
  1282. }
  1283. struct archive_string *
  1284. __archive_string_ensure(struct archive_string *as, size_t s)
  1285. {
  1286. if (as->s && (s <= as->buffer_length))
  1287. return (as);
  1288. if (as->buffer_length < 32)
  1289. as->buffer_length = 32;
  1290. while (as->buffer_length < s)
  1291. as->buffer_length *= 2;
  1292. as->s = (char*)realloc(as->s, as->buffer_length);
  1293. /* TODO: Return null instead and fix up all of our callers to
  1294. * handle this correctly. */
  1295. if (as->s == NULL)
  1296. __archive_errx(1, "Out of memory");
  1297. return (as);
  1298. }
  1299. struct archive_string *
  1300. __archive_strncat(struct archive_string *as, const char *p, size_t n)
  1301. {
  1302. size_t s;
  1303. const char *pp;
  1304. /* Like strlen(p), except won't examine positions beyond p[n]. */
  1305. s = 0;
  1306. pp = p;
  1307. while (*pp && s < n) {
  1308. pp++;
  1309. s++;
  1310. }
  1311. return (__archive_string_append(as, p, s));
  1312. }
  1313. struct archive_string *
  1314. __archive_strappend_char(struct archive_string *as, char c)
  1315. {
  1316. return (__archive_string_append(as, &c, 1));
  1317. }
  1318. struct archive_string *
  1319. __archive_strappend_int(struct archive_string *as, int d, int base)
  1320. {
  1321. static const char *digits = "0123457890abcdef";
  1322. if (d < 0) {
  1323. __archive_strappend_char(as, '-');
  1324. d = -d;
  1325. }
  1326. if (d >= base)
  1327. __archive_strappend_int(as, d/base, base);
  1328. __archive_strappend_char(as, digits[d % base]);
  1329. return (as);
  1330. }
  1331. //--- archive_entry.c
  1332. /* Obtain suitable wide-character manipulation functions. */
  1333. #ifdef HAVE_WCHAR_H
  1334. #include <wchar.h>
  1335. #else
  1336. static size_t wcslen(const wchar_t *s)
  1337. {
  1338. const wchar_t *p = s;
  1339. while (*p != L'\0')
  1340. ++p;
  1341. return p - s;
  1342. }
  1343. static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
  1344. {
  1345. wchar_t *dest = s1;
  1346. while((*s1 = *s2) != L'\0')
  1347. ++s1, ++s2;
  1348. return dest;
  1349. }
  1350. #define wmemcpy(a,b,i) (wchar_t *)memcpy((a),(b),(i)*sizeof(wchar_t))
  1351. /* Good enough for simple equality testing, but not for sorting. */
  1352. #define wmemcmp(a,b,i) memcmp((a),(b),(i)*sizeof(wchar_t))
  1353. #endif
  1354. #undef max
  1355. #define max(a, b) ((a)>(b)?(a):(b))
  1356. /*
  1357. * Handle wide character (i.e., Unicode) and non-wide character
  1358. * strings transparently.
  1359. *
  1360. */
  1361. struct aes {
  1362. const char *aes_mbs;
  1363. char *aes_mbs_alloc;
  1364. const wchar_t *aes_wcs;
  1365. wchar_t *aes_wcs_alloc;
  1366. };
  1367. struct ae_acl {
  1368. struct ae_acl *next;
  1369. int type; /* E.g., access or default */
  1370. int tag; /* E.g., user/group/other/mask */
  1371. int permset; /* r/w/x bits */
  1372. int id; /* uid/gid for user/group */
  1373. struct aes name; /* uname/gname */
  1374. };
  1375. static void aes_clean(struct aes *);
  1376. static void aes_copy(struct aes *dest, struct aes *src);
  1377. static const char * aes_get_mbs(struct aes *);
  1378. static const wchar_t * aes_get_wcs(struct aes *);
  1379. static void aes_set_mbs(struct aes *, const char *mbs);
  1380. static void aes_copy_mbs(struct aes *, const char *mbs);
  1381. /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
  1382. static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
  1383. static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
  1384. static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
  1385. unsigned long *setp, unsigned long *clrp);
  1386. static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
  1387. const wchar_t *wname, int perm, int id);
  1388. static void append_id_w(wchar_t **wp, int id);
  1389. static int acl_special(struct archive_entry *entry,
  1390. int type, int permset, int tag);
  1391. static struct ae_acl *acl_new_entry(struct archive_entry *entry,
  1392. int type, int permset, int tag, int id);
  1393. static void next_field_w(const wchar_t **wp, const wchar_t **start,
  1394. const wchar_t **end, wchar_t *sep);
  1395. static int prefix_w(const wchar_t *start, const wchar_t *end,
  1396. const wchar_t *test);
  1397. /*
  1398. * Description of an archive entry.
  1399. *
  1400. * Basically, this is a "struct stat" with a few text fields added in.
  1401. *
  1402. * TODO: Add "comment", "charset", and possibly other entries
  1403. * that are supported by "pax interchange" format. However, GNU, ustar,
  1404. * cpio, and other variants don't support these features, so they're not an
  1405. * excruciatingly high priority right now.
  1406. *
  1407. * TODO: "pax interchange" format allows essentially arbitrary
  1408. * key/value attributes to be attached to any entry. Supporting
  1409. * such extensions may make this library useful for special
  1410. * applications (e.g., a package manager could attach special
  1411. * package-management attributes to each entry). There are tricky
  1412. * API issues involved, so this is not going to happen until
  1413. * there's a real demand for it.
  1414. *
  1415. * TODO: Design a good API for handling sparse files.
  1416. */
  1417. struct archive_entry {
  1418. /*
  1419. * Note that ae_stat.st_mode & S_IFMT can be 0!
  1420. *
  1421. * This occurs when the actual file type of the object is not
  1422. * in the archive. For example, 'tar' archives store
  1423. * hardlinks without marking the type of the underlying
  1424. * object.
  1425. */
  1426. struct stat ae_stat;
  1427. /*
  1428. * Use aes here so that we get transparent mbs<->wcs conversions.
  1429. */
  1430. struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
  1431. unsigned long ae_fflags_set; /* Bitmap fflags */
  1432. unsigned long ae_fflags_clear;
  1433. struct aes ae_gname; /* Name of owning group */
  1434. struct aes ae_hardlink; /* Name of target for hardlink */
  1435. struct aes ae_pathname; /* Name of entry */
  1436. struct aes ae_symlink; /* symlink contents */
  1437. struct aes ae_uname; /* Name of owner */
  1438. struct ae_acl *acl_head;
  1439. struct ae_acl *acl_p;
  1440. int acl_state; /* See acl_next for details. */
  1441. wchar_t *acl_text_w;
  1442. };
  1443. static void
  1444. aes_clean(struct aes *aes)
  1445. {
  1446. if (aes->aes_mbs_alloc) {
  1447. free(aes->aes_mbs_alloc);
  1448. aes->aes_mbs_alloc = NULL;
  1449. }
  1450. if (aes->aes_wcs_alloc) {
  1451. free(aes->aes_wcs_alloc);
  1452. aes->aes_wcs_alloc = NULL;
  1453. }
  1454. memset(aes, 0, sizeof(*aes));
  1455. }
  1456. static void
  1457. aes_copy(struct aes *dest, struct aes *src)
  1458. {
  1459. *dest = *src;
  1460. if (src->aes_mbs != NULL) {
  1461. dest->aes_mbs_alloc = strdup(src->aes_mbs);
  1462. dest->aes_mbs = dest->aes_mbs_alloc;
  1463. if (dest->aes_mbs == NULL)
  1464. __archive_errx(1, "No memory for aes_copy()");
  1465. }
  1466. if (src->aes_wcs != NULL) {
  1467. dest->aes_wcs_alloc = (wchar_t*)malloc((wcslen(src->aes_wcs) + 1)
  1468. * sizeof(wchar_t));
  1469. dest->aes_wcs = dest->aes_wcs_alloc;
  1470. if (dest->aes_wcs == NULL)
  1471. __archive_errx(1, "No memory for aes_copy()");
  1472. wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
  1473. }
  1474. }
  1475. static const char *
  1476. aes_get_mbs(struct aes *aes)
  1477. {
  1478. if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
  1479. return NULL;
  1480. if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
  1481. /*
  1482. * XXX Need to estimate the number of byte in the
  1483. * multi-byte form. Assume that, on average, wcs
  1484. * chars encode to no more than 3 bytes. There must
  1485. * be a better way... XXX
  1486. */
  1487. int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
  1488. aes->aes_mbs_alloc = (char*)malloc(mbs_length);
  1489. aes->aes_mbs = aes->aes_mbs_alloc;
  1490. if (aes->aes_mbs == NULL)
  1491. __archive_errx(1, "No memory for aes_get_mbs()");
  1492. wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
  1493. aes->aes_mbs_alloc[mbs_length - 1] = 0;
  1494. }
  1495. return (aes->aes_mbs);
  1496. }
  1497. static const wchar_t *
  1498. aes_get_wcs(struct aes *aes)
  1499. {
  1500. if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
  1501. return NULL;
  1502. if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
  1503. /*
  1504. * No single byte will be more than one wide character,
  1505. * so this length estimate will always be big enough.
  1506. */
  1507. int wcs_length = strlen(aes->aes_mbs);
  1508. aes->aes_wcs_alloc
  1509. = (wchar_t*)malloc((wcs_length + 1) * sizeof(wchar_t));
  1510. aes->aes_wcs = aes->aes_wcs_alloc;
  1511. if (aes->aes_wcs == NULL)
  1512. __archive_errx(1, "No memory for aes_get_wcs()");
  1513. mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
  1514. aes->aes_wcs_alloc[wcs_length] = 0;
  1515. }
  1516. return (aes->aes_wcs);
  1517. }
  1518. static void
  1519. aes_set_mbs(struct aes *aes, const char *mbs)
  1520. {
  1521. if (aes->aes_mbs_alloc) {
  1522. free(aes->aes_mbs_alloc);
  1523. aes->aes_mbs_alloc = NULL;
  1524. }
  1525. if (aes->aes_wcs_alloc) {
  1526. free(aes->aes_wcs_alloc);
  1527. aes->aes_wcs_alloc = NULL;
  1528. }
  1529. aes->aes_mbs = mbs;
  1530. aes->aes_wcs = NULL;
  1531. }
  1532. static void
  1533. aes_copy_mbs(struct aes *aes, const char *mbs)
  1534. {
  1535. if (aes->aes_mbs_alloc) {
  1536. free(aes->aes_mbs_alloc);
  1537. aes->aes_mbs_alloc = NULL;
  1538. }
  1539. if (aes->aes_wcs_alloc) {
  1540. free(aes->aes_wcs_alloc);
  1541. aes->aes_wcs_alloc = NULL;
  1542. }
  1543. aes->aes_mbs_alloc = (char*)malloc((strlen(mbs) + 1) * sizeof(char));
  1544. if (aes->aes_mbs_alloc == NULL)
  1545. __archive_errx(1, "No memory for aes_copy_mbs()");
  1546. strcpy(aes->aes_mbs_alloc, mbs);
  1547. aes->aes_mbs = aes->aes_mbs_alloc;
  1548. aes->aes_wcs = NULL;
  1549. }
  1550. #if 0
  1551. static void
  1552. aes_set_wcs(struct aes *aes, const wchar_t *wcs)
  1553. {
  1554. if (aes->aes_mbs_alloc) {
  1555. free(aes->aes_mbs_alloc);
  1556. aes->aes_mbs_alloc = NULL;
  1557. }
  1558. if (aes->aes_wcs_alloc) {
  1559. free(aes->aes_wcs_alloc);
  1560. aes->aes_wcs_alloc = NULL;
  1561. }
  1562. aes->aes_mbs = NULL;
  1563. aes->aes_wcs = wcs;
  1564. }
  1565. #endif
  1566. static void
  1567. aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
  1568. {
  1569. if (aes->aes_mbs_alloc) {
  1570. free(aes->aes_mbs_alloc);
  1571. aes->aes_mbs_alloc = NULL;
  1572. }
  1573. if (aes->aes_wcs_alloc) {
  1574. free(aes->aes_wcs_alloc);
  1575. aes->aes_wcs_alloc = NULL;
  1576. }
  1577. aes->aes_mbs = NULL;
  1578. aes->aes_wcs_alloc = (wchar_t*)malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
  1579. if (aes->aes_wcs_alloc == NULL)
  1580. __archive_errx(1, "No memory for aes_copy_wcs()");
  1581. wcscpy(aes->aes_wcs_alloc, wcs);
  1582. aes->aes_wcs = aes->aes_wcs_alloc;
  1583. }
  1584. struct archive_entry *
  1585. archive_entry_clear(struct archive_entry *entry)
  1586. {
  1587. aes_clean(&entry->ae_fflags_text);
  1588. aes_clean(&entry->ae_gname);
  1589. aes_clean(&entry->ae_hardlink);
  1590. aes_clean(&entry->ae_pathname);
  1591. aes_clean(&entry->ae_symlink);
  1592. aes_clean(&entry->ae_uname);
  1593. archive_entry_acl_clear(entry);
  1594. memset(entry, 0, sizeof(*entry));
  1595. return entry;
  1596. }
  1597. struct archive_entry *
  1598. archive_entry_clone(struct archive_entry *entry)
  1599. {
  1600. struct archive_entry *entry2;
  1601. /* Allocate new structure and copy over all of the fields. */
  1602. entry2 = (struct archive_entry*)malloc(sizeof(*entry2));
  1603. if (entry2 == NULL)
  1604. return (NULL);
  1605. memset(entry2, 0, sizeof(*entry2));
  1606. entry2->ae_stat = entry->ae_stat;
  1607. entry2->ae_fflags_set = entry->ae_fflags_set;
  1608. entry2->ae_fflags_clear = entry->ae_fflags_clear;
  1609. aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
  1610. aes_copy(&entry2->ae_gname, &entry->ae_gname);
  1611. aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
  1612. aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
  1613. aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
  1614. aes_copy(&entry2->ae_uname, &entry->ae_uname);
  1615. /* XXX TODO: Copy ACL data over as well. XXX */
  1616. return (entry2);
  1617. }
  1618. void
  1619. archive_entry_free(struct archive_entry *entry)
  1620. {
  1621. archive_entry_clear(entry);
  1622. free(entry);
  1623. }
  1624. struct archive_entry *
  1625. archive_entry_new(void)
  1626. {
  1627. struct archive_entry *entry;
  1628. entry = (struct archive_entry*)malloc(sizeof(*entry));
  1629. if (entry == NULL)
  1630. return (NULL);
  1631. memset(entry, 0, sizeof(*entry));
  1632. return (entry);
  1633. }
  1634. /*
  1635. * Functions for reading fields from an archive_entry.
  1636. */
  1637. time_t
  1638. archive_entry_atime(struct archive_entry *entry)
  1639. {
  1640. return (entry->ae_stat.st_atime);
  1641. }
  1642. long
  1643. archive_entry_atime_nsec(struct archive_entry *entry)
  1644. {
  1645. (void)entry; /* entry can be unused here. */
  1646. return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
  1647. }
  1648. time_t
  1649. archive_entry_ctime(struct archive_entry *entry)
  1650. {
  1651. return (entry->ae_stat.st_ctime);
  1652. }
  1653. long
  1654. archive_entry_ctime_nsec(struct archive_entry *entry)
  1655. {
  1656. (void)entry; /* entry can be unused here. */
  1657. return (ARCHIVE_STAT_CTIME_NANOS(&entry->ae_stat));
  1658. }
  1659. dev_t
  1660. archive_entry_dev(struct archive_entry *entry)
  1661. {
  1662. return (entry->ae_stat.st_dev);
  1663. }
  1664. void
  1665. archive_entry_fflags(struct archive_entry *entry,
  1666. unsigned long *set, unsigned long *clear)
  1667. {
  1668. *set = entry->ae_fflags_set;
  1669. *clear = entry->ae_fflags_clear;
  1670. }
  1671. /*
  1672. * Note: if text was provided, this just returns that text. If you
  1673. * really need the text to be rebuilt in a canonical form, set the
  1674. * text, ask for the bitmaps, then set the bitmaps. (Setting the
  1675. * bitmaps clears any stored text.) This design is deliberate: if
  1676. * we're editing archives, we don't want to discard flags just because
  1677. * they aren't supported on the current system. The bitmap<->text
  1678. * conversions are platform-specific (see below).
  1679. */
  1680. const char *
  1681. archive_entry_fflags_text(struct archive_entry *entry)
  1682. {
  1683. const char *f;
  1684. char *p;
  1685. f = aes_get_mbs(&entry->ae_fflags_text);
  1686. if (f != NULL)
  1687. return (f);
  1688. if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
  1689. return (NULL);
  1690. p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
  1691. if (p == NULL)
  1692. return (NULL);
  1693. aes_copy_mbs(&entry->ae_fflags_text, p);
  1694. free(p);
  1695. f = aes_get_mbs(&entry->ae_fflags_text);
  1696. return (f);
  1697. }
  1698. gid_t
  1699. archive_entry_gid(struct archive_entry *entry)
  1700. {
  1701. return (entry->ae_stat.st_gid);
  1702. }
  1703. const char *
  1704. archive_entry_gname(struct archive_entry *entry)
  1705. {
  1706. return (aes_get_mbs(&entry->ae_gname));
  1707. }
  1708. const wchar_t *
  1709. archive_entry_gname_w(struct archive_entry *entry)
  1710. {
  1711. return (aes_get_wcs(&entry->ae_gname));
  1712. }
  1713. const char *
  1714. archive_entry_hardlink(struct archive_entry *entry)
  1715. {
  1716. return (aes_get_mbs(&entry->ae_hardlink));
  1717. }
  1718. const wchar_t *
  1719. archive_entry_hardlink_w(struct archive_entry *entry)
  1720. {
  1721. return (aes_get_wcs(&entry->ae_hardlink));
  1722. }
  1723. ino_t
  1724. archive_entry_ino(struct archive_entry *entry)
  1725. {
  1726. return (entry->ae_stat.st_ino);
  1727. }
  1728. mode_t
  1729. archive_entry_mode(struct archive_entry *entry)
  1730. {
  1731. return (entry->ae_stat.st_mode);
  1732. }
  1733. time_t
  1734. archive_entry_mtime(struct archive_entry *entry)
  1735. {
  1736. return (entry->ae_stat.st_mtime);
  1737. }
  1738. long
  1739. archive_entry_mtime_nsec(struct archive_entry *entry)
  1740. {
  1741. (void)entry; /* entry can be unused here. */
  1742. return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
  1743. }
  1744. const char *
  1745. archive_entry_pathname(struct archive_entry *entry)
  1746. {
  1747. return (aes_get_mbs(&entry->ae_pathname));
  1748. }
  1749. const wchar_t *
  1750. archive_entry_pathname_w(struct archive_entry *entry)
  1751. {
  1752. return (aes_get_wcs(&entry->ae_pathname));
  1753. }
  1754. dev_t
  1755. archive_entry_rdev(struct archive_entry *entry)
  1756. {
  1757. return (entry->ae_stat.st_rdev);
  1758. }
  1759. int64_t
  1760. archive_entry_size(struct archive_entry *entry)
  1761. {
  1762. return (entry->ae_stat.st_size);
  1763. }
  1764. const struct stat *
  1765. archive_entry_stat(struct archive_entry *entry)
  1766. {
  1767. return (&entry->ae_stat);
  1768. }
  1769. const char *
  1770. archive_entry_symlink(struct archive_entry *entry)
  1771. {
  1772. return (aes_get_mbs(&entry->ae_symlink));
  1773. }
  1774. const wchar_t *
  1775. archive_entry_symlink_w(struct archive_entry *entry)
  1776. {
  1777. return (aes_get_wcs(&entry->ae_symlink));
  1778. }
  1779. uid_t
  1780. archive_entry_uid(struct archive_entry *entry)
  1781. {
  1782. return (entry->ae_stat.st_uid);
  1783. }
  1784. const char *
  1785. archive_entry_uname(struct archive_entry *entry)
  1786. {
  1787. return (aes_get_mbs(&entry->ae_uname));
  1788. }
  1789. const wchar_t *
  1790. archive_entry_uname_w(struct archive_entry *entry)
  1791. {
  1792. return (aes_get_wcs(&entry->ae_uname));
  1793. }
  1794. /*
  1795. * Functions to set archive_entry properties.
  1796. */
  1797. /*
  1798. * Note "copy" not "set" here. The "set" functions that accept a pointer
  1799. * only store the pointer; they don't copy the underlying object.
  1800. */
  1801. void
  1802. archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
  1803. {
  1804. entry->ae_stat = *st;
  1805. }
  1806. void
  1807. archive_entry_set_fflags(struct archive_entry *entry,
  1808. unsigned long set, unsigned long clear)
  1809. {
  1810. aes_clean(&entry->ae_fflags_text);
  1811. entry->ae_fflags_set = set;
  1812. entry->ae_fflags_clear = clear;
  1813. }
  1814. const wchar_t *
  1815. archive_entry_copy_fflags_text_w(struct archive_entry *entry,
  1816. const wchar_t *flags)
  1817. {
  1818. aes_copy_wcs(&entry->ae_fflags_text, flags);
  1819. return (ae_wcstofflags(flags,
  1820. &entry->ae_fflags_set, &entry->ae_fflags_clear));
  1821. }
  1822. void
  1823. archive_entry_set_gid(struct archive_entry *entry, gid_t g)
  1824. {
  1825. entry->ae_stat.st_gid = g;
  1826. }
  1827. void
  1828. archive_entry_set_gname(struct archive_entry *entry, const char *name)
  1829. {
  1830. aes_set_mbs(&entry->ae_gname, name);
  1831. }
  1832. void
  1833. archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
  1834. {
  1835. aes_copy_wcs(&entry->ae_gname, name);
  1836. }
  1837. void
  1838. archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
  1839. {
  1840. aes_set_mbs(&entry->ae_hardlink, target);
  1841. }
  1842. void
  1843. archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
  1844. {
  1845. aes_copy_mbs(&entry->ae_hardlink, target);
  1846. }
  1847. void
  1848. archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
  1849. {
  1850. aes_copy_wcs(&entry->ae_hardlink, target);
  1851. }
  1852. void
  1853. archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
  1854. {
  1855. (void)ns;
  1856. entry->ae_stat.st_atime = t;
  1857. ARCHIVE_STAT_SET_ATIME_NANOS(&entry->ae_stat, ns);
  1858. }
  1859. void
  1860. archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
  1861. {
  1862. (void)ns;
  1863. entry->ae_stat.st_ctime = t;
  1864. ARCHIVE_STAT_SET_CTIME_NANOS(&entry->ae_stat, ns);
  1865. }
  1866. /* Set symlink if symlink is already set, else set hardlink. */
  1867. void
  1868. archive_entry_set_link(struct archive_entry *entry, const char *target)
  1869. {
  1870. if (entry->ae_symlink.aes_mbs != NULL ||
  1871. entry->ae_symlink.aes_wcs != NULL)
  1872. aes_set_mbs(&entry->ae_symlink, target);
  1873. else
  1874. aes_set_mbs(&entry->ae_hardlink, target);
  1875. }
  1876. void
  1877. archive_entry_set_mode(struct archive_entry *entry, mode_t m)
  1878. {
  1879. entry->ae_stat.st_mode = m;
  1880. }
  1881. void
  1882. archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
  1883. {
  1884. (void)ns;
  1885. entry->ae_stat.st_mtime = m;
  1886. ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
  1887. }
  1888. void
  1889. archive_entry_set_pathname(struct archive_entry *entry, const char *name)
  1890. {
  1891. aes_set_mbs(&entry->ae_pathname, name);
  1892. }
  1893. void
  1894. archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
  1895. {
  1896. aes_copy_mbs(&entry->ae_pathname, name);
  1897. }
  1898. void
  1899. archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
  1900. {
  1901. aes_copy_wcs(&entry->ae_pathname, name);
  1902. }
  1903. void
  1904. archive_entry_set_size(struct archive_entry *entry, int64_t s)
  1905. {
  1906. entry->ae_stat.st_size = s;
  1907. }
  1908. void
  1909. archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
  1910. {
  1911. aes_set_mbs(&entry->ae_symlink, linkname);
  1912. }
  1913. void
  1914. archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
  1915. {
  1916. aes_copy_wcs(&entry->ae_symlink, linkname);
  1917. }
  1918. void
  1919. archive_entry_set_uid(struct archive_entry *entry, uid_t u)
  1920. {
  1921. entry->ae_stat.st_uid = u;
  1922. }
  1923. void
  1924. archive_entry_set_uname(struct archive_entry *entry, const char *name)
  1925. {
  1926. aes_set_mbs(&entry->ae_uname, name);
  1927. }
  1928. void
  1929. archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
  1930. {
  1931. aes_copy_wcs(&entry->ae_uname, name);
  1932. }
  1933. /*
  1934. * ACL management. The following would, of course, be a lot simpler
  1935. * if: 1) the last draft of POSIX.1e were a really thorough and
  1936. * complete standard that addressed the needs of ACL archiving and 2)
  1937. * everyone followed it faithfully. Alas, neither is true, so the
  1938. * following is a lot more complex than might seem necessary to the
  1939. * uninitiated.
  1940. */
  1941. void
  1942. archive_entry_acl_clear(struct archive_entry *entry)
  1943. {
  1944. struct ae_acl *ap;
  1945. while (entry->acl_head != NULL) {
  1946. ap = entry->acl_head->next;
  1947. aes_clean(&entry->acl_head->name);
  1948. free(entry->acl_head);
  1949. entry->acl_head = ap;
  1950. }
  1951. if (entry->acl_text_w != NULL) {
  1952. free(entry->acl_text_w);
  1953. entry->acl_text_w = NULL;
  1954. }
  1955. entry->acl_p = NULL;
  1956. entry->acl_state = 0; /* Not counting. */
  1957. }
  1958. /*
  1959. * Add a single ACL entry to the internal list of ACL data.
  1960. */
  1961. void
  1962. archive_entry_acl_add_entry(struct archive_entry *entry,
  1963. int type, int permset, int tag, int id, const char *name)
  1964. {
  1965. struct ae_acl *ap;
  1966. if (acl_special(entry, type, permset, tag) == 0)
  1967. return;
  1968. ap = acl_new_entry(entry, type, permset, tag, id);
  1969. if (ap == NULL) {
  1970. /* XXX Error XXX */
  1971. return;
  1972. }
  1973. if (name != NULL && *name != '\0')
  1974. aes_copy_mbs(&ap->name, name);
  1975. else
  1976. aes_clean(&ap->name);
  1977. }
  1978. /*
  1979. * As above, but with a wide-character name.
  1980. */
  1981. void
  1982. archive_entry_acl_add_entry_w(struct archive_entry *entry,
  1983. int type, int permset, int tag, int id, const wchar_t *name)
  1984. {
  1985. struct ae_acl *ap;
  1986. if (acl_special(entry, type, permset, tag) == 0)
  1987. return;
  1988. ap = acl_new_entry(entry, type, permset, tag, id);
  1989. if (ap == NULL) {
  1990. /* XXX Error XXX */
  1991. return;
  1992. }
  1993. if (name != NULL && *name != L'\0')
  1994. aes_copy_wcs(&ap->name, name);
  1995. else
  1996. aes_clean(&ap->name);
  1997. }
  1998. /*
  1999. * If this ACL entry is part of the standard POSIX permissions set,
  2000. * store the permissions in the stat structure and return zero.
  2001. */
  2002. static int
  2003. acl_special(struct archive_entry *entry, int type, int permset, int tag)
  2004. {
  2005. if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
  2006. switch (tag) {
  2007. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  2008. entry->ae_stat.st_mode &= ~0700;
  2009. entry->ae_stat.st_mode |= (permset & 7) << 6;
  2010. return (0);
  2011. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  2012. entry->ae_stat.st_mode &= ~0070;
  2013. entry->ae_stat.st_mode |= (permset & 7) << 3;
  2014. return (0);
  2015. case ARCHIVE_ENTRY_ACL_OTHER:
  2016. entry->ae_stat.st_mode &= ~0007;
  2017. entry->ae_stat.st_mode |= permset & 7;
  2018. return (0);
  2019. }
  2020. }
  2021. return (1);
  2022. }
  2023. /*
  2024. * Allocate and populate a new ACL entry with everything but the
  2025. * name.
  2026. */
  2027. static struct ae_acl *
  2028. acl_new_entry(struct archive_entry *entry,
  2029. int type, int permset, int tag, int id)
  2030. {
  2031. struct ae_acl *ap;
  2032. if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
  2033. type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
  2034. return (NULL);
  2035. if (entry->acl_text_w != NULL) {
  2036. free(entry->acl_text_w);
  2037. entry->acl_text_w = NULL;
  2038. }
  2039. /* XXX TODO: More sanity-checks on the arguments XXX */
  2040. /* If there's a matching entry already in the list, overwrite it. */
  2041. for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
  2042. if (ap->type == type && ap->tag == tag && ap->id == id) {
  2043. ap->permset = permset;
  2044. return (ap);
  2045. }
  2046. }
  2047. /* Add a new entry to the list. */
  2048. ap = (struct ae_acl *)malloc(sizeof(*ap));
  2049. if (ap == NULL)
  2050. return (NULL);
  2051. memset(ap, 0, sizeof(*ap));
  2052. ap->next = entry->acl_head;
  2053. entry->acl_head = ap;
  2054. ap->type = type;
  2055. ap->tag = tag;
  2056. ap->id = id;
  2057. ap->permset = permset;
  2058. return (ap);
  2059. }
  2060. /*
  2061. * Return a count of entries matching "want_type".
  2062. */
  2063. int
  2064. archive_entry_acl_count(struct archive_entry *entry, int want_type)
  2065. {
  2066. int count;
  2067. struct ae_acl *ap;
  2068. count = 0;
  2069. ap = entry->acl_head;
  2070. while (ap != NULL) {
  2071. if ((ap->type & want_type) != 0)
  2072. count++;
  2073. ap = ap->next;
  2074. }
  2075. if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
  2076. count += 3;
  2077. return (count);
  2078. }
  2079. /*
  2080. * Prepare for reading entries from the ACL data. Returns a count
  2081. * of entries matching "want_type", or zero if there are no
  2082. * non-extended ACL entries of that type.
  2083. */
  2084. int
  2085. archive_entry_acl_reset(struct archive_entry *entry, int want_type)
  2086. {
  2087. int count, cutoff;
  2088. count = archive_entry_acl_count(entry, want_type);
  2089. /*
  2090. * If the only entries are the three standard ones,
  2091. * then don't return any ACL data. (In this case,
  2092. * client can just use chmod(2) to set permissions.)
  2093. */
  2094. if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
  2095. cutoff = 3;
  2096. else
  2097. cutoff = 0;
  2098. if (count > cutoff)
  2099. entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
  2100. else
  2101. entry->acl_state = 0;
  2102. entry->acl_p = entry->acl_head;
  2103. return (count);
  2104. }
  2105. /*
  2106. * Return the next ACL entry in the list. Fake entries for the
  2107. * standard permissions and include them in the returned list.
  2108. */
  2109. int
  2110. archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
  2111. int *permset, int *tag, int *id, const char **name)
  2112. {
  2113. *name = NULL;
  2114. *id = -1;
  2115. /*
  2116. * The acl_state is either zero (no entries available), -1
  2117. * (reading from list), or an entry type (retrieve that type
  2118. * from ae_stat.st_mode).
  2119. */
  2120. if (entry->acl_state == 0)
  2121. return (ARCHIVE_WARN);
  2122. /* The first three access entries are special. */
  2123. if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  2124. switch (entry->acl_state) {
  2125. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  2126. *permset = (entry->ae_stat.st_mode >> 6) & 7;
  2127. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  2128. *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  2129. entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  2130. return (ARCHIVE_OK);
  2131. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  2132. *permset = (entry->ae_stat.st_mode >> 3) & 7;
  2133. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  2134. *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  2135. entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
  2136. return (ARCHIVE_OK);
  2137. case ARCHIVE_ENTRY_ACL_OTHER:
  2138. *permset = entry->ae_stat.st_mode & 7;
  2139. *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  2140. *tag = ARCHIVE_ENTRY_ACL_OTHER;
  2141. entry->acl_state = -1;
  2142. entry->acl_p = entry->acl_head;
  2143. return (ARCHIVE_OK);
  2144. default:
  2145. break;
  2146. }
  2147. }
  2148. while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
  2149. entry->acl_p = entry->acl_p->next;
  2150. if (entry->acl_p == NULL) {
  2151. entry->acl_state = 0;
  2152. return (ARCHIVE_WARN);
  2153. }
  2154. *type = entry->acl_p->type;
  2155. *permset = entry->acl_p->permset;
  2156. *tag = entry->acl_p->tag;
  2157. *id = entry->acl_p->id;
  2158. *name = aes_get_mbs(&entry->acl_p->name);
  2159. entry->acl_p = entry->acl_p->next;
  2160. return (ARCHIVE_OK);
  2161. }
  2162. /*
  2163. * Generate a text version of the ACL. The flags parameter controls
  2164. * the style of the generated ACL.
  2165. */
  2166. const wchar_t *
  2167. archive_entry_acl_text_w(struct archive_entry *entry, int flags)
  2168. {
  2169. int count;
  2170. int length;
  2171. const wchar_t *wname;
  2172. const wchar_t *prefix;
  2173. wchar_t separator;
  2174. struct ae_acl *ap;
  2175. int id;
  2176. wchar_t *wp;
  2177. if (entry->acl_text_w != NULL) {
  2178. free (entry->acl_text_w);
  2179. entry->acl_text_w = NULL;
  2180. }
  2181. separator = L',';
  2182. count = 0;
  2183. length = 0;
  2184. ap = entry->acl_head;
  2185. while (ap != NULL) {
  2186. if ((ap->type & flags) != 0) {
  2187. count++;
  2188. if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
  2189. (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
  2190. length += 8; /* "default:" */
  2191. length += 5; /* tag name */
  2192. length += 1; /* colon */
  2193. wname = aes_get_wcs(&ap->name);
  2194. if (wname != NULL)
  2195. length += wcslen(wname);
  2196. length ++; /* colon */
  2197. length += 3; /* rwx */
  2198. length += 1; /* colon */
  2199. length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
  2200. length ++; /* newline */
  2201. }
  2202. ap = ap->next;
  2203. }
  2204. if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
  2205. length += 10; /* "user::rwx\n" */
  2206. length += 11; /* "group::rwx\n" */
  2207. length += 11; /* "other::rwx\n" */
  2208. }
  2209. if (count == 0)
  2210. return (NULL);
  2211. /* Now, allocate the string and actually populate it. */
  2212. wp = entry->acl_text_w = (wchar_t*)malloc(length * sizeof(wchar_t));
  2213. if (wp == NULL)
  2214. __archive_errx(1, "No memory to generate the text version of the ACL");
  2215. count = 0;
  2216. if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  2217. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
  2218. entry->ae_stat.st_mode & 0700, -1);
  2219. *wp++ = ',';
  2220. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
  2221. entry->ae_stat.st_mode & 0070, -1);
  2222. *wp++ = ',';
  2223. append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
  2224. entry->ae_stat.st_mode & 0007, -1);
  2225. count += 3;
  2226. ap = entry->acl_head;
  2227. while (ap != NULL) {
  2228. if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
  2229. wname = aes_get_wcs(&ap->name);
  2230. *wp++ = separator;
  2231. if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
  2232. id = ap->id;
  2233. else
  2234. id = -1;
  2235. append_entry_w(&wp, NULL, ap->tag, wname,
  2236. ap->permset, id);
  2237. count++;
  2238. }
  2239. ap = ap->next;
  2240. }
  2241. }
  2242. if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
  2243. if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
  2244. prefix = L"default:";
  2245. else
  2246. prefix = NULL;
  2247. ap = entry->acl_head;
  2248. count = 0;
  2249. while (ap != NULL) {
  2250. if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
  2251. wname = aes_get_wcs(&ap->name);
  2252. if (count > 0)
  2253. *wp++ = separator;
  2254. if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
  2255. id = ap->id;
  2256. else
  2257. id = -1;
  2258. append_entry_w(&wp, prefix, ap->tag,
  2259. wname, ap->permset, id);
  2260. count ++;
  2261. }
  2262. ap = ap->next;
  2263. }
  2264. }
  2265. return (entry->acl_text_w);
  2266. }
  2267. static void
  2268. append_id_w(wchar_t **wp, int id)
  2269. {
  2270. if (id > 9)
  2271. append_id_w(wp, id / 10);
  2272. *(*wp)++ = L"0123456789"[id % 10];
  2273. }
  2274. static void
  2275. append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
  2276. const wchar_t *wname, int perm, int id)
  2277. {
  2278. if (prefix != NULL) {
  2279. wcscpy(*wp, prefix);
  2280. *wp += wcslen(*wp);
  2281. }
  2282. switch (tag) {
  2283. case ARCHIVE_ENTRY_ACL_USER_OBJ:
  2284. wname = NULL;
  2285. id = -1;
  2286. /* FALL THROUGH */
  2287. case ARCHIVE_ENTRY_ACL_USER:
  2288. wcscpy(*wp, L"user");
  2289. break;
  2290. case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
  2291. wname = NULL;
  2292. id = -1;
  2293. /* FALL THROUGH */
  2294. case ARCHIVE_ENTRY_ACL_GROUP:
  2295. wcscpy(*wp, L"group");
  2296. break;
  2297. case ARCHIVE_ENTRY_ACL_MASK:
  2298. wcscpy(*wp, L"mask");
  2299. wname = NULL;
  2300. id = -1;
  2301. break;
  2302. case ARCHIVE_ENTRY_ACL_OTHER:
  2303. wcscpy(*wp, L"other");
  2304. wname = NULL;
  2305. id = -1;
  2306. break;
  2307. }
  2308. *wp += wcslen(*wp);
  2309. *(*wp)++ = L':';
  2310. if (wname != NULL) {
  2311. wcscpy(*wp, wname);
  2312. *wp += wcslen(*wp);
  2313. }
  2314. *(*wp)++ = L':';
  2315. *(*wp)++ = (perm & 0444) ? L'r' : L'-';
  2316. *(*wp)++ = (perm & 0222) ? L'w' : L'-';
  2317. *(*wp)++ = (perm & 0111) ? L'x' : L'-';
  2318. if (id != -1) {
  2319. *(*wp)++ = L':';
  2320. append_id_w(wp, id);
  2321. }
  2322. **wp = L'\0';
  2323. }
  2324. /*
  2325. * Parse a textual ACL. This automatically recognizes and supports
  2326. * extensions described above. The 'type' argument is used to
  2327. * indicate the type that should be used for any entries not
  2328. * explicitly marked as "default:".
  2329. */
  2330. int
  2331. __archive_entry_acl_parse_w(struct archive_entry *entry,
  2332. const wchar_t *text, int default_type)
  2333. {
  2334. int type, tag, permset, id;
  2335. const wchar_t *start, *end;
  2336. const wchar_t *name_start, *name_end;
  2337. wchar_t sep;
  2338. wchar_t *namebuff;
  2339. int namebuff_length;
  2340. name_start = name_end = NULL;
  2341. namebuff = NULL;
  2342. namebuff_length = 0;
  2343. while (text != NULL && *text != L'\0') {
  2344. next_field_w(&text, &start, &end, &sep);
  2345. if (sep != L':')
  2346. goto fail;
  2347. /*
  2348. * Solaris extension: "defaultuser::rwx" is the
  2349. * default ACL corresponding to "user::rwx", etc.
  2350. */
  2351. if (end-start > 7 && wmemcmp(start, L"default", 7) == 0) {
  2352. type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
  2353. start += 7;
  2354. } else
  2355. type = default_type;
  2356. if (prefix_w(start, end, L"user")) {
  2357. next_field_w(&text, &start, &end, &sep);
  2358. if (sep != L':')
  2359. goto fail;
  2360. if (end > start) {
  2361. tag = ARCHIVE_ENTRY_ACL_USER;
  2362. name_start = start;
  2363. name_end = end;
  2364. } else
  2365. tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
  2366. } else if (prefix_w(start, end, L"group")) {
  2367. next_field_w(&text, &start, &end, &sep);
  2368. if (sep != L':')
  2369. goto fail;
  2370. if (end > start) {
  2371. tag = ARCHIVE_ENTRY_ACL_GROUP;
  2372. name_start = start;
  2373. name_end = end;
  2374. } else
  2375. tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
  2376. } else if (prefix_w(start, end, L"other")) {
  2377. next_field_w(&text, &start, &end, &sep);
  2378. if (sep != L':')
  2379. goto fail;
  2380. if (end > start)
  2381. goto fail;
  2382. tag = ARCHIVE_ENTRY_ACL_OTHER;
  2383. } else if (prefix_w(start, end, L"mask")) {
  2384. next_field_w(&text, &start, &end, &sep);
  2385. if (sep != L':')
  2386. goto fail;
  2387. if (end > start)
  2388. goto fail;
  2389. tag = ARCHIVE_ENTRY_ACL_MASK;
  2390. } else
  2391. goto fail;
  2392. next_field_w(&text, &start, &end, &sep);
  2393. permset = 0;
  2394. while (start < end) {
  2395. switch (*start++) {
  2396. case 'r': case 'R':
  2397. permset |= ARCHIVE_ENTRY_ACL_READ;
  2398. break;
  2399. case 'w': case 'W':
  2400. permset |= ARCHIVE_ENTRY_ACL_WRITE;
  2401. break;
  2402. case 'x': case 'X':
  2403. permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
  2404. break;
  2405. case '-':
  2406. break;
  2407. default:
  2408. goto fail;
  2409. }
  2410. }
  2411. /*
  2412. * Support star-compatible numeric UID/GID extension.
  2413. * This extension adds a ":" followed by the numeric
  2414. * ID so that "group:groupname:rwx", for example,
  2415. * becomes "group:groupname:rwx:999", where 999 is the
  2416. * numeric GID. This extension makes it possible, for
  2417. * example, to correctly restore ACLs on a system that
  2418. * might have a damaged passwd file or be disconnected
  2419. * from a central NIS server. This extension is compatible
  2420. * with POSIX.1e draft 17.
  2421. */
  2422. if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
  2423. tag == ARCHIVE_ENTRY_ACL_GROUP)) {
  2424. next_field_w(&text, &start, &end, &sep);
  2425. id = 0;
  2426. while (start < end && *start >= '0' && *start <= '9') {
  2427. if (id > (INT_MAX / 10))
  2428. id = INT_MAX;
  2429. else {
  2430. id *= 10;
  2431. id += *start - '0';
  2432. start++;
  2433. }
  2434. }
  2435. } else
  2436. id = -1; /* No id specified. */
  2437. /* Skip any additional entries. */
  2438. while (sep == L':') {
  2439. next_field_w(&text, &start, &end, &sep);
  2440. }
  2441. /* Add entry to the internal list. */
  2442. if (name_end == name_start) {
  2443. archive_entry_acl_add_entry_w(entry, type, permset,
  2444. tag, id, NULL);
  2445. } else {
  2446. if (namebuff_length <= name_end - name_start) {
  2447. if (namebuff != NULL)
  2448. free(namebuff);
  2449. namebuff_length = name_end - name_start + 256;
  2450. namebuff =
  2451. (wchar_t*)malloc(namebuff_length * sizeof(wchar_t));
  2452. if (namebuff == NULL)
  2453. goto fail;
  2454. }
  2455. wmemcpy(namebuff, name_start, name_end - name_start);
  2456. namebuff[name_end - name_start] = L'\0';
  2457. archive_entry_acl_add_entry_w(entry, type,
  2458. permset, tag, id, namebuff);
  2459. }
  2460. }
  2461. if (namebuff != NULL)
  2462. free(namebuff);
  2463. return (ARCHIVE_OK);
  2464. fail:
  2465. if (namebuff != NULL)
  2466. free(namebuff);
  2467. return (ARCHIVE_WARN);
  2468. }
  2469. /*
  2470. * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
  2471. * to point to just after the separator. *start points to the first
  2472. * character of the matched text and *end just after the last
  2473. * character of the matched identifier. In particular *end - *start
  2474. * is the length of the field body, not including leading or trailing
  2475. * whitespace.
  2476. */
  2477. static void
  2478. next_field_w(const wchar_t **wp, const wchar_t **start,
  2479. const wchar_t **end, wchar_t *sep)
  2480. {
  2481. /* Skip leading whitespace to find start of field. */
  2482. while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
  2483. (*wp)++;
  2484. }
  2485. *start = *wp;
  2486. /* Scan for the separator. */
  2487. while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
  2488. **wp != L'\n') {
  2489. (*wp)++;
  2490. }
  2491. *sep = **wp;
  2492. /* Trim trailing whitespace to locate end of field. */
  2493. *end = *wp - 1;
  2494. while (**end == L' ' || **end == L'\t' || **end == L'\n') {
  2495. (*end)--;
  2496. }
  2497. (*end)++;
  2498. /* Adjust scanner location. */
  2499. if (**wp != L'\0')
  2500. (*wp)++;
  2501. }
  2502. static int
  2503. prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
  2504. {
  2505. if (start == end)
  2506. return (0);
  2507. if (*start++ != *test++)
  2508. return (0);
  2509. while (start < end && *start++ == *test++)
  2510. ;
  2511. if (start < end)
  2512. return (0);
  2513. return (1);
  2514. }
  2515. /*
  2516. * Following code is modified from UC Berkeley sources, and
  2517. * is subject to the following copyright notice.
  2518. */
  2519. /*-
  2520. * Copyright (c) 1993
  2521. * The Regents of the University of California. All rights reserved.
  2522. *
  2523. * Redistribution and use in source and binary forms, with or without
  2524. * modification, are permitted provided that the following conditions
  2525. * are met:
  2526. * 1. Redistributions of source code must retain the above copyright
  2527. * notice, this list of conditions and the following disclaimer.
  2528. * 2. Redistributions in binary form must reproduce the above copyright
  2529. * notice, this list of conditions and the following disclaimer in the
  2530. * documentation and/or other materials provided with the distribution.
  2531. * 4. Neither the name of the University nor the names of its contributors
  2532. * may be used to endorse or promote products derived from this software
  2533. * without specific prior written permission.
  2534. *
  2535. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2536. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2537. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2538. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2539. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2540. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2541. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2542. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2543. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2544. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2545. * SUCH DAMAGE.
  2546. */
  2547. static struct flag {
  2548. const char *name;
  2549. const wchar_t *wname;
  2550. unsigned long set;
  2551. unsigned long clear;
  2552. } flags[] = {
  2553. /* Preferred (shorter) names per flag first, all prefixed by "no" */
  2554. #ifdef SF_APPEND
  2555. { "nosappnd", L"nosappnd", SF_APPEND, 0 },
  2556. { "nosappend", L"nosappend", SF_APPEND, 0 },
  2557. #endif
  2558. #ifdef EXT2_APPEND_FL /* 'a' */
  2559. { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
  2560. { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
  2561. #endif
  2562. #ifdef SF_ARCHIVED
  2563. { "noarch", L"noarch", SF_ARCHIVED, 0 },
  2564. { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
  2565. #endif
  2566. #ifdef SF_IMMUTABLE
  2567. { "noschg", L"noschg", SF_IMMUTABLE, 0 },
  2568. { "noschange", L"noschange", SF_IMMUTABLE, 0 },
  2569. { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
  2570. #endif
  2571. #ifdef EXT2_IMMUTABLE_FL /* 'i' */
  2572. { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
  2573. { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
  2574. { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
  2575. #endif
  2576. #ifdef SF_NOUNLINK
  2577. { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
  2578. { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
  2579. #endif
  2580. #ifdef SF_SNAPSHOT
  2581. { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
  2582. #endif
  2583. #ifdef UF_APPEND
  2584. { "nouappnd", L"nouappnd", UF_APPEND, 0 },
  2585. { "nouappend", L"nouappend", UF_APPEND, 0 },
  2586. #endif
  2587. #ifdef UF_IMMUTABLE
  2588. { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
  2589. { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
  2590. { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
  2591. #endif
  2592. #ifdef UF_NODUMP
  2593. { "nodump", L"nodump", 0, UF_NODUMP},
  2594. #endif
  2595. #ifdef EXT2_NODUMP_FL /* 'd' */
  2596. { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
  2597. #endif
  2598. #ifdef UF_OPAQUE
  2599. { "noopaque", L"noopaque", UF_OPAQUE, 0 },
  2600. #endif
  2601. #ifdef UF_NOUNLINK
  2602. { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
  2603. { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
  2604. #endif
  2605. #ifdef EXT2_COMPR_FL /* 'c' */
  2606. { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
  2607. #endif
  2608. #ifdef EXT2_NOATIME_FL /* 'A' */
  2609. { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
  2610. #endif
  2611. { NULL, NULL, 0, 0 }
  2612. };
  2613. /*
  2614. * fflagstostr --
  2615. * Convert file flags to a comma-separated string. If no flags
  2616. * are set, return the empty string.
  2617. */
  2618. char *
  2619. ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
  2620. {
  2621. char *string, *dp;
  2622. const char *sp;
  2623. unsigned long bits;
  2624. struct flag *flag;
  2625. int length;
  2626. bits = bitset | bitclear;
  2627. length = 0;
  2628. for (flag = flags; flag->name != NULL; flag++)
  2629. if (bits & (flag->set | flag->clear)) {
  2630. length += strlen(flag->name) + 1;
  2631. bits &= ~(flag->set | flag->clear);
  2632. }
  2633. if (length == 0)
  2634. return (NULL);
  2635. string = (char*)malloc(length);
  2636. if (string == NULL)
  2637. return (NULL);
  2638. dp = string;
  2639. for (flag = flags; flag->name != NULL; flag++) {
  2640. if (bitset & flag->set || bitclear & flag->clear) {
  2641. sp = flag->name + 2;
  2642. } else if (bitset & flag->clear || bitclear & flag->set) {
  2643. sp = flag->name;
  2644. } else
  2645. continue;
  2646. bitset &= ~(flag->set | flag->clear);
  2647. bitclear &= ~(flag->set | flag->clear);
  2648. if (dp > string)
  2649. *dp++ = ',';
  2650. while ((*dp++ = *sp++) != '\0')
  2651. ;
  2652. dp--;
  2653. }
  2654. *dp = '\0';
  2655. return (string);
  2656. }
  2657. /*
  2658. * wcstofflags --
  2659. * Take string of arguments and return file flags. This
  2660. * version works a little differently than strtofflags(3).
  2661. * In particular, it always tests every token, skipping any
  2662. * unrecognized tokens. It returns a pointer to the first
  2663. * unrecognized token, or NULL if every token was recognized.
  2664. * This version is also const-correct and does not modify the
  2665. * provided string.
  2666. */
  2667. const wchar_t *
  2668. ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
  2669. {
  2670. const wchar_t *start, *end;
  2671. struct flag *flag;
  2672. unsigned long set, clear;
  2673. const wchar_t *failed;
  2674. set = clear = 0;
  2675. start = s;
  2676. failed = NULL;
  2677. /* Find start of first token. */
  2678. while (*start == L'\t' || *start == L' ' || *start == L',')
  2679. start++;
  2680. while (*start != L'\0') {
  2681. /* Locate end of token. */
  2682. end = start;
  2683. while (*end != L'\0' && *end != L'\t' &&
  2684. *end != L' ' && *end != L',')
  2685. end++;
  2686. for (flag = flags; flag->wname != NULL; flag++) {
  2687. if (wmemcmp(start, flag->wname, end - start) == 0) {
  2688. /* Matched "noXXXX", so reverse the sense. */
  2689. clear |= flag->set;
  2690. set |= flag->clear;
  2691. break;
  2692. } else if (wmemcmp(start, flag->wname + 2, end - start)
  2693. == 0) {
  2694. /* Matched "XXXX", so don't reverse. */
  2695. set |= flag->set;
  2696. clear |= flag->clear;
  2697. break;
  2698. }
  2699. }
  2700. /* Ignore unknown flag names. */
  2701. if (flag->wname == NULL && failed == NULL)
  2702. failed = start;
  2703. /* Find start of next token. */
  2704. start = end;
  2705. while (*start == L'\t' || *start == L' ' || *start == L',')
  2706. start++;
  2707. }
  2708. if (setp)
  2709. *setp = set;
  2710. if (clrp)
  2711. *clrp = clear;
  2712. /* Return location of first failure. */
  2713. return (failed);
  2714. }
  2715. //--- archive_string_sprintf.c
  2716. /*
  2717. * Like 'vsprintf', but ensures the target is big enough, resizing if
  2718. * necessary.
  2719. */
  2720. void
  2721. __archive_string_vsprintf(struct archive_string *as, const char *fmt,
  2722. va_list ap)
  2723. {
  2724. char long_flag;
  2725. intmax_t s; /* Signed integer temp. */
  2726. uintmax_t u; /* Unsigned integer temp. */
  2727. const char *p, *p2;
  2728. __archive_string_ensure(as, 64);
  2729. if (fmt == NULL) {
  2730. as->s[0] = 0;
  2731. return;
  2732. }
  2733. long_flag = '\0';
  2734. for (p = fmt; *p != '\0'; p++) {
  2735. const char *saved_p = p;
  2736. if (*p != '%') {
  2737. archive_strappend_char(as, *p);
  2738. continue;
  2739. }
  2740. p++;
  2741. switch(*p) {
  2742. case 'j':
  2743. long_flag = 'j';
  2744. p++;
  2745. break;
  2746. case 'l':
  2747. long_flag = 'l';
  2748. p++;
  2749. break;
  2750. }
  2751. switch (*p) {
  2752. case '%':
  2753. __archive_strappend_char(as, '%');
  2754. break;
  2755. case 'c':
  2756. s = va_arg(ap, int);
  2757. __archive_strappend_char(as, s);
  2758. break;
  2759. case 'd':
  2760. switch(long_flag) {
  2761. case 'j': s = va_arg(ap, intmax_t); break;
  2762. case 'l': s = va_arg(ap, long); break;
  2763. default: s = va_arg(ap, int); break;
  2764. }
  2765. archive_strappend_int(as, s, 10);
  2766. break;
  2767. case 's':
  2768. p2 = va_arg(ap, char *);
  2769. archive_strcat(as, p2);
  2770. break;
  2771. case 'o': case 'u': case 'x': case 'X':
  2772. /* Common handling for unsigned integer formats. */
  2773. switch(long_flag) {
  2774. case 'j': u = va_arg(ap, uintmax_t); break;
  2775. case 'l': u = va_arg(ap, unsigned long); break;
  2776. default: u = va_arg(ap, unsigned int); break;
  2777. }
  2778. /* Format it in the correct base. */
  2779. switch (*p) {
  2780. case 'o': archive_strappend_int(as, u, 8); break;
  2781. case 'u': archive_strappend_int(as, u, 10); break;
  2782. default: archive_strappend_int(as, u, 16); break;
  2783. }
  2784. break;
  2785. default:
  2786. /* Rewind and print the initial '%' literally. */
  2787. p = saved_p;
  2788. archive_strappend_char(as, *p);
  2789. }
  2790. }
  2791. }
  2792. //----------------------------------------------------------------------
  2793. class cmCPackTGZ_Data
  2794. {
  2795. public:
  2796. cmCPackTGZ_Data(cmCPackTGZGenerator* gen) :
  2797. Name(0), OutputStream(0), Generator(gen) {}
  2798. const char *Name;
  2799. std::ostream* OutputStream;
  2800. cmCPackTGZGenerator* Generator;
  2801. };
  2802. //----------------------------------------------------------------------
  2803. int cmCPackTGZGenerator::TGZ_Open(struct archive *a, void *client_data)
  2804. {
  2805. cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
  2806. (void)a;
  2807. mydata->OutputStream = new cmGeneratedFileStream(mydata->Name);
  2808. if ( *mydata->OutputStream &&
  2809. mydata->Generator->GenerateHeader(mydata->OutputStream))
  2810. {
  2811. return (ARCHIVE_OK);
  2812. }
  2813. else
  2814. {
  2815. return (ARCHIVE_FATAL);
  2816. }
  2817. }
  2818. //----------------------------------------------------------------------
  2819. ssize_t cmCPackTGZGenerator::TGZ_Write(struct archive *a, void *client_data, void *buff, size_t n)
  2820. {
  2821. cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
  2822. (void)a;
  2823. mydata->OutputStream->write(reinterpret_cast<const char*>(buff), n);
  2824. if ( !*mydata->OutputStream )
  2825. {
  2826. return 0;
  2827. }
  2828. return n;
  2829. }
  2830. //----------------------------------------------------------------------
  2831. int cmCPackTGZGenerator::TGZ_Close(struct archive *a, void *client_data)
  2832. {
  2833. cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
  2834. (void)a;
  2835. delete mydata->OutputStream;
  2836. return (0);
  2837. }
  2838. //----------------------------------------------------------------------
  2839. int cmCPackTGZGenerator::CompressFiles(const char* outFileName, const char* toplevel,
  2840. const std::vector<std::string>& files)
  2841. {
  2842. std::cout << "Toplevel: " << toplevel << std::endl;
  2843. cmCPackTGZ_Data mydata(this);
  2844. struct archive *a;
  2845. struct archive_entry *entry;
  2846. struct stat st;
  2847. char buff[8192];
  2848. int len;
  2849. int fd;
  2850. a = archive_write_new();
  2851. mydata.Name = outFileName;
  2852. archive_write_set_compression_gzip(a);
  2853. archive_write_set_format_ustar(a);
  2854. archive_write_open(a, &mydata, cmCPackTGZGenerator::TGZ_Open,
  2855. cmCPackTGZGenerator::TGZ_Write, cmCPackTGZGenerator::TGZ_Close);
  2856. std::vector<std::string>::const_iterator fileIt;
  2857. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  2858. {
  2859. std::string fname = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
  2860. const char* filename = fileIt->c_str();
  2861. stat(filename, &st);
  2862. entry = archive_entry_new();
  2863. archive_entry_copy_stat(entry, &st);
  2864. archive_entry_set_pathname(entry, fname.c_str());
  2865. archive_write_header(a, entry);
  2866. fd = open(filename, O_RDONLY);
  2867. len = read(fd, buff, sizeof(buff));
  2868. while ( len > 0 )
  2869. {
  2870. archive_write_data(a, buff, len);
  2871. len = read(fd, buff, sizeof(buff));
  2872. }
  2873. archive_entry_free(entry);
  2874. }
  2875. archive_write_finish(a);
  2876. return 1;
  2877. }
  2878. //--- archive_write_set_format_ustar.c
  2879. struct ustar {
  2880. uint64_t entry_bytes_remaining;
  2881. uint64_t entry_padding;
  2882. char written;
  2883. };
  2884. /*
  2885. * Define structure of POSIX 'ustar' tar header.
  2886. */
  2887. struct archive_entry_header_ustar {
  2888. char name[100];
  2889. char mode[6];
  2890. char mode_padding[2];
  2891. char uid[6];
  2892. char uid_padding[2];
  2893. char gid[6];
  2894. char gid_padding[2];
  2895. char size[11];
  2896. char size_padding[1];
  2897. char mtime[11];
  2898. char mtime_padding[1];
  2899. char checksum[8];
  2900. char typeflag[1];
  2901. char linkname[100];
  2902. char magic[6]; /* For POSIX: "ustar\0" */
  2903. char version[2]; /* For POSIX: "00" */
  2904. char uname[32];
  2905. char gname[32];
  2906. char rdevmajor[6];
  2907. char rdevmajor_padding[2];
  2908. char rdevminor[6];
  2909. char rdevminor_padding[2];
  2910. char prefix[155];
  2911. char padding[12];
  2912. };
  2913. /*
  2914. * A filled-in copy of the header for initialization.
  2915. */
  2916. static const struct archive_entry_header_ustar template_header = {
  2917. { "" }, /* name */
  2918. { '0','0','0','0','0','0' }, { ' ', '\0' }, /* mode, space-null termination. */
  2919. { '0','0','0','0','0','0' }, { ' ', '\0' }, /* uid, space-null termination. */
  2920. { '0','0','0','0','0','0' }, { ' ', '\0' }, /* gid, space-null termination. */
  2921. { '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' }, /* size, space termination. */
  2922. { '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' }, /* mtime, space termination. */
  2923. { ' ',' ',' ',' ',' ',' ',' ',' ' }, /* Initial checksum value. */
  2924. { '0' }, /* default: regular file */
  2925. { "" }, /* linkname */
  2926. { 'u','s','t','a','r' }, /* magic */
  2927. { '0', '0' }, /* version */
  2928. { "" }, /* uname */
  2929. { "" }, /* gname */
  2930. {'0','0','0','0','0','0'}, { ' ', '\0' }, /* rdevmajor, space-null termination */
  2931. {'0','0','0','0','0','0'}, { ' ', '\0' }, /* rdevminor, space-null termination */
  2932. { "" }, /* prefix */
  2933. { "" } /* padding */
  2934. };
  2935. static int archive_write_ustar_data(struct archive *a, const void *buff,
  2936. size_t s);
  2937. static int archive_write_ustar_finish(struct archive *);
  2938. static int archive_write_ustar_finish_entry(struct archive *);
  2939. static int archive_write_ustar_header(struct archive *,
  2940. struct archive_entry *entry);
  2941. static int format_256(int64_t, char *, int);
  2942. static int format_number(int64_t, char *, int size, int max, int strict);
  2943. static int format_octal(int64_t, char *, int);
  2944. static int write_nulls(struct archive *a, size_t);
  2945. /*
  2946. * Set output format to 'ustar' format.
  2947. */
  2948. int
  2949. archive_write_set_format_ustar(struct archive *a)
  2950. {
  2951. struct ustar *ustar;
  2952. /* If someone else was already registered, unregister them. */
  2953. if (a->format_finish != NULL)
  2954. (a->format_finish)(a);
  2955. ustar = (struct ustar*)malloc(sizeof(*ustar));
  2956. if (ustar == NULL) {
  2957. archive_set_error(a, ENOMEM, "Can't allocate ustar data");
  2958. return (ARCHIVE_FATAL);
  2959. }
  2960. memset(ustar, 0, sizeof(*ustar));
  2961. a->format_data = ustar;
  2962. a->pad_uncompressed = 1; /* Mimic gtar in this respect. */
  2963. a->format_write_header = archive_write_ustar_header;
  2964. a->format_write_data = archive_write_ustar_data;
  2965. a->format_finish = archive_write_ustar_finish;
  2966. a->format_finish_entry = archive_write_ustar_finish_entry;
  2967. a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
  2968. a->archive_format_name = "POSIX ustar";
  2969. return (ARCHIVE_OK);
  2970. }
  2971. static int
  2972. archive_write_ustar_header(struct archive *a, struct archive_entry *entry)
  2973. {
  2974. char buff[512];
  2975. int ret;
  2976. struct ustar *ustar;
  2977. ustar = (struct ustar*)a->format_data;
  2978. ustar->written = 1;
  2979. /* Only regular files (not hardlinks) have data. */
  2980. if (archive_entry_hardlink(entry) != NULL ||
  2981. archive_entry_symlink(entry) != NULL ||
  2982. !S_ISREG(archive_entry_mode(entry)))
  2983. archive_entry_set_size(entry, 0);
  2984. ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1);
  2985. if (ret != ARCHIVE_OK)
  2986. return (ret);
  2987. ret = (a->compression_write)(a, buff, 512);
  2988. if (ret != ARCHIVE_OK)
  2989. return (ret);
  2990. ustar->entry_bytes_remaining = archive_entry_size(entry);
  2991. ustar->entry_padding = 0x1ff & (- ustar->entry_bytes_remaining);
  2992. return (ARCHIVE_OK);
  2993. }
  2994. /*
  2995. * Format a basic 512-byte "ustar" header.
  2996. *
  2997. * Returns -1 if format failed (due to field overflow).
  2998. * Note that this always formats as much of the header as possible.
  2999. * If "strict" is set to zero, it will extend numeric fields as
  3000. * necessary (overwriting terminators or using base-256 extensions).
  3001. *
  3002. * This is exported so that other 'tar' formats can use it.
  3003. */
  3004. int
  3005. __archive_write_format_header_ustar(struct archive *a, char buff[512],
  3006. struct archive_entry *entry, int tartype, int strict)
  3007. {
  3008. unsigned int checksum;
  3009. struct archive_entry_header_ustar *h;
  3010. int i, ret;
  3011. size_t copy_length;
  3012. const char *p, *pp;
  3013. const struct stat *st;
  3014. int mytartype;
  3015. ret = 0;
  3016. mytartype = -1;
  3017. /*
  3018. * The "template header" already includes the "ustar"
  3019. * signature, various end-of-field markers and other required
  3020. * elements.
  3021. */
  3022. memcpy(buff, &template_header, 512);
  3023. h = (struct archive_entry_header_ustar *)buff;
  3024. /*
  3025. * Because the block is already null-filled, and strings
  3026. * are allowed to exactly fill their destination (without null),
  3027. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  3028. */
  3029. pp = archive_entry_pathname(entry);
  3030. if (strlen(pp) <= sizeof(h->name))
  3031. memcpy(h->name, pp, strlen(pp));
  3032. else {
  3033. /* Store in two pieces, splitting at a '/'. */
  3034. p = strchr(pp + strlen(pp) - sizeof(h->name) - 1, '/');
  3035. /*
  3036. * If there is no path separator, or the prefix or
  3037. * remaining name are too large, return an error.
  3038. */
  3039. if (!p) {
  3040. archive_set_error(a, ENAMETOOLONG,
  3041. "Pathname too long");
  3042. ret = ARCHIVE_WARN;
  3043. } else if (p > pp + sizeof(h->prefix)) {
  3044. archive_set_error(a, ENAMETOOLONG,
  3045. "Pathname too long");
  3046. ret = ARCHIVE_WARN;
  3047. } else {
  3048. /* Copy prefix and remainder to appropriate places */
  3049. memcpy(h->prefix, pp, p - pp);
  3050. memcpy(h->name, p + 1, pp + strlen(pp) - p - 1);
  3051. }
  3052. }
  3053. p = archive_entry_hardlink(entry);
  3054. if (p != NULL)
  3055. mytartype = '1';
  3056. else
  3057. p = archive_entry_symlink(entry);
  3058. if (p != NULL && p[0] != '\0') {
  3059. copy_length = strlen(p);
  3060. if (copy_length > sizeof(h->linkname)) {
  3061. archive_set_error(a, ENAMETOOLONG,
  3062. "Link contents too long");
  3063. ret = ARCHIVE_WARN;
  3064. copy_length = sizeof(h->linkname);
  3065. }
  3066. memcpy(h->linkname, p, copy_length);
  3067. }
  3068. p = archive_entry_uname(entry);
  3069. if (p != NULL && p[0] != '\0') {
  3070. copy_length = strlen(p);
  3071. if (copy_length > sizeof(h->uname)) {
  3072. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3073. "Username too long");
  3074. ret = ARCHIVE_WARN;
  3075. copy_length = sizeof(h->uname);
  3076. }
  3077. memcpy(h->uname, p, copy_length);
  3078. }
  3079. p = archive_entry_gname(entry);
  3080. if (p != NULL && p[0] != '\0') {
  3081. copy_length = strlen(p);
  3082. if (strlen(p) > sizeof(h->gname)) {
  3083. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3084. "Group name too long");
  3085. ret = ARCHIVE_WARN;
  3086. copy_length = sizeof(h->gname);
  3087. }
  3088. memcpy(h->gname, p, copy_length);
  3089. }
  3090. st = archive_entry_stat(entry);
  3091. if (format_number(st->st_mode & 07777, h->mode, sizeof(h->mode), 8, strict)) {
  3092. archive_set_error(a, ERANGE, "Numeric mode too large");
  3093. ret = ARCHIVE_WARN;
  3094. }
  3095. if (format_number(st->st_uid, h->uid, sizeof(h->uid), 8, strict)) {
  3096. archive_set_error(a, ERANGE, "Numeric user ID too large");
  3097. ret = ARCHIVE_WARN;
  3098. }
  3099. if (format_number(st->st_gid, h->gid, sizeof(h->gid), 8, strict)) {
  3100. archive_set_error(a, ERANGE, "Numeric group ID too large");
  3101. ret = ARCHIVE_WARN;
  3102. }
  3103. if (format_number(st->st_size, h->size, sizeof(h->size), 12, strict)) {
  3104. archive_set_error(a, ERANGE, "File size out of range");
  3105. ret = ARCHIVE_WARN;
  3106. }
  3107. if (format_number(st->st_mtime, h->mtime, sizeof(h->mtime), 12, strict)) {
  3108. archive_set_error(a, ERANGE,
  3109. "File modification time too large");
  3110. ret = ARCHIVE_WARN;
  3111. }
  3112. #if defined(S_ISBLK)
  3113. if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
  3114. if (format_number(major(st->st_rdev), h->rdevmajor,
  3115. sizeof(h->rdevmajor), 8, strict)) {
  3116. archive_set_error(a, ERANGE,
  3117. "Major device number too large");
  3118. ret = ARCHIVE_WARN;
  3119. }
  3120. if (format_number(minor(st->st_rdev), h->rdevminor,
  3121. sizeof(h->rdevminor), 8, strict)) {
  3122. archive_set_error(a, ERANGE,
  3123. "Minor device number too large");
  3124. ret = ARCHIVE_WARN;
  3125. }
  3126. }
  3127. #endif
  3128. if (tartype >= 0) {
  3129. h->typeflag[0] = tartype;
  3130. } else if (mytartype >= 0) {
  3131. h->typeflag[0] = mytartype;
  3132. } else {
  3133. switch (st->st_mode & S_IFMT) {
  3134. case S_IFREG: h->typeflag[0] = '0' ; break;
  3135. #if defined(S_IFLNK)
  3136. case S_IFLNK: h->typeflag[0] = '2' ; break;
  3137. #endif
  3138. case S_IFCHR: h->typeflag[0] = '3' ; break;
  3139. #if defined(S_IFBLK)
  3140. case S_IFBLK: h->typeflag[0] = '4' ; break;
  3141. #endif
  3142. case S_IFDIR: h->typeflag[0] = '5' ; break;
  3143. #if defined(S_IFIFO)
  3144. case S_IFIFO: h->typeflag[0] = '6' ; break;
  3145. #endif
  3146. #if defined(S_IFSOCK)
  3147. case S_IFSOCK:
  3148. archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
  3149. "tar format cannot archive socket");
  3150. ret = ARCHIVE_WARN;
  3151. break;
  3152. #endif
  3153. default:
  3154. archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
  3155. "tar format cannot archive this (mode=0%lo)",
  3156. (unsigned long)st->st_mode);
  3157. ret = ARCHIVE_WARN;
  3158. }
  3159. }
  3160. checksum = 0;
  3161. for (i = 0; i < 512; i++)
  3162. checksum += 255 & (unsigned int)buff[i];
  3163. h->checksum[6] = '\0'; /* Can't be pre-set in the template. */
  3164. /* h->checksum[7] = ' '; */ /* This is pre-set in the template. */
  3165. format_octal(checksum, h->checksum, 6);
  3166. return (ret);
  3167. }
  3168. /*
  3169. * Format a number into a field, with some intelligence.
  3170. */
  3171. static int
  3172. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  3173. {
  3174. int64_t limit;
  3175. limit = ((int64_t)1 << (s*3));
  3176. /* "Strict" only permits octal values with proper termination. */
  3177. if (strict)
  3178. return (format_octal(v, p, s));
  3179. /*
  3180. * In non-strict mode, we allow the number to overwrite one or
  3181. * more bytes of the field termination. Even old tar
  3182. * implementations should be able to handle this with no
  3183. * problem.
  3184. */
  3185. if (v >= 0) {
  3186. while (s <= maxsize) {
  3187. if (v < limit)
  3188. return (format_octal(v, p, s));
  3189. s++;
  3190. limit <<= 3;
  3191. }
  3192. }
  3193. /* Base-256 can handle any number, positive or negative. */
  3194. return (format_256(v, p, maxsize));
  3195. }
  3196. /*
  3197. * Format a number into the specified field using base-256.
  3198. */
  3199. static int
  3200. format_256(int64_t v, char *p, int s)
  3201. {
  3202. p += s;
  3203. while (s-- > 0) {
  3204. *--p = (char)(v & 0xff);
  3205. v >>= 8;
  3206. }
  3207. *p |= 0x80; /* Set the base-256 marker bit. */
  3208. return (0);
  3209. }
  3210. /*
  3211. * Format a number into the specified field.
  3212. */
  3213. static int
  3214. format_octal(int64_t v, char *p, int s)
  3215. {
  3216. int len;
  3217. len = s;
  3218. /* Octal values can't be negative, so use 0. */
  3219. if (v < 0) {
  3220. while (len-- > 0)
  3221. *p++ = '0';
  3222. return (-1);
  3223. }
  3224. p += s; /* Start at the end and work backwards. */
  3225. while (s-- > 0) {
  3226. *--p = '0' + (v & 7);
  3227. v >>= 3;
  3228. }
  3229. if (v == 0)
  3230. return (0);
  3231. /* If it overflowed, fill field with max value. */
  3232. while (len-- > 0)
  3233. *p++ = '7';
  3234. return (-1);
  3235. }
  3236. static int
  3237. archive_write_ustar_finish(struct archive *a)
  3238. {
  3239. struct ustar *ustar;
  3240. int r;
  3241. r = ARCHIVE_OK;
  3242. ustar = (struct ustar*)a->format_data;
  3243. /*
  3244. * Suppress end-of-archive if nothing else was ever written.
  3245. * This fixes a problem where setting one format, then another
  3246. * ends up writing a gratuitous end-of-archive marker.
  3247. */
  3248. if (ustar->written && a->compression_write != NULL)
  3249. r = write_nulls(a, 512*2);
  3250. free(ustar);
  3251. a->format_data = NULL;
  3252. return (r);
  3253. }
  3254. static int
  3255. archive_write_ustar_finish_entry(struct archive *a)
  3256. {
  3257. struct ustar *ustar;
  3258. int ret;
  3259. ustar = (struct ustar*) a->format_data;
  3260. ret = write_nulls(a,
  3261. ustar->entry_bytes_remaining + ustar->entry_padding);
  3262. ustar->entry_bytes_remaining = ustar->entry_padding = 0;
  3263. return (ret);
  3264. }
  3265. static int
  3266. write_nulls(struct archive *a, size_t padding)
  3267. {
  3268. int ret, to_write;
  3269. while (padding > 0) {
  3270. to_write = padding < a->null_length ? padding : a->null_length;
  3271. ret = (a->compression_write)(a, a->nulls, to_write);
  3272. if (ret != ARCHIVE_OK)
  3273. return (ret);
  3274. padding -= to_write;
  3275. }
  3276. return (ARCHIVE_OK);
  3277. }
  3278. static int
  3279. archive_write_ustar_data(struct archive *a, const void *buff, size_t s)
  3280. {
  3281. struct ustar *ustar;
  3282. int ret;
  3283. ustar = (struct ustar*)a->format_data;
  3284. if (s > ustar->entry_bytes_remaining)
  3285. s = ustar->entry_bytes_remaining;
  3286. ret = (a->compression_write)(a, buff, s);
  3287. ustar->entry_bytes_remaining -= s;
  3288. return (ret);
  3289. }
  3290. //--- archive_write_set_compression_gzip.c
  3291. struct private_data {
  3292. z_stream stream;
  3293. int64_t total_in;
  3294. unsigned char *compressed;
  3295. size_t compressed_buffer_size;
  3296. unsigned long crc;
  3297. };
  3298. /*
  3299. * Yuck. zlib.h is not const-correct, so I need this one bit
  3300. * of ugly hackery to convert a const * pointer to a non-const pointer.
  3301. */
  3302. #define SET_NEXT_IN(st,src) \
  3303. (st)->stream.next_in = (Bytef*)(void *)(uintptr_t)(const void *)(src)
  3304. static int archive_compressor_gzip_finish(struct archive *);
  3305. static int archive_compressor_gzip_init(struct archive *);
  3306. static int archive_compressor_gzip_write(struct archive *, const void *,
  3307. size_t);
  3308. static int drive_compressor(struct archive *, struct private_data *,
  3309. int finishing);
  3310. /*
  3311. * Allocate, initialize and return a archive object.
  3312. */
  3313. int
  3314. archive_write_set_compression_gzip(struct archive *a)
  3315. {
  3316. __archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_compression_gzip");
  3317. a->compression_init = &archive_compressor_gzip_init;
  3318. a->compression_code = ARCHIVE_COMPRESSION_GZIP;
  3319. a->compression_name = "gzip";
  3320. return (ARCHIVE_OK);
  3321. }
  3322. /*
  3323. * Setup callback.
  3324. */
  3325. static int
  3326. archive_compressor_gzip_init(struct archive *a)
  3327. {
  3328. int ret;
  3329. struct private_data *state;
  3330. time_t t;
  3331. a->compression_code = ARCHIVE_COMPRESSION_GZIP;
  3332. a->compression_name = "gzip";
  3333. if (a->client_opener != NULL) {
  3334. ret = (a->client_opener)(a, a->client_data);
  3335. if (ret != ARCHIVE_OK)
  3336. return (ret);
  3337. }
  3338. state = (struct private_data *)malloc(sizeof(*state));
  3339. if (state == NULL) {
  3340. archive_set_error(a, ENOMEM,
  3341. "Can't allocate data for compression");
  3342. return (ARCHIVE_FATAL);
  3343. }
  3344. memset(state, 0, sizeof(*state));
  3345. state->compressed_buffer_size = a->bytes_per_block;
  3346. state->compressed = (unsigned char*)malloc(state->compressed_buffer_size);
  3347. state->crc = crc32(0L, NULL, 0);
  3348. if (state->compressed == NULL) {
  3349. archive_set_error(a, ENOMEM,
  3350. "Can't allocate data for compression buffer");
  3351. free(state);
  3352. return (ARCHIVE_FATAL);
  3353. }
  3354. state->stream.next_out = state->compressed;
  3355. state->stream.avail_out = state->compressed_buffer_size;
  3356. /* Prime output buffer with a gzip header. */
  3357. t = time(NULL);
  3358. state->compressed[0] = 0x1f; /* GZip signature bytes */
  3359. state->compressed[1] = 0x8b;
  3360. state->compressed[2] = 0x08; /* "Deflate" compression */
  3361. state->compressed[3] = 0; /* No options */
  3362. state->compressed[4] = (t)&0xff; /* Timestamp */
  3363. state->compressed[5] = (t>>8)&0xff;
  3364. state->compressed[6] = (t>>16)&0xff;
  3365. state->compressed[7] = (t>>24)&0xff;
  3366. state->compressed[8] = 0; /* No deflate options */
  3367. state->compressed[9] = 3; /* OS=Unix */
  3368. state->stream.next_out += 10;
  3369. state->stream.avail_out -= 10;
  3370. a->compression_write = archive_compressor_gzip_write;
  3371. a->compression_finish = archive_compressor_gzip_finish;
  3372. /* Initialize compression library. */
  3373. ret = deflateInit2(&(state->stream),
  3374. Z_DEFAULT_COMPRESSION,
  3375. Z_DEFLATED,
  3376. -15 /* < 0 to suppress zlib header */,
  3377. 8,
  3378. Z_DEFAULT_STRATEGY);
  3379. if (ret == Z_OK) {
  3380. a->compression_data = state;
  3381. return (0);
  3382. }
  3383. /* Library setup failed: clean up. */
  3384. archive_set_error(a, ARCHIVE_ERRNO_MISC, "Internal error "
  3385. "initializing compression library");
  3386. free(state->compressed);
  3387. free(state);
  3388. /* Override the error message if we know what really went wrong. */
  3389. switch (ret) {
  3390. case Z_STREAM_ERROR:
  3391. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3392. "Internal error initializing "
  3393. "compression library: invalid setup parameter");
  3394. break;
  3395. case Z_MEM_ERROR:
  3396. archive_set_error(a, ENOMEM, "Internal error initializing "
  3397. "compression library");
  3398. break;
  3399. case Z_VERSION_ERROR:
  3400. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3401. "Internal error initializing "
  3402. "compression library: invalid library version");
  3403. break;
  3404. }
  3405. return (ARCHIVE_FATAL);
  3406. }
  3407. /*
  3408. * Write data to the compressed stream.
  3409. */
  3410. static int
  3411. archive_compressor_gzip_write(struct archive *a, const void *buff,
  3412. size_t length)
  3413. {
  3414. struct private_data *state;
  3415. int ret;
  3416. state = (struct private_data*)a->compression_data;
  3417. if (a->client_writer == NULL) {
  3418. archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
  3419. "No write callback is registered? "
  3420. "This is probably an internal programming error.");
  3421. return (ARCHIVE_FATAL);
  3422. }
  3423. /* Update statistics */
  3424. state->crc = crc32(state->crc, (const Bytef*)buff, length);
  3425. state->total_in += length;
  3426. /* Compress input data to output buffer */
  3427. SET_NEXT_IN(state, buff);
  3428. state->stream.avail_in = length;
  3429. if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
  3430. return (ret);
  3431. a->file_position += length;
  3432. return (ARCHIVE_OK);
  3433. }
  3434. /*
  3435. * Finish the compression...
  3436. */
  3437. static int
  3438. archive_compressor_gzip_finish(struct archive *a)
  3439. {
  3440. ssize_t block_length, target_block_length, bytes_written;
  3441. int ret;
  3442. struct private_data *state;
  3443. unsigned tocopy;
  3444. unsigned char trailer[8];
  3445. state = (struct private_data*)a->compression_data;
  3446. ret = 0;
  3447. if (a->client_writer == NULL) {
  3448. archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
  3449. "No write callback is registered? "
  3450. "This is probably an internal programming error.");
  3451. ret = ARCHIVE_FATAL;
  3452. goto cleanup;
  3453. }
  3454. /* By default, always pad the uncompressed data. */
  3455. if (a->pad_uncompressed) {
  3456. tocopy = a->bytes_per_block -
  3457. (state->total_in % a->bytes_per_block);
  3458. while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
  3459. SET_NEXT_IN(state, a->nulls);
  3460. state->stream.avail_in = tocopy < a->null_length ?
  3461. tocopy : a->null_length;
  3462. state->crc = crc32(state->crc, a->nulls,
  3463. state->stream.avail_in);
  3464. state->total_in += state->stream.avail_in;
  3465. tocopy -= state->stream.avail_in;
  3466. ret = drive_compressor(a, state, 0);
  3467. if (ret != ARCHIVE_OK)
  3468. goto cleanup;
  3469. }
  3470. }
  3471. /* Finish compression cycle */
  3472. if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
  3473. goto cleanup;
  3474. /* Build trailer: 4-byte CRC and 4-byte length. */
  3475. trailer[0] = (state->crc)&0xff;
  3476. trailer[1] = (state->crc >> 8)&0xff;
  3477. trailer[2] = (state->crc >> 16)&0xff;
  3478. trailer[3] = (state->crc >> 24)&0xff;
  3479. trailer[4] = (state->total_in)&0xff;
  3480. trailer[5] = (state->total_in >> 8)&0xff;
  3481. trailer[6] = (state->total_in >> 16)&0xff;
  3482. trailer[7] = (state->total_in >> 24)&0xff;
  3483. /* Add trailer to current block. */
  3484. tocopy = 8;
  3485. if (tocopy > state->stream.avail_out)
  3486. tocopy = state->stream.avail_out;
  3487. memcpy(state->stream.next_out, trailer, tocopy);
  3488. state->stream.next_out += tocopy;
  3489. state->stream.avail_out -= tocopy;
  3490. /* If it overflowed, flush and start a new block. */
  3491. if (tocopy < 8) {
  3492. bytes_written = (a->client_writer)(a, a->client_data,
  3493. state->compressed, state->compressed_buffer_size);
  3494. if (bytes_written <= 0) {
  3495. ret = ARCHIVE_FATAL;
  3496. goto cleanup;
  3497. }
  3498. a->raw_position += bytes_written;
  3499. state->stream.next_out = state->compressed;
  3500. state->stream.avail_out = state->compressed_buffer_size;
  3501. memcpy(state->stream.next_out, trailer + tocopy, 8-tocopy);
  3502. state->stream.next_out += 8-tocopy;
  3503. state->stream.avail_out -= 8-tocopy;
  3504. }
  3505. /* Optionally, pad the final compressed block. */
  3506. block_length = state->stream.next_out - state->compressed;
  3507. /* Tricky calculation to determine size of last block. */
  3508. target_block_length = block_length;
  3509. if (a->bytes_in_last_block <= 0)
  3510. /* Default or Zero: pad to full block */
  3511. target_block_length = a->bytes_per_block;
  3512. else
  3513. /* Round length to next multiple of bytes_in_last_block. */
  3514. target_block_length = a->bytes_in_last_block *
  3515. ( (block_length + a->bytes_in_last_block - 1) /
  3516. a->bytes_in_last_block);
  3517. if (target_block_length > a->bytes_per_block)
  3518. target_block_length = a->bytes_per_block;
  3519. if (block_length < target_block_length) {
  3520. memset(state->stream.next_out, 0,
  3521. target_block_length - block_length);
  3522. block_length = target_block_length;
  3523. }
  3524. /* Write the last block */
  3525. bytes_written = (a->client_writer)(a, a->client_data,
  3526. state->compressed, block_length);
  3527. if (bytes_written <= 0) {
  3528. ret = ARCHIVE_FATAL;
  3529. goto cleanup;
  3530. }
  3531. a->raw_position += bytes_written;
  3532. /* Cleanup: shut down compressor, release memory, etc. */
  3533. cleanup:
  3534. switch (deflateEnd(&(state->stream))) {
  3535. case Z_OK:
  3536. break;
  3537. default:
  3538. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3539. "Failed to clean up compressor");
  3540. ret = ARCHIVE_FATAL;
  3541. }
  3542. free(state->compressed);
  3543. free(state);
  3544. /* Close the output */
  3545. if (a->client_closer != NULL)
  3546. (a->client_closer)(a, a->client_data);
  3547. return (ret);
  3548. }
  3549. /*
  3550. * Utility function to push input data through compressor,
  3551. * writing full output blocks as necessary.
  3552. *
  3553. * Note that this handles both the regular write case (finishing ==
  3554. * false) and the end-of-archive case (finishing == true).
  3555. */
  3556. static int
  3557. drive_compressor(struct archive *a, struct private_data *state, int finishing)
  3558. {
  3559. ssize_t bytes_written;
  3560. int ret;
  3561. for (;;) {
  3562. if (state->stream.avail_out == 0) {
  3563. bytes_written = (a->client_writer)(a, a->client_data,
  3564. state->compressed, state->compressed_buffer_size);
  3565. if (bytes_written <= 0) {
  3566. /* TODO: Handle this write failure */
  3567. return (ARCHIVE_FATAL);
  3568. } else if ((size_t)bytes_written < state->compressed_buffer_size) {
  3569. /* Short write: Move remaining to
  3570. * front of block and keep filling */
  3571. memmove(state->compressed,
  3572. state->compressed + bytes_written,
  3573. state->compressed_buffer_size - bytes_written);
  3574. }
  3575. a->raw_position += bytes_written;
  3576. state->stream.next_out
  3577. = state->compressed +
  3578. state->compressed_buffer_size - bytes_written;
  3579. state->stream.avail_out = bytes_written;
  3580. }
  3581. ret = deflate(&(state->stream),
  3582. finishing ? Z_FINISH : Z_NO_FLUSH );
  3583. switch (ret) {
  3584. case Z_OK:
  3585. /* In non-finishing case, check if compressor
  3586. * consumed everything */
  3587. if (!finishing && state->stream.avail_in == 0)
  3588. return (ARCHIVE_OK);
  3589. /* In finishing case, this return always means
  3590. * there's more work */
  3591. break;
  3592. case Z_STREAM_END:
  3593. /* This return can only occur in finishing case. */
  3594. return (ARCHIVE_OK);
  3595. default:
  3596. /* Any other return value indicates an error. */
  3597. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  3598. "GZip compression failed");
  3599. return (ARCHIVE_FATAL);
  3600. }
  3601. }
  3602. }