archive_write_disk_posix.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327
  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, int64_t d, 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. int64_t (*lookup_gid)(void *private, const char *gname, 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, 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, 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. archive_entry_unset_size(a->entry);
  1830. return (ARCHIVE_OK);
  1831. }
  1832. /*
  1833. * Some platforms return EISDIR if you call
  1834. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  1835. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  1836. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  1837. * on an existing item.
  1838. */
  1839. if (en == EISDIR) {
  1840. /* A dir is in the way of a non-dir, rmdir it. */
  1841. if (rmdir(a->name) != 0) {
  1842. archive_set_error(&a->archive, errno,
  1843. "Can't remove already-existing dir");
  1844. return (ARCHIVE_FAILED);
  1845. }
  1846. a->pst = NULL;
  1847. /* Try again. */
  1848. en = create_filesystem_object(a);
  1849. } else if (en == EEXIST) {
  1850. /*
  1851. * We know something is in the way, but we don't know what;
  1852. * we need to find out before we go any further.
  1853. */
  1854. int r = 0;
  1855. /*
  1856. * The SECURE_SYMLINKS logic has already removed a
  1857. * symlink to a dir if the client wants that. So
  1858. * follow the symlink if we're creating a dir.
  1859. */
  1860. if (S_ISDIR(a->mode))
  1861. r = stat(a->name, &a->st);
  1862. /*
  1863. * If it's not a dir (or it's a broken symlink),
  1864. * then don't follow it.
  1865. */
  1866. if (r != 0 || !S_ISDIR(a->mode))
  1867. r = lstat(a->name, &a->st);
  1868. if (r != 0) {
  1869. archive_set_error(&a->archive, errno,
  1870. "Can't stat existing object");
  1871. return (ARCHIVE_FAILED);
  1872. }
  1873. /*
  1874. * NO_OVERWRITE_NEWER doesn't apply to directories.
  1875. */
  1876. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  1877. && !S_ISDIR(a->st.st_mode)) {
  1878. if (!older(&(a->st), a->entry)) {
  1879. archive_entry_unset_size(a->entry);
  1880. return (ARCHIVE_OK);
  1881. }
  1882. }
  1883. /* If it's our archive, we're done. */
  1884. if (a->skip_file_set &&
  1885. a->st.st_dev == (dev_t)a->skip_file_dev &&
  1886. a->st.st_ino == (ino_t)a->skip_file_ino) {
  1887. archive_set_error(&a->archive, 0,
  1888. "Refusing to overwrite archive");
  1889. return (ARCHIVE_FAILED);
  1890. }
  1891. if (!S_ISDIR(a->st.st_mode)) {
  1892. /* A non-dir is in the way, unlink it. */
  1893. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1894. (void)clear_nochange_fflags(a);
  1895. if (unlink(a->name) != 0) {
  1896. archive_set_error(&a->archive, errno,
  1897. "Can't unlink already-existing object");
  1898. return (ARCHIVE_FAILED);
  1899. }
  1900. a->pst = NULL;
  1901. /* Try again. */
  1902. en = create_filesystem_object(a);
  1903. } else if (!S_ISDIR(a->mode)) {
  1904. /* A dir is in the way of a non-dir, rmdir it. */
  1905. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1906. (void)clear_nochange_fflags(a);
  1907. if (rmdir(a->name) != 0) {
  1908. archive_set_error(&a->archive, errno,
  1909. "Can't replace existing directory with non-directory");
  1910. return (ARCHIVE_FAILED);
  1911. }
  1912. /* Try again. */
  1913. en = create_filesystem_object(a);
  1914. } else {
  1915. /*
  1916. * There's a dir in the way of a dir. Don't
  1917. * waste time with rmdir()/mkdir(), just fix
  1918. * up the permissions on the existing dir.
  1919. * Note that we don't change perms on existing
  1920. * dirs unless _EXTRACT_PERM is specified.
  1921. */
  1922. if ((a->mode != a->st.st_mode)
  1923. && (a->todo & TODO_MODE_FORCE))
  1924. a->deferred |= (a->todo & TODO_MODE);
  1925. /* Ownership doesn't need deferred fixup. */
  1926. en = 0; /* Forget the EEXIST. */
  1927. }
  1928. }
  1929. if (en) {
  1930. /* Everything failed; give up here. */
  1931. if ((&a->archive)->error == NULL)
  1932. archive_set_error(&a->archive, en, "Can't create '%s'",
  1933. a->name);
  1934. return (ARCHIVE_FAILED);
  1935. }
  1936. a->pst = NULL; /* Cached stat data no longer valid. */
  1937. return (ret);
  1938. }
  1939. /*
  1940. * Returns 0 if creation succeeds, or else returns errno value from
  1941. * the failed system call. Note: This function should only ever perform
  1942. * a single system call.
  1943. */
  1944. static int
  1945. create_filesystem_object(struct archive_write_disk *a)
  1946. {
  1947. /* Create the entry. */
  1948. const char *linkname;
  1949. mode_t final_mode, mode;
  1950. int r;
  1951. /* these for check_symlinks_fsobj */
  1952. char *linkname_copy; /* non-const copy of linkname */
  1953. struct stat st;
  1954. struct archive_string error_string;
  1955. int error_number;
  1956. /* We identify hard/symlinks according to the link names. */
  1957. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  1958. linkname = archive_entry_hardlink(a->entry);
  1959. if (linkname != NULL) {
  1960. #if !HAVE_LINK
  1961. return (EPERM);
  1962. #else
  1963. archive_string_init(&error_string);
  1964. linkname_copy = strdup(linkname);
  1965. if (linkname_copy == NULL) {
  1966. return (EPERM);
  1967. }
  1968. /*
  1969. * TODO: consider using the cleaned-up path as the link
  1970. * target?
  1971. */
  1972. r = cleanup_pathname_fsobj(linkname_copy, &error_number,
  1973. &error_string, a->flags);
  1974. if (r != ARCHIVE_OK) {
  1975. archive_set_error(&a->archive, error_number, "%s",
  1976. error_string.s);
  1977. free(linkname_copy);
  1978. archive_string_free(&error_string);
  1979. /*
  1980. * EPERM is more appropriate than error_number for our
  1981. * callers
  1982. */
  1983. return (EPERM);
  1984. }
  1985. r = check_symlinks_fsobj(linkname_copy, &error_number,
  1986. &error_string, a->flags);
  1987. if (r != ARCHIVE_OK) {
  1988. archive_set_error(&a->archive, error_number, "%s",
  1989. error_string.s);
  1990. free(linkname_copy);
  1991. archive_string_free(&error_string);
  1992. /*
  1993. * EPERM is more appropriate than error_number for our
  1994. * callers
  1995. */
  1996. return (EPERM);
  1997. }
  1998. free(linkname_copy);
  1999. archive_string_free(&error_string);
  2000. r = link(linkname, a->name) ? errno : 0;
  2001. /*
  2002. * New cpio and pax formats allow hardlink entries
  2003. * to carry data, so we may have to open the file
  2004. * for hardlink entries.
  2005. *
  2006. * If the hardlink was successfully created and
  2007. * the archive doesn't have carry data for it,
  2008. * consider it to be non-authoritative for meta data.
  2009. * This is consistent with GNU tar and BSD pax.
  2010. * If the hardlink does carry data, let the last
  2011. * archive entry decide ownership.
  2012. */
  2013. if (r == 0 && a->filesize <= 0) {
  2014. a->todo = 0;
  2015. a->deferred = 0;
  2016. } else if (r == 0 && a->filesize > 0) {
  2017. #ifdef HAVE_LSTAT
  2018. r = lstat(a->name, &st);
  2019. #else
  2020. r = stat(a->name, &st);
  2021. #endif
  2022. if (r != 0)
  2023. r = errno;
  2024. else if ((st.st_mode & AE_IFMT) == AE_IFREG) {
  2025. a->fd = open(a->name, O_WRONLY | O_TRUNC |
  2026. O_BINARY | O_CLOEXEC | O_NOFOLLOW);
  2027. __archive_ensure_cloexec_flag(a->fd);
  2028. if (a->fd < 0)
  2029. r = errno;
  2030. }
  2031. }
  2032. return (r);
  2033. #endif
  2034. }
  2035. linkname = archive_entry_symlink(a->entry);
  2036. if (linkname != NULL) {
  2037. #if HAVE_SYMLINK
  2038. return symlink(linkname, a->name) ? errno : 0;
  2039. #else
  2040. return (EPERM);
  2041. #endif
  2042. }
  2043. /*
  2044. * The remaining system calls all set permissions, so let's
  2045. * try to take advantage of that to avoid an extra chmod()
  2046. * call. (Recall that umask is set to zero right now!)
  2047. */
  2048. /* Mode we want for the final restored object (w/o file type bits). */
  2049. final_mode = a->mode & 07777;
  2050. /*
  2051. * The mode that will actually be restored in this step. Note
  2052. * that SUID, SGID, etc, require additional work to ensure
  2053. * security, so we never restore them at this point.
  2054. */
  2055. mode = final_mode & 0777 & ~a->user_umask;
  2056. switch (a->mode & AE_IFMT) {
  2057. default:
  2058. /* POSIX requires that we fall through here. */
  2059. /* FALLTHROUGH */
  2060. case AE_IFREG:
  2061. a->fd = open(a->name,
  2062. O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, mode);
  2063. __archive_ensure_cloexec_flag(a->fd);
  2064. r = (a->fd < 0);
  2065. break;
  2066. case AE_IFCHR:
  2067. #ifdef HAVE_MKNOD
  2068. /* Note: we use AE_IFCHR for the case label, and
  2069. * S_IFCHR for the mknod() call. This is correct. */
  2070. r = mknod(a->name, mode | S_IFCHR,
  2071. archive_entry_rdev(a->entry));
  2072. break;
  2073. #else
  2074. /* TODO: Find a better way to warn about our inability
  2075. * to restore a char device node. */
  2076. return (EINVAL);
  2077. #endif /* HAVE_MKNOD */
  2078. case AE_IFBLK:
  2079. #ifdef HAVE_MKNOD
  2080. r = mknod(a->name, mode | S_IFBLK,
  2081. archive_entry_rdev(a->entry));
  2082. break;
  2083. #else
  2084. /* TODO: Find a better way to warn about our inability
  2085. * to restore a block device node. */
  2086. return (EINVAL);
  2087. #endif /* HAVE_MKNOD */
  2088. case AE_IFDIR:
  2089. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  2090. r = mkdir(a->name, mode);
  2091. if (r == 0) {
  2092. /* Defer setting dir times. */
  2093. a->deferred |= (a->todo & TODO_TIMES);
  2094. a->todo &= ~TODO_TIMES;
  2095. /* Never use an immediate chmod(). */
  2096. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  2097. * because of SysV SGID inheritance. */
  2098. if ((mode != final_mode)
  2099. || (a->flags & ARCHIVE_EXTRACT_PERM))
  2100. a->deferred |= (a->todo & TODO_MODE);
  2101. a->todo &= ~TODO_MODE;
  2102. }
  2103. break;
  2104. case AE_IFIFO:
  2105. #ifdef HAVE_MKFIFO
  2106. r = mkfifo(a->name, mode);
  2107. break;
  2108. #else
  2109. /* TODO: Find a better way to warn about our inability
  2110. * to restore a fifo. */
  2111. return (EINVAL);
  2112. #endif /* HAVE_MKFIFO */
  2113. }
  2114. /* All the system calls above set errno on failure. */
  2115. if (r)
  2116. return (errno);
  2117. /* If we managed to set the final mode, we've avoided a chmod(). */
  2118. if (mode == final_mode)
  2119. a->todo &= ~TODO_MODE;
  2120. return (0);
  2121. }
  2122. /*
  2123. * Cleanup function for archive_extract. Mostly, this involves processing
  2124. * the fixup list, which is used to address a number of problems:
  2125. * * Dir permissions might prevent us from restoring a file in that
  2126. * dir, so we restore the dir with minimum 0700 permissions first,
  2127. * then correct the mode at the end.
  2128. * * Similarly, the act of restoring a file touches the directory
  2129. * and changes the timestamp on the dir, so we have to touch-up dir
  2130. * timestamps at the end as well.
  2131. * * Some file flags can interfere with the restore by, for example,
  2132. * preventing the creation of hardlinks to those files.
  2133. * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
  2134. *
  2135. * Note that tar/cpio do not require that archives be in a particular
  2136. * order; there is no way to know when the last file has been restored
  2137. * within a directory, so there's no way to optimize the memory usage
  2138. * here by fixing up the directory any earlier than the
  2139. * end-of-archive.
  2140. *
  2141. * XXX TODO: Directory ACLs should be restored here, for the same
  2142. * reason we set directory perms here. XXX
  2143. */
  2144. static int
  2145. _archive_write_disk_close(struct archive *_a)
  2146. {
  2147. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  2148. struct fixup_entry *next, *p;
  2149. int ret;
  2150. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  2151. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  2152. "archive_write_disk_close");
  2153. ret = _archive_write_disk_finish_entry(&a->archive);
  2154. /* Sort dir list so directories are fixed up in depth-first order. */
  2155. p = sort_dir_list(a->fixup_list);
  2156. while (p != NULL) {
  2157. a->pst = NULL; /* Mark stat cache as out-of-date. */
  2158. if (p->fixup & TODO_TIMES) {
  2159. set_times(a, -1, p->mode, p->name,
  2160. p->atime, p->atime_nanos,
  2161. p->birthtime, p->birthtime_nanos,
  2162. p->mtime, p->mtime_nanos,
  2163. p->ctime, p->ctime_nanos);
  2164. }
  2165. if (p->fixup & TODO_MODE_BASE)
  2166. chmod(p->name, p->mode);
  2167. if (p->fixup & TODO_ACLS)
  2168. archive_write_disk_set_acls(&a->archive, -1, p->name,
  2169. &p->acl, p->mode);
  2170. if (p->fixup & TODO_FFLAGS)
  2171. set_fflags_platform(a, -1, p->name,
  2172. p->mode, p->fflags_set, 0);
  2173. if (p->fixup & TODO_MAC_METADATA)
  2174. set_mac_metadata(a, p->name, p->mac_metadata,
  2175. p->mac_metadata_size);
  2176. next = p->next;
  2177. archive_acl_clear(&p->acl);
  2178. free(p->mac_metadata);
  2179. free(p->name);
  2180. free(p);
  2181. p = next;
  2182. }
  2183. a->fixup_list = NULL;
  2184. return (ret);
  2185. }
  2186. static int
  2187. _archive_write_disk_free(struct archive *_a)
  2188. {
  2189. struct archive_write_disk *a;
  2190. int ret;
  2191. if (_a == NULL)
  2192. return (ARCHIVE_OK);
  2193. archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
  2194. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
  2195. a = (struct archive_write_disk *)_a;
  2196. ret = _archive_write_disk_close(&a->archive);
  2197. archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
  2198. archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
  2199. if (a->entry)
  2200. archive_entry_free(a->entry);
  2201. archive_string_free(&a->_name_data);
  2202. archive_string_free(&a->archive.error_string);
  2203. archive_string_free(&a->path_safe);
  2204. a->archive.magic = 0;
  2205. __archive_clean(&a->archive);
  2206. free(a->decmpfs_header_p);
  2207. free(a->resource_fork);
  2208. free(a->compressed_buffer);
  2209. free(a->uncompressed_buffer);
  2210. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
  2211. && defined(HAVE_ZLIB_H)
  2212. if (a->stream_valid) {
  2213. switch (deflateEnd(&a->stream)) {
  2214. case Z_OK:
  2215. break;
  2216. default:
  2217. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2218. "Failed to clean up compressor");
  2219. ret = ARCHIVE_FATAL;
  2220. break;
  2221. }
  2222. }
  2223. #endif
  2224. free(a);
  2225. return (ret);
  2226. }
  2227. /*
  2228. * Simple O(n log n) merge sort to order the fixup list. In
  2229. * particular, we want to restore dir timestamps depth-first.
  2230. */
  2231. static struct fixup_entry *
  2232. sort_dir_list(struct fixup_entry *p)
  2233. {
  2234. struct fixup_entry *a, *b, *t;
  2235. if (p == NULL)
  2236. return (NULL);
  2237. /* A one-item list is already sorted. */
  2238. if (p->next == NULL)
  2239. return (p);
  2240. /* Step 1: split the list. */
  2241. t = p;
  2242. a = p->next->next;
  2243. while (a != NULL) {
  2244. /* Step a twice, t once. */
  2245. a = a->next;
  2246. if (a != NULL)
  2247. a = a->next;
  2248. t = t->next;
  2249. }
  2250. /* Now, t is at the mid-point, so break the list here. */
  2251. b = t->next;
  2252. t->next = NULL;
  2253. a = p;
  2254. /* Step 2: Recursively sort the two sub-lists. */
  2255. a = sort_dir_list(a);
  2256. b = sort_dir_list(b);
  2257. /* Step 3: Merge the returned lists. */
  2258. /* Pick the first element for the merged list. */
  2259. if (strcmp(a->name, b->name) > 0) {
  2260. t = p = a;
  2261. a = a->next;
  2262. } else {
  2263. t = p = b;
  2264. b = b->next;
  2265. }
  2266. /* Always put the later element on the list first. */
  2267. while (a != NULL && b != NULL) {
  2268. if (strcmp(a->name, b->name) > 0) {
  2269. t->next = a;
  2270. a = a->next;
  2271. } else {
  2272. t->next = b;
  2273. b = b->next;
  2274. }
  2275. t = t->next;
  2276. }
  2277. /* Only one list is non-empty, so just splice it on. */
  2278. if (a != NULL)
  2279. t->next = a;
  2280. if (b != NULL)
  2281. t->next = b;
  2282. return (p);
  2283. }
  2284. /*
  2285. * Returns a new, initialized fixup entry.
  2286. *
  2287. * TODO: Reduce the memory requirements for this list by using a tree
  2288. * structure rather than a simple list of names.
  2289. */
  2290. static struct fixup_entry *
  2291. new_fixup(struct archive_write_disk *a, const char *pathname)
  2292. {
  2293. struct fixup_entry *fe;
  2294. fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
  2295. if (fe == NULL) {
  2296. archive_set_error(&a->archive, ENOMEM,
  2297. "Can't allocate memory for a fixup");
  2298. return (NULL);
  2299. }
  2300. fe->next = a->fixup_list;
  2301. a->fixup_list = fe;
  2302. fe->fixup = 0;
  2303. fe->name = strdup(pathname);
  2304. return (fe);
  2305. }
  2306. /*
  2307. * Returns a fixup structure for the current entry.
  2308. */
  2309. static struct fixup_entry *
  2310. current_fixup(struct archive_write_disk *a, const char *pathname)
  2311. {
  2312. if (a->current_fixup == NULL)
  2313. a->current_fixup = new_fixup(a, pathname);
  2314. return (a->current_fixup);
  2315. }
  2316. /* Error helper for new *_fsobj functions */
  2317. static void
  2318. fsobj_error(int *a_eno, struct archive_string *a_estr,
  2319. int err, const char *errstr, const char *path)
  2320. {
  2321. if (a_eno)
  2322. *a_eno = err;
  2323. if (a_estr)
  2324. archive_string_sprintf(a_estr, "%s%s", errstr, path);
  2325. }
  2326. /*
  2327. * TODO: Someday, integrate this with the deep dir support; they both
  2328. * scan the path and both can be optimized by comparing against other
  2329. * recent paths.
  2330. */
  2331. /* TODO: Extend this to support symlinks on Windows Vista and later. */
  2332. /*
  2333. * Checks the given path to see if any elements along it are symlinks. Returns
  2334. * ARCHIVE_OK if there are none, otherwise puts an error in errmsg.
  2335. */
  2336. static int
  2337. check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
  2338. int flags)
  2339. {
  2340. #if !defined(HAVE_LSTAT)
  2341. /* Platform doesn't have lstat, so we can't look for symlinks. */
  2342. (void)path; /* UNUSED */
  2343. (void)error_number; /* UNUSED */
  2344. (void)error_string; /* UNUSED */
  2345. (void)flags; /* UNUSED */
  2346. return (ARCHIVE_OK);
  2347. #else
  2348. int res = ARCHIVE_OK;
  2349. char *tail;
  2350. char *head;
  2351. int last;
  2352. char c;
  2353. int r;
  2354. struct stat st;
  2355. int restore_pwd;
  2356. /* Nothing to do here if name is empty */
  2357. if(path[0] == '\0')
  2358. return (ARCHIVE_OK);
  2359. /*
  2360. * Guard against symlink tricks. Reject any archive entry whose
  2361. * destination would be altered by a symlink.
  2362. *
  2363. * Walk the filename in chunks separated by '/'. For each segment:
  2364. * - if it doesn't exist, continue
  2365. * - if it's symlink, abort or remove it
  2366. * - if it's a directory and it's not the last chunk, cd into it
  2367. * As we go:
  2368. * head points to the current (relative) path
  2369. * tail points to the temporary \0 terminating the segment we're
  2370. * currently examining
  2371. * c holds what used to be in *tail
  2372. * last is 1 if this is the last tail
  2373. */
  2374. restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC);
  2375. __archive_ensure_cloexec_flag(restore_pwd);
  2376. if (restore_pwd < 0)
  2377. return (ARCHIVE_FATAL);
  2378. head = path;
  2379. tail = path;
  2380. last = 0;
  2381. /* TODO: reintroduce a safe cache here? */
  2382. /* Skip the root directory if the path is absolute. */
  2383. if(tail == path && tail[0] == '/')
  2384. ++tail;
  2385. /* Keep going until we've checked the entire name.
  2386. * head, tail, path all alias the same string, which is
  2387. * temporarily zeroed at tail, so be careful restoring the
  2388. * stashed (c=tail[0]) for error messages.
  2389. * Exiting the loop with break is okay; continue is not.
  2390. */
  2391. while (!last) {
  2392. /*
  2393. * Skip the separator we just consumed, plus any adjacent ones
  2394. */
  2395. while (*tail == '/')
  2396. ++tail;
  2397. /* Skip the next path element. */
  2398. while (*tail != '\0' && *tail != '/')
  2399. ++tail;
  2400. /* is this the last path component? */
  2401. last = (tail[0] == '\0') || (tail[0] == '/' && tail[1] == '\0');
  2402. /* temporarily truncate the string here */
  2403. c = tail[0];
  2404. tail[0] = '\0';
  2405. /* Check that we haven't hit a symlink. */
  2406. r = lstat(head, &st);
  2407. if (r != 0) {
  2408. tail[0] = c;
  2409. /* We've hit a dir that doesn't exist; stop now. */
  2410. if (errno == ENOENT) {
  2411. break;
  2412. } else {
  2413. /*
  2414. * Treat any other error as fatal - best to be
  2415. * paranoid here.
  2416. * Note: This effectively disables deep
  2417. * directory support when security checks are
  2418. * enabled. Otherwise, very long pathnames that
  2419. * trigger an error here could evade the
  2420. * sandbox.
  2421. * TODO: We could do better, but it would
  2422. * probably require merging the symlink checks
  2423. * with the deep-directory editing.
  2424. */
  2425. fsobj_error(a_eno, a_estr, errno,
  2426. "Could not stat ", path);
  2427. res = ARCHIVE_FAILED;
  2428. break;
  2429. }
  2430. } else if (S_ISDIR(st.st_mode)) {
  2431. if (!last) {
  2432. if (chdir(head) != 0) {
  2433. tail[0] = c;
  2434. fsobj_error(a_eno, a_estr, errno,
  2435. "Could not chdir ", path);
  2436. res = (ARCHIVE_FATAL);
  2437. break;
  2438. }
  2439. /* Our view is now from inside this dir: */
  2440. head = tail + 1;
  2441. }
  2442. } else if (S_ISLNK(st.st_mode)) {
  2443. if (last) {
  2444. /*
  2445. * Last element is symlink; remove it
  2446. * so we can overwrite it with the
  2447. * item being extracted.
  2448. */
  2449. if (unlink(head)) {
  2450. tail[0] = c;
  2451. fsobj_error(a_eno, a_estr, errno,
  2452. "Could not remove symlink ",
  2453. path);
  2454. res = ARCHIVE_FAILED;
  2455. break;
  2456. }
  2457. /*
  2458. * Even if we did remove it, a warning
  2459. * is in order. The warning is silly,
  2460. * though, if we're just replacing one
  2461. * symlink with another symlink.
  2462. */
  2463. tail[0] = c;
  2464. /*
  2465. * FIXME: not sure how important this is to
  2466. * restore
  2467. */
  2468. /*
  2469. if (!S_ISLNK(path)) {
  2470. fsobj_error(a_eno, a_estr, 0,
  2471. "Removing symlink ", path);
  2472. }
  2473. */
  2474. /* Symlink gone. No more problem! */
  2475. res = ARCHIVE_OK;
  2476. break;
  2477. } else if (flags & ARCHIVE_EXTRACT_UNLINK) {
  2478. /* User asked us to remove problems. */
  2479. if (unlink(head) != 0) {
  2480. tail[0] = c;
  2481. fsobj_error(a_eno, a_estr, 0,
  2482. "Cannot remove intervening "
  2483. "symlink ", path);
  2484. res = ARCHIVE_FAILED;
  2485. break;
  2486. }
  2487. tail[0] = c;
  2488. } else if ((flags &
  2489. ARCHIVE_EXTRACT_SECURE_SYMLINKS) == 0) {
  2490. /*
  2491. * We are not the last element and we want to
  2492. * follow symlinks if they are a directory.
  2493. *
  2494. * This is needed to extract hardlinks over
  2495. * symlinks.
  2496. */
  2497. r = stat(head, &st);
  2498. if (r != 0) {
  2499. tail[0] = c;
  2500. if (errno == ENOENT) {
  2501. break;
  2502. } else {
  2503. fsobj_error(a_eno, a_estr,
  2504. errno,
  2505. "Could not stat ", path);
  2506. res = (ARCHIVE_FAILED);
  2507. break;
  2508. }
  2509. } else if (S_ISDIR(st.st_mode)) {
  2510. if (chdir(head) != 0) {
  2511. tail[0] = c;
  2512. fsobj_error(a_eno, a_estr,
  2513. errno,
  2514. "Could not chdir ", path);
  2515. res = (ARCHIVE_FATAL);
  2516. break;
  2517. }
  2518. /*
  2519. * Our view is now from inside
  2520. * this dir:
  2521. */
  2522. head = tail + 1;
  2523. } else {
  2524. tail[0] = c;
  2525. fsobj_error(a_eno, a_estr, 0,
  2526. "Cannot extract through "
  2527. "symlink ", path);
  2528. res = ARCHIVE_FAILED;
  2529. break;
  2530. }
  2531. } else {
  2532. tail[0] = c;
  2533. fsobj_error(a_eno, a_estr, 0,
  2534. "Cannot extract through symlink ", path);
  2535. res = ARCHIVE_FAILED;
  2536. break;
  2537. }
  2538. }
  2539. /* be sure to always maintain this */
  2540. tail[0] = c;
  2541. if (tail[0] != '\0')
  2542. tail++; /* Advance to the next segment. */
  2543. }
  2544. /* Catches loop exits via break */
  2545. tail[0] = c;
  2546. #ifdef HAVE_FCHDIR
  2547. /* If we changed directory above, restore it here. */
  2548. if (restore_pwd >= 0) {
  2549. r = fchdir(restore_pwd);
  2550. if (r != 0) {
  2551. fsobj_error(a_eno, a_estr, errno,
  2552. "chdir() failure", "");
  2553. }
  2554. close(restore_pwd);
  2555. restore_pwd = -1;
  2556. if (r != 0) {
  2557. res = (ARCHIVE_FATAL);
  2558. }
  2559. }
  2560. #endif
  2561. /* TODO: reintroduce a safe cache here? */
  2562. return res;
  2563. #endif
  2564. }
  2565. /*
  2566. * Check a->name for symlinks, returning ARCHIVE_OK if its clean, otherwise
  2567. * calls archive_set_error and returns ARCHIVE_{FATAL,FAILED}
  2568. */
  2569. static int
  2570. check_symlinks(struct archive_write_disk *a)
  2571. {
  2572. struct archive_string error_string;
  2573. int error_number;
  2574. int rc;
  2575. archive_string_init(&error_string);
  2576. rc = check_symlinks_fsobj(a->name, &error_number, &error_string,
  2577. a->flags);
  2578. if (rc != ARCHIVE_OK) {
  2579. archive_set_error(&a->archive, error_number, "%s",
  2580. error_string.s);
  2581. }
  2582. archive_string_free(&error_string);
  2583. a->pst = NULL; /* to be safe */
  2584. return rc;
  2585. }
  2586. #if defined(__CYGWIN__)
  2587. /*
  2588. * 1. Convert a path separator from '\' to '/' .
  2589. * We shouldn't check multibyte character directly because some
  2590. * character-set have been using the '\' character for a part of
  2591. * its multibyte character code.
  2592. * 2. Replace unusable characters in Windows with underscore('_').
  2593. * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
  2594. */
  2595. static void
  2596. cleanup_pathname_win(char *path)
  2597. {
  2598. wchar_t wc;
  2599. char *p;
  2600. size_t alen, l;
  2601. int mb, complete, utf8;
  2602. alen = 0;
  2603. mb = 0;
  2604. complete = 1;
  2605. utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0;
  2606. for (p = path; *p != '\0'; p++) {
  2607. ++alen;
  2608. if (*p == '\\') {
  2609. /* If previous byte is smaller than 128,
  2610. * this is not second byte of multibyte characters,
  2611. * so we can replace '\' with '/'. */
  2612. if (utf8 || !mb)
  2613. *p = '/';
  2614. else
  2615. complete = 0;/* uncompleted. */
  2616. } else if (*(unsigned char *)p > 127)
  2617. mb = 1;
  2618. else
  2619. mb = 0;
  2620. /* Rewrite the path name if its next character is unusable. */
  2621. if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
  2622. *p == '<' || *p == '>' || *p == '|')
  2623. *p = '_';
  2624. }
  2625. if (complete)
  2626. return;
  2627. /*
  2628. * Convert path separator in wide-character.
  2629. */
  2630. p = path;
  2631. while (*p != '\0' && alen) {
  2632. l = mbtowc(&wc, p, alen);
  2633. if (l == (size_t)-1) {
  2634. while (*p != '\0') {
  2635. if (*p == '\\')
  2636. *p = '/';
  2637. ++p;
  2638. }
  2639. break;
  2640. }
  2641. if (l == 1 && wc == L'\\')
  2642. *p = '/';
  2643. p += l;
  2644. alen -= l;
  2645. }
  2646. }
  2647. #endif
  2648. /*
  2649. * Canonicalize the pathname. In particular, this strips duplicate
  2650. * '/' characters, '.' elements, and trailing '/'. It also raises an
  2651. * error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is
  2652. * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
  2653. * is set) if the path is absolute.
  2654. */
  2655. static int
  2656. cleanup_pathname_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
  2657. int flags)
  2658. {
  2659. char *dest, *src;
  2660. char separator = '\0';
  2661. dest = src = path;
  2662. if (*src == '\0') {
  2663. fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
  2664. "Invalid empty ", "pathname");
  2665. return (ARCHIVE_FAILED);
  2666. }
  2667. #if defined(__CYGWIN__)
  2668. cleanup_pathname_win(path);
  2669. #endif
  2670. /* Skip leading '/'. */
  2671. if (*src == '/') {
  2672. if (flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) {
  2673. fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
  2674. "Path is ", "absolute");
  2675. return (ARCHIVE_FAILED);
  2676. }
  2677. separator = *src++;
  2678. }
  2679. /* Scan the pathname one element at a time. */
  2680. for (;;) {
  2681. /* src points to first char after '/' */
  2682. if (src[0] == '\0') {
  2683. break;
  2684. } else if (src[0] == '/') {
  2685. /* Found '//', ignore second one. */
  2686. src++;
  2687. continue;
  2688. } else if (src[0] == '.') {
  2689. if (src[1] == '\0') {
  2690. /* Ignore trailing '.' */
  2691. break;
  2692. } else if (src[1] == '/') {
  2693. /* Skip './'. */
  2694. src += 2;
  2695. continue;
  2696. } else if (src[1] == '.') {
  2697. if (src[2] == '/' || src[2] == '\0') {
  2698. /* Conditionally warn about '..' */
  2699. if (flags
  2700. & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  2701. fsobj_error(a_eno, a_estr,
  2702. ARCHIVE_ERRNO_MISC,
  2703. "Path contains ", "'..'");
  2704. return (ARCHIVE_FAILED);
  2705. }
  2706. }
  2707. /*
  2708. * Note: Under no circumstances do we
  2709. * remove '..' elements. In
  2710. * particular, restoring
  2711. * '/foo/../bar/' should create the
  2712. * 'foo' dir as a side-effect.
  2713. */
  2714. }
  2715. }
  2716. /* Copy current element, including leading '/'. */
  2717. if (separator)
  2718. *dest++ = '/';
  2719. while (*src != '\0' && *src != '/') {
  2720. *dest++ = *src++;
  2721. }
  2722. if (*src == '\0')
  2723. break;
  2724. /* Skip '/' separator. */
  2725. separator = *src++;
  2726. }
  2727. /*
  2728. * We've just copied zero or more path elements, not including the
  2729. * final '/'.
  2730. */
  2731. if (dest == path) {
  2732. /*
  2733. * Nothing got copied. The path must have been something
  2734. * like '.' or '/' or './' or '/././././/./'.
  2735. */
  2736. if (separator)
  2737. *dest++ = '/';
  2738. else
  2739. *dest++ = '.';
  2740. }
  2741. /* Terminate the result. */
  2742. *dest = '\0';
  2743. return (ARCHIVE_OK);
  2744. }
  2745. static int
  2746. cleanup_pathname(struct archive_write_disk *a)
  2747. {
  2748. struct archive_string error_string;
  2749. int error_number;
  2750. int rc;
  2751. archive_string_init(&error_string);
  2752. rc = cleanup_pathname_fsobj(a->name, &error_number, &error_string,
  2753. a->flags);
  2754. if (rc != ARCHIVE_OK) {
  2755. archive_set_error(&a->archive, error_number, "%s",
  2756. error_string.s);
  2757. }
  2758. archive_string_free(&error_string);
  2759. return rc;
  2760. }
  2761. /*
  2762. * Create the parent directory of the specified path, assuming path
  2763. * is already in mutable storage.
  2764. */
  2765. static int
  2766. create_parent_dir(struct archive_write_disk *a, char *path)
  2767. {
  2768. char *slash;
  2769. int r;
  2770. /* Remove tail element to obtain parent name. */
  2771. slash = strrchr(path, '/');
  2772. if (slash == NULL)
  2773. return (ARCHIVE_OK);
  2774. *slash = '\0';
  2775. r = create_dir(a, path);
  2776. *slash = '/';
  2777. return (r);
  2778. }
  2779. /*
  2780. * Create the specified dir, recursing to create parents as necessary.
  2781. *
  2782. * Returns ARCHIVE_OK if the path exists when we're done here.
  2783. * Otherwise, returns ARCHIVE_FAILED.
  2784. * Assumes path is in mutable storage; path is unchanged on exit.
  2785. */
  2786. static int
  2787. create_dir(struct archive_write_disk *a, char *path)
  2788. {
  2789. struct stat st;
  2790. struct fixup_entry *le;
  2791. char *slash, *base;
  2792. mode_t mode_final, mode;
  2793. int r;
  2794. /* Check for special names and just skip them. */
  2795. slash = strrchr(path, '/');
  2796. if (slash == NULL)
  2797. base = path;
  2798. else
  2799. base = slash + 1;
  2800. if (base[0] == '\0' ||
  2801. (base[0] == '.' && base[1] == '\0') ||
  2802. (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
  2803. /* Don't bother trying to create null path, '.', or '..'. */
  2804. if (slash != NULL) {
  2805. *slash = '\0';
  2806. r = create_dir(a, path);
  2807. *slash = '/';
  2808. return (r);
  2809. }
  2810. return (ARCHIVE_OK);
  2811. }
  2812. /*
  2813. * Yes, this should be stat() and not lstat(). Using lstat()
  2814. * here loses the ability to extract through symlinks. Also note
  2815. * that this should not use the a->st cache.
  2816. */
  2817. if (stat(path, &st) == 0) {
  2818. if (S_ISDIR(st.st_mode))
  2819. return (ARCHIVE_OK);
  2820. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  2821. archive_set_error(&a->archive, EEXIST,
  2822. "Can't create directory '%s'", path);
  2823. return (ARCHIVE_FAILED);
  2824. }
  2825. if (unlink(path) != 0) {
  2826. archive_set_error(&a->archive, errno,
  2827. "Can't create directory '%s': "
  2828. "Conflicting file cannot be removed",
  2829. path);
  2830. return (ARCHIVE_FAILED);
  2831. }
  2832. } else if (errno != ENOENT && errno != ENOTDIR) {
  2833. /* Stat failed? */
  2834. archive_set_error(&a->archive, errno,
  2835. "Can't test directory '%s'", path);
  2836. return (ARCHIVE_FAILED);
  2837. } else if (slash != NULL) {
  2838. *slash = '\0';
  2839. r = create_dir(a, path);
  2840. *slash = '/';
  2841. if (r != ARCHIVE_OK)
  2842. return (r);
  2843. }
  2844. /*
  2845. * Mode we want for the final restored directory. Per POSIX,
  2846. * implicitly-created dirs must be created obeying the umask.
  2847. * There's no mention whether this is different for privileged
  2848. * restores (which the rest of this code handles by pretending
  2849. * umask=0). I've chosen here to always obey the user's umask for
  2850. * implicit dirs, even if _EXTRACT_PERM was specified.
  2851. */
  2852. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  2853. /* Mode we want on disk during the restore process. */
  2854. mode = mode_final;
  2855. mode |= MINIMUM_DIR_MODE;
  2856. mode &= MAXIMUM_DIR_MODE;
  2857. if (mkdir(path, mode) == 0) {
  2858. if (mode != mode_final) {
  2859. le = new_fixup(a, path);
  2860. if (le == NULL)
  2861. return (ARCHIVE_FATAL);
  2862. le->fixup |=TODO_MODE_BASE;
  2863. le->mode = mode_final;
  2864. }
  2865. return (ARCHIVE_OK);
  2866. }
  2867. /*
  2868. * Without the following check, a/b/../b/c/d fails at the
  2869. * second visit to 'b', so 'd' can't be created. Note that we
  2870. * don't add it to the fixup list here, as it's already been
  2871. * added.
  2872. */
  2873. if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
  2874. return (ARCHIVE_OK);
  2875. archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
  2876. path);
  2877. return (ARCHIVE_FAILED);
  2878. }
  2879. /*
  2880. * Note: Although we can skip setting the user id if the desired user
  2881. * id matches the current user, we cannot skip setting the group, as
  2882. * many systems set the gid based on the containing directory. So
  2883. * we have to perform a chown syscall if we want to set the SGID
  2884. * bit. (The alternative is to stat() and then possibly chown(); it's
  2885. * more efficient to skip the stat() and just always chown().) Note
  2886. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  2887. * allows set_mode to skip the stat() check for the GID.
  2888. */
  2889. static int
  2890. set_ownership(struct archive_write_disk *a)
  2891. {
  2892. #ifndef __CYGWIN__
  2893. /* unfortunately, on win32 there is no 'root' user with uid 0,
  2894. so we just have to try the chown and see if it works */
  2895. /* If we know we can't change it, don't bother trying. */
  2896. if (a->user_uid != 0 && a->user_uid != a->uid) {
  2897. archive_set_error(&a->archive, errno,
  2898. "Can't set UID=%jd", (intmax_t)a->uid);
  2899. return (ARCHIVE_WARN);
  2900. }
  2901. #endif
  2902. #ifdef HAVE_FCHOWN
  2903. /* If we have an fd, we can avoid a race. */
  2904. if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
  2905. /* We've set owner and know uid/gid are correct. */
  2906. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2907. return (ARCHIVE_OK);
  2908. }
  2909. #endif
  2910. /* We prefer lchown() but will use chown() if that's all we have. */
  2911. /* Of course, if we have neither, this will always fail. */
  2912. #ifdef HAVE_LCHOWN
  2913. if (lchown(a->name, a->uid, a->gid) == 0) {
  2914. /* We've set owner and know uid/gid are correct. */
  2915. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2916. return (ARCHIVE_OK);
  2917. }
  2918. #elif HAVE_CHOWN
  2919. if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
  2920. /* We've set owner and know uid/gid are correct. */
  2921. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2922. return (ARCHIVE_OK);
  2923. }
  2924. #endif
  2925. archive_set_error(&a->archive, errno,
  2926. "Can't set user=%jd/group=%jd for %s",
  2927. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  2928. return (ARCHIVE_WARN);
  2929. }
  2930. /*
  2931. * Note: Returns 0 on success, non-zero on failure.
  2932. */
  2933. static int
  2934. set_time(int fd, int mode, const char *name,
  2935. time_t atime, long atime_nsec,
  2936. time_t mtime, long mtime_nsec)
  2937. {
  2938. /* Select the best implementation for this platform. */
  2939. #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
  2940. /*
  2941. * utimensat() and futimens() are defined in
  2942. * POSIX.1-2008. They support ns resolution and setting times
  2943. * on fds and symlinks.
  2944. */
  2945. struct timespec ts[2];
  2946. (void)mode; /* UNUSED */
  2947. ts[0].tv_sec = atime;
  2948. ts[0].tv_nsec = atime_nsec;
  2949. ts[1].tv_sec = mtime;
  2950. ts[1].tv_nsec = mtime_nsec;
  2951. if (fd >= 0)
  2952. return futimens(fd, ts);
  2953. return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
  2954. #elif HAVE_UTIMES
  2955. /*
  2956. * The utimes()-family functions support µs-resolution and
  2957. * setting times fds and symlinks. utimes() is documented as
  2958. * LEGACY by POSIX, futimes() and lutimes() are not described
  2959. * in POSIX.
  2960. */
  2961. struct timeval times[2];
  2962. times[0].tv_sec = atime;
  2963. times[0].tv_usec = atime_nsec / 1000;
  2964. times[1].tv_sec = mtime;
  2965. times[1].tv_usec = mtime_nsec / 1000;
  2966. #ifdef HAVE_FUTIMES
  2967. if (fd >= 0)
  2968. return (futimes(fd, times));
  2969. #else
  2970. (void)fd; /* UNUSED */
  2971. #endif
  2972. #ifdef HAVE_LUTIMES
  2973. (void)mode; /* UNUSED */
  2974. return (lutimes(name, times));
  2975. #else
  2976. if (S_ISLNK(mode))
  2977. return (0);
  2978. return (utimes(name, times));
  2979. #endif
  2980. #elif defined(HAVE_UTIME)
  2981. /*
  2982. * utime() is POSIX-standard but only supports 1s resolution and
  2983. * does not support fds or symlinks.
  2984. */
  2985. struct utimbuf times;
  2986. (void)fd; /* UNUSED */
  2987. (void)name; /* UNUSED */
  2988. (void)atime_nsec; /* UNUSED */
  2989. (void)mtime_nsec; /* UNUSED */
  2990. times.actime = atime;
  2991. times.modtime = mtime;
  2992. if (S_ISLNK(mode))
  2993. return (ARCHIVE_OK);
  2994. return (utime(name, &times));
  2995. #else
  2996. /*
  2997. * We don't know how to set the time on this platform.
  2998. */
  2999. (void)fd; /* UNUSED */
  3000. (void)mode; /* UNUSED */
  3001. (void)name; /* UNUSED */
  3002. (void)atime_nsec; /* UNUSED */
  3003. (void)mtime_nsec; /* UNUSED */
  3004. return (ARCHIVE_WARN);
  3005. #endif
  3006. }
  3007. #ifdef F_SETTIMES
  3008. static int
  3009. set_time_tru64(int fd, int mode, const char *name,
  3010. time_t atime, long atime_nsec,
  3011. time_t mtime, long mtime_nsec,
  3012. time_t ctime, long ctime_nsec)
  3013. {
  3014. struct attr_timbuf tstamp;
  3015. tstamp.atime.tv_sec = atime;
  3016. tstamp.mtime.tv_sec = mtime;
  3017. tstamp.ctime.tv_sec = ctime;
  3018. #if defined (__hpux) && defined (__ia64)
  3019. tstamp.atime.tv_nsec = atime_nsec;
  3020. tstamp.mtime.tv_nsec = mtime_nsec;
  3021. tstamp.ctime.tv_nsec = ctime_nsec;
  3022. #else
  3023. tstamp.atime.tv_usec = atime_nsec / 1000;
  3024. tstamp.mtime.tv_usec = mtime_nsec / 1000;
  3025. tstamp.ctime.tv_usec = ctime_nsec / 1000;
  3026. #endif
  3027. return (fcntl(fd,F_SETTIMES,&tstamp));
  3028. }
  3029. #endif /* F_SETTIMES */
  3030. static int
  3031. set_times(struct archive_write_disk *a,
  3032. int fd, int mode, const char *name,
  3033. time_t atime, long atime_nanos,
  3034. time_t birthtime, long birthtime_nanos,
  3035. time_t mtime, long mtime_nanos,
  3036. time_t cctime, long ctime_nanos)
  3037. {
  3038. /* Note: set_time doesn't use libarchive return conventions!
  3039. * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */
  3040. int r1 = 0, r2 = 0;
  3041. #ifdef F_SETTIMES
  3042. /*
  3043. * on Tru64 try own fcntl first which can restore even the
  3044. * ctime, fall back to default code path below if it fails
  3045. * or if we are not running as root
  3046. */
  3047. if (a->user_uid == 0 &&
  3048. set_time_tru64(fd, mode, name,
  3049. atime, atime_nanos, mtime,
  3050. mtime_nanos, cctime, ctime_nanos) == 0) {
  3051. return (ARCHIVE_OK);
  3052. }
  3053. #else /* Tru64 */
  3054. (void)cctime; /* UNUSED */
  3055. (void)ctime_nanos; /* UNUSED */
  3056. #endif /* Tru64 */
  3057. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  3058. /*
  3059. * If you have struct stat.st_birthtime, we assume BSD
  3060. * birthtime semantics, in which {f,l,}utimes() updates
  3061. * birthtime to earliest mtime. So we set the time twice,
  3062. * first using the birthtime, then using the mtime. If
  3063. * birthtime == mtime, this isn't necessary, so we skip it.
  3064. * If birthtime > mtime, then this won't work, so we skip it.
  3065. */
  3066. if (birthtime < mtime
  3067. || (birthtime == mtime && birthtime_nanos < mtime_nanos))
  3068. r1 = set_time(fd, mode, name,
  3069. atime, atime_nanos,
  3070. birthtime, birthtime_nanos);
  3071. #else
  3072. (void)birthtime; /* UNUSED */
  3073. (void)birthtime_nanos; /* UNUSED */
  3074. #endif
  3075. r2 = set_time(fd, mode, name,
  3076. atime, atime_nanos,
  3077. mtime, mtime_nanos);
  3078. if (r1 != 0 || r2 != 0) {
  3079. archive_set_error(&a->archive, errno,
  3080. "Can't restore time");
  3081. return (ARCHIVE_WARN);
  3082. }
  3083. return (ARCHIVE_OK);
  3084. }
  3085. static int
  3086. set_times_from_entry(struct archive_write_disk *a)
  3087. {
  3088. time_t atime, birthtime, mtime, cctime;
  3089. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  3090. /* Suitable defaults. */
  3091. atime = birthtime = mtime = cctime = a->start_time;
  3092. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  3093. /* If no time was provided, we're done. */
  3094. if (!archive_entry_atime_is_set(a->entry)
  3095. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  3096. && !archive_entry_birthtime_is_set(a->entry)
  3097. #endif
  3098. && !archive_entry_mtime_is_set(a->entry))
  3099. return (ARCHIVE_OK);
  3100. if (archive_entry_atime_is_set(a->entry)) {
  3101. atime = archive_entry_atime(a->entry);
  3102. atime_nsec = archive_entry_atime_nsec(a->entry);
  3103. }
  3104. if (archive_entry_birthtime_is_set(a->entry)) {
  3105. birthtime = archive_entry_birthtime(a->entry);
  3106. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  3107. }
  3108. if (archive_entry_mtime_is_set(a->entry)) {
  3109. mtime = archive_entry_mtime(a->entry);
  3110. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  3111. }
  3112. if (archive_entry_ctime_is_set(a->entry)) {
  3113. cctime = archive_entry_ctime(a->entry);
  3114. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  3115. }
  3116. return set_times(a, a->fd, a->mode, a->name,
  3117. atime, atime_nsec,
  3118. birthtime, birthtime_nsec,
  3119. mtime, mtime_nsec,
  3120. cctime, ctime_nsec);
  3121. }
  3122. static int
  3123. set_mode(struct archive_write_disk *a, int mode)
  3124. {
  3125. int r = ARCHIVE_OK;
  3126. mode &= 07777; /* Strip off file type bits. */
  3127. if (a->todo & TODO_SGID_CHECK) {
  3128. /*
  3129. * If we don't know the GID is right, we must stat()
  3130. * to verify it. We can't just check the GID of this
  3131. * process, since systems sometimes set GID from
  3132. * the enclosing dir or based on ACLs.
  3133. */
  3134. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  3135. return (r);
  3136. if (a->pst->st_gid != a->gid) {
  3137. mode &= ~ S_ISGID;
  3138. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3139. /*
  3140. * This is only an error if you
  3141. * requested owner restore. If you
  3142. * didn't, we'll try to restore
  3143. * sgid/suid, but won't consider it a
  3144. * problem if we can't.
  3145. */
  3146. archive_set_error(&a->archive, -1,
  3147. "Can't restore SGID bit");
  3148. r = ARCHIVE_WARN;
  3149. }
  3150. }
  3151. /* While we're here, double-check the UID. */
  3152. if (a->pst->st_uid != a->uid
  3153. && (a->todo & TODO_SUID)) {
  3154. mode &= ~ S_ISUID;
  3155. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3156. archive_set_error(&a->archive, -1,
  3157. "Can't restore SUID bit");
  3158. r = ARCHIVE_WARN;
  3159. }
  3160. }
  3161. a->todo &= ~TODO_SGID_CHECK;
  3162. a->todo &= ~TODO_SUID_CHECK;
  3163. } else if (a->todo & TODO_SUID_CHECK) {
  3164. /*
  3165. * If we don't know the UID is right, we can just check
  3166. * the user, since all systems set the file UID from
  3167. * the process UID.
  3168. */
  3169. if (a->user_uid != a->uid) {
  3170. mode &= ~ S_ISUID;
  3171. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  3172. archive_set_error(&a->archive, -1,
  3173. "Can't make file SUID");
  3174. r = ARCHIVE_WARN;
  3175. }
  3176. }
  3177. a->todo &= ~TODO_SUID_CHECK;
  3178. }
  3179. if (S_ISLNK(a->mode)) {
  3180. #ifdef HAVE_LCHMOD
  3181. /*
  3182. * If this is a symlink, use lchmod(). If the
  3183. * platform doesn't support lchmod(), just skip it. A
  3184. * platform that doesn't provide a way to set
  3185. * permissions on symlinks probably ignores
  3186. * permissions on symlinks, so a failure here has no
  3187. * impact.
  3188. */
  3189. if (lchmod(a->name, mode) != 0) {
  3190. switch (errno) {
  3191. case ENOTSUP:
  3192. case ENOSYS:
  3193. #if ENOTSUP != EOPNOTSUPP
  3194. case EOPNOTSUPP:
  3195. #endif
  3196. /*
  3197. * if lchmod is defined but the platform
  3198. * doesn't support it, silently ignore
  3199. * error
  3200. */
  3201. break;
  3202. default:
  3203. archive_set_error(&a->archive, errno,
  3204. "Can't set permissions to 0%o", (int)mode);
  3205. r = ARCHIVE_WARN;
  3206. }
  3207. }
  3208. #endif
  3209. } else if (!S_ISDIR(a->mode)) {
  3210. /*
  3211. * If it's not a symlink and not a dir, then use
  3212. * fchmod() or chmod(), depending on whether we have
  3213. * an fd. Dirs get their perms set during the
  3214. * post-extract fixup, which is handled elsewhere.
  3215. */
  3216. #ifdef HAVE_FCHMOD
  3217. if (a->fd >= 0) {
  3218. if (fchmod(a->fd, mode) != 0) {
  3219. archive_set_error(&a->archive, errno,
  3220. "Can't set permissions to 0%o", (int)mode);
  3221. r = ARCHIVE_WARN;
  3222. }
  3223. } else
  3224. #endif
  3225. /* If this platform lacks fchmod(), then
  3226. * we'll just use chmod(). */
  3227. if (chmod(a->name, mode) != 0) {
  3228. archive_set_error(&a->archive, errno,
  3229. "Can't set permissions to 0%o", (int)mode);
  3230. r = ARCHIVE_WARN;
  3231. }
  3232. }
  3233. return (r);
  3234. }
  3235. static int
  3236. set_fflags(struct archive_write_disk *a)
  3237. {
  3238. struct fixup_entry *le;
  3239. unsigned long set, clear;
  3240. int r;
  3241. int critical_flags;
  3242. mode_t mode = archive_entry_mode(a->entry);
  3243. /*
  3244. * Make 'critical_flags' hold all file flags that can't be
  3245. * immediately restored. For example, on BSD systems,
  3246. * SF_IMMUTABLE prevents hardlinks from being created, so
  3247. * should not be set until after any hardlinks are created. To
  3248. * preserve some semblance of portability, this uses #ifdef
  3249. * extensively. Ugly, but it works.
  3250. *
  3251. * Yes, Virginia, this does create a security race. It's mitigated
  3252. * somewhat by the practice of creating dirs 0700 until the extract
  3253. * is done, but it would be nice if we could do more than that.
  3254. * People restoring critical file systems should be wary of
  3255. * other programs that might try to muck with files as they're
  3256. * being restored.
  3257. */
  3258. /* Hopefully, the compiler will optimize this mess into a constant. */
  3259. critical_flags = 0;
  3260. #ifdef SF_IMMUTABLE
  3261. critical_flags |= SF_IMMUTABLE;
  3262. #endif
  3263. #ifdef UF_IMMUTABLE
  3264. critical_flags |= UF_IMMUTABLE;
  3265. #endif
  3266. #ifdef SF_APPEND
  3267. critical_flags |= SF_APPEND;
  3268. #endif
  3269. #ifdef UF_APPEND
  3270. critical_flags |= UF_APPEND;
  3271. #endif
  3272. #if defined(FS_APPEND_FL)
  3273. critical_flags |= FS_APPEND_FL;
  3274. #elif defined(EXT2_APPEND_FL)
  3275. critical_flags |= EXT2_APPEND_FL;
  3276. #endif
  3277. #if defined(FS_IMMUTABLE_FL)
  3278. critical_flags |= FS_IMMUTABLE_FL;
  3279. #elif defined(EXT2_IMMUTABLE_FL)
  3280. critical_flags |= EXT2_IMMUTABLE_FL;
  3281. #endif
  3282. #ifdef FS_JOURNAL_DATA_FL
  3283. critical_flags |= FS_JOURNAL_DATA_FL;
  3284. #endif
  3285. if (a->todo & TODO_FFLAGS) {
  3286. archive_entry_fflags(a->entry, &set, &clear);
  3287. /*
  3288. * The first test encourages the compiler to eliminate
  3289. * all of this if it's not necessary.
  3290. */
  3291. if ((critical_flags != 0) && (set & critical_flags)) {
  3292. le = current_fixup(a, a->name);
  3293. if (le == NULL)
  3294. return (ARCHIVE_FATAL);
  3295. le->fixup |= TODO_FFLAGS;
  3296. le->fflags_set = set;
  3297. /* Store the mode if it's not already there. */
  3298. if ((le->fixup & TODO_MODE) == 0)
  3299. le->mode = mode;
  3300. } else {
  3301. r = set_fflags_platform(a, a->fd,
  3302. a->name, mode, set, clear);
  3303. if (r != ARCHIVE_OK)
  3304. return (r);
  3305. }
  3306. }
  3307. return (ARCHIVE_OK);
  3308. }
  3309. static int
  3310. clear_nochange_fflags(struct archive_write_disk *a)
  3311. {
  3312. int nochange_flags;
  3313. mode_t mode = archive_entry_mode(a->entry);
  3314. /* Hopefully, the compiler will optimize this mess into a constant. */
  3315. nochange_flags = 0;
  3316. #ifdef SF_IMMUTABLE
  3317. nochange_flags |= SF_IMMUTABLE;
  3318. #endif
  3319. #ifdef UF_IMMUTABLE
  3320. nochange_flags |= UF_IMMUTABLE;
  3321. #endif
  3322. #ifdef SF_APPEND
  3323. nochange_flags |= SF_APPEND;
  3324. #endif
  3325. #ifdef UF_APPEND
  3326. nochange_flags |= UF_APPEND;
  3327. #endif
  3328. #ifdef EXT2_APPEND_FL
  3329. nochange_flags |= EXT2_APPEND_FL;
  3330. #endif
  3331. #ifdef EXT2_IMMUTABLE_FL
  3332. nochange_flags |= EXT2_IMMUTABLE_FL;
  3333. #endif
  3334. return (set_fflags_platform(a, a->fd, a->name, mode, 0,
  3335. nochange_flags));
  3336. }
  3337. #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
  3338. /*
  3339. * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
  3340. */
  3341. static int
  3342. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3343. mode_t mode, unsigned long set, unsigned long clear)
  3344. {
  3345. int r;
  3346. (void)mode; /* UNUSED */
  3347. if (set == 0 && clear == 0)
  3348. return (ARCHIVE_OK);
  3349. /*
  3350. * XXX Is the stat here really necessary? Or can I just use
  3351. * the 'set' flags directly? In particular, I'm not sure
  3352. * about the correct approach if we're overwriting an existing
  3353. * file that already has flags on it. XXX
  3354. */
  3355. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  3356. return (r);
  3357. a->st.st_flags &= ~clear;
  3358. a->st.st_flags |= set;
  3359. #ifdef HAVE_FCHFLAGS
  3360. /* If platform has fchflags() and we were given an fd, use it. */
  3361. if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
  3362. return (ARCHIVE_OK);
  3363. #endif
  3364. /*
  3365. * If we can't use the fd to set the flags, we'll use the
  3366. * pathname to set flags. We prefer lchflags() but will use
  3367. * chflags() if we must.
  3368. */
  3369. #ifdef HAVE_LCHFLAGS
  3370. if (lchflags(name, a->st.st_flags) == 0)
  3371. return (ARCHIVE_OK);
  3372. #elif defined(HAVE_CHFLAGS)
  3373. if (S_ISLNK(a->st.st_mode)) {
  3374. archive_set_error(&a->archive, errno,
  3375. "Can't set file flags on symlink.");
  3376. return (ARCHIVE_WARN);
  3377. }
  3378. if (chflags(name, a->st.st_flags) == 0)
  3379. return (ARCHIVE_OK);
  3380. #endif
  3381. archive_set_error(&a->archive, errno,
  3382. "Failed to set file flags");
  3383. return (ARCHIVE_WARN);
  3384. }
  3385. #elif (defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) && \
  3386. defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
  3387. (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && \
  3388. defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
  3389. /*
  3390. * Linux uses ioctl() to read and write file flags.
  3391. */
  3392. static int
  3393. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3394. mode_t mode, unsigned long set, unsigned long clear)
  3395. {
  3396. int ret;
  3397. int myfd = fd;
  3398. int newflags, oldflags;
  3399. int sf_mask = 0;
  3400. if (set == 0 && clear == 0)
  3401. return (ARCHIVE_OK);
  3402. /* Only regular files and dirs can have flags. */
  3403. if (!S_ISREG(mode) && !S_ISDIR(mode))
  3404. return (ARCHIVE_OK);
  3405. /* If we weren't given an fd, open it ourselves. */
  3406. if (myfd < 0) {
  3407. myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY | O_CLOEXEC);
  3408. __archive_ensure_cloexec_flag(myfd);
  3409. }
  3410. if (myfd < 0)
  3411. return (ARCHIVE_OK);
  3412. /*
  3413. * Linux has no define for the flags that are only settable by
  3414. * the root user. This code may seem a little complex, but
  3415. * there seem to be some Linux systems that lack these
  3416. * defines. (?) The code below degrades reasonably gracefully
  3417. * if sf_mask is incomplete.
  3418. */
  3419. #if defined(FS_IMMUTABLE_FL)
  3420. sf_mask |= FS_IMMUTABLE_FL;
  3421. #elif defined(EXT2_IMMUTABLE_FL)
  3422. sf_mask |= EXT2_IMMUTABLE_FL;
  3423. #endif
  3424. #if defined(FS_APPEND_FL)
  3425. sf_mask |= FS_APPEND_FL;
  3426. #elif defined(EXT2_APPEND_FL)
  3427. sf_mask |= EXT2_APPEND_FL;
  3428. #endif
  3429. #if defined(FS_JOURNAL_DATA_FL)
  3430. sf_mask |= FS_JOURNAL_DATA_FL;
  3431. #endif
  3432. /*
  3433. * XXX As above, this would be way simpler if we didn't have
  3434. * to read the current flags from disk. XXX
  3435. */
  3436. ret = ARCHIVE_OK;
  3437. /* Read the current file flags. */
  3438. if (ioctl(myfd,
  3439. #ifdef FS_IOC_GETFLAGS
  3440. FS_IOC_GETFLAGS,
  3441. #else
  3442. EXT2_IOC_GETFLAGS,
  3443. #endif
  3444. &oldflags) < 0)
  3445. goto fail;
  3446. /* Try setting the flags as given. */
  3447. newflags = (oldflags & ~clear) | set;
  3448. if (ioctl(myfd,
  3449. #ifdef FS_IOC_SETFLAGS
  3450. FS_IOC_SETFLAGS,
  3451. #else
  3452. EXT2_IOC_SETFLAGS,
  3453. #endif
  3454. &newflags) >= 0)
  3455. goto cleanup;
  3456. if (errno != EPERM)
  3457. goto fail;
  3458. /* If we couldn't set all the flags, try again with a subset. */
  3459. newflags &= ~sf_mask;
  3460. oldflags &= sf_mask;
  3461. newflags |= oldflags;
  3462. if (ioctl(myfd,
  3463. #ifdef FS_IOC_SETFLAGS
  3464. FS_IOC_SETFLAGS,
  3465. #else
  3466. EXT2_IOC_SETFLAGS,
  3467. #endif
  3468. &newflags) >= 0)
  3469. goto cleanup;
  3470. /* We couldn't set the flags, so report the failure. */
  3471. fail:
  3472. archive_set_error(&a->archive, errno,
  3473. "Failed to set file flags");
  3474. ret = ARCHIVE_WARN;
  3475. cleanup:
  3476. if (fd < 0)
  3477. close(myfd);
  3478. return (ret);
  3479. }
  3480. #else
  3481. /*
  3482. * Of course, some systems have neither BSD chflags() nor Linux' flags
  3483. * support through ioctl().
  3484. */
  3485. static int
  3486. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3487. mode_t mode, unsigned long set, unsigned long clear)
  3488. {
  3489. (void)a; /* UNUSED */
  3490. (void)fd; /* UNUSED */
  3491. (void)name; /* UNUSED */
  3492. (void)mode; /* UNUSED */
  3493. (void)set; /* UNUSED */
  3494. (void)clear; /* UNUSED */
  3495. return (ARCHIVE_OK);
  3496. }
  3497. #endif /* __linux */
  3498. #ifndef HAVE_COPYFILE_H
  3499. /* Default is to simply drop Mac extended metadata. */
  3500. static int
  3501. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3502. const void *metadata, size_t metadata_size)
  3503. {
  3504. (void)a; /* UNUSED */
  3505. (void)pathname; /* UNUSED */
  3506. (void)metadata; /* UNUSED */
  3507. (void)metadata_size; /* UNUSED */
  3508. return (ARCHIVE_OK);
  3509. }
  3510. static int
  3511. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3512. {
  3513. (void)a; /* UNUSED */
  3514. (void)pathname; /* UNUSED */
  3515. return (ARCHIVE_OK);
  3516. }
  3517. #else
  3518. /*
  3519. * On Mac OS, we use copyfile() to unpack the metadata and
  3520. * apply it to the target file.
  3521. */
  3522. #if defined(HAVE_SYS_XATTR_H)
  3523. static int
  3524. copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd)
  3525. {
  3526. ssize_t xattr_size;
  3527. char *xattr_names = NULL, *xattr_val = NULL;
  3528. int ret = ARCHIVE_OK, xattr_i;
  3529. xattr_size = flistxattr(tmpfd, NULL, 0, 0);
  3530. if (xattr_size == -1) {
  3531. archive_set_error(&a->archive, errno,
  3532. "Failed to read metadata(xattr)");
  3533. ret = ARCHIVE_WARN;
  3534. goto exit_xattr;
  3535. }
  3536. xattr_names = malloc(xattr_size);
  3537. if (xattr_names == NULL) {
  3538. archive_set_error(&a->archive, ENOMEM,
  3539. "Can't allocate memory for metadata(xattr)");
  3540. ret = ARCHIVE_FATAL;
  3541. goto exit_xattr;
  3542. }
  3543. xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0);
  3544. if (xattr_size == -1) {
  3545. archive_set_error(&a->archive, errno,
  3546. "Failed to read metadata(xattr)");
  3547. ret = ARCHIVE_WARN;
  3548. goto exit_xattr;
  3549. }
  3550. for (xattr_i = 0; xattr_i < xattr_size;
  3551. xattr_i += strlen(xattr_names + xattr_i) + 1) {
  3552. char *xattr_val_saved;
  3553. ssize_t s;
  3554. int f;
  3555. s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0);
  3556. if (s == -1) {
  3557. archive_set_error(&a->archive, errno,
  3558. "Failed to get metadata(xattr)");
  3559. ret = ARCHIVE_WARN;
  3560. goto exit_xattr;
  3561. }
  3562. xattr_val_saved = xattr_val;
  3563. xattr_val = realloc(xattr_val, s);
  3564. if (xattr_val == NULL) {
  3565. archive_set_error(&a->archive, ENOMEM,
  3566. "Failed to get metadata(xattr)");
  3567. ret = ARCHIVE_WARN;
  3568. free(xattr_val_saved);
  3569. goto exit_xattr;
  3570. }
  3571. s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3572. if (s == -1) {
  3573. archive_set_error(&a->archive, errno,
  3574. "Failed to get metadata(xattr)");
  3575. ret = ARCHIVE_WARN;
  3576. goto exit_xattr;
  3577. }
  3578. f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3579. if (f == -1) {
  3580. archive_set_error(&a->archive, errno,
  3581. "Failed to get metadata(xattr)");
  3582. ret = ARCHIVE_WARN;
  3583. goto exit_xattr;
  3584. }
  3585. }
  3586. exit_xattr:
  3587. free(xattr_names);
  3588. free(xattr_val);
  3589. return (ret);
  3590. }
  3591. #endif
  3592. static int
  3593. copy_acls(struct archive_write_disk *a, int tmpfd, int dffd)
  3594. {
  3595. #ifndef HAVE_SYS_ACL_H
  3596. return 0;
  3597. #else
  3598. acl_t acl, dfacl = NULL;
  3599. int acl_r, ret = ARCHIVE_OK;
  3600. acl = acl_get_fd(tmpfd);
  3601. if (acl == NULL) {
  3602. if (errno == ENOENT)
  3603. /* There are not any ACLs. */
  3604. return (ret);
  3605. archive_set_error(&a->archive, errno,
  3606. "Failed to get metadata(acl)");
  3607. ret = ARCHIVE_WARN;
  3608. goto exit_acl;
  3609. }
  3610. dfacl = acl_dup(acl);
  3611. acl_r = acl_set_fd(dffd, dfacl);
  3612. if (acl_r == -1) {
  3613. archive_set_error(&a->archive, errno,
  3614. "Failed to get metadata(acl)");
  3615. ret = ARCHIVE_WARN;
  3616. goto exit_acl;
  3617. }
  3618. exit_acl:
  3619. if (acl)
  3620. acl_free(acl);
  3621. if (dfacl)
  3622. acl_free(dfacl);
  3623. return (ret);
  3624. #endif
  3625. }
  3626. static int
  3627. create_tempdatafork(struct archive_write_disk *a, const char *pathname)
  3628. {
  3629. struct archive_string tmpdatafork;
  3630. int tmpfd;
  3631. archive_string_init(&tmpdatafork);
  3632. archive_strcpy(&tmpdatafork, "tar.md.XXXXXX");
  3633. tmpfd = mkstemp(tmpdatafork.s);
  3634. if (tmpfd < 0) {
  3635. archive_set_error(&a->archive, errno,
  3636. "Failed to mkstemp");
  3637. archive_string_free(&tmpdatafork);
  3638. return (-1);
  3639. }
  3640. if (copyfile(pathname, tmpdatafork.s, 0,
  3641. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3642. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3643. archive_set_error(&a->archive, errno,
  3644. "Failed to restore metadata");
  3645. close(tmpfd);
  3646. tmpfd = -1;
  3647. }
  3648. unlink(tmpdatafork.s);
  3649. archive_string_free(&tmpdatafork);
  3650. return (tmpfd);
  3651. }
  3652. static int
  3653. copy_metadata(struct archive_write_disk *a, const char *metadata,
  3654. const char *datafork, int datafork_compressed)
  3655. {
  3656. int ret = ARCHIVE_OK;
  3657. if (datafork_compressed) {
  3658. int dffd, tmpfd;
  3659. tmpfd = create_tempdatafork(a, metadata);
  3660. if (tmpfd == -1)
  3661. return (ARCHIVE_WARN);
  3662. /*
  3663. * Do not open the data fork compressed by HFS+ compression
  3664. * with at least a writing mode(O_RDWR or O_WRONLY). it
  3665. * makes the data fork uncompressed.
  3666. */
  3667. dffd = open(datafork, 0);
  3668. if (dffd == -1) {
  3669. archive_set_error(&a->archive, errno,
  3670. "Failed to open the data fork for metadata");
  3671. close(tmpfd);
  3672. return (ARCHIVE_WARN);
  3673. }
  3674. #if defined(HAVE_SYS_XATTR_H)
  3675. ret = copy_xattrs(a, tmpfd, dffd);
  3676. if (ret == ARCHIVE_OK)
  3677. #endif
  3678. ret = copy_acls(a, tmpfd, dffd);
  3679. close(tmpfd);
  3680. close(dffd);
  3681. } else {
  3682. if (copyfile(metadata, datafork, 0,
  3683. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3684. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3685. archive_set_error(&a->archive, errno,
  3686. "Failed to restore metadata");
  3687. ret = ARCHIVE_WARN;
  3688. }
  3689. }
  3690. return (ret);
  3691. }
  3692. static int
  3693. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3694. const void *metadata, size_t metadata_size)
  3695. {
  3696. struct archive_string tmp;
  3697. ssize_t written;
  3698. int fd;
  3699. int ret = ARCHIVE_OK;
  3700. /* This would be simpler if copyfile() could just accept the
  3701. * metadata as a block of memory; then we could sidestep this
  3702. * silly dance of writing the data to disk just so that
  3703. * copyfile() can read it back in again. */
  3704. archive_string_init(&tmp);
  3705. archive_strcpy(&tmp, pathname);
  3706. archive_strcat(&tmp, ".XXXXXX");
  3707. fd = mkstemp(tmp.s);
  3708. if (fd < 0) {
  3709. archive_set_error(&a->archive, errno,
  3710. "Failed to restore metadata");
  3711. archive_string_free(&tmp);
  3712. return (ARCHIVE_WARN);
  3713. }
  3714. written = write(fd, metadata, metadata_size);
  3715. close(fd);
  3716. if ((size_t)written != metadata_size) {
  3717. archive_set_error(&a->archive, errno,
  3718. "Failed to restore metadata");
  3719. ret = ARCHIVE_WARN;
  3720. } else {
  3721. int compressed;
  3722. #if defined(UF_COMPRESSED)
  3723. if ((a->todo & TODO_HFS_COMPRESSION) != 0 &&
  3724. (ret = lazy_stat(a)) == ARCHIVE_OK)
  3725. compressed = a->st.st_flags & UF_COMPRESSED;
  3726. else
  3727. #endif
  3728. compressed = 0;
  3729. ret = copy_metadata(a, tmp.s, pathname, compressed);
  3730. }
  3731. unlink(tmp.s);
  3732. archive_string_free(&tmp);
  3733. return (ret);
  3734. }
  3735. static int
  3736. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3737. {
  3738. char buff[8];
  3739. struct stat st;
  3740. const char *p;
  3741. struct archive_string datafork;
  3742. int fd = -1, ret = ARCHIVE_OK;
  3743. archive_string_init(&datafork);
  3744. /* Check if the current file name is a type of the resource
  3745. * fork file. */
  3746. p = strrchr(pathname, '/');
  3747. if (p == NULL)
  3748. p = pathname;
  3749. else
  3750. p++;
  3751. if (p[0] != '.' || p[1] != '_')
  3752. goto skip_appledouble;
  3753. /*
  3754. * Check if the data fork file exists.
  3755. *
  3756. * TODO: Check if this write disk object has handled it.
  3757. */
  3758. archive_strncpy(&datafork, pathname, p - pathname);
  3759. archive_strcat(&datafork, p + 2);
  3760. if (lstat(datafork.s, &st) == -1 ||
  3761. (st.st_mode & AE_IFMT) != AE_IFREG)
  3762. goto skip_appledouble;
  3763. /*
  3764. * Check if the file is in the AppleDouble form.
  3765. */
  3766. fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC);
  3767. __archive_ensure_cloexec_flag(fd);
  3768. if (fd == -1) {
  3769. archive_set_error(&a->archive, errno,
  3770. "Failed to open a restoring file");
  3771. ret = ARCHIVE_WARN;
  3772. goto skip_appledouble;
  3773. }
  3774. if (read(fd, buff, 8) == -1) {
  3775. archive_set_error(&a->archive, errno,
  3776. "Failed to read a restoring file");
  3777. close(fd);
  3778. ret = ARCHIVE_WARN;
  3779. goto skip_appledouble;
  3780. }
  3781. close(fd);
  3782. /* Check AppleDouble Magic Code. */
  3783. if (archive_be32dec(buff) != 0x00051607)
  3784. goto skip_appledouble;
  3785. /* Check AppleDouble Version. */
  3786. if (archive_be32dec(buff+4) != 0x00020000)
  3787. goto skip_appledouble;
  3788. ret = copy_metadata(a, pathname, datafork.s,
  3789. #if defined(UF_COMPRESSED)
  3790. st.st_flags & UF_COMPRESSED);
  3791. #else
  3792. 0);
  3793. #endif
  3794. if (ret == ARCHIVE_OK) {
  3795. unlink(pathname);
  3796. ret = ARCHIVE_EOF;
  3797. }
  3798. skip_appledouble:
  3799. archive_string_free(&datafork);
  3800. return (ret);
  3801. }
  3802. #endif
  3803. #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
  3804. /*
  3805. * Restore extended attributes - Linux, Darwin and AIX implementations:
  3806. * AIX' ea interface is syntaxwise identical to the Linux xattr interface.
  3807. */
  3808. static int
  3809. set_xattrs(struct archive_write_disk *a)
  3810. {
  3811. struct archive_entry *entry = a->entry;
  3812. struct archive_string errlist;
  3813. int ret = ARCHIVE_OK;
  3814. int i = archive_entry_xattr_reset(entry);
  3815. short fail = 0;
  3816. archive_string_init(&errlist);
  3817. while (i--) {
  3818. const char *name;
  3819. const void *value;
  3820. size_t size;
  3821. int e;
  3822. archive_entry_xattr_next(entry, &name, &value, &size);
  3823. if (name == NULL)
  3824. continue;
  3825. #if ARCHIVE_XATTR_LINUX
  3826. /* Linux: quietly skip POSIX.1e ACL extended attributes */
  3827. if (strncmp(name, "system.", 7) == 0 &&
  3828. (strcmp(name + 7, "posix_acl_access") == 0 ||
  3829. strcmp(name + 7, "posix_acl_default") == 0))
  3830. continue;
  3831. if (strncmp(name, "trusted.SGI_", 12) == 0 &&
  3832. (strcmp(name + 12, "ACL_DEFAULT") == 0 ||
  3833. strcmp(name + 12, "ACL_FILE") == 0))
  3834. continue;
  3835. /* Linux: xfsroot namespace is obsolete and unsupported */
  3836. if (strncmp(name, "xfsroot.", 8) == 0) {
  3837. fail = 1;
  3838. archive_strcat(&errlist, name);
  3839. archive_strappend_char(&errlist, ' ');
  3840. continue;
  3841. }
  3842. #endif
  3843. if (a->fd >= 0) {
  3844. #if ARCHIVE_XATTR_LINUX
  3845. e = fsetxattr(a->fd, name, value, size, 0);
  3846. #elif ARCHIVE_XATTR_DARWIN
  3847. e = fsetxattr(a->fd, name, value, size, 0, 0);
  3848. #elif ARCHIVE_XATTR_AIX
  3849. e = fsetea(a->fd, name, value, size, 0);
  3850. #endif
  3851. } else {
  3852. #if ARCHIVE_XATTR_LINUX
  3853. e = lsetxattr(archive_entry_pathname(entry),
  3854. name, value, size, 0);
  3855. #elif ARCHIVE_XATTR_DARWIN
  3856. e = setxattr(archive_entry_pathname(entry),
  3857. name, value, size, 0, XATTR_NOFOLLOW);
  3858. #elif ARCHIVE_XATTR_AIX
  3859. e = lsetea(archive_entry_pathname(entry),
  3860. name, value, size, 0);
  3861. #endif
  3862. }
  3863. if (e == -1) {
  3864. ret = ARCHIVE_WARN;
  3865. archive_strcat(&errlist, name);
  3866. archive_strappend_char(&errlist, ' ');
  3867. if (errno != ENOTSUP && errno != ENOSYS)
  3868. fail = 1;
  3869. }
  3870. }
  3871. if (ret == ARCHIVE_WARN) {
  3872. if (fail && errlist.length > 0) {
  3873. errlist.length--;
  3874. errlist.s[errlist.length] = '\0';
  3875. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3876. "Cannot restore extended attributes: %s",
  3877. errlist.s);
  3878. } else
  3879. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3880. "Cannot restore extended "
  3881. "attributes on this file system.");
  3882. }
  3883. archive_string_free(&errlist);
  3884. return (ret);
  3885. }
  3886. #elif ARCHIVE_XATTR_FREEBSD
  3887. /*
  3888. * Restore extended attributes - FreeBSD implementation
  3889. */
  3890. static int
  3891. set_xattrs(struct archive_write_disk *a)
  3892. {
  3893. struct archive_entry *entry = a->entry;
  3894. struct archive_string errlist;
  3895. int ret = ARCHIVE_OK;
  3896. int i = archive_entry_xattr_reset(entry);
  3897. short fail = 0;
  3898. archive_string_init(&errlist);
  3899. while (i--) {
  3900. const char *name;
  3901. const void *value;
  3902. size_t size;
  3903. archive_entry_xattr_next(entry, &name, &value, &size);
  3904. if (name != NULL) {
  3905. int e;
  3906. int namespace;
  3907. if (strncmp(name, "user.", 5) == 0) {
  3908. /* "user." attributes go to user namespace */
  3909. name += 5;
  3910. namespace = EXTATTR_NAMESPACE_USER;
  3911. } else {
  3912. /* Other namespaces are unsupported */
  3913. archive_strcat(&errlist, name);
  3914. archive_strappend_char(&errlist, ' ');
  3915. fail = 1;
  3916. ret = ARCHIVE_WARN;
  3917. continue;
  3918. }
  3919. if (a->fd >= 0) {
  3920. e = extattr_set_fd(a->fd, namespace, name,
  3921. value, size);
  3922. } else {
  3923. e = extattr_set_link(
  3924. archive_entry_pathname(entry), namespace,
  3925. name, value, size);
  3926. }
  3927. if (e != (int)size) {
  3928. archive_strcat(&errlist, name);
  3929. archive_strappend_char(&errlist, ' ');
  3930. ret = ARCHIVE_WARN;
  3931. if (errno != ENOTSUP && errno != ENOSYS)
  3932. fail = 1;
  3933. }
  3934. }
  3935. }
  3936. if (ret == ARCHIVE_WARN) {
  3937. if (fail && errlist.length > 0) {
  3938. errlist.length--;
  3939. errlist.s[errlist.length] = '\0';
  3940. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3941. "Cannot restore extended attributes: %s",
  3942. errlist.s);
  3943. } else
  3944. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  3945. "Cannot restore extended "
  3946. "attributes on this file system.");
  3947. }
  3948. archive_string_free(&errlist);
  3949. return (ret);
  3950. }
  3951. #else
  3952. /*
  3953. * Restore extended attributes - stub implementation for unsupported systems
  3954. */
  3955. static int
  3956. set_xattrs(struct archive_write_disk *a)
  3957. {
  3958. static int warning_done = 0;
  3959. /* If there aren't any extended attributes, then it's okay not
  3960. * to extract them, otherwise, issue a single warning. */
  3961. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  3962. warning_done = 1;
  3963. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3964. "Cannot restore extended attributes on this system");
  3965. return (ARCHIVE_WARN);
  3966. }
  3967. /* Warning was already emitted; suppress further warnings. */
  3968. return (ARCHIVE_OK);
  3969. }
  3970. #endif
  3971. /*
  3972. * Test if file on disk is older than entry.
  3973. */
  3974. static int
  3975. older(struct stat *st, struct archive_entry *entry)
  3976. {
  3977. /* First, test the seconds and return if we have a definite answer. */
  3978. /* Definitely older. */
  3979. if (to_int64_time(st->st_mtime) < to_int64_time(archive_entry_mtime(entry)))
  3980. return (1);
  3981. /* Definitely younger. */
  3982. if (to_int64_time(st->st_mtime) > to_int64_time(archive_entry_mtime(entry)))
  3983. return (0);
  3984. /* If this platform supports fractional seconds, try those. */
  3985. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  3986. /* Definitely older. */
  3987. if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
  3988. return (1);
  3989. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  3990. /* Definitely older. */
  3991. if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
  3992. return (1);
  3993. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  3994. /* older. */
  3995. if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
  3996. return (1);
  3997. #elif HAVE_STRUCT_STAT_ST_UMTIME
  3998. /* older. */
  3999. if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
  4000. return (1);
  4001. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  4002. /* older. */
  4003. if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
  4004. return (1);
  4005. #else
  4006. /* This system doesn't have high-res timestamps. */
  4007. #endif
  4008. /* Same age or newer, so not older. */
  4009. return (0);
  4010. }
  4011. #ifndef ARCHIVE_ACL_SUPPORT
  4012. int
  4013. archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
  4014. struct archive_acl *abstract_acl, __LA_MODE_T mode)
  4015. {
  4016. (void)a; /* UNUSED */
  4017. (void)fd; /* UNUSED */
  4018. (void)name; /* UNUSED */
  4019. (void)abstract_acl; /* UNUSED */
  4020. (void)mode; /* UNUSED */
  4021. return (ARCHIVE_OK);
  4022. }
  4023. #endif
  4024. #endif /* !_WIN32 || __CYGWIN__ */