archive_write_disk_windows.c 69 KB

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