archive_write_disk_posix.c 127 KB

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