-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy patharchive_read_disk_windows.c
2477 lines (2229 loc) · 66.8 KB
/
archive_read_disk_windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2003-2009 Tim Kientzle
* Copyright (c) 2010-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "archive_platform.h"
__FBSDID("$FreeBSD$");
#if defined(_WIN32) && !defined(__CYGWIN__)
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <winioctl.h>
#include "archive.h"
#include "archive_string.h"
#include "archive_entry.h"
#include "archive_private.h"
#include "archive_read_disk_private.h"
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef IO_REPARSE_TAG_SYMLINK
/* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
#define IO_REPARSE_TAG_SYMLINK 0xA000000CL
#endif
/*-
* This is a new directory-walking system that addresses a number
* of problems I've had with fts(3). In particular, it has no
* pathname-length limits (other than the size of 'int'), handles
* deep logical traversals, uses considerably less memory, and has
* an opaque interface (easier to modify in the future).
*
* Internally, it keeps a single list of "tree_entry" items that
* represent filesystem objects that require further attention.
* Non-directories are not kept in memory: they are pulled from
* readdir(), returned to the client, then freed as soon as possible.
* Any directory entry to be traversed gets pushed onto the stack.
*
* There is surprisingly little information that needs to be kept for
* each item on the stack. Just the name, depth (represented here as the
* string length of the parent directory's pathname), and some markers
* indicating how to get back to the parent (via chdir("..") for a
* regular dir or via fchdir(2) for a symlink).
*/
struct restore_time {
const wchar_t *full_path;
FILETIME lastWriteTime;
FILETIME lastAccessTime;
mode_t filetype;
};
struct tree_entry {
int depth;
struct tree_entry *next;
struct tree_entry *parent;
size_t full_path_dir_length;
struct archive_wstring name;
struct archive_wstring full_path;
size_t dirname_length;
int64_t dev;
int64_t ino;
int flags;
int filesystem_id;
/* How to restore time of a directory. */
struct restore_time restore_time;
};
struct filesystem {
int64_t dev;
int synthetic;
int remote;
DWORD bytesPerSector;
};
/* Definitions for tree_entry.flags bitmap. */
#define isDir 1 /* This entry is a regular directory. */
#define isDirLink 2 /* This entry is a symbolic link to a directory. */
#define needsFirstVisit 4 /* This is an initial entry. */
#define needsDescent 8 /* This entry needs to be previsited. */
#define needsOpen 16 /* This is a directory that needs to be opened. */
#define needsAscent 32 /* This entry needs to be postvisited. */
/*
* On Windows, "first visit" is handled as a pattern to be handed to
* _findfirst(). This is consistent with Windows conventions that
* file patterns are handled within the application. On Posix,
* "first visit" is just returned to the client.
*/
#define MAX_OVERLAPPED 8
#define READ_BUFFER_SIZE (1024 * 64) /* Default to 64KB per https://technet.microsoft.com/en-us/library/cc938632.aspx */
#define DIRECT_IO 0/* Disabled */
#define ASYNC_IO 1/* Enabled */
/*
* Local data for this package.
*/
struct tree {
struct tree_entry *stack;
struct tree_entry *current;
HANDLE d;
WIN32_FIND_DATAW _findData;
WIN32_FIND_DATAW *findData;
int flags;
int visit_type;
/* Error code from last failed operation. */
int tree_errno;
/* A full path with "\\?\" prefix. */
struct archive_wstring full_path;
size_t full_path_dir_length;
/* Dynamically-sized buffer for holding path */
struct archive_wstring path;
/* Last path element */
const wchar_t *basename;
/* Leading dir length */
size_t dirname_length;
int depth;
BY_HANDLE_FILE_INFORMATION lst;
BY_HANDLE_FILE_INFORMATION st;
int descend;
/* How to restore time of a file. */
struct restore_time restore_time;
struct entry_sparse {
int64_t length;
int64_t offset;
} *sparse_list, *current_sparse;
int sparse_count;
int sparse_list_size;
char initial_symlink_mode;
char symlink_mode;
struct filesystem *current_filesystem;
struct filesystem *filesystem_table;
int initial_filesystem_id;
int current_filesystem_id;
int max_filesystem_id;
int allocated_filesystem;
HANDLE entry_fh;
int entry_eof;
int64_t entry_remaining_bytes;
int64_t entry_total;
int ol_idx_doing;
int ol_idx_done;
int ol_num_doing;
int ol_num_done;
int64_t ol_remaining_bytes;
int64_t ol_total;
struct la_overlapped {
OVERLAPPED ol;
struct archive * _a;
unsigned char *buff;
size_t buff_size;
int64_t offset;
size_t bytes_expected;
size_t bytes_transferred;
} ol[MAX_OVERLAPPED];
int direct_io;
int async_io;
};
#define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
/* Treat FileIndex as i-node. We should remove a sequence number
* which is high-16-bits of nFileIndexHigh. */
#define bhfi_ino(bhfi) \
((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
+ (bhfi)->nFileIndexLow)
/* Definitions for tree.flags bitmap. */
#define hasStat 16 /* The st entry is valid. */
#define hasLstat 32 /* The lst entry is valid. */
#define needsRestoreTimes 128
static int
tree_dir_next_windows(struct tree *t, const wchar_t *pattern);
/* Initiate/terminate a tree traversal. */
static struct tree *tree_open(const wchar_t *, int, int);
static struct tree *tree_reopen(struct tree *, const wchar_t *, int);
static void tree_close(struct tree *);
static void tree_free(struct tree *);
static void tree_push(struct tree *, const wchar_t *, const wchar_t *,
int, int64_t, int64_t, struct restore_time *);
/*
* tree_next() returns Zero if there is no next entry, non-zero if
* there is. Note that directories are visited three times.
* Directories are always visited first as part of enumerating their
* parent; that is a "regular" visit. If tree_descend() is invoked at
* that time, the directory is added to a work list and will
* subsequently be visited two more times: once just after descending
* into the directory ("postdescent") and again just after ascending
* back to the parent ("postascent").
*
* TREE_ERROR_DIR is returned if the descent failed (because the
* directory couldn't be opened, for instance). This is returned
* instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
* fatal error, but it does imply that the relevant subtree won't be
* visited. TREE_ERROR_FATAL is returned for an error that left the
* traversal completely hosed. Right now, this is only returned for
* chdir() failures during ascent.
*/
#define TREE_REGULAR 1
#define TREE_POSTDESCENT 2
#define TREE_POSTASCENT 3
#define TREE_ERROR_DIR -1
#define TREE_ERROR_FATAL -2
static int tree_next(struct tree *);
/*
* Return information about the current entry.
*/
/*
* The current full pathname, length of the full pathname, and a name
* that can be used to access the file. Because tree does use chdir
* extensively, the access path is almost never the same as the full
* current path.
*
*/
static const wchar_t *tree_current_path(struct tree *);
static const wchar_t *tree_current_access_path(struct tree *);
/*
* Request the lstat() or stat() data for the current path. Since the
* tree package needs to do some of this anyway, and caches the
* results, you should take advantage of it here if you need it rather
* than make a redundant stat() or lstat() call of your own.
*/
static const BY_HANDLE_FILE_INFORMATION *tree_current_stat(struct tree *);
static const BY_HANDLE_FILE_INFORMATION *tree_current_lstat(struct tree *);
/* The following functions use tricks to avoid a certain number of
* stat()/lstat() calls. */
/* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
static int tree_current_is_physical_dir(struct tree *);
/* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
static int tree_current_is_physical_link(struct tree *);
/* Instead of archive_entry_copy_stat for BY_HANDLE_FILE_INFORMATION */
static void tree_archive_entry_copy_bhfi(struct archive_entry *,
struct tree *, const BY_HANDLE_FILE_INFORMATION *);
/* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
static int tree_current_is_dir(struct tree *);
static int update_current_filesystem(struct archive_read_disk *a,
int64_t dev);
static int setup_current_filesystem(struct archive_read_disk *);
static int tree_target_is_same_as_parent(struct tree *,
const BY_HANDLE_FILE_INFORMATION *);
static int _archive_read_disk_open_w(struct archive *, const wchar_t *);
static int _archive_read_free(struct archive *);
static int _archive_read_close(struct archive *);
static int _archive_read_data_block(struct archive *,
const void **, size_t *, int64_t *);
static int _archive_read_next_header(struct archive *,
struct archive_entry **);
static int _archive_read_next_header2(struct archive *,
struct archive_entry *);
static const char *trivial_lookup_gname(void *, int64_t gid);
static const char *trivial_lookup_uname(void *, int64_t uid);
static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
static int close_and_restore_time(HANDLE, struct tree *,
struct restore_time *);
static int setup_sparse_from_disk(struct archive_read_disk *,
struct archive_entry *, HANDLE);
static int la_linkname_from_handle(HANDLE, wchar_t **, int *);
static int la_linkname_from_pathw(const wchar_t *, wchar_t **, int *);
static void entry_symlink_from_pathw(struct archive_entry *,
const wchar_t *path);
typedef struct _REPARSE_DATA_BUFFER {
ULONG ReparseTag;
USHORT ReparseDataLength;
USHORT Reserved;
union {
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
ULONG Flags;
WCHAR PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
USHORT SubstituteNameOffset;
USHORT SubstituteNameLength;
USHORT PrintNameOffset;
USHORT PrintNameLength;
WCHAR PathBuffer[1];
} MountPointReparseBuffer;
struct {
UCHAR DataBuffer[1];
} GenericReparseBuffer;
} DUMMYUNIONNAME;
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
/*
* Reads the target of a symbolic link
*
* Returns 0 on success and -1 on failure
* outbuf is allocated in the function
*/
static int
la_linkname_from_handle(HANDLE h, wchar_t **linkname, int *linktype)
{
DWORD inbytes;
REPARSE_DATA_BUFFER *buf;
BY_HANDLE_FILE_INFORMATION st;
size_t len;
BOOL ret;
BYTE *indata;
wchar_t *tbuf;
ret = GetFileInformationByHandle(h, &st);
if (ret == 0 ||
(st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
return (-1);
}
indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
1024, &inbytes, NULL);
if (ret == 0) {
la_dosmaperr(GetLastError());
free(indata);
return (-1);
}
buf = (REPARSE_DATA_BUFFER *) indata;
if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
free(indata);
/* File is not a symbolic link */
errno = EINVAL;
return (-1);
}
len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
if (len <= 0) {
free(indata);
return (-1);
}
tbuf = malloc(len + 1 * sizeof(wchar_t));
if (tbuf == NULL) {
free(indata);
return (-1);
}
memcpy(tbuf, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
[buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
free(indata);
tbuf[len / sizeof(wchar_t)] = L'\0';
*linkname = tbuf;
/*
* Translate backslashes to slashes for libarchive internal use
*/
while(*tbuf != L'\0') {
if (*tbuf == L'\\')
*tbuf = L'/';
tbuf++;
}
if ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
*linktype = AE_SYMLINK_TYPE_FILE;
else
*linktype = AE_SYMLINK_TYPE_DIRECTORY;
return (0);
}
/*
* Returns AE_SYMLINK_TYPE_FILE, AE_SYMLINK_TYPE_DIRECTORY or -1 on error
*/
static int
la_linkname_from_pathw(const wchar_t *path, wchar_t **outbuf, int *linktype)
{
HANDLE h;
const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
FILE_FLAG_OPEN_REPARSE_POINT;
int ret;
h = CreateFileW(path, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, flag,
NULL);
if (h == INVALID_HANDLE_VALUE) {
la_dosmaperr(GetLastError());
return (-1);
}
ret = la_linkname_from_handle(h, outbuf, linktype);
CloseHandle(h);
return (ret);
}
static void
entry_symlink_from_pathw(struct archive_entry *entry, const wchar_t *path)
{
wchar_t *linkname = NULL;
int ret, linktype;
ret = la_linkname_from_pathw(path, &linkname, &linktype);
if (ret != 0)
return;
if (linktype >= 0) {
archive_entry_copy_symlink_w(entry, linkname);
archive_entry_set_symlink_type(entry, linktype);
}
free(linkname);
return;
}
static struct archive_vtable *
archive_read_disk_vtable(void)
{
static struct archive_vtable av;
static int inited = 0;
if (!inited) {
av.archive_free = _archive_read_free;
av.archive_close = _archive_read_close;
av.archive_read_data_block = _archive_read_data_block;
av.archive_read_next_header = _archive_read_next_header;
av.archive_read_next_header2 = _archive_read_next_header2;
inited = 1;
}
return (&av);
}
const char *
archive_read_disk_gname(struct archive *_a, la_int64_t gid)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
return (NULL);
if (a->lookup_gname == NULL)
return (NULL);
return ((*a->lookup_gname)(a->lookup_gname_data, gid));
}
const char *
archive_read_disk_uname(struct archive *_a, la_int64_t uid)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
return (NULL);
if (a->lookup_uname == NULL)
return (NULL);
return ((*a->lookup_uname)(a->lookup_uname_data, uid));
}
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))
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
(a->cleanup_gname)(a->lookup_gname_data);
a->lookup_gname = lookup_gname;
a->cleanup_gname = cleanup_gname;
a->lookup_gname_data = private_data;
return (ARCHIVE_OK);
}
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))
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
(a->cleanup_uname)(a->lookup_uname_data);
a->lookup_uname = lookup_uname;
a->cleanup_uname = cleanup_uname;
a->lookup_uname_data = private_data;
return (ARCHIVE_OK);
}
/*
* Create a new archive_read_disk object and initialize it with global state.
*/
struct archive *
archive_read_disk_new(void)
{
struct archive_read_disk *a;
a = (struct archive_read_disk *)calloc(1, sizeof(*a));
if (a == NULL)
return (NULL);
a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
a->archive.state = ARCHIVE_STATE_NEW;
a->archive.vtable = archive_read_disk_vtable();
a->entry = archive_entry_new2(&a->archive);
a->lookup_uname = trivial_lookup_uname;
a->lookup_gname = trivial_lookup_gname;
a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
return (&a->archive);
}
static int
_archive_read_free(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
int r;
if (_a == NULL)
return (ARCHIVE_OK);
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
if (a->archive.state != ARCHIVE_STATE_CLOSED)
r = _archive_read_close(&a->archive);
else
r = ARCHIVE_OK;
tree_free(a->tree);
if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
(a->cleanup_gname)(a->lookup_gname_data);
if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
(a->cleanup_uname)(a->lookup_uname_data);
archive_string_free(&a->archive.error_string);
archive_entry_free(a->entry);
a->archive.magic = 0;
free(a);
return (r);
}
static int
_archive_read_close(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
if (a->archive.state != ARCHIVE_STATE_FATAL)
a->archive.state = ARCHIVE_STATE_CLOSED;
tree_close(a->tree);
return (ARCHIVE_OK);
}
static void
setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
int follow_symlinks)
{
a->symlink_mode = symlink_mode;
a->follow_symlinks = follow_symlinks;
if (a->tree != NULL) {
a->tree->initial_symlink_mode = a->symlink_mode;
a->tree->symlink_mode = a->symlink_mode;
}
}
int
archive_read_disk_set_symlink_logical(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
setup_symlink_mode(a, 'L', 1);
return (ARCHIVE_OK);
}
int
archive_read_disk_set_symlink_physical(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
setup_symlink_mode(a, 'P', 0);
return (ARCHIVE_OK);
}
int
archive_read_disk_set_symlink_hybrid(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
return (ARCHIVE_OK);
}
int
archive_read_disk_set_atime_restored(struct archive *_a)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
if (a->tree != NULL)
a->tree->flags |= needsRestoreTimes;
return (ARCHIVE_OK);
}
int
archive_read_disk_set_behavior(struct archive *_a, int flags)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
int r = ARCHIVE_OK;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
a->flags = flags;
if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
r = archive_read_disk_set_atime_restored(_a);
else {
if (a->tree != NULL)
a->tree->flags &= ~needsRestoreTimes;
}
return (r);
}
/*
* Trivial implementations of gname/uname lookup functions.
* These are normally overridden by the client, but these stub
* versions ensure that we always have something that works.
*/
static const char *
trivial_lookup_gname(void *private_data, int64_t gid)
{
(void)private_data; /* UNUSED */
(void)gid; /* UNUSED */
return (NULL);
}
static const char *
trivial_lookup_uname(void *private_data, int64_t uid)
{
(void)private_data; /* UNUSED */
(void)uid; /* UNUSED */
return (NULL);
}
static int64_t
align_num_per_sector(struct tree *t, int64_t size)
{
int64_t surplus;
size += t->current_filesystem->bytesPerSector -1;
surplus = size % t->current_filesystem->bytesPerSector;
size -= surplus;
return (size);
}
static int
start_next_async_read(struct archive_read_disk *a, struct tree *t)
{
struct la_overlapped *olp;
DWORD buffbytes, rbytes;
if (t->ol_remaining_bytes == 0)
return (ARCHIVE_EOF);
olp = &(t->ol[t->ol_idx_doing]);
t->ol_idx_doing = (t->ol_idx_doing + 1) % MAX_OVERLAPPED;
/* Allocate read buffer. */
if (olp->buff == NULL) {
void *p;
size_t s = (size_t)align_num_per_sector(t, READ_BUFFER_SIZE);
p = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
if (p == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Couldn't allocate memory");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
olp->buff = p;
olp->buff_size = s;
olp->_a = &a->archive;
olp->ol.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (olp->ol.hEvent == NULL) {
la_dosmaperr(GetLastError());
archive_set_error(&a->archive, errno,
"CreateEvent failed");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
} else
ResetEvent(olp->ol.hEvent);
buffbytes = (DWORD)olp->buff_size;
if (buffbytes > t->current_sparse->length)
buffbytes = (DWORD)t->current_sparse->length;
/* Skip hole. */
if (t->current_sparse->offset > t->ol_total) {
t->ol_remaining_bytes -=
t->current_sparse->offset - t->ol_total;
}
olp->offset = t->current_sparse->offset;
olp->ol.Offset = (DWORD)(olp->offset & 0xffffffff);
olp->ol.OffsetHigh = (DWORD)(olp->offset >> 32);
if (t->ol_remaining_bytes > buffbytes) {
olp->bytes_expected = buffbytes;
t->ol_remaining_bytes -= buffbytes;
} else {
olp->bytes_expected = (size_t)t->ol_remaining_bytes;
t->ol_remaining_bytes = 0;
}
olp->bytes_transferred = 0;
t->current_sparse->offset += buffbytes;
t->current_sparse->length -= buffbytes;
t->ol_total = t->current_sparse->offset;
if (t->current_sparse->length == 0 && t->ol_remaining_bytes > 0)
t->current_sparse++;
if (!ReadFile(t->entry_fh, olp->buff, buffbytes, &rbytes, &(olp->ol))) {
DWORD lasterr;
lasterr = GetLastError();
if (lasterr == ERROR_HANDLE_EOF) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Reading file truncated");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
} else if (lasterr != ERROR_IO_PENDING) {
if (lasterr == ERROR_NO_DATA)
errno = EAGAIN;
else if (lasterr == ERROR_ACCESS_DENIED)
errno = EBADF;
else
la_dosmaperr(lasterr);
archive_set_error(&a->archive, errno, "Read error");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
} else
olp->bytes_transferred = rbytes;
t->ol_num_doing++;
return (t->ol_remaining_bytes == 0)? ARCHIVE_EOF: ARCHIVE_OK;
}
static void
cancel_async(struct tree *t)
{
if (t->ol_num_doing != t->ol_num_done) {
CancelIo(t->entry_fh);
t->ol_num_doing = t->ol_num_done = 0;
}
}
static int
_archive_read_data_block(struct archive *_a, const void **buff,
size_t *size, int64_t *offset)
{
struct archive_read_disk *a = (struct archive_read_disk *)_a;
struct tree *t = a->tree;
struct la_overlapped *olp;
DWORD bytes_transferred;
int r = ARCHIVE_FATAL;
archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
"archive_read_data_block");
if (t->entry_eof || t->entry_remaining_bytes <= 0) {
r = ARCHIVE_EOF;
goto abort_read_data;
}
/*
* Make a request to read the file in asynchronous.
*/
if (t->ol_num_doing == 0) {
do {
r = start_next_async_read(a, t);
if (r == ARCHIVE_FATAL)
goto abort_read_data;
if (!t->async_io)
break;
} while (r == ARCHIVE_OK && t->ol_num_doing < MAX_OVERLAPPED);
} else {
if ((r = start_next_async_read(a, t)) == ARCHIVE_FATAL)
goto abort_read_data;
}
olp = &(t->ol[t->ol_idx_done]);
t->ol_idx_done = (t->ol_idx_done + 1) % MAX_OVERLAPPED;
if (olp->bytes_transferred)
bytes_transferred = (DWORD)olp->bytes_transferred;
else if (!GetOverlappedResult(t->entry_fh, &(olp->ol),
&bytes_transferred, TRUE)) {
la_dosmaperr(GetLastError());
archive_set_error(&a->archive, errno,
"GetOverlappedResult failed");
a->archive.state = ARCHIVE_STATE_FATAL;
r = ARCHIVE_FATAL;
goto abort_read_data;
}
t->ol_num_done++;
if (bytes_transferred == 0 ||
olp->bytes_expected != bytes_transferred) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Reading file truncated");
a->archive.state = ARCHIVE_STATE_FATAL;
r = ARCHIVE_FATAL;
goto abort_read_data;
}
*buff = olp->buff;
*size = bytes_transferred;
*offset = olp->offset;
if (olp->offset > t->entry_total)
t->entry_remaining_bytes -= olp->offset - t->entry_total;
t->entry_total = olp->offset + *size;
t->entry_remaining_bytes -= *size;
if (t->entry_remaining_bytes == 0) {
/* Close the current file descriptor */
close_and_restore_time(t->entry_fh, t, &t->restore_time);
t->entry_fh = INVALID_HANDLE_VALUE;
t->entry_eof = 1;
}
return (ARCHIVE_OK);
abort_read_data:
*buff = NULL;
*size = 0;
*offset = t->entry_total;
if (t->entry_fh != INVALID_HANDLE_VALUE) {
cancel_async(t);
/* Close the current file descriptor */
close_and_restore_time(t->entry_fh, t, &t->restore_time);
t->entry_fh = INVALID_HANDLE_VALUE;
}
return (r);
}
static int
next_entry(struct archive_read_disk *a, struct tree *t,
struct archive_entry *entry)
{
const BY_HANDLE_FILE_INFORMATION *st;
const BY_HANDLE_FILE_INFORMATION *lst;
const char*name;
int descend, r;
st = NULL;
lst = NULL;
t->descend = 0;
do {
switch (tree_next(t)) {
case TREE_ERROR_FATAL:
archive_set_error(&a->archive, t->tree_errno,
"%ls: Unable to continue traversing directory tree",
tree_current_path(t));
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
case TREE_ERROR_DIR:
archive_set_error(&a->archive, t->tree_errno,
"%ls: Couldn't visit directory",
tree_current_path(t));
return (ARCHIVE_FAILED);
case 0:
return (ARCHIVE_EOF);
case TREE_POSTDESCENT:
case TREE_POSTASCENT:
break;
case TREE_REGULAR:
lst = tree_current_lstat(t);
if (lst == NULL) {
archive_set_error(&a->archive, t->tree_errno,
"%ls: Cannot stat",
tree_current_path(t));
return (ARCHIVE_FAILED);
}
break;
}
} while (lst == NULL);
archive_entry_copy_pathname_w(entry, tree_current_path(t));
/*
* Perform path matching.
*/
if (a->matching) {
r = archive_match_path_excluded(a->matching, entry);
if (r < 0) {
archive_set_error(&(a->archive), errno,
"Failed : %s", archive_error_string(a->matching));
return (r);
}
if (r) {
if (a->excluded_cb_func)
a->excluded_cb_func(&(a->archive),
a->excluded_cb_data, entry);
return (ARCHIVE_RETRY);
}
}
/*
* Distinguish 'L'/'P'/'H' symlink following.
*/
switch(t->symlink_mode) {
case 'H':
/* 'H': After the first item, rest like 'P'. */
t->symlink_mode = 'P';
/* 'H': First item (from command line) like 'L'. */
/* FALLTHROUGH */
case 'L':
/* 'L': Do descend through a symlink to dir. */
descend = tree_current_is_dir(t);
/* 'L': Follow symlinks to files. */
a->symlink_mode = 'L';
a->follow_symlinks = 1;
/* 'L': Archive symlinks as targets, if we can. */
st = tree_current_stat(t);
if (st != NULL && !tree_target_is_same_as_parent(t, st))
break;
/* If stat fails, we have a broken symlink;
* in that case, don't follow the link. */
/* FALLTHROUGH */
default:
/* 'P': Don't descend through a symlink to dir. */
descend = tree_current_is_physical_dir(t);
/* 'P': Don't follow symlinks to files. */
a->symlink_mode = 'P';
a->follow_symlinks = 0;
/* 'P': Archive symlinks as symlinks. */
st = lst;
break;
}
if (update_current_filesystem(a, bhfi_dev(st)) != ARCHIVE_OK) {
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
if (t->initial_filesystem_id == -1)
t->initial_filesystem_id = t->current_filesystem_id;
if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
if (t->initial_filesystem_id != t->current_filesystem_id)
return (ARCHIVE_RETRY);
}
t->descend = descend;
tree_archive_entry_copy_bhfi(entry, t, st);
/* Save the times to be restored. This must be in before
* calling archive_read_disk_descend() or any chance of it,
* especially, invoking a callback. */