archive_write_disk_posix.c 128 KB

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