archive_write_disk_posix.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * Copyright (c) 2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD$");
  29. #if !defined(_WIN32) || defined(__CYGWIN__)
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_ACL_H
  34. #include <sys/acl.h>
  35. #endif
  36. #ifdef HAVE_SYS_EXTATTR_H
  37. #include <sys/extattr.h>
  38. #endif
  39. #if HAVE_SYS_XATTR_H
  40. #include <sys/xattr.h>
  41. #elif HAVE_ATTR_XATTR_H
  42. #include <attr/xattr.h>
  43. #endif
  44. #ifdef HAVE_SYS_EA_H
  45. #include <sys/ea.h>
  46. #endif
  47. #ifdef HAVE_SYS_IOCTL_H
  48. #include <sys/ioctl.h>
  49. #endif
  50. #ifdef HAVE_SYS_STAT_H
  51. #include <sys/stat.h>
  52. #endif
  53. #ifdef HAVE_SYS_TIME_H
  54. #include <sys/time.h>
  55. #endif
  56. #ifdef HAVE_SYS_UTIME_H
  57. #include <sys/utime.h>
  58. #endif
  59. #ifdef HAVE_COPYFILE_H
  60. #include <copyfile.h>
  61. #endif
  62. #ifdef HAVE_ERRNO_H
  63. #include <errno.h>
  64. #endif
  65. #ifdef HAVE_FCNTL_H
  66. #include <fcntl.h>
  67. #endif
  68. #ifdef HAVE_GRP_H
  69. #include <grp.h>
  70. #endif
  71. #ifdef HAVE_LANGINFO_H
  72. #include <langinfo.h>
  73. #endif
  74. #ifdef HAVE_LINUX_FS_H
  75. #include <linux/fs.h> /* for Linux file flags */
  76. #endif
  77. /*
  78. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  79. * As the include guards don't agree, the order of include is important.
  80. */
  81. #ifdef HAVE_LINUX_EXT2_FS_H
  82. #include <linux/ext2_fs.h> /* for Linux file flags */
  83. #endif
  84. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  85. #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
  86. #endif
  87. #ifdef HAVE_LIMITS_H
  88. #include <limits.h>
  89. #endif
  90. #ifdef HAVE_PWD_H
  91. #include <pwd.h>
  92. #endif
  93. #include <stdio.h>
  94. #ifdef HAVE_STDLIB_H
  95. #include <stdlib.h>
  96. #endif
  97. #ifdef HAVE_STRING_H
  98. #include <string.h>
  99. #endif
  100. #ifdef HAVE_UNISTD_H
  101. #include <unistd.h>
  102. #endif
  103. #ifdef HAVE_UTIME_H
  104. #include <utime.h>
  105. #endif
  106. #ifdef F_GETTIMES /* Tru64 specific */
  107. #include <sys/fcntl1.h>
  108. #endif
  109. /*
  110. * Macro to cast st_mtime and time_t to an int64 so that 2 numbers can reliably be compared.
  111. *
  112. * It assumes that the input is an integer type of no more than 64 bits.
  113. * If the number is less than zero, t must be a signed type, so it fits in
  114. * int64_t. Otherwise, it's a nonnegative value so we can cast it to uint64_t
  115. * without loss. But it could be a large unsigned value, so we have to clip it
  116. * to INT64_MAX.*
  117. */
  118. #define to_int64_time(t) \
  119. ((t) < 0 ? (int64_t)(t) : (uint64_t)(t) > (uint64_t)INT64_MAX ? INT64_MAX : (int64_t)(t))
  120. #if __APPLE__
  121. #include <TargetConditionals.h>
  122. #if TARGET_OS_MAC && !TARGET_OS_EMBEDDED && HAVE_QUARANTINE_H
  123. #include <quarantine.h>
  124. #define HAVE_QUARANTINE 1
  125. #endif
  126. #endif
  127. #ifdef HAVE_ZLIB_H
  128. #include <cm_zlib.h>
  129. #endif
  130. /* TODO: Support Mac OS 'quarantine' feature. This is really just a
  131. * standard tag to mark files that have been downloaded as "tainted".
  132. * On Mac OS, we should mark the extracted files as tainted if the
  133. * archive being read was tainted. Windows has a similar feature; we
  134. * should investigate ways to support this generically. */
  135. #include "archive.h"
  136. #include "archive_acl_private.h"
  137. #include "archive_string.h"
  138. #include "archive_endian.h"
  139. #include "archive_entry.h"
  140. #include "archive_private.h"
  141. #include "archive_write_disk_private.h"
  142. #ifndef O_BINARY
  143. #define O_BINARY 0
  144. #endif
  145. #ifndef O_CLOEXEC
  146. #define O_CLOEXEC 0
  147. #endif
  148. /* Ignore non-int O_NOFOLLOW constant. */
  149. /* gnulib's fcntl.h does this on AIX, but it seems practical everywhere */
  150. #if defined O_NOFOLLOW && !(INT_MIN <= O_NOFOLLOW && O_NOFOLLOW <= INT_MAX)
  151. #undef O_NOFOLLOW
  152. #endif
  153. #ifndef O_NOFOLLOW
  154. #define O_NOFOLLOW 0
  155. #endif
  156. #ifndef AT_FDCWD
  157. #define AT_FDCWD -100
  158. #endif
  159. struct fixup_entry {
  160. struct fixup_entry *next;
  161. struct archive_acl acl;
  162. mode_t mode;
  163. int64_t atime;
  164. int64_t birthtime;
  165. int64_t mtime;
  166. int64_t ctime;
  167. unsigned long atime_nanos;
  168. unsigned long birthtime_nanos;
  169. unsigned long mtime_nanos;
  170. unsigned long ctime_nanos;
  171. unsigned long fflags_set;
  172. size_t mac_metadata_size;
  173. void *mac_metadata;
  174. int fixup; /* bitmask of what needs fixing */
  175. char *name;
  176. };
  177. /*
  178. * We use a bitmask to track which operations remain to be done for
  179. * this file. In particular, this helps us avoid unnecessary
  180. * operations when it's possible to take care of one step as a
  181. * side-effect of another. For example, mkdir() can specify the mode
  182. * for the newly-created object but symlink() cannot. This means we
  183. * can skip chmod() if mkdir() succeeded, but we must explicitly
  184. * chmod() if we're trying to create a directory that already exists
  185. * (mkdir() failed) or if we're restoring a symlink. Similarly, we
  186. * need to verify UID/GID before trying to restore SUID/SGID bits;
  187. * that verification can occur explicitly through a stat() call or
  188. * implicitly because of a successful chown() call.
  189. */
  190. #define TODO_MODE_FORCE 0x40000000
  191. #define TODO_MODE_BASE 0x20000000
  192. #define TODO_SUID 0x10000000
  193. #define TODO_SUID_CHECK 0x08000000
  194. #define TODO_SGID 0x04000000
  195. #define TODO_SGID_CHECK 0x02000000
  196. #define TODO_APPLEDOUBLE 0x01000000
  197. #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
  198. #define TODO_TIMES ARCHIVE_EXTRACT_TIME
  199. #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
  200. #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
  201. #define TODO_ACLS ARCHIVE_EXTRACT_ACL
  202. #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
  203. #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
  204. #define TODO_HFS_COMPRESSION ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED
  205. struct archive_write_disk {
  206. struct archive archive;
  207. mode_t user_umask;
  208. struct fixup_entry *fixup_list;
  209. struct fixup_entry *current_fixup;
  210. int64_t user_uid;
  211. int skip_file_set;
  212. int64_t skip_file_dev;
  213. int64_t skip_file_ino;
  214. time_t start_time;
  215. int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
  216. void (*cleanup_gid)(void *private);
  217. void *lookup_gid_data;
  218. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
  219. void (*cleanup_uid)(void *private);
  220. void *lookup_uid_data;
  221. /*
  222. * Full path of last file to satisfy symlink checks.
  223. */
  224. struct archive_string path_safe;
  225. /*
  226. * Cached stat data from disk for the current entry.
  227. * If this is valid, pst points to st. Otherwise,
  228. * pst is null.
  229. */
  230. struct stat st;
  231. struct stat *pst;
  232. /* Information about the object being restored right now. */
  233. struct archive_entry *entry; /* Entry being extracted. */
  234. char *name; /* Name of entry, possibly edited. */
  235. struct archive_string _name_data; /* backing store for 'name' */
  236. char *tmpname; /* Temporary name * */
  237. struct archive_string _tmpname_data; /* backing store for 'tmpname' */
  238. /* Tasks remaining for this object. */
  239. int todo;
  240. /* Tasks deferred until end-of-archive. */
  241. int deferred;
  242. /* Options requested by the client. */
  243. int flags;
  244. /* Handle for the file we're restoring. */
  245. int fd;
  246. /* Current offset for writing data to the file. */
  247. int64_t offset;
  248. /* Last offset actually written to disk. */
  249. int64_t fd_offset;
  250. /* Total bytes actually written to files. */
  251. int64_t total_bytes_written;
  252. /* Maximum size of file, -1 if unknown. */
  253. int64_t filesize;
  254. /* Dir we were in before this restore; only for deep paths. */
  255. int restore_pwd;
  256. /* Mode we should use for this entry; affected by _PERM and umask. */
  257. mode_t mode;
  258. /* UID/GID to use in restoring this entry. */
  259. int64_t uid;
  260. int64_t gid;
  261. /*
  262. * HFS+ Compression.
  263. */
  264. /* Xattr "com.apple.decmpfs". */
  265. uint32_t decmpfs_attr_size;
  266. unsigned char *decmpfs_header_p;
  267. /* ResourceFork set options used for fsetxattr. */
  268. int rsrc_xattr_options;
  269. /* Xattr "com.apple.ResourceFork". */
  270. unsigned char *resource_fork;
  271. size_t resource_fork_allocated_size;
  272. unsigned int decmpfs_block_count;
  273. uint32_t *decmpfs_block_info;
  274. /* Buffer for compressed data. */
  275. unsigned char *compressed_buffer;
  276. size_t compressed_buffer_size;
  277. size_t compressed_buffer_remaining;
  278. /* The offset of the ResourceFork where compressed data will
  279. * be placed. */
  280. uint32_t compressed_rsrc_position;
  281. uint32_t compressed_rsrc_position_v;
  282. /* Buffer for uncompressed data. */
  283. char *uncompressed_buffer;
  284. size_t block_remaining_bytes;
  285. size_t file_remaining_bytes;
  286. #ifdef HAVE_ZLIB_H
  287. z_stream stream;
  288. int stream_valid;
  289. int decmpfs_compression_level;
  290. #endif
  291. };
  292. /*
  293. * Default mode for dirs created automatically (will be modified by umask).
  294. * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
  295. * by the process' file creation mask."
  296. */
  297. #define DEFAULT_DIR_MODE 0777
  298. /*
  299. * Dir modes are restored in two steps: During the extraction, the permissions
  300. * in the archive are modified to match the following limits. During
  301. * the post-extract fixup pass, the permissions from the archive are
  302. * applied.
  303. */
  304. #define MINIMUM_DIR_MODE 0700
  305. #define MAXIMUM_DIR_MODE 0775
  306. /*
  307. * Maximum uncompressed size of a decmpfs block.
  308. */
  309. #define MAX_DECMPFS_BLOCK_SIZE (64 * 1024)
  310. /*
  311. * HFS+ compression type.
  312. */
  313. #define CMP_XATTR 3/* Compressed data in xattr. */
  314. #define CMP_RESOURCE_FORK 4/* Compressed data in resource fork. */
  315. /*
  316. * HFS+ compression resource fork.
  317. */
  318. #define RSRC_H_SIZE 260 /* Base size of Resource fork header. */
  319. #define RSRC_F_SIZE 50 /* Size of Resource fork footer. */
  320. /* Size to write compressed data to resource fork. */
  321. #define COMPRESSED_W_SIZE (64 * 1024)
  322. /* decmpfs definitions. */
  323. #define MAX_DECMPFS_XATTR_SIZE 3802
  324. #ifndef DECMPFS_XATTR_NAME
  325. #define DECMPFS_XATTR_NAME "com.apple.decmpfs"
  326. #endif
  327. #define DECMPFS_MAGIC 0x636d7066
  328. #define DECMPFS_COMPRESSION_MAGIC 0
  329. #define DECMPFS_COMPRESSION_TYPE 4
  330. #define DECMPFS_UNCOMPRESSED_SIZE 8
  331. #define DECMPFS_HEADER_SIZE 16
  332. #define HFS_BLOCKS(s) ((s) >> 12)
  333. static int la_opendirat(int, const char *);
  334. static int la_mktemp(struct archive_write_disk *);
  335. static void fsobj_error(int *, struct archive_string *, int, const char *,
  336. const char *);
  337. static int check_symlinks_fsobj(char *, int *, struct archive_string *,
  338. int);
  339. static int check_symlinks(struct archive_write_disk *);
  340. static int create_filesystem_object(struct archive_write_disk *);
  341. static struct fixup_entry *current_fixup(struct archive_write_disk *,
  342. const char *pathname);
  343. #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
  344. static void edit_deep_directories(struct archive_write_disk *ad);
  345. #endif
  346. static int cleanup_pathname_fsobj(char *, int *, struct archive_string *,
  347. int);
  348. static int cleanup_pathname(struct archive_write_disk *);
  349. static int create_dir(struct archive_write_disk *, char *);
  350. static int create_parent_dir(struct archive_write_disk *, char *);
  351. static ssize_t hfs_write_data_block(struct archive_write_disk *,
  352. const char *, size_t);
  353. static int fixup_appledouble(struct archive_write_disk *, const char *);
  354. static int older(struct stat *, struct archive_entry *);
  355. static int restore_entry(struct archive_write_disk *);
  356. static int set_mac_metadata(struct archive_write_disk *, const char *,
  357. const void *, size_t);
  358. static int set_xattrs(struct archive_write_disk *);
  359. static int clear_nochange_fflags(struct archive_write_disk *);
  360. static int set_fflags(struct archive_write_disk *);
  361. static int set_fflags_platform(struct archive_write_disk *, int fd,
  362. const char *name, mode_t mode,
  363. unsigned long fflags_set, unsigned long fflags_clear);
  364. static int set_ownership(struct archive_write_disk *);
  365. static int set_mode(struct archive_write_disk *, int mode);
  366. static int set_time(int, int, const char *, time_t, long, time_t, long);
  367. static int set_times(struct archive_write_disk *, int, int, const char *,
  368. time_t, long, time_t, long, time_t, long, time_t, long);
  369. static int set_times_from_entry(struct archive_write_disk *);
  370. static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
  371. static ssize_t write_data_block(struct archive_write_disk *,
  372. const char *, size_t);
  373. static struct archive_vtable *archive_write_disk_vtable(void);
  374. static int _archive_write_disk_close(struct archive *);
  375. static int _archive_write_disk_free(struct archive *);
  376. static int _archive_write_disk_header(struct archive *,
  377. struct archive_entry *);
  378. static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
  379. static int _archive_write_disk_finish_entry(struct archive *);
  380. static ssize_t _archive_write_disk_data(struct archive *, const void *,
  381. size_t);
  382. static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
  383. size_t, int64_t);
  384. static int
  385. la_mktemp(struct archive_write_disk *a)
  386. {
  387. int oerrno, fd;
  388. mode_t mode;
  389. archive_string_empty(&a->_tmpname_data);
  390. archive_string_sprintf(&a->_tmpname_data, "%s.XXXXXX", a->name);
  391. a->tmpname = a->_tmpname_data.s;
  392. fd = __archive_mkstemp(a->tmpname);
  393. if (fd == -1)
  394. return -1;
  395. mode = a->mode & 0777 & ~a->user_umask;
  396. if (fchmod(fd, mode) == -1) {
  397. oerrno = errno;
  398. close(fd);
  399. errno = oerrno;
  400. return -1;
  401. }
  402. return fd;
  403. }
  404. static int
  405. la_opendirat(int fd, const char *path) {
  406. const int flags = O_CLOEXEC
  407. #if defined(O_BINARY)
  408. | O_BINARY
  409. #endif
  410. #if defined(O_DIRECTORY)
  411. | O_DIRECTORY
  412. #endif
  413. #if defined(O_PATH)
  414. | O_PATH
  415. #elif defined(O_SEARCH)
  416. | O_SEARCH
  417. #elif defined(__FreeBSD__) && defined(O_EXEC)
  418. | O_EXEC
  419. #else
  420. | O_RDONLY
  421. #endif
  422. ;
  423. #if !defined(HAVE_OPENAT)
  424. if (fd != AT_FDCWD) {
  425. errno = ENOTSUP;
  426. return (-1);
  427. } else
  428. return (open(path, flags));
  429. #else
  430. return (openat(fd, path, flags));
  431. #endif
  432. }
  433. static int
  434. lazy_stat(struct archive_write_disk *a)
  435. {
  436. if (a->pst != NULL) {
  437. /* Already have stat() data available. */
  438. return (ARCHIVE_OK);
  439. }
  440. #ifdef HAVE_FSTAT
  441. if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
  442. a->pst = &a->st;
  443. return (ARCHIVE_OK);
  444. }
  445. #endif
  446. /*
  447. * XXX At this point, symlinks should not be hit, otherwise
  448. * XXX a race occurred. Do we want to check explicitly for that?
  449. */
  450. if (lstat(a->name, &a->st) == 0) {
  451. a->pst = &a->st;
  452. return (ARCHIVE_OK);
  453. }
  454. archive_set_error(&a->archive, errno, "Couldn't stat file");
  455. return (ARCHIVE_WARN);
  456. }
  457. static struct archive_vtable *
  458. archive_write_disk_vtable(void)
  459. {
  460. static struct archive_vtable av;
  461. static int inited = 0;
  462. if (!inited) {
  463. av.archive_close = _archive_write_disk_close;
  464. av.archive_filter_bytes = _archive_write_disk_filter_bytes;
  465. av.archive_free = _archive_write_disk_free;
  466. av.archive_write_header = _archive_write_disk_header;
  467. av.archive_write_finish_entry
  468. = _archive_write_disk_finish_entry;
  469. av.archive_write_data = _archive_write_disk_data;
  470. av.archive_write_data_block = _archive_write_disk_data_block;
  471. inited = 1;
  472. }
  473. return (&av);
  474. }
  475. static int64_t
  476. _archive_write_disk_filter_bytes(struct archive *_a, int n)
  477. {
  478. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  479. (void)n; /* UNUSED */
  480. if (n == -1 || n == 0)
  481. return (a->total_bytes_written);
  482. return (-1);
  483. }
  484. int
  485. archive_write_disk_set_options(struct archive *_a, int flags)
  486. {
  487. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  488. a->flags = flags;
  489. return (ARCHIVE_OK);
  490. }
  491. /*
  492. * Extract this entry to disk.
  493. *
  494. * TODO: Validate hardlinks. According to the standards, we're
  495. * supposed to check each extracted hardlink and squawk if it refers
  496. * to a file that we didn't restore. I'm not entirely convinced this
  497. * is a good idea, but more importantly: Is there any way to validate
  498. * hardlinks without keeping a complete list of filenames from the
  499. * entire archive?? Ugh.
  500. *
  501. */
  502. static int
  503. _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
  504. {
  505. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  506. struct fixup_entry *fe;
  507. int ret, r;
  508. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  509. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  510. "archive_write_disk_header");
  511. archive_clear_error(&a->archive);
  512. if (a->archive.state & ARCHIVE_STATE_DATA) {
  513. r = _archive_write_disk_finish_entry(&a->archive);
  514. if (r == ARCHIVE_FATAL)
  515. return (r);
  516. }
  517. /* Set up for this particular entry. */
  518. a->pst = NULL;
  519. a->current_fixup = NULL;
  520. a->deferred = 0;
  521. if (a->entry) {
  522. archive_entry_free(a->entry);
  523. a->entry = NULL;
  524. }
  525. a->entry = archive_entry_clone(entry);
  526. a->fd = -1;
  527. a->fd_offset = 0;
  528. a->offset = 0;
  529. a->restore_pwd = -1;
  530. a->uid = a->user_uid;
  531. a->mode = archive_entry_mode(a->entry);
  532. if (archive_entry_size_is_set(a->entry))
  533. a->filesize = archive_entry_size(a->entry);
  534. else
  535. a->filesize = -1;
  536. archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
  537. a->name = a->_name_data.s;
  538. archive_clear_error(&a->archive);
  539. /*
  540. * Clean up the requested path. This is necessary for correct
  541. * dir restores; the dir restore logic otherwise gets messed
  542. * up by nonsense like "dir/.".
  543. */
  544. ret = cleanup_pathname(a);
  545. if (ret != ARCHIVE_OK)
  546. return (ret);
  547. /*
  548. * Query the umask so we get predictable mode settings.
  549. * This gets done on every call to _write_header in case the
  550. * user edits their umask during the extraction for some
  551. * reason.
  552. */
  553. umask(a->user_umask = umask(0));
  554. /* Figure out what we need to do for this entry. */
  555. a->todo = TODO_MODE_BASE;
  556. if (a->flags & ARCHIVE_EXTRACT_PERM) {
  557. a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
  558. /*
  559. * SGID requires an extra "check" step because we
  560. * cannot easily predict the GID that the system will
  561. * assign. (Different systems assign GIDs to files
  562. * based on a variety of criteria, including process
  563. * credentials and the gid of the enclosing
  564. * directory.) We can only restore the SGID bit if
  565. * the file has the right GID, and we only know the
  566. * GID if we either set it (see set_ownership) or if
  567. * we've actually called stat() on the file after it
  568. * was restored. Since there are several places at
  569. * which we might verify the GID, we need a TODO bit
  570. * to keep track.
  571. */
  572. if (a->mode & S_ISGID)
  573. a->todo |= TODO_SGID | TODO_SGID_CHECK;
  574. /*
  575. * Verifying the SUID is simpler, but can still be
  576. * done in multiple ways, hence the separate "check" bit.
  577. */
  578. if (a->mode & S_ISUID)
  579. a->todo |= TODO_SUID | TODO_SUID_CHECK;
  580. } else {
  581. /*
  582. * User didn't request full permissions, so don't
  583. * restore SUID, SGID bits and obey umask.
  584. */
  585. a->mode &= ~S_ISUID;
  586. a->mode &= ~S_ISGID;
  587. a->mode &= ~S_ISVTX;
  588. a->mode &= ~a->user_umask;
  589. }
  590. if (a->flags & ARCHIVE_EXTRACT_OWNER)
  591. a->todo |= TODO_OWNER;
  592. if (a->flags & ARCHIVE_EXTRACT_TIME)
  593. a->todo |= TODO_TIMES;
  594. if (a->flags & ARCHIVE_EXTRACT_ACL) {
  595. #if ARCHIVE_ACL_DARWIN
  596. /*
  597. * On MacOS, platform ACLs get stored in mac_metadata, too.
  598. * If we intend to extract mac_metadata and it is present
  599. * we skip extracting libarchive NFSv4 ACLs.
  600. */
  601. size_t metadata_size;
  602. if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
  603. archive_entry_mac_metadata(a->entry,
  604. &metadata_size) == NULL || metadata_size == 0)
  605. #endif
  606. #if ARCHIVE_ACL_LIBRICHACL
  607. /*
  608. * RichACLs are stored in an extended attribute.
  609. * If we intend to extract extended attributes and have this
  610. * attribute we skip extracting libarchive NFSv4 ACLs.
  611. */
  612. short extract_acls = 1;
  613. if (a->flags & ARCHIVE_EXTRACT_XATTR && (
  614. archive_entry_acl_types(a->entry) &
  615. ARCHIVE_ENTRY_ACL_TYPE_NFS4)) {
  616. const char *attr_name;
  617. const void *attr_value;
  618. size_t attr_size;
  619. int i = archive_entry_xattr_reset(a->entry);
  620. while (i--) {
  621. archive_entry_xattr_next(a->entry, &attr_name,
  622. &attr_value, &attr_size);
  623. if (attr_name != NULL && attr_value != NULL &&
  624. attr_size > 0 && strcmp(attr_name,
  625. "trusted.richacl") == 0) {
  626. extract_acls = 0;
  627. break;
  628. }
  629. }
  630. }
  631. if (extract_acls)
  632. #endif
  633. #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
  634. {
  635. #endif
  636. if (archive_entry_filetype(a->entry) == AE_IFDIR)
  637. a->deferred |= TODO_ACLS;
  638. else
  639. a->todo |= TODO_ACLS;
  640. #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
  641. }
  642. #endif
  643. }
  644. if (a->flags & ARCHIVE_EXTRACT_MAC_METADATA) {
  645. if (archive_entry_filetype(a->entry) == AE_IFDIR)
  646. a->deferred |= TODO_MAC_METADATA;
  647. else
  648. a->todo |= TODO_MAC_METADATA;
  649. }
  650. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
  651. if ((a->flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) == 0) {
  652. unsigned long set, clear;
  653. archive_entry_fflags(a->entry, &set, &clear);
  654. if ((set & ~clear) & UF_COMPRESSED) {
  655. a->todo |= TODO_HFS_COMPRESSION;
  656. a->decmpfs_block_count = (unsigned)-1;
  657. }
  658. }
  659. if ((a->flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) != 0 &&
  660. (a->mode & AE_IFMT) == AE_IFREG && a->filesize > 0) {
  661. a->todo |= TODO_HFS_COMPRESSION;
  662. a->decmpfs_block_count = (unsigned)-1;
  663. }
  664. {
  665. const char *p;
  666. /* Check if the current file name is a type of the
  667. * resource fork file. */
  668. p = strrchr(a->name, '/');
  669. if (p == NULL)
  670. p = a->name;
  671. else
  672. p++;
  673. if (p[0] == '.' && p[1] == '_') {
  674. /* Do not compress "._XXX" files. */
  675. a->todo &= ~TODO_HFS_COMPRESSION;
  676. if (a->filesize > 0)
  677. a->todo |= TODO_APPLEDOUBLE;
  678. }
  679. }
  680. #endif
  681. if (a->flags & ARCHIVE_EXTRACT_XATTR) {
  682. #if ARCHIVE_XATTR_DARWIN
  683. /*
  684. * On MacOS, extended attributes get stored in mac_metadata,
  685. * too. If we intend to extract mac_metadata and it is present
  686. * we skip extracting extended attributes.
  687. */
  688. size_t metadata_size;
  689. if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
  690. archive_entry_mac_metadata(a->entry,
  691. &metadata_size) == NULL || metadata_size == 0)
  692. #endif
  693. a->todo |= TODO_XATTR;
  694. }
  695. if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
  696. a->todo |= TODO_FFLAGS;
  697. if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
  698. ret = check_symlinks(a);
  699. if (ret != ARCHIVE_OK)
  700. return (ret);
  701. }
  702. #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
  703. /* If path exceeds PATH_MAX, shorten the path. */
  704. edit_deep_directories(a);
  705. #endif
  706. ret = restore_entry(a);
  707. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
  708. /*
  709. * Check if the filesystem the file is restoring on supports
  710. * HFS+ Compression. If not, cancel HFS+ Compression.
  711. */
  712. if (a->todo | TODO_HFS_COMPRESSION) {
  713. /*
  714. * NOTE: UF_COMPRESSED is ignored even if the filesystem
  715. * supports HFS+ Compression because the file should
  716. * have at least an extended attribute "com.apple.decmpfs"
  717. * before the flag is set to indicate that the file have
  718. * been compressed. If the filesystem does not support
  719. * HFS+ Compression the system call will fail.
  720. */
  721. if (a->fd < 0 || fchflags(a->fd, UF_COMPRESSED) != 0)
  722. a->todo &= ~TODO_HFS_COMPRESSION;
  723. }
  724. #endif
  725. /*
  726. * TODO: There are rumours that some extended attributes must
  727. * be restored before file data is written. If this is true,
  728. * then we either need to write all extended attributes both
  729. * before and after restoring the data, or find some rule for
  730. * determining which must go first and which last. Due to the
  731. * many ways people are using xattrs, this may prove to be an
  732. * intractable problem.
  733. */
  734. #ifdef HAVE_FCHDIR
  735. /* If we changed directory above, restore it here. */
  736. if (a->restore_pwd >= 0) {
  737. r = fchdir(a->restore_pwd);
  738. if (r != 0) {
  739. archive_set_error(&a->archive, errno,
  740. "chdir() failure");
  741. ret = ARCHIVE_FATAL;
  742. }
  743. close(a->restore_pwd);
  744. a->restore_pwd = -1;
  745. }
  746. #endif
  747. /*
  748. * Fixup uses the unedited pathname from archive_entry_pathname(),
  749. * because it is relative to the base dir and the edited path
  750. * might be relative to some intermediate dir as a result of the
  751. * deep restore logic.
  752. */
  753. if (a->deferred & TODO_MODE) {
  754. fe = current_fixup(a, archive_entry_pathname(entry));
  755. if (fe == NULL)
  756. return (ARCHIVE_FATAL);
  757. fe->fixup |= TODO_MODE_BASE;
  758. fe->mode = a->mode;
  759. }
  760. if ((a->deferred & TODO_TIMES)
  761. && (archive_entry_mtime_is_set(entry)
  762. || archive_entry_atime_is_set(entry))) {
  763. fe = current_fixup(a, archive_entry_pathname(entry));
  764. if (fe == NULL)
  765. return (ARCHIVE_FATAL);
  766. fe->mode = a->mode;
  767. fe->fixup |= TODO_TIMES;
  768. if (archive_entry_atime_is_set(entry)) {
  769. fe->atime = archive_entry_atime(entry);
  770. fe->atime_nanos = archive_entry_atime_nsec(entry);
  771. } else {
  772. /* If atime is unset, use start time. */
  773. fe->atime = a->start_time;
  774. fe->atime_nanos = 0;
  775. }
  776. if (archive_entry_mtime_is_set(entry)) {
  777. fe->mtime = archive_entry_mtime(entry);
  778. fe->mtime_nanos = archive_entry_mtime_nsec(entry);
  779. } else {
  780. /* If mtime is unset, use start time. */
  781. fe->mtime = a->start_time;
  782. fe->mtime_nanos = 0;
  783. }
  784. if (archive_entry_birthtime_is_set(entry)) {
  785. fe->birthtime = archive_entry_birthtime(entry);
  786. fe->birthtime_nanos = archive_entry_birthtime_nsec(
  787. entry);
  788. } else {
  789. /* If birthtime is unset, use mtime. */
  790. fe->birthtime = fe->mtime;
  791. fe->birthtime_nanos = fe->mtime_nanos;
  792. }
  793. }
  794. if (a->deferred & TODO_ACLS) {
  795. fe = current_fixup(a, archive_entry_pathname(entry));
  796. if (fe == NULL)
  797. return (ARCHIVE_FATAL);
  798. fe->fixup |= TODO_ACLS;
  799. archive_acl_copy(&fe->acl, archive_entry_acl(entry));
  800. }
  801. if (a->deferred & TODO_MAC_METADATA) {
  802. const void *metadata;
  803. size_t metadata_size;
  804. metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
  805. if (metadata != NULL && metadata_size > 0) {
  806. fe = current_fixup(a, archive_entry_pathname(entry));
  807. if (fe == NULL)
  808. return (ARCHIVE_FATAL);
  809. fe->mac_metadata = malloc(metadata_size);
  810. if (fe->mac_metadata != NULL) {
  811. memcpy(fe->mac_metadata, metadata,
  812. metadata_size);
  813. fe->mac_metadata_size = metadata_size;
  814. fe->fixup |= TODO_MAC_METADATA;
  815. }
  816. }
  817. }
  818. if (a->deferred & TODO_FFLAGS) {
  819. fe = current_fixup(a, archive_entry_pathname(entry));
  820. if (fe == NULL)
  821. return (ARCHIVE_FATAL);
  822. fe->fixup |= TODO_FFLAGS;
  823. /* TODO: Complete this.. defer fflags from below. */
  824. }
  825. /* We've created the object and are ready to pour data into it. */
  826. if (ret >= ARCHIVE_WARN)
  827. a->archive.state = ARCHIVE_STATE_DATA;
  828. /*
  829. * If it's not open, tell our client not to try writing.
  830. * In particular, dirs, links, etc, don't get written to.
  831. */
  832. if (a->fd < 0) {
  833. archive_entry_set_size(entry, 0);
  834. a->filesize = 0;
  835. }
  836. return (ret);
  837. }
  838. int
  839. archive_write_disk_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
  840. {
  841. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  842. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  843. ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
  844. a->skip_file_set = 1;
  845. a->skip_file_dev = d;
  846. a->skip_file_ino = i;
  847. return (ARCHIVE_OK);
  848. }
  849. static ssize_t
  850. write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
  851. {
  852. uint64_t start_size = size;
  853. ssize_t bytes_written = 0;
  854. ssize_t block_size = 0, bytes_to_write;
  855. if (size == 0)
  856. return (ARCHIVE_OK);
  857. if (a->filesize == 0 || a->fd < 0) {
  858. archive_set_error(&a->archive, 0,
  859. "Attempt to write to an empty file");
  860. return (ARCHIVE_WARN);
  861. }
  862. if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
  863. #if HAVE_STRUCT_STAT_ST_BLKSIZE
  864. int r;
  865. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  866. return (r);
  867. block_size = a->pst->st_blksize;
  868. #else
  869. /* XXX TODO XXX Is there a more appropriate choice here ? */
  870. /* This needn't match the filesystem allocation size. */
  871. block_size = 16*1024;
  872. #endif
  873. }
  874. /* If this write would run beyond the file size, truncate it. */
  875. if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
  876. start_size = size = (size_t)(a->filesize - a->offset);
  877. /* Write the data. */
  878. while (size > 0) {
  879. if (block_size == 0) {
  880. bytes_to_write = size;
  881. } else {
  882. /* We're sparsifying the file. */
  883. const char *p, *end;
  884. int64_t block_end;
  885. /* Skip leading zero bytes. */
  886. for (p = buff, end = buff + size; p < end; ++p) {
  887. if (*p != '\0')
  888. break;
  889. }
  890. a->offset += p - buff;
  891. size -= p - buff;
  892. buff = p;
  893. if (size == 0)
  894. break;
  895. /* Calculate next block boundary after offset. */
  896. block_end
  897. = (a->offset / block_size + 1) * block_size;
  898. /* If the adjusted write would cross block boundary,
  899. * truncate it to the block boundary. */
  900. bytes_to_write = size;
  901. if (a->offset + bytes_to_write > block_end)
  902. bytes_to_write = block_end - a->offset;
  903. }
  904. /* Seek if necessary to the specified offset. */
  905. if (a->offset != a->fd_offset) {
  906. if (lseek(a->fd, a->offset, SEEK_SET) < 0) {
  907. archive_set_error(&a->archive, errno,
  908. "Seek failed");
  909. return (ARCHIVE_FATAL);
  910. }
  911. a->fd_offset = a->offset;
  912. }
  913. bytes_written = write(a->fd, buff, bytes_to_write);
  914. if (bytes_written < 0) {
  915. archive_set_error(&a->archive, errno, "Write failed");
  916. return (ARCHIVE_WARN);
  917. }
  918. buff += bytes_written;
  919. size -= bytes_written;
  920. a->total_bytes_written += bytes_written;
  921. a->offset += bytes_written;
  922. a->fd_offset = a->offset;
  923. }
  924. return (start_size - size);
  925. }
  926. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
  927. && defined(HAVE_ZLIB_H)
  928. /*
  929. * Set UF_COMPRESSED file flag.
  930. * This have to be called after hfs_write_decmpfs() because if the
  931. * file does not have "com.apple.decmpfs" xattr the flag is ignored.
  932. */
  933. static int
  934. hfs_set_compressed_fflag(struct archive_write_disk *a)
  935. {
  936. int r;
  937. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  938. return (r);
  939. a->st.st_flags |= UF_COMPRESSED;
  940. if (fchflags(a->fd, a->st.st_flags) != 0) {
  941. archive_set_error(&a->archive, errno,
  942. "Failed to set UF_COMPRESSED file flag");
  943. return (ARCHIVE_WARN);
  944. }
  945. return (ARCHIVE_OK);
  946. }
  947. /*
  948. * HFS+ Compression decmpfs
  949. *
  950. * +------------------------------+ +0
  951. * | Magic(LE 4 bytes) |
  952. * +------------------------------+
  953. * | Type(LE 4 bytes) |
  954. * +------------------------------+
  955. * | Uncompressed size(LE 8 bytes)|
  956. * +------------------------------+ +16
  957. * | |
  958. * | Compressed data |
  959. * | (Placed only if Type == 3) |
  960. * | |
  961. * +------------------------------+ +3802 = MAX_DECMPFS_XATTR_SIZE
  962. *
  963. * Type is 3: decmpfs has compressed data.
  964. * Type is 4: Resource Fork has compressed data.
  965. */
  966. /*
  967. * Write "com.apple.decmpfs"
  968. */
  969. static int
  970. hfs_write_decmpfs(struct archive_write_disk *a)
  971. {
  972. int r;
  973. uint32_t compression_type;
  974. r = fsetxattr(a->fd, DECMPFS_XATTR_NAME, a->decmpfs_header_p,
  975. a->decmpfs_attr_size, 0, 0);
  976. if (r < 0) {
  977. archive_set_error(&a->archive, errno,
  978. "Cannot restore xattr:%s", DECMPFS_XATTR_NAME);
  979. compression_type = archive_le32dec(
  980. &a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE]);
  981. if (compression_type == CMP_RESOURCE_FORK)
  982. fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME,
  983. XATTR_SHOWCOMPRESSION);
  984. return (ARCHIVE_WARN);
  985. }
  986. return (ARCHIVE_OK);
  987. }
  988. /*
  989. * HFS+ Compression Resource Fork
  990. *
  991. * +-----------------------------+
  992. * | Header(260 bytes) |
  993. * +-----------------------------+
  994. * | Block count(LE 4 bytes) |
  995. * +-----------------------------+ --+
  996. * +-- | Offset (LE 4 bytes) | |
  997. * | | [distance from Block count] | | Block 0
  998. * | +-----------------------------+ |
  999. * | | Compressed size(LE 4 bytes) | |
  1000. * | +-----------------------------+ --+
  1001. * | | |
  1002. * | | .................. |
  1003. * | | |
  1004. * | +-----------------------------+ --+
  1005. * | | Offset (LE 4 bytes) | |
  1006. * | +-----------------------------+ | Block (Block count -1)
  1007. * | | Compressed size(LE 4 bytes) | |
  1008. * +-> +-----------------------------+ --+
  1009. * | Compressed data(n bytes) | Block 0
  1010. * +-----------------------------+
  1011. * | |
  1012. * | .................. |
  1013. * | |
  1014. * +-----------------------------+
  1015. * | Compressed data(n bytes) | Block (Block count -1)
  1016. * +-----------------------------+
  1017. * | Footer(50 bytes) |
  1018. * +-----------------------------+
  1019. *
  1020. */
  1021. /*
  1022. * Write the header of "com.apple.ResourceFork"
  1023. */
  1024. static int
  1025. hfs_write_resource_fork(struct archive_write_disk *a, unsigned char *buff,
  1026. size_t bytes, uint32_t position)
  1027. {
  1028. int ret;
  1029. ret = fsetxattr(a->fd, XATTR_RESOURCEFORK_NAME, buff, bytes,
  1030. position, a->rsrc_xattr_options);
  1031. if (ret < 0) {
  1032. archive_set_error(&a->archive, errno,
  1033. "Cannot restore xattr: %s at %u pos %u bytes",
  1034. XATTR_RESOURCEFORK_NAME,
  1035. (unsigned)position,
  1036. (unsigned)bytes);
  1037. return (ARCHIVE_WARN);
  1038. }
  1039. a->rsrc_xattr_options &= ~XATTR_CREATE;
  1040. return (ARCHIVE_OK);
  1041. }
  1042. static int
  1043. hfs_write_compressed_data(struct archive_write_disk *a, size_t bytes_compressed)
  1044. {
  1045. int ret;
  1046. ret = hfs_write_resource_fork(a, a->compressed_buffer,
  1047. bytes_compressed, a->compressed_rsrc_position);
  1048. if (ret == ARCHIVE_OK)
  1049. a->compressed_rsrc_position += bytes_compressed;
  1050. return (ret);
  1051. }
  1052. static int
  1053. hfs_write_resource_fork_header(struct archive_write_disk *a)
  1054. {
  1055. unsigned char *buff;
  1056. uint32_t rsrc_bytes;
  1057. uint32_t rsrc_header_bytes;
  1058. /*
  1059. * Write resource fork header + block info.
  1060. */
  1061. buff = a->resource_fork;
  1062. rsrc_bytes = a->compressed_rsrc_position - RSRC_F_SIZE;
  1063. rsrc_header_bytes =
  1064. RSRC_H_SIZE + /* Header base size. */
  1065. 4 + /* Block count. */
  1066. (a->decmpfs_block_count * 8);/* Block info */
  1067. archive_be32enc(buff, 0x100);
  1068. archive_be32enc(buff + 4, rsrc_bytes);
  1069. archive_be32enc(buff + 8, rsrc_bytes - 256);
  1070. archive_be32enc(buff + 12, 0x32);
  1071. memset(buff + 16, 0, 240);
  1072. archive_be32enc(buff + 256, rsrc_bytes - 260);
  1073. return hfs_write_resource_fork(a, buff, rsrc_header_bytes, 0);
  1074. }
  1075. static size_t
  1076. hfs_set_resource_fork_footer(unsigned char *buff, size_t buff_size)
  1077. {
  1078. static const char rsrc_footer[RSRC_F_SIZE] = {
  1079. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1080. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1081. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1082. 0x00, 0x1c, 0x00, 0x32, 0x00, 0x00, 'c', 'm',
  1083. 'p', 'f', 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01,
  1084. 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1085. 0x00, 0x00
  1086. };
  1087. if (buff_size < sizeof(rsrc_footer))
  1088. return (0);
  1089. memcpy(buff, rsrc_footer, sizeof(rsrc_footer));
  1090. return (sizeof(rsrc_footer));
  1091. }
  1092. static int
  1093. hfs_reset_compressor(struct archive_write_disk *a)
  1094. {
  1095. int ret;
  1096. if (a->stream_valid)
  1097. ret = deflateReset(&a->stream);
  1098. else
  1099. ret = deflateInit(&a->stream, a->decmpfs_compression_level);
  1100. if (ret != Z_OK) {
  1101. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1102. "Failed to initialize compressor");
  1103. return (ARCHIVE_FATAL);
  1104. } else
  1105. a->stream_valid = 1;
  1106. return (ARCHIVE_OK);
  1107. }
  1108. static int
  1109. hfs_decompress(struct archive_write_disk *a)
  1110. {
  1111. uint32_t *block_info;
  1112. unsigned int block_count;
  1113. uint32_t data_pos, data_size;
  1114. ssize_t r;
  1115. ssize_t bytes_written, bytes_to_write;
  1116. unsigned char *b;
  1117. block_info = (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
  1118. block_count = archive_le32dec(block_info++);
  1119. while (block_count--) {
  1120. data_pos = RSRC_H_SIZE + archive_le32dec(block_info++);
  1121. data_size = archive_le32dec(block_info++);
  1122. r = fgetxattr(a->fd, XATTR_RESOURCEFORK_NAME,
  1123. a->compressed_buffer, data_size, data_pos, 0);
  1124. if (r != data_size) {
  1125. archive_set_error(&a->archive,
  1126. (r < 0)?errno:ARCHIVE_ERRNO_MISC,
  1127. "Failed to read resource fork");
  1128. return (ARCHIVE_WARN);
  1129. }
  1130. if (a->compressed_buffer[0] == 0xff) {
  1131. bytes_to_write = data_size -1;
  1132. b = a->compressed_buffer + 1;
  1133. } else {
  1134. uLong dest_len = MAX_DECMPFS_BLOCK_SIZE;
  1135. int zr;
  1136. zr = uncompress((Bytef *)a->uncompressed_buffer,
  1137. &dest_len, a->compressed_buffer, data_size);
  1138. if (zr != Z_OK) {
  1139. archive_set_error(&a->archive,
  1140. ARCHIVE_ERRNO_MISC,
  1141. "Failed to decompress resource fork");
  1142. return (ARCHIVE_WARN);
  1143. }
  1144. bytes_to_write = dest_len;
  1145. b = (unsigned char *)a->uncompressed_buffer;
  1146. }
  1147. do {
  1148. bytes_written = write(a->fd, b, bytes_to_write);
  1149. if (bytes_written < 0) {
  1150. archive_set_error(&a->archive, errno,
  1151. "Write failed");
  1152. return (ARCHIVE_WARN);
  1153. }
  1154. bytes_to_write -= bytes_written;
  1155. b += bytes_written;
  1156. } while (bytes_to_write > 0);
  1157. }
  1158. r = fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME, 0);
  1159. if (r == -1) {
  1160. archive_set_error(&a->archive, errno,
  1161. "Failed to remove resource fork");
  1162. return (ARCHIVE_WARN);
  1163. }
  1164. return (ARCHIVE_OK);
  1165. }
  1166. static int
  1167. hfs_drive_compressor(struct archive_write_disk *a, const char *buff,
  1168. size_t size)
  1169. {
  1170. unsigned char *buffer_compressed;
  1171. size_t bytes_compressed;
  1172. size_t bytes_used;
  1173. int ret;
  1174. ret = hfs_reset_compressor(a);
  1175. if (ret != ARCHIVE_OK)
  1176. return (ret);
  1177. if (a->compressed_buffer == NULL) {
  1178. size_t block_size;
  1179. block_size = COMPRESSED_W_SIZE + RSRC_F_SIZE +
  1180. + compressBound(MAX_DECMPFS_BLOCK_SIZE);
  1181. a->compressed_buffer = malloc(block_size);
  1182. if (a->compressed_buffer == NULL) {
  1183. archive_set_error(&a->archive, ENOMEM,
  1184. "Can't allocate memory for Resource Fork");
  1185. return (ARCHIVE_FATAL);
  1186. }
  1187. a->compressed_buffer_size = block_size;
  1188. a->compressed_buffer_remaining = block_size;
  1189. }
  1190. buffer_compressed = a->compressed_buffer +
  1191. a->compressed_buffer_size - a->compressed_buffer_remaining;
  1192. a->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff;
  1193. a->stream.avail_in = size;
  1194. a->stream.next_out = buffer_compressed;
  1195. a->stream.avail_out = a->compressed_buffer_remaining;
  1196. do {
  1197. ret = deflate(&a->stream, Z_FINISH);
  1198. switch (ret) {
  1199. case Z_OK:
  1200. case Z_STREAM_END:
  1201. break;
  1202. default:
  1203. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1204. "Failed to compress data");
  1205. return (ARCHIVE_FAILED);
  1206. }
  1207. } while (ret == Z_OK);
  1208. bytes_compressed = a->compressed_buffer_remaining - a->stream.avail_out;
  1209. /*
  1210. * If the compressed size is larger than the original size,
  1211. * throw away compressed data, use uncompressed data instead.
  1212. */
  1213. if (bytes_compressed > size) {
  1214. buffer_compressed[0] = 0xFF;/* uncompressed marker. */
  1215. memcpy(buffer_compressed + 1, buff, size);
  1216. bytes_compressed = size + 1;
  1217. }
  1218. a->compressed_buffer_remaining -= bytes_compressed;
  1219. /*
  1220. * If the compressed size is smaller than MAX_DECMPFS_XATTR_SIZE
  1221. * and the block count in the file is only one, store compressed
  1222. * data to decmpfs xattr instead of the resource fork.
  1223. */
  1224. if (a->decmpfs_block_count == 1 &&
  1225. (a->decmpfs_attr_size + bytes_compressed)
  1226. <= MAX_DECMPFS_XATTR_SIZE) {
  1227. archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
  1228. CMP_XATTR);
  1229. memcpy(a->decmpfs_header_p + DECMPFS_HEADER_SIZE,
  1230. buffer_compressed, bytes_compressed);
  1231. a->decmpfs_attr_size += bytes_compressed;
  1232. a->compressed_buffer_remaining = a->compressed_buffer_size;
  1233. /*
  1234. * Finish HFS+ Compression.
  1235. * - Write the decmpfs xattr.
  1236. * - Set the UF_COMPRESSED file flag.
  1237. */
  1238. ret = hfs_write_decmpfs(a);
  1239. if (ret == ARCHIVE_OK)
  1240. ret = hfs_set_compressed_fflag(a);
  1241. return (ret);
  1242. }
  1243. /* Update block info. */
  1244. archive_le32enc(a->decmpfs_block_info++,
  1245. a->compressed_rsrc_position_v - RSRC_H_SIZE);
  1246. archive_le32enc(a->decmpfs_block_info++, bytes_compressed);
  1247. a->compressed_rsrc_position_v += bytes_compressed;
  1248. /*
  1249. * Write the compressed data to the resource fork.
  1250. */
  1251. bytes_used = a->compressed_buffer_size - a->compressed_buffer_remaining;
  1252. while (bytes_used >= COMPRESSED_W_SIZE) {
  1253. ret = hfs_write_compressed_data(a, COMPRESSED_W_SIZE);
  1254. if (ret != ARCHIVE_OK)
  1255. return (ret);
  1256. bytes_used -= COMPRESSED_W_SIZE;
  1257. if (bytes_used > COMPRESSED_W_SIZE)
  1258. memmove(a->compressed_buffer,
  1259. a->compressed_buffer + COMPRESSED_W_SIZE,
  1260. bytes_used);
  1261. else
  1262. memcpy(a->compressed_buffer,
  1263. a->compressed_buffer + COMPRESSED_W_SIZE,
  1264. bytes_used);
  1265. }
  1266. a->compressed_buffer_remaining = a->compressed_buffer_size - bytes_used;
  1267. /*
  1268. * If the current block is the last block, write the remaining
  1269. * compressed data and the resource fork footer.
  1270. */
  1271. if (a->file_remaining_bytes == 0) {
  1272. size_t rsrc_size;
  1273. int64_t bk;
  1274. /* Append the resource footer. */
  1275. rsrc_size = hfs_set_resource_fork_footer(
  1276. a->compressed_buffer + bytes_used,
  1277. a->compressed_buffer_remaining);
  1278. ret = hfs_write_compressed_data(a, bytes_used + rsrc_size);
  1279. a->compressed_buffer_remaining = a->compressed_buffer_size;
  1280. /* If the compressed size is not enough smaller than
  1281. * the uncompressed size. cancel HFS+ compression.
  1282. * TODO: study a behavior of ditto utility and improve
  1283. * the condition to fall back into no HFS+ compression. */
  1284. bk = HFS_BLOCKS(a->compressed_rsrc_position);
  1285. bk += bk >> 7;
  1286. if (bk > HFS_BLOCKS(a->filesize))
  1287. return hfs_decompress(a);
  1288. /*
  1289. * Write the resourcefork header.
  1290. */
  1291. if (ret == ARCHIVE_OK)
  1292. ret = hfs_write_resource_fork_header(a);
  1293. /*
  1294. * Finish HFS+ Compression.
  1295. * - Write the decmpfs xattr.
  1296. * - Set the UF_COMPRESSED file flag.
  1297. */
  1298. if (ret == ARCHIVE_OK)
  1299. ret = hfs_write_decmpfs(a);
  1300. if (ret == ARCHIVE_OK)
  1301. ret = hfs_set_compressed_fflag(a);
  1302. }
  1303. return (ret);
  1304. }
  1305. static ssize_t
  1306. hfs_write_decmpfs_block(struct archive_write_disk *a, const char *buff,
  1307. size_t size)
  1308. {
  1309. const char *buffer_to_write;
  1310. size_t bytes_to_write;
  1311. int ret;
  1312. if (a->decmpfs_block_count == (unsigned)-1) {
  1313. void *new_block;
  1314. size_t new_size;
  1315. unsigned int block_count;
  1316. if (a->decmpfs_header_p == NULL) {
  1317. new_block = malloc(MAX_DECMPFS_XATTR_SIZE
  1318. + sizeof(uint32_t));
  1319. if (new_block == NULL) {
  1320. archive_set_error(&a->archive, ENOMEM,
  1321. "Can't allocate memory for decmpfs");
  1322. return (ARCHIVE_FATAL);
  1323. }
  1324. a->decmpfs_header_p = new_block;
  1325. }
  1326. a->decmpfs_attr_size = DECMPFS_HEADER_SIZE;
  1327. archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_MAGIC],
  1328. DECMPFS_MAGIC);
  1329. archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
  1330. CMP_RESOURCE_FORK);
  1331. archive_le64enc(&a->decmpfs_header_p[DECMPFS_UNCOMPRESSED_SIZE],
  1332. a->filesize);
  1333. /* Calculate a block count of the file. */
  1334. block_count =
  1335. (a->filesize + MAX_DECMPFS_BLOCK_SIZE -1) /
  1336. MAX_DECMPFS_BLOCK_SIZE;
  1337. /*
  1338. * Allocate buffer for resource fork.
  1339. * Set up related pointers;
  1340. */
  1341. new_size =
  1342. RSRC_H_SIZE + /* header */
  1343. 4 + /* Block count */
  1344. (block_count * sizeof(uint32_t) * 2) +
  1345. RSRC_F_SIZE; /* footer */
  1346. if (new_size > a->resource_fork_allocated_size) {
  1347. new_block = realloc(a->resource_fork, new_size);
  1348. if (new_block == NULL) {
  1349. archive_set_error(&a->archive, ENOMEM,
  1350. "Can't allocate memory for ResourceFork");
  1351. return (ARCHIVE_FATAL);
  1352. }
  1353. a->resource_fork_allocated_size = new_size;
  1354. a->resource_fork = new_block;
  1355. }
  1356. /* Allocate uncompressed buffer */
  1357. if (a->uncompressed_buffer == NULL) {
  1358. new_block = malloc(MAX_DECMPFS_BLOCK_SIZE);
  1359. if (new_block == NULL) {
  1360. archive_set_error(&a->archive, ENOMEM,
  1361. "Can't allocate memory for decmpfs");
  1362. return (ARCHIVE_FATAL);
  1363. }
  1364. a->uncompressed_buffer = new_block;
  1365. }
  1366. a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
  1367. a->file_remaining_bytes = a->filesize;
  1368. a->compressed_buffer_remaining = a->compressed_buffer_size;
  1369. /*
  1370. * Set up a resource fork.
  1371. */
  1372. a->rsrc_xattr_options = XATTR_CREATE;
  1373. /* Get the position where we are going to set a bunch
  1374. * of block info. */
  1375. a->decmpfs_block_info =
  1376. (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
  1377. /* Set the block count to the resource fork. */
  1378. archive_le32enc(a->decmpfs_block_info++, block_count);
  1379. /* Get the position where we are going to set compressed
  1380. * data. */
  1381. a->compressed_rsrc_position =
  1382. RSRC_H_SIZE + 4 + (block_count * 8);
  1383. a->compressed_rsrc_position_v = a->compressed_rsrc_position;
  1384. a->decmpfs_block_count = block_count;
  1385. }
  1386. /* Ignore redundant bytes. */
  1387. if (a->file_remaining_bytes == 0)
  1388. return ((ssize_t)size);
  1389. /* Do not overrun a block size. */
  1390. if (size > a->block_remaining_bytes)
  1391. bytes_to_write = a->block_remaining_bytes;
  1392. else
  1393. bytes_to_write = size;
  1394. /* Do not overrun the file size. */
  1395. if (bytes_to_write > a->file_remaining_bytes)
  1396. bytes_to_write = a->file_remaining_bytes;
  1397. /* For efficiency, if a copy length is full of the uncompressed
  1398. * buffer size, do not copy writing data to it. */
  1399. if (bytes_to_write == MAX_DECMPFS_BLOCK_SIZE)
  1400. buffer_to_write = buff;
  1401. else {
  1402. memcpy(a->uncompressed_buffer +
  1403. MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes,
  1404. buff, bytes_to_write);
  1405. buffer_to_write = a->uncompressed_buffer;
  1406. }
  1407. a->block_remaining_bytes -= bytes_to_write;
  1408. a->file_remaining_bytes -= bytes_to_write;
  1409. if (a->block_remaining_bytes == 0 || a->file_remaining_bytes == 0) {
  1410. ret = hfs_drive_compressor(a, buffer_to_write,
  1411. MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes);
  1412. if (ret < 0)
  1413. return (ret);
  1414. a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
  1415. }
  1416. /* Ignore redundant bytes. */
  1417. if (a->file_remaining_bytes == 0)
  1418. return ((ssize_t)size);
  1419. return (bytes_to_write);
  1420. }
  1421. static ssize_t
  1422. hfs_write_data_block(struct archive_write_disk *a, const char *buff,
  1423. size_t size)
  1424. {
  1425. uint64_t start_size = size;
  1426. ssize_t bytes_written = 0;
  1427. ssize_t bytes_to_write;
  1428. if (size == 0)
  1429. return (ARCHIVE_OK);
  1430. if (a->filesize == 0 || a->fd < 0) {
  1431. archive_set_error(&a->archive, 0,
  1432. "Attempt to write to an empty file");
  1433. return (ARCHIVE_WARN);
  1434. }
  1435. /* If this write would run beyond the file size, truncate it. */
  1436. if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
  1437. start_size = size = (size_t)(a->filesize - a->offset);
  1438. /* Write the data. */
  1439. while (size > 0) {
  1440. bytes_to_write = size;
  1441. /* Seek if necessary to the specified offset. */
  1442. if (a->offset < a->fd_offset) {
  1443. /* Can't support backward move. */
  1444. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1445. "Seek failed");
  1446. return (ARCHIVE_FATAL);
  1447. } else if (a->offset > a->fd_offset) {
  1448. int64_t skip = a->offset - a->fd_offset;
  1449. char nullblock[1024];
  1450. memset(nullblock, 0, sizeof(nullblock));
  1451. while (skip > 0) {
  1452. if (skip > (int64_t)sizeof(nullblock))
  1453. bytes_written = hfs_write_decmpfs_block(
  1454. a, nullblock, sizeof(nullblock));
  1455. else
  1456. bytes_written = hfs_write_decmpfs_block(
  1457. a, nullblock, skip);
  1458. if (bytes_written < 0) {
  1459. archive_set_error(&a->archive, errno,
  1460. "Write failed");
  1461. return (ARCHIVE_WARN);
  1462. }
  1463. skip -= bytes_written;
  1464. }
  1465. a->fd_offset = a->offset;
  1466. }
  1467. bytes_written =
  1468. hfs_write_decmpfs_block(a, buff, bytes_to_write);
  1469. if (bytes_written < 0)
  1470. return (bytes_written);
  1471. buff += bytes_written;
  1472. size -= bytes_written;
  1473. a->total_bytes_written += bytes_written;
  1474. a->offset += bytes_written;
  1475. a->fd_offset = a->offset;
  1476. }
  1477. return (start_size - size);
  1478. }
  1479. #else
  1480. static ssize_t
  1481. hfs_write_data_block(struct archive_write_disk *a, const char *buff,
  1482. size_t size)
  1483. {
  1484. return (write_data_block(a, buff, size));
  1485. }
  1486. #endif
  1487. static ssize_t
  1488. _archive_write_disk_data_block(struct archive *_a,
  1489. const void *buff, size_t size, int64_t offset)
  1490. {
  1491. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1492. ssize_t r;
  1493. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1494. ARCHIVE_STATE_DATA, "archive_write_data_block");
  1495. a->offset = offset;
  1496. if (a->todo & TODO_HFS_COMPRESSION)
  1497. r = hfs_write_data_block(a, buff, size);
  1498. else
  1499. r = write_data_block(a, buff, size);
  1500. if (r < ARCHIVE_OK)
  1501. return (r);
  1502. if ((size_t)r < size) {
  1503. archive_set_error(&a->archive, 0,
  1504. "Too much data: Truncating file at %ju bytes",
  1505. (uintmax_t)a->filesize);
  1506. return (ARCHIVE_WARN);
  1507. }
  1508. #if ARCHIVE_VERSION_NUMBER < 3999000
  1509. return (ARCHIVE_OK);
  1510. #else
  1511. return (size);
  1512. #endif
  1513. }
  1514. static ssize_t
  1515. _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
  1516. {
  1517. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1518. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1519. ARCHIVE_STATE_DATA, "archive_write_data");
  1520. if (a->todo & TODO_HFS_COMPRESSION)
  1521. return (hfs_write_data_block(a, buff, size));
  1522. return (write_data_block(a, buff, size));
  1523. }
  1524. static int
  1525. _archive_write_disk_finish_entry(struct archive *_a)
  1526. {
  1527. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1528. int ret = ARCHIVE_OK;
  1529. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1530. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1531. "archive_write_finish_entry");
  1532. if (a->archive.state & ARCHIVE_STATE_HEADER)
  1533. return (ARCHIVE_OK);
  1534. archive_clear_error(&a->archive);
  1535. /* Pad or truncate file to the right size. */
  1536. if (a->fd < 0) {
  1537. /* There's no file. */
  1538. } else if (a->filesize < 0) {
  1539. /* File size is unknown, so we can't set the size. */
  1540. } else if (a->fd_offset == a->filesize) {
  1541. /* Last write ended at exactly the filesize; we're done. */
  1542. /* Hopefully, this is the common case. */
  1543. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
  1544. } else if (a->todo & TODO_HFS_COMPRESSION) {
  1545. char null_d[1024];
  1546. ssize_t r;
  1547. if (a->file_remaining_bytes)
  1548. memset(null_d, 0, sizeof(null_d));
  1549. while (a->file_remaining_bytes) {
  1550. if (a->file_remaining_bytes > sizeof(null_d))
  1551. r = hfs_write_data_block(
  1552. a, null_d, sizeof(null_d));
  1553. else
  1554. r = hfs_write_data_block(
  1555. a, null_d, a->file_remaining_bytes);
  1556. if (r < 0)
  1557. return ((int)r);
  1558. }
  1559. #endif
  1560. } else {
  1561. #if HAVE_FTRUNCATE
  1562. if (ftruncate(a->fd, a->filesize) == -1 &&
  1563. a->filesize == 0) {
  1564. archive_set_error(&a->archive, errno,
  1565. "File size could not be restored");
  1566. return (ARCHIVE_FAILED);
  1567. }
  1568. #endif
  1569. /*
  1570. * Not all platforms implement the XSI option to
  1571. * extend files via ftruncate. Stat() the file again
  1572. * to see what happened.
  1573. */
  1574. a->pst = NULL;
  1575. if ((ret = lazy_stat(a)) != ARCHIVE_OK)
  1576. return (ret);
  1577. /* We can use lseek()/write() to extend the file if
  1578. * ftruncate didn't work or isn't available. */
  1579. if (a->st.st_size < a->filesize) {
  1580. const char nul = '\0';
  1581. if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) {
  1582. archive_set_error(&a->archive, errno,
  1583. "Seek failed");
  1584. return (ARCHIVE_FATAL);
  1585. }
  1586. if (write(a->fd, &nul, 1) < 0) {
  1587. archive_set_error(&a->archive, errno,
  1588. "Write to restore size failed");
  1589. return (ARCHIVE_FATAL);
  1590. }
  1591. a->pst = NULL;
  1592. }
  1593. }
  1594. /* Restore metadata. */
  1595. /*
  1596. * This is specific to Mac OS X.
  1597. * If the current file is an AppleDouble file, it should be
  1598. * linked with the data fork file and remove it.
  1599. */
  1600. if (a->todo & TODO_APPLEDOUBLE) {
  1601. int r2 = fixup_appledouble(a, a->name);
  1602. if (r2 == ARCHIVE_EOF) {
  1603. /* The current file has been successfully linked
  1604. * with the data fork file and removed. So there
  1605. * is nothing to do on the current file. */
  1606. goto finish_metadata;
  1607. }
  1608. if (r2 < ret) ret = r2;
  1609. }
  1610. /*
  1611. * Look up the "real" UID only if we're going to need it.
  1612. * TODO: the TODO_SGID condition can be dropped here, can't it?
  1613. */
  1614. if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
  1615. a->uid = archive_write_disk_uid(&a->archive,
  1616. archive_entry_uname(a->entry),
  1617. archive_entry_uid(a->entry));
  1618. }
  1619. /* Look up the "real" GID only if we're going to need it. */
  1620. /* TODO: the TODO_SUID condition can be dropped here, can't it? */
  1621. if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
  1622. a->gid = archive_write_disk_gid(&a->archive,
  1623. archive_entry_gname(a->entry),
  1624. archive_entry_gid(a->entry));
  1625. }
  1626. /*
  1627. * Restore ownership before set_mode tries to restore suid/sgid
  1628. * bits. If we set the owner, we know what it is and can skip
  1629. * a stat() call to examine the ownership of the file on disk.
  1630. */
  1631. if (a->todo & TODO_OWNER) {
  1632. int r2 = set_ownership(a);
  1633. if (r2 < ret) ret = r2;
  1634. }
  1635. /*
  1636. * HYPOTHESIS:
  1637. * If we're not root, we won't be setting any security
  1638. * attributes that may be wiped by the set_mode() routine
  1639. * below. We also can't set xattr on non-owner-writable files,
  1640. * which may be the state after set_mode(). Perform
  1641. * set_xattrs() first based on these constraints.
  1642. */
  1643. if (a->user_uid != 0 &&
  1644. (a->todo & TODO_XATTR)) {
  1645. int r2 = set_xattrs(a);
  1646. if (r2 < ret) ret = r2;
  1647. }
  1648. /*
  1649. * set_mode must precede ACLs on systems such as Solaris and
  1650. * FreeBSD where setting the mode implicitly clears extended ACLs
  1651. */
  1652. if (a->todo & TODO_MODE) {
  1653. int r2 = set_mode(a, a->mode);
  1654. if (r2 < ret) ret = r2;
  1655. }
  1656. /*
  1657. * Security-related extended attributes (such as
  1658. * security.capability on Linux) have to be restored last,
  1659. * since they're implicitly removed by other file changes.
  1660. * We do this last only when root.
  1661. */
  1662. if (a->user_uid == 0 &&
  1663. (a->todo & TODO_XATTR)) {
  1664. int r2 = set_xattrs(a);
  1665. if (r2 < ret) ret = r2;
  1666. }
  1667. /*
  1668. * Some flags prevent file modification; they must be restored after
  1669. * file contents are written.
  1670. */
  1671. if (a->todo & TODO_FFLAGS) {
  1672. int r2 = set_fflags(a);
  1673. if (r2 < ret) ret = r2;
  1674. }
  1675. /*
  1676. * Time must follow most other metadata;
  1677. * otherwise atime will get changed.
  1678. */
  1679. if (a->todo & TODO_TIMES) {
  1680. int r2 = set_times_from_entry(a);
  1681. if (r2 < ret) ret = r2;
  1682. }
  1683. /*
  1684. * Mac extended metadata includes ACLs.
  1685. */
  1686. if (a->todo & TODO_MAC_METADATA) {
  1687. const void *metadata;
  1688. size_t metadata_size;
  1689. metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
  1690. if (metadata != NULL && metadata_size > 0) {
  1691. int r2 = set_mac_metadata(a, archive_entry_pathname(
  1692. a->entry), metadata, metadata_size);
  1693. if (r2 < ret) ret = r2;
  1694. }
  1695. }
  1696. /*
  1697. * ACLs must be restored after timestamps because there are
  1698. * ACLs that prevent attribute changes (including time).
  1699. */
  1700. if (a->todo & TODO_ACLS) {
  1701. int r2;
  1702. r2 = archive_write_disk_set_acls(&a->archive, a->fd,
  1703. archive_entry_pathname(a->entry),
  1704. archive_entry_acl(a->entry),
  1705. archive_entry_mode(a->entry));
  1706. if (r2 < ret) ret = r2;
  1707. }
  1708. finish_metadata:
  1709. /* If there's an fd, we can close it now. */
  1710. if (a->fd >= 0) {
  1711. close(a->fd);
  1712. a->fd = -1;
  1713. if (a->tmpname) {
  1714. if (rename(a->tmpname, a->name) == -1) {
  1715. archive_set_error(&a->archive, errno,
  1716. "rename failed");
  1717. ret = ARCHIVE_FATAL;
  1718. }
  1719. a->tmpname = NULL;
  1720. }
  1721. }
  1722. /* If there's an entry, we can release it now. */
  1723. archive_entry_free(a->entry);
  1724. a->entry = NULL;
  1725. a->archive.state = ARCHIVE_STATE_HEADER;
  1726. return (ret);
  1727. }
  1728. int
  1729. archive_write_disk_set_group_lookup(struct archive *_a,
  1730. void *private_data,
  1731. la_int64_t (*lookup_gid)(void *private, const char *gname, la_int64_t gid),
  1732. void (*cleanup_gid)(void *private))
  1733. {
  1734. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1735. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1736. ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
  1737. if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
  1738. (a->cleanup_gid)(a->lookup_gid_data);
  1739. a->lookup_gid = lookup_gid;
  1740. a->cleanup_gid = cleanup_gid;
  1741. a->lookup_gid_data = private_data;
  1742. return (ARCHIVE_OK);
  1743. }
  1744. int
  1745. archive_write_disk_set_user_lookup(struct archive *_a,
  1746. void *private_data,
  1747. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
  1748. void (*cleanup_uid)(void *private))
  1749. {
  1750. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1751. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1752. ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
  1753. if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
  1754. (a->cleanup_uid)(a->lookup_uid_data);
  1755. a->lookup_uid = lookup_uid;
  1756. a->cleanup_uid = cleanup_uid;
  1757. a->lookup_uid_data = private_data;
  1758. return (ARCHIVE_OK);
  1759. }
  1760. int64_t
  1761. archive_write_disk_gid(struct archive *_a, const char *name, la_int64_t id)
  1762. {
  1763. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1764. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1765. ARCHIVE_STATE_ANY, "archive_write_disk_gid");
  1766. if (a->lookup_gid)
  1767. return (a->lookup_gid)(a->lookup_gid_data, name, id);
  1768. return (id);
  1769. }
  1770. int64_t
  1771. archive_write_disk_uid(struct archive *_a, const char *name, la_int64_t id)
  1772. {
  1773. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1774. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1775. ARCHIVE_STATE_ANY, "archive_write_disk_uid");
  1776. if (a->lookup_uid)
  1777. return (a->lookup_uid)(a->lookup_uid_data, name, id);
  1778. return (id);
  1779. }
  1780. /*
  1781. * Create a new archive_write_disk object and initialize it with global state.
  1782. */
  1783. struct archive *
  1784. archive_write_disk_new(void)
  1785. {
  1786. struct archive_write_disk *a;
  1787. a = (struct archive_write_disk *)calloc(1, sizeof(*a));
  1788. if (a == NULL)
  1789. return (NULL);
  1790. a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
  1791. /* We're ready to write a header immediately. */
  1792. a->archive.state = ARCHIVE_STATE_HEADER;
  1793. a->archive.vtable = archive_write_disk_vtable();
  1794. a->start_time = time(NULL);
  1795. /* Query and restore the umask. */
  1796. umask(a->user_umask = umask(0));
  1797. #ifdef HAVE_GETEUID
  1798. a->user_uid = geteuid();
  1799. #endif /* HAVE_GETEUID */
  1800. if (archive_string_ensure(&a->path_safe, 512) == NULL) {
  1801. free(a);
  1802. return (NULL);
  1803. }
  1804. #ifdef HAVE_ZLIB_H
  1805. a->decmpfs_compression_level = 5;
  1806. #endif
  1807. return (&a->archive);
  1808. }
  1809. /*
  1810. * If pathname is longer than PATH_MAX, chdir to a suitable
  1811. * intermediate dir and edit the path down to a shorter suffix. Note
  1812. * that this routine never returns an error; if the chdir() attempt
  1813. * fails for any reason, we just go ahead with the long pathname. The
  1814. * object creation is likely to fail, but any error will get handled
  1815. * at that time.
  1816. */
  1817. #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
  1818. static void
  1819. edit_deep_directories(struct archive_write_disk *a)
  1820. {
  1821. int ret;
  1822. char *tail = a->name;
  1823. /* If path is short, avoid the open() below. */
  1824. if (strlen(tail) < PATH_MAX)
  1825. return;
  1826. /* Try to record our starting dir. */
  1827. a->restore_pwd = la_opendirat(AT_FDCWD, ".");
  1828. __archive_ensure_cloexec_flag(a->restore_pwd);
  1829. if (a->restore_pwd < 0)
  1830. return;
  1831. /* As long as the path is too long... */
  1832. while (strlen(tail) >= PATH_MAX) {
  1833. /* Locate a dir prefix shorter than PATH_MAX. */
  1834. tail += PATH_MAX - 8;
  1835. while (tail > a->name && *tail != '/')
  1836. tail--;
  1837. /* Exit if we find a too-long path component. */
  1838. if (tail <= a->name)
  1839. return;
  1840. /* Create the intermediate dir and chdir to it. */
  1841. *tail = '\0'; /* Terminate dir portion */
  1842. ret = create_dir(a, a->name);
  1843. if (ret == ARCHIVE_OK && chdir(a->name) != 0)
  1844. ret = ARCHIVE_FAILED;
  1845. *tail = '/'; /* Restore the / we removed. */
  1846. if (ret != ARCHIVE_OK)
  1847. return;
  1848. tail++;
  1849. /* The chdir() succeeded; we've now shortened the path. */
  1850. a->name = tail;
  1851. }
  1852. return;
  1853. }
  1854. #endif
  1855. /*
  1856. * The main restore function.
  1857. */
  1858. static int
  1859. restore_entry(struct archive_write_disk *a)
  1860. {
  1861. int ret = ARCHIVE_OK, en;
  1862. if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
  1863. /*
  1864. * TODO: Fix this. Apparently, there are platforms
  1865. * that still allow root to hose the entire filesystem
  1866. * by unlinking a dir. The S_ISDIR() test above
  1867. * prevents us from using unlink() here if the new
  1868. * object is a dir, but that doesn't mean the old
  1869. * object isn't a dir.
  1870. */
  1871. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1872. (void)clear_nochange_fflags(a);
  1873. if (unlink(a->name) == 0) {
  1874. /* We removed it, reset cached stat. */
  1875. a->pst = NULL;
  1876. } else if (errno == ENOENT) {
  1877. /* File didn't exist, that's just as good. */
  1878. } else if (rmdir(a->name) == 0) {
  1879. /* It was a dir, but now it's gone. */
  1880. a->pst = NULL;
  1881. } else {
  1882. /* We tried, but couldn't get rid of it. */
  1883. archive_set_error(&a->archive, errno,
  1884. "Could not unlink");
  1885. return(ARCHIVE_FAILED);
  1886. }
  1887. }
  1888. /* Try creating it first; if this fails, we'll try to recover. */
  1889. en = create_filesystem_object(a);
  1890. if ((en == ENOTDIR || en == ENOENT)
  1891. && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
  1892. /* If the parent dir doesn't exist, try creating it. */
  1893. create_parent_dir(a, a->name);
  1894. /* Now try to create the object again. */
  1895. en = create_filesystem_object(a);
  1896. }
  1897. if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) {
  1898. archive_set_error(&a->archive, en,
  1899. "Hard-link target '%s' does not exist.",
  1900. archive_entry_hardlink(a->entry));
  1901. return (ARCHIVE_FAILED);
  1902. }
  1903. if ((en == EISDIR || en == EEXIST)
  1904. && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1905. /* If we're not overwriting, we're done. */
  1906. if (S_ISDIR(a->mode)) {
  1907. /* Don't overwrite any settings on existing directories. */
  1908. a->todo = 0;
  1909. }
  1910. archive_entry_unset_size(a->entry);
  1911. return (ARCHIVE_OK);
  1912. }
  1913. /*
  1914. * Some platforms return EISDIR if you call
  1915. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  1916. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  1917. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  1918. * on an existing item.
  1919. */
  1920. if (en == EISDIR) {
  1921. /* A dir is in the way of a non-dir, rmdir it. */
  1922. if (rmdir(a->name) != 0) {
  1923. archive_set_error(&a->archive, errno,
  1924. "Can't remove already-existing dir");
  1925. return (ARCHIVE_FAILED);
  1926. }
  1927. a->pst = NULL;
  1928. /* Try again. */
  1929. en = create_filesystem_object(a);
  1930. } else if (en == EEXIST) {
  1931. /*
  1932. * We know something is in the way, but we don't know what;
  1933. * we need to find out before we go any further.
  1934. */
  1935. int r = 0;
  1936. /*
  1937. * The SECURE_SYMLINKS logic has already removed a
  1938. * symlink to a dir if the client wants that. So
  1939. * follow the symlink if we're creating a dir.
  1940. */
  1941. if (S_ISDIR(a->mode))
  1942. r = la_stat(a->name, &a->st);
  1943. /*
  1944. * If it's not a dir (or it's a broken symlink),
  1945. * then don't follow it.
  1946. */
  1947. if (r != 0 || !S_ISDIR(a->mode))
  1948. r = lstat(a->name, &a->st);
  1949. if (r != 0) {
  1950. archive_set_error(&a->archive, errno,
  1951. "Can't stat existing object");
  1952. return (ARCHIVE_FAILED);
  1953. }
  1954. /*
  1955. * NO_OVERWRITE_NEWER doesn't apply to directories.
  1956. */
  1957. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  1958. && !S_ISDIR(a->st.st_mode)) {
  1959. if (!older(&(a->st), a->entry)) {
  1960. archive_entry_unset_size(a->entry);
  1961. return (ARCHIVE_OK);
  1962. }
  1963. }
  1964. /* If it's our archive, we're done. */
  1965. if (a->skip_file_set &&
  1966. a->st.st_dev == (dev_t)a->skip_file_dev &&
  1967. a->st.st_ino == (ino_t)a->skip_file_ino) {
  1968. archive_set_error(&a->archive, 0,
  1969. "Refusing to overwrite archive");
  1970. return (ARCHIVE_FAILED);
  1971. }
  1972. if (!S_ISDIR(a->st.st_mode)) {
  1973. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1974. (void)clear_nochange_fflags(a);
  1975. if ((a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) &&
  1976. S_ISREG(a->st.st_mode)) {
  1977. /* Use a temporary file to extract */
  1978. if ((a->fd = la_mktemp(a)) == -1)
  1979. return ARCHIVE_FAILED;
  1980. a->pst = NULL;
  1981. en = 0;
  1982. } else {
  1983. /* A non-dir is in the way, unlink it. */
  1984. if (unlink(a->name) != 0) {
  1985. archive_set_error(&a->archive, errno,
  1986. "Can't unlink already-existing "
  1987. "object");
  1988. return (ARCHIVE_FAILED);
  1989. }
  1990. a->pst = NULL;
  1991. /* Try again. */
  1992. en = create_filesystem_object(a);
  1993. }
  1994. } else if (!S_ISDIR(a->mode)) {
  1995. /* A dir is in the way of a non-dir, rmdir it. */
  1996. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1997. (void)clear_nochange_fflags(a);
  1998. if (rmdir(a->name) != 0) {
  1999. archive_set_error(&a->archive, errno,
  2000. "Can't replace existing directory with non-directory");
  2001. return (ARCHIVE_FAILED);
  2002. }
  2003. /* Try again. */
  2004. en = create_filesystem_object(a);
  2005. } else {
  2006. /*
  2007. * There's a dir in the way of a dir. Don't
  2008. * waste time with rmdir()/mkdir(), just fix
  2009. * up the permissions on the existing dir.
  2010. * Note that we don't change perms on existing
  2011. * dirs unless _EXTRACT_PERM is specified.
  2012. */
  2013. if ((a->mode != a->st.st_mode)
  2014. && (a->todo & TODO_MODE_FORCE))
  2015. a->deferred |= (a->todo & TODO_MODE);
  2016. /* Ownership doesn't need deferred fixup. */
  2017. en = 0; /* Forget the EEXIST. */
  2018. }
  2019. }
  2020. if (en) {
  2021. /* Everything failed; give up here. */
  2022. if ((&a->archive)->error == NULL)
  2023. archive_set_error(&a->archive, en, "Can't create '%s'",
  2024. a->name);
  2025. return (ARCHIVE_FAILED);
  2026. }
  2027. a->pst = NULL; /* Cached stat data no longer valid. */
  2028. return (ret);
  2029. }
  2030. /*
  2031. * Returns 0 if creation succeeds, or else returns errno value from
  2032. * the failed system call. Note: This function should only ever perform
  2033. * a single system call.
  2034. */
  2035. static int
  2036. create_filesystem_object(struct archive_write_disk *a)
  2037. {
  2038. /* Create the entry. */
  2039. const char *linkname;
  2040. mode_t final_mode, mode;
  2041. int r;
  2042. /* these for check_symlinks_fsobj */
  2043. char *linkname_copy; /* non-const copy of linkname */
  2044. struct stat st;
  2045. struct archive_string error_string;
  2046. int error_number;
  2047. /* We identify hard/symlinks according to the link names. */
  2048. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  2049. linkname = archive_entry_hardlink(a->entry);
  2050. if (linkname != NULL) {
  2051. #if !HAVE_LINK
  2052. return (EPERM);
  2053. #else
  2054. archive_string_init(&error_string);
  2055. linkname_copy = strdup(linkname);
  2056. if (linkname_copy == NULL) {
  2057. return (EPERM);
  2058. }
  2059. /*
  2060. * TODO: consider using the cleaned-up path as the link
  2061. * target?
  2062. */
  2063. r = cleanup_pathname_fsobj(linkname_copy, &error_number,
  2064. &error_string, a->flags);
  2065. if (r != ARCHIVE_OK) {
  2066. archive_set_error(&a->archive, error_number, "%s",
  2067. error_string.s);
  2068. free(linkname_copy);
  2069. archive_string_free(&error_string);
  2070. /*
  2071. * EPERM is more appropriate than error_number for our
  2072. * callers
  2073. */
  2074. return (EPERM);
  2075. }
  2076. r = check_symlinks_fsobj(linkname_copy, &error_number,
  2077. &error_string, a->flags);
  2078. if (r != ARCHIVE_OK) {
  2079. archive_set_error(&a->archive, error_number, "%s",
  2080. error_string.s);
  2081. free(linkname_copy);
  2082. archive_string_free(&error_string);
  2083. /*
  2084. * EPERM is more appropriate than error_number for our
  2085. * callers
  2086. */
  2087. return (EPERM);
  2088. }
  2089. free(linkname_copy);
  2090. archive_string_free(&error_string);
  2091. /*
  2092. * Unlinking and linking here is really not atomic,
  2093. * but doing it right, would require us to construct
  2094. * an mktemplink() function, and then use rename(2).
  2095. */
  2096. if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES)
  2097. unlink(a->name);
  2098. r = link(linkname, a->name) ? errno : 0;
  2099. /*
  2100. * New cpio and pax formats allow hardlink entries
  2101. * to carry data, so we may have to open the file
  2102. * for hardlink entries.
  2103. *
  2104. * If the hardlink was successfully created and
  2105. * the archive doesn't have carry data for it,
  2106. * consider it to be non-authoritative for meta data.
  2107. * This is consistent with GNU tar and BSD pax.
  2108. * If the hardlink does carry data, let the last
  2109. * archive entry decide ownership.
  2110. */
  2111. if (r == 0 && a->filesize <= 0) {
  2112. a->todo = 0;
  2113. a->deferred = 0;
  2114. } else if (r == 0 && a->filesize > 0) {
  2115. #ifdef HAVE_LSTAT
  2116. r = lstat(a->name, &st);
  2117. #else
  2118. r = la_stat(a->name, &st);
  2119. #endif
  2120. if (r != 0)
  2121. r = errno;
  2122. else if ((st.st_mode & AE_IFMT) == AE_IFREG) {
  2123. a->fd = open(a->name, O_WRONLY | O_TRUNC |
  2124. O_BINARY | O_CLOEXEC | O_NOFOLLOW);
  2125. __archive_ensure_cloexec_flag(a->fd);
  2126. if (a->fd < 0)
  2127. r = errno;
  2128. }
  2129. }
  2130. return (r);
  2131. #endif
  2132. }
  2133. linkname = archive_entry_symlink(a->entry);
  2134. if (linkname != NULL) {
  2135. #if HAVE_SYMLINK
  2136. /*
  2137. * Unlinking and linking here is really not atomic,
  2138. * but doing it right, would require us to construct
  2139. * an mktempsymlink() function, and then use rename(2).
  2140. */
  2141. if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES)
  2142. unlink(a->name);
  2143. return symlink(linkname, a->name) ? errno : 0;
  2144. #else
  2145. return (EPERM);
  2146. #endif
  2147. }
  2148. /*
  2149. * The remaining system calls all set permissions, so let's
  2150. * try to take advantage of that to avoid an extra chmod()
  2151. * call. (Recall that umask is set to zero right now!)
  2152. */
  2153. /* Mode we want for the final restored object (w/o file type bits). */
  2154. final_mode = a->mode & 07777;
  2155. /*
  2156. * The mode that will actually be restored in this step. Note
  2157. * that SUID, SGID, etc, require additional work to ensure
  2158. * security, so we never restore them at this point.
  2159. */
  2160. mode = final_mode & 0777 & ~a->user_umask;
  2161. /*
  2162. * Always create writable such that [f]setxattr() works if we're not
  2163. * root.
  2164. */
  2165. if (a->user_uid != 0 &&
  2166. a->todo & (TODO_HFS_COMPRESSION | TODO_XATTR)) {
  2167. mode |= 0200;
  2168. }
  2169. switch (a->mode & AE_IFMT) {
  2170. default:
  2171. /* POSIX requires that we fall through here. */
  2172. /* FALLTHROUGH */
  2173. case AE_IFREG:
  2174. a->tmpname = NULL;
  2175. a->fd = open(a->name,
  2176. O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, mode);
  2177. __archive_ensure_cloexec_flag(a->fd);
  2178. r = (a->fd < 0);
  2179. break;
  2180. case AE_IFCHR:
  2181. #ifdef HAVE_MKNOD
  2182. /* Note: we use AE_IFCHR for the case label, and
  2183. * S_IFCHR for the mknod() call. This is correct. */
  2184. r = mknod(a->name, mode | S_IFCHR,
  2185. archive_entry_rdev(a->entry));
  2186. break;
  2187. #else
  2188. /* TODO: Find a better way to warn about our inability
  2189. * to restore a char device node. */
  2190. return (EINVAL);
  2191. #endif /* HAVE_MKNOD */
  2192. case AE_IFBLK:
  2193. #ifdef HAVE_MKNOD
  2194. r = mknod(a->name, mode | S_IFBLK,
  2195. archive_entry_rdev(a->entry));
  2196. break;
  2197. #else
  2198. /* TODO: Find a better way to warn about our inability
  2199. * to restore a block device node. */
  2200. return (EINVAL);
  2201. #endif /* HAVE_MKNOD */
  2202. case AE_IFDIR:
  2203. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  2204. r = mkdir(a->name, mode);
  2205. if (r == 0) {
  2206. /* Defer setting dir times. */
  2207. a->deferred |= (a->todo & TODO_TIMES);
  2208. a->todo &= ~TODO_TIMES;
  2209. /* Never use an immediate chmod(). */
  2210. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  2211. * because of SysV SGID inheritance. */
  2212. if ((mode != final_mode)
  2213. || (a->flags & ARCHIVE_EXTRACT_PERM))
  2214. a->deferred |= (a->todo & TODO_MODE);
  2215. a->todo &= ~TODO_MODE;
  2216. }
  2217. break;
  2218. case AE_IFIFO:
  2219. #ifdef HAVE_MKFIFO
  2220. r = mkfifo(a->name, mode);
  2221. break;
  2222. #else
  2223. /* TODO: Find a better way to warn about our inability
  2224. * to restore a fifo. */
  2225. return (EINVAL);
  2226. #endif /* HAVE_MKFIFO */
  2227. }
  2228. /* All the system calls above set errno on failure. */
  2229. if (r)
  2230. return (errno);
  2231. /* If we managed to set the final mode, we've avoided a chmod(). */
  2232. if (mode == final_mode)
  2233. a->todo &= ~TODO_MODE;
  2234. return (0);
  2235. }
  2236. /*
  2237. * Cleanup function for archive_extract. Mostly, this involves processing
  2238. * the fixup list, which is used to address a number of problems:
  2239. * * Dir permissions might prevent us from restoring a file in that
  2240. * dir, so we restore the dir with minimum 0700 permissions first,
  2241. * then correct the mode at the end.
  2242. * * Similarly, the act of restoring a file touches the directory
  2243. * and changes the timestamp on the dir, so we have to touch-up dir
  2244. * timestamps at the end as well.
  2245. * * Some file flags can interfere with the restore by, for example,
  2246. * preventing the creation of hardlinks to those files.
  2247. * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
  2248. *
  2249. * Note that tar/cpio do not require that archives be in a particular
  2250. * order; there is no way to know when the last file has been restored
  2251. * within a directory, so there's no way to optimize the memory usage
  2252. * here by fixing up the directory any earlier than the
  2253. * end-of-archive.
  2254. *
  2255. * XXX TODO: Directory ACLs should be restored here, for the same
  2256. * reason we set directory perms here. XXX
  2257. */
  2258. static int
  2259. _archive_write_disk_close(struct archive *_a)
  2260. {
  2261. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  2262. struct fixup_entry *next, *p;
  2263. int fd, ret;
  2264. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  2265. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  2266. "archive_write_disk_close");
  2267. ret = _archive_write_disk_finish_entry(&a->archive);
  2268. /* Sort dir list so directories are fixed up in depth-first order. */
  2269. p = sort_dir_list(a->fixup_list);
  2270. while (p != NULL) {
  2271. fd = -1;
  2272. a->pst = NULL; /* Mark stat cache as out-of-date. */
  2273. if (p->fixup &
  2274. (TODO_TIMES | TODO_MODE_BASE | TODO_ACLS | TODO_FFLAGS)) {
  2275. fd = open(p->name,
  2276. O_WRONLY | O_BINARY | O_NOFOLLOW | O_CLOEXEC);
  2277. }
  2278. if (p->fixup & TODO_TIMES) {
  2279. set_times(a, fd, p->mode, p->name,
  2280. p->atime, p->atime_nanos,
  2281. p->birthtime, p->birthtime_nanos,
  2282. p->mtime, p->mtime_nanos,
  2283. p->ctime, p->ctime_nanos);
  2284. }
  2285. if (p->fixup & TODO_MODE_BASE) {
  2286. #ifdef HAVE_FCHMOD
  2287. if (fd >= 0)
  2288. fchmod(fd, p->mode);
  2289. else
  2290. #endif
  2291. chmod(p->name, p->mode);
  2292. }
  2293. if (p->fixup & TODO_ACLS)
  2294. archive_write_disk_set_acls(&a->archive, fd,
  2295. p->name, &p->acl, p->mode);
  2296. if (p->fixup & TODO_FFLAGS)
  2297. set_fflags_platform(a, fd, p->name,
  2298. p->mode, p->fflags_set, 0);
  2299. if (p->fixup & TODO_MAC_METADATA)
  2300. set_mac_metadata(a, p->name, p->mac_metadata,
  2301. p->mac_metadata_size);
  2302. next = p->next;
  2303. archive_acl_clear(&p->acl);
  2304. free(p->mac_metadata);
  2305. free(p->name);
  2306. if (fd >= 0)
  2307. close(fd);
  2308. free(p);
  2309. p = next;
  2310. }
  2311. a->fixup_list = NULL;
  2312. return (ret);
  2313. }
  2314. static int
  2315. _archive_write_disk_free(struct archive *_a)
  2316. {
  2317. struct archive_write_disk *a;
  2318. int ret;
  2319. if (_a == NULL)
  2320. return (ARCHIVE_OK);
  2321. archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
  2322. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
  2323. a = (struct archive_write_disk *)_a;
  2324. ret = _archive_write_disk_close(&a->archive);
  2325. archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
  2326. archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
  2327. archive_entry_free(a->entry);
  2328. archive_string_free(&a->_name_data);
  2329. archive_string_free(&a->_tmpname_data);
  2330. archive_string_free(&a->archive.error_string);
  2331. archive_string_free(&a->path_safe);
  2332. a->archive.magic = 0;
  2333. __archive_clean(&a->archive);
  2334. free(a->decmpfs_header_p);
  2335. free(a->resource_fork);
  2336. free(a->compressed_buffer);
  2337. free(a->uncompressed_buffer);
  2338. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
  2339. && defined(HAVE_ZLIB_H)
  2340. if (a->stream_valid) {
  2341. switch (deflateEnd(&a->stream)) {
  2342. case Z_OK:
  2343. break;
  2344. default:
  2345. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2346. "Failed to clean up compressor");
  2347. ret = ARCHIVE_FATAL;
  2348. break;
  2349. }
  2350. }
  2351. #endif
  2352. free(a);
  2353. return (ret);
  2354. }
  2355. /*
  2356. * Simple O(n log n) merge sort to order the fixup list. In
  2357. * particular, we want to restore dir timestamps depth-first.
  2358. */
  2359. static struct fixup_entry *
  2360. sort_dir_list(struct fixup_entry *p)
  2361. {
  2362. struct fixup_entry *a, *b, *t;
  2363. if (p == NULL)
  2364. return (NULL);
  2365. /* A one-item list is already sorted. */
  2366. if (p->next == NULL)
  2367. return (p);
  2368. /* Step 1: split the list. */
  2369. t = p;
  2370. a = p->next->next;
  2371. while (a != NULL) {
  2372. /* Step a twice, t once. */
  2373. a = a->next;
  2374. if (a != NULL)
  2375. a = a->next;
  2376. t = t->next;
  2377. }
  2378. /* Now, t is at the mid-point, so break the list here. */
  2379. b = t->next;
  2380. t->next = NULL;
  2381. a = p;
  2382. /* Step 2: Recursively sort the two sub-lists. */
  2383. a = sort_dir_list(a);
  2384. b = sort_dir_list(b);
  2385. /* Step 3: Merge the returned lists. */
  2386. /* Pick the first element for the merged list. */
  2387. if (strcmp(a->name, b->name) > 0) {
  2388. t = p = a;
  2389. a = a->next;
  2390. } else {
  2391. t = p = b;
  2392. b = b->next;
  2393. }
  2394. /* Always put the later element on the list first. */
  2395. while (a != NULL && b != NULL) {
  2396. if (strcmp(a->name, b->name) > 0) {
  2397. t->next = a;
  2398. a = a->next;
  2399. } else {
  2400. t->next = b;
  2401. b = b->next;
  2402. }
  2403. t = t->next;
  2404. }
  2405. /* Only one list is non-empty, so just splice it on. */
  2406. if (a != NULL)
  2407. t->next = a;
  2408. if (b != NULL)
  2409. t->next = b;
  2410. return (p);
  2411. }
  2412. /*
  2413. * Returns a new, initialized fixup entry.
  2414. *
  2415. * TODO: Reduce the memory requirements for this list by using a tree
  2416. * structure rather than a simple list of names.
  2417. */
  2418. static struct fixup_entry *
  2419. new_fixup(struct archive_write_disk *a, const char *pathname)
  2420. {
  2421. struct fixup_entry *fe;
  2422. fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
  2423. if (fe == NULL) {
  2424. archive_set_error(&a->archive, ENOMEM,
  2425. "Can't allocate memory for a fixup");
  2426. return (NULL);
  2427. }
  2428. fe->next = a->fixup_list;
  2429. a->fixup_list = fe;
  2430. fe->fixup = 0;
  2431. fe->name = strdup(pathname);
  2432. return (fe);
  2433. }
  2434. /*
  2435. * Returns a fixup structure for the current entry.
  2436. */
  2437. static struct fixup_entry *
  2438. current_fixup(struct archive_write_disk *a, const char *pathname)
  2439. {
  2440. if (a->current_fixup == NULL)
  2441. a->current_fixup = new_fixup(a, pathname);
  2442. return (a->current_fixup);
  2443. }
  2444. /* Error helper for new *_fsobj functions */
  2445. static void
  2446. fsobj_error(int *a_eno, struct archive_string *a_estr,
  2447. int err, const char *errstr, const char *path)
  2448. {
  2449. if (a_eno)
  2450. *a_eno = err;
  2451. if (a_estr)
  2452. archive_string_sprintf(a_estr, "%s%s", errstr, path);
  2453. }
  2454. /*
  2455. * TODO: Someday, integrate this with the deep dir support; they both
  2456. * scan the path and both can be optimized by comparing against other
  2457. * recent paths.
  2458. */
  2459. /*
  2460. * Checks the given path to see if any elements along it are symlinks. Returns
  2461. * ARCHIVE_OK if there are none, otherwise puts an error in errmsg.
  2462. */
  2463. static int
  2464. check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
  2465. int flags)
  2466. {
  2467. #if !defined(HAVE_LSTAT) && \
  2468. !(defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT))
  2469. /* Platform doesn't have lstat, so we can't look for symlinks. */
  2470. (void)path; /* UNUSED */
  2471. (void)error_number; /* UNUSED */
  2472. (void)error_string; /* UNUSED */
  2473. (void)flags; /* UNUSED */
  2474. return (ARCHIVE_OK);
  2475. #else
  2476. int res = ARCHIVE_OK;
  2477. char *tail;
  2478. char *head;
  2479. int last;
  2480. char c;
  2481. int r;
  2482. struct stat st;
  2483. int chdir_fd;
  2484. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2485. int fd;
  2486. #endif
  2487. /* Nothing to do here if name is empty */
  2488. if(path[0] == '\0')
  2489. return (ARCHIVE_OK);
  2490. /*
  2491. * Guard against symlink tricks. Reject any archive entry whose
  2492. * destination would be altered by a symlink.
  2493. *
  2494. * Walk the filename in chunks separated by '/'. For each segment:
  2495. * - if it doesn't exist, continue
  2496. * - if it's symlink, abort or remove it
  2497. * - if it's a directory and it's not the last chunk, cd into it
  2498. * As we go:
  2499. * head points to the current (relative) path
  2500. * tail points to the temporary \0 terminating the segment we're
  2501. * currently examining
  2502. * c holds what used to be in *tail
  2503. * last is 1 if this is the last tail
  2504. */
  2505. chdir_fd = la_opendirat(AT_FDCWD, ".");
  2506. __archive_ensure_cloexec_flag(chdir_fd);
  2507. if (chdir_fd < 0) {
  2508. fsobj_error(a_eno, a_estr, errno,
  2509. "Could not open ", path);
  2510. return (ARCHIVE_FATAL);
  2511. }
  2512. head = path;
  2513. tail = path;
  2514. last = 0;
  2515. /* TODO: reintroduce a safe cache here? */
  2516. /* Skip the root directory if the path is absolute. */
  2517. if(tail == path && tail[0] == '/')
  2518. ++tail;
  2519. /* Keep going until we've checked the entire name.
  2520. * head, tail, path all alias the same string, which is
  2521. * temporarily zeroed at tail, so be careful restoring the
  2522. * stashed (c=tail[0]) for error messages.
  2523. * Exiting the loop with break is okay; continue is not.
  2524. */
  2525. while (!last) {
  2526. /*
  2527. * Skip the separator we just consumed, plus any adjacent ones
  2528. */
  2529. while (*tail == '/')
  2530. ++tail;
  2531. /* Skip the next path element. */
  2532. while (*tail != '\0' && *tail != '/')
  2533. ++tail;
  2534. /* is this the last path component? */
  2535. last = (tail[0] == '\0') || (tail[0] == '/' && tail[1] == '\0');
  2536. /* temporarily truncate the string here */
  2537. c = tail[0];
  2538. tail[0] = '\0';
  2539. /* Check that we haven't hit a symlink. */
  2540. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2541. r = fstatat(chdir_fd, head, &st, AT_SYMLINK_NOFOLLOW);
  2542. #else
  2543. r = lstat(head, &st);
  2544. #endif
  2545. if (r != 0) {
  2546. tail[0] = c;
  2547. /* We've hit a dir that doesn't exist; stop now. */
  2548. if (errno == ENOENT) {
  2549. break;
  2550. } else {
  2551. /*
  2552. * Treat any other error as fatal - best to be
  2553. * paranoid here.
  2554. * Note: This effectively disables deep
  2555. * directory support when security checks are
  2556. * enabled. Otherwise, very long pathnames that
  2557. * trigger an error here could evade the
  2558. * sandbox.
  2559. * TODO: We could do better, but it would
  2560. * probably require merging the symlink checks
  2561. * with the deep-directory editing.
  2562. */
  2563. fsobj_error(a_eno, a_estr, errno,
  2564. "Could not stat ", path);
  2565. res = ARCHIVE_FAILED;
  2566. break;
  2567. }
  2568. } else if (S_ISDIR(st.st_mode)) {
  2569. if (!last) {
  2570. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2571. fd = la_opendirat(chdir_fd, head);
  2572. if (fd < 0)
  2573. r = -1;
  2574. else {
  2575. r = 0;
  2576. close(chdir_fd);
  2577. chdir_fd = fd;
  2578. }
  2579. #else
  2580. r = chdir(head);
  2581. #endif
  2582. if (r != 0) {
  2583. tail[0] = c;
  2584. fsobj_error(a_eno, a_estr, errno,
  2585. "Could not chdir ", path);
  2586. res = (ARCHIVE_FATAL);
  2587. break;
  2588. }
  2589. /* Our view is now from inside this dir: */
  2590. head = tail + 1;
  2591. }
  2592. } else if (S_ISLNK(st.st_mode)) {
  2593. if (last) {
  2594. /*
  2595. * Last element is symlink; remove it
  2596. * so we can overwrite it with the
  2597. * item being extracted.
  2598. */
  2599. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2600. r = unlinkat(chdir_fd, head, 0);
  2601. #else
  2602. r = unlink(head);
  2603. #endif
  2604. if (r != 0) {
  2605. tail[0] = c;
  2606. fsobj_error(a_eno, a_estr, errno,
  2607. "Could not remove symlink ",
  2608. path);
  2609. res = ARCHIVE_FAILED;
  2610. break;
  2611. }
  2612. /*
  2613. * Even if we did remove it, a warning
  2614. * is in order. The warning is silly,
  2615. * though, if we're just replacing one
  2616. * symlink with another symlink.
  2617. */
  2618. tail[0] = c;
  2619. /*
  2620. * FIXME: not sure how important this is to
  2621. * restore
  2622. */
  2623. /*
  2624. if (!S_ISLNK(path)) {
  2625. fsobj_error(a_eno, a_estr, 0,
  2626. "Removing symlink ", path);
  2627. }
  2628. */
  2629. /* Symlink gone. No more problem! */
  2630. res = ARCHIVE_OK;
  2631. break;
  2632. } else if (flags & ARCHIVE_EXTRACT_UNLINK) {
  2633. /* User asked us to remove problems. */
  2634. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2635. r = unlinkat(chdir_fd, head, 0);
  2636. #else
  2637. r = unlink(head);
  2638. #endif
  2639. if (r != 0) {
  2640. tail[0] = c;
  2641. fsobj_error(a_eno, a_estr, 0,
  2642. "Cannot remove intervening "
  2643. "symlink ", path);
  2644. res = ARCHIVE_FAILED;
  2645. break;
  2646. }
  2647. tail[0] = c;
  2648. } else if ((flags &
  2649. ARCHIVE_EXTRACT_SECURE_SYMLINKS) == 0) {
  2650. /*
  2651. * We are not the last element and we want to
  2652. * follow symlinks if they are a directory.
  2653. *
  2654. * This is needed to extract hardlinks over
  2655. * symlinks.
  2656. */
  2657. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2658. r = fstatat(chdir_fd, head, &st, 0);
  2659. #else
  2660. r = la_stat(head, &st);
  2661. #endif
  2662. if (r != 0) {
  2663. tail[0] = c;
  2664. if (errno == ENOENT) {
  2665. break;
  2666. } else {
  2667. fsobj_error(a_eno, a_estr,
  2668. errno,
  2669. "Could not stat ", path);
  2670. res = (ARCHIVE_FAILED);
  2671. break;
  2672. }
  2673. } else if (S_ISDIR(st.st_mode)) {
  2674. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2675. fd = la_opendirat(chdir_fd, head);
  2676. if (fd < 0)
  2677. r = -1;
  2678. else {
  2679. r = 0;
  2680. close(chdir_fd);
  2681. chdir_fd = fd;
  2682. }
  2683. #else
  2684. r = chdir(head);
  2685. #endif
  2686. if (r != 0) {
  2687. tail[0] = c;
  2688. fsobj_error(a_eno, a_estr,
  2689. errno,
  2690. "Could not chdir ", path);
  2691. res = (ARCHIVE_FATAL);
  2692. break;
  2693. }
  2694. /*
  2695. * Our view is now from inside
  2696. * this dir:
  2697. */
  2698. head = tail + 1;
  2699. } else {
  2700. tail[0] = c;
  2701. fsobj_error(a_eno, a_estr, 0,
  2702. "Cannot extract through "
  2703. "symlink ", path);
  2704. res = ARCHIVE_FAILED;
  2705. break;
  2706. }
  2707. } else {
  2708. tail[0] = c;
  2709. fsobj_error(a_eno, a_estr, 0,
  2710. "Cannot extract through symlink ", path);
  2711. res = ARCHIVE_FAILED;
  2712. break;
  2713. }
  2714. }
  2715. /* be sure to always maintain this */
  2716. tail[0] = c;
  2717. if (tail[0] != '\0')
  2718. tail++; /* Advance to the next segment. */
  2719. }
  2720. /* Catches loop exits via break */
  2721. tail[0] = c;
  2722. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
  2723. /* If we operate with openat(), fstatat() and unlinkat() there was
  2724. * no chdir(), so just close the fd */
  2725. if (chdir_fd >= 0)
  2726. close(chdir_fd);
  2727. #elif HAVE_FCHDIR
  2728. /* If we changed directory above, restore it here. */
  2729. if (chdir_fd >= 0) {
  2730. r = fchdir(chdir_fd);
  2731. if (r != 0) {
  2732. fsobj_error(a_eno, a_estr, errno,
  2733. "chdir() failure", "");
  2734. }
  2735. close(chdir_fd);
  2736. chdir_fd = -1;
  2737. if (r != 0) {
  2738. res = (ARCHIVE_FATAL);
  2739. }
  2740. }
  2741. #endif
  2742. /* TODO: reintroduce a safe cache here? */
  2743. return res;
  2744. #endif
  2745. }
  2746. /*
  2747. * Check a->name for symlinks, returning ARCHIVE_OK if its clean, otherwise
  2748. * calls archive_set_error and returns ARCHIVE_{FATAL,FAILED}
  2749. */
  2750. static int
  2751. check_symlinks(struct archive_write_disk *a)
  2752. {
  2753. struct archive_string error_string;
  2754. int error_number;
  2755. int rc;
  2756. archive_string_init(&error_string);
  2757. rc = check_symlinks_fsobj(a->name, &error_number, &error_string,
  2758. a->flags);
  2759. if (rc != ARCHIVE_OK) {
  2760. archive_set_error(&a->archive, error_number, "%s",
  2761. error_string.s);
  2762. }
  2763. archive_string_free(&error_string);
  2764. a->pst = NULL; /* to be safe */
  2765. return rc;
  2766. }
  2767. #if defined(__CYGWIN__)
  2768. /*
  2769. * 1. Convert a path separator from '\' to '/' .
  2770. * We shouldn't check multibyte character directly because some
  2771. * character-set have been using the '\' character for a part of
  2772. * its multibyte character code.
  2773. * 2. Replace unusable characters in Windows with underscore('_').
  2774. * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
  2775. */
  2776. static void
  2777. cleanup_pathname_win(char *path)
  2778. {
  2779. wchar_t wc;
  2780. char *p;
  2781. size_t alen, l;
  2782. int mb, complete, utf8;
  2783. alen = 0;
  2784. mb = 0;
  2785. complete = 1;
  2786. utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0;
  2787. for (p = path; *p != '\0'; p++) {
  2788. ++alen;
  2789. if (*p == '\\') {
  2790. /* If previous byte is smaller than 128,
  2791. * this is not second byte of multibyte characters,
  2792. * so we can replace '\' with '/'. */
  2793. if (utf8 || !mb)
  2794. *p = '/';
  2795. else
  2796. complete = 0;/* uncompleted. */
  2797. } else if (*(unsigned char *)p > 127)
  2798. mb = 1;
  2799. else
  2800. mb = 0;
  2801. /* Rewrite the path name if its next character is unusable. */
  2802. if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
  2803. *p == '<' || *p == '>' || *p == '|')
  2804. *p = '_';
  2805. }
  2806. if (complete)
  2807. return;
  2808. /*
  2809. * Convert path separator in wide-character.
  2810. */
  2811. p = path;
  2812. while (*p != '\0' && alen) {
  2813. l = mbtowc(&wc, p, alen);
  2814. if (l == (size_t)-1) {
  2815. while (*p != '\0') {
  2816. if (*p == '\\')
  2817. *p = '/';
  2818. ++p;
  2819. }
  2820. break;
  2821. }
  2822. if (l == 1 && wc == L'\\')
  2823. *p = '/';
  2824. p += l;
  2825. alen -= l;
  2826. }
  2827. }
  2828. #endif
  2829. /*
  2830. * Canonicalize the pathname. In particular, this strips duplicate
  2831. * '/' characters, '.' elements, and trailing '/'. It also raises an
  2832. * error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is
  2833. * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
  2834. * is set) if the path is absolute.
  2835. */
  2836. static int
  2837. cleanup_pathname_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
  2838. int flags)
  2839. {
  2840. char *dest, *src;
  2841. char separator = '\0';
  2842. dest = src = path;
  2843. if (*src == '\0') {
  2844. fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
  2845. "Invalid empty ", "pathname");
  2846. return (ARCHIVE_FAILED);
  2847. }
  2848. #if defined(__CYGWIN__)
  2849. cleanup_pathname_win(path);
  2850. #endif
  2851. /* Skip leading '/'. */
  2852. if (*src == '/') {
  2853. if (flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) {
  2854. fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
  2855. "Path is ", "absolute");
  2856. return (ARCHIVE_FAILED);
  2857. }
  2858. separator = *src++;
  2859. }
  2860. /* Scan the pathname one element at a time. */
  2861. for (;;) {
  2862. /* src points to first char after '/' */
  2863. if (src[0] == '\0') {
  2864. break;
  2865. } else if (src[0] == '/') {
  2866. /* Found '//', ignore second one. */
  2867. src++;
  2868. continue;
  2869. } else if (src[0] == '.') {
  2870. if (src[1] == '\0') {
  2871. /* Ignore trailing '.' */
  2872. break;
  2873. } else if (src[1] == '/') {
  2874. /* Skip './'. */
  2875. src += 2;
  2876. continue;
  2877. } else if (src[1] == '.') {
  2878. if (src[2] == '/' || src[2] == '\0') {
  2879. /* Conditionally warn about '..' */
  2880. if (flags
  2881. & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  2882. fsobj_error(a_eno, a_estr,
  2883. ARCHIVE_ERRNO_MISC,
  2884. "Path contains ", "'..'");
  2885. return (ARCHIVE_FAILED);
  2886. }
  2887. }
  2888. /*
  2889. * Note: Under no circumstances do we
  2890. * remove '..' elements. In
  2891. * particular, restoring
  2892. * '/foo/../bar/' should create the
  2893. * 'foo' dir as a side-effect.
  2894. */
  2895. }
  2896. }
  2897. /* Copy current element, including leading '/'. */
  2898. if (separator)
  2899. *dest++ = '/';
  2900. while (*src != '\0' && *src != '/') {
  2901. *dest++ = *src++;
  2902. }
  2903. if (*src == '\0')
  2904. break;
  2905. /* Skip '/' separator. */
  2906. separator = *src++;
  2907. }
  2908. /*
  2909. * We've just copied zero or more path elements, not including the
  2910. * final '/'.
  2911. */
  2912. if (dest == path) {
  2913. /*
  2914. * Nothing got copied. The path must have been something
  2915. * like '.' or '/' or './' or '/././././/./'.
  2916. */
  2917. if (separator)
  2918. *dest++ = '/';
  2919. else
  2920. *dest++ = '.';
  2921. }
  2922. /* Terminate the result. */
  2923. *dest = '\0';
  2924. return (ARCHIVE_OK);
  2925. }
  2926. static int
  2927. cleanup_pathname(struct archive_write_disk *a)
  2928. {
  2929. struct archive_string error_string;
  2930. int error_number;
  2931. int rc;
  2932. archive_string_init(&error_string);
  2933. rc = cleanup_pathname_fsobj(a->name, &error_number, &error_string,
  2934. a->flags);
  2935. if (rc != ARCHIVE_OK) {
  2936. archive_set_error(&a->archive, error_number, "%s",
  2937. error_string.s);
  2938. }
  2939. archive_string_free(&error_string);
  2940. return rc;
  2941. }
  2942. /*
  2943. * Create the parent directory of the specified path, assuming path
  2944. * is already in mutable storage.
  2945. */
  2946. static int
  2947. create_parent_dir(struct archive_write_disk *a, char *path)
  2948. {
  2949. char *slash;
  2950. int r;
  2951. /* Remove tail element to obtain parent name. */
  2952. slash = strrchr(path, '/');
  2953. if (slash == NULL)
  2954. return (ARCHIVE_OK);
  2955. *slash = '\0';
  2956. r = create_dir(a, path);
  2957. *slash = '/';
  2958. return (r);
  2959. }
  2960. /*
  2961. * Create the specified dir, recursing to create parents as necessary.
  2962. *
  2963. * Returns ARCHIVE_OK if the path exists when we're done here.
  2964. * Otherwise, returns ARCHIVE_FAILED.
  2965. * Assumes path is in mutable storage; path is unchanged on exit.
  2966. */
  2967. static int
  2968. create_dir(struct archive_write_disk *a, char *path)
  2969. {
  2970. struct stat st;
  2971. struct fixup_entry *le;
  2972. char *slash, *base;
  2973. mode_t mode_final, mode;
  2974. int r;
  2975. /* Check for special names and just skip them. */
  2976. slash = strrchr(path, '/');
  2977. if (slash == NULL)
  2978. base = path;
  2979. else
  2980. base = slash + 1;
  2981. if (base[0] == '\0' ||
  2982. (base[0] == '.' && base[1] == '\0') ||
  2983. (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
  2984. /* Don't bother trying to create null path, '.', or '..'. */
  2985. if (slash != NULL) {
  2986. *slash = '\0';
  2987. r = create_dir(a, path);
  2988. *slash = '/';
  2989. return (r);
  2990. }
  2991. return (ARCHIVE_OK);
  2992. }
  2993. /*
  2994. * Yes, this should be stat() and not lstat(). Using lstat()
  2995. * here loses the ability to extract through symlinks. Also note
  2996. * that this should not use the a->st cache.
  2997. */
  2998. if (la_stat(path, &st) == 0) {
  2999. if (S_ISDIR(st.st_mode))
  3000. return (ARCHIVE_OK);
  3001. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  3002. archive_set_error(&a->archive, EEXIST,
  3003. "Can't create directory '%s'", path);
  3004. return (ARCHIVE_FAILED);
  3005. }
  3006. if (unlink(path) != 0) {
  3007. archive_set_error(&a->archive, errno,
  3008. "Can't create directory '%s': "
  3009. "Conflicting file cannot be removed",
  3010. path);
  3011. return (ARCHIVE_FAILED);
  3012. }
  3013. } else if (errno != ENOENT && errno != ENOTDIR) {
  3014. /* Stat failed? */
  3015. archive_set_error(&a->archive, errno,
  3016. "Can't test directory '%s'", path);
  3017. return (ARCHIVE_FAILED);
  3018. } else if (slash != NULL) {
  3019. *slash = '\0';
  3020. r = create_dir(a, path);
  3021. *slash = '/';
  3022. if (r != ARCHIVE_OK)
  3023. return (r);
  3024. }
  3025. /*
  3026. * Mode we want for the final restored directory. Per POSIX,
  3027. * implicitly-created dirs must be created obeying the umask.
  3028. * There's no mention whether this is different for privileged
  3029. * restores (which the rest of this code handles by pretending
  3030. * umask=0). I've chosen here to always obey the user's umask for
  3031. * implicit dirs, even if _EXTRACT_PERM was specified.
  3032. */
  3033. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  3034. /* Mode we want on disk during the restore process. */
  3035. mode = mode_final;
  3036. mode |= MINIMUM_DIR_MODE;
  3037. mode &= MAXIMUM_DIR_MODE;
  3038. if (mkdir(path, mode) == 0) {
  3039. if (mode != mode_final) {
  3040. le = new_fixup(a, path);
  3041. if (le == NULL)
  3042. return (ARCHIVE_FATAL);
  3043. le->fixup |=TODO_MODE_BASE;
  3044. le->mode = mode_final;
  3045. }
  3046. return (ARCHIVE_OK);
  3047. }
  3048. /*
  3049. * Without the following check, a/b/../b/c/d fails at the
  3050. * second visit to 'b', so 'd' can't be created. Note that we
  3051. * don't add it to the fixup list here, as it's already been
  3052. * added.
  3053. */
  3054. if (la_stat(path, &st) == 0 && S_ISDIR(st.st_mode))
  3055. return (ARCHIVE_OK);
  3056. archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
  3057. path);
  3058. return (ARCHIVE_FAILED);
  3059. }
  3060. /*
  3061. * Note: Although we can skip setting the user id if the desired user
  3062. * id matches the current user, we cannot skip setting the group, as
  3063. * many systems set the gid based on the containing directory. So
  3064. * we have to perform a chown syscall if we want to set the SGID
  3065. * bit. (The alternative is to stat() and then possibly chown(); it's
  3066. * more efficient to skip the stat() and just always chown().) Note
  3067. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  3068. * allows set_mode to skip the stat() check for the GID.
  3069. */
  3070. static int
  3071. set_ownership(struct archive_write_disk *a)
  3072. {
  3073. #if !defined(__CYGWIN__) && !defined(__linux__)
  3074. /*
  3075. * On Linux, a process may have the CAP_CHOWN capability.
  3076. * On Windows there is no 'root' user with uid 0.
  3077. * Elsewhere we can skip calling chown if we are not root and the desired
  3078. * user id does not match the current user.
  3079. */
  3080. if (a->user_uid != 0 && a->user_uid != a->uid) {
  3081. archive_set_error(&a->archive, errno,
  3082. "Can't set UID=%jd", (intmax_t)a->uid);
  3083. return (ARCHIVE_WARN);
  3084. }
  3085. #endif
  3086. #ifdef HAVE_FCHOWN
  3087. /* If we have an fd, we can avoid a race. */
  3088. if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
  3089. /* We've set owner and know uid/gid are correct. */
  3090. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  3091. return (ARCHIVE_OK);
  3092. }
  3093. #endif
  3094. /* We prefer lchown() but will use chown() if that's all we have. */
  3095. /* Of course, if we have neither, this will always fail. */
  3096. #ifdef HAVE_LCHOWN
  3097. if (lchown(a->name, a->uid, a->gid) == 0) {
  3098. /* We've set owner and know uid/gid are correct. */
  3099. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  3100. return (ARCHIVE_OK);
  3101. }
  3102. #elif HAVE_CHOWN
  3103. if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
  3104. /* We've set owner and know uid/gid are correct. */
  3105. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  3106. return (ARCHIVE_OK);
  3107. }
  3108. #endif
  3109. archive_set_error(&a->archive, errno,
  3110. "Can't set user=%jd/group=%jd for %s",
  3111. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  3112. return (ARCHIVE_WARN);
  3113. }
  3114. /*
  3115. * Note: Returns 0 on success, non-zero on failure.
  3116. */
  3117. static int
  3118. set_time(int fd, int mode, const char *name,
  3119. time_t atime, long atime_nsec,
  3120. time_t mtime, long mtime_nsec)
  3121. {
  3122. /* Select the best implementation for this platform. */
  3123. #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
  3124. /*
  3125. * utimensat() and futimens() are defined in
  3126. * POSIX.1-2008. They support ns resolution and setting times
  3127. * on fds and symlinks.
  3128. */
  3129. struct timespec ts[2];
  3130. (void)mode; /* UNUSED */
  3131. ts[0].tv_sec = atime;
  3132. ts[0].tv_nsec = atime_nsec;
  3133. ts[1].tv_sec = mtime;
  3134. ts[1].tv_nsec = mtime_nsec;
  3135. if (fd >= 0)
  3136. return futimens(fd, ts);
  3137. return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
  3138. #elif HAVE_UTIMES
  3139. /*
  3140. * The utimes()-family functions support µs-resolution and
  3141. * setting times fds and symlinks. utimes() is documented as
  3142. * LEGACY by POSIX, futimes() and lutimes() are not described
  3143. * in POSIX.
  3144. */
  3145. struct timeval times[2];
  3146. times[0].tv_sec = atime;
  3147. times[0].tv_usec = atime_nsec / 1000;
  3148. times[1].tv_sec = mtime;
  3149. times[1].tv_usec = mtime_nsec / 1000;
  3150. #ifdef HAVE_FUTIMES
  3151. if (fd >= 0)
  3152. return (futimes(fd, times));
  3153. #else
  3154. (void)fd; /* UNUSED */
  3155. #endif
  3156. #ifdef HAVE_LUTIMES
  3157. (void)mode; /* UNUSED */
  3158. return (lutimes(name, times));
  3159. #else
  3160. if (S_ISLNK(mode))
  3161. return (0);
  3162. return (utimes(name, times));
  3163. #endif
  3164. #elif defined(HAVE_UTIME)
  3165. /*
  3166. * utime() is POSIX-standard but only supports 1s resolution and
  3167. * does not support fds or symlinks.
  3168. */
  3169. struct utimbuf times;
  3170. (void)fd; /* UNUSED */
  3171. (void)name; /* UNUSED */
  3172. (void)atime_nsec; /* UNUSED */
  3173. (void)mtime_nsec; /* UNUSED */
  3174. times.actime = atime;
  3175. times.modtime = mtime;
  3176. if (S_ISLNK(mode))
  3177. return (ARCHIVE_OK);
  3178. return (utime(name, &times));
  3179. #else
  3180. /*
  3181. * We don't know how to set the time on this platform.
  3182. */
  3183. (void)fd; /* UNUSED */
  3184. (void)mode; /* UNUSED */
  3185. (void)name; /* UNUSED */
  3186. (void)atime_nsec; /* UNUSED */
  3187. (void)mtime_nsec; /* UNUSED */
  3188. return (ARCHIVE_WARN);
  3189. #endif
  3190. }
  3191. #ifdef F_SETTIMES
  3192. static int
  3193. set_time_tru64(int fd, int mode, const char *name,
  3194. time_t atime, long atime_nsec,
  3195. time_t mtime, long mtime_nsec,
  3196. time_t ctime, long ctime_nsec)
  3197. {
  3198. struct attr_timbuf tstamp;
  3199. tstamp.atime.tv_sec = atime;
  3200. tstamp.mtime.tv_sec = mtime;
  3201. tstamp.ctime.tv_sec = ctime;
  3202. #if defined (__hpux) && defined (__ia64)
  3203. tstamp.atime.tv_nsec = atime_nsec;
  3204. tstamp.mtime.tv_nsec = mtime_nsec;
  3205. tstamp.ctime.tv_nsec = ctime_nsec;
  3206. #else
  3207. tstamp.atime.tv_usec = atime_nsec / 1000;
  3208. tstamp.mtime.tv_usec = mtime_nsec / 1000;
  3209. tstamp.ctime.tv_usec = ctime_nsec / 1000;
  3210. #endif
  3211. return (fcntl(fd,F_SETTIMES,&tstamp));
  3212. }
  3213. #endif /* F_SETTIMES */
  3214. static int
  3215. set_times(struct archive_write_disk *a,
  3216. int fd, int mode, const char *name,
  3217. time_t atime, long atime_nanos,
  3218. time_t birthtime, long birthtime_nanos,
  3219. time_t mtime, long mtime_nanos,
  3220. time_t cctime, long ctime_nanos)
  3221. {
  3222. /* Note: set_time doesn't use libarchive return conventions!
  3223. * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */
  3224. int r1 = 0, r2 = 0;
  3225. #ifdef F_SETTIMES
  3226. /*
  3227. * on Tru64 try own fcntl first which can restore even the
  3228. * ctime, fall back to default code path below if it fails
  3229. * or if we are not running as root
  3230. */
  3231. if (a->user_uid == 0 &&
  3232. set_time_tru64(fd, mode, name,
  3233. atime, atime_nanos, mtime,
  3234. mtime_nanos, cctime, ctime_nanos) == 0) {
  3235. return (ARCHIVE_OK);
  3236. }
  3237. #else /* Tru64 */
  3238. (void)cctime; /* UNUSED */
  3239. (void)ctime_nanos; /* UNUSED */
  3240. #endif /* Tru64 */
  3241. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  3242. /*
  3243. * If you have struct stat.st_birthtime, we assume BSD
  3244. * birthtime semantics, in which {f,l,}utimes() updates
  3245. * birthtime to earliest mtime. So we set the time twice,
  3246. * first using the birthtime, then using the mtime. If
  3247. * birthtime == mtime, this isn't necessary, so we skip it.
  3248. * If birthtime > mtime, then this won't work, so we skip it.
  3249. */
  3250. if (birthtime < mtime
  3251. || (birthtime == mtime && birthtime_nanos < mtime_nanos))
  3252. r1 = set_time(fd, mode, name,
  3253. atime, atime_nanos,
  3254. birthtime, birthtime_nanos);
  3255. #else
  3256. (void)birthtime; /* UNUSED */
  3257. (void)birthtime_nanos; /* UNUSED */
  3258. #endif
  3259. r2 = set_time(fd, mode, name,
  3260. atime, atime_nanos,
  3261. mtime, mtime_nanos);
  3262. if (r1 != 0 || r2 != 0) {
  3263. archive_set_error(&a->archive, errno,
  3264. "Can't restore time");
  3265. return (ARCHIVE_WARN);
  3266. }
  3267. return (ARCHIVE_OK);
  3268. }
  3269. static int
  3270. set_times_from_entry(struct archive_write_disk *a)
  3271. {
  3272. time_t atime, birthtime, mtime, cctime;
  3273. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  3274. /* Suitable defaults. */
  3275. atime = birthtime = mtime = cctime = a->start_time;
  3276. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  3277. /* If no time was provided, we're done. */
  3278. if (!archive_entry_atime_is_set(a->entry)
  3279. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  3280. && !archive_entry_birthtime_is_set(a->entry)
  3281. #endif
  3282. && !archive_entry_mtime_is_set(a->entry))
  3283. return (ARCHIVE_OK);
  3284. if (archive_entry_atime_is_set(a->entry)) {
  3285. atime = archive_entry_atime(a->entry);
  3286. atime_nsec = archive_entry_atime_nsec(a->entry);
  3287. }
  3288. if (archive_entry_birthtime_is_set(a->entry)) {
  3289. birthtime = archive_entry_birthtime(a->entry);
  3290. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  3291. }
  3292. if (archive_entry_mtime_is_set(a->entry)) {
  3293. mtime = archive_entry_mtime(a->entry);
  3294. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  3295. }
  3296. if (archive_entry_ctime_is_set(a->entry)) {
  3297. cctime = archive_entry_ctime(a->entry);
  3298. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  3299. }
  3300. return set_times(a, a->fd, a->mode, a->name,
  3301. atime, atime_nsec,
  3302. birthtime, birthtime_nsec,
  3303. mtime, mtime_nsec,
  3304. cctime, ctime_nsec);
  3305. }
  3306. static int
  3307. set_mode(struct archive_write_disk *a, int mode)
  3308. {
  3309. int r = ARCHIVE_OK;
  3310. int r2;
  3311. mode &= 07777; /* Strip off file type bits. */
  3312. if (a->todo & TODO_SGID_CHECK) {
  3313. /*
  3314. * If we don't know the GID is right, we must stat()
  3315. * to verify it. We can't just check the GID of this
  3316. * process, since systems sometimes set GID from
  3317. * the enclosing dir or based on ACLs.
  3318. */
  3319. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  3320. return (r);
  3321. if (a->pst->st_gid != a->gid) {
  3322. mode &= ~ S_ISGID;
  3323. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3324. /*
  3325. * This is only an error if you
  3326. * requested owner restore. If you
  3327. * didn't, we'll try to restore
  3328. * sgid/suid, but won't consider it a
  3329. * problem if we can't.
  3330. */
  3331. archive_set_error(&a->archive, -1,
  3332. "Can't restore SGID bit");
  3333. r = ARCHIVE_WARN;
  3334. }
  3335. }
  3336. /* While we're here, double-check the UID. */
  3337. if (a->pst->st_uid != a->uid
  3338. && (a->todo & TODO_SUID)) {
  3339. mode &= ~ S_ISUID;
  3340. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3341. archive_set_error(&a->archive, -1,
  3342. "Can't restore SUID bit");
  3343. r = ARCHIVE_WARN;
  3344. }
  3345. }
  3346. a->todo &= ~TODO_SGID_CHECK;
  3347. a->todo &= ~TODO_SUID_CHECK;
  3348. } else if (a->todo & TODO_SUID_CHECK) {
  3349. /*
  3350. * If we don't know the UID is right, we can just check
  3351. * the user, since all systems set the file UID from
  3352. * the process UID.
  3353. */
  3354. if (a->user_uid != a->uid) {
  3355. mode &= ~ S_ISUID;
  3356. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3357. archive_set_error(&a->archive, -1,
  3358. "Can't make file SUID");
  3359. r = ARCHIVE_WARN;
  3360. }
  3361. }
  3362. a->todo &= ~TODO_SUID_CHECK;
  3363. }
  3364. if (S_ISLNK(a->mode)) {
  3365. #ifdef HAVE_LCHMOD
  3366. /*
  3367. * If this is a symlink, use lchmod(). If the
  3368. * platform doesn't support lchmod(), just skip it. A
  3369. * platform that doesn't provide a way to set
  3370. * permissions on symlinks probably ignores
  3371. * permissions on symlinks, so a failure here has no
  3372. * impact.
  3373. */
  3374. if (lchmod(a->name, mode) != 0) {
  3375. switch (errno) {
  3376. case ENOTSUP:
  3377. case ENOSYS:
  3378. #if ENOTSUP != EOPNOTSUPP
  3379. case EOPNOTSUPP:
  3380. #endif
  3381. /*
  3382. * if lchmod is defined but the platform
  3383. * doesn't support it, silently ignore
  3384. * error
  3385. */
  3386. break;
  3387. default:
  3388. archive_set_error(&a->archive, errno,
  3389. "Can't set permissions to 0%o", (int)mode);
  3390. r = ARCHIVE_WARN;
  3391. }
  3392. }
  3393. #endif
  3394. } else if (!S_ISDIR(a->mode)) {
  3395. /*
  3396. * If it's not a symlink and not a dir, then use
  3397. * fchmod() or chmod(), depending on whether we have
  3398. * an fd. Dirs get their perms set during the
  3399. * post-extract fixup, which is handled elsewhere.
  3400. */
  3401. #ifdef HAVE_FCHMOD
  3402. if (a->fd >= 0)
  3403. r2 = fchmod(a->fd, mode);
  3404. else
  3405. #endif
  3406. /* If this platform lacks fchmod(), then
  3407. * we'll just use chmod(). */
  3408. r2 = chmod(a->name, mode);
  3409. if (r2 != 0) {
  3410. archive_set_error(&a->archive, errno,
  3411. "Can't set permissions to 0%o", (int)mode);
  3412. r = ARCHIVE_WARN;
  3413. }
  3414. }
  3415. return (r);
  3416. }
  3417. static int
  3418. set_fflags(struct archive_write_disk *a)
  3419. {
  3420. struct fixup_entry *le;
  3421. unsigned long set, clear;
  3422. int r;
  3423. mode_t mode = archive_entry_mode(a->entry);
  3424. /*
  3425. * Make 'critical_flags' hold all file flags that can't be
  3426. * immediately restored. For example, on BSD systems,
  3427. * SF_IMMUTABLE prevents hardlinks from being created, so
  3428. * should not be set until after any hardlinks are created. To
  3429. * preserve some semblance of portability, this uses #ifdef
  3430. * extensively. Ugly, but it works.
  3431. *
  3432. * Yes, Virginia, this does create a security race. It's mitigated
  3433. * somewhat by the practice of creating dirs 0700 until the extract
  3434. * is done, but it would be nice if we could do more than that.
  3435. * People restoring critical file systems should be wary of
  3436. * other programs that might try to muck with files as they're
  3437. * being restored.
  3438. */
  3439. const int critical_flags = 0
  3440. #ifdef SF_IMMUTABLE
  3441. | SF_IMMUTABLE
  3442. #endif
  3443. #ifdef UF_IMMUTABLE
  3444. | UF_IMMUTABLE
  3445. #endif
  3446. #ifdef SF_APPEND
  3447. | SF_APPEND
  3448. #endif
  3449. #ifdef UF_APPEND
  3450. | UF_APPEND
  3451. #endif
  3452. #if defined(FS_APPEND_FL)
  3453. | FS_APPEND_FL
  3454. #elif defined(EXT2_APPEND_FL)
  3455. | EXT2_APPEND_FL
  3456. #endif
  3457. #if defined(FS_IMMUTABLE_FL)
  3458. | FS_IMMUTABLE_FL
  3459. #elif defined(EXT2_IMMUTABLE_FL)
  3460. | EXT2_IMMUTABLE_FL
  3461. #endif
  3462. #ifdef FS_JOURNAL_DATA_FL
  3463. | FS_JOURNAL_DATA_FL
  3464. #endif
  3465. ;
  3466. if (a->todo & TODO_FFLAGS) {
  3467. archive_entry_fflags(a->entry, &set, &clear);
  3468. /*
  3469. * The first test encourages the compiler to eliminate
  3470. * all of this if it's not necessary.
  3471. */
  3472. if ((critical_flags != 0) && (set & critical_flags)) {
  3473. le = current_fixup(a, a->name);
  3474. if (le == NULL)
  3475. return (ARCHIVE_FATAL);
  3476. le->fixup |= TODO_FFLAGS;
  3477. le->fflags_set = set;
  3478. /* Store the mode if it's not already there. */
  3479. if ((le->fixup & TODO_MODE) == 0)
  3480. le->mode = mode;
  3481. } else {
  3482. r = set_fflags_platform(a, a->fd,
  3483. a->name, mode, set, clear);
  3484. if (r != ARCHIVE_OK)
  3485. return (r);
  3486. }
  3487. }
  3488. return (ARCHIVE_OK);
  3489. }
  3490. static int
  3491. clear_nochange_fflags(struct archive_write_disk *a)
  3492. {
  3493. mode_t mode = archive_entry_mode(a->entry);
  3494. const int nochange_flags = 0
  3495. #ifdef SF_IMMUTABLE
  3496. | SF_IMMUTABLE
  3497. #endif
  3498. #ifdef UF_IMMUTABLE
  3499. | UF_IMMUTABLE
  3500. #endif
  3501. #ifdef SF_APPEND
  3502. | SF_APPEND
  3503. #endif
  3504. #ifdef UF_APPEND
  3505. | UF_APPEND
  3506. #endif
  3507. #ifdef EXT2_APPEND_FL
  3508. | EXT2_APPEND_FL
  3509. #endif
  3510. #ifdef EXT2_IMMUTABLE_FL
  3511. | EXT2_IMMUTABLE_FL
  3512. #endif
  3513. ;
  3514. return (set_fflags_platform(a, a->fd, a->name, mode, 0,
  3515. nochange_flags));
  3516. }
  3517. #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
  3518. /*
  3519. * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
  3520. */
  3521. static int
  3522. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3523. mode_t mode, unsigned long set, unsigned long clear)
  3524. {
  3525. int r;
  3526. const int sf_mask = 0
  3527. #ifdef SF_APPEND
  3528. | SF_APPEND
  3529. #endif
  3530. #ifdef SF_ARCHIVED
  3531. | SF_ARCHIVED
  3532. #endif
  3533. #ifdef SF_IMMUTABLE
  3534. | SF_IMMUTABLE
  3535. #endif
  3536. #ifdef SF_NOUNLINK
  3537. | SF_NOUNLINK
  3538. #endif
  3539. ;
  3540. (void)mode; /* UNUSED */
  3541. if (set == 0 && clear == 0)
  3542. return (ARCHIVE_OK);
  3543. /*
  3544. * XXX Is the stat here really necessary? Or can I just use
  3545. * the 'set' flags directly? In particular, I'm not sure
  3546. * about the correct approach if we're overwriting an existing
  3547. * file that already has flags on it. XXX
  3548. */
  3549. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  3550. return (r);
  3551. a->st.st_flags &= ~clear;
  3552. a->st.st_flags |= set;
  3553. /* Only super-user may change SF_* flags */
  3554. if (a->user_uid != 0)
  3555. a->st.st_flags &= ~sf_mask;
  3556. #ifdef HAVE_FCHFLAGS
  3557. /* If platform has fchflags() and we were given an fd, use it. */
  3558. if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
  3559. return (ARCHIVE_OK);
  3560. #endif
  3561. /*
  3562. * If we can't use the fd to set the flags, we'll use the
  3563. * pathname to set flags. We prefer lchflags() but will use
  3564. * chflags() if we must.
  3565. */
  3566. #ifdef HAVE_LCHFLAGS
  3567. if (lchflags(name, a->st.st_flags) == 0)
  3568. return (ARCHIVE_OK);
  3569. #elif defined(HAVE_CHFLAGS)
  3570. if (S_ISLNK(a->st.st_mode)) {
  3571. archive_set_error(&a->archive, errno,
  3572. "Can't set file flags on symlink.");
  3573. return (ARCHIVE_WARN);
  3574. }
  3575. if (chflags(name, a->st.st_flags) == 0)
  3576. return (ARCHIVE_OK);
  3577. #endif
  3578. archive_set_error(&a->archive, errno,
  3579. "Failed to set file flags");
  3580. return (ARCHIVE_WARN);
  3581. }
  3582. #elif (defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) && \
  3583. defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
  3584. (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && \
  3585. defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
  3586. /*
  3587. * Linux uses ioctl() to read and write file flags.
  3588. */
  3589. static int
  3590. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3591. mode_t mode, unsigned long set, unsigned long clear)
  3592. {
  3593. int ret;
  3594. int myfd = fd;
  3595. int newflags, oldflags;
  3596. /*
  3597. * Linux has no define for the flags that are only settable by
  3598. * the root user. This code may seem a little complex, but
  3599. * there seem to be some Linux systems that lack these
  3600. * defines. (?) The code below degrades reasonably gracefully
  3601. * if sf_mask is incomplete.
  3602. */
  3603. const int sf_mask = 0
  3604. #if defined(FS_IMMUTABLE_FL)
  3605. | FS_IMMUTABLE_FL
  3606. #elif defined(EXT2_IMMUTABLE_FL)
  3607. | EXT2_IMMUTABLE_FL
  3608. #endif
  3609. #if defined(FS_APPEND_FL)
  3610. | FS_APPEND_FL
  3611. #elif defined(EXT2_APPEND_FL)
  3612. | EXT2_APPEND_FL
  3613. #endif
  3614. #if defined(FS_JOURNAL_DATA_FL)
  3615. | FS_JOURNAL_DATA_FL
  3616. #endif
  3617. ;
  3618. if (set == 0 && clear == 0)
  3619. return (ARCHIVE_OK);
  3620. /* Only regular files and dirs can have flags. */
  3621. if (!S_ISREG(mode) && !S_ISDIR(mode))
  3622. return (ARCHIVE_OK);
  3623. /* If we weren't given an fd, open it ourselves. */
  3624. if (myfd < 0) {
  3625. myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY | O_CLOEXEC);
  3626. __archive_ensure_cloexec_flag(myfd);
  3627. }
  3628. if (myfd < 0)
  3629. return (ARCHIVE_OK);
  3630. /*
  3631. * XXX As above, this would be way simpler if we didn't have
  3632. * to read the current flags from disk. XXX
  3633. */
  3634. ret = ARCHIVE_OK;
  3635. /* Read the current file flags. */
  3636. if (ioctl(myfd,
  3637. #ifdef FS_IOC_GETFLAGS
  3638. FS_IOC_GETFLAGS,
  3639. #else
  3640. EXT2_IOC_GETFLAGS,
  3641. #endif
  3642. &oldflags) < 0)
  3643. goto fail;
  3644. /* Try setting the flags as given. */
  3645. newflags = (oldflags & ~clear) | set;
  3646. if (ioctl(myfd,
  3647. #ifdef FS_IOC_SETFLAGS
  3648. FS_IOC_SETFLAGS,
  3649. #else
  3650. EXT2_IOC_SETFLAGS,
  3651. #endif
  3652. &newflags) >= 0)
  3653. goto cleanup;
  3654. if (errno != EPERM)
  3655. goto fail;
  3656. /* If we couldn't set all the flags, try again with a subset. */
  3657. newflags &= ~sf_mask;
  3658. oldflags &= sf_mask;
  3659. newflags |= oldflags;
  3660. if (ioctl(myfd,
  3661. #ifdef FS_IOC_SETFLAGS
  3662. FS_IOC_SETFLAGS,
  3663. #else
  3664. EXT2_IOC_SETFLAGS,
  3665. #endif
  3666. &newflags) >= 0)
  3667. goto cleanup;
  3668. /* We couldn't set the flags, so report the failure. */
  3669. fail:
  3670. archive_set_error(&a->archive, errno,
  3671. "Failed to set file flags");
  3672. ret = ARCHIVE_WARN;
  3673. cleanup:
  3674. if (fd < 0)
  3675. close(myfd);
  3676. return (ret);
  3677. }
  3678. #else
  3679. /*
  3680. * Of course, some systems have neither BSD chflags() nor Linux' flags
  3681. * support through ioctl().
  3682. */
  3683. static int
  3684. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3685. mode_t mode, unsigned long set, unsigned long clear)
  3686. {
  3687. (void)a; /* UNUSED */
  3688. (void)fd; /* UNUSED */
  3689. (void)name; /* UNUSED */
  3690. (void)mode; /* UNUSED */
  3691. (void)set; /* UNUSED */
  3692. (void)clear; /* UNUSED */
  3693. return (ARCHIVE_OK);
  3694. }
  3695. #endif /* __linux */
  3696. #ifndef HAVE_COPYFILE_H
  3697. /* Default is to simply drop Mac extended metadata. */
  3698. static int
  3699. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3700. const void *metadata, size_t metadata_size)
  3701. {
  3702. (void)a; /* UNUSED */
  3703. (void)pathname; /* UNUSED */
  3704. (void)metadata; /* UNUSED */
  3705. (void)metadata_size; /* UNUSED */
  3706. return (ARCHIVE_OK);
  3707. }
  3708. static int
  3709. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3710. {
  3711. (void)a; /* UNUSED */
  3712. (void)pathname; /* UNUSED */
  3713. return (ARCHIVE_OK);
  3714. }
  3715. #else
  3716. /*
  3717. * On Mac OS, we use copyfile() to unpack the metadata and
  3718. * apply it to the target file.
  3719. */
  3720. #if defined(HAVE_SYS_XATTR_H)
  3721. static int
  3722. copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd)
  3723. {
  3724. ssize_t xattr_size;
  3725. char *xattr_names = NULL, *xattr_val = NULL;
  3726. int ret = ARCHIVE_OK, xattr_i;
  3727. xattr_size = flistxattr(tmpfd, NULL, 0, 0);
  3728. if (xattr_size == -1) {
  3729. archive_set_error(&a->archive, errno,
  3730. "Failed to read metadata(xattr)");
  3731. ret = ARCHIVE_WARN;
  3732. goto exit_xattr;
  3733. }
  3734. xattr_names = malloc(xattr_size);
  3735. if (xattr_names == NULL) {
  3736. archive_set_error(&a->archive, ENOMEM,
  3737. "Can't allocate memory for metadata(xattr)");
  3738. ret = ARCHIVE_FATAL;
  3739. goto exit_xattr;
  3740. }
  3741. xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0);
  3742. if (xattr_size == -1) {
  3743. archive_set_error(&a->archive, errno,
  3744. "Failed to read metadata(xattr)");
  3745. ret = ARCHIVE_WARN;
  3746. goto exit_xattr;
  3747. }
  3748. for (xattr_i = 0; xattr_i < xattr_size;
  3749. xattr_i += strlen(xattr_names + xattr_i) + 1) {
  3750. char *xattr_val_saved;
  3751. ssize_t s;
  3752. int f;
  3753. s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0);
  3754. if (s == -1) {
  3755. archive_set_error(&a->archive, errno,
  3756. "Failed to get metadata(xattr)");
  3757. ret = ARCHIVE_WARN;
  3758. goto exit_xattr;
  3759. }
  3760. xattr_val_saved = xattr_val;
  3761. xattr_val = realloc(xattr_val, s);
  3762. if (xattr_val == NULL) {
  3763. archive_set_error(&a->archive, ENOMEM,
  3764. "Failed to get metadata(xattr)");
  3765. ret = ARCHIVE_WARN;
  3766. free(xattr_val_saved);
  3767. goto exit_xattr;
  3768. }
  3769. s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3770. if (s == -1) {
  3771. archive_set_error(&a->archive, errno,
  3772. "Failed to get metadata(xattr)");
  3773. ret = ARCHIVE_WARN;
  3774. goto exit_xattr;
  3775. }
  3776. f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3777. if (f == -1) {
  3778. archive_set_error(&a->archive, errno,
  3779. "Failed to get metadata(xattr)");
  3780. ret = ARCHIVE_WARN;
  3781. goto exit_xattr;
  3782. }
  3783. }
  3784. exit_xattr:
  3785. free(xattr_names);
  3786. free(xattr_val);
  3787. return (ret);
  3788. }
  3789. #endif
  3790. static int
  3791. copy_acls(struct archive_write_disk *a, int tmpfd, int dffd)
  3792. {
  3793. #ifndef HAVE_SYS_ACL_H
  3794. return 0;
  3795. #else
  3796. acl_t acl, dfacl = NULL;
  3797. int acl_r, ret = ARCHIVE_OK;
  3798. acl = acl_get_fd(tmpfd);
  3799. if (acl == NULL) {
  3800. if (errno == ENOENT)
  3801. /* There are not any ACLs. */
  3802. return (ret);
  3803. archive_set_error(&a->archive, errno,
  3804. "Failed to get metadata(acl)");
  3805. ret = ARCHIVE_WARN;
  3806. goto exit_acl;
  3807. }
  3808. dfacl = acl_dup(acl);
  3809. acl_r = acl_set_fd(dffd, dfacl);
  3810. if (acl_r == -1) {
  3811. archive_set_error(&a->archive, errno,
  3812. "Failed to get metadata(acl)");
  3813. ret = ARCHIVE_WARN;
  3814. goto exit_acl;
  3815. }
  3816. exit_acl:
  3817. if (acl)
  3818. acl_free(acl);
  3819. if (dfacl)
  3820. acl_free(dfacl);
  3821. return (ret);
  3822. #endif
  3823. }
  3824. static int
  3825. create_tempdatafork(struct archive_write_disk *a, const char *pathname)
  3826. {
  3827. struct archive_string tmpdatafork;
  3828. int tmpfd;
  3829. archive_string_init(&tmpdatafork);
  3830. archive_strcpy(&tmpdatafork, "tar.md.XXXXXX");
  3831. tmpfd = mkstemp(tmpdatafork.s);
  3832. if (tmpfd < 0) {
  3833. archive_set_error(&a->archive, errno,
  3834. "Failed to mkstemp");
  3835. archive_string_free(&tmpdatafork);
  3836. return (-1);
  3837. }
  3838. if (copyfile(pathname, tmpdatafork.s, 0,
  3839. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3840. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3841. archive_set_error(&a->archive, errno,
  3842. "Failed to restore metadata");
  3843. close(tmpfd);
  3844. tmpfd = -1;
  3845. }
  3846. unlink(tmpdatafork.s);
  3847. archive_string_free(&tmpdatafork);
  3848. return (tmpfd);
  3849. }
  3850. static int
  3851. copy_metadata(struct archive_write_disk *a, const char *metadata,
  3852. const char *datafork, int datafork_compressed)
  3853. {
  3854. int ret = ARCHIVE_OK;
  3855. if (datafork_compressed) {
  3856. int dffd, tmpfd;
  3857. tmpfd = create_tempdatafork(a, metadata);
  3858. if (tmpfd == -1)
  3859. return (ARCHIVE_WARN);
  3860. /*
  3861. * Do not open the data fork compressed by HFS+ compression
  3862. * with at least a writing mode(O_RDWR or O_WRONLY). it
  3863. * makes the data fork uncompressed.
  3864. */
  3865. dffd = open(datafork, 0);
  3866. if (dffd == -1) {
  3867. archive_set_error(&a->archive, errno,
  3868. "Failed to open the data fork for metadata");
  3869. close(tmpfd);
  3870. return (ARCHIVE_WARN);
  3871. }
  3872. #if defined(HAVE_SYS_XATTR_H)
  3873. ret = copy_xattrs(a, tmpfd, dffd);
  3874. if (ret == ARCHIVE_OK)
  3875. #endif
  3876. ret = copy_acls(a, tmpfd, dffd);
  3877. close(tmpfd);
  3878. close(dffd);
  3879. } else {
  3880. if (copyfile(metadata, datafork, 0,
  3881. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3882. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3883. archive_set_error(&a->archive, errno,
  3884. "Failed to restore metadata");
  3885. ret = ARCHIVE_WARN;
  3886. }
  3887. }
  3888. return (ret);
  3889. }
  3890. static int
  3891. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3892. const void *metadata, size_t metadata_size)
  3893. {
  3894. struct archive_string tmp;
  3895. ssize_t written;
  3896. int fd;
  3897. int ret = ARCHIVE_OK;
  3898. /* This would be simpler if copyfile() could just accept the
  3899. * metadata as a block of memory; then we could sidestep this
  3900. * silly dance of writing the data to disk just so that
  3901. * copyfile() can read it back in again. */
  3902. archive_string_init(&tmp);
  3903. archive_strcpy(&tmp, pathname);
  3904. archive_strcat(&tmp, ".XXXXXX");
  3905. fd = mkstemp(tmp.s);
  3906. if (fd < 0) {
  3907. archive_set_error(&a->archive, errno,
  3908. "Failed to restore metadata");
  3909. archive_string_free(&tmp);
  3910. return (ARCHIVE_WARN);
  3911. }
  3912. written = write(fd, metadata, metadata_size);
  3913. close(fd);
  3914. if ((size_t)written != metadata_size) {
  3915. archive_set_error(&a->archive, errno,
  3916. "Failed to restore metadata");
  3917. ret = ARCHIVE_WARN;
  3918. } else {
  3919. int compressed;
  3920. #if defined(UF_COMPRESSED)
  3921. if ((a->todo & TODO_HFS_COMPRESSION) != 0 &&
  3922. (ret = lazy_stat(a)) == ARCHIVE_OK)
  3923. compressed = a->st.st_flags & UF_COMPRESSED;
  3924. else
  3925. #endif
  3926. compressed = 0;
  3927. ret = copy_metadata(a, tmp.s, pathname, compressed);
  3928. }
  3929. unlink(tmp.s);
  3930. archive_string_free(&tmp);
  3931. return (ret);
  3932. }
  3933. static int
  3934. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3935. {
  3936. char buff[8];
  3937. struct stat st;
  3938. const char *p;
  3939. struct archive_string datafork;
  3940. int fd = -1, ret = ARCHIVE_OK;
  3941. archive_string_init(&datafork);
  3942. /* Check if the current file name is a type of the resource
  3943. * fork file. */
  3944. p = strrchr(pathname, '/');
  3945. if (p == NULL)
  3946. p = pathname;
  3947. else
  3948. p++;
  3949. if (p[0] != '.' || p[1] != '_')
  3950. goto skip_appledouble;
  3951. /*
  3952. * Check if the data fork file exists.
  3953. *
  3954. * TODO: Check if this write disk object has handled it.
  3955. */
  3956. archive_strncpy(&datafork, pathname, p - pathname);
  3957. archive_strcat(&datafork, p + 2);
  3958. if (lstat(datafork.s, &st) == -1 ||
  3959. (st.st_mode & AE_IFMT) != AE_IFREG)
  3960. goto skip_appledouble;
  3961. /*
  3962. * Check if the file is in the AppleDouble form.
  3963. */
  3964. fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC);
  3965. __archive_ensure_cloexec_flag(fd);
  3966. if (fd == -1) {
  3967. archive_set_error(&a->archive, errno,
  3968. "Failed to open a restoring file");
  3969. ret = ARCHIVE_WARN;
  3970. goto skip_appledouble;
  3971. }
  3972. if (read(fd, buff, 8) == -1) {
  3973. archive_set_error(&a->archive, errno,
  3974. "Failed to read a restoring file");
  3975. close(fd);
  3976. ret = ARCHIVE_WARN;
  3977. goto skip_appledouble;
  3978. }
  3979. close(fd);
  3980. /* Check AppleDouble Magic Code. */
  3981. if (archive_be32dec(buff) != 0x00051607)
  3982. goto skip_appledouble;
  3983. /* Check AppleDouble Version. */
  3984. if (archive_be32dec(buff+4) != 0x00020000)
  3985. goto skip_appledouble;
  3986. ret = copy_metadata(a, pathname, datafork.s,
  3987. #if defined(UF_COMPRESSED)
  3988. st.st_flags & UF_COMPRESSED);
  3989. #else
  3990. 0);
  3991. #endif
  3992. if (ret == ARCHIVE_OK) {
  3993. unlink(pathname);
  3994. ret = ARCHIVE_EOF;
  3995. }
  3996. skip_appledouble:
  3997. archive_string_free(&datafork);
  3998. return (ret);
  3999. }
  4000. #endif
  4001. #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
  4002. /*
  4003. * Restore extended attributes - Linux, Darwin and AIX implementations:
  4004. * AIX' ea interface is syntaxwise identical to the Linux xattr interface.
  4005. */
  4006. static int
  4007. set_xattrs(struct archive_write_disk *a)
  4008. {
  4009. struct archive_entry *entry = a->entry;
  4010. struct archive_string errlist;
  4011. int ret = ARCHIVE_OK;
  4012. int i = archive_entry_xattr_reset(entry);
  4013. short fail = 0;
  4014. archive_string_init(&errlist);
  4015. while (i--) {
  4016. const char *name;
  4017. const void *value;
  4018. size_t size;
  4019. int e;
  4020. archive_entry_xattr_next(entry, &name, &value, &size);
  4021. if (name == NULL)
  4022. continue;
  4023. #if ARCHIVE_XATTR_LINUX
  4024. /* Linux: quietly skip POSIX.1e ACL extended attributes */
  4025. if (strncmp(name, "system.", 7) == 0 &&
  4026. (strcmp(name + 7, "posix_acl_access") == 0 ||
  4027. strcmp(name + 7, "posix_acl_default") == 0))
  4028. continue;
  4029. if (strncmp(name, "trusted.SGI_", 12) == 0 &&
  4030. (strcmp(name + 12, "ACL_DEFAULT") == 0 ||
  4031. strcmp(name + 12, "ACL_FILE") == 0))
  4032. continue;
  4033. /* Linux: xfsroot namespace is obsolete and unsupported */
  4034. if (strncmp(name, "xfsroot.", 8) == 0) {
  4035. fail = 1;
  4036. archive_strcat(&errlist, name);
  4037. archive_strappend_char(&errlist, ' ');
  4038. continue;
  4039. }
  4040. #endif
  4041. if (a->fd >= 0) {
  4042. #if ARCHIVE_XATTR_LINUX
  4043. e = fsetxattr(a->fd, name, value, size, 0);
  4044. #elif ARCHIVE_XATTR_DARWIN
  4045. e = fsetxattr(a->fd, name, value, size, 0, 0);
  4046. #elif ARCHIVE_XATTR_AIX
  4047. e = fsetea(a->fd, name, value, size, 0);
  4048. #endif
  4049. } else {
  4050. #if ARCHIVE_XATTR_LINUX
  4051. e = lsetxattr(archive_entry_pathname(entry),
  4052. name, value, size, 0);
  4053. #elif ARCHIVE_XATTR_DARWIN
  4054. e = setxattr(archive_entry_pathname(entry),
  4055. name, value, size, 0, XATTR_NOFOLLOW);
  4056. #elif ARCHIVE_XATTR_AIX
  4057. e = lsetea(archive_entry_pathname(entry),
  4058. name, value, size, 0);
  4059. #endif
  4060. }
  4061. if (e == -1) {
  4062. ret = ARCHIVE_WARN;
  4063. archive_strcat(&errlist, name);
  4064. archive_strappend_char(&errlist, ' ');
  4065. if (errno != ENOTSUP && errno != ENOSYS)
  4066. fail = 1;
  4067. }
  4068. }
  4069. if (ret == ARCHIVE_WARN) {
  4070. if (fail && errlist.length > 0) {
  4071. errlist.length--;
  4072. errlist.s[errlist.length] = '\0';
  4073. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  4074. "Cannot restore extended attributes: %s",
  4075. errlist.s);
  4076. } else
  4077. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  4078. "Cannot restore extended "
  4079. "attributes on this file system.");
  4080. }
  4081. archive_string_free(&errlist);
  4082. return (ret);
  4083. }
  4084. #elif ARCHIVE_XATTR_FREEBSD
  4085. /*
  4086. * Restore extended attributes - FreeBSD implementation
  4087. */
  4088. static int
  4089. set_xattrs(struct archive_write_disk *a)
  4090. {
  4091. struct archive_entry *entry = a->entry;
  4092. struct archive_string errlist;
  4093. int ret = ARCHIVE_OK;
  4094. int i = archive_entry_xattr_reset(entry);
  4095. short fail = 0;
  4096. archive_string_init(&errlist);
  4097. while (i--) {
  4098. const char *name;
  4099. const void *value;
  4100. size_t size;
  4101. archive_entry_xattr_next(entry, &name, &value, &size);
  4102. if (name != NULL) {
  4103. int e;
  4104. int namespace;
  4105. if (strncmp(name, "user.", 5) == 0) {
  4106. /* "user." attributes go to user namespace */
  4107. name += 5;
  4108. namespace = EXTATTR_NAMESPACE_USER;
  4109. } else {
  4110. /* Other namespaces are unsupported */
  4111. archive_strcat(&errlist, name);
  4112. archive_strappend_char(&errlist, ' ');
  4113. fail = 1;
  4114. ret = ARCHIVE_WARN;
  4115. continue;
  4116. }
  4117. if (a->fd >= 0) {
  4118. e = extattr_set_fd(a->fd, namespace, name,
  4119. value, size);
  4120. } else {
  4121. e = extattr_set_link(
  4122. archive_entry_pathname(entry), namespace,
  4123. name, value, size);
  4124. }
  4125. if (e != (int)size) {
  4126. archive_strcat(&errlist, name);
  4127. archive_strappend_char(&errlist, ' ');
  4128. ret = ARCHIVE_WARN;
  4129. if (errno != ENOTSUP && errno != ENOSYS)
  4130. fail = 1;
  4131. }
  4132. }
  4133. }
  4134. if (ret == ARCHIVE_WARN) {
  4135. if (fail && errlist.length > 0) {
  4136. errlist.length--;
  4137. errlist.s[errlist.length] = '\0';
  4138. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  4139. "Cannot restore extended attributes: %s",
  4140. errlist.s);
  4141. } else
  4142. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  4143. "Cannot restore extended "
  4144. "attributes on this file system.");
  4145. }
  4146. archive_string_free(&errlist);
  4147. return (ret);
  4148. }
  4149. #else
  4150. /*
  4151. * Restore extended attributes - stub implementation for unsupported systems
  4152. */
  4153. static int
  4154. set_xattrs(struct archive_write_disk *a)
  4155. {
  4156. static int warning_done = 0;
  4157. /* If there aren't any extended attributes, then it's okay not
  4158. * to extract them, otherwise, issue a single warning. */
  4159. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  4160. warning_done = 1;
  4161. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  4162. "Cannot restore extended attributes on this system");
  4163. return (ARCHIVE_WARN);
  4164. }
  4165. /* Warning was already emitted; suppress further warnings. */
  4166. return (ARCHIVE_OK);
  4167. }
  4168. #endif
  4169. /*
  4170. * Test if file on disk is older than entry.
  4171. */
  4172. static int
  4173. older(struct stat *st, struct archive_entry *entry)
  4174. {
  4175. /* First, test the seconds and return if we have a definite answer. */
  4176. /* Definitely older. */
  4177. if (to_int64_time(st->st_mtime) < to_int64_time(archive_entry_mtime(entry)))
  4178. return (1);
  4179. /* Definitely younger. */
  4180. if (to_int64_time(st->st_mtime) > to_int64_time(archive_entry_mtime(entry)))
  4181. return (0);
  4182. /* If this platform supports fractional seconds, try those. */
  4183. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  4184. /* Definitely older. */
  4185. if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
  4186. return (1);
  4187. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  4188. /* Definitely older. */
  4189. if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
  4190. return (1);
  4191. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  4192. /* older. */
  4193. if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
  4194. return (1);
  4195. #elif HAVE_STRUCT_STAT_ST_UMTIME
  4196. /* older. */
  4197. if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
  4198. return (1);
  4199. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  4200. /* older. */
  4201. if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
  4202. return (1);
  4203. #else
  4204. /* This system doesn't have high-res timestamps. */
  4205. #endif
  4206. /* Same age or newer, so not older. */
  4207. return (0);
  4208. }
  4209. #ifndef ARCHIVE_ACL_SUPPORT
  4210. int
  4211. archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
  4212. struct archive_acl *abstract_acl, __LA_MODE_T mode)
  4213. {
  4214. (void)a; /* UNUSED */
  4215. (void)fd; /* UNUSED */
  4216. (void)name; /* UNUSED */
  4217. (void)abstract_acl; /* UNUSED */
  4218. (void)mode; /* UNUSED */
  4219. return (ARCHIVE_OK);
  4220. }
  4221. #endif
  4222. #endif /* !_WIN32 || __CYGWIN__ */