archive_write_disk_posix.c 118 KB

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