archive_write_disk_windows.c 77 KB

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