archive_read_disk_windows.c 63 KB

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