/
archive_read_disk_posix.c
2759 lines (2526 loc) · 73.2 KB
/
archive_read_disk_posix.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.
*/
/* This is the tree-walking code for POSIX systems. */
#if !defined(_WIN32) || defined(__CYGWIN__)
#include "archive_platform.h"
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_LINUX_MAGIC_H
#include <linux/magic.h>
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
#elif HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
/*
* Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
* As the include guards don't agree, the order of include is important.
*/
#ifdef HAVE_LINUX_EXT2_FS_H
#include <linux/ext2_fs.h> /* for Linux file flags */
#endif
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include "archive.h"
#include "archive_string.h"
#include "archive_entry.h"
#include "archive_private.h"
#include "archive_read_disk_private.h"
#ifndef HAVE_FCHDIR
#error fchdir function required.
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#if defined(__hpux) && !defined(HAVE_DIRFD)
#define dirfd(x) ((x)->__dd_fd)
#define HAVE_DIRFD
#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).
*/
/*
* TODO:
* 1) Loop checking.
* 3) Arbitrary logical traversals by closing/reopening intermediate fds.
*/
struct restore_time {
const char *name;
time_t mtime;
long mtime_nsec;
time_t atime;
long atime_nsec;
mode_t filetype;
int noatime;
};
struct tree_entry {
int depth;
struct tree_entry *next;
struct tree_entry *parent;
struct archive_string name;
size_t dirname_length;
int64_t dev;
int64_t ino;
int flags;
int filesystem_id;
/* How to return back to the parent of a symlink. */
int symlink_parent_fd;
/* How to restore time of a directory. */
struct restore_time restore_time;
};
struct filesystem {
int64_t dev;
int synthetic;
int remote;
int noatime;
#if defined(USE_READDIR_R)
size_t name_max;
#endif
long incr_xfer_size;
long max_xfer_size;
long min_xfer_size;
long xfer_align;
/*
* Buffer used for reading file contents.
*/
/* Exactly allocated memory pointer. */
unsigned char *allocation_ptr;
/* Pointer adjusted to the filesystem alignment . */
unsigned char *buff;
size_t buff_size;
};
/* 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. */
/*
* Local data for this package.
*/
struct tree {
struct tree_entry *stack;
struct tree_entry *current;
DIR *d;
#define INVALID_DIR_HANDLE NULL
struct dirent *de;
#if defined(USE_READDIR_R)
struct dirent *dirent;
size_t dirent_allocated;
#endif
int flags;
int visit_type;
/* Error code from last failed operation. */
int tree_errno;
/* Dynamically-sized buffer for holding path */
struct archive_string path;
/* Last path element */
const char *basename;
/* Leading dir length */
size_t dirname_length;
int depth;
int openCount;
int maxOpenCount;
int initial_dir_fd;
int working_dir_fd;
struct stat lst;
struct stat st;
int descend;
int nlink;
/* 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;
int entry_fd;
int entry_eof;
int64_t entry_remaining_bytes;
int64_t entry_total;
unsigned char *entry_buff;
size_t entry_buff_size;
};
/* Definitions for tree.flags bitmap. */
#define hasStat 16 /* The st entry is valid. */
#define hasLstat 32 /* The lst entry is valid. */
#define onWorkingDir 64 /* We are on the working dir where we are
* reading directory entry at this time. */
#define needsRestoreTimes 128
#define onInitialDir 256 /* We are on the initial dir. */
static int
tree_dir_next_posix(struct tree *t);
#ifdef HAVE_DIRENT_D_NAMLEN
/* BSD extension; avoids need for a strlen() call. */
#define D_NAMELEN(dp) (dp)->d_namlen
#else
#define D_NAMELEN(dp) (strlen((dp)->d_name))
#endif
/* Initiate/terminate a tree traversal. */
static struct tree *tree_open(const char *, int, int);
static struct tree *tree_reopen(struct tree *, const char *, int);
static void tree_close(struct tree *);
static void tree_free(struct tree *);
static void tree_push(struct tree *, const char *, int, int64_t, int64_t,
struct restore_time *);
static int tree_enter_initial_dir(struct tree *);
static int tree_enter_working_dir(struct tree *);
static int tree_current_dir_fd(struct tree *);
/*
* 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.
*
* TODO: On platforms that support it, use openat()-style operations
* to eliminate the chdir() operations entirely while still supporting
* arbitrarily deep traversals. This makes access_path troublesome to
* support, of course, which means we'll need a rich enough interface
* that clients can function without it. (In particular, we'll need
* tree_current_open() that returns an open file descriptor.)
*
*/
static const char *tree_current_path(struct tree *);
static const char *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 struct stat *tree_current_stat(struct tree *);
static const struct stat *tree_current_lstat(struct tree *);
static int tree_current_is_symblic_link_target(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_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 struct stat *);
static int _archive_read_disk_open(struct archive *, const char *);
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(int fd, struct tree *,
struct restore_time *);
static int open_on_current_dir(struct tree *, const char *, int);
static int tree_dup(int);
static const struct archive_vtable
archive_read_disk_vtable = {
.archive_free = _archive_read_free,
.archive_close = _archive_read_close,
.archive_read_data_block = _archive_read_data_block,
.archive_read_next_header = _archive_read_next_header,
.archive_read_next_header2 = _archive_read_next_header2,
};
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, la_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;
a->open_on_current_dir = open_on_current_dir;
a->tree_current_dir_fd = tree_current_dir_fd;
a->tree_enter_working_dir = tree_enter_working_dir;
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;
__archive_clean(&a->archive);
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");
#ifdef HAVE_UTIMES
a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
if (a->tree != NULL)
a->tree->flags |= needsRestoreTimes;
return (ARCHIVE_OK);
#else
/* Display warning and unset flag */
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"Cannot restore access time on this system");
a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
return (ARCHIVE_WARN);
#endif
}
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);
}
/*
* Allocate memory for the reading buffer adjusted to the filesystem
* alignment.
*/
static int
setup_suitable_read_buffer(struct archive_read_disk *a)
{
struct tree *t = a->tree;
struct filesystem *cf = t->current_filesystem;
size_t asize;
size_t s;
if (cf->allocation_ptr == NULL) {
/* If we couldn't get a filesystem alignment,
* we use 4096 as default value but we won't use
* O_DIRECT to open() and openat() operations. */
long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
if (cf->max_xfer_size != -1)
asize = cf->max_xfer_size + xfer_align;
else {
long incr = cf->incr_xfer_size;
/* Some platform does not set a proper value to
* incr_xfer_size.*/
if (incr < 0)
incr = cf->min_xfer_size;
if (cf->min_xfer_size < 0) {
incr = xfer_align;
asize = xfer_align;
} else
asize = cf->min_xfer_size;
/* Increase a buffer size up to 64K bytes in
* a proper increment size. */
while (asize < 1024*64)
asize += incr;
/* Take a margin to adjust to the filesystem
* alignment. */
asize += xfer_align;
}
cf->allocation_ptr = malloc(asize);
if (cf->allocation_ptr == NULL) {
archive_set_error(&a->archive, ENOMEM,
"Couldn't allocate memory");
a->archive.state = ARCHIVE_STATE_FATAL;
return (ARCHIVE_FATAL);
}
/*
* Calculate proper address for the filesystem.
*/
s = (uintptr_t)cf->allocation_ptr;
s %= xfer_align;
if (s > 0)
s = xfer_align - s;
/*
* Set a read buffer pointer in the proper alignment of
* the current filesystem.
*/
cf->buff = cf->allocation_ptr + s;
cf->buff_size = asize - xfer_align;
}
return (ARCHIVE_OK);
}
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;
int r;
ssize_t bytes;
int64_t sparse_bytes;
size_t buffbytes;
int empty_sparse_region = 0;
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;
}
/*
* Open the current file.
*/
if (t->entry_fd < 0) {
int flags = O_RDONLY | O_BINARY | O_CLOEXEC;
/*
* Eliminate or reduce cache effects if we can.
*
* Carefully consider this to be enabled.
*/
#if defined(O_DIRECT) && 0/* Disabled for now */
if (t->current_filesystem->xfer_align != -1 &&
t->nlink == 1)
flags |= O_DIRECT;
#endif
#if defined(O_NOATIME)
/*
* Linux has O_NOATIME flag; use it if we need.
*/
if ((t->flags & needsRestoreTimes) != 0 &&
t->restore_time.noatime == 0)
flags |= O_NOATIME;
#endif
t->entry_fd = open_on_current_dir(t,
tree_current_access_path(t), flags);
__archive_ensure_cloexec_flag(t->entry_fd);
#if defined(O_NOATIME)
/*
* When we did open the file with O_NOATIME flag,
* if successful, set 1 to t->restore_time.noatime
* not to restore an atime of the file later.
* if failed by EPERM, retry it without O_NOATIME flag.
*/
if (flags & O_NOATIME) {
if (t->entry_fd >= 0)
t->restore_time.noatime = 1;
else if (errno == EPERM)
flags &= ~O_NOATIME;
}
#endif
if (t->entry_fd < 0) {
archive_set_error(&a->archive, errno,
"Couldn't open %s", tree_current_path(t));
r = ARCHIVE_FAILED;
tree_enter_initial_dir(t);
goto abort_read_data;
}
tree_enter_initial_dir(t);
}
/*
* Allocate read buffer if not allocated.
*/
if (t->current_filesystem->allocation_ptr == NULL) {
r = setup_suitable_read_buffer(a);
if (r != ARCHIVE_OK) {
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
}
t->entry_buff = t->current_filesystem->buff;
t->entry_buff_size = t->current_filesystem->buff_size;
buffbytes = t->entry_buff_size;
if ((int64_t)buffbytes > t->current_sparse->length)
buffbytes = t->current_sparse->length;
if (t->current_sparse->length == 0)
empty_sparse_region = 1;
/*
* Skip hole.
* TODO: Should we consider t->current_filesystem->xfer_align?
*/
if (t->current_sparse->offset > t->entry_total) {
if (lseek(t->entry_fd,
(off_t)t->current_sparse->offset, SEEK_SET) < 0) {
archive_set_error(&a->archive, errno, "Seek error");
r = ARCHIVE_FATAL;
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
sparse_bytes = t->current_sparse->offset - t->entry_total;
t->entry_remaining_bytes -= sparse_bytes;
t->entry_total += sparse_bytes;
}
/*
* Read file contents.
*/
if (buffbytes > 0) {
bytes = read(t->entry_fd, t->entry_buff, buffbytes);
if (bytes < 0) {
archive_set_error(&a->archive, errno, "Read error");
r = ARCHIVE_FATAL;
a->archive.state = ARCHIVE_STATE_FATAL;
goto abort_read_data;
}
} else
bytes = 0;
/*
* Return an EOF unless we've read a leading empty sparse region, which
* is used to represent fully-sparse files.
*/
if (bytes == 0 && !empty_sparse_region) {
/* Get EOF */
t->entry_eof = 1;
r = ARCHIVE_EOF;
goto abort_read_data;
}
*buff = t->entry_buff;
*size = bytes;
*offset = t->entry_total;
t->entry_total += bytes;
t->entry_remaining_bytes -= bytes;
if (t->entry_remaining_bytes == 0) {
/* Close the current file descriptor */
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
t->entry_eof = 1;
}
t->current_sparse->offset += bytes;
t->current_sparse->length -= bytes;
if (t->current_sparse->length == 0 && !t->entry_eof)
t->current_sparse++;
return (ARCHIVE_OK);
abort_read_data:
*buff = NULL;
*size = 0;
*offset = t->entry_total;
if (t->entry_fd >= 0) {
/* Close the current file descriptor */
close_and_restore_time(t->entry_fd, t, &t->restore_time);
t->entry_fd = -1;
}
return (r);
}
static int
next_entry(struct archive_read_disk *a, struct tree *t,
struct archive_entry *entry)
{
const struct stat *st; /* info to use for this entry */
const struct stat *lst;/* lstat() information */
const char *name;
int delayed, delayed_errno, descend, r;
struct archive_string delayed_str;
delayed = ARCHIVE_OK;
delayed_errno = 0;
archive_string_init(&delayed_str);
st = NULL;
lst = NULL;
t->descend = 0;
do {
switch (tree_next(t)) {
case TREE_ERROR_FATAL:
archive_set_error(&a->archive, t->tree_errno,
"%s: Unable to continue traversing directory tree",
tree_current_path(t));
a->archive.state = ARCHIVE_STATE_FATAL;
tree_enter_initial_dir(t);
return (ARCHIVE_FATAL);
case TREE_ERROR_DIR:
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"%s: Couldn't visit directory",
tree_current_path(t));
tree_enter_initial_dir(t);
return (ARCHIVE_FAILED);
case 0:
tree_enter_initial_dir(t);
return (ARCHIVE_EOF);
case TREE_POSTDESCENT:
case TREE_POSTASCENT:
break;
case TREE_REGULAR:
lst = tree_current_lstat(t);
if (lst == NULL) {
if (errno == ENOENT && t->depth > 0) {
delayed = ARCHIVE_WARN;
delayed_errno = errno;
if (delayed_str.length == 0) {
archive_string_sprintf(&delayed_str,
"%s", tree_current_path(t));
} else {
archive_string_sprintf(&delayed_str,
" %s", tree_current_path(t));
}
} else {
archive_set_error(&a->archive, errno,
"%s: Cannot stat",
tree_current_path(t));
tree_enter_initial_dir(t);
return (ARCHIVE_FAILED);
}
}
break;
}
} while (lst == NULL);
#ifdef __APPLE__
if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
/* If we're using copyfile(), ignore "._XXX" files. */
const char *bname = strrchr(tree_current_path(t), '/');
if (bname == NULL)
bname = tree_current_path(t);
else
++bname;
if (bname[0] == '.' && bname[1] == '_')
return (ARCHIVE_RETRY);
}
#endif
archive_entry_copy_pathname(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, st->st_dev) != ARCHIVE_OK) {
a->archive.state = ARCHIVE_STATE_FATAL;
tree_enter_initial_dir(t);
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)
descend = 0;
}
t->descend = descend;
/*
* Honor nodump flag.
* If the file is marked with nodump flag, do not return this entry.
*/
if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
#if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
if (st->st_flags & UF_NODUMP)
return (ARCHIVE_RETRY);
#elif (defined(FS_IOC_GETFLAGS) && defined(FS_NODUMP_FL) && \
defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
(defined(EXT2_IOC_GETFLAGS) && defined(EXT2_NODUMP_FL) && \
defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
int stflags;
t->entry_fd = open_on_current_dir(t,