archive_read_disk_windows.c 63 KB

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