archive_read_disk_windows.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. /*-
  2. * Copyright (c) 2003-2009 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #include <winioctl.h>
  36. #include "archive.h"
  37. #include "archive_string.h"
  38. #include "archive_entry.h"
  39. #include "archive_private.h"
  40. #include "archive_read_disk_private.h"
  41. #include "archive_time_private.h"
  42. #ifndef O_BINARY
  43. #define O_BINARY 0
  44. #endif
  45. #ifndef IO_REPARSE_TAG_SYMLINK
  46. /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
  47. #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
  48. #endif
  49. /* To deal with absolute symlink issues */
  50. #define START_ABSOLUTE_SYMLINK_REPARSE L"\\??\\"
  51. /*-
  52. * This is a new directory-walking system that addresses a number
  53. * of problems I've had with fts(3). In particular, it has no
  54. * pathname-length limits (other than the size of 'int'), handles
  55. * deep logical traversals, uses considerably less memory, and has
  56. * an opaque interface (easier to modify in the future).
  57. *
  58. * Internally, it keeps a single list of "tree_entry" items that
  59. * represent filesystem objects that require further attention.
  60. * Non-directories are not kept in memory: they are pulled from
  61. * readdir(), returned to the client, then freed as soon as possible.
  62. * Any directory entry to be traversed gets pushed onto the stack.
  63. *
  64. * There is surprisingly little information that needs to be kept for
  65. * each item on the stack. Just the name, depth (represented here as the
  66. * string length of the parent directory's pathname), and some markers
  67. * indicating how to get back to the parent (via chdir("..") for a
  68. * regular dir or via fchdir(2) for a symlink).
  69. */
  70. struct restore_time {
  71. const wchar_t *full_path;
  72. FILETIME lastWriteTime;
  73. FILETIME lastAccessTime;
  74. mode_t filetype;
  75. };
  76. struct tree_entry {
  77. int depth;
  78. struct tree_entry *next;
  79. struct tree_entry *parent;
  80. size_t full_path_dir_length;
  81. struct archive_wstring name;
  82. struct archive_wstring full_path;
  83. size_t dirname_length;
  84. int64_t dev;
  85. int64_t ino;
  86. int flags;
  87. int filesystem_id;
  88. /* How to restore time of a directory. */
  89. struct restore_time restore_time;
  90. };
  91. struct filesystem {
  92. int64_t dev;
  93. int synthetic;
  94. int remote;
  95. DWORD bytesPerSector;
  96. };
  97. /* Definitions for tree_entry.flags bitmap. */
  98. #define isDir 1 /* This entry is a regular directory. */
  99. #define isDirLink 2 /* This entry is a symbolic link to a directory. */
  100. #define needsFirstVisit 4 /* This is an initial entry. */
  101. #define needsDescent 8 /* This entry needs to be previsited. */
  102. #define needsOpen 16 /* This is a directory that needs to be opened. */
  103. #define needsAscent 32 /* This entry needs to be postvisited. */
  104. /*
  105. * On Windows, "first visit" is handled as a pattern to be handed to
  106. * _findfirst(). This is consistent with Windows conventions that
  107. * file patterns are handled within the application. On Posix,
  108. * "first visit" is just returned to the client.
  109. */
  110. #define MAX_OVERLAPPED 8
  111. #define READ_BUFFER_SIZE (1024 * 64) /* Default to 64KB per https://technet.microsoft.com/en-us/library/cc938632.aspx */
  112. #define DIRECT_IO 0/* Disabled */
  113. #define ASYNC_IO 1/* Enabled */
  114. /*
  115. * Local data for this package.
  116. */
  117. struct tree {
  118. struct tree_entry *stack;
  119. struct tree_entry *current;
  120. HANDLE d;
  121. WIN32_FIND_DATAW _findData;
  122. WIN32_FIND_DATAW *findData;
  123. int flags;
  124. int visit_type;
  125. /* Error code from last failed operation. */
  126. int tree_errno;
  127. /* A full path with "\\?\" prefix. */
  128. struct archive_wstring full_path;
  129. size_t full_path_dir_length;
  130. /* Dynamically-sized buffer for holding path */
  131. struct archive_wstring path;
  132. /* Last path element */
  133. const wchar_t *basename;
  134. /* Leading dir length */
  135. size_t dirname_length;
  136. int depth;
  137. BY_HANDLE_FILE_INFORMATION lst;
  138. BY_HANDLE_FILE_INFORMATION st;
  139. int descend;
  140. /* How to restore time of a file. */
  141. struct restore_time restore_time;
  142. struct entry_sparse {
  143. int64_t length;
  144. int64_t offset;
  145. } *sparse_list, *current_sparse;
  146. int sparse_count;
  147. int sparse_list_size;
  148. char initial_symlink_mode;
  149. char symlink_mode;
  150. struct filesystem *current_filesystem;
  151. struct filesystem *filesystem_table;
  152. int initial_filesystem_id;
  153. int current_filesystem_id;
  154. int max_filesystem_id;
  155. int allocated_filesystem;
  156. HANDLE entry_fh;
  157. int entry_eof;
  158. int64_t entry_remaining_bytes;
  159. int64_t entry_total;
  160. int ol_idx_doing;
  161. int ol_idx_done;
  162. int ol_num_doing;
  163. int ol_num_done;
  164. int64_t ol_remaining_bytes;
  165. int64_t ol_total;
  166. struct la_overlapped {
  167. OVERLAPPED ol;
  168. struct archive * _a;
  169. unsigned char *buff;
  170. size_t buff_size;
  171. int64_t offset;
  172. size_t bytes_expected;
  173. size_t bytes_transferred;
  174. } ol[MAX_OVERLAPPED];
  175. int direct_io;
  176. int async_io;
  177. };
  178. #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
  179. /* Treat FileIndex as i-node. We should remove a sequence number
  180. * which is high-16-bits of nFileIndexHigh. */
  181. #define bhfi_ino(bhfi) \
  182. ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
  183. + (bhfi)->nFileIndexLow)
  184. /* Definitions for tree.flags bitmap. */
  185. #define hasStat 16 /* The st entry is valid. */
  186. #define hasLstat 32 /* The lst entry is valid. */
  187. #define needsRestoreTimes 128
  188. static int
  189. tree_dir_next_windows(struct tree *t, const wchar_t *pattern);
  190. /* Initiate/terminate a tree traversal. */
  191. static struct tree *tree_open(const wchar_t *, int, int);
  192. static struct tree *tree_reopen(struct tree *, const wchar_t *, int);
  193. static void tree_close(struct tree *);
  194. static void tree_free(struct tree *);
  195. static void tree_push(struct tree *, const wchar_t *, const wchar_t *,
  196. int, int64_t, int64_t, struct restore_time *);
  197. /*
  198. * tree_next() returns Zero if there is no next entry, non-zero if
  199. * there is. Note that directories are visited three times.
  200. * Directories are always visited first as part of enumerating their
  201. * parent; that is a "regular" visit. If tree_descend() is invoked at
  202. * that time, the directory is added to a work list and will
  203. * subsequently be visited two more times: once just after descending
  204. * into the directory ("postdescent") and again just after ascending
  205. * back to the parent ("postascent").
  206. *
  207. * TREE_ERROR_DIR is returned if the descent failed (because the
  208. * directory couldn't be opened, for instance). This is returned
  209. * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
  210. * fatal error, but it does imply that the relevant subtree won't be
  211. * visited. TREE_ERROR_FATAL is returned for an error that left the
  212. * traversal completely hosed. Right now, this is only returned for
  213. * chdir() failures during ascent.
  214. */
  215. #define TREE_REGULAR 1
  216. #define TREE_POSTDESCENT 2
  217. #define TREE_POSTASCENT 3
  218. #define TREE_ERROR_DIR -1
  219. #define TREE_ERROR_FATAL -2
  220. static int tree_next(struct tree *);
  221. /*
  222. * Return information about the current entry.
  223. */
  224. /*
  225. * The current full pathname, length of the full pathname, and a name
  226. * that can be used to access the file. Because tree does use chdir
  227. * extensively, the access path is almost never the same as the full
  228. * current path.
  229. *
  230. */
  231. static const wchar_t *tree_current_path(struct tree *);
  232. static const wchar_t *tree_current_access_path(struct tree *);
  233. /*
  234. * Request the lstat() or stat() data for the current path. Since the
  235. * tree package needs to do some of this anyway, and caches the
  236. * results, you should take advantage of it here if you need it rather
  237. * than make a redundant stat() or lstat() call of your own.
  238. */
  239. static const BY_HANDLE_FILE_INFORMATION *tree_current_stat(struct tree *);
  240. static const BY_HANDLE_FILE_INFORMATION *tree_current_lstat(struct tree *);
  241. /* The following functions use tricks to avoid a certain number of
  242. * stat()/lstat() calls. */
  243. /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
  244. static int tree_current_is_physical_dir(struct tree *);
  245. /* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
  246. static int tree_current_is_physical_link(struct tree *);
  247. /* Instead of archive_entry_copy_stat for BY_HANDLE_FILE_INFORMATION */
  248. static void tree_archive_entry_copy_bhfi(struct archive_entry *,
  249. struct tree *, const BY_HANDLE_FILE_INFORMATION *);
  250. /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
  251. static int tree_current_is_dir(struct tree *);
  252. static int update_current_filesystem(struct archive_read_disk *a,
  253. int64_t dev);
  254. static int setup_current_filesystem(struct archive_read_disk *);
  255. static int tree_target_is_same_as_parent(struct tree *,
  256. const BY_HANDLE_FILE_INFORMATION *);
  257. static int _archive_read_disk_open_w(struct archive *, const wchar_t *);
  258. static int _archive_read_free(struct archive *);
  259. static int _archive_read_close(struct archive *);
  260. static int _archive_read_data_block(struct archive *,
  261. const void **, size_t *, int64_t *);
  262. static int _archive_read_next_header(struct archive *,
  263. struct archive_entry **);
  264. static int _archive_read_next_header2(struct archive *,
  265. struct archive_entry *);
  266. static const char *trivial_lookup_gname(void *, int64_t gid);
  267. static const char *trivial_lookup_uname(void *, int64_t uid);
  268. static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
  269. static int close_and_restore_time(HANDLE, struct tree *,
  270. struct restore_time *);
  271. static int setup_sparse_from_disk(struct archive_read_disk *,
  272. struct archive_entry *, HANDLE);
  273. static int la_linkname_from_handle(HANDLE, wchar_t **, int *);
  274. static int la_linkname_from_pathw(const wchar_t *, wchar_t **, int *);
  275. static void entry_symlink_from_pathw(struct archive_entry *,
  276. const wchar_t *path);
  277. typedef struct _REPARSE_DATA_BUFFER {
  278. ULONG ReparseTag;
  279. USHORT ReparseDataLength;
  280. USHORT Reserved;
  281. union {
  282. struct {
  283. USHORT SubstituteNameOffset;
  284. USHORT SubstituteNameLength;
  285. USHORT PrintNameOffset;
  286. USHORT PrintNameLength;
  287. ULONG Flags;
  288. WCHAR PathBuffer[1];
  289. } SymbolicLinkReparseBuffer;
  290. struct {
  291. USHORT SubstituteNameOffset;
  292. USHORT SubstituteNameLength;
  293. USHORT PrintNameOffset;
  294. USHORT PrintNameLength;
  295. WCHAR PathBuffer[1];
  296. } MountPointReparseBuffer;
  297. struct {
  298. UCHAR DataBuffer[1];
  299. } GenericReparseBuffer;
  300. } DUMMYUNIONNAME;
  301. } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
  302. /*
  303. * Reads the target of a symbolic link
  304. *
  305. * Returns 0 on success and -1 on failure
  306. * outbuf is allocated in the function
  307. */
  308. static int
  309. la_linkname_from_handle(HANDLE h, wchar_t **linkname, int *linktype)
  310. {
  311. DWORD inbytes;
  312. REPARSE_DATA_BUFFER *buf;
  313. BY_HANDLE_FILE_INFORMATION st;
  314. size_t len;
  315. BOOL ret;
  316. BYTE *indata;
  317. wchar_t *tbuf;
  318. ret = GetFileInformationByHandle(h, &st);
  319. if (ret == 0 ||
  320. (st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
  321. return (-1);
  322. }
  323. indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
  324. ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
  325. 1024, &inbytes, NULL);
  326. if (ret == 0) {
  327. la_dosmaperr(GetLastError());
  328. free(indata);
  329. return (-1);
  330. }
  331. buf = (REPARSE_DATA_BUFFER *) indata;
  332. if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
  333. free(indata);
  334. /* File is not a symbolic link */
  335. errno = EINVAL;
  336. return (-1);
  337. }
  338. len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
  339. if (len <= 0) {
  340. free(indata);
  341. return (-1);
  342. }
  343. tbuf = malloc(len + sizeof(wchar_t));
  344. if (tbuf == NULL) {
  345. free(indata);
  346. return (-1);
  347. }
  348. memcpy(tbuf, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
  349. [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
  350. free(indata);
  351. tbuf[len / sizeof(wchar_t)] = L'\0';
  352. if (wcsncmp(tbuf, START_ABSOLUTE_SYMLINK_REPARSE, 4) == 0) {
  353. /* Absolute symlink, so we'll change the NT path into a verbatim one */
  354. tbuf[1] = L'\\';
  355. } else {
  356. /* Relative symlink, so we can translate backslashes to slashes */
  357. wchar_t *temp = tbuf;
  358. do {
  359. if (*temp == L'\\')
  360. *temp = L'/';
  361. temp++;
  362. } while(*temp != L'\0');
  363. }
  364. *linkname = tbuf;
  365. if ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  366. *linktype = AE_SYMLINK_TYPE_FILE;
  367. else
  368. *linktype = AE_SYMLINK_TYPE_DIRECTORY;
  369. return (0);
  370. }
  371. /*
  372. * Returns AE_SYMLINK_TYPE_FILE, AE_SYMLINK_TYPE_DIRECTORY or -1 on error
  373. */
  374. static int
  375. la_linkname_from_pathw(const wchar_t *path, wchar_t **outbuf, int *linktype)
  376. {
  377. HANDLE h;
  378. const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
  379. FILE_FLAG_OPEN_REPARSE_POINT;
  380. int ret;
  381. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  382. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  383. ZeroMemory(&createExParams, sizeof(createExParams));
  384. createExParams.dwSize = sizeof(createExParams);
  385. createExParams.dwFileFlags = flag;
  386. h = CreateFile2(path, 0,
  387. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  388. OPEN_EXISTING, &createExParams);
  389. #else
  390. h = CreateFileW(path, 0,
  391. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
  392. OPEN_EXISTING, flag, NULL);
  393. #endif
  394. if (h == INVALID_HANDLE_VALUE) {
  395. la_dosmaperr(GetLastError());
  396. return (-1);
  397. }
  398. ret = la_linkname_from_handle(h, outbuf, linktype);
  399. CloseHandle(h);
  400. return (ret);
  401. }
  402. static void
  403. entry_symlink_from_pathw(struct archive_entry *entry, const wchar_t *path)
  404. {
  405. wchar_t *linkname = NULL;
  406. int ret, linktype;
  407. ret = la_linkname_from_pathw(path, &linkname, &linktype);
  408. if (ret != 0)
  409. return;
  410. if (linktype >= 0) {
  411. archive_entry_copy_symlink_w(entry, linkname);
  412. archive_entry_set_symlink_type(entry, linktype);
  413. }
  414. free(linkname);
  415. return;
  416. }
  417. static const struct archive_vtable
  418. archive_read_disk_vtable = {
  419. .archive_free = _archive_read_free,
  420. .archive_close = _archive_read_close,
  421. .archive_read_data_block = _archive_read_data_block,
  422. .archive_read_next_header = _archive_read_next_header,
  423. .archive_read_next_header2 = _archive_read_next_header2,
  424. };
  425. const char *
  426. archive_read_disk_gname(struct archive *_a, la_int64_t gid)
  427. {
  428. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  429. if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  430. ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
  431. return (NULL);
  432. if (a->lookup_gname == NULL)
  433. return (NULL);
  434. return ((*a->lookup_gname)(a->lookup_gname_data, gid));
  435. }
  436. const char *
  437. archive_read_disk_uname(struct archive *_a, la_int64_t uid)
  438. {
  439. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  440. if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  441. ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
  442. return (NULL);
  443. if (a->lookup_uname == NULL)
  444. return (NULL);
  445. return ((*a->lookup_uname)(a->lookup_uname_data, uid));
  446. }
  447. int
  448. archive_read_disk_set_gname_lookup(struct archive *_a,
  449. void *private_data,
  450. const char * (*lookup_gname)(void *private, la_int64_t gid),
  451. void (*cleanup_gname)(void *private))
  452. {
  453. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  454. archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  455. ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
  456. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  457. (a->cleanup_gname)(a->lookup_gname_data);
  458. a->lookup_gname = lookup_gname;
  459. a->cleanup_gname = cleanup_gname;
  460. a->lookup_gname_data = private_data;
  461. return (ARCHIVE_OK);
  462. }
  463. int
  464. archive_read_disk_set_uname_lookup(struct archive *_a,
  465. void *private_data,
  466. const char * (*lookup_uname)(void *private, int64_t uid),
  467. void (*cleanup_uname)(void *private))
  468. {
  469. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  470. archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  471. ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
  472. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  473. (a->cleanup_uname)(a->lookup_uname_data);
  474. a->lookup_uname = lookup_uname;
  475. a->cleanup_uname = cleanup_uname;
  476. a->lookup_uname_data = private_data;
  477. return (ARCHIVE_OK);
  478. }
  479. /*
  480. * Create a new archive_read_disk object and initialize it with global state.
  481. */
  482. struct archive *
  483. archive_read_disk_new(void)
  484. {
  485. struct archive_read_disk *a;
  486. a = calloc(1, sizeof(*a));
  487. if (a == NULL)
  488. return (NULL);
  489. a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
  490. a->archive.state = ARCHIVE_STATE_NEW;
  491. a->archive.vtable = &archive_read_disk_vtable;
  492. a->entry = archive_entry_new2(&a->archive);
  493. a->lookup_uname = trivial_lookup_uname;
  494. a->lookup_gname = trivial_lookup_gname;
  495. a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
  496. return (&a->archive);
  497. }
  498. static int
  499. _archive_read_free(struct archive *_a)
  500. {
  501. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  502. int r;
  503. if (_a == NULL)
  504. return (ARCHIVE_OK);
  505. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  506. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
  507. if (a->archive.state != ARCHIVE_STATE_CLOSED)
  508. r = _archive_read_close(&a->archive);
  509. else
  510. r = ARCHIVE_OK;
  511. tree_free(a->tree);
  512. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  513. (a->cleanup_gname)(a->lookup_gname_data);
  514. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  515. (a->cleanup_uname)(a->lookup_uname_data);
  516. archive_string_free(&a->archive.error_string);
  517. archive_entry_free(a->entry);
  518. a->archive.magic = 0;
  519. free(a);
  520. return (r);
  521. }
  522. static int
  523. _archive_read_close(struct archive *_a)
  524. {
  525. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  526. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  527. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
  528. if (a->archive.state != ARCHIVE_STATE_FATAL)
  529. a->archive.state = ARCHIVE_STATE_CLOSED;
  530. tree_close(a->tree);
  531. return (ARCHIVE_OK);
  532. }
  533. static void
  534. setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
  535. char follow_symlinks)
  536. {
  537. a->symlink_mode = symlink_mode;
  538. a->follow_symlinks = follow_symlinks;
  539. if (a->tree != NULL) {
  540. a->tree->initial_symlink_mode = a->symlink_mode;
  541. a->tree->symlink_mode = a->symlink_mode;
  542. }
  543. }
  544. int
  545. archive_read_disk_set_symlink_logical(struct archive *_a)
  546. {
  547. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  548. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  549. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
  550. setup_symlink_mode(a, 'L', 1);
  551. return (ARCHIVE_OK);
  552. }
  553. int
  554. archive_read_disk_set_symlink_physical(struct archive *_a)
  555. {
  556. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  557. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  558. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
  559. setup_symlink_mode(a, 'P', 0);
  560. return (ARCHIVE_OK);
  561. }
  562. int
  563. archive_read_disk_set_symlink_hybrid(struct archive *_a)
  564. {
  565. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  566. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  567. ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
  568. setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
  569. return (ARCHIVE_OK);
  570. }
  571. int
  572. archive_read_disk_set_atime_restored(struct archive *_a)
  573. {
  574. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  575. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  576. ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
  577. a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
  578. if (a->tree != NULL)
  579. a->tree->flags |= needsRestoreTimes;
  580. return (ARCHIVE_OK);
  581. }
  582. int
  583. archive_read_disk_set_behavior(struct archive *_a, int flags)
  584. {
  585. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  586. int r = ARCHIVE_OK;
  587. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  588. ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
  589. a->flags = flags;
  590. if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
  591. r = archive_read_disk_set_atime_restored(_a);
  592. else {
  593. if (a->tree != NULL)
  594. a->tree->flags &= ~needsRestoreTimes;
  595. }
  596. return (r);
  597. }
  598. /*
  599. * Trivial implementations of gname/uname lookup functions.
  600. * These are normally overridden by the client, but these stub
  601. * versions ensure that we always have something that works.
  602. */
  603. static const char *
  604. trivial_lookup_gname(void *private_data, int64_t gid)
  605. {
  606. (void)private_data; /* UNUSED */
  607. (void)gid; /* UNUSED */
  608. return (NULL);
  609. }
  610. static const char *
  611. trivial_lookup_uname(void *private_data, int64_t uid)
  612. {
  613. (void)private_data; /* UNUSED */
  614. (void)uid; /* UNUSED */
  615. return (NULL);
  616. }
  617. static int64_t
  618. align_num_per_sector(struct tree *t, int64_t size)
  619. {
  620. int64_t surplus;
  621. size += t->current_filesystem->bytesPerSector -1;
  622. surplus = size % t->current_filesystem->bytesPerSector;
  623. size -= surplus;
  624. return (size);
  625. }
  626. static int
  627. start_next_async_read(struct archive_read_disk *a, struct tree *t)
  628. {
  629. struct la_overlapped *olp;
  630. DWORD buffbytes, rbytes;
  631. if (t->ol_remaining_bytes == 0)
  632. return (ARCHIVE_EOF);
  633. olp = &(t->ol[t->ol_idx_doing]);
  634. t->ol_idx_doing = (t->ol_idx_doing + 1) % MAX_OVERLAPPED;
  635. /* Allocate read buffer. */
  636. if (olp->buff == NULL) {
  637. void *p;
  638. size_t s = (size_t)align_num_per_sector(t, READ_BUFFER_SIZE);
  639. p = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
  640. if (p == NULL) {
  641. archive_set_error(&a->archive, ENOMEM,
  642. "Couldn't allocate memory");
  643. a->archive.state = ARCHIVE_STATE_FATAL;
  644. return (ARCHIVE_FATAL);
  645. }
  646. olp->buff = p;
  647. olp->buff_size = s;
  648. olp->_a = &a->archive;
  649. olp->ol.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
  650. if (olp->ol.hEvent == NULL) {
  651. la_dosmaperr(GetLastError());
  652. archive_set_error(&a->archive, errno,
  653. "CreateEvent failed");
  654. a->archive.state = ARCHIVE_STATE_FATAL;
  655. return (ARCHIVE_FATAL);
  656. }
  657. } else
  658. ResetEvent(olp->ol.hEvent);
  659. buffbytes = (DWORD)olp->buff_size;
  660. if (buffbytes > t->current_sparse->length)
  661. buffbytes = (DWORD)t->current_sparse->length;
  662. /* Skip hole. */
  663. if (t->current_sparse->offset > t->ol_total) {
  664. t->ol_remaining_bytes -=
  665. t->current_sparse->offset - t->ol_total;
  666. }
  667. olp->offset = t->current_sparse->offset;
  668. olp->ol.Offset = (DWORD)(olp->offset & 0xffffffff);
  669. olp->ol.OffsetHigh = (DWORD)(olp->offset >> 32);
  670. if (t->ol_remaining_bytes > buffbytes) {
  671. olp->bytes_expected = buffbytes;
  672. t->ol_remaining_bytes -= buffbytes;
  673. } else {
  674. olp->bytes_expected = (size_t)t->ol_remaining_bytes;
  675. t->ol_remaining_bytes = 0;
  676. }
  677. olp->bytes_transferred = 0;
  678. t->current_sparse->offset += buffbytes;
  679. t->current_sparse->length -= buffbytes;
  680. t->ol_total = t->current_sparse->offset;
  681. if (t->current_sparse->length == 0 && t->ol_remaining_bytes > 0)
  682. t->current_sparse++;
  683. if (!ReadFile(t->entry_fh, olp->buff, buffbytes, &rbytes, &(olp->ol))) {
  684. DWORD lasterr;
  685. lasterr = GetLastError();
  686. if (lasterr == ERROR_HANDLE_EOF) {
  687. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  688. "Reading file truncated");
  689. a->archive.state = ARCHIVE_STATE_FATAL;
  690. return (ARCHIVE_FATAL);
  691. } else if (lasterr != ERROR_IO_PENDING) {
  692. if (lasterr == ERROR_NO_DATA)
  693. errno = EAGAIN;
  694. else if (lasterr == ERROR_ACCESS_DENIED)
  695. errno = EBADF;
  696. else
  697. la_dosmaperr(lasterr);
  698. archive_set_error(&a->archive, errno, "Read error");
  699. a->archive.state = ARCHIVE_STATE_FATAL;
  700. return (ARCHIVE_FATAL);
  701. }
  702. } else
  703. olp->bytes_transferred = rbytes;
  704. t->ol_num_doing++;
  705. return (t->ol_remaining_bytes == 0)? ARCHIVE_EOF: ARCHIVE_OK;
  706. }
  707. static void
  708. cancel_async(struct tree *t)
  709. {
  710. if (t->ol_num_doing != t->ol_num_done) {
  711. CancelIo(t->entry_fh);
  712. t->ol_num_doing = t->ol_num_done = 0;
  713. }
  714. }
  715. static int
  716. _archive_read_data_block(struct archive *_a, const void **buff,
  717. size_t *size, int64_t *offset)
  718. {
  719. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  720. struct tree *t = a->tree;
  721. struct la_overlapped *olp;
  722. DWORD bytes_transferred;
  723. int r = ARCHIVE_FATAL;
  724. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  725. "archive_read_data_block");
  726. if (t->entry_eof || t->entry_remaining_bytes <= 0) {
  727. r = ARCHIVE_EOF;
  728. goto abort_read_data;
  729. }
  730. /*
  731. * Make a request to read the file in asynchronous.
  732. */
  733. if (t->ol_num_doing == 0) {
  734. do {
  735. r = start_next_async_read(a, t);
  736. if (r == ARCHIVE_FATAL)
  737. goto abort_read_data;
  738. if (!t->async_io)
  739. break;
  740. } while (r == ARCHIVE_OK && t->ol_num_doing < MAX_OVERLAPPED);
  741. } else {
  742. if ((r = start_next_async_read(a, t)) == ARCHIVE_FATAL)
  743. goto abort_read_data;
  744. }
  745. olp = &(t->ol[t->ol_idx_done]);
  746. t->ol_idx_done = (t->ol_idx_done + 1) % MAX_OVERLAPPED;
  747. if (olp->bytes_transferred)
  748. bytes_transferred = (DWORD)olp->bytes_transferred;
  749. else if (!GetOverlappedResult(t->entry_fh, &(olp->ol),
  750. &bytes_transferred, TRUE)) {
  751. la_dosmaperr(GetLastError());
  752. archive_set_error(&a->archive, errno,
  753. "GetOverlappedResult failed");
  754. a->archive.state = ARCHIVE_STATE_FATAL;
  755. r = ARCHIVE_FATAL;
  756. goto abort_read_data;
  757. }
  758. t->ol_num_done++;
  759. if (bytes_transferred == 0 ||
  760. olp->bytes_expected != bytes_transferred) {
  761. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  762. "Reading file truncated");
  763. a->archive.state = ARCHIVE_STATE_FATAL;
  764. r = ARCHIVE_FATAL;
  765. goto abort_read_data;
  766. }
  767. *buff = olp->buff;
  768. *size = bytes_transferred;
  769. *offset = olp->offset;
  770. if (olp->offset > t->entry_total)
  771. t->entry_remaining_bytes -= olp->offset - t->entry_total;
  772. t->entry_total = olp->offset + *size;
  773. t->entry_remaining_bytes -= *size;
  774. if (t->entry_remaining_bytes == 0) {
  775. /* Close the current file descriptor */
  776. close_and_restore_time(t->entry_fh, t, &t->restore_time);
  777. t->entry_fh = INVALID_HANDLE_VALUE;
  778. t->entry_eof = 1;
  779. }
  780. return (ARCHIVE_OK);
  781. abort_read_data:
  782. *buff = NULL;
  783. *size = 0;
  784. *offset = t->entry_total;
  785. if (t->entry_fh != INVALID_HANDLE_VALUE) {
  786. cancel_async(t);
  787. /* Close the current file descriptor */
  788. close_and_restore_time(t->entry_fh, t, &t->restore_time);
  789. t->entry_fh = INVALID_HANDLE_VALUE;
  790. }
  791. return (r);
  792. }
  793. static int
  794. next_entry(struct archive_read_disk *a, struct tree *t,
  795. struct archive_entry *entry)
  796. {
  797. const BY_HANDLE_FILE_INFORMATION *st;
  798. const BY_HANDLE_FILE_INFORMATION *lst;
  799. const char*name;
  800. int descend, r;
  801. st = NULL;
  802. lst = NULL;
  803. t->descend = 0;
  804. do {
  805. switch (tree_next(t)) {
  806. case TREE_ERROR_FATAL:
  807. archive_set_error(&a->archive, t->tree_errno,
  808. "%ls: Unable to continue traversing directory tree",
  809. tree_current_path(t));
  810. a->archive.state = ARCHIVE_STATE_FATAL;
  811. return (ARCHIVE_FATAL);
  812. case TREE_ERROR_DIR:
  813. archive_set_error(&a->archive, t->tree_errno,
  814. "%ls: Couldn't visit directory",
  815. tree_current_path(t));
  816. return (ARCHIVE_FAILED);
  817. case 0:
  818. return (ARCHIVE_EOF);
  819. case TREE_POSTDESCENT:
  820. case TREE_POSTASCENT:
  821. break;
  822. case TREE_REGULAR:
  823. lst = tree_current_lstat(t);
  824. if (lst == NULL) {
  825. archive_set_error(&a->archive, t->tree_errno,
  826. "%ls: Cannot stat",
  827. tree_current_path(t));
  828. return (ARCHIVE_FAILED);
  829. }
  830. break;
  831. }
  832. } while (lst == NULL);
  833. archive_entry_copy_pathname_w(entry, tree_current_path(t));
  834. /*
  835. * Perform path matching.
  836. */
  837. if (a->matching) {
  838. r = archive_match_path_excluded(a->matching, entry);
  839. if (r < 0) {
  840. archive_set_error(&(a->archive), errno,
  841. "Failed : %s", archive_error_string(a->matching));
  842. return (r);
  843. }
  844. if (r) {
  845. if (a->excluded_cb_func)
  846. a->excluded_cb_func(&(a->archive),
  847. a->excluded_cb_data, entry);
  848. return (ARCHIVE_RETRY);
  849. }
  850. }
  851. /*
  852. * Distinguish 'L'/'P'/'H' symlink following.
  853. */
  854. switch(t->symlink_mode) {
  855. case 'H':
  856. /* 'H': After the first item, rest like 'P'. */
  857. t->symlink_mode = 'P';
  858. /* 'H': First item (from command line) like 'L'. */
  859. /* FALLTHROUGH */
  860. case 'L':
  861. /* 'L': Do descend through a symlink to dir. */
  862. descend = tree_current_is_dir(t);
  863. /* 'L': Follow symlinks to files. */
  864. a->symlink_mode = 'L';
  865. a->follow_symlinks = 1;
  866. /* 'L': Archive symlinks as targets, if we can. */
  867. st = tree_current_stat(t);
  868. if (st != NULL && !tree_target_is_same_as_parent(t, st))
  869. break;
  870. /* If stat fails, we have a broken symlink;
  871. * in that case, don't follow the link. */
  872. /* FALLTHROUGH */
  873. default:
  874. /* 'P': Don't descend through a symlink to dir. */
  875. descend = tree_current_is_physical_dir(t);
  876. /* 'P': Don't follow symlinks to files. */
  877. a->symlink_mode = 'P';
  878. a->follow_symlinks = 0;
  879. /* 'P': Archive symlinks as symlinks. */
  880. st = lst;
  881. break;
  882. }
  883. if (update_current_filesystem(a, bhfi_dev(st)) != ARCHIVE_OK) {
  884. a->archive.state = ARCHIVE_STATE_FATAL;
  885. return (ARCHIVE_FATAL);
  886. }
  887. if (t->initial_filesystem_id == -1)
  888. t->initial_filesystem_id = t->current_filesystem_id;
  889. if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
  890. if (t->initial_filesystem_id != t->current_filesystem_id)
  891. return (ARCHIVE_RETRY);
  892. }
  893. t->descend = descend;
  894. tree_archive_entry_copy_bhfi(entry, t, st);
  895. /* Save the times to be restored. This must be in before
  896. * calling archive_read_disk_descend() or any chance of it,
  897. * especially, invoking a callback. */
  898. t->restore_time.lastWriteTime = st->ftLastWriteTime;
  899. t->restore_time.lastAccessTime = st->ftLastAccessTime;
  900. t->restore_time.filetype = archive_entry_filetype(entry);
  901. /*
  902. * Perform time matching.
  903. */
  904. if (a->matching) {
  905. r = archive_match_time_excluded(a->matching, entry);
  906. if (r < 0) {
  907. archive_set_error(&(a->archive), errno,
  908. "Failed : %s", archive_error_string(a->matching));
  909. return (r);
  910. }
  911. if (r) {
  912. if (a->excluded_cb_func)
  913. a->excluded_cb_func(&(a->archive),
  914. a->excluded_cb_data, entry);
  915. return (ARCHIVE_RETRY);
  916. }
  917. }
  918. /* Lookup uname/gname */
  919. name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
  920. if (name != NULL)
  921. archive_entry_copy_uname(entry, name);
  922. name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
  923. if (name != NULL)
  924. archive_entry_copy_gname(entry, name);
  925. /*
  926. * Perform owner matching.
  927. */
  928. if (a->matching) {
  929. r = archive_match_owner_excluded(a->matching, entry);
  930. if (r < 0) {
  931. archive_set_error(&(a->archive), errno,
  932. "Failed : %s", archive_error_string(a->matching));
  933. return (r);
  934. }
  935. if (r) {
  936. if (a->excluded_cb_func)
  937. a->excluded_cb_func(&(a->archive),
  938. a->excluded_cb_data, entry);
  939. return (ARCHIVE_RETRY);
  940. }
  941. }
  942. /*
  943. * File attributes
  944. */
  945. if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
  946. const int supported_attrs =
  947. FILE_ATTRIBUTE_READONLY |
  948. FILE_ATTRIBUTE_HIDDEN |
  949. FILE_ATTRIBUTE_SYSTEM;
  950. DWORD file_attrs = st->dwFileAttributes & supported_attrs;
  951. if (file_attrs != 0)
  952. archive_entry_set_fflags(entry, file_attrs, 0);
  953. }
  954. /*
  955. * Invoke a meta data filter callback.
  956. */
  957. if (a->metadata_filter_func) {
  958. if (!a->metadata_filter_func(&(a->archive),
  959. a->metadata_filter_data, entry))
  960. return (ARCHIVE_RETRY);
  961. }
  962. archive_entry_copy_sourcepath_w(entry, tree_current_access_path(t));
  963. r = ARCHIVE_OK;
  964. if (archive_entry_filetype(entry) == AE_IFREG &&
  965. archive_entry_size(entry) > 0) {
  966. DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
  967. #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  968. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  969. #endif
  970. if (t->async_io)
  971. flags |= FILE_FLAG_OVERLAPPED;
  972. if (t->direct_io)
  973. flags |= FILE_FLAG_NO_BUFFERING;
  974. else
  975. flags |= FILE_FLAG_SEQUENTIAL_SCAN;
  976. #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  977. ZeroMemory(&createExParams, sizeof(createExParams));
  978. createExParams.dwSize = sizeof(createExParams);
  979. createExParams.dwFileFlags = flags;
  980. t->entry_fh = CreateFile2(tree_current_access_path(t),
  981. GENERIC_READ,
  982. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  983. OPEN_EXISTING, &createExParams);
  984. #else
  985. t->entry_fh = CreateFileW(tree_current_access_path(t),
  986. GENERIC_READ,
  987. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  988. NULL, OPEN_EXISTING, flags, NULL);
  989. #endif
  990. if (t->entry_fh == INVALID_HANDLE_VALUE) {
  991. la_dosmaperr(GetLastError());
  992. archive_set_error(&a->archive, errno,
  993. "Couldn't open %ls", tree_current_path(a->tree));
  994. return (ARCHIVE_FAILED);
  995. }
  996. /* Find sparse data from the disk. */
  997. if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
  998. if (archive_entry_hardlink(entry) == NULL &&
  999. (st->dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) != 0)
  1000. r = setup_sparse_from_disk(a, entry, t->entry_fh);
  1001. }
  1002. }
  1003. return (r);
  1004. }
  1005. static int
  1006. _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
  1007. {
  1008. int ret;
  1009. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1010. *entryp = NULL;
  1011. ret = _archive_read_next_header2(_a, a->entry);
  1012. *entryp = a->entry;
  1013. return ret;
  1014. }
  1015. static int
  1016. _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
  1017. {
  1018. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1019. struct tree *t;
  1020. int r;
  1021. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1022. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1023. "archive_read_next_header2");
  1024. t = a->tree;
  1025. if (t->entry_fh != INVALID_HANDLE_VALUE) {
  1026. cancel_async(t);
  1027. close_and_restore_time(t->entry_fh, t, &t->restore_time);
  1028. t->entry_fh = INVALID_HANDLE_VALUE;
  1029. }
  1030. archive_entry_clear(entry);
  1031. while ((r = next_entry(a, t, entry)) == ARCHIVE_RETRY)
  1032. archive_entry_clear(entry);
  1033. /*
  1034. * EOF and FATAL are persistent at this layer. By
  1035. * modifying the state, we guarantee that future calls to
  1036. * read a header or read data will fail.
  1037. */
  1038. switch (r) {
  1039. case ARCHIVE_EOF:
  1040. a->archive.state = ARCHIVE_STATE_EOF;
  1041. break;
  1042. case ARCHIVE_OK:
  1043. case ARCHIVE_WARN:
  1044. t->entry_total = 0;
  1045. if (archive_entry_filetype(entry) == AE_IFREG) {
  1046. t->entry_remaining_bytes = archive_entry_size(entry);
  1047. t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
  1048. if (!t->entry_eof &&
  1049. setup_sparse(a, entry) != ARCHIVE_OK)
  1050. return (ARCHIVE_FATAL);
  1051. } else {
  1052. t->entry_remaining_bytes = 0;
  1053. t->entry_eof = 1;
  1054. }
  1055. t->ol_idx_doing = t->ol_idx_done = 0;
  1056. t->ol_num_doing = t->ol_num_done = 0;
  1057. t->ol_remaining_bytes = t->entry_remaining_bytes;
  1058. t->ol_total = 0;
  1059. a->archive.state = ARCHIVE_STATE_DATA;
  1060. break;
  1061. case ARCHIVE_RETRY:
  1062. break;
  1063. case ARCHIVE_FATAL:
  1064. a->archive.state = ARCHIVE_STATE_FATAL;
  1065. break;
  1066. }
  1067. __archive_reset_read_data(&a->archive);
  1068. return (r);
  1069. }
  1070. static int
  1071. setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
  1072. {
  1073. struct tree *t = a->tree;
  1074. int64_t aligned, length, offset;
  1075. int i;
  1076. t->sparse_count = archive_entry_sparse_reset(entry);
  1077. if (t->sparse_count+1 > t->sparse_list_size) {
  1078. free(t->sparse_list);
  1079. t->sparse_list_size = t->sparse_count + 1;
  1080. t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
  1081. t->sparse_list_size);
  1082. if (t->sparse_list == NULL) {
  1083. t->sparse_list_size = 0;
  1084. archive_set_error(&a->archive, ENOMEM,
  1085. "Can't allocate data");
  1086. a->archive.state = ARCHIVE_STATE_FATAL;
  1087. return (ARCHIVE_FATAL);
  1088. }
  1089. }
  1090. /*
  1091. * Get sparse list and make sure those offsets and lengths are
  1092. * aligned by a sector size.
  1093. */
  1094. for (i = 0; i < t->sparse_count; i++) {
  1095. archive_entry_sparse_next(entry, &offset, &length);
  1096. aligned = align_num_per_sector(t, offset);
  1097. if (aligned != offset) {
  1098. aligned -= t->current_filesystem->bytesPerSector;
  1099. length += offset - aligned;
  1100. }
  1101. t->sparse_list[i].offset = aligned;
  1102. aligned = align_num_per_sector(t, length);
  1103. t->sparse_list[i].length = aligned;
  1104. }
  1105. aligned = align_num_per_sector(t, archive_entry_size(entry));
  1106. if (i == 0) {
  1107. t->sparse_list[i].offset = 0;
  1108. t->sparse_list[i].length = aligned;
  1109. } else {
  1110. int j, last = i;
  1111. t->sparse_list[i].offset = aligned;
  1112. t->sparse_list[i].length = 0;
  1113. for (i = 0; i < last; i++) {
  1114. if ((t->sparse_list[i].offset +
  1115. t->sparse_list[i].length) <=
  1116. t->sparse_list[i+1].offset)
  1117. continue;
  1118. /*
  1119. * Now sparse_list[i+1] is overlapped by sparse_list[i].
  1120. * Merge those two.
  1121. */
  1122. length = t->sparse_list[i+1].offset -
  1123. t->sparse_list[i].offset;
  1124. t->sparse_list[i+1].offset = t->sparse_list[i].offset;
  1125. t->sparse_list[i+1].length += length;
  1126. /* Remove sparse_list[i]. */
  1127. for (j = i; j < last; j++) {
  1128. t->sparse_list[j].offset =
  1129. t->sparse_list[j+1].offset;
  1130. t->sparse_list[j].length =
  1131. t->sparse_list[j+1].length;
  1132. }
  1133. last--;
  1134. }
  1135. }
  1136. t->current_sparse = t->sparse_list;
  1137. return (ARCHIVE_OK);
  1138. }
  1139. int
  1140. archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
  1141. void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
  1142. void *_client_data)
  1143. {
  1144. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1145. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1146. ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
  1147. a->matching = _ma;
  1148. a->excluded_cb_func = _excluded_func;
  1149. a->excluded_cb_data = _client_data;
  1150. return (ARCHIVE_OK);
  1151. }
  1152. int
  1153. archive_read_disk_set_metadata_filter_callback(struct archive *_a,
  1154. int (*_metadata_filter_func)(struct archive *, void *,
  1155. struct archive_entry *), void *_client_data)
  1156. {
  1157. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1158. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
  1159. "archive_read_disk_set_metadata_filter_callback");
  1160. a->metadata_filter_func = _metadata_filter_func;
  1161. a->metadata_filter_data = _client_data;
  1162. return (ARCHIVE_OK);
  1163. }
  1164. int
  1165. archive_read_disk_can_descend(struct archive *_a)
  1166. {
  1167. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1168. struct tree *t = a->tree;
  1169. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1170. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1171. "archive_read_disk_can_descend");
  1172. return (t->visit_type == TREE_REGULAR && t->descend);
  1173. }
  1174. /*
  1175. * Called by the client to mark the directory just returned from
  1176. * tree_next() as needing to be visited.
  1177. */
  1178. int
  1179. archive_read_disk_descend(struct archive *_a)
  1180. {
  1181. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1182. struct tree *t = a->tree;
  1183. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1184. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  1185. "archive_read_disk_descend");
  1186. if (!archive_read_disk_can_descend(_a))
  1187. return (ARCHIVE_OK);
  1188. if (tree_current_is_physical_dir(t)) {
  1189. tree_push(t, t->basename, t->full_path.s,
  1190. t->current_filesystem_id,
  1191. bhfi_dev(&(t->lst)), bhfi_ino(&(t->lst)),
  1192. &t->restore_time);
  1193. t->stack->flags |= isDir;
  1194. } else if (tree_current_is_dir(t)) {
  1195. tree_push(t, t->basename, t->full_path.s,
  1196. t->current_filesystem_id,
  1197. bhfi_dev(&(t->st)), bhfi_ino(&(t->st)),
  1198. &t->restore_time);
  1199. t->stack->flags |= isDirLink;
  1200. }
  1201. t->descend = 0;
  1202. return (ARCHIVE_OK);
  1203. }
  1204. int
  1205. archive_read_disk_open(struct archive *_a, const char *pathname)
  1206. {
  1207. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1208. struct archive_wstring wpath;
  1209. int ret;
  1210. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1211. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1212. "archive_read_disk_open");
  1213. archive_clear_error(&a->archive);
  1214. /* Make a wchar_t string from a char string. */
  1215. archive_string_init(&wpath);
  1216. if (archive_wstring_append_from_mbs(&wpath, pathname,
  1217. strlen(pathname)) != 0) {
  1218. if (errno == ENOMEM)
  1219. archive_set_error(&a->archive, ENOMEM,
  1220. "Can't allocate memory");
  1221. else
  1222. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1223. "Can't convert a path to a wchar_t string");
  1224. a->archive.state = ARCHIVE_STATE_FATAL;
  1225. ret = ARCHIVE_FATAL;
  1226. } else
  1227. ret = _archive_read_disk_open_w(_a, wpath.s);
  1228. archive_wstring_free(&wpath);
  1229. return (ret);
  1230. }
  1231. int
  1232. archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
  1233. {
  1234. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1235. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
  1236. ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
  1237. "archive_read_disk_open_w");
  1238. archive_clear_error(&a->archive);
  1239. return (_archive_read_disk_open_w(_a, pathname));
  1240. }
  1241. static int
  1242. _archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
  1243. {
  1244. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1245. if (a->tree != NULL)
  1246. a->tree = tree_reopen(a->tree, pathname,
  1247. a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
  1248. else
  1249. a->tree = tree_open(pathname, a->symlink_mode,
  1250. a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
  1251. if (a->tree == NULL) {
  1252. archive_set_error(&a->archive, ENOMEM,
  1253. "Can't allocate directory traversal data");
  1254. a->archive.state = ARCHIVE_STATE_FATAL;
  1255. return (ARCHIVE_FATAL);
  1256. }
  1257. a->archive.state = ARCHIVE_STATE_HEADER;
  1258. return (ARCHIVE_OK);
  1259. }
  1260. /*
  1261. * Return a current filesystem ID which is index of the filesystem entry
  1262. * you've visited through archive_read_disk.
  1263. */
  1264. int
  1265. archive_read_disk_current_filesystem(struct archive *_a)
  1266. {
  1267. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1268. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1269. "archive_read_disk_current_filesystem");
  1270. return (a->tree->current_filesystem_id);
  1271. }
  1272. static int
  1273. update_current_filesystem(struct archive_read_disk *a, int64_t dev)
  1274. {
  1275. struct tree *t = a->tree;
  1276. int i, fid;
  1277. if (t->current_filesystem != NULL &&
  1278. t->current_filesystem->dev == dev)
  1279. return (ARCHIVE_OK);
  1280. for (i = 0; i < t->max_filesystem_id; i++) {
  1281. if (t->filesystem_table[i].dev == dev) {
  1282. /* There is the filesystem ID we've already generated. */
  1283. t->current_filesystem_id = i;
  1284. t->current_filesystem = &(t->filesystem_table[i]);
  1285. return (ARCHIVE_OK);
  1286. }
  1287. }
  1288. /*
  1289. * There is a new filesystem, we generate a new ID for.
  1290. */
  1291. fid = t->max_filesystem_id++;
  1292. if (t->max_filesystem_id > t->allocated_filesystem) {
  1293. size_t s;
  1294. void *p;
  1295. s = t->max_filesystem_id * 2;
  1296. p = realloc(t->filesystem_table,
  1297. s * sizeof(*t->filesystem_table));
  1298. if (p == NULL) {
  1299. archive_set_error(&a->archive, ENOMEM,
  1300. "Can't allocate tar data");
  1301. return (ARCHIVE_FATAL);
  1302. }
  1303. t->filesystem_table = (struct filesystem *)p;
  1304. t->allocated_filesystem = (int)s;
  1305. }
  1306. t->current_filesystem_id = fid;
  1307. t->current_filesystem = &(t->filesystem_table[fid]);
  1308. t->current_filesystem->dev = dev;
  1309. return (setup_current_filesystem(a));
  1310. }
  1311. /*
  1312. * Returns 1 if current filesystem is generated filesystem, 0 if it is not
  1313. * or -1 if it is unknown.
  1314. */
  1315. int
  1316. archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
  1317. {
  1318. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1319. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1320. "archive_read_disk_current_filesystem");
  1321. return (a->tree->current_filesystem->synthetic);
  1322. }
  1323. /*
  1324. * Returns 1 if current filesystem is remote filesystem, 0 if it is not
  1325. * or -1 if it is unknown.
  1326. */
  1327. int
  1328. archive_read_disk_current_filesystem_is_remote(struct archive *_a)
  1329. {
  1330. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  1331. archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
  1332. "archive_read_disk_current_filesystem");
  1333. return (a->tree->current_filesystem->remote);
  1334. }
  1335. /*
  1336. * If symlink is broken, statfs or statvfs will fail.
  1337. * Use its directory path instead.
  1338. */
  1339. static wchar_t *
  1340. safe_path_for_statfs(struct tree *t)
  1341. {
  1342. const wchar_t *path;
  1343. wchar_t *cp, *p = NULL;
  1344. path = tree_current_access_path(t);
  1345. if (tree_current_stat(t) == NULL) {
  1346. p = _wcsdup(path);
  1347. cp = wcsrchr(p, '/');
  1348. if (cp != NULL && wcslen(cp) >= 2) {
  1349. cp[1] = '.';
  1350. cp[2] = '\0';
  1351. path = p;
  1352. }
  1353. } else
  1354. p = _wcsdup(path);
  1355. return (p);
  1356. }
  1357. /*
  1358. * Get conditions of synthetic and remote on Windows
  1359. */
  1360. static int
  1361. setup_current_filesystem(struct archive_read_disk *a)
  1362. {
  1363. struct tree *t = a->tree;
  1364. wchar_t vol[256];
  1365. wchar_t *path;
  1366. t->current_filesystem->synthetic = -1;/* Not supported */
  1367. path = safe_path_for_statfs(t);
  1368. if (!GetVolumePathNameW(path, vol, sizeof(vol)/sizeof(vol[0]))) {
  1369. free(path);
  1370. t->current_filesystem->remote = -1;
  1371. t->current_filesystem->bytesPerSector = 0;
  1372. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1373. "GetVolumePathName failed: %d", (int)GetLastError());
  1374. return (ARCHIVE_FAILED);
  1375. }
  1376. free(path);
  1377. switch (GetDriveTypeW(vol)) {
  1378. case DRIVE_UNKNOWN:
  1379. case DRIVE_NO_ROOT_DIR:
  1380. t->current_filesystem->remote = -1;
  1381. break;
  1382. case DRIVE_REMOTE:
  1383. t->current_filesystem->remote = 1;
  1384. break;
  1385. default:
  1386. t->current_filesystem->remote = 0;
  1387. break;
  1388. }
  1389. if (!GetDiskFreeSpaceW(vol, NULL,
  1390. &(t->current_filesystem->bytesPerSector), NULL, NULL)) {
  1391. t->current_filesystem->bytesPerSector = 0;
  1392. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1393. "GetDiskFreeSpace failed: %d", (int)GetLastError());
  1394. return (ARCHIVE_FAILED);
  1395. }
  1396. return (ARCHIVE_OK);
  1397. }
  1398. static int
  1399. close_and_restore_time(HANDLE h, struct tree *t, struct restore_time *rt)
  1400. {
  1401. HANDLE handle;
  1402. int r = 0;
  1403. #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1404. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  1405. #endif
  1406. if (h == INVALID_HANDLE_VALUE && AE_IFLNK == rt->filetype)
  1407. return (0);
  1408. /* Close a file descriptor.
  1409. * It will not be used for SetFileTime() because it has been opened
  1410. * by a read only mode.
  1411. */
  1412. if (h != INVALID_HANDLE_VALUE)
  1413. CloseHandle(h);
  1414. if ((t->flags & needsRestoreTimes) == 0)
  1415. return (r);
  1416. #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1417. ZeroMemory(&createExParams, sizeof(createExParams));
  1418. createExParams.dwSize = sizeof(createExParams);
  1419. createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
  1420. handle = CreateFile2(rt->full_path, FILE_WRITE_ATTRIBUTES,
  1421. 0, OPEN_EXISTING, &createExParams);
  1422. #else
  1423. handle = CreateFileW(rt->full_path, FILE_WRITE_ATTRIBUTES,
  1424. 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  1425. #endif
  1426. if (handle == INVALID_HANDLE_VALUE) {
  1427. errno = EINVAL;
  1428. return (-1);
  1429. }
  1430. if (SetFileTime(handle, NULL, &rt->lastAccessTime,
  1431. &rt->lastWriteTime) == 0) {
  1432. errno = EINVAL;
  1433. r = -1;
  1434. } else
  1435. r = 0;
  1436. CloseHandle(handle);
  1437. return (r);
  1438. }
  1439. /*
  1440. * Add a directory path to the current stack.
  1441. */
  1442. static void
  1443. tree_push(struct tree *t, const wchar_t *path, const wchar_t *full_path,
  1444. int filesystem_id, int64_t dev, int64_t ino, struct restore_time *rt)
  1445. {
  1446. struct tree_entry *te;
  1447. te = calloc(1, sizeof(*te));
  1448. te->next = t->stack;
  1449. te->parent = t->current;
  1450. if (te->parent)
  1451. te->depth = te->parent->depth + 1;
  1452. t->stack = te;
  1453. archive_string_init(&te->name);
  1454. archive_wstrcpy(&te->name, path);
  1455. archive_string_init(&te->full_path);
  1456. archive_wstrcpy(&te->full_path, full_path);
  1457. te->flags = needsDescent | needsOpen | needsAscent;
  1458. te->filesystem_id = filesystem_id;
  1459. te->dev = dev;
  1460. te->ino = ino;
  1461. te->dirname_length = t->dirname_length;
  1462. te->full_path_dir_length = t->full_path_dir_length;
  1463. te->restore_time.full_path = te->full_path.s;
  1464. if (rt != NULL) {
  1465. te->restore_time.lastWriteTime = rt->lastWriteTime;
  1466. te->restore_time.lastAccessTime = rt->lastAccessTime;
  1467. te->restore_time.filetype = rt->filetype;
  1468. }
  1469. }
  1470. /*
  1471. * Append a name to the current dir path.
  1472. */
  1473. static int
  1474. tree_append(struct tree *t, const wchar_t *name, size_t name_length)
  1475. {
  1476. size_t size_needed;
  1477. t->path.s[t->dirname_length] = L'\0';
  1478. t->path.length = t->dirname_length;
  1479. /* Strip trailing '/' from name, unless entire name is "/". */
  1480. while (name_length > 1 && name[name_length - 1] == L'/')
  1481. name_length--;
  1482. /* Resize pathname buffer as needed. */
  1483. size_needed = name_length + t->dirname_length + 2;
  1484. if (archive_wstring_ensure(&t->path, size_needed) == NULL)
  1485. return (TREE_ERROR_FATAL);
  1486. /* Add a separating '/' if it's needed. */
  1487. if (t->dirname_length > 0 &&
  1488. t->path.s[archive_strlen(&t->path)-1] != L'/')
  1489. archive_wstrappend_wchar(&t->path, L'/');
  1490. t->basename = t->path.s + archive_strlen(&t->path);
  1491. archive_wstrncat(&t->path, name, name_length);
  1492. t->restore_time.full_path = t->basename;
  1493. if (t->full_path_dir_length > 0) {
  1494. t->full_path.s[t->full_path_dir_length] = L'\0';
  1495. t->full_path.length = t->full_path_dir_length;
  1496. size_needed = name_length + t->full_path_dir_length + 2;
  1497. if (archive_wstring_ensure(&t->full_path, size_needed) == NULL)
  1498. return (TREE_ERROR_FATAL);
  1499. /* Add a separating '\' if it's needed. */
  1500. if (t->full_path.s[archive_strlen(&t->full_path)-1] != L'\\')
  1501. archive_wstrappend_wchar(&t->full_path, L'\\');
  1502. archive_wstrncat(&t->full_path, name, name_length);
  1503. t->restore_time.full_path = t->full_path.s;
  1504. }
  1505. return (0);
  1506. }
  1507. /*
  1508. * Open a directory tree for traversal.
  1509. */
  1510. static struct tree *
  1511. tree_open(const wchar_t *path, int symlink_mode, int restore_time)
  1512. {
  1513. struct tree *t;
  1514. t = calloc(1, sizeof(*t));
  1515. archive_string_init(&(t->full_path));
  1516. archive_string_init(&t->path);
  1517. if (archive_wstring_ensure(&t->path, 15) == NULL) {
  1518. free(t);
  1519. return (NULL);
  1520. }
  1521. t->initial_symlink_mode = symlink_mode;
  1522. return (tree_reopen(t, path, restore_time));
  1523. }
  1524. static struct tree *
  1525. tree_reopen(struct tree *t, const wchar_t *path, int restore_time)
  1526. {
  1527. struct archive_wstring ws;
  1528. wchar_t *pathname, *p, *base;
  1529. t->flags = (restore_time != 0)?needsRestoreTimes:0;
  1530. t->visit_type = 0;
  1531. t->tree_errno = 0;
  1532. t->full_path_dir_length = 0;
  1533. t->dirname_length = 0;
  1534. t->depth = 0;
  1535. t->descend = 0;
  1536. t->current = NULL;
  1537. t->d = INVALID_HANDLE_VALUE;
  1538. t->symlink_mode = t->initial_symlink_mode;
  1539. archive_string_empty(&(t->full_path));
  1540. archive_string_empty(&t->path);
  1541. t->entry_fh = INVALID_HANDLE_VALUE;
  1542. t->entry_eof = 0;
  1543. t->entry_remaining_bytes = 0;
  1544. t->initial_filesystem_id = -1;
  1545. /* Get wchar_t strings from char strings. */
  1546. archive_string_init(&ws);
  1547. archive_wstrcpy(&ws, path);
  1548. pathname = ws.s;
  1549. /* Get a full-path-name. */
  1550. p = __la_win_permissive_name_w(pathname);
  1551. if (p == NULL)
  1552. goto failed;
  1553. archive_wstrcpy(&(t->full_path), p);
  1554. free(p);
  1555. /* Convert path separators from '\' to '/' */
  1556. for (p = pathname; *p != L'\0'; ++p) {
  1557. if (*p == L'\\')
  1558. *p = L'/';
  1559. }
  1560. base = pathname;
  1561. /* First item is set up a lot like a symlink traversal. */
  1562. /* printf("Looking for wildcard in %s\n", path); */
  1563. if ((base[0] == L'/' && base[1] == L'/' &&
  1564. base[2] == L'?' && base[3] == L'/' &&
  1565. (wcschr(base+4, L'*') || wcschr(base+4, L'?'))) ||
  1566. (!(base[0] == L'/' && base[1] == L'/' &&
  1567. base[2] == L'?' && base[3] == L'/') &&
  1568. (wcschr(base, L'*') || wcschr(base, L'?')))) {
  1569. // It has a wildcard in it...
  1570. // Separate the last element.
  1571. p = wcsrchr(base, L'/');
  1572. if (p != NULL) {
  1573. *p = L'\0';
  1574. if (tree_append(t, base, p - base))
  1575. goto failed;
  1576. t->dirname_length = archive_strlen(&t->path);
  1577. base = p + 1;
  1578. }
  1579. p = wcsrchr(t->full_path.s, L'\\');
  1580. if (p != NULL) {
  1581. *p = L'\0';
  1582. t->full_path.length = wcslen(t->full_path.s);
  1583. t->full_path_dir_length = archive_strlen(&t->full_path);
  1584. }
  1585. }
  1586. tree_push(t, base, t->full_path.s, 0, 0, 0, NULL);
  1587. archive_wstring_free(&ws);
  1588. t->stack->flags = needsFirstVisit;
  1589. /*
  1590. * Debug flag for Direct IO(No buffering) or Async IO.
  1591. * Those dependent on environment variable switches
  1592. * will be removed until next release.
  1593. */
  1594. {
  1595. const char *e;
  1596. if ((e = getenv("LIBARCHIVE_DIRECT_IO")) != NULL) {
  1597. if (e[0] == '0')
  1598. t->direct_io = 0;
  1599. else
  1600. t->direct_io = 1;
  1601. fprintf(stderr, "LIBARCHIVE_DIRECT_IO=%s\n",
  1602. (t->direct_io)?"Enabled":"Disabled");
  1603. } else
  1604. t->direct_io = DIRECT_IO;
  1605. if ((e = getenv("LIBARCHIVE_ASYNC_IO")) != NULL) {
  1606. if (e[0] == '0')
  1607. t->async_io = 0;
  1608. else
  1609. t->async_io = 1;
  1610. fprintf(stderr, "LIBARCHIVE_ASYNC_IO=%s\n",
  1611. (t->async_io)?"Enabled":"Disabled");
  1612. } else
  1613. t->async_io = ASYNC_IO;
  1614. }
  1615. return (t);
  1616. failed:
  1617. archive_wstring_free(&ws);
  1618. tree_free(t);
  1619. return (NULL);
  1620. }
  1621. static int
  1622. tree_descent(struct tree *t)
  1623. {
  1624. t->dirname_length = archive_strlen(&t->path);
  1625. t->full_path_dir_length = archive_strlen(&t->full_path);
  1626. t->depth++;
  1627. return (0);
  1628. }
  1629. /*
  1630. * We've finished a directory; ascend back to the parent.
  1631. */
  1632. static int
  1633. tree_ascend(struct tree *t)
  1634. {
  1635. struct tree_entry *te;
  1636. te = t->stack;
  1637. t->depth--;
  1638. close_and_restore_time(INVALID_HANDLE_VALUE, t, &te->restore_time);
  1639. return (0);
  1640. }
  1641. /*
  1642. * Pop the working stack.
  1643. */
  1644. static void
  1645. tree_pop(struct tree *t)
  1646. {
  1647. struct tree_entry *te;
  1648. t->full_path.s[t->full_path_dir_length] = L'\0';
  1649. t->full_path.length = t->full_path_dir_length;
  1650. t->path.s[t->dirname_length] = L'\0';
  1651. t->path.length = t->dirname_length;
  1652. if (t->stack == t->current && t->current != NULL)
  1653. t->current = t->current->parent;
  1654. te = t->stack;
  1655. t->stack = te->next;
  1656. t->dirname_length = te->dirname_length;
  1657. t->basename = t->path.s + t->dirname_length;
  1658. t->full_path_dir_length = te->full_path_dir_length;
  1659. while (t->basename[0] == L'/')
  1660. t->basename++;
  1661. archive_wstring_free(&te->name);
  1662. archive_wstring_free(&te->full_path);
  1663. free(te);
  1664. }
  1665. /*
  1666. * Get the next item in the tree traversal.
  1667. */
  1668. static int
  1669. tree_next(struct tree *t)
  1670. {
  1671. int r;
  1672. while (t->stack != NULL) {
  1673. /* If there's an open dir, get the next entry from there. */
  1674. if (t->d != INVALID_HANDLE_VALUE) {
  1675. r = tree_dir_next_windows(t, NULL);
  1676. if (r == 0)
  1677. continue;
  1678. return (r);
  1679. }
  1680. if (t->stack->flags & needsFirstVisit) {
  1681. wchar_t *d = t->stack->name.s;
  1682. t->stack->flags &= ~needsFirstVisit;
  1683. if (!(d[0] == L'/' && d[1] == L'/' &&
  1684. d[2] == L'?' && d[3] == L'/') &&
  1685. (wcschr(d, L'*') || wcschr(d, L'?'))) {
  1686. r = tree_dir_next_windows(t, d);
  1687. if (r == 0)
  1688. continue;
  1689. return (r);
  1690. } else {
  1691. HANDLE h = FindFirstFileW(t->stack->full_path.s, &t->_findData);
  1692. if (h == INVALID_HANDLE_VALUE) {
  1693. la_dosmaperr(GetLastError());
  1694. t->tree_errno = errno;
  1695. t->visit_type = TREE_ERROR_DIR;
  1696. return (t->visit_type);
  1697. }
  1698. t->findData = &t->_findData;
  1699. FindClose(h);
  1700. }
  1701. /* Top stack item needs a regular visit. */
  1702. t->current = t->stack;
  1703. r = tree_append(t, t->stack->name.s,
  1704. archive_strlen(&(t->stack->name)));
  1705. if (r != 0)
  1706. return (r);
  1707. //t->dirname_length = t->path_length;
  1708. //tree_pop(t);
  1709. t->stack->flags &= ~needsFirstVisit;
  1710. return (t->visit_type = TREE_REGULAR);
  1711. } else if (t->stack->flags & needsDescent) {
  1712. /* Top stack item is dir to descend into. */
  1713. t->current = t->stack;
  1714. r = tree_append(t, t->stack->name.s,
  1715. archive_strlen(&(t->stack->name)));
  1716. if (r != 0)
  1717. return (r);
  1718. t->stack->flags &= ~needsDescent;
  1719. r = tree_descent(t);
  1720. if (r != 0) {
  1721. tree_pop(t);
  1722. t->visit_type = r;
  1723. } else
  1724. t->visit_type = TREE_POSTDESCENT;
  1725. return (t->visit_type);
  1726. } else if (t->stack->flags & needsOpen) {
  1727. t->stack->flags &= ~needsOpen;
  1728. r = tree_dir_next_windows(t, L"*");
  1729. if (r == 0)
  1730. continue;
  1731. return (r);
  1732. } else if (t->stack->flags & needsAscent) {
  1733. /* Top stack item is dir and we're done with it. */
  1734. r = tree_ascend(t);
  1735. tree_pop(t);
  1736. t->visit_type = r != 0 ? r : TREE_POSTASCENT;
  1737. return (t->visit_type);
  1738. } else {
  1739. /* Top item on stack is dead. */
  1740. tree_pop(t);
  1741. t->flags &= ~hasLstat;
  1742. t->flags &= ~hasStat;
  1743. }
  1744. }
  1745. return (t->visit_type = 0);
  1746. }
  1747. static int
  1748. tree_dir_next_windows(struct tree *t, const wchar_t *pattern)
  1749. {
  1750. const wchar_t *name;
  1751. size_t namelen;
  1752. int r;
  1753. for (;;) {
  1754. if (pattern != NULL) {
  1755. struct archive_wstring pt;
  1756. archive_string_init(&pt);
  1757. if (archive_wstring_ensure(&pt,
  1758. archive_strlen(&(t->full_path))
  1759. + 2 + wcslen(pattern)) == NULL)
  1760. return (TREE_ERROR_FATAL);
  1761. archive_wstring_copy(&pt, &(t->full_path));
  1762. archive_wstrappend_wchar(&pt, L'\\');
  1763. archive_wstrcat(&pt, pattern);
  1764. t->d = FindFirstFileW(pt.s, &t->_findData);
  1765. archive_wstring_free(&pt);
  1766. if (t->d == INVALID_HANDLE_VALUE) {
  1767. la_dosmaperr(GetLastError());
  1768. t->tree_errno = errno;
  1769. r = tree_ascend(t); /* Undo "chdir" */
  1770. tree_pop(t);
  1771. t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
  1772. return (t->visit_type);
  1773. }
  1774. /* Top stack item needs a regular visit. */
  1775. t->current = t->stack;
  1776. t->findData = &t->_findData;
  1777. pattern = NULL;
  1778. } else if (!FindNextFileW(t->d, &t->_findData)) {
  1779. FindClose(t->d);
  1780. t->d = INVALID_HANDLE_VALUE;
  1781. t->findData = NULL;
  1782. return (0);
  1783. }
  1784. name = t->findData->cFileName;
  1785. namelen = wcslen(name);
  1786. t->flags &= ~hasLstat;
  1787. t->flags &= ~hasStat;
  1788. if (name[0] == L'.' && name[1] == L'\0')
  1789. continue;
  1790. if (name[0] == L'.' && name[1] == L'.' && name[2] == L'\0')
  1791. continue;
  1792. r = tree_append(t, name, namelen);
  1793. if (r != 0)
  1794. return (r);
  1795. return (t->visit_type = TREE_REGULAR);
  1796. }
  1797. }
  1798. static void
  1799. entry_copy_bhfi(struct archive_entry *entry, const wchar_t *path,
  1800. const WIN32_FIND_DATAW *findData,
  1801. const BY_HANDLE_FILE_INFORMATION *bhfi)
  1802. {
  1803. int64_t secs;
  1804. uint32_t nsecs;
  1805. mode_t mode;
  1806. ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastAccessTime), &secs, &nsecs);
  1807. archive_entry_set_atime(entry, secs, nsecs);
  1808. ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastWriteTime), &secs, &nsecs);
  1809. archive_entry_set_mtime(entry, secs, nsecs);
  1810. ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftCreationTime), &secs, &nsecs);
  1811. archive_entry_set_birthtime(entry, secs, nsecs);
  1812. archive_entry_set_ctime(entry, secs, nsecs);
  1813. archive_entry_set_dev(entry, bhfi_dev(bhfi));
  1814. archive_entry_set_ino64(entry, bhfi_ino(bhfi));
  1815. if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1816. archive_entry_set_nlink(entry, bhfi->nNumberOfLinks + 1);
  1817. else
  1818. archive_entry_set_nlink(entry, bhfi->nNumberOfLinks);
  1819. archive_entry_set_size(entry,
  1820. (((int64_t)bhfi->nFileSizeHigh) << 32)
  1821. + bhfi->nFileSizeLow);
  1822. archive_entry_set_uid(entry, 0);
  1823. archive_entry_set_gid(entry, 0);
  1824. archive_entry_set_rdev(entry, 0);
  1825. mode = S_IRUSR | S_IRGRP | S_IROTH;
  1826. if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
  1827. mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  1828. if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  1829. findData != NULL &&
  1830. findData->dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
  1831. mode |= S_IFLNK;
  1832. entry_symlink_from_pathw(entry, path);
  1833. } else if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1834. mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  1835. else {
  1836. const wchar_t *p;
  1837. mode |= S_IFREG;
  1838. p = wcsrchr(path, L'.');
  1839. if (p != NULL && wcslen(p) == 4) {
  1840. switch (p[1]) {
  1841. case L'B': case L'b':
  1842. if ((p[2] == L'A' || p[2] == L'a' ) &&
  1843. (p[3] == L'T' || p[3] == L't' ))
  1844. mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  1845. break;
  1846. case L'C': case L'c':
  1847. if (((p[2] == L'M' || p[2] == L'm' ) &&
  1848. (p[3] == L'D' || p[3] == L'd' )))
  1849. mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  1850. break;
  1851. case L'E': case L'e':
  1852. if ((p[2] == L'X' || p[2] == L'x' ) &&
  1853. (p[3] == L'E' || p[3] == L'e' ))
  1854. mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  1855. break;
  1856. default:
  1857. break;
  1858. }
  1859. }
  1860. }
  1861. archive_entry_set_mode(entry, mode);
  1862. }
  1863. static void
  1864. tree_archive_entry_copy_bhfi(struct archive_entry *entry, struct tree *t,
  1865. const BY_HANDLE_FILE_INFORMATION *bhfi)
  1866. {
  1867. entry_copy_bhfi(entry, tree_current_path(t), t->findData, bhfi);
  1868. }
  1869. static int
  1870. tree_current_file_information(struct tree *t, BY_HANDLE_FILE_INFORMATION *st,
  1871. int sim_lstat)
  1872. {
  1873. HANDLE h;
  1874. int r;
  1875. DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
  1876. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1877. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  1878. #endif
  1879. if (sim_lstat && tree_current_is_physical_link(t))
  1880. flag |= FILE_FLAG_OPEN_REPARSE_POINT;
  1881. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  1882. ZeroMemory(&createExParams, sizeof(createExParams));
  1883. createExParams.dwSize = sizeof(createExParams);
  1884. createExParams.dwFileFlags = flag;
  1885. h = CreateFile2(tree_current_access_path(t), 0,
  1886. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  1887. OPEN_EXISTING, &createExParams);
  1888. #else
  1889. h = CreateFileW(tree_current_access_path(t), 0,
  1890. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
  1891. OPEN_EXISTING, flag, NULL);
  1892. #endif
  1893. if (h == INVALID_HANDLE_VALUE) {
  1894. la_dosmaperr(GetLastError());
  1895. t->tree_errno = errno;
  1896. return (0);
  1897. }
  1898. r = GetFileInformationByHandle(h, st);
  1899. CloseHandle(h);
  1900. return (r);
  1901. }
  1902. /*
  1903. * Get the stat() data for the entry just returned from tree_next().
  1904. */
  1905. static const BY_HANDLE_FILE_INFORMATION *
  1906. tree_current_stat(struct tree *t)
  1907. {
  1908. if (!(t->flags & hasStat)) {
  1909. if (!tree_current_file_information(t, &t->st, 0))
  1910. return NULL;
  1911. t->flags |= hasStat;
  1912. }
  1913. return (&t->st);
  1914. }
  1915. /*
  1916. * Get the lstat() data for the entry just returned from tree_next().
  1917. */
  1918. static const BY_HANDLE_FILE_INFORMATION *
  1919. tree_current_lstat(struct tree *t)
  1920. {
  1921. if (!(t->flags & hasLstat)) {
  1922. if (!tree_current_file_information(t, &t->lst, 1))
  1923. return NULL;
  1924. t->flags |= hasLstat;
  1925. }
  1926. return (&t->lst);
  1927. }
  1928. /*
  1929. * Test whether current entry is a dir or link to a dir.
  1930. */
  1931. static int
  1932. tree_current_is_dir(struct tree *t)
  1933. {
  1934. if (t->findData)
  1935. return (t->findData->dwFileAttributes
  1936. & FILE_ATTRIBUTE_DIRECTORY);
  1937. return (0);
  1938. }
  1939. /*
  1940. * Test whether current entry is a physical directory. Usually, we
  1941. * already have at least one of stat() or lstat() in memory, so we
  1942. * use tricks to try to avoid an extra trip to the disk.
  1943. */
  1944. static int
  1945. tree_current_is_physical_dir(struct tree *t)
  1946. {
  1947. if (tree_current_is_physical_link(t))
  1948. return (0);
  1949. return (tree_current_is_dir(t));
  1950. }
  1951. /*
  1952. * Test whether current entry is a symbolic link.
  1953. */
  1954. static int
  1955. tree_current_is_physical_link(struct tree *t)
  1956. {
  1957. if (t->findData)
  1958. return ((t->findData->dwFileAttributes
  1959. & FILE_ATTRIBUTE_REPARSE_POINT) &&
  1960. (t->findData->dwReserved0
  1961. == IO_REPARSE_TAG_SYMLINK));
  1962. return (0);
  1963. }
  1964. /*
  1965. * Test whether the same file has been in the tree as its parent.
  1966. */
  1967. static int
  1968. tree_target_is_same_as_parent(struct tree *t,
  1969. const BY_HANDLE_FILE_INFORMATION *st)
  1970. {
  1971. struct tree_entry *te;
  1972. int64_t dev = bhfi_dev(st);
  1973. int64_t ino = bhfi_ino(st);
  1974. for (te = t->current->parent; te != NULL; te = te->parent) {
  1975. if (te->dev == dev && te->ino == ino)
  1976. return (1);
  1977. }
  1978. return (0);
  1979. }
  1980. /*
  1981. * Return the access path for the entry just returned from tree_next().
  1982. */
  1983. static const wchar_t *
  1984. tree_current_access_path(struct tree *t)
  1985. {
  1986. return (t->full_path.s);
  1987. }
  1988. /*
  1989. * Return the full path for the entry just returned from tree_next().
  1990. */
  1991. static const wchar_t *
  1992. tree_current_path(struct tree *t)
  1993. {
  1994. return (t->path.s);
  1995. }
  1996. /*
  1997. * Terminate the traversal.
  1998. */
  1999. static void
  2000. tree_close(struct tree *t)
  2001. {
  2002. if (t == NULL)
  2003. return;
  2004. if (t->entry_fh != INVALID_HANDLE_VALUE) {
  2005. cancel_async(t);
  2006. close_and_restore_time(t->entry_fh, t, &t->restore_time);
  2007. t->entry_fh = INVALID_HANDLE_VALUE;
  2008. }
  2009. /* Close the handle of FindFirstFileW */
  2010. if (t->d != INVALID_HANDLE_VALUE) {
  2011. FindClose(t->d);
  2012. t->d = INVALID_HANDLE_VALUE;
  2013. t->findData = NULL;
  2014. }
  2015. /* Release anything remaining in the stack. */
  2016. while (t->stack != NULL)
  2017. tree_pop(t);
  2018. }
  2019. /*
  2020. * Release any resources.
  2021. */
  2022. static void
  2023. tree_free(struct tree *t)
  2024. {
  2025. int i;
  2026. if (t == NULL)
  2027. return;
  2028. archive_wstring_free(&t->path);
  2029. archive_wstring_free(&t->full_path);
  2030. free(t->sparse_list);
  2031. free(t->filesystem_table);
  2032. for (i = 0; i < MAX_OVERLAPPED; i++) {
  2033. if (t->ol[i].buff)
  2034. VirtualFree(t->ol[i].buff, 0, MEM_RELEASE);
  2035. CloseHandle(t->ol[i].ol.hEvent);
  2036. }
  2037. free(t);
  2038. }
  2039. /*
  2040. * Populate the archive_entry with metadata from the disk.
  2041. */
  2042. int
  2043. archive_read_disk_entry_from_file(struct archive *_a,
  2044. struct archive_entry *entry, int fd, const struct stat *st)
  2045. {
  2046. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  2047. const wchar_t *path;
  2048. const wchar_t *wname;
  2049. const char *name;
  2050. HANDLE h;
  2051. BY_HANDLE_FILE_INFORMATION bhfi;
  2052. DWORD fileAttributes = 0;
  2053. int r;
  2054. archive_clear_error(_a);
  2055. wname = archive_entry_sourcepath_w(entry);
  2056. if (wname == NULL)
  2057. wname = archive_entry_pathname_w(entry);
  2058. if (wname == NULL) {
  2059. archive_set_error(&a->archive, EINVAL,
  2060. "Can't get a wide character version of the path");
  2061. return (ARCHIVE_FAILED);
  2062. }
  2063. path = __la_win_permissive_name_w(wname);
  2064. if (st == NULL) {
  2065. /*
  2066. * Get metadata through GetFileInformationByHandle().
  2067. */
  2068. if (fd >= 0) {
  2069. h = (HANDLE)_get_osfhandle(fd);
  2070. r = GetFileInformationByHandle(h, &bhfi);
  2071. if (r == 0) {
  2072. la_dosmaperr(GetLastError());
  2073. archive_set_error(&a->archive, errno,
  2074. "Can't GetFileInformationByHandle");
  2075. return (ARCHIVE_FAILED);
  2076. }
  2077. entry_copy_bhfi(entry, path, NULL, &bhfi);
  2078. } else {
  2079. WIN32_FIND_DATAW findData;
  2080. DWORD flag, desiredAccess;
  2081. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  2082. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  2083. #endif
  2084. h = FindFirstFileW(path, &findData);
  2085. if (h == INVALID_HANDLE_VALUE) {
  2086. la_dosmaperr(GetLastError());
  2087. archive_set_error(&a->archive, errno,
  2088. "Can't FindFirstFileW");
  2089. return (ARCHIVE_FAILED);
  2090. }
  2091. FindClose(h);
  2092. flag = FILE_FLAG_BACKUP_SEMANTICS;
  2093. if (!a->follow_symlinks &&
  2094. (findData.dwFileAttributes
  2095. & FILE_ATTRIBUTE_REPARSE_POINT) &&
  2096. (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
  2097. flag |= FILE_FLAG_OPEN_REPARSE_POINT;
  2098. desiredAccess = 0;
  2099. } else if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  2100. desiredAccess = 0;
  2101. } else
  2102. desiredAccess = GENERIC_READ;
  2103. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  2104. ZeroMemory(&createExParams, sizeof(createExParams));
  2105. createExParams.dwSize = sizeof(createExParams);
  2106. createExParams.dwFileFlags = flag;
  2107. h = CreateFile2(path, desiredAccess,
  2108. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  2109. OPEN_EXISTING, &createExParams);
  2110. #else
  2111. h = CreateFileW(path, desiredAccess,
  2112. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
  2113. OPEN_EXISTING, flag, NULL);
  2114. #endif
  2115. if (h == INVALID_HANDLE_VALUE) {
  2116. la_dosmaperr(GetLastError());
  2117. archive_set_error(&a->archive, errno,
  2118. "Can't CreateFileW");
  2119. return (ARCHIVE_FAILED);
  2120. }
  2121. r = GetFileInformationByHandle(h, &bhfi);
  2122. if (r == 0) {
  2123. la_dosmaperr(GetLastError());
  2124. archive_set_error(&a->archive, errno,
  2125. "Can't GetFileInformationByHandle");
  2126. CloseHandle(h);
  2127. return (ARCHIVE_FAILED);
  2128. }
  2129. entry_copy_bhfi(entry, path, &findData, &bhfi);
  2130. }
  2131. fileAttributes = bhfi.dwFileAttributes;
  2132. } else {
  2133. archive_entry_copy_stat(entry, st);
  2134. if (st->st_mode & S_IFLNK)
  2135. entry_symlink_from_pathw(entry, path);
  2136. h = INVALID_HANDLE_VALUE;
  2137. }
  2138. /* Lookup uname/gname */
  2139. name = archive_read_disk_uname(_a, archive_entry_uid(entry));
  2140. if (name != NULL)
  2141. archive_entry_copy_uname(entry, name);
  2142. name = archive_read_disk_gname(_a, archive_entry_gid(entry));
  2143. if (name != NULL)
  2144. archive_entry_copy_gname(entry, name);
  2145. /*
  2146. * File attributes
  2147. */
  2148. if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
  2149. const int supported_attrs =
  2150. FILE_ATTRIBUTE_READONLY |
  2151. FILE_ATTRIBUTE_HIDDEN |
  2152. FILE_ATTRIBUTE_SYSTEM;
  2153. DWORD file_attrs = fileAttributes & supported_attrs;
  2154. if (file_attrs != 0)
  2155. archive_entry_set_fflags(entry, file_attrs, 0);
  2156. }
  2157. /*
  2158. * Can this file be sparse file ?
  2159. */
  2160. if (archive_entry_filetype(entry) != AE_IFREG
  2161. || archive_entry_size(entry) <= 0
  2162. || archive_entry_hardlink(entry) != NULL) {
  2163. if (h != INVALID_HANDLE_VALUE && fd < 0)
  2164. CloseHandle(h);
  2165. return (ARCHIVE_OK);
  2166. }
  2167. if (h == INVALID_HANDLE_VALUE) {
  2168. if (fd >= 0) {
  2169. h = (HANDLE)_get_osfhandle(fd);
  2170. } else {
  2171. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  2172. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  2173. ZeroMemory(&createExParams, sizeof(createExParams));
  2174. createExParams.dwSize = sizeof(createExParams);
  2175. createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
  2176. h = CreateFile2(path, GENERIC_READ,
  2177. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  2178. OPEN_EXISTING, &createExParams);
  2179. #else
  2180. h = CreateFileW(path, GENERIC_READ,
  2181. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
  2182. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  2183. #endif
  2184. if (h == INVALID_HANDLE_VALUE) {
  2185. la_dosmaperr(GetLastError());
  2186. archive_set_error(&a->archive, errno,
  2187. "Can't CreateFileW");
  2188. return (ARCHIVE_FAILED);
  2189. }
  2190. }
  2191. r = GetFileInformationByHandle(h, &bhfi);
  2192. if (r == 0) {
  2193. la_dosmaperr(GetLastError());
  2194. archive_set_error(&a->archive, errno,
  2195. "Can't GetFileInformationByHandle");
  2196. if (h != INVALID_HANDLE_VALUE && fd < 0)
  2197. CloseHandle(h);
  2198. return (ARCHIVE_FAILED);
  2199. }
  2200. fileAttributes = bhfi.dwFileAttributes;
  2201. }
  2202. /* Sparse file must be set a mark, FILE_ATTRIBUTE_SPARSE_FILE */
  2203. if ((fileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
  2204. if (fd < 0)
  2205. CloseHandle(h);
  2206. return (ARCHIVE_OK);
  2207. }
  2208. r = ARCHIVE_OK;
  2209. if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
  2210. r = setup_sparse_from_disk(a, entry, h);
  2211. if (fd < 0)
  2212. CloseHandle(h);
  2213. }
  2214. return (r);
  2215. }
  2216. /*
  2217. * Windows sparse interface.
  2218. */
  2219. #if defined(__MINGW32__) && !defined(FSCTL_QUERY_ALLOCATED_RANGES)
  2220. #define FSCTL_QUERY_ALLOCATED_RANGES 0x940CF
  2221. typedef struct {
  2222. LARGE_INTEGER FileOffset;
  2223. LARGE_INTEGER Length;
  2224. } FILE_ALLOCATED_RANGE_BUFFER;
  2225. #endif
  2226. static int
  2227. setup_sparse_from_disk(struct archive_read_disk *a,
  2228. struct archive_entry *entry, HANDLE handle)
  2229. {
  2230. FILE_ALLOCATED_RANGE_BUFFER range, *outranges = NULL;
  2231. size_t outranges_size;
  2232. int64_t entry_size = archive_entry_size(entry);
  2233. int exit_sts = ARCHIVE_OK;
  2234. range.FileOffset.QuadPart = 0;
  2235. range.Length.QuadPart = entry_size;
  2236. outranges_size = 2048;
  2237. outranges = malloc(outranges_size);
  2238. if (outranges == NULL) {
  2239. archive_set_error(&a->archive, ENOMEM,
  2240. "Couldn't allocate memory");
  2241. exit_sts = ARCHIVE_FATAL;
  2242. goto exit_setup_sparse;
  2243. }
  2244. for (;;) {
  2245. DWORD retbytes;
  2246. BOOL ret;
  2247. for (;;) {
  2248. ret = DeviceIoControl(handle,
  2249. FSCTL_QUERY_ALLOCATED_RANGES,
  2250. &range, sizeof(range), outranges,
  2251. (DWORD)outranges_size, &retbytes, NULL);
  2252. if (ret == 0 && GetLastError() == ERROR_MORE_DATA) {
  2253. free(outranges);
  2254. outranges_size *= 2;
  2255. outranges = (FILE_ALLOCATED_RANGE_BUFFER *)
  2256. malloc(outranges_size);
  2257. if (outranges == NULL) {
  2258. archive_set_error(&a->archive, ENOMEM,
  2259. "Couldn't allocate memory");
  2260. exit_sts = ARCHIVE_FATAL;
  2261. goto exit_setup_sparse;
  2262. }
  2263. continue;
  2264. } else
  2265. break;
  2266. }
  2267. if (ret != 0) {
  2268. if (retbytes > 0) {
  2269. DWORD i, n;
  2270. n = retbytes / sizeof(outranges[0]);
  2271. if (n == 1 &&
  2272. outranges[0].FileOffset.QuadPart == 0 &&
  2273. outranges[0].Length.QuadPart == entry_size)
  2274. break;/* This is not sparse. */
  2275. for (i = 0; i < n; i++)
  2276. archive_entry_sparse_add_entry(entry,
  2277. outranges[i].FileOffset.QuadPart,
  2278. outranges[i].Length.QuadPart);
  2279. range.FileOffset.QuadPart =
  2280. outranges[n-1].FileOffset.QuadPart
  2281. + outranges[n-1].Length.QuadPart;
  2282. range.Length.QuadPart =
  2283. entry_size - range.FileOffset.QuadPart;
  2284. if (range.Length.QuadPart > 0)
  2285. continue;
  2286. } else {
  2287. /* The entire file is a hole. Add one data block of size 0 at the end. */
  2288. archive_entry_sparse_add_entry(entry,
  2289. entry_size,
  2290. 0);
  2291. }
  2292. break;
  2293. } else {
  2294. la_dosmaperr(GetLastError());
  2295. archive_set_error(&a->archive, errno,
  2296. "DeviceIoControl Failed: %lu", GetLastError());
  2297. exit_sts = ARCHIVE_FAILED;
  2298. goto exit_setup_sparse;
  2299. }
  2300. }
  2301. exit_setup_sparse:
  2302. free(outranges);
  2303. return (exit_sts);
  2304. }
  2305. #endif