archive_write_disk_posix.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824
  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. #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
  2052. && defined(HAVE_ZLIB_H)
  2053. if (a->stream_valid) {
  2054. switch (deflateEnd(&a->stream)) {
  2055. case Z_OK:
  2056. break;
  2057. default:
  2058. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2059. "Failed to clean up compressor");
  2060. ret = ARCHIVE_FATAL;
  2061. break;
  2062. }
  2063. }
  2064. #endif
  2065. free(a);
  2066. return (ret);
  2067. }
  2068. /*
  2069. * Simple O(n log n) merge sort to order the fixup list. In
  2070. * particular, we want to restore dir timestamps depth-first.
  2071. */
  2072. static struct fixup_entry *
  2073. sort_dir_list(struct fixup_entry *p)
  2074. {
  2075. struct fixup_entry *a, *b, *t;
  2076. if (p == NULL)
  2077. return (NULL);
  2078. /* A one-item list is already sorted. */
  2079. if (p->next == NULL)
  2080. return (p);
  2081. /* Step 1: split the list. */
  2082. t = p;
  2083. a = p->next->next;
  2084. while (a != NULL) {
  2085. /* Step a twice, t once. */
  2086. a = a->next;
  2087. if (a != NULL)
  2088. a = a->next;
  2089. t = t->next;
  2090. }
  2091. /* Now, t is at the mid-point, so break the list here. */
  2092. b = t->next;
  2093. t->next = NULL;
  2094. a = p;
  2095. /* Step 2: Recursively sort the two sub-lists. */
  2096. a = sort_dir_list(a);
  2097. b = sort_dir_list(b);
  2098. /* Step 3: Merge the returned lists. */
  2099. /* Pick the first element for the merged list. */
  2100. if (strcmp(a->name, b->name) > 0) {
  2101. t = p = a;
  2102. a = a->next;
  2103. } else {
  2104. t = p = b;
  2105. b = b->next;
  2106. }
  2107. /* Always put the later element on the list first. */
  2108. while (a != NULL && b != NULL) {
  2109. if (strcmp(a->name, b->name) > 0) {
  2110. t->next = a;
  2111. a = a->next;
  2112. } else {
  2113. t->next = b;
  2114. b = b->next;
  2115. }
  2116. t = t->next;
  2117. }
  2118. /* Only one list is non-empty, so just splice it on. */
  2119. if (a != NULL)
  2120. t->next = a;
  2121. if (b != NULL)
  2122. t->next = b;
  2123. return (p);
  2124. }
  2125. /*
  2126. * Returns a new, initialized fixup entry.
  2127. *
  2128. * TODO: Reduce the memory requirements for this list by using a tree
  2129. * structure rather than a simple list of names.
  2130. */
  2131. static struct fixup_entry *
  2132. new_fixup(struct archive_write_disk *a, const char *pathname)
  2133. {
  2134. struct fixup_entry *fe;
  2135. fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
  2136. if (fe == NULL) {
  2137. archive_set_error(&a->archive, ENOMEM,
  2138. "Can't allocate memory for a fixup");
  2139. return (NULL);
  2140. }
  2141. fe->next = a->fixup_list;
  2142. a->fixup_list = fe;
  2143. fe->fixup = 0;
  2144. fe->name = strdup(pathname);
  2145. return (fe);
  2146. }
  2147. /*
  2148. * Returns a fixup structure for the current entry.
  2149. */
  2150. static struct fixup_entry *
  2151. current_fixup(struct archive_write_disk *a, const char *pathname)
  2152. {
  2153. if (a->current_fixup == NULL)
  2154. a->current_fixup = new_fixup(a, pathname);
  2155. return (a->current_fixup);
  2156. }
  2157. /* TODO: Make this work. */
  2158. /*
  2159. * TODO: The deep-directory support bypasses this; disable deep directory
  2160. * support if we're doing symlink checks.
  2161. */
  2162. /*
  2163. * TODO: Someday, integrate this with the deep dir support; they both
  2164. * scan the path and both can be optimized by comparing against other
  2165. * recent paths.
  2166. */
  2167. /* TODO: Extend this to support symlinks on Windows Vista and later. */
  2168. static int
  2169. check_symlinks(struct archive_write_disk *a)
  2170. {
  2171. #if !defined(HAVE_LSTAT)
  2172. /* Platform doesn't have lstat, so we can't look for symlinks. */
  2173. (void)a; /* UNUSED */
  2174. return (ARCHIVE_OK);
  2175. #else
  2176. char *pn;
  2177. char c;
  2178. int r;
  2179. struct stat st;
  2180. /*
  2181. * Guard against symlink tricks. Reject any archive entry whose
  2182. * destination would be altered by a symlink.
  2183. */
  2184. /* Whatever we checked last time doesn't need to be re-checked. */
  2185. pn = a->name;
  2186. if (archive_strlen(&(a->path_safe)) > 0) {
  2187. char *p = a->path_safe.s;
  2188. while ((*pn != '\0') && (*p == *pn))
  2189. ++p, ++pn;
  2190. }
  2191. c = pn[0];
  2192. /* Keep going until we've checked the entire name. */
  2193. while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
  2194. /* Skip the next path element. */
  2195. while (*pn != '\0' && *pn != '/')
  2196. ++pn;
  2197. c = pn[0];
  2198. pn[0] = '\0';
  2199. /* Check that we haven't hit a symlink. */
  2200. r = lstat(a->name, &st);
  2201. if (r != 0) {
  2202. /* We've hit a dir that doesn't exist; stop now. */
  2203. if (errno == ENOENT)
  2204. break;
  2205. } else if (S_ISLNK(st.st_mode)) {
  2206. if (c == '\0') {
  2207. /*
  2208. * Last element is symlink; remove it
  2209. * so we can overwrite it with the
  2210. * item being extracted.
  2211. */
  2212. if (unlink(a->name)) {
  2213. archive_set_error(&a->archive, errno,
  2214. "Could not remove symlink %s",
  2215. a->name);
  2216. pn[0] = c;
  2217. return (ARCHIVE_FAILED);
  2218. }
  2219. a->pst = NULL;
  2220. /*
  2221. * Even if we did remove it, a warning
  2222. * is in order. The warning is silly,
  2223. * though, if we're just replacing one
  2224. * symlink with another symlink.
  2225. */
  2226. if (!S_ISLNK(a->mode)) {
  2227. archive_set_error(&a->archive, 0,
  2228. "Removing symlink %s",
  2229. a->name);
  2230. }
  2231. /* Symlink gone. No more problem! */
  2232. pn[0] = c;
  2233. return (0);
  2234. } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
  2235. /* User asked us to remove problems. */
  2236. if (unlink(a->name) != 0) {
  2237. archive_set_error(&a->archive, 0,
  2238. "Cannot remove intervening symlink %s",
  2239. a->name);
  2240. pn[0] = c;
  2241. return (ARCHIVE_FAILED);
  2242. }
  2243. a->pst = NULL;
  2244. } else {
  2245. archive_set_error(&a->archive, 0,
  2246. "Cannot extract through symlink %s",
  2247. a->name);
  2248. pn[0] = c;
  2249. return (ARCHIVE_FAILED);
  2250. }
  2251. }
  2252. }
  2253. pn[0] = c;
  2254. /* We've checked and/or cleaned the whole path, so remember it. */
  2255. archive_strcpy(&a->path_safe, a->name);
  2256. return (ARCHIVE_OK);
  2257. #endif
  2258. }
  2259. #if defined(__CYGWIN__)
  2260. /*
  2261. * 1. Convert a path separator from '\' to '/' .
  2262. * We shouldn't check multibyte character directly because some
  2263. * character-set have been using the '\' character for a part of
  2264. * its multibyte character code.
  2265. * 2. Replace unusable characters in Windows with underscore('_').
  2266. * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
  2267. */
  2268. static void
  2269. cleanup_pathname_win(struct archive_write_disk *a)
  2270. {
  2271. wchar_t wc;
  2272. char *p;
  2273. size_t alen, l;
  2274. int mb, complete, utf8;
  2275. alen = 0;
  2276. mb = 0;
  2277. complete = 1;
  2278. utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0;
  2279. for (p = a->name; *p != '\0'; p++) {
  2280. ++alen;
  2281. if (*p == '\\') {
  2282. /* If previous byte is smaller than 128,
  2283. * this is not second byte of multibyte characters,
  2284. * so we can replace '\' with '/'. */
  2285. if (utf8 || !mb)
  2286. *p = '/';
  2287. else
  2288. complete = 0;/* uncompleted. */
  2289. } else if (*(unsigned char *)p > 127)
  2290. mb = 1;
  2291. else
  2292. mb = 0;
  2293. /* Rewrite the path name if its next character is unusable. */
  2294. if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
  2295. *p == '<' || *p == '>' || *p == '|')
  2296. *p = '_';
  2297. }
  2298. if (complete)
  2299. return;
  2300. /*
  2301. * Convert path separator in wide-character.
  2302. */
  2303. p = a->name;
  2304. while (*p != '\0' && alen) {
  2305. l = mbtowc(&wc, p, alen);
  2306. if (l == (size_t)-1) {
  2307. while (*p != '\0') {
  2308. if (*p == '\\')
  2309. *p = '/';
  2310. ++p;
  2311. }
  2312. break;
  2313. }
  2314. if (l == 1 && wc == L'\\')
  2315. *p = '/';
  2316. p += l;
  2317. alen -= l;
  2318. }
  2319. }
  2320. #endif
  2321. /*
  2322. * Canonicalize the pathname. In particular, this strips duplicate
  2323. * '/' characters, '.' elements, and trailing '/'. It also raises an
  2324. * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
  2325. * set) any '..' in the path.
  2326. */
  2327. static int
  2328. cleanup_pathname(struct archive_write_disk *a)
  2329. {
  2330. char *dest, *src;
  2331. char separator = '\0';
  2332. dest = src = a->name;
  2333. if (*src == '\0') {
  2334. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2335. "Invalid empty pathname");
  2336. return (ARCHIVE_FAILED);
  2337. }
  2338. #if defined(__CYGWIN__)
  2339. cleanup_pathname_win(a);
  2340. #endif
  2341. /* Skip leading '/'. */
  2342. if (*src == '/')
  2343. separator = *src++;
  2344. /* Scan the pathname one element at a time. */
  2345. for (;;) {
  2346. /* src points to first char after '/' */
  2347. if (src[0] == '\0') {
  2348. break;
  2349. } else if (src[0] == '/') {
  2350. /* Found '//', ignore second one. */
  2351. src++;
  2352. continue;
  2353. } else if (src[0] == '.') {
  2354. if (src[1] == '\0') {
  2355. /* Ignore trailing '.' */
  2356. break;
  2357. } else if (src[1] == '/') {
  2358. /* Skip './'. */
  2359. src += 2;
  2360. continue;
  2361. } else if (src[1] == '.') {
  2362. if (src[2] == '/' || src[2] == '\0') {
  2363. /* Conditionally warn about '..' */
  2364. if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  2365. archive_set_error(&a->archive,
  2366. ARCHIVE_ERRNO_MISC,
  2367. "Path contains '..'");
  2368. return (ARCHIVE_FAILED);
  2369. }
  2370. }
  2371. /*
  2372. * Note: Under no circumstances do we
  2373. * remove '..' elements. In
  2374. * particular, restoring
  2375. * '/foo/../bar/' should create the
  2376. * 'foo' dir as a side-effect.
  2377. */
  2378. }
  2379. }
  2380. /* Copy current element, including leading '/'. */
  2381. if (separator)
  2382. *dest++ = '/';
  2383. while (*src != '\0' && *src != '/') {
  2384. *dest++ = *src++;
  2385. }
  2386. if (*src == '\0')
  2387. break;
  2388. /* Skip '/' separator. */
  2389. separator = *src++;
  2390. }
  2391. /*
  2392. * We've just copied zero or more path elements, not including the
  2393. * final '/'.
  2394. */
  2395. if (dest == a->name) {
  2396. /*
  2397. * Nothing got copied. The path must have been something
  2398. * like '.' or '/' or './' or '/././././/./'.
  2399. */
  2400. if (separator)
  2401. *dest++ = '/';
  2402. else
  2403. *dest++ = '.';
  2404. }
  2405. /* Terminate the result. */
  2406. *dest = '\0';
  2407. return (ARCHIVE_OK);
  2408. }
  2409. /*
  2410. * Create the parent directory of the specified path, assuming path
  2411. * is already in mutable storage.
  2412. */
  2413. static int
  2414. create_parent_dir(struct archive_write_disk *a, char *path)
  2415. {
  2416. char *slash;
  2417. int r;
  2418. /* Remove tail element to obtain parent name. */
  2419. slash = strrchr(path, '/');
  2420. if (slash == NULL)
  2421. return (ARCHIVE_OK);
  2422. *slash = '\0';
  2423. r = create_dir(a, path);
  2424. *slash = '/';
  2425. return (r);
  2426. }
  2427. /*
  2428. * Create the specified dir, recursing to create parents as necessary.
  2429. *
  2430. * Returns ARCHIVE_OK if the path exists when we're done here.
  2431. * Otherwise, returns ARCHIVE_FAILED.
  2432. * Assumes path is in mutable storage; path is unchanged on exit.
  2433. */
  2434. static int
  2435. create_dir(struct archive_write_disk *a, char *path)
  2436. {
  2437. struct stat st;
  2438. struct fixup_entry *le;
  2439. char *slash, *base;
  2440. mode_t mode_final, mode;
  2441. int r;
  2442. /* Check for special names and just skip them. */
  2443. slash = strrchr(path, '/');
  2444. if (slash == NULL)
  2445. base = path;
  2446. else
  2447. base = slash + 1;
  2448. if (base[0] == '\0' ||
  2449. (base[0] == '.' && base[1] == '\0') ||
  2450. (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
  2451. /* Don't bother trying to create null path, '.', or '..'. */
  2452. if (slash != NULL) {
  2453. *slash = '\0';
  2454. r = create_dir(a, path);
  2455. *slash = '/';
  2456. return (r);
  2457. }
  2458. return (ARCHIVE_OK);
  2459. }
  2460. /*
  2461. * Yes, this should be stat() and not lstat(). Using lstat()
  2462. * here loses the ability to extract through symlinks. Also note
  2463. * that this should not use the a->st cache.
  2464. */
  2465. if (stat(path, &st) == 0) {
  2466. if (S_ISDIR(st.st_mode))
  2467. return (ARCHIVE_OK);
  2468. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  2469. archive_set_error(&a->archive, EEXIST,
  2470. "Can't create directory '%s'", path);
  2471. return (ARCHIVE_FAILED);
  2472. }
  2473. if (unlink(path) != 0) {
  2474. archive_set_error(&a->archive, errno,
  2475. "Can't create directory '%s': "
  2476. "Conflicting file cannot be removed",
  2477. path);
  2478. return (ARCHIVE_FAILED);
  2479. }
  2480. } else if (errno != ENOENT && errno != ENOTDIR) {
  2481. /* Stat failed? */
  2482. archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
  2483. return (ARCHIVE_FAILED);
  2484. } else if (slash != NULL) {
  2485. *slash = '\0';
  2486. r = create_dir(a, path);
  2487. *slash = '/';
  2488. if (r != ARCHIVE_OK)
  2489. return (r);
  2490. }
  2491. /*
  2492. * Mode we want for the final restored directory. Per POSIX,
  2493. * implicitly-created dirs must be created obeying the umask.
  2494. * There's no mention whether this is different for privileged
  2495. * restores (which the rest of this code handles by pretending
  2496. * umask=0). I've chosen here to always obey the user's umask for
  2497. * implicit dirs, even if _EXTRACT_PERM was specified.
  2498. */
  2499. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  2500. /* Mode we want on disk during the restore process. */
  2501. mode = mode_final;
  2502. mode |= MINIMUM_DIR_MODE;
  2503. mode &= MAXIMUM_DIR_MODE;
  2504. if (mkdir(path, mode) == 0) {
  2505. if (mode != mode_final) {
  2506. le = new_fixup(a, path);
  2507. if (le == NULL)
  2508. return (ARCHIVE_FATAL);
  2509. le->fixup |=TODO_MODE_BASE;
  2510. le->mode = mode_final;
  2511. }
  2512. return (ARCHIVE_OK);
  2513. }
  2514. /*
  2515. * Without the following check, a/b/../b/c/d fails at the
  2516. * second visit to 'b', so 'd' can't be created. Note that we
  2517. * don't add it to the fixup list here, as it's already been
  2518. * added.
  2519. */
  2520. if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
  2521. return (ARCHIVE_OK);
  2522. archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
  2523. path);
  2524. return (ARCHIVE_FAILED);
  2525. }
  2526. /*
  2527. * Note: Although we can skip setting the user id if the desired user
  2528. * id matches the current user, we cannot skip setting the group, as
  2529. * many systems set the gid based on the containing directory. So
  2530. * we have to perform a chown syscall if we want to set the SGID
  2531. * bit. (The alternative is to stat() and then possibly chown(); it's
  2532. * more efficient to skip the stat() and just always chown().) Note
  2533. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  2534. * allows set_mode to skip the stat() check for the GID.
  2535. */
  2536. static int
  2537. set_ownership(struct archive_write_disk *a)
  2538. {
  2539. #ifndef __CYGWIN__
  2540. /* unfortunately, on win32 there is no 'root' user with uid 0,
  2541. so we just have to try the chown and see if it works */
  2542. /* If we know we can't change it, don't bother trying. */
  2543. if (a->user_uid != 0 && a->user_uid != a->uid) {
  2544. archive_set_error(&a->archive, errno,
  2545. "Can't set UID=%jd", (intmax_t)a->uid);
  2546. return (ARCHIVE_WARN);
  2547. }
  2548. #endif
  2549. #ifdef HAVE_FCHOWN
  2550. /* If we have an fd, we can avoid a race. */
  2551. if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
  2552. /* We've set owner and know uid/gid are correct. */
  2553. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2554. return (ARCHIVE_OK);
  2555. }
  2556. #endif
  2557. /* We prefer lchown() but will use chown() if that's all we have. */
  2558. /* Of course, if we have neither, this will always fail. */
  2559. #ifdef HAVE_LCHOWN
  2560. if (lchown(a->name, a->uid, a->gid) == 0) {
  2561. /* We've set owner and know uid/gid are correct. */
  2562. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2563. return (ARCHIVE_OK);
  2564. }
  2565. #elif HAVE_CHOWN
  2566. if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
  2567. /* We've set owner and know uid/gid are correct. */
  2568. a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
  2569. return (ARCHIVE_OK);
  2570. }
  2571. #endif
  2572. archive_set_error(&a->archive, errno,
  2573. "Can't set user=%jd/group=%jd for %s",
  2574. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  2575. return (ARCHIVE_WARN);
  2576. }
  2577. /*
  2578. * Note: Returns 0 on success, non-zero on failure.
  2579. */
  2580. static int
  2581. set_time(int fd, int mode, const char *name,
  2582. time_t atime, long atime_nsec,
  2583. time_t mtime, long mtime_nsec)
  2584. {
  2585. /* Select the best implementation for this platform. */
  2586. #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
  2587. /*
  2588. * utimensat() and futimens() are defined in
  2589. * POSIX.1-2008. They support ns resolution and setting times
  2590. * on fds and symlinks.
  2591. */
  2592. struct timespec ts[2];
  2593. (void)mode; /* UNUSED */
  2594. ts[0].tv_sec = atime;
  2595. ts[0].tv_nsec = atime_nsec;
  2596. ts[1].tv_sec = mtime;
  2597. ts[1].tv_nsec = mtime_nsec;
  2598. if (fd >= 0)
  2599. return futimens(fd, ts);
  2600. return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
  2601. #elif HAVE_UTIMES
  2602. /*
  2603. * The utimes()-family functions support µs-resolution and
  2604. * setting times fds and symlinks. utimes() is documented as
  2605. * LEGACY by POSIX, futimes() and lutimes() are not described
  2606. * in POSIX.
  2607. */
  2608. struct timeval times[2];
  2609. times[0].tv_sec = atime;
  2610. times[0].tv_usec = atime_nsec / 1000;
  2611. times[1].tv_sec = mtime;
  2612. times[1].tv_usec = mtime_nsec / 1000;
  2613. #ifdef HAVE_FUTIMES
  2614. if (fd >= 0)
  2615. return (futimes(fd, times));
  2616. #else
  2617. (void)fd; /* UNUSED */
  2618. #endif
  2619. #ifdef HAVE_LUTIMES
  2620. (void)mode; /* UNUSED */
  2621. return (lutimes(name, times));
  2622. #else
  2623. if (S_ISLNK(mode))
  2624. return (0);
  2625. return (utimes(name, times));
  2626. #endif
  2627. #elif defined(HAVE_UTIME)
  2628. /*
  2629. * utime() is POSIX-standard but only supports 1s resolution and
  2630. * does not support fds or symlinks.
  2631. */
  2632. struct utimbuf times;
  2633. (void)fd; /* UNUSED */
  2634. (void)name; /* UNUSED */
  2635. (void)atime_nsec; /* UNUSED */
  2636. (void)mtime_nsec; /* UNUSED */
  2637. times.actime = atime;
  2638. times.modtime = mtime;
  2639. if (S_ISLNK(mode))
  2640. return (ARCHIVE_OK);
  2641. return (utime(name, &times));
  2642. #else
  2643. /*
  2644. * We don't know how to set the time on this platform.
  2645. */
  2646. (void)fd; /* UNUSED */
  2647. (void)mode; /* UNUSED */
  2648. (void)name; /* UNUSED */
  2649. (void)atime_nsec; /* UNUSED */
  2650. (void)mtime_nsec; /* UNUSED */
  2651. return (ARCHIVE_WARN);
  2652. #endif
  2653. }
  2654. #ifdef F_SETTIMES
  2655. static int
  2656. set_time_tru64(int fd, int mode, const char *name,
  2657. time_t atime, long atime_nsec,
  2658. time_t mtime, long mtime_nsec,
  2659. time_t ctime, long ctime_nsec)
  2660. {
  2661. struct attr_timbuf tstamp;
  2662. tstamp.atime.tv_sec = atime;
  2663. tstamp.mtime.tv_sec = mtime;
  2664. tstamp.ctime.tv_sec = ctime;
  2665. #if defined (__hpux) && defined (__ia64)
  2666. tstamp.atime.tv_nsec = atime_nsec;
  2667. tstamp.mtime.tv_nsec = mtime_nsec;
  2668. tstamp.ctime.tv_nsec = ctime_nsec;
  2669. #else
  2670. tstamp.atimetv_usec = atime_nsec / 1000;
  2671. tstamp.mtime.tv_usec = mtime_nsec / 1000;
  2672. tstamp.ctime.tv_usec = ctime_nsec / 1000;
  2673. #endif
  2674. return (fcntl(fd,F_SETTIMES,&tstamp));
  2675. }
  2676. #endif /* F_SETTIMES */
  2677. static int
  2678. set_times(struct archive_write_disk *a,
  2679. int fd, int mode, const char *name,
  2680. time_t atime, long atime_nanos,
  2681. time_t birthtime, long birthtime_nanos,
  2682. time_t mtime, long mtime_nanos,
  2683. time_t cctime, long ctime_nanos)
  2684. {
  2685. /* Note: set_time doesn't use libarchive return conventions!
  2686. * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */
  2687. int r1 = 0, r2 = 0;
  2688. #ifdef F_SETTIMES
  2689. /*
  2690. * on Tru64 try own fcntl first which can restore even the
  2691. * ctime, fall back to default code path below if it fails
  2692. * or if we are not running as root
  2693. */
  2694. if (a->user_uid == 0 &&
  2695. set_time_tru64(fd, mode, name,
  2696. atime, atime_nanos, mtime,
  2697. mtime_nanos, cctime, ctime_nanos) == 0) {
  2698. return (ARCHIVE_OK);
  2699. }
  2700. #else /* Tru64 */
  2701. (void)cctime; /* UNUSED */
  2702. (void)ctime_nanos; /* UNUSED */
  2703. #endif /* Tru64 */
  2704. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  2705. /*
  2706. * If you have struct stat.st_birthtime, we assume BSD
  2707. * birthtime semantics, in which {f,l,}utimes() updates
  2708. * birthtime to earliest mtime. So we set the time twice,
  2709. * first using the birthtime, then using the mtime. If
  2710. * birthtime == mtime, this isn't necessary, so we skip it.
  2711. * If birthtime > mtime, then this won't work, so we skip it.
  2712. */
  2713. if (birthtime < mtime
  2714. || (birthtime == mtime && birthtime_nanos < mtime_nanos))
  2715. r1 = set_time(fd, mode, name,
  2716. atime, atime_nanos,
  2717. birthtime, birthtime_nanos);
  2718. #else
  2719. (void)birthtime; /* UNUSED */
  2720. (void)birthtime_nanos; /* UNUSED */
  2721. #endif
  2722. r2 = set_time(fd, mode, name,
  2723. atime, atime_nanos,
  2724. mtime, mtime_nanos);
  2725. if (r1 != 0 || r2 != 0) {
  2726. archive_set_error(&a->archive, errno,
  2727. "Can't restore time");
  2728. return (ARCHIVE_WARN);
  2729. }
  2730. return (ARCHIVE_OK);
  2731. }
  2732. static int
  2733. set_times_from_entry(struct archive_write_disk *a)
  2734. {
  2735. time_t atime, birthtime, mtime, cctime;
  2736. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  2737. /* Suitable defaults. */
  2738. atime = birthtime = mtime = cctime = a->start_time;
  2739. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  2740. /* If no time was provided, we're done. */
  2741. if (!archive_entry_atime_is_set(a->entry)
  2742. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  2743. && !archive_entry_birthtime_is_set(a->entry)
  2744. #endif
  2745. && !archive_entry_mtime_is_set(a->entry))
  2746. return (ARCHIVE_OK);
  2747. if (archive_entry_atime_is_set(a->entry)) {
  2748. atime = archive_entry_atime(a->entry);
  2749. atime_nsec = archive_entry_atime_nsec(a->entry);
  2750. }
  2751. if (archive_entry_birthtime_is_set(a->entry)) {
  2752. birthtime = archive_entry_birthtime(a->entry);
  2753. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  2754. }
  2755. if (archive_entry_mtime_is_set(a->entry)) {
  2756. mtime = archive_entry_mtime(a->entry);
  2757. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  2758. }
  2759. if (archive_entry_ctime_is_set(a->entry)) {
  2760. cctime = archive_entry_ctime(a->entry);
  2761. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  2762. }
  2763. return set_times(a, a->fd, a->mode, a->name,
  2764. atime, atime_nsec,
  2765. birthtime, birthtime_nsec,
  2766. mtime, mtime_nsec,
  2767. cctime, ctime_nsec);
  2768. }
  2769. static int
  2770. set_mode(struct archive_write_disk *a, int mode)
  2771. {
  2772. int r = ARCHIVE_OK;
  2773. mode &= 07777; /* Strip off file type bits. */
  2774. if (a->todo & TODO_SGID_CHECK) {
  2775. /*
  2776. * If we don't know the GID is right, we must stat()
  2777. * to verify it. We can't just check the GID of this
  2778. * process, since systems sometimes set GID from
  2779. * the enclosing dir or based on ACLs.
  2780. */
  2781. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  2782. return (r);
  2783. if (a->pst->st_gid != a->gid) {
  2784. mode &= ~ S_ISGID;
  2785. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  2786. /*
  2787. * This is only an error if you
  2788. * requested owner restore. If you
  2789. * didn't, we'll try to restore
  2790. * sgid/suid, but won't consider it a
  2791. * problem if we can't.
  2792. */
  2793. archive_set_error(&a->archive, -1,
  2794. "Can't restore SGID bit");
  2795. r = ARCHIVE_WARN;
  2796. }
  2797. }
  2798. /* While we're here, double-check the UID. */
  2799. if (a->pst->st_uid != a->uid
  2800. && (a->todo & TODO_SUID)) {
  2801. mode &= ~ S_ISUID;
  2802. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  2803. archive_set_error(&a->archive, -1,
  2804. "Can't restore SUID bit");
  2805. r = ARCHIVE_WARN;
  2806. }
  2807. }
  2808. a->todo &= ~TODO_SGID_CHECK;
  2809. a->todo &= ~TODO_SUID_CHECK;
  2810. } else if (a->todo & TODO_SUID_CHECK) {
  2811. /*
  2812. * If we don't know the UID is right, we can just check
  2813. * the user, since all systems set the file UID from
  2814. * the process UID.
  2815. */
  2816. if (a->user_uid != a->uid) {
  2817. mode &= ~ S_ISUID;
  2818. if (a->flags & ARCHIVE_EXTRACT_OWNER) {
  2819. archive_set_error(&a->archive, -1,
  2820. "Can't make file SUID");
  2821. r = ARCHIVE_WARN;
  2822. }
  2823. }
  2824. a->todo &= ~TODO_SUID_CHECK;
  2825. }
  2826. if (S_ISLNK(a->mode)) {
  2827. #ifdef HAVE_LCHMOD
  2828. /*
  2829. * If this is a symlink, use lchmod(). If the
  2830. * platform doesn't support lchmod(), just skip it. A
  2831. * platform that doesn't provide a way to set
  2832. * permissions on symlinks probably ignores
  2833. * permissions on symlinks, so a failure here has no
  2834. * impact.
  2835. */
  2836. if (lchmod(a->name, mode) != 0) {
  2837. archive_set_error(&a->archive, errno,
  2838. "Can't set permissions to 0%o", (int)mode);
  2839. r = ARCHIVE_WARN;
  2840. }
  2841. #endif
  2842. } else if (!S_ISDIR(a->mode)) {
  2843. /*
  2844. * If it's not a symlink and not a dir, then use
  2845. * fchmod() or chmod(), depending on whether we have
  2846. * an fd. Dirs get their perms set during the
  2847. * post-extract fixup, which is handled elsewhere.
  2848. */
  2849. #ifdef HAVE_FCHMOD
  2850. if (a->fd >= 0) {
  2851. if (fchmod(a->fd, mode) != 0) {
  2852. archive_set_error(&a->archive, errno,
  2853. "Can't set permissions to 0%o", (int)mode);
  2854. r = ARCHIVE_WARN;
  2855. }
  2856. } else
  2857. #endif
  2858. /* If this platform lacks fchmod(), then
  2859. * we'll just use chmod(). */
  2860. if (chmod(a->name, mode) != 0) {
  2861. archive_set_error(&a->archive, errno,
  2862. "Can't set permissions to 0%o", (int)mode);
  2863. r = ARCHIVE_WARN;
  2864. }
  2865. }
  2866. return (r);
  2867. }
  2868. static int
  2869. set_fflags(struct archive_write_disk *a)
  2870. {
  2871. struct fixup_entry *le;
  2872. unsigned long set, clear;
  2873. int r;
  2874. int critical_flags;
  2875. mode_t mode = archive_entry_mode(a->entry);
  2876. /*
  2877. * Make 'critical_flags' hold all file flags that can't be
  2878. * immediately restored. For example, on BSD systems,
  2879. * SF_IMMUTABLE prevents hardlinks from being created, so
  2880. * should not be set until after any hardlinks are created. To
  2881. * preserve some semblance of portability, this uses #ifdef
  2882. * extensively. Ugly, but it works.
  2883. *
  2884. * Yes, Virginia, this does create a security race. It's mitigated
  2885. * somewhat by the practice of creating dirs 0700 until the extract
  2886. * is done, but it would be nice if we could do more than that.
  2887. * People restoring critical file systems should be wary of
  2888. * other programs that might try to muck with files as they're
  2889. * being restored.
  2890. */
  2891. /* Hopefully, the compiler will optimize this mess into a constant. */
  2892. critical_flags = 0;
  2893. #ifdef SF_IMMUTABLE
  2894. critical_flags |= SF_IMMUTABLE;
  2895. #endif
  2896. #ifdef UF_IMMUTABLE
  2897. critical_flags |= UF_IMMUTABLE;
  2898. #endif
  2899. #ifdef SF_APPEND
  2900. critical_flags |= SF_APPEND;
  2901. #endif
  2902. #ifdef UF_APPEND
  2903. critical_flags |= UF_APPEND;
  2904. #endif
  2905. #ifdef EXT2_APPEND_FL
  2906. critical_flags |= EXT2_APPEND_FL;
  2907. #endif
  2908. #ifdef EXT2_IMMUTABLE_FL
  2909. critical_flags |= EXT2_IMMUTABLE_FL;
  2910. #endif
  2911. if (a->todo & TODO_FFLAGS) {
  2912. archive_entry_fflags(a->entry, &set, &clear);
  2913. /*
  2914. * The first test encourages the compiler to eliminate
  2915. * all of this if it's not necessary.
  2916. */
  2917. if ((critical_flags != 0) && (set & critical_flags)) {
  2918. le = current_fixup(a, a->name);
  2919. if (le == NULL)
  2920. return (ARCHIVE_FATAL);
  2921. le->fixup |= TODO_FFLAGS;
  2922. le->fflags_set = set;
  2923. /* Store the mode if it's not already there. */
  2924. if ((le->fixup & TODO_MODE) == 0)
  2925. le->mode = mode;
  2926. } else {
  2927. r = set_fflags_platform(a, a->fd,
  2928. a->name, mode, set, clear);
  2929. if (r != ARCHIVE_OK)
  2930. return (r);
  2931. }
  2932. }
  2933. return (ARCHIVE_OK);
  2934. }
  2935. #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
  2936. /*
  2937. * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
  2938. */
  2939. static int
  2940. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  2941. mode_t mode, unsigned long set, unsigned long clear)
  2942. {
  2943. int r;
  2944. (void)mode; /* UNUSED */
  2945. if (set == 0 && clear == 0)
  2946. return (ARCHIVE_OK);
  2947. /*
  2948. * XXX Is the stat here really necessary? Or can I just use
  2949. * the 'set' flags directly? In particular, I'm not sure
  2950. * about the correct approach if we're overwriting an existing
  2951. * file that already has flags on it. XXX
  2952. */
  2953. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  2954. return (r);
  2955. a->st.st_flags &= ~clear;
  2956. a->st.st_flags |= set;
  2957. #ifdef HAVE_FCHFLAGS
  2958. /* If platform has fchflags() and we were given an fd, use it. */
  2959. if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
  2960. return (ARCHIVE_OK);
  2961. #endif
  2962. /*
  2963. * If we can't use the fd to set the flags, we'll use the
  2964. * pathname to set flags. We prefer lchflags() but will use
  2965. * chflags() if we must.
  2966. */
  2967. #ifdef HAVE_LCHFLAGS
  2968. if (lchflags(name, a->st.st_flags) == 0)
  2969. return (ARCHIVE_OK);
  2970. #elif defined(HAVE_CHFLAGS)
  2971. if (S_ISLNK(a->st.st_mode)) {
  2972. archive_set_error(&a->archive, errno,
  2973. "Can't set file flags on symlink.");
  2974. return (ARCHIVE_WARN);
  2975. }
  2976. if (chflags(name, a->st.st_flags) == 0)
  2977. return (ARCHIVE_OK);
  2978. #endif
  2979. archive_set_error(&a->archive, errno,
  2980. "Failed to set file flags");
  2981. return (ARCHIVE_WARN);
  2982. }
  2983. #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)
  2984. /*
  2985. * Linux uses ioctl() to read and write file flags.
  2986. */
  2987. static int
  2988. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  2989. mode_t mode, unsigned long set, unsigned long clear)
  2990. {
  2991. int ret;
  2992. int myfd = fd;
  2993. int newflags, oldflags;
  2994. int sf_mask = 0;
  2995. if (set == 0 && clear == 0)
  2996. return (ARCHIVE_OK);
  2997. /* Only regular files and dirs can have flags. */
  2998. if (!S_ISREG(mode) && !S_ISDIR(mode))
  2999. return (ARCHIVE_OK);
  3000. /* If we weren't given an fd, open it ourselves. */
  3001. if (myfd < 0) {
  3002. myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY | O_CLOEXEC);
  3003. __archive_ensure_cloexec_flag(myfd);
  3004. }
  3005. if (myfd < 0)
  3006. return (ARCHIVE_OK);
  3007. /*
  3008. * Linux has no define for the flags that are only settable by
  3009. * the root user. This code may seem a little complex, but
  3010. * there seem to be some Linux systems that lack these
  3011. * defines. (?) The code below degrades reasonably gracefully
  3012. * if sf_mask is incomplete.
  3013. */
  3014. #ifdef EXT2_IMMUTABLE_FL
  3015. sf_mask |= EXT2_IMMUTABLE_FL;
  3016. #endif
  3017. #ifdef EXT2_APPEND_FL
  3018. sf_mask |= EXT2_APPEND_FL;
  3019. #endif
  3020. /*
  3021. * XXX As above, this would be way simpler if we didn't have
  3022. * to read the current flags from disk. XXX
  3023. */
  3024. ret = ARCHIVE_OK;
  3025. /* Read the current file flags. */
  3026. if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) < 0)
  3027. goto fail;
  3028. /* Try setting the flags as given. */
  3029. newflags = (oldflags & ~clear) | set;
  3030. if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
  3031. goto cleanup;
  3032. if (errno != EPERM)
  3033. goto fail;
  3034. /* If we couldn't set all the flags, try again with a subset. */
  3035. newflags &= ~sf_mask;
  3036. oldflags &= sf_mask;
  3037. newflags |= oldflags;
  3038. if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
  3039. goto cleanup;
  3040. /* We couldn't set the flags, so report the failure. */
  3041. fail:
  3042. archive_set_error(&a->archive, errno,
  3043. "Failed to set file flags");
  3044. ret = ARCHIVE_WARN;
  3045. cleanup:
  3046. if (fd < 0)
  3047. close(myfd);
  3048. return (ret);
  3049. }
  3050. #else
  3051. /*
  3052. * Of course, some systems have neither BSD chflags() nor Linux' flags
  3053. * support through ioctl().
  3054. */
  3055. static int
  3056. set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
  3057. mode_t mode, unsigned long set, unsigned long clear)
  3058. {
  3059. (void)a; /* UNUSED */
  3060. (void)fd; /* UNUSED */
  3061. (void)name; /* UNUSED */
  3062. (void)mode; /* UNUSED */
  3063. (void)set; /* UNUSED */
  3064. (void)clear; /* UNUSED */
  3065. return (ARCHIVE_OK);
  3066. }
  3067. #endif /* __linux */
  3068. #ifndef HAVE_COPYFILE_H
  3069. /* Default is to simply drop Mac extended metadata. */
  3070. static int
  3071. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3072. const void *metadata, size_t metadata_size)
  3073. {
  3074. (void)a; /* UNUSED */
  3075. (void)pathname; /* UNUSED */
  3076. (void)metadata; /* UNUSED */
  3077. (void)metadata_size; /* UNUSED */
  3078. return (ARCHIVE_OK);
  3079. }
  3080. static int
  3081. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3082. {
  3083. (void)a; /* UNUSED */
  3084. (void)pathname; /* UNUSED */
  3085. return (ARCHIVE_OK);
  3086. }
  3087. #else
  3088. /*
  3089. * On Mac OS, we use copyfile() to unpack the metadata and
  3090. * apply it to the target file.
  3091. */
  3092. #if defined(HAVE_SYS_XATTR_H)
  3093. static int
  3094. copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd)
  3095. {
  3096. ssize_t xattr_size;
  3097. char *xattr_names = NULL, *xattr_val = NULL;
  3098. int ret = ARCHIVE_OK, xattr_i;
  3099. xattr_size = flistxattr(tmpfd, NULL, 0, 0);
  3100. if (xattr_size == -1) {
  3101. archive_set_error(&a->archive, errno,
  3102. "Failed to read metadata(xattr)");
  3103. ret = ARCHIVE_WARN;
  3104. goto exit_xattr;
  3105. }
  3106. xattr_names = malloc(xattr_size);
  3107. if (xattr_names == NULL) {
  3108. archive_set_error(&a->archive, ENOMEM,
  3109. "Can't allocate memory for metadata(xattr)");
  3110. ret = ARCHIVE_FATAL;
  3111. goto exit_xattr;
  3112. }
  3113. xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0);
  3114. if (xattr_size == -1) {
  3115. archive_set_error(&a->archive, errno,
  3116. "Failed to read metadata(xattr)");
  3117. ret = ARCHIVE_WARN;
  3118. goto exit_xattr;
  3119. }
  3120. for (xattr_i = 0; xattr_i < xattr_size;
  3121. xattr_i += strlen(xattr_names + xattr_i) + 1) {
  3122. ssize_t s;
  3123. int f;
  3124. s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0);
  3125. if (s == -1) {
  3126. archive_set_error(&a->archive, errno,
  3127. "Failed to get metadata(xattr)");
  3128. ret = ARCHIVE_WARN;
  3129. goto exit_xattr;
  3130. }
  3131. xattr_val = realloc(xattr_val, s);
  3132. if (xattr_val == NULL) {
  3133. archive_set_error(&a->archive, ENOMEM,
  3134. "Failed to get metadata(xattr)");
  3135. ret = ARCHIVE_WARN;
  3136. goto exit_xattr;
  3137. }
  3138. s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3139. if (s == -1) {
  3140. archive_set_error(&a->archive, errno,
  3141. "Failed to get metadata(xattr)");
  3142. ret = ARCHIVE_WARN;
  3143. goto exit_xattr;
  3144. }
  3145. f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0);
  3146. if (f == -1) {
  3147. archive_set_error(&a->archive, errno,
  3148. "Failed to get metadata(xattr)");
  3149. ret = ARCHIVE_WARN;
  3150. goto exit_xattr;
  3151. }
  3152. }
  3153. exit_xattr:
  3154. free(xattr_names);
  3155. free(xattr_val);
  3156. return (ret);
  3157. }
  3158. #endif
  3159. static int
  3160. copy_acls(struct archive_write_disk *a, int tmpfd, int dffd)
  3161. {
  3162. acl_t acl, dfacl = NULL;
  3163. int acl_r, ret = ARCHIVE_OK;
  3164. acl = acl_get_fd(tmpfd);
  3165. if (acl == NULL) {
  3166. if (errno == ENOENT)
  3167. /* There are not any ACLs. */
  3168. return (ret);
  3169. archive_set_error(&a->archive, errno,
  3170. "Failed to get metadata(acl)");
  3171. ret = ARCHIVE_WARN;
  3172. goto exit_acl;
  3173. }
  3174. dfacl = acl_dup(acl);
  3175. acl_r = acl_set_fd(dffd, dfacl);
  3176. if (acl_r == -1) {
  3177. archive_set_error(&a->archive, errno,
  3178. "Failed to get metadata(acl)");
  3179. ret = ARCHIVE_WARN;
  3180. goto exit_acl;
  3181. }
  3182. exit_acl:
  3183. if (acl)
  3184. acl_free(acl);
  3185. if (dfacl)
  3186. acl_free(dfacl);
  3187. return (ret);
  3188. }
  3189. static int
  3190. create_tempdatafork(struct archive_write_disk *a, const char *pathname)
  3191. {
  3192. struct archive_string tmpdatafork;
  3193. int tmpfd;
  3194. archive_string_init(&tmpdatafork);
  3195. archive_strcpy(&tmpdatafork, "tar.md.XXXXXX");
  3196. tmpfd = mkstemp(tmpdatafork.s);
  3197. if (tmpfd < 0) {
  3198. archive_set_error(&a->archive, errno,
  3199. "Failed to mkstemp");
  3200. archive_string_free(&tmpdatafork);
  3201. return (-1);
  3202. }
  3203. if (copyfile(pathname, tmpdatafork.s, 0,
  3204. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3205. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3206. archive_set_error(&a->archive, errno,
  3207. "Failed to restore metadata");
  3208. close(tmpfd);
  3209. tmpfd = -1;
  3210. }
  3211. unlink(tmpdatafork.s);
  3212. archive_string_free(&tmpdatafork);
  3213. return (tmpfd);
  3214. }
  3215. static int
  3216. copy_metadata(struct archive_write_disk *a, const char *metadata,
  3217. const char *datafork, int datafork_compressed)
  3218. {
  3219. int ret = ARCHIVE_OK;
  3220. if (datafork_compressed) {
  3221. int dffd, tmpfd;
  3222. tmpfd = create_tempdatafork(a, metadata);
  3223. if (tmpfd == -1)
  3224. return (ARCHIVE_WARN);
  3225. /*
  3226. * Do not open the data fork compressed by HFS+ compression
  3227. * with at least a writing mode(O_RDWR or O_WRONLY). it
  3228. * makes the data fork uncompressed.
  3229. */
  3230. dffd = open(datafork, 0);
  3231. if (dffd == -1) {
  3232. archive_set_error(&a->archive, errno,
  3233. "Failed to open the data fork for metadata");
  3234. close(tmpfd);
  3235. return (ARCHIVE_WARN);
  3236. }
  3237. #if defined(HAVE_SYS_XATTR_H)
  3238. ret = copy_xattrs(a, tmpfd, dffd);
  3239. if (ret == ARCHIVE_OK)
  3240. #endif
  3241. ret = copy_acls(a, tmpfd, dffd);
  3242. close(tmpfd);
  3243. close(dffd);
  3244. } else {
  3245. if (copyfile(metadata, datafork, 0,
  3246. COPYFILE_UNPACK | COPYFILE_NOFOLLOW
  3247. | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
  3248. archive_set_error(&a->archive, errno,
  3249. "Failed to restore metadata");
  3250. ret = ARCHIVE_WARN;
  3251. }
  3252. }
  3253. return (ret);
  3254. }
  3255. static int
  3256. set_mac_metadata(struct archive_write_disk *a, const char *pathname,
  3257. const void *metadata, size_t metadata_size)
  3258. {
  3259. struct archive_string tmp;
  3260. ssize_t written;
  3261. int fd;
  3262. int ret = ARCHIVE_OK;
  3263. /* This would be simpler if copyfile() could just accept the
  3264. * metadata as a block of memory; then we could sidestep this
  3265. * silly dance of writing the data to disk just so that
  3266. * copyfile() can read it back in again. */
  3267. archive_string_init(&tmp);
  3268. archive_strcpy(&tmp, pathname);
  3269. archive_strcat(&tmp, ".XXXXXX");
  3270. fd = mkstemp(tmp.s);
  3271. if (fd < 0) {
  3272. archive_set_error(&a->archive, errno,
  3273. "Failed to restore metadata");
  3274. archive_string_free(&tmp);
  3275. return (ARCHIVE_WARN);
  3276. }
  3277. written = write(fd, metadata, metadata_size);
  3278. close(fd);
  3279. if ((size_t)written != metadata_size) {
  3280. archive_set_error(&a->archive, errno,
  3281. "Failed to restore metadata");
  3282. ret = ARCHIVE_WARN;
  3283. } else {
  3284. int compressed;
  3285. #if defined(UF_COMPRESSED)
  3286. if ((a->todo & TODO_HFS_COMPRESSION) != 0 &&
  3287. (ret = lazy_stat(a)) == ARCHIVE_OK)
  3288. compressed = a->st.st_flags & UF_COMPRESSED;
  3289. else
  3290. #endif
  3291. compressed = 0;
  3292. ret = copy_metadata(a, tmp.s, pathname, compressed);
  3293. }
  3294. unlink(tmp.s);
  3295. archive_string_free(&tmp);
  3296. return (ret);
  3297. }
  3298. static int
  3299. fixup_appledouble(struct archive_write_disk *a, const char *pathname)
  3300. {
  3301. char buff[8];
  3302. struct stat st;
  3303. const char *p;
  3304. struct archive_string datafork;
  3305. int fd = -1, ret = ARCHIVE_OK;
  3306. archive_string_init(&datafork);
  3307. /* Check if the current file name is a type of the resource
  3308. * fork file. */
  3309. p = strrchr(pathname, '/');
  3310. if (p == NULL)
  3311. p = pathname;
  3312. else
  3313. p++;
  3314. if (p[0] != '.' || p[1] != '_')
  3315. goto skip_appledouble;
  3316. /*
  3317. * Check if the data fork file exists.
  3318. *
  3319. * TODO: Check if this write disk object has handled it.
  3320. */
  3321. archive_strncpy(&datafork, pathname, p - pathname);
  3322. archive_strcat(&datafork, p + 2);
  3323. if (lstat(datafork.s, &st) == -1 ||
  3324. (st.st_mode & AE_IFMT) != AE_IFREG)
  3325. goto skip_appledouble;
  3326. /*
  3327. * Check if the file is in the AppleDouble form.
  3328. */
  3329. fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC);
  3330. __archive_ensure_cloexec_flag(fd);
  3331. if (fd == -1) {
  3332. archive_set_error(&a->archive, errno,
  3333. "Failed to open a restoring file");
  3334. ret = ARCHIVE_WARN;
  3335. goto skip_appledouble;
  3336. }
  3337. if (read(fd, buff, 8) == -1) {
  3338. archive_set_error(&a->archive, errno,
  3339. "Failed to read a restoring file");
  3340. close(fd);
  3341. ret = ARCHIVE_WARN;
  3342. goto skip_appledouble;
  3343. }
  3344. close(fd);
  3345. /* Check AppleDouble Magic Code. */
  3346. if (archive_be32dec(buff) != 0x00051607)
  3347. goto skip_appledouble;
  3348. /* Check AppleDouble Version. */
  3349. if (archive_be32dec(buff+4) != 0x00020000)
  3350. goto skip_appledouble;
  3351. ret = copy_metadata(a, pathname, datafork.s,
  3352. #if defined(UF_COMPRESSED)
  3353. st.st_flags & UF_COMPRESSED);
  3354. #else
  3355. 0);
  3356. #endif
  3357. if (ret == ARCHIVE_OK) {
  3358. unlink(pathname);
  3359. ret = ARCHIVE_EOF;
  3360. }
  3361. skip_appledouble:
  3362. archive_string_free(&datafork);
  3363. return (ret);
  3364. }
  3365. #endif
  3366. #if HAVE_LSETXATTR || HAVE_LSETEA
  3367. /*
  3368. * Restore extended attributes - Linux and AIX implementations:
  3369. * AIX' ea interface is syntaxwise identical to the Linux xattr interface.
  3370. */
  3371. static int
  3372. set_xattrs(struct archive_write_disk *a)
  3373. {
  3374. struct archive_entry *entry = a->entry;
  3375. static int warning_done = 0;
  3376. int ret = ARCHIVE_OK;
  3377. int i = archive_entry_xattr_reset(entry);
  3378. while (i--) {
  3379. const char *name;
  3380. const void *value;
  3381. size_t size;
  3382. archive_entry_xattr_next(entry, &name, &value, &size);
  3383. if (name != NULL &&
  3384. strncmp(name, "xfsroot.", 8) != 0 &&
  3385. strncmp(name, "system.", 7) != 0) {
  3386. int e;
  3387. #if HAVE_FSETXATTR
  3388. if (a->fd >= 0)
  3389. e = fsetxattr(a->fd, name, value, size, 0);
  3390. else
  3391. #elif HAVE_FSETEA
  3392. if (a->fd >= 0)
  3393. e = fsetea(a->fd, name, value, size, 0);
  3394. else
  3395. #endif
  3396. {
  3397. #if HAVE_LSETXATTR
  3398. e = lsetxattr(archive_entry_pathname(entry),
  3399. name, value, size, 0);
  3400. #elif HAVE_LSETEA
  3401. e = lsetea(archive_entry_pathname(entry),
  3402. name, value, size, 0);
  3403. #endif
  3404. }
  3405. if (e == -1) {
  3406. if (errno == ENOTSUP || errno == ENOSYS) {
  3407. if (!warning_done) {
  3408. warning_done = 1;
  3409. archive_set_error(&a->archive, errno,
  3410. "Cannot restore extended "
  3411. "attributes on this file "
  3412. "system");
  3413. }
  3414. } else
  3415. archive_set_error(&a->archive, errno,
  3416. "Failed to set extended attribute");
  3417. ret = ARCHIVE_WARN;
  3418. }
  3419. } else {
  3420. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3421. "Invalid extended attribute encountered");
  3422. ret = ARCHIVE_WARN;
  3423. }
  3424. }
  3425. return (ret);
  3426. }
  3427. #elif HAVE_EXTATTR_SET_FILE && HAVE_DECL_EXTATTR_NAMESPACE_USER
  3428. /*
  3429. * Restore extended attributes - FreeBSD implementation
  3430. */
  3431. static int
  3432. set_xattrs(struct archive_write_disk *a)
  3433. {
  3434. struct archive_entry *entry = a->entry;
  3435. static int warning_done = 0;
  3436. int ret = ARCHIVE_OK;
  3437. int i = archive_entry_xattr_reset(entry);
  3438. while (i--) {
  3439. const char *name;
  3440. const void *value;
  3441. size_t size;
  3442. archive_entry_xattr_next(entry, &name, &value, &size);
  3443. if (name != NULL) {
  3444. int e;
  3445. int namespace;
  3446. if (strncmp(name, "user.", 5) == 0) {
  3447. /* "user." attributes go to user namespace */
  3448. name += 5;
  3449. namespace = EXTATTR_NAMESPACE_USER;
  3450. } else {
  3451. /* Warn about other extended attributes. */
  3452. archive_set_error(&a->archive,
  3453. ARCHIVE_ERRNO_FILE_FORMAT,
  3454. "Can't restore extended attribute ``%s''",
  3455. name);
  3456. ret = ARCHIVE_WARN;
  3457. continue;
  3458. }
  3459. errno = 0;
  3460. #if HAVE_EXTATTR_SET_FD
  3461. if (a->fd >= 0)
  3462. e = extattr_set_fd(a->fd, namespace, name, value, size);
  3463. else
  3464. #endif
  3465. /* TODO: should we use extattr_set_link() instead? */
  3466. {
  3467. e = extattr_set_file(archive_entry_pathname(entry),
  3468. namespace, name, value, size);
  3469. }
  3470. if (e != (int)size) {
  3471. if (errno == ENOTSUP || errno == ENOSYS) {
  3472. if (!warning_done) {
  3473. warning_done = 1;
  3474. archive_set_error(&a->archive, errno,
  3475. "Cannot restore extended "
  3476. "attributes on this file "
  3477. "system");
  3478. }
  3479. } else {
  3480. archive_set_error(&a->archive, errno,
  3481. "Failed to set extended attribute");
  3482. }
  3483. ret = ARCHIVE_WARN;
  3484. }
  3485. }
  3486. }
  3487. return (ret);
  3488. }
  3489. #else
  3490. /*
  3491. * Restore extended attributes - stub implementation for unsupported systems
  3492. */
  3493. static int
  3494. set_xattrs(struct archive_write_disk *a)
  3495. {
  3496. static int warning_done = 0;
  3497. /* If there aren't any extended attributes, then it's okay not
  3498. * to extract them, otherwise, issue a single warning. */
  3499. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  3500. warning_done = 1;
  3501. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  3502. "Cannot restore extended attributes on this system");
  3503. return (ARCHIVE_WARN);
  3504. }
  3505. /* Warning was already emitted; suppress further warnings. */
  3506. return (ARCHIVE_OK);
  3507. }
  3508. #endif
  3509. /*
  3510. * Test if file on disk is older than entry.
  3511. */
  3512. static int
  3513. older(struct stat *st, struct archive_entry *entry)
  3514. {
  3515. /* First, test the seconds and return if we have a definite answer. */
  3516. /* Definitely older. */
  3517. if (st->st_mtime < archive_entry_mtime(entry))
  3518. return (1);
  3519. /* Definitely younger. */
  3520. if (st->st_mtime > archive_entry_mtime(entry))
  3521. return (0);
  3522. /* If this platform supports fractional seconds, try those. */
  3523. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  3524. /* Definitely older. */
  3525. if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
  3526. return (1);
  3527. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  3528. /* Definitely older. */
  3529. if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
  3530. return (1);
  3531. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  3532. /* older. */
  3533. if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
  3534. return (1);
  3535. #elif HAVE_STRUCT_STAT_ST_UMTIME
  3536. /* older. */
  3537. if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
  3538. return (1);
  3539. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  3540. /* older. */
  3541. if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
  3542. return (1);
  3543. #else
  3544. /* This system doesn't have high-res timestamps. */
  3545. #endif
  3546. /* Same age or newer, so not older. */
  3547. return (0);
  3548. }
  3549. #endif /* !_WIN32 || __CYGWIN__ */