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