archive_write_disk_posix.c 105 KB

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