archive_write_disk_windows.c 77 KB

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