archive_write_disk_windows.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921
  1. /*-
  2. * Copyright (c) 2003-2010 Tim Kientzle
  3. * Copyright (c) 2011-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. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. #ifdef HAVE_SYS_TYPES_H
  30. #include <sys/types.h>
  31. #endif
  32. #ifdef HAVE_SYS_UTIME_H
  33. #include <sys/utime.h>
  34. #endif
  35. #ifdef HAVE_ERRNO_H
  36. #include <errno.h>
  37. #endif
  38. #ifdef HAVE_FCNTL_H
  39. #include <fcntl.h>
  40. #endif
  41. #ifdef HAVE_LIMITS_H
  42. #include <limits.h>
  43. #endif
  44. #ifdef HAVE_STDLIB_H
  45. #include <stdlib.h>
  46. #endif
  47. #include <winioctl.h>
  48. /* TODO: Support Mac OS 'quarantine' feature. This is really just a
  49. * standard tag to mark files that have been downloaded as "tainted".
  50. * On Mac OS, we should mark the extracted files as tainted if the
  51. * archive being read was tainted. Windows has a similar feature; we
  52. * should investigate ways to support this generically. */
  53. #include "archive.h"
  54. #include "archive_acl_private.h"
  55. #include "archive_string.h"
  56. #include "archive_entry.h"
  57. #include "archive_private.h"
  58. #include "archive_time_private.h"
  59. #ifndef O_BINARY
  60. #define O_BINARY 0
  61. #endif
  62. #ifndef IO_REPARSE_TAG_SYMLINK
  63. /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
  64. #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
  65. #endif
  66. static BOOL SetFilePointerEx_perso(HANDLE hFile,
  67. LARGE_INTEGER liDistanceToMove,
  68. PLARGE_INTEGER lpNewFilePointer,
  69. DWORD dwMoveMethod)
  70. {
  71. LARGE_INTEGER li;
  72. li.QuadPart = liDistanceToMove.QuadPart;
  73. li.LowPart = SetFilePointer(
  74. hFile, li.LowPart, &li.HighPart, dwMoveMethod);
  75. if(lpNewFilePointer) {
  76. lpNewFilePointer->QuadPart = li.QuadPart;
  77. }
  78. return li.LowPart != (DWORD)-1 || GetLastError() == NO_ERROR;
  79. }
  80. struct fixup_entry {
  81. struct fixup_entry *next;
  82. struct archive_acl acl;
  83. mode_t mode;
  84. int64_t atime;
  85. int64_t birthtime;
  86. int64_t mtime;
  87. int64_t ctime;
  88. unsigned long atime_nanos;
  89. unsigned long birthtime_nanos;
  90. unsigned long mtime_nanos;
  91. unsigned long ctime_nanos;
  92. unsigned long fflags_set;
  93. int fixup; /* bitmask of what needs fixing */
  94. wchar_t *name;
  95. };
  96. /*
  97. * We use a bitmask to track which operations remain to be done for
  98. * this file. In particular, this helps us avoid unnecessary
  99. * operations when it's possible to take care of one step as a
  100. * side-effect of another. For example, mkdir() can specify the mode
  101. * for the newly-created object but symlink() cannot. This means we
  102. * can skip chmod() if mkdir() succeeded, but we must explicitly
  103. * chmod() if we're trying to create a directory that already exists
  104. * (mkdir() failed) or if we're restoring a symlink. Similarly, we
  105. * need to verify UID/GID before trying to restore SUID/SGID bits;
  106. * that verification can occur explicitly through a stat() call or
  107. * implicitly because of a successful chown() call.
  108. */
  109. #define TODO_MODE_FORCE 0x40000000
  110. #define TODO_MODE_BASE 0x20000000
  111. #define TODO_SUID 0x10000000
  112. #define TODO_SUID_CHECK 0x08000000
  113. #define TODO_SGID 0x04000000
  114. #define TODO_SGID_CHECK 0x02000000
  115. #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
  116. #define TODO_TIMES ARCHIVE_EXTRACT_TIME
  117. #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
  118. #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
  119. #define TODO_ACLS ARCHIVE_EXTRACT_ACL
  120. #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
  121. #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
  122. struct archive_write_disk {
  123. struct archive archive;
  124. mode_t user_umask;
  125. struct fixup_entry *fixup_list;
  126. struct fixup_entry *current_fixup;
  127. int64_t user_uid;
  128. int skip_file_set;
  129. int64_t skip_file_dev;
  130. int64_t skip_file_ino;
  131. time_t start_time;
  132. int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
  133. void (*cleanup_gid)(void *private);
  134. void *lookup_gid_data;
  135. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
  136. void (*cleanup_uid)(void *private);
  137. void *lookup_uid_data;
  138. /*
  139. * Full path of last file to satisfy symlink checks.
  140. */
  141. struct archive_wstring path_safe;
  142. /*
  143. * Cached stat data from disk for the current entry.
  144. * If this is valid, pst points to st. Otherwise,
  145. * pst is null.
  146. */
  147. BY_HANDLE_FILE_INFORMATION st;
  148. BY_HANDLE_FILE_INFORMATION *pst;
  149. /* Information about the object being restored right now. */
  150. struct archive_entry *entry; /* Entry being extracted. */
  151. wchar_t *name; /* Name of entry, possibly edited. */
  152. struct archive_wstring _name_data; /* backing store for 'name' */
  153. wchar_t *tmpname; /* Temporary name */
  154. struct archive_wstring _tmpname_data; /* backing store for 'tmpname' */
  155. /* Tasks remaining for this object. */
  156. int todo;
  157. /* Tasks deferred until end-of-archive. */
  158. int deferred;
  159. /* Options requested by the client. */
  160. int flags;
  161. /* Handle for the file we're restoring. */
  162. HANDLE fh;
  163. /* Current offset for writing data to the file. */
  164. int64_t offset;
  165. /* Last offset actually written to disk. */
  166. int64_t fd_offset;
  167. /* Total bytes actually written to files. */
  168. int64_t total_bytes_written;
  169. /* Maximum size of file, -1 if unknown. */
  170. int64_t filesize;
  171. /* Dir we were in before this restore; only for deep paths. */
  172. int restore_pwd;
  173. /* Mode we should use for this entry; affected by _PERM and umask. */
  174. mode_t mode;
  175. /* UID/GID to use in restoring this entry. */
  176. int64_t uid;
  177. int64_t gid;
  178. };
  179. /*
  180. * Default mode for dirs created automatically (will be modified by umask).
  181. * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
  182. * by the process' file creation mask."
  183. */
  184. #define DEFAULT_DIR_MODE 0777
  185. /*
  186. * Dir modes are restored in two steps: During the extraction, the permissions
  187. * in the archive are modified to match the following limits. During
  188. * the post-extract fixup pass, the permissions from the archive are
  189. * applied.
  190. */
  191. #define MINIMUM_DIR_MODE 0700
  192. #define MAXIMUM_DIR_MODE 0775
  193. static int disk_unlink(const wchar_t *);
  194. static int disk_rmdir(const wchar_t *);
  195. static int check_symlinks(struct archive_write_disk *);
  196. static int create_filesystem_object(struct archive_write_disk *);
  197. static struct fixup_entry *current_fixup(struct archive_write_disk *,
  198. const wchar_t *pathname);
  199. static int cleanup_pathname(struct archive_write_disk *, wchar_t *);
  200. static int create_dir(struct archive_write_disk *, wchar_t *);
  201. static int create_parent_dir(struct archive_write_disk *, wchar_t *);
  202. static int la_chmod(const wchar_t *, mode_t);
  203. static int la_mktemp(struct archive_write_disk *);
  204. static int older(BY_HANDLE_FILE_INFORMATION *, struct archive_entry *);
  205. static int permissive_name_w(struct archive_write_disk *);
  206. static int restore_entry(struct archive_write_disk *);
  207. static int set_acls(struct archive_write_disk *, HANDLE h,
  208. const wchar_t *, struct archive_acl *);
  209. static int set_xattrs(struct archive_write_disk *);
  210. static int clear_nochange_fflags(struct archive_write_disk *);
  211. static int set_fflags(struct archive_write_disk *);
  212. static int set_fflags_platform(const wchar_t *, unsigned long,
  213. unsigned long);
  214. static int set_ownership(struct archive_write_disk *);
  215. static int set_mode(struct archive_write_disk *, int mode);
  216. static int set_times(struct archive_write_disk *, HANDLE, int,
  217. const wchar_t *, time_t, long, time_t, long, time_t,
  218. long, time_t, long);
  219. static int set_times_from_entry(struct archive_write_disk *);
  220. static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
  221. static ssize_t write_data_block(struct archive_write_disk *,
  222. const char *, size_t);
  223. static int _archive_write_disk_close(struct archive *);
  224. static int _archive_write_disk_free(struct archive *);
  225. static int _archive_write_disk_header(struct archive *,
  226. struct archive_entry *);
  227. static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
  228. static int _archive_write_disk_finish_entry(struct archive *);
  229. static ssize_t _archive_write_disk_data(struct archive *, const void *,
  230. size_t);
  231. static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
  232. size_t, int64_t);
  233. #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
  234. /* Treat FileIndex as i-node. We should remove a sequence number
  235. * which is high-16-bits of nFileIndexHigh. */
  236. #define bhfi_ino(bhfi) \
  237. ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
  238. | (bhfi)->nFileIndexLow)
  239. #define bhfi_size(bhfi) \
  240. ((((int64_t)(bhfi)->nFileSizeHigh) << 32) | (bhfi)->nFileSizeLow)
  241. static int
  242. file_information(struct archive_write_disk *a, wchar_t *path,
  243. BY_HANDLE_FILE_INFORMATION *st, mode_t *mode, int sim_lstat)
  244. {
  245. HANDLE h;
  246. int r;
  247. DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
  248. WIN32_FIND_DATAW findData;
  249. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  250. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  251. #endif
  252. if (sim_lstat || mode != NULL) {
  253. h = FindFirstFileW(path, &findData);
  254. if (h == INVALID_HANDLE_VALUE &&
  255. GetLastError() == ERROR_INVALID_NAME) {
  256. wchar_t *full;
  257. full = __la_win_permissive_name_w(path);
  258. h = FindFirstFileW(full, &findData);
  259. free(full);
  260. }
  261. if (h == INVALID_HANDLE_VALUE) {
  262. la_dosmaperr(GetLastError());
  263. return (-1);
  264. }
  265. FindClose(h);
  266. }
  267. /* Is symlink file ? */
  268. if (sim_lstat &&
  269. ((findData.dwFileAttributes
  270. & FILE_ATTRIBUTE_REPARSE_POINT) &&
  271. (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)))
  272. flag |= FILE_FLAG_OPEN_REPARSE_POINT;
  273. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  274. ZeroMemory(&createExParams, sizeof(createExParams));
  275. createExParams.dwSize = sizeof(createExParams);
  276. createExParams.dwFileFlags = flag;
  277. h = CreateFile2(a->name, 0, 0,
  278. OPEN_EXISTING, &createExParams);
  279. #else
  280. h = CreateFileW(a->name, 0, 0, NULL,
  281. OPEN_EXISTING, flag, NULL);
  282. #endif
  283. if (h == INVALID_HANDLE_VALUE &&
  284. GetLastError() == ERROR_INVALID_NAME) {
  285. wchar_t *full;
  286. full = __la_win_permissive_name_w(path);
  287. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  288. h = CreateFile2(full, 0, 0,
  289. OPEN_EXISTING, &createExParams);
  290. #else
  291. h = CreateFileW(full, 0, 0, NULL,
  292. OPEN_EXISTING, flag, NULL);
  293. #endif
  294. free(full);
  295. }
  296. if (h == INVALID_HANDLE_VALUE) {
  297. la_dosmaperr(GetLastError());
  298. return (-1);
  299. }
  300. r = GetFileInformationByHandle(h, st);
  301. CloseHandle(h);
  302. if (r == 0) {
  303. la_dosmaperr(GetLastError());
  304. return (-1);
  305. }
  306. if (mode == NULL)
  307. return (0);
  308. *mode = S_IRUSR | S_IRGRP | S_IROTH;
  309. if ((st->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
  310. *mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  311. if ((st->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  312. findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
  313. *mode |= S_IFLNK;
  314. else if (st->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  315. *mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  316. else {
  317. const wchar_t *p;
  318. *mode |= S_IFREG;
  319. p = wcsrchr(path, L'.');
  320. if (p != NULL && wcslen(p) == 4) {
  321. switch (p[1]) {
  322. case L'B': case L'b':
  323. if ((p[2] == L'A' || p[2] == L'a' ) &&
  324. (p[3] == L'T' || p[3] == L't' ))
  325. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  326. break;
  327. case L'C': case L'c':
  328. if (((p[2] == L'M' || p[2] == L'm' ) &&
  329. (p[3] == L'D' || p[3] == L'd' )))
  330. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  331. break;
  332. case L'E': case L'e':
  333. if ((p[2] == L'X' || p[2] == L'x' ) &&
  334. (p[3] == L'E' || p[3] == L'e' ))
  335. *mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. }
  342. return (0);
  343. }
  344. /*
  345. * Note: The path, for example, "aa/a/../b../c" will be converted to "aa/c"
  346. * by GetFullPathNameW() W32 API, which __la_win_permissive_name_w uses.
  347. * It means we cannot handle multiple dirs in one archive_entry.
  348. * So we have to make the full-pathname in another way, which does not
  349. * break "../" path string.
  350. */
  351. static int
  352. permissive_name_w(struct archive_write_disk *a)
  353. {
  354. wchar_t *wn, *wnp;
  355. wchar_t *ws, *wsp;
  356. DWORD l;
  357. wnp = a->name;
  358. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  359. wnp[2] == L'?' && wnp[3] == L'\\')
  360. /* We have already a permissive name. */
  361. return (0);
  362. if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
  363. wnp[2] == L'.' && wnp[3] == L'\\') {
  364. /* This is a device name */
  365. if (((wnp[4] >= L'a' && wnp[4] <= L'z') ||
  366. (wnp[4] >= L'A' && wnp[4] <= L'Z')) &&
  367. wnp[5] == L':' && wnp[6] == L'\\') {
  368. wnp[2] = L'?';/* Not device name. */
  369. return (0);
  370. }
  371. }
  372. /*
  373. * A full-pathname starting with a drive name like "C:\abc".
  374. */
  375. if (((wnp[0] >= L'a' && wnp[0] <= L'z') ||
  376. (wnp[0] >= L'A' && wnp[0] <= L'Z')) &&
  377. wnp[1] == L':' && wnp[2] == L'\\') {
  378. wn = _wcsdup(wnp);
  379. if (wn == NULL)
  380. return (-1);
  381. if (archive_wstring_ensure(&(a->_name_data),
  382. 4 + wcslen(wn) + 1) == NULL) {
  383. free(wn);
  384. return (-1);
  385. }
  386. a->name = a->_name_data.s;
  387. /* Prepend "\\?\" */
  388. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  389. archive_wstrcat(&(a->_name_data), wn);
  390. free(wn);
  391. return (0);
  392. }
  393. /*
  394. * A full-pathname pointing to a network drive
  395. * like "\\<server-name>\<share-name>\file".
  396. */
  397. if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
  398. const wchar_t *p = &wnp[2];
  399. /* Skip server-name letters. */
  400. while (*p != L'\\' && *p != L'\0')
  401. ++p;
  402. if (*p == L'\\') {
  403. const wchar_t *rp = ++p;
  404. /* Skip share-name letters. */
  405. while (*p != L'\\' && *p != L'\0')
  406. ++p;
  407. if (*p == L'\\' && p != rp) {
  408. /* Now, match patterns such as
  409. * "\\server-name\share-name\" */
  410. wn = _wcsdup(wnp);
  411. if (wn == NULL)
  412. return (-1);
  413. if (archive_wstring_ensure(&(a->_name_data),
  414. 8 + wcslen(wn) + 1) == NULL) {
  415. free(wn);
  416. return (-1);
  417. }
  418. a->name = a->_name_data.s;
  419. /* Prepend "\\?\UNC\" */
  420. archive_wstrncpy(&(a->_name_data),
  421. L"\\\\?\\UNC\\", 8);
  422. archive_wstrcat(&(a->_name_data), wn+2);
  423. free(wn);
  424. return (0);
  425. }
  426. }
  427. return (0);
  428. }
  429. /*
  430. * Get current working directory.
  431. */
  432. l = GetCurrentDirectoryW(0, NULL);
  433. if (l == 0)
  434. return (-1);
  435. ws = malloc(l * sizeof(wchar_t));
  436. l = GetCurrentDirectoryW(l, ws);
  437. if (l == 0) {
  438. free(ws);
  439. return (-1);
  440. }
  441. wsp = ws;
  442. /*
  443. * A full-pathname starting without a drive name like "\abc".
  444. */
  445. if (wnp[0] == L'\\') {
  446. wn = _wcsdup(wnp);
  447. if (wn == NULL) {
  448. free(wsp);
  449. return (-1);
  450. }
  451. if (archive_wstring_ensure(&(a->_name_data),
  452. 4 + 2 + wcslen(wn) + 1) == NULL) {
  453. free(wsp);
  454. free(wn);
  455. return (-1);
  456. }
  457. a->name = a->_name_data.s;
  458. /* Prepend "\\?\" and drive name. */
  459. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  460. archive_wstrncat(&(a->_name_data), wsp, 2);
  461. archive_wstrcat(&(a->_name_data), wn);
  462. free(wsp);
  463. free(wn);
  464. return (0);
  465. }
  466. wn = _wcsdup(wnp);
  467. if (wn == NULL) {
  468. free(wsp);
  469. return (-1);
  470. }
  471. if (archive_wstring_ensure(&(a->_name_data),
  472. 4 + l + 1 + wcslen(wn) + 1) == NULL) {
  473. free(wsp);
  474. free(wn);
  475. return (-1);
  476. }
  477. a->name = a->_name_data.s;
  478. /* Prepend "\\?\" and drive name if not already added. */
  479. if (l > 3 && wsp[0] == L'\\' && wsp[1] == L'\\' &&
  480. wsp[2] == L'?' && wsp[3] == L'\\')
  481. {
  482. archive_wstrncpy(&(a->_name_data), wsp, l);
  483. }
  484. else if (l > 2 && wsp[0] == L'\\' && wsp[1] == L'\\' && wsp[2] != L'\\')
  485. {
  486. archive_wstrncpy(&(a->_name_data), L"\\\\?\\UNC\\", 8);
  487. archive_wstrncat(&(a->_name_data), wsp+2, l-2);
  488. }
  489. else
  490. {
  491. archive_wstrncpy(&(a->_name_data), L"\\\\?\\", 4);
  492. archive_wstrncat(&(a->_name_data), wsp, l);
  493. }
  494. archive_wstrncat(&(a->_name_data), L"\\", 1);
  495. archive_wstrcat(&(a->_name_data), wn);
  496. a->name = a->_name_data.s;
  497. free(wsp);
  498. free(wn);
  499. return (0);
  500. }
  501. static int
  502. la_chmod(const wchar_t *path, mode_t mode)
  503. {
  504. DWORD attr;
  505. BOOL r;
  506. wchar_t *fullname;
  507. int ret = 0;
  508. fullname = NULL;
  509. attr = GetFileAttributesW(path);
  510. if (attr == (DWORD)-1 &&
  511. GetLastError() == ERROR_INVALID_NAME) {
  512. fullname = __la_win_permissive_name_w(path);
  513. attr = GetFileAttributesW(fullname);
  514. }
  515. if (attr == (DWORD)-1) {
  516. la_dosmaperr(GetLastError());
  517. ret = -1;
  518. goto exit_chmode;
  519. }
  520. if (mode & _S_IWRITE)
  521. attr &= ~FILE_ATTRIBUTE_READONLY;
  522. else
  523. attr |= FILE_ATTRIBUTE_READONLY;
  524. if (fullname != NULL)
  525. r = SetFileAttributesW(fullname, attr);
  526. else
  527. r = SetFileAttributesW(path, attr);
  528. if (r == 0) {
  529. la_dosmaperr(GetLastError());
  530. ret = -1;
  531. }
  532. exit_chmode:
  533. free(fullname);
  534. return (ret);
  535. }
  536. static int
  537. la_mktemp(struct archive_write_disk *a)
  538. {
  539. int fd;
  540. mode_t mode;
  541. archive_wstring_empty(&(a->_tmpname_data));
  542. archive_wstrcpy(&(a->_tmpname_data), a->name);
  543. archive_wstrcat(&(a->_tmpname_data), L".XXXXXX");
  544. a->tmpname = a->_tmpname_data.s;
  545. fd = __archive_mkstemp(a->tmpname);
  546. if (fd == -1)
  547. return -1;
  548. mode = a->mode & 0777 & ~a->user_umask;
  549. if (la_chmod(a->tmpname, mode) == -1) {
  550. la_dosmaperr(GetLastError());
  551. _close(fd);
  552. return -1;
  553. }
  554. return (fd);
  555. }
  556. #if _WIN32_WINNT < _WIN32_WINNT_VISTA
  557. static void *
  558. la_GetFunctionKernel32(const char *name)
  559. {
  560. static HINSTANCE lib;
  561. static int set;
  562. if (!set) {
  563. set = 1;
  564. lib = LoadLibrary(TEXT("kernel32.dll"));
  565. }
  566. if (lib == NULL) {
  567. fprintf(stderr, "Can't load kernel32.dll?!\n");
  568. exit(1);
  569. }
  570. return (void *)GetProcAddress(lib, name);
  571. }
  572. #endif
  573. static int
  574. la_CreateHardLinkW(wchar_t *linkname, wchar_t *target)
  575. {
  576. static BOOL (WINAPI *f)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
  577. BOOL ret;
  578. #if _WIN32_WINNT < _WIN32_WINNT_XP
  579. static int set;
  580. /* CreateHardLinkW is available since XP and always loaded */
  581. if (!set) {
  582. set = 1;
  583. f = la_GetFunctionKernel32("CreateHardLinkW");
  584. }
  585. #else
  586. f = CreateHardLinkW;
  587. #endif
  588. if (!f) {
  589. errno = ENOTSUP;
  590. return (0);
  591. }
  592. ret = (*f)(linkname, target, NULL);
  593. if (!ret) {
  594. /* Under windows 2000, it is necessary to remove
  595. * the "\\?\" prefix. */
  596. #define IS_UNC(name) ((name[0] == L'U' || name[0] == L'u') && \
  597. (name[1] == L'N' || name[1] == L'n') && \
  598. (name[2] == L'C' || name[2] == L'c') && \
  599. name[3] == L'\\')
  600. if (!wcsncmp(linkname,L"\\\\?\\", 4)) {
  601. linkname += 4;
  602. if (IS_UNC(linkname))
  603. linkname += 4;
  604. }
  605. if (!wcsncmp(target,L"\\\\?\\", 4)) {
  606. target += 4;
  607. if (IS_UNC(target))
  608. target += 4;
  609. }
  610. #undef IS_UNC
  611. ret = (*f)(linkname, target, NULL);
  612. }
  613. return (ret);
  614. }
  615. /*
  616. * Create file or directory symbolic link
  617. *
  618. * If linktype is AE_SYMLINK_TYPE_UNDEFINED (or unknown), guess linktype from
  619. * the link target
  620. */
  621. static int
  622. la_CreateSymbolicLinkW(const wchar_t *linkname, const wchar_t *target,
  623. int linktype) {
  624. static BOOLEAN (WINAPI *f)(LPCWSTR, LPCWSTR, DWORD);
  625. wchar_t *ttarget, *p;
  626. size_t len;
  627. DWORD attrs = 0;
  628. DWORD flags = 0;
  629. DWORD newflags = 0;
  630. BOOL ret = 0;
  631. #if _WIN32_WINNT < _WIN32_WINNT_VISTA
  632. /* CreateSymbolicLinkW is available since Vista and always loaded */
  633. static int set;
  634. if (!set) {
  635. set = 1;
  636. f = la_GetFunctionKernel32("CreateSymbolicLinkW");
  637. }
  638. #else
  639. # if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  640. f = CreateSymbolicLinkW;
  641. # else
  642. f = NULL;
  643. # endif
  644. #endif
  645. if (!f)
  646. return (0);
  647. len = wcslen(target);
  648. if (len == 0) {
  649. errno = EINVAL;
  650. return(0);
  651. }
  652. /*
  653. * When writing path targets, we need to translate slashes
  654. * to backslashes
  655. */
  656. ttarget = malloc((len + 1) * sizeof(wchar_t));
  657. if (ttarget == NULL)
  658. return(0);
  659. p = ttarget;
  660. while(*target != L'\0') {
  661. if (*target == L'/')
  662. *p = L'\\';
  663. else
  664. *p = *target;
  665. target++;
  666. p++;
  667. }
  668. *p = L'\0';
  669. /*
  670. * In case of undefined symlink type we guess it from the target.
  671. * If the target equals ".", "..", ends with a backslash or a
  672. * backslash followed by "." or ".." we assume it is a directory
  673. * symlink. In all other cases we assume a file symlink.
  674. */
  675. if (linktype != AE_SYMLINK_TYPE_FILE && (
  676. linktype == AE_SYMLINK_TYPE_DIRECTORY ||
  677. *(p - 1) == L'\\' || (*(p - 1) == L'.' && (
  678. len == 1 || *(p - 2) == L'\\' || ( *(p - 2) == L'.' && (
  679. len == 2 || *(p - 3) == L'\\')))))) {
  680. #if defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
  681. flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
  682. #else
  683. flags |= 0x1;
  684. #endif
  685. }
  686. #if defined(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
  687. newflags = flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
  688. #else
  689. newflags = flags | 0x2;
  690. #endif
  691. /*
  692. * Windows won't overwrite existing links
  693. */
  694. attrs = GetFileAttributesW(linkname);
  695. if (attrs != INVALID_FILE_ATTRIBUTES) {
  696. if (attrs & FILE_ATTRIBUTE_DIRECTORY)
  697. disk_rmdir(linkname);
  698. else
  699. disk_unlink(linkname);
  700. }
  701. ret = (*f)(linkname, ttarget, newflags);
  702. /*
  703. * Prior to Windows 10 calling CreateSymbolicLinkW() will fail
  704. * if SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE is set
  705. */
  706. if (!ret) {
  707. ret = (*f)(linkname, ttarget, flags);
  708. }
  709. free(ttarget);
  710. return (ret);
  711. }
  712. static int
  713. la_ftruncate(HANDLE handle, int64_t length)
  714. {
  715. LARGE_INTEGER distance;
  716. if (GetFileType(handle) != FILE_TYPE_DISK) {
  717. errno = EBADF;
  718. return (-1);
  719. }
  720. distance.QuadPart = length;
  721. if (!SetFilePointerEx_perso(handle, distance, NULL, FILE_BEGIN)) {
  722. la_dosmaperr(GetLastError());
  723. return (-1);
  724. }
  725. if (!SetEndOfFile(handle)) {
  726. la_dosmaperr(GetLastError());
  727. return (-1);
  728. }
  729. return (0);
  730. }
  731. static int
  732. lazy_stat(struct archive_write_disk *a)
  733. {
  734. if (a->pst != NULL) {
  735. /* Already have stat() data available. */
  736. return (ARCHIVE_OK);
  737. }
  738. if (a->fh != INVALID_HANDLE_VALUE &&
  739. GetFileInformationByHandle(a->fh, &a->st) == 0) {
  740. a->pst = &a->st;
  741. return (ARCHIVE_OK);
  742. }
  743. /*
  744. * XXX At this point, symlinks should not be hit, otherwise
  745. * XXX a race occurred. Do we want to check explicitly for that?
  746. */
  747. if (file_information(a, a->name, &a->st, NULL, 1) == 0) {
  748. a->pst = &a->st;
  749. return (ARCHIVE_OK);
  750. }
  751. archive_set_error(&a->archive, errno, "Couldn't stat file");
  752. return (ARCHIVE_WARN);
  753. }
  754. static const struct archive_vtable
  755. archive_write_disk_vtable = {
  756. .archive_close = _archive_write_disk_close,
  757. .archive_filter_bytes = _archive_write_disk_filter_bytes,
  758. .archive_free = _archive_write_disk_free,
  759. .archive_write_header = _archive_write_disk_header,
  760. .archive_write_finish_entry = _archive_write_disk_finish_entry,
  761. .archive_write_data = _archive_write_disk_data,
  762. .archive_write_data_block = _archive_write_disk_data_block,
  763. };
  764. static int64_t
  765. _archive_write_disk_filter_bytes(struct archive *_a, int n)
  766. {
  767. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  768. (void)n; /* UNUSED */
  769. if (n == -1 || n == 0)
  770. return (a->total_bytes_written);
  771. return (-1);
  772. }
  773. int
  774. archive_write_disk_set_options(struct archive *_a, int flags)
  775. {
  776. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  777. a->flags = flags;
  778. return (ARCHIVE_OK);
  779. }
  780. /*
  781. * Extract this entry to disk.
  782. *
  783. * TODO: Validate hardlinks. According to the standards, we're
  784. * supposed to check each extracted hardlink and squawk if it refers
  785. * to a file that we didn't restore. I'm not entirely convinced this
  786. * is a good idea, but more importantly: Is there any way to validate
  787. * hardlinks without keeping a complete list of filenames from the
  788. * entire archive?? Ugh.
  789. *
  790. */
  791. static int
  792. _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
  793. {
  794. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  795. struct fixup_entry *fe;
  796. int ret, r;
  797. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  798. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  799. "archive_write_disk_header");
  800. archive_clear_error(&a->archive);
  801. if (a->archive.state & ARCHIVE_STATE_DATA) {
  802. r = _archive_write_disk_finish_entry(&a->archive);
  803. if (r == ARCHIVE_FATAL)
  804. return (r);
  805. }
  806. /* Set up for this particular entry. */
  807. a->pst = NULL;
  808. a->current_fixup = NULL;
  809. a->deferred = 0;
  810. archive_entry_free(a->entry);
  811. a->entry = NULL;
  812. a->entry = archive_entry_clone(entry);
  813. a->fh = INVALID_HANDLE_VALUE;
  814. a->fd_offset = 0;
  815. a->offset = 0;
  816. a->restore_pwd = -1;
  817. a->uid = a->user_uid;
  818. a->mode = archive_entry_mode(a->entry);
  819. if (archive_entry_size_is_set(a->entry))
  820. a->filesize = archive_entry_size(a->entry);
  821. else
  822. a->filesize = -1;
  823. archive_wstrcpy(&(a->_name_data), archive_entry_pathname_w(a->entry));
  824. a->name = a->_name_data.s;
  825. archive_clear_error(&a->archive);
  826. /*
  827. * Clean up the requested path. This is necessary for correct
  828. * dir restores; the dir restore logic otherwise gets messed
  829. * up by nonsense like "dir/.".
  830. */
  831. ret = cleanup_pathname(a, a->name);
  832. if (ret != ARCHIVE_OK)
  833. return (ret);
  834. /*
  835. * Generate a full-pathname and use it from here.
  836. */
  837. if (permissive_name_w(a) < 0) {
  838. errno = EINVAL;
  839. return (ARCHIVE_FAILED);
  840. }
  841. /*
  842. * Query the umask so we get predictable mode settings.
  843. * This gets done on every call to _write_header in case the
  844. * user edits their umask during the extraction for some
  845. * reason.
  846. */
  847. umask(a->user_umask = umask(0));
  848. /* Figure out what we need to do for this entry. */
  849. a->todo = TODO_MODE_BASE;
  850. if (a->flags & ARCHIVE_EXTRACT_PERM) {
  851. a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
  852. /*
  853. * SGID requires an extra "check" step because we
  854. * cannot easily predict the GID that the system will
  855. * assign. (Different systems assign GIDs to files
  856. * based on a variety of criteria, including process
  857. * credentials and the gid of the enclosing
  858. * directory.) We can only restore the SGID bit if
  859. * the file has the right GID, and we only know the
  860. * GID if we either set it (see set_ownership) or if
  861. * we've actually called stat() on the file after it
  862. * was restored. Since there are several places at
  863. * which we might verify the GID, we need a TODO bit
  864. * to keep track.
  865. */
  866. if (a->mode & S_ISGID)
  867. a->todo |= TODO_SGID | TODO_SGID_CHECK;
  868. /*
  869. * Verifying the SUID is simpler, but can still be
  870. * done in multiple ways, hence the separate "check" bit.
  871. */
  872. if (a->mode & S_ISUID)
  873. a->todo |= TODO_SUID | TODO_SUID_CHECK;
  874. } else {
  875. /*
  876. * User didn't request full permissions, so don't
  877. * restore SUID, SGID bits and obey umask.
  878. */
  879. a->mode &= ~S_ISUID;
  880. a->mode &= ~S_ISGID;
  881. a->mode &= ~S_ISVTX;
  882. a->mode &= ~a->user_umask;
  883. }
  884. #if 0
  885. if (a->flags & ARCHIVE_EXTRACT_OWNER)
  886. a->todo |= TODO_OWNER;
  887. #endif
  888. if (a->flags & ARCHIVE_EXTRACT_TIME)
  889. a->todo |= TODO_TIMES;
  890. if (a->flags & ARCHIVE_EXTRACT_ACL) {
  891. if (archive_entry_filetype(a->entry) == AE_IFDIR)
  892. a->deferred |= TODO_ACLS;
  893. else
  894. a->todo |= TODO_ACLS;
  895. }
  896. if (a->flags & ARCHIVE_EXTRACT_XATTR)
  897. a->todo |= TODO_XATTR;
  898. if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
  899. a->todo |= TODO_FFLAGS;
  900. if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
  901. ret = check_symlinks(a);
  902. if (ret != ARCHIVE_OK)
  903. return (ret);
  904. }
  905. ret = restore_entry(a);
  906. /*
  907. * TODO: There are rumours that some extended attributes must
  908. * be restored before file data is written. If this is true,
  909. * then we either need to write all extended attributes both
  910. * before and after restoring the data, or find some rule for
  911. * determining which must go first and which last. Due to the
  912. * many ways people are using xattrs, this may prove to be an
  913. * intractable problem.
  914. */
  915. /*
  916. * Fixup uses the unedited pathname from archive_entry_pathname(),
  917. * because it is relative to the base dir and the edited path
  918. * might be relative to some intermediate dir as a result of the
  919. * deep restore logic.
  920. */
  921. if (a->deferred & TODO_MODE) {
  922. fe = current_fixup(a, archive_entry_pathname_w(entry));
  923. fe->fixup |= TODO_MODE_BASE;
  924. fe->mode = a->mode;
  925. }
  926. if ((a->deferred & TODO_TIMES)
  927. && (archive_entry_mtime_is_set(entry)
  928. || archive_entry_atime_is_set(entry))) {
  929. fe = current_fixup(a, archive_entry_pathname_w(entry));
  930. fe->mode = a->mode;
  931. fe->fixup |= TODO_TIMES;
  932. if (archive_entry_atime_is_set(entry)) {
  933. fe->atime = archive_entry_atime(entry);
  934. fe->atime_nanos = archive_entry_atime_nsec(entry);
  935. } else {
  936. /* If atime is unset, use start time. */
  937. fe->atime = a->start_time;
  938. fe->atime_nanos = 0;
  939. }
  940. if (archive_entry_mtime_is_set(entry)) {
  941. fe->mtime = archive_entry_mtime(entry);
  942. fe->mtime_nanos = archive_entry_mtime_nsec(entry);
  943. } else {
  944. /* If mtime is unset, use start time. */
  945. fe->mtime = a->start_time;
  946. fe->mtime_nanos = 0;
  947. }
  948. if (archive_entry_birthtime_is_set(entry)) {
  949. fe->birthtime = archive_entry_birthtime(entry);
  950. fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
  951. } else {
  952. /* If birthtime is unset, use mtime. */
  953. fe->birthtime = fe->mtime;
  954. fe->birthtime_nanos = fe->mtime_nanos;
  955. }
  956. }
  957. if (a->deferred & TODO_ACLS) {
  958. fe = current_fixup(a, archive_entry_pathname_w(entry));
  959. archive_acl_copy(&fe->acl, archive_entry_acl(entry));
  960. }
  961. if (a->deferred & TODO_FFLAGS) {
  962. unsigned long set, clear;
  963. fe = current_fixup(a, archive_entry_pathname_w(entry));
  964. archive_entry_fflags(entry, &set, &clear);
  965. fe->fflags_set = set;
  966. }
  967. /*
  968. * On Windows, A creating sparse file requires a special mark.
  969. */
  970. if (a->fh != INVALID_HANDLE_VALUE &&
  971. archive_entry_sparse_count(entry) > 0) {
  972. int64_t base = 0, offset, length;
  973. int i, cnt = archive_entry_sparse_reset(entry);
  974. int sparse = 0;
  975. for (i = 0; i < cnt; i++) {
  976. archive_entry_sparse_next(entry, &offset, &length);
  977. if (offset - base >= 4096) {
  978. sparse = 1;/* we have a hole. */
  979. break;
  980. }
  981. base = offset + length;
  982. }
  983. if (sparse) {
  984. DWORD dmy;
  985. /* Mark this file as sparse. */
  986. DeviceIoControl(a->fh, FSCTL_SET_SPARSE,
  987. NULL, 0, NULL, 0, &dmy, NULL);
  988. }
  989. }
  990. /* We've created the object and are ready to pour data into it. */
  991. if (ret >= ARCHIVE_WARN)
  992. a->archive.state = ARCHIVE_STATE_DATA;
  993. /*
  994. * If it's not open, tell our client not to try writing.
  995. * In particular, dirs, links, etc, don't get written to.
  996. */
  997. if (a->fh == INVALID_HANDLE_VALUE) {
  998. archive_entry_set_size(entry, 0);
  999. a->filesize = 0;
  1000. }
  1001. return (ret);
  1002. }
  1003. int
  1004. archive_write_disk_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
  1005. {
  1006. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1007. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1008. ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
  1009. a->skip_file_set = 1;
  1010. a->skip_file_dev = d;
  1011. a->skip_file_ino = i;
  1012. return (ARCHIVE_OK);
  1013. }
  1014. static ssize_t
  1015. write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
  1016. {
  1017. OVERLAPPED ol;
  1018. uint64_t start_size = size;
  1019. DWORD bytes_written = 0;
  1020. ssize_t block_size = 0, bytes_to_write;
  1021. if (size == 0)
  1022. return (ARCHIVE_OK);
  1023. if (a->filesize == 0 || a->fh == INVALID_HANDLE_VALUE) {
  1024. archive_set_error(&a->archive, 0,
  1025. "Attempt to write to an empty file");
  1026. return (ARCHIVE_WARN);
  1027. }
  1028. if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
  1029. /* XXX TODO XXX Is there a more appropriate choice here ? */
  1030. /* This needn't match the filesystem allocation size. */
  1031. block_size = 16*1024;
  1032. }
  1033. /* If this write would run beyond the file size, truncate it. */
  1034. if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
  1035. start_size = size = (size_t)(a->filesize - a->offset);
  1036. /* Write the data. */
  1037. while (size > 0) {
  1038. if (block_size == 0) {
  1039. bytes_to_write = size;
  1040. } else {
  1041. /* We're sparsifying the file. */
  1042. const char *p, *end;
  1043. int64_t block_end;
  1044. /* Skip leading zero bytes. */
  1045. for (p = buff, end = buff + size; p < end; ++p) {
  1046. if (*p != '\0')
  1047. break;
  1048. }
  1049. a->offset += p - buff;
  1050. size -= p - buff;
  1051. buff = p;
  1052. if (size == 0)
  1053. break;
  1054. /* Calculate next block boundary after offset. */
  1055. block_end
  1056. = (a->offset / block_size + 1) * block_size;
  1057. /* If the adjusted write would cross block boundary,
  1058. * truncate it to the block boundary. */
  1059. bytes_to_write = size;
  1060. if (a->offset + bytes_to_write > block_end)
  1061. bytes_to_write = (DWORD)(block_end - a->offset);
  1062. }
  1063. memset(&ol, 0, sizeof(ol));
  1064. ol.Offset = (DWORD)(a->offset & 0xFFFFFFFF);
  1065. ol.OffsetHigh = (DWORD)(a->offset >> 32);
  1066. if (!WriteFile(a->fh, buff, (uint32_t)bytes_to_write,
  1067. &bytes_written, &ol)) {
  1068. DWORD lasterr;
  1069. lasterr = GetLastError();
  1070. if (lasterr == ERROR_ACCESS_DENIED)
  1071. errno = EBADF;
  1072. else
  1073. la_dosmaperr(lasterr);
  1074. archive_set_error(&a->archive, errno, "Write failed");
  1075. return (ARCHIVE_WARN);
  1076. }
  1077. buff += bytes_written;
  1078. size -= bytes_written;
  1079. a->total_bytes_written += bytes_written;
  1080. a->offset += bytes_written;
  1081. a->fd_offset = a->offset;
  1082. }
  1083. return ((ssize_t)(start_size - size));
  1084. }
  1085. static ssize_t
  1086. _archive_write_disk_data_block(struct archive *_a,
  1087. const void *buff, size_t size, int64_t offset)
  1088. {
  1089. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1090. ssize_t r;
  1091. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1092. ARCHIVE_STATE_DATA, "archive_write_data_block");
  1093. a->offset = offset;
  1094. r = write_data_block(a, buff, size);
  1095. if (r < ARCHIVE_OK)
  1096. return (r);
  1097. if ((size_t)r < size) {
  1098. archive_set_error(&a->archive, 0,
  1099. "Write request too large");
  1100. return (ARCHIVE_WARN);
  1101. }
  1102. #if ARCHIVE_VERSION_NUMBER < 3999000
  1103. return (ARCHIVE_OK);
  1104. #else
  1105. return (size);
  1106. #endif
  1107. }
  1108. static ssize_t
  1109. _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
  1110. {
  1111. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1112. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1113. ARCHIVE_STATE_DATA, "archive_write_data");
  1114. return (write_data_block(a, buff, size));
  1115. }
  1116. static int
  1117. _archive_write_disk_finish_entry(struct archive *_a)
  1118. {
  1119. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1120. int ret = ARCHIVE_OK;
  1121. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1122. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1123. "archive_write_finish_entry");
  1124. if (a->archive.state & ARCHIVE_STATE_HEADER)
  1125. return (ARCHIVE_OK);
  1126. archive_clear_error(&a->archive);
  1127. /* Pad or truncate file to the right size. */
  1128. if (a->fh == INVALID_HANDLE_VALUE) {
  1129. /* There's no file. */
  1130. } else if (a->filesize < 0) {
  1131. /* File size is unknown, so we can't set the size. */
  1132. } else if (a->fd_offset == a->filesize) {
  1133. /* Last write ended at exactly the filesize; we're done. */
  1134. /* Hopefully, this is the common case. */
  1135. } else {
  1136. if (la_ftruncate(a->fh, a->filesize) == -1) {
  1137. archive_set_error(&a->archive, errno,
  1138. "File size could not be restored");
  1139. CloseHandle(a->fh);
  1140. a->fh = INVALID_HANDLE_VALUE;
  1141. return (ARCHIVE_FAILED);
  1142. }
  1143. }
  1144. /* Restore metadata. */
  1145. /*
  1146. * Look up the "real" UID only if we're going to need it.
  1147. * TODO: the TODO_SGID condition can be dropped here, can't it?
  1148. */
  1149. if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
  1150. a->uid = archive_write_disk_uid(&a->archive,
  1151. archive_entry_uname(a->entry),
  1152. archive_entry_uid(a->entry));
  1153. }
  1154. /* Look up the "real" GID only if we're going to need it. */
  1155. /* TODO: the TODO_SUID condition can be dropped here, can't it? */
  1156. if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
  1157. a->gid = archive_write_disk_gid(&a->archive,
  1158. archive_entry_gname(a->entry),
  1159. archive_entry_gid(a->entry));
  1160. }
  1161. /*
  1162. * Restore ownership before set_mode tries to restore suid/sgid
  1163. * bits. If we set the owner, we know what it is and can skip
  1164. * a stat() call to examine the ownership of the file on disk.
  1165. */
  1166. if (a->todo & TODO_OWNER)
  1167. ret = set_ownership(a);
  1168. /*
  1169. * set_mode must precede ACLs on systems such as Solaris and
  1170. * FreeBSD where setting the mode implicitly clears extended ACLs
  1171. */
  1172. if (a->todo & TODO_MODE) {
  1173. int r2 = set_mode(a, a->mode);
  1174. if (r2 < ret) ret = r2;
  1175. }
  1176. /*
  1177. * Security-related extended attributes (such as
  1178. * security.capability on Linux) have to be restored last,
  1179. * since they're implicitly removed by other file changes.
  1180. */
  1181. if (a->todo & TODO_XATTR) {
  1182. int r2 = set_xattrs(a);
  1183. if (r2 < ret) ret = r2;
  1184. }
  1185. /*
  1186. * Some flags prevent file modification; they must be restored after
  1187. * file contents are written.
  1188. */
  1189. if (a->todo & TODO_FFLAGS) {
  1190. int r2 = set_fflags(a);
  1191. if (r2 < ret) ret = r2;
  1192. }
  1193. /*
  1194. * Time must follow most other metadata;
  1195. * otherwise atime will get changed.
  1196. */
  1197. if (a->todo & TODO_TIMES) {
  1198. int r2 = set_times_from_entry(a);
  1199. if (r2 < ret) ret = r2;
  1200. }
  1201. /*
  1202. * ACLs must be restored after timestamps because there are
  1203. * ACLs that prevent attribute changes (including time).
  1204. */
  1205. if (a->todo & TODO_ACLS) {
  1206. int r2 = set_acls(a, a->fh,
  1207. archive_entry_pathname_w(a->entry),
  1208. archive_entry_acl(a->entry));
  1209. if (r2 < ret) ret = r2;
  1210. }
  1211. /* If there's an fd, we can close it now. */
  1212. if (a->fh != INVALID_HANDLE_VALUE) {
  1213. CloseHandle(a->fh);
  1214. a->fh = INVALID_HANDLE_VALUE;
  1215. if (a->tmpname) {
  1216. /* Windows does not support atomic rename */
  1217. disk_unlink(a->name);
  1218. if (_wrename(a->tmpname, a->name) != 0) {
  1219. la_dosmaperr(GetLastError());
  1220. archive_set_error(&a->archive, errno,
  1221. "Failed to rename temporary file");
  1222. ret = ARCHIVE_FAILED;
  1223. disk_unlink(a->tmpname);
  1224. }
  1225. a->tmpname = NULL;
  1226. }
  1227. }
  1228. /* If there's an entry, we can release it now. */
  1229. archive_entry_free(a->entry);
  1230. a->entry = NULL;
  1231. a->archive.state = ARCHIVE_STATE_HEADER;
  1232. return (ret);
  1233. }
  1234. int
  1235. archive_write_disk_set_group_lookup(struct archive *_a,
  1236. void *private_data,
  1237. la_int64_t (*lookup_gid)(void *private, const char *gname, la_int64_t gid),
  1238. void (*cleanup_gid)(void *private))
  1239. {
  1240. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1241. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1242. ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
  1243. if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
  1244. (a->cleanup_gid)(a->lookup_gid_data);
  1245. a->lookup_gid = lookup_gid;
  1246. a->cleanup_gid = cleanup_gid;
  1247. a->lookup_gid_data = private_data;
  1248. return (ARCHIVE_OK);
  1249. }
  1250. int
  1251. archive_write_disk_set_user_lookup(struct archive *_a,
  1252. void *private_data,
  1253. int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
  1254. void (*cleanup_uid)(void *private))
  1255. {
  1256. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1257. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1258. ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
  1259. if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
  1260. (a->cleanup_uid)(a->lookup_uid_data);
  1261. a->lookup_uid = lookup_uid;
  1262. a->cleanup_uid = cleanup_uid;
  1263. a->lookup_uid_data = private_data;
  1264. return (ARCHIVE_OK);
  1265. }
  1266. int64_t
  1267. archive_write_disk_gid(struct archive *_a, const char *name, la_int64_t id)
  1268. {
  1269. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1270. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1271. ARCHIVE_STATE_ANY, "archive_write_disk_gid");
  1272. if (a->lookup_gid)
  1273. return (a->lookup_gid)(a->lookup_gid_data, name, id);
  1274. return (id);
  1275. }
  1276. int64_t
  1277. archive_write_disk_uid(struct archive *_a, const char *name, la_int64_t id)
  1278. {
  1279. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1280. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1281. ARCHIVE_STATE_ANY, "archive_write_disk_uid");
  1282. if (a->lookup_uid)
  1283. return (a->lookup_uid)(a->lookup_uid_data, name, id);
  1284. return (id);
  1285. }
  1286. /*
  1287. * Create a new archive_write_disk object and initialize it with global state.
  1288. */
  1289. struct archive *
  1290. archive_write_disk_new(void)
  1291. {
  1292. struct archive_write_disk *a;
  1293. a = calloc(1, sizeof(*a));
  1294. if (a == NULL)
  1295. return (NULL);
  1296. a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
  1297. /* We're ready to write a header immediately. */
  1298. a->archive.state = ARCHIVE_STATE_HEADER;
  1299. a->archive.vtable = &archive_write_disk_vtable;
  1300. a->start_time = time(NULL);
  1301. /* Query and restore the umask. */
  1302. umask(a->user_umask = umask(0));
  1303. if (archive_wstring_ensure(&a->path_safe, 512) == NULL) {
  1304. free(a);
  1305. return (NULL);
  1306. }
  1307. a->path_safe.s[0] = 0;
  1308. return (&a->archive);
  1309. }
  1310. static int
  1311. disk_unlink(const wchar_t *path)
  1312. {
  1313. wchar_t *fullname;
  1314. int r;
  1315. r = _wunlink(path);
  1316. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1317. fullname = __la_win_permissive_name_w(path);
  1318. r = _wunlink(fullname);
  1319. free(fullname);
  1320. }
  1321. return (r);
  1322. }
  1323. static int
  1324. disk_rmdir(const wchar_t *path)
  1325. {
  1326. wchar_t *fullname;
  1327. int r;
  1328. r = _wrmdir(path);
  1329. if (r != 0 && GetLastError() == ERROR_INVALID_NAME) {
  1330. fullname = __la_win_permissive_name_w(path);
  1331. r = _wrmdir(fullname);
  1332. free(fullname);
  1333. }
  1334. return (r);
  1335. }
  1336. /*
  1337. * The main restore function.
  1338. */
  1339. static int
  1340. restore_entry(struct archive_write_disk *a)
  1341. {
  1342. int ret = ARCHIVE_OK, en;
  1343. if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
  1344. /*
  1345. * TODO: Fix this. Apparently, there are platforms
  1346. * that still allow root to hose the entire filesystem
  1347. * by unlinking a dir. The S_ISDIR() test above
  1348. * prevents us from using unlink() here if the new
  1349. * object is a dir, but that doesn't mean the old
  1350. * object isn't a dir.
  1351. */
  1352. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1353. (void)clear_nochange_fflags(a);
  1354. if (disk_unlink(a->name) == 0) {
  1355. /* We removed it, reset cached stat. */
  1356. a->pst = NULL;
  1357. } else if (errno == ENOENT) {
  1358. /* File didn't exist, that's just as good. */
  1359. } else if (disk_rmdir(a->name) == 0) {
  1360. /* It was a dir, but now it's gone. */
  1361. a->pst = NULL;
  1362. } else {
  1363. /* We tried, but couldn't get rid of it. */
  1364. archive_set_error(&a->archive, errno,
  1365. "Could not unlink");
  1366. return(ARCHIVE_FAILED);
  1367. }
  1368. }
  1369. /* Try creating it first; if this fails, we'll try to recover. */
  1370. en = create_filesystem_object(a);
  1371. if ((en == ENOTDIR || en == ENOENT)
  1372. && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
  1373. wchar_t *full;
  1374. /* If the parent dir doesn't exist, try creating it. */
  1375. create_parent_dir(a, a->name);
  1376. /* Now try to create the object again. */
  1377. full = __la_win_permissive_name_w(a->name);
  1378. if (full == NULL) {
  1379. en = EINVAL;
  1380. } else {
  1381. /* Remove multiple directories such as "a/../b../c" */
  1382. archive_wstrcpy(&(a->_name_data), full);
  1383. a->name = a->_name_data.s;
  1384. free(full);
  1385. en = create_filesystem_object(a);
  1386. }
  1387. }
  1388. if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) {
  1389. archive_set_error(&a->archive, en,
  1390. "Hard-link target '%s' does not exist.",
  1391. archive_entry_hardlink(a->entry));
  1392. return (ARCHIVE_FAILED);
  1393. }
  1394. if ((en == EISDIR || en == EEXIST)
  1395. && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  1396. /* If we're not overwriting, we're done. */
  1397. if (S_ISDIR(a->mode)) {
  1398. /* Don't overwrite any settings on existing directories. */
  1399. a->todo = 0;
  1400. }
  1401. archive_entry_unset_size(a->entry);
  1402. return (ARCHIVE_OK);
  1403. }
  1404. /*
  1405. * Some platforms return EISDIR if you call
  1406. * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
  1407. * return EEXIST. POSIX is ambiguous, requiring EISDIR
  1408. * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
  1409. * on an existing item.
  1410. */
  1411. if (en == EISDIR) {
  1412. /* A dir is in the way of a non-dir, rmdir it. */
  1413. if (disk_rmdir(a->name) != 0) {
  1414. archive_set_error(&a->archive, errno,
  1415. "Can't remove already-existing dir");
  1416. return (ARCHIVE_FAILED);
  1417. }
  1418. a->pst = NULL;
  1419. /* Try again. */
  1420. en = create_filesystem_object(a);
  1421. } else if (en == EEXIST) {
  1422. mode_t st_mode;
  1423. mode_t lst_mode;
  1424. BY_HANDLE_FILE_INFORMATION lst;
  1425. /*
  1426. * We know something is in the way, but we don't know what;
  1427. * we need to find out before we go any further.
  1428. */
  1429. int r = 0;
  1430. int dirlnk = 0;
  1431. /*
  1432. * The SECURE_SYMLINK logic has already removed a
  1433. * symlink to a dir if the client wants that. So
  1434. * follow the symlink if we're creating a dir.
  1435. * If it's not a dir (or it's a broken symlink),
  1436. * then don't follow it.
  1437. *
  1438. * Windows distinguishes file and directory symlinks.
  1439. * A file symlink may erroneously point to a directory
  1440. * and a directory symlink to a file. Windows does not follow
  1441. * such symlinks. We always need both source and target
  1442. * information.
  1443. */
  1444. r = file_information(a, a->name, &lst, &lst_mode, 1);
  1445. if (r != 0) {
  1446. archive_set_error(&a->archive, errno,
  1447. "Can't stat existing object");
  1448. return (ARCHIVE_FAILED);
  1449. } else if (S_ISLNK(lst_mode)) {
  1450. if (lst.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1451. dirlnk = 1;
  1452. /* In case of a symlink we need target information */
  1453. r = file_information(a, a->name, &a->st, &st_mode, 0);
  1454. if (r != 0) {
  1455. a->st = lst;
  1456. st_mode = lst_mode;
  1457. }
  1458. } else {
  1459. a->st = lst;
  1460. st_mode = lst_mode;
  1461. }
  1462. /*
  1463. * NO_OVERWRITE_NEWER doesn't apply to directories.
  1464. */
  1465. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
  1466. && !S_ISDIR(st_mode)) {
  1467. if (!older(&(a->st), a->entry)) {
  1468. archive_entry_unset_size(a->entry);
  1469. return (ARCHIVE_OK);
  1470. }
  1471. }
  1472. /* If it's our archive, we're done. */
  1473. if (a->skip_file_set &&
  1474. bhfi_dev(&a->st) == a->skip_file_dev &&
  1475. bhfi_ino(&a->st) == a->skip_file_ino) {
  1476. archive_set_error(&a->archive, 0,
  1477. "Refusing to overwrite archive");
  1478. return (ARCHIVE_FAILED);
  1479. }
  1480. if (!S_ISDIR(st_mode)) {
  1481. if (a->flags &
  1482. ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
  1483. (void)clear_nochange_fflags(a);
  1484. }
  1485. if ((a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) &&
  1486. S_ISREG(st_mode)) {
  1487. int fd = la_mktemp(a);
  1488. if (fd == -1) {
  1489. la_dosmaperr(GetLastError());
  1490. archive_set_error(&a->archive, errno,
  1491. "Can't create temporary file");
  1492. return (ARCHIVE_FAILED);
  1493. }
  1494. a->fh = (HANDLE)_get_osfhandle(fd);
  1495. if (a->fh == INVALID_HANDLE_VALUE) {
  1496. la_dosmaperr(GetLastError());
  1497. return (ARCHIVE_FAILED);
  1498. }
  1499. a->pst = NULL;
  1500. en = 0;
  1501. } else {
  1502. if (dirlnk) {
  1503. /* Edge case: dir symlink pointing
  1504. * to a file */
  1505. if (disk_rmdir(a->name) != 0) {
  1506. archive_set_error(&a->archive,
  1507. errno, "Can't unlink "
  1508. "directory symlink");
  1509. return (ARCHIVE_FAILED);
  1510. }
  1511. } else {
  1512. if (disk_unlink(a->name) != 0) {
  1513. /* A non-dir is in the way,
  1514. * unlink it. */
  1515. archive_set_error(&a->archive,
  1516. errno, "Can't unlink "
  1517. "already-existing object");
  1518. return (ARCHIVE_FAILED);
  1519. }
  1520. }
  1521. a->pst = NULL;
  1522. /* Try again. */
  1523. en = create_filesystem_object(a);
  1524. }
  1525. } else if (!S_ISDIR(a->mode)) {
  1526. /* A dir is in the way of a non-dir, rmdir it. */
  1527. if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
  1528. (void)clear_nochange_fflags(a);
  1529. if (disk_rmdir(a->name) != 0) {
  1530. archive_set_error(&a->archive, errno,
  1531. "Can't remove already-existing dir");
  1532. return (ARCHIVE_FAILED);
  1533. }
  1534. /* Try again. */
  1535. en = create_filesystem_object(a);
  1536. } else {
  1537. /*
  1538. * There's a dir in the way of a dir. Don't
  1539. * waste time with rmdir()/mkdir(), just fix
  1540. * up the permissions on the existing dir.
  1541. * Note that we don't change perms on existing
  1542. * dirs unless _EXTRACT_PERM is specified.
  1543. */
  1544. if ((a->mode != st_mode)
  1545. && (a->todo & TODO_MODE_FORCE))
  1546. a->deferred |= (a->todo & TODO_MODE);
  1547. /* Ownership doesn't need deferred fixup. */
  1548. en = 0; /* Forget the EEXIST. */
  1549. }
  1550. }
  1551. if (en) {
  1552. /* Everything failed; give up here. */
  1553. archive_set_error(&a->archive, en, "Can't create '%ls'",
  1554. a->name);
  1555. return (ARCHIVE_FAILED);
  1556. }
  1557. a->pst = NULL; /* Cached stat data no longer valid. */
  1558. return (ret);
  1559. }
  1560. /*
  1561. * Returns 0 if creation succeeds, or else returns errno value from
  1562. * the failed system call. Note: This function should only ever perform
  1563. * a single system call.
  1564. */
  1565. static int
  1566. create_filesystem_object(struct archive_write_disk *a)
  1567. {
  1568. /* Create the entry. */
  1569. const wchar_t *linkname;
  1570. wchar_t *fullname;
  1571. mode_t final_mode, mode;
  1572. int r;
  1573. DWORD attrs = 0;
  1574. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1575. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  1576. #endif
  1577. /* We identify hard/symlinks according to the link names. */
  1578. /* Since link(2) and symlink(2) don't handle modes, we're done here. */
  1579. linkname = archive_entry_hardlink_w(a->entry);
  1580. if (linkname != NULL) {
  1581. wchar_t *linksanitized, *linkfull, *namefull;
  1582. size_t l = (wcslen(linkname) + 1) * sizeof(wchar_t);
  1583. linksanitized = malloc(l);
  1584. if (linksanitized == NULL) {
  1585. archive_set_error(&a->archive, ENOMEM,
  1586. "Can't allocate memory for hardlink target");
  1587. return (-1);
  1588. }
  1589. memcpy(linksanitized, linkname, l);
  1590. r = cleanup_pathname(a, linksanitized);
  1591. if (r != ARCHIVE_OK) {
  1592. free(linksanitized);
  1593. return (r);
  1594. }
  1595. linkfull = __la_win_permissive_name_w(linksanitized);
  1596. free(linksanitized);
  1597. namefull = __la_win_permissive_name_w(a->name);
  1598. if (linkfull == NULL || namefull == NULL) {
  1599. errno = EINVAL;
  1600. r = -1;
  1601. } else {
  1602. /*
  1603. * Unlinking and linking here is really not atomic,
  1604. * but doing it right, would require us to construct
  1605. * an mktemplink() function, and then use _wrename().
  1606. */
  1607. if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) {
  1608. attrs = GetFileAttributesW(namefull);
  1609. if (attrs != INVALID_FILE_ATTRIBUTES) {
  1610. if (attrs & FILE_ATTRIBUTE_DIRECTORY)
  1611. disk_rmdir(namefull);
  1612. else
  1613. disk_unlink(namefull);
  1614. }
  1615. }
  1616. r = la_CreateHardLinkW(namefull, linkfull);
  1617. if (r == 0) {
  1618. la_dosmaperr(GetLastError());
  1619. r = errno;
  1620. } else
  1621. r = 0;
  1622. }
  1623. /*
  1624. * New cpio and pax formats allow hardlink entries
  1625. * to carry data, so we may have to open the file
  1626. * for hardlink entries.
  1627. *
  1628. * If the hardlink was successfully created and
  1629. * the archive doesn't have carry data for it,
  1630. * consider it to be non-authoritative for meta data.
  1631. * This is consistent with GNU tar and BSD pax.
  1632. * If the hardlink does carry data, let the last
  1633. * archive entry decide ownership.
  1634. */
  1635. if (r == 0 && a->filesize <= 0) {
  1636. a->todo = 0;
  1637. a->deferred = 0;
  1638. } else if (r == 0 && a->filesize > 0) {
  1639. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1640. ZeroMemory(&createExParams, sizeof(createExParams));
  1641. createExParams.dwSize = sizeof(createExParams);
  1642. createExParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
  1643. a->fh = CreateFile2(namefull, GENERIC_WRITE, 0,
  1644. TRUNCATE_EXISTING, &createExParams);
  1645. #else
  1646. a->fh = CreateFileW(namefull, GENERIC_WRITE, 0, NULL,
  1647. TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1648. #endif
  1649. if (a->fh == INVALID_HANDLE_VALUE) {
  1650. la_dosmaperr(GetLastError());
  1651. r = errno;
  1652. }
  1653. }
  1654. free(linkfull);
  1655. free(namefull);
  1656. return (r);
  1657. }
  1658. linkname = archive_entry_symlink_w(a->entry);
  1659. if (linkname != NULL) {
  1660. /*
  1661. * Unlinking and linking here is really not atomic,
  1662. * but doing it right, would require us to construct
  1663. * an mktemplink() function, and then use _wrename().
  1664. */
  1665. attrs = GetFileAttributesW(a->name);
  1666. if (attrs != INVALID_FILE_ATTRIBUTES) {
  1667. if (attrs & FILE_ATTRIBUTE_DIRECTORY)
  1668. disk_rmdir(a->name);
  1669. else
  1670. disk_unlink(a->name);
  1671. }
  1672. #if HAVE_SYMLINK
  1673. return symlink(linkname, a->name) ? errno : 0;
  1674. #else
  1675. errno = 0;
  1676. r = la_CreateSymbolicLinkW((const wchar_t *)a->name, linkname,
  1677. archive_entry_symlink_type(a->entry));
  1678. if (r == 0) {
  1679. if (errno == 0)
  1680. la_dosmaperr(GetLastError());
  1681. r = errno;
  1682. } else
  1683. r = 0;
  1684. return (r);
  1685. #endif
  1686. }
  1687. /*
  1688. * The remaining system calls all set permissions, so let's
  1689. * try to take advantage of that to avoid an extra chmod()
  1690. * call. (Recall that umask is set to zero right now!)
  1691. */
  1692. /* Mode we want for the final restored object (w/o file type bits). */
  1693. final_mode = a->mode & 07777;
  1694. /*
  1695. * The mode that will actually be restored in this step. Note
  1696. * that SUID, SGID, etc, require additional work to ensure
  1697. * security, so we never restore them at this point.
  1698. */
  1699. mode = final_mode & 0777 & ~a->user_umask;
  1700. switch (a->mode & AE_IFMT) {
  1701. default:
  1702. /* POSIX requires that we fall through here. */
  1703. /* FALLTHROUGH */
  1704. case AE_IFREG:
  1705. a->tmpname = NULL;
  1706. fullname = a->name;
  1707. /* O_WRONLY | O_CREAT | O_EXCL */
  1708. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1709. ZeroMemory(&createExParams, sizeof(createExParams));
  1710. createExParams.dwSize = sizeof(createExParams);
  1711. createExParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
  1712. a->fh = CreateFile2(fullname, GENERIC_WRITE, 0,
  1713. CREATE_NEW, &createExParams);
  1714. #else
  1715. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1716. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1717. #endif
  1718. if (a->fh == INVALID_HANDLE_VALUE &&
  1719. GetLastError() == ERROR_INVALID_NAME &&
  1720. fullname == a->name) {
  1721. fullname = __la_win_permissive_name_w(a->name);
  1722. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1723. a->fh = CreateFile2(fullname, GENERIC_WRITE, 0,
  1724. CREATE_NEW, &createExParams);
  1725. #else
  1726. a->fh = CreateFileW(fullname, GENERIC_WRITE, 0, NULL,
  1727. CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  1728. #endif
  1729. }
  1730. if (a->fh == INVALID_HANDLE_VALUE) {
  1731. if (GetLastError() == ERROR_ACCESS_DENIED) {
  1732. DWORD attr;
  1733. /* Simulate an errno of POSIX system. */
  1734. attr = GetFileAttributesW(fullname);
  1735. if (attr == (DWORD)-1)
  1736. la_dosmaperr(GetLastError());
  1737. else if (attr & FILE_ATTRIBUTE_DIRECTORY)
  1738. errno = EISDIR;
  1739. else
  1740. errno = EACCES;
  1741. } else
  1742. la_dosmaperr(GetLastError());
  1743. r = 1;
  1744. } else
  1745. r = 0;
  1746. if (fullname != a->name)
  1747. free(fullname);
  1748. break;
  1749. case AE_IFCHR:
  1750. case AE_IFBLK:
  1751. /* TODO: Find a better way to warn about our inability
  1752. * to restore a block device node. */
  1753. return (EINVAL);
  1754. case AE_IFDIR:
  1755. mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
  1756. fullname = a->name;
  1757. r = CreateDirectoryW(fullname, NULL);
  1758. if (r == 0 && GetLastError() == ERROR_INVALID_NAME &&
  1759. fullname == a->name) {
  1760. fullname = __la_win_permissive_name_w(a->name);
  1761. r = CreateDirectoryW(fullname, NULL);
  1762. }
  1763. if (r != 0) {
  1764. r = 0;
  1765. /* Defer setting dir times. */
  1766. a->deferred |= (a->todo & TODO_TIMES);
  1767. a->todo &= ~TODO_TIMES;
  1768. /* Never use an immediate chmod(). */
  1769. /* We can't avoid the chmod() entirely if EXTRACT_PERM
  1770. * because of SysV SGID inheritance. */
  1771. if ((mode != final_mode)
  1772. || (a->flags & ARCHIVE_EXTRACT_PERM))
  1773. a->deferred |= (a->todo & TODO_MODE);
  1774. a->todo &= ~TODO_MODE;
  1775. } else {
  1776. la_dosmaperr(GetLastError());
  1777. r = -1;
  1778. }
  1779. if (fullname != a->name)
  1780. free(fullname);
  1781. break;
  1782. case AE_IFIFO:
  1783. /* TODO: Find a better way to warn about our inability
  1784. * to restore a fifo. */
  1785. return (EINVAL);
  1786. }
  1787. /* All the system calls above set errno on failure. */
  1788. if (r)
  1789. return (errno);
  1790. /* If we managed to set the final mode, we've avoided a chmod(). */
  1791. if (mode == final_mode)
  1792. a->todo &= ~TODO_MODE;
  1793. return (0);
  1794. }
  1795. /*
  1796. * Cleanup function for archive_extract. Mostly, this involves processing
  1797. * the fixup list, which is used to address a number of problems:
  1798. * * Dir permissions might prevent us from restoring a file in that
  1799. * dir, so we restore the dir with minimum 0700 permissions first,
  1800. * then correct the mode at the end.
  1801. * * Similarly, the act of restoring a file touches the directory
  1802. * and changes the timestamp on the dir, so we have to touch-up dir
  1803. * timestamps at the end as well.
  1804. * * Some file flags can interfere with the restore by, for example,
  1805. * preventing the creation of hardlinks to those files.
  1806. * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
  1807. *
  1808. * Note that tar/cpio do not require that archives be in a particular
  1809. * order; there is no way to know when the last file has been restored
  1810. * within a directory, so there's no way to optimize the memory usage
  1811. * here by fixing up the directory any earlier than the
  1812. * end-of-archive.
  1813. *
  1814. * XXX TODO: Directory ACLs should be restored here, for the same
  1815. * reason we set directory perms here. XXX
  1816. */
  1817. static int
  1818. _archive_write_disk_close(struct archive *_a)
  1819. {
  1820. struct archive_write_disk *a = (struct archive_write_disk *)_a;
  1821. struct fixup_entry *next, *p;
  1822. int ret;
  1823. archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
  1824. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1825. "archive_write_disk_close");
  1826. ret = _archive_write_disk_finish_entry(&a->archive);
  1827. /* Sort dir list so directories are fixed up in depth-first order. */
  1828. p = sort_dir_list(a->fixup_list);
  1829. while (p != NULL) {
  1830. a->pst = NULL; /* Mark stat cache as out-of-date. */
  1831. if (p->fixup & TODO_TIMES) {
  1832. set_times(a, INVALID_HANDLE_VALUE, p->mode, p->name,
  1833. p->atime, p->atime_nanos,
  1834. p->birthtime, p->birthtime_nanos,
  1835. p->mtime, p->mtime_nanos,
  1836. p->ctime, p->ctime_nanos);
  1837. }
  1838. if (p->fixup & TODO_MODE_BASE)
  1839. la_chmod(p->name, p->mode);
  1840. if (p->fixup & TODO_ACLS)
  1841. set_acls(a, INVALID_HANDLE_VALUE, p->name, &p->acl);
  1842. if (p->fixup & TODO_FFLAGS)
  1843. set_fflags_platform(p->name, p->fflags_set, 0);
  1844. next = p->next;
  1845. archive_acl_clear(&p->acl);
  1846. free(p->name);
  1847. free(p);
  1848. p = next;
  1849. }
  1850. a->fixup_list = NULL;
  1851. return (ret);
  1852. }
  1853. static int
  1854. _archive_write_disk_free(struct archive *_a)
  1855. {
  1856. struct archive_write_disk *a;
  1857. int ret;
  1858. if (_a == NULL)
  1859. return (ARCHIVE_OK);
  1860. archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
  1861. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
  1862. a = (struct archive_write_disk *)_a;
  1863. ret = _archive_write_disk_close(&a->archive);
  1864. archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
  1865. archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
  1866. archive_entry_free(a->entry);
  1867. archive_wstring_free(&a->_name_data);
  1868. archive_wstring_free(&a->_tmpname_data);
  1869. archive_string_free(&a->archive.error_string);
  1870. archive_wstring_free(&a->path_safe);
  1871. a->archive.magic = 0;
  1872. __archive_clean(&a->archive);
  1873. free(a);
  1874. return (ret);
  1875. }
  1876. /*
  1877. * Simple O(n log n) merge sort to order the fixup list. In
  1878. * particular, we want to restore dir timestamps depth-first.
  1879. */
  1880. static struct fixup_entry *
  1881. sort_dir_list(struct fixup_entry *p)
  1882. {
  1883. struct fixup_entry *a, *b, *t;
  1884. if (p == NULL)
  1885. return (NULL);
  1886. /* A one-item list is already sorted. */
  1887. if (p->next == NULL)
  1888. return (p);
  1889. /* Step 1: split the list. */
  1890. t = p;
  1891. a = p->next->next;
  1892. while (a != NULL) {
  1893. /* Step a twice, t once. */
  1894. a = a->next;
  1895. if (a != NULL)
  1896. a = a->next;
  1897. t = t->next;
  1898. }
  1899. /* Now, t is at the mid-point, so break the list here. */
  1900. b = t->next;
  1901. t->next = NULL;
  1902. a = p;
  1903. /* Step 2: Recursively sort the two sub-lists. */
  1904. a = sort_dir_list(a);
  1905. b = sort_dir_list(b);
  1906. /* Step 3: Merge the returned lists. */
  1907. /* Pick the first element for the merged list. */
  1908. if (wcscmp(a->name, b->name) > 0) {
  1909. t = p = a;
  1910. a = a->next;
  1911. } else {
  1912. t = p = b;
  1913. b = b->next;
  1914. }
  1915. /* Always put the later element on the list first. */
  1916. while (a != NULL && b != NULL) {
  1917. if (wcscmp(a->name, b->name) > 0) {
  1918. t->next = a;
  1919. a = a->next;
  1920. } else {
  1921. t->next = b;
  1922. b = b->next;
  1923. }
  1924. t = t->next;
  1925. }
  1926. /* Only one list is non-empty, so just splice it on. */
  1927. if (a != NULL)
  1928. t->next = a;
  1929. if (b != NULL)
  1930. t->next = b;
  1931. return (p);
  1932. }
  1933. /*
  1934. * Returns a new, initialized fixup entry.
  1935. *
  1936. * TODO: Reduce the memory requirements for this list by using a tree
  1937. * structure rather than a simple list of names.
  1938. */
  1939. static struct fixup_entry *
  1940. new_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1941. {
  1942. struct fixup_entry *fe;
  1943. fe = calloc(1, sizeof(struct fixup_entry));
  1944. if (fe == NULL)
  1945. return (NULL);
  1946. fe->next = a->fixup_list;
  1947. a->fixup_list = fe;
  1948. fe->fixup = 0;
  1949. fe->name = _wcsdup(pathname);
  1950. fe->fflags_set = 0;
  1951. return (fe);
  1952. }
  1953. /*
  1954. * Returns a fixup structure for the current entry.
  1955. */
  1956. static struct fixup_entry *
  1957. current_fixup(struct archive_write_disk *a, const wchar_t *pathname)
  1958. {
  1959. if (a->current_fixup == NULL)
  1960. a->current_fixup = new_fixup(a, pathname);
  1961. return (a->current_fixup);
  1962. }
  1963. /*
  1964. * TODO: The deep-directory support bypasses this; disable deep directory
  1965. * support if we're doing symlink checks.
  1966. */
  1967. /*
  1968. * TODO: Someday, integrate this with the deep dir support; they both
  1969. * scan the path and both can be optimized by comparing against other
  1970. * recent paths.
  1971. */
  1972. static int
  1973. check_symlinks(struct archive_write_disk *a)
  1974. {
  1975. wchar_t *pn, *p;
  1976. wchar_t c;
  1977. int r;
  1978. BY_HANDLE_FILE_INFORMATION st;
  1979. mode_t st_mode;
  1980. /*
  1981. * Guard against symlink tricks. Reject any archive entry whose
  1982. * destination would be altered by a symlink.
  1983. */
  1984. /* Whatever we checked last time doesn't need to be re-checked. */
  1985. pn = a->name;
  1986. p = a->path_safe.s;
  1987. while ((*pn != '\0') && (*p == *pn))
  1988. ++p, ++pn;
  1989. /* Skip leading backslashes */
  1990. while (*pn == '\\')
  1991. ++pn;
  1992. c = pn[0];
  1993. /* Keep going until we've checked the entire name. */
  1994. while (pn[0] != '\0' && (pn[0] != '\\' || pn[1] != '\0')) {
  1995. /* Skip the next path element. */
  1996. while (*pn != '\0' && *pn != '\\')
  1997. ++pn;
  1998. c = pn[0];
  1999. pn[0] = '\0';
  2000. /* Check that we haven't hit a symlink. */
  2001. r = file_information(a, a->name, &st, &st_mode, 1);
  2002. if (r != 0) {
  2003. /* We've hit a dir that doesn't exist; stop now. */
  2004. if (errno == ENOENT)
  2005. break;
  2006. } else if (S_ISLNK(st_mode)) {
  2007. if (c == '\0') {
  2008. /*
  2009. * Last element is a file or directory symlink.
  2010. * Remove it so we can overwrite it with the
  2011. * item being extracted.
  2012. */
  2013. if (a->flags &
  2014. ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
  2015. (void)clear_nochange_fflags(a);
  2016. }
  2017. if (st.dwFileAttributes &
  2018. FILE_ATTRIBUTE_DIRECTORY) {
  2019. r = disk_rmdir(a->name);
  2020. } else {
  2021. r = disk_unlink(a->name);
  2022. }
  2023. if (r) {
  2024. archive_set_error(&a->archive, errno,
  2025. "Could not remove symlink %ls",
  2026. a->name);
  2027. pn[0] = c;
  2028. return (ARCHIVE_FAILED);
  2029. }
  2030. a->pst = NULL;
  2031. /*
  2032. * Even if we did remove it, a warning
  2033. * is in order. The warning is silly,
  2034. * though, if we're just replacing one
  2035. * symlink with another symlink.
  2036. */
  2037. if (!S_ISLNK(a->mode)) {
  2038. archive_set_error(&a->archive, 0,
  2039. "Removing symlink %ls",
  2040. a->name);
  2041. }
  2042. /* Symlink gone. No more problem! */
  2043. pn[0] = c;
  2044. return (0);
  2045. } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
  2046. /* User asked us to remove problems. */
  2047. if (a->flags &
  2048. ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) {
  2049. (void)clear_nochange_fflags(a);
  2050. }
  2051. if (st.dwFileAttributes &
  2052. FILE_ATTRIBUTE_DIRECTORY) {
  2053. r = disk_rmdir(a->name);
  2054. } else {
  2055. r = disk_unlink(a->name);
  2056. }
  2057. if (r != 0) {
  2058. archive_set_error(&a->archive, 0,
  2059. "Cannot remove intervening "
  2060. "symlink %ls", a->name);
  2061. pn[0] = c;
  2062. return (ARCHIVE_FAILED);
  2063. }
  2064. a->pst = NULL;
  2065. } else {
  2066. archive_set_error(&a->archive, 0,
  2067. "Cannot extract through symlink %ls",
  2068. a->name);
  2069. pn[0] = c;
  2070. return (ARCHIVE_FAILED);
  2071. }
  2072. }
  2073. if (!c)
  2074. break;
  2075. pn[0] = c;
  2076. pn++;
  2077. }
  2078. pn[0] = c;
  2079. /* We've checked and/or cleaned the whole path, so remember it. */
  2080. archive_wstrcpy(&a->path_safe, a->name);
  2081. return (ARCHIVE_OK);
  2082. }
  2083. static int
  2084. guidword(wchar_t *p, int n)
  2085. {
  2086. int i;
  2087. for (i = 0; i < n; i++) {
  2088. if ((*p >= L'0' && *p <= L'9') ||
  2089. (*p >= L'a' && *p <= L'f') ||
  2090. (*p >= L'A' && *p <= L'F'))
  2091. p++;
  2092. else
  2093. return (-1);
  2094. }
  2095. return (0);
  2096. }
  2097. /*
  2098. * Canonicalize the pathname. In particular, this strips duplicate
  2099. * '\' characters, '.' elements, and trailing '\'. It also raises an
  2100. * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
  2101. * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS)
  2102. * if the path is absolute.
  2103. */
  2104. static int
  2105. cleanup_pathname(struct archive_write_disk *a, wchar_t *name)
  2106. {
  2107. wchar_t *dest, *src, *p, *top;
  2108. wchar_t separator = L'\0';
  2109. BOOL absolute_path = 0;
  2110. p = name;
  2111. if (*p == L'\0') {
  2112. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2113. "Invalid empty pathname");
  2114. return (ARCHIVE_FAILED);
  2115. }
  2116. /* Replace '/' by '\' */
  2117. for (; *p != L'\0'; p++) {
  2118. if (*p == L'/')
  2119. *p = L'\\';
  2120. }
  2121. p = name;
  2122. /* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or
  2123. * "\\?\Volume{GUID}\"
  2124. * (absolute path prefixes used by Windows API) */
  2125. if (p[0] == L'\\' && p[1] == L'\\' &&
  2126. (p[2] == L'.' || p[2] == L'?') && p[3] == L'\\')
  2127. {
  2128. absolute_path = 1;
  2129. /* A path begin with "\\?\UNC\" */
  2130. if (p[2] == L'?' &&
  2131. (p[4] == L'U' || p[4] == L'u') &&
  2132. (p[5] == L'N' || p[5] == L'n') &&
  2133. (p[6] == L'C' || p[6] == L'c') &&
  2134. p[7] == L'\\')
  2135. p += 8;
  2136. /* A path begin with "\\?\Volume{GUID}\" */
  2137. else if (p[2] == L'?' &&
  2138. (p[4] == L'V' || p[4] == L'v') &&
  2139. (p[5] == L'O' || p[5] == L'o') &&
  2140. (p[6] == L'L' || p[6] == L'l') &&
  2141. (p[7] == L'U' || p[7] == L'u') &&
  2142. (p[8] == L'M' || p[8] == L'm') &&
  2143. (p[9] == L'E' || p[9] == L'e') &&
  2144. p[10] == L'{') {
  2145. if (guidword(p+11, 8) == 0 && p[19] == L'-' &&
  2146. guidword(p+20, 4) == 0 && p[24] == L'-' &&
  2147. guidword(p+25, 4) == 0 && p[29] == L'-' &&
  2148. guidword(p+30, 4) == 0 && p[34] == L'-' &&
  2149. guidword(p+35, 12) == 0 && p[47] == L'}' &&
  2150. p[48] == L'\\')
  2151. p += 49;
  2152. else
  2153. p += 4;
  2154. /* A path begin with "\\.\PhysicalDriveX" */
  2155. } else if (p[2] == L'.' &&
  2156. (p[4] == L'P' || p[4] == L'p') &&
  2157. (p[5] == L'H' || p[5] == L'h') &&
  2158. (p[6] == L'Y' || p[6] == L'y') &&
  2159. (p[7] == L'S' || p[7] == L's') &&
  2160. (p[8] == L'I' || p[8] == L'i') &&
  2161. (p[9] == L'C' || p[9] == L'c') &&
  2162. (p[9] == L'A' || p[9] == L'a') &&
  2163. (p[9] == L'L' || p[9] == L'l') &&
  2164. (p[9] == L'D' || p[9] == L'd') &&
  2165. (p[9] == L'R' || p[9] == L'r') &&
  2166. (p[9] == L'I' || p[9] == L'i') &&
  2167. (p[9] == L'V' || p[9] == L'v') &&
  2168. (p[9] == L'E' || p[9] == L'e') &&
  2169. (p[10] >= L'0' && p[10] <= L'9') &&
  2170. p[11] == L'\0') {
  2171. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2172. "Path is a physical drive name");
  2173. return (ARCHIVE_FAILED);
  2174. } else
  2175. p += 4;
  2176. /* Network drive path like "\\<server-name>\<share-name>\file" */
  2177. } else if (p[0] == L'\\' && p[1] == L'\\') {
  2178. absolute_path = 1;
  2179. p += 2;
  2180. }
  2181. /* Skip leading drive letter from archives created
  2182. * on Windows. */
  2183. if (((p[0] >= L'a' && p[0] <= L'z') ||
  2184. (p[0] >= L'A' && p[0] <= L'Z')) &&
  2185. p[1] == L':') {
  2186. if (p[2] == L'\0') {
  2187. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2188. "Path is a drive name");
  2189. return (ARCHIVE_FAILED);
  2190. }
  2191. absolute_path = 1;
  2192. if (p[2] == L'\\')
  2193. p += 2;
  2194. }
  2195. if (absolute_path && (a->flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS)) {
  2196. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Path is absolute");
  2197. return (ARCHIVE_FAILED);
  2198. }
  2199. top = dest = src = p;
  2200. /* Rewrite the path name if its character is a unusable. */
  2201. for (; *p != L'\0'; p++) {
  2202. if (*p == L':' || *p == L'*' || *p == L'?' || *p == L'"' ||
  2203. *p == L'<' || *p == L'>' || *p == L'|')
  2204. *p = L'_';
  2205. }
  2206. /* Skip leading '\'. */
  2207. if (*src == L'\\')
  2208. separator = *src++;
  2209. /* Scan the pathname one element at a time. */
  2210. for (;;) {
  2211. /* src points to first char after '\' */
  2212. if (src[0] == L'\0') {
  2213. break;
  2214. } else if (src[0] == L'\\') {
  2215. /* Found '\\'('//'), ignore second one. */
  2216. src++;
  2217. continue;
  2218. } else if (src[0] == L'.') {
  2219. if (src[1] == L'\0') {
  2220. /* Ignore trailing '.' */
  2221. break;
  2222. } else if (src[1] == L'\\') {
  2223. /* Skip '.\'. */
  2224. src += 2;
  2225. continue;
  2226. } else if (src[1] == L'.') {
  2227. if (src[2] == L'\\' || src[2] == L'\0') {
  2228. /* Conditionally warn about '..' */
  2229. if (a->flags &
  2230. ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
  2231. archive_set_error(&a->archive,
  2232. ARCHIVE_ERRNO_MISC,
  2233. "Path contains '..'");
  2234. return (ARCHIVE_FAILED);
  2235. }
  2236. }
  2237. /*
  2238. * Note: Under no circumstances do we
  2239. * remove '..' elements. In
  2240. * particular, restoring
  2241. * '\foo\..\bar\' should create the
  2242. * 'foo' dir as a side-effect.
  2243. */
  2244. }
  2245. }
  2246. /* Copy current element, including leading '\'. */
  2247. if (separator)
  2248. *dest++ = L'\\';
  2249. while (*src != L'\0' && *src != L'\\') {
  2250. *dest++ = *src++;
  2251. }
  2252. if (*src == L'\0')
  2253. break;
  2254. /* Skip '\' separator. */
  2255. separator = *src++;
  2256. }
  2257. /*
  2258. * We've just copied zero or more path elements, not including the
  2259. * final '\'.
  2260. */
  2261. if (dest == top) {
  2262. /*
  2263. * Nothing got copied. The path must have been something
  2264. * like '.' or '\' or './' or '/././././/./'.
  2265. */
  2266. if (separator)
  2267. *dest++ = L'\\';
  2268. else
  2269. *dest++ = L'.';
  2270. }
  2271. /* Terminate the result. */
  2272. *dest = L'\0';
  2273. return (ARCHIVE_OK);
  2274. }
  2275. /*
  2276. * Create the parent directory of the specified path, assuming path
  2277. * is already in mutable storage.
  2278. */
  2279. static int
  2280. create_parent_dir(struct archive_write_disk *a, wchar_t *path)
  2281. {
  2282. wchar_t *slash;
  2283. int r;
  2284. /* Remove tail element to obtain parent name. */
  2285. slash = wcsrchr(path, L'\\');
  2286. if (slash == NULL)
  2287. return (ARCHIVE_OK);
  2288. *slash = L'\0';
  2289. r = create_dir(a, path);
  2290. *slash = L'\\';
  2291. return (r);
  2292. }
  2293. /*
  2294. * Create the specified dir, recursing to create parents as necessary.
  2295. *
  2296. * Returns ARCHIVE_OK if the path exists when we're done here.
  2297. * Otherwise, returns ARCHIVE_FAILED.
  2298. * Assumes path is in mutable storage; path is unchanged on exit.
  2299. */
  2300. static int
  2301. create_dir(struct archive_write_disk *a, wchar_t *path)
  2302. {
  2303. BY_HANDLE_FILE_INFORMATION st;
  2304. struct fixup_entry *le;
  2305. wchar_t *slash, *base, *full;
  2306. mode_t mode_final, mode, st_mode;
  2307. int r;
  2308. /* Check for special names and just skip them. */
  2309. slash = wcsrchr(path, L'\\');
  2310. if (slash == NULL)
  2311. base = path;
  2312. else
  2313. base = slash + 1;
  2314. if (base[0] == L'\0' ||
  2315. (base[0] == L'.' && base[1] == L'\0') ||
  2316. (base[0] == L'.' && base[1] == L'.' && base[2] == L'\0')) {
  2317. /* Don't bother trying to create null path, '.', or '..'. */
  2318. if (slash != NULL) {
  2319. *slash = L'\0';
  2320. r = create_dir(a, path);
  2321. *slash = L'\\';
  2322. return (r);
  2323. }
  2324. return (ARCHIVE_OK);
  2325. }
  2326. /*
  2327. * Yes, this should be stat() and not lstat(). Using lstat()
  2328. * here loses the ability to extract through symlinks. Also note
  2329. * that this should not use the a->st cache.
  2330. */
  2331. if (file_information(a, path, &st, &st_mode, 0) == 0) {
  2332. if (S_ISDIR(st_mode))
  2333. return (ARCHIVE_OK);
  2334. if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
  2335. archive_set_error(&a->archive, EEXIST,
  2336. "Can't create directory '%ls'", path);
  2337. return (ARCHIVE_FAILED);
  2338. }
  2339. if (disk_unlink(path) != 0) {
  2340. archive_set_error(&a->archive, errno,
  2341. "Can't create directory '%ls': "
  2342. "Conflicting file cannot be removed",
  2343. path);
  2344. return (ARCHIVE_FAILED);
  2345. }
  2346. } else if (errno != ENOENT && errno != ENOTDIR) {
  2347. /* Stat failed? */
  2348. archive_set_error(&a->archive, errno,
  2349. "Can't test directory '%ls'", path);
  2350. return (ARCHIVE_FAILED);
  2351. } else if (slash != NULL) {
  2352. *slash = '\0';
  2353. r = create_dir(a, path);
  2354. *slash = '\\';
  2355. if (r != ARCHIVE_OK)
  2356. return (r);
  2357. }
  2358. /*
  2359. * Mode we want for the final restored directory. Per POSIX,
  2360. * implicitly-created dirs must be created obeying the umask.
  2361. * There's no mention whether this is different for privileged
  2362. * restores (which the rest of this code handles by pretending
  2363. * umask=0). I've chosen here to always obey the user's umask for
  2364. * implicit dirs, even if _EXTRACT_PERM was specified.
  2365. */
  2366. mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
  2367. /* Mode we want on disk during the restore process. */
  2368. mode = mode_final;
  2369. mode |= MINIMUM_DIR_MODE;
  2370. mode &= MAXIMUM_DIR_MODE;
  2371. /*
  2372. * Apply __la_win_permissive_name_w to path in order to
  2373. * remove '../' path string.
  2374. */
  2375. full = __la_win_permissive_name_w(path);
  2376. if (full == NULL)
  2377. errno = EINVAL;
  2378. else if (CreateDirectoryW(full, NULL) != 0) {
  2379. if (mode != mode_final) {
  2380. le = new_fixup(a, path);
  2381. le->fixup |=TODO_MODE_BASE;
  2382. le->mode = mode_final;
  2383. }
  2384. free(full);
  2385. return (ARCHIVE_OK);
  2386. } else {
  2387. la_dosmaperr(GetLastError());
  2388. }
  2389. free(full);
  2390. /*
  2391. * Without the following check, a/b/../b/c/d fails at the
  2392. * second visit to 'b', so 'd' can't be created. Note that we
  2393. * don't add it to the fixup list here, as it's already been
  2394. * added.
  2395. */
  2396. if (file_information(a, path, &st, &st_mode, 0) == 0 &&
  2397. S_ISDIR(st_mode))
  2398. return (ARCHIVE_OK);
  2399. archive_set_error(&a->archive, errno, "Failed to create dir '%ls'",
  2400. path);
  2401. return (ARCHIVE_FAILED);
  2402. }
  2403. /*
  2404. * Note: Although we can skip setting the user id if the desired user
  2405. * id matches the current user, we cannot skip setting the group, as
  2406. * many systems set the gid based on the containing directory. So
  2407. * we have to perform a chown syscall if we want to set the SGID
  2408. * bit. (The alternative is to stat() and then possibly chown(); it's
  2409. * more efficient to skip the stat() and just always chown().) Note
  2410. * that a successful chown() here clears the TODO_SGID_CHECK bit, which
  2411. * allows set_mode to skip the stat() check for the GID.
  2412. */
  2413. static int
  2414. set_ownership(struct archive_write_disk *a)
  2415. {
  2416. /* unfortunately, on win32 there is no 'root' user with uid 0,
  2417. so we just have to try the chown and see if it works */
  2418. /* If we know we can't change it, don't bother trying. */
  2419. if (a->user_uid != 0 && a->user_uid != a->uid) {
  2420. archive_set_error(&a->archive, errno,
  2421. "Can't set UID=%jd", (intmax_t)a->uid);
  2422. return (ARCHIVE_WARN);
  2423. }
  2424. archive_set_error(&a->archive, errno,
  2425. "Can't set user=%jd/group=%jd for %ls",
  2426. (intmax_t)a->uid, (intmax_t)a->gid, a->name);
  2427. return (ARCHIVE_WARN);
  2428. }
  2429. static int
  2430. set_times(struct archive_write_disk *a,
  2431. HANDLE h, int mode, const wchar_t *name,
  2432. time_t atime, long atime_nanos,
  2433. time_t birthtime, long birthtime_nanos,
  2434. time_t mtime, long mtime_nanos,
  2435. time_t ctime_sec, long ctime_nanos)
  2436. {
  2437. HANDLE hw = 0;
  2438. ULARGE_INTEGER wintm;
  2439. FILETIME *pfbtime;
  2440. FILETIME fatime, fbtime, fmtime;
  2441. (void)ctime_sec; /* UNUSED */
  2442. (void)ctime_nanos; /* UNUSED */
  2443. if (h != INVALID_HANDLE_VALUE) {
  2444. hw = NULL;
  2445. } else {
  2446. wchar_t *ws;
  2447. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  2448. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  2449. #endif
  2450. if (S_ISLNK(mode))
  2451. return (ARCHIVE_OK);
  2452. ws = __la_win_permissive_name_w(name);
  2453. if (ws == NULL)
  2454. goto settimes_failed;
  2455. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  2456. ZeroMemory(&createExParams, sizeof(createExParams));
  2457. createExParams.dwSize = sizeof(createExParams);
  2458. createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
  2459. hw = CreateFile2(ws, FILE_WRITE_ATTRIBUTES, 0,
  2460. OPEN_EXISTING, &createExParams);
  2461. #else
  2462. hw = CreateFileW(ws, FILE_WRITE_ATTRIBUTES,
  2463. 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  2464. #endif
  2465. free(ws);
  2466. if (hw == INVALID_HANDLE_VALUE)
  2467. goto settimes_failed;
  2468. h = hw;
  2469. }
  2470. wintm.QuadPart = unix_to_ntfs(atime, atime_nanos);
  2471. fatime.dwLowDateTime = wintm.LowPart;
  2472. fatime.dwHighDateTime = wintm.HighPart;
  2473. wintm.QuadPart = unix_to_ntfs(mtime, mtime_nanos);
  2474. fmtime.dwLowDateTime = wintm.LowPart;
  2475. fmtime.dwHighDateTime = wintm.HighPart;
  2476. /*
  2477. * SetFileTime() supports birthtime.
  2478. */
  2479. if (birthtime > 0 || birthtime_nanos > 0) {
  2480. wintm.QuadPart = unix_to_ntfs(birthtime, birthtime_nanos);
  2481. fbtime.dwLowDateTime = wintm.LowPart;
  2482. fbtime.dwHighDateTime = wintm.HighPart;
  2483. pfbtime = &fbtime;
  2484. } else
  2485. pfbtime = NULL;
  2486. if (SetFileTime(h, pfbtime, &fatime, &fmtime) == 0)
  2487. goto settimes_failed;
  2488. CloseHandle(hw);
  2489. return (ARCHIVE_OK);
  2490. settimes_failed:
  2491. CloseHandle(hw);
  2492. archive_set_error(&a->archive, EINVAL, "Can't restore time");
  2493. return (ARCHIVE_WARN);
  2494. }
  2495. static int
  2496. set_times_from_entry(struct archive_write_disk *a)
  2497. {
  2498. time_t atime, birthtime, mtime, ctime_sec;
  2499. long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
  2500. /* Suitable defaults. */
  2501. atime = birthtime = mtime = ctime_sec = a->start_time;
  2502. atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
  2503. /* If no time was provided, we're done. */
  2504. if (!archive_entry_atime_is_set(a->entry)
  2505. && !archive_entry_birthtime_is_set(a->entry)
  2506. && !archive_entry_mtime_is_set(a->entry))
  2507. return (ARCHIVE_OK);
  2508. if (archive_entry_atime_is_set(a->entry)) {
  2509. atime = archive_entry_atime(a->entry);
  2510. atime_nsec = archive_entry_atime_nsec(a->entry);
  2511. }
  2512. if (archive_entry_birthtime_is_set(a->entry)) {
  2513. birthtime = archive_entry_birthtime(a->entry);
  2514. birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
  2515. }
  2516. if (archive_entry_mtime_is_set(a->entry)) {
  2517. mtime = archive_entry_mtime(a->entry);
  2518. mtime_nsec = archive_entry_mtime_nsec(a->entry);
  2519. }
  2520. if (archive_entry_ctime_is_set(a->entry)) {
  2521. ctime_sec = archive_entry_ctime(a->entry);
  2522. ctime_nsec = archive_entry_ctime_nsec(a->entry);
  2523. }
  2524. return set_times(a, a->fh, a->mode, a->name,
  2525. atime, atime_nsec,
  2526. birthtime, birthtime_nsec,
  2527. mtime, mtime_nsec,
  2528. ctime_sec, ctime_nsec);
  2529. }
  2530. static int
  2531. set_mode(struct archive_write_disk *a, int mode)
  2532. {
  2533. int r = ARCHIVE_OK;
  2534. mode &= 07777; /* Strip off file type bits. */
  2535. if (a->todo & TODO_SGID_CHECK) {
  2536. /*
  2537. * If we don't know the GID is right, we must stat()
  2538. * to verify it. We can't just check the GID of this
  2539. * process, since systems sometimes set GID from
  2540. * the enclosing dir or based on ACLs.
  2541. */
  2542. if ((r = lazy_stat(a)) != ARCHIVE_OK)
  2543. return (r);
  2544. if (0 != a->gid) {
  2545. mode &= ~ S_ISGID;
  2546. }
  2547. /* While we're here, double-check the UID. */
  2548. if (0 != a->uid
  2549. && (a->todo & TODO_SUID)) {
  2550. mode &= ~ S_ISUID;
  2551. }
  2552. a->todo &= ~TODO_SGID_CHECK;
  2553. a->todo &= ~TODO_SUID_CHECK;
  2554. } else if (a->todo & TODO_SUID_CHECK) {
  2555. /*
  2556. * If we don't know the UID is right, we can just check
  2557. * the user, since all systems set the file UID from
  2558. * the process UID.
  2559. */
  2560. if (a->user_uid != a->uid) {
  2561. mode &= ~ S_ISUID;
  2562. }
  2563. a->todo &= ~TODO_SUID_CHECK;
  2564. }
  2565. if (S_ISLNK(a->mode)) {
  2566. #ifdef HAVE_LCHMOD
  2567. /*
  2568. * If this is a symlink, use lchmod(). If the
  2569. * platform doesn't support lchmod(), just skip it. A
  2570. * platform that doesn't provide a way to set
  2571. * permissions on symlinks probably ignores
  2572. * permissions on symlinks, so a failure here has no
  2573. * impact.
  2574. */
  2575. if (lchmod(a->name, mode) != 0) {
  2576. archive_set_error(&a->archive, errno,
  2577. "Can't set permissions to 0%o", (int)mode);
  2578. r = ARCHIVE_WARN;
  2579. }
  2580. #endif
  2581. } else if (!S_ISDIR(a->mode)) {
  2582. /*
  2583. * If it's not a symlink and not a dir, then use
  2584. * fchmod() or chmod(), depending on whether we have
  2585. * an fd. Dirs get their perms set during the
  2586. * post-extract fixup, which is handled elsewhere.
  2587. */
  2588. #ifdef HAVE_FCHMOD
  2589. if (a->fd >= 0) {
  2590. if (fchmod(a->fd, mode) != 0) {
  2591. archive_set_error(&a->archive, errno,
  2592. "Can't set permissions to 0%o", (int)mode);
  2593. r = ARCHIVE_WARN;
  2594. }
  2595. } else
  2596. #endif
  2597. /* If this platform lacks fchmod(), then
  2598. * we'll just use chmod(). */
  2599. if (la_chmod(a->name, mode) != 0) {
  2600. archive_set_error(&a->archive, errno,
  2601. "Can't set permissions to 0%o", (int)mode);
  2602. r = ARCHIVE_WARN;
  2603. }
  2604. }
  2605. return (r);
  2606. }
  2607. static int set_fflags_platform(const wchar_t *name, unsigned long fflags_set,
  2608. unsigned long fflags_clear)
  2609. {
  2610. DWORD oldflags, newflags;
  2611. wchar_t *fullname;
  2612. const DWORD settable_flags =
  2613. FILE_ATTRIBUTE_ARCHIVE |
  2614. FILE_ATTRIBUTE_HIDDEN |
  2615. FILE_ATTRIBUTE_NORMAL |
  2616. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
  2617. FILE_ATTRIBUTE_OFFLINE |
  2618. FILE_ATTRIBUTE_READONLY |
  2619. FILE_ATTRIBUTE_SYSTEM |
  2620. FILE_ATTRIBUTE_TEMPORARY;
  2621. oldflags = GetFileAttributesW(name);
  2622. if (oldflags == (DWORD)-1 &&
  2623. GetLastError() == ERROR_INVALID_NAME) {
  2624. fullname = __la_win_permissive_name_w(name);
  2625. oldflags = GetFileAttributesW(fullname);
  2626. }
  2627. if (oldflags == (DWORD)-1) {
  2628. la_dosmaperr(GetLastError());
  2629. return (ARCHIVE_WARN);
  2630. }
  2631. newflags = ((oldflags & ~fflags_clear) | fflags_set) & settable_flags;
  2632. if(SetFileAttributesW(name, newflags) == 0)
  2633. return (ARCHIVE_WARN);
  2634. return (ARCHIVE_OK);
  2635. }
  2636. static int
  2637. clear_nochange_fflags(struct archive_write_disk *a)
  2638. {
  2639. return (set_fflags_platform(a->name, 0, FILE_ATTRIBUTE_READONLY));
  2640. }
  2641. static int
  2642. set_fflags(struct archive_write_disk *a)
  2643. {
  2644. unsigned long set, clear;
  2645. if (a->todo & TODO_FFLAGS) {
  2646. archive_entry_fflags(a->entry, &set, &clear);
  2647. if (set == 0 && clear == 0)
  2648. return (ARCHIVE_OK);
  2649. return (set_fflags_platform(a->name, set, clear));
  2650. }
  2651. return (ARCHIVE_OK);
  2652. }
  2653. /* Default empty function body to satisfy mainline code. */
  2654. static int
  2655. set_acls(struct archive_write_disk *a, HANDLE h, const wchar_t *name,
  2656. struct archive_acl *acl)
  2657. {
  2658. (void)a; /* UNUSED */
  2659. (void)h; /* UNUSED */
  2660. (void)name; /* UNUSED */
  2661. (void)acl; /* UNUSED */
  2662. return (ARCHIVE_OK);
  2663. }
  2664. /*
  2665. * Restore extended attributes - stub implementation for unsupported systems
  2666. */
  2667. static int
  2668. set_xattrs(struct archive_write_disk *a)
  2669. {
  2670. static int warning_done = 0;
  2671. /* If there aren't any extended attributes, then it's okay not
  2672. * to extract them, otherwise, issue a single warning. */
  2673. if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
  2674. warning_done = 1;
  2675. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2676. "Cannot restore extended attributes on this system");
  2677. return (ARCHIVE_WARN);
  2678. }
  2679. /* Warning was already emitted; suppress further warnings. */
  2680. return (ARCHIVE_OK);
  2681. }
  2682. /*
  2683. * Test if file on disk is older than entry.
  2684. */
  2685. static int
  2686. older(BY_HANDLE_FILE_INFORMATION *st, struct archive_entry *entry)
  2687. {
  2688. int64_t sec;
  2689. uint32_t nsec;
  2690. ntfs_to_unix(FILETIME_to_ntfs(&st->ftLastWriteTime), &sec, &nsec);
  2691. /* First, test the seconds and return if we have a definite answer. */
  2692. /* Definitely older. */
  2693. if (sec < archive_entry_mtime(entry))
  2694. return (1);
  2695. /* Definitely younger. */
  2696. if (sec > archive_entry_mtime(entry))
  2697. return (0);
  2698. if ((long)nsec < archive_entry_mtime_nsec(entry))
  2699. return (1);
  2700. /* Same age or newer, so not older. */
  2701. return (0);
  2702. }
  2703. #endif /* _WIN32 && !__CYGWIN__ */