Lines Matching +full:te +full:- +full:source

1 /*-
2 * Copyright (c) 2003-2009 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
9 * 1. Redistributions of source code must retain the above copyright
28 /* This is the tree-walking code for POSIX systems. */
111 #define dirfd(x) ((x)->__dd_fd)
115 /*-
116 * This is a new directory-walking system that addresses a number
118 * pathname-length limits (other than the size of 'int'), handles
124 * Non-directories are not kept in memory: they are pulled from
215 /* Dynamically-sized buffer for holding path */
273 #define D_NAMELEN(dp) (dp)->d_namlen
275 #define D_NAMELEN(dp) (strlen((dp)->d_name))
290 * tree_next() returns Zero if there is no next entry, non-zero if
310 #define TREE_ERROR_DIR -1
311 #define TREE_ERROR_FATAL -2
325 * TODO: On platforms that support it, use openat()-style operations
348 /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
350 /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
391 if (a->lookup_gname == NULL)
393 return ((*a->lookup_gname)(a->lookup_gname_data, gid));
403 if (a->lookup_uname == NULL)
405 return ((*a->lookup_uname)(a->lookup_uname_data, uid));
415 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
418 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
419 (a->cleanup_gname)(a->lookup_gname_data);
421 a->lookup_gname = lookup_gname;
422 a->cleanup_gname = cleanup_gname;
423 a->lookup_gname_data = private_data;
434 archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
437 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
438 (a->cleanup_uname)(a->lookup_uname_data);
440 a->lookup_uname = lookup_uname;
441 a->cleanup_uname = cleanup_uname;
442 a->lookup_uname_data = private_data;
457 a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
458 a->archive.state = ARCHIVE_STATE_NEW;
459 a->archive.vtable = &archive_read_disk_vtable;
460 a->entry = archive_entry_new2(&a->archive);
461 a->lookup_uname = trivial_lookup_uname;
462 a->lookup_gname = trivial_lookup_gname;
463 a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
464 a->open_on_current_dir = open_on_current_dir;
465 a->tree_current_dir_fd = tree_current_dir_fd;
466 a->tree_enter_working_dir = tree_enter_working_dir;
467 return (&a->archive);
481 if (a->archive.state != ARCHIVE_STATE_CLOSED)
482 r = _archive_read_close(&a->archive);
486 tree_free(a->tree);
487 if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
488 (a->cleanup_gname)(a->lookup_gname_data);
489 if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
490 (a->cleanup_uname)(a->lookup_uname_data);
491 archive_string_free(&a->archive.error_string);
492 archive_entry_free(a->entry);
493 a->archive.magic = 0;
494 __archive_clean(&a->archive);
507 if (a->archive.state != ARCHIVE_STATE_FATAL)
508 a->archive.state = ARCHIVE_STATE_CLOSED;
510 tree_close(a->tree);
519 a->symlink_mode = symlink_mode;
520 a->follow_symlinks = follow_symlinks;
521 if (a->tree != NULL) {
522 a->tree->initial_symlink_mode = a->symlink_mode;
523 a->tree->symlink_mode = a->symlink_mode;
564 a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
565 if (a->tree != NULL)
566 a->tree->flags |= needsRestoreTimes;
570 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
572 a->flags &= ~ARCHIVE_READDISK_RESTORE_ATIME;
586 a->flags = flags;
591 if (a->tree != NULL)
592 a->tree->flags &= ~needsRestoreTimes;
625 struct tree *t = a->tree;
626 struct filesystem *cf = t->current_filesystem;
630 if (cf->allocation_ptr == NULL) {
634 long xfer_align = (cf->xfer_align == -1)?4096:cf->xfer_align;
636 if (cf->max_xfer_size != -1)
637 asize = cf->max_xfer_size + xfer_align;
639 long incr = cf->incr_xfer_size;
643 incr = cf->min_xfer_size;
644 if (cf->min_xfer_size < 0) {
648 asize = cf->min_xfer_size;
658 cf->allocation_ptr = malloc(asize);
659 if (cf->allocation_ptr == NULL) {
660 archive_set_error(&a->archive, ENOMEM,
662 a->archive.state = ARCHIVE_STATE_FATAL;
669 s = (uintptr_t)cf->allocation_ptr;
672 s = xfer_align - s;
678 cf->buff = cf->allocation_ptr + s;
679 cf->buff_size = asize - xfer_align;
689 struct tree *t = a->tree;
699 if (t->entry_eof || t->entry_remaining_bytes <= 0) {
707 if (t->entry_fd < 0) {
716 if (t->current_filesystem->xfer_align != -1 &&
717 t->nlink == 1)
724 if ((t->flags & needsRestoreTimes) != 0 &&
725 t->restore_time.noatime == 0)
728 t->entry_fd = open_on_current_dir(t,
730 __archive_ensure_cloexec_flag(t->entry_fd);
734 * if successful, set 1 to t->restore_time.noatime
739 if (t->entry_fd >= 0)
740 t->restore_time.noatime = 1;
745 if (t->entry_fd < 0) {
746 archive_set_error(&a->archive, errno,
758 if (t->current_filesystem->allocation_ptr == NULL) {
761 a->archive.state = ARCHIVE_STATE_FATAL;
765 t->entry_buff = t->current_filesystem->buff;
766 t->entry_buff_size = t->current_filesystem->buff_size;
768 buffbytes = t->entry_buff_size;
769 if ((int64_t)buffbytes > t->current_sparse->length)
770 buffbytes = t->current_sparse->length;
772 if (t->current_sparse->length == 0)
777 * TODO: Should we consider t->current_filesystem->xfer_align?
779 if (t->current_sparse->offset > t->entry_total) {
780 if (lseek(t->entry_fd,
781 (off_t)t->current_sparse->offset, SEEK_SET) < 0) {
782 archive_set_error(&a->archive, errno, "Seek error");
784 a->archive.state = ARCHIVE_STATE_FATAL;
787 sparse_bytes = t->current_sparse->offset - t->entry_total;
788 t->entry_remaining_bytes -= sparse_bytes;
789 t->entry_total += sparse_bytes;
796 bytes = read(t->entry_fd, t->entry_buff, buffbytes);
798 archive_set_error(&a->archive, errno, "Read error");
800 a->archive.state = ARCHIVE_STATE_FATAL;
807 * is used to represent fully-sparse files.
811 t->entry_eof = 1;
815 *buff = t->entry_buff;
817 *offset = t->entry_total;
818 t->entry_total += bytes;
819 t->entry_remaining_bytes -= bytes;
820 if (t->entry_remaining_bytes == 0) {
822 close_and_restore_time(t->entry_fd, t, &t->restore_time);
823 t->entry_fd = -1;
824 t->entry_eof = 1;
826 t->current_sparse->offset += bytes;
827 t->current_sparse->length -= bytes;
828 if (t->current_sparse->length == 0 && !t->entry_eof)
829 t->current_sparse++;
835 *offset = t->entry_total;
836 if (t->entry_fd >= 0) {
838 close_and_restore_time(t->entry_fd, t, &t->restore_time);
839 t->entry_fd = -1;
860 t->descend = 0;
864 archive_set_error(&a->archive, t->tree_errno,
867 a->archive.state = ARCHIVE_STATE_FATAL;
871 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
885 if (errno == ENOENT && t->depth > 0) {
896 archive_set_error(&a->archive, errno,
908 if (a->flags & ARCHIVE_READDISK_MAC_COPYFILE) {
924 if (a->matching) {
925 r = archive_match_path_excluded(a->matching, entry);
927 archive_set_error(&(a->archive), errno,
928 "Failed : %s", archive_error_string(a->matching));
932 if (a->excluded_cb_func)
933 a->excluded_cb_func(&(a->archive),
934 a->excluded_cb_data, entry);
942 switch(t->symlink_mode) {
945 t->symlink_mode = 'P';
952 a->symlink_mode = 'L';
953 a->follow_symlinks = 1;
965 a->symlink_mode = 'P';
966 a->follow_symlinks = 0;
972 if (update_current_filesystem(a, st->st_dev) != ARCHIVE_OK) {
973 a->archive.state = ARCHIVE_STATE_FATAL;
977 if (t->initial_filesystem_id == -1)
978 t->initial_filesystem_id = t->current_filesystem_id;
979 if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
980 if (t->initial_filesystem_id != t->current_filesystem_id)
983 t->descend = descend;
989 if (a->flags & ARCHIVE_READDISK_HONOR_NODUMP) {
991 if (st->st_flags & UF_NODUMP)
997 if (S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) {
1000 t->entry_fd = open_on_current_dir(t,
1003 __archive_ensure_cloexec_flag(t->entry_fd);
1004 if (t->entry_fd >= 0) {
1005 r = ioctl(t->entry_fd,
1028 t->restore_time.mtime = archive_entry_mtime(entry);
1029 t->restore_time.mtime_nsec = archive_entry_mtime_nsec(entry);
1030 t->restore_time.atime = archive_entry_atime(entry);
1031 t->restore_time.atime_nsec = archive_entry_atime_nsec(entry);
1032 t->restore_time.filetype = archive_entry_filetype(entry);
1033 t->restore_time.noatime = t->current_filesystem->noatime;
1038 if (a->matching) {
1039 r = archive_match_time_excluded(a->matching, entry);
1041 archive_set_error(&(a->archive), errno,
1042 "Failed : %s", archive_error_string(a->matching));
1046 if (a->excluded_cb_func)
1047 a->excluded_cb_func(&(a->archive),
1048 a->excluded_cb_data, entry);
1054 name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1057 name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1064 if (a->matching) {
1065 r = archive_match_owner_excluded(a->matching, entry);
1067 archive_set_error(&(a->archive), errno,
1068 "Failed : %s", archive_error_string(a->matching));
1072 if (a->excluded_cb_func)
1073 a->excluded_cb_func(&(a->archive),
1074 a->excluded_cb_data, entry);
1082 if (a->metadata_filter_func) {
1083 if (!a->metadata_filter_func(&(a->archive),
1084 a->metadata_filter_data, entry))
1092 r = archive_read_disk_entry_from_file(&(a->archive), entry,
1093 t->entry_fd, st);
1100 archive_set_error(&(a->archive), delayed_errno,
1115 ret = _archive_read_next_header2(_a, a->entry);
1116 *entryp = a->entry;
1131 t = a->tree;
1132 if (t->entry_fd >= 0) {
1133 close_and_restore_time(t->entry_fd, t, &t->restore_time);
1134 t->entry_fd = -1;
1141 if (t->entry_fd >= 0) {
1142 close(t->entry_fd);
1143 t->entry_fd = -1;
1163 a->archive.state = ARCHIVE_STATE_EOF;
1169 t->entry_total = 0;
1171 t->nlink = archive_entry_nlink(entry);
1172 t->entry_remaining_bytes = archive_entry_size(entry);
1173 t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1174 if (!t->entry_eof &&
1178 t->entry_remaining_bytes = 0;
1179 t->entry_eof = 1;
1181 a->archive.state = ARCHIVE_STATE_DATA;
1186 a->archive.state = ARCHIVE_STATE_FATAL;
1190 __archive_reset_read_data(&a->archive);
1197 struct tree *t = a->tree;
1201 t->sparse_count = archive_entry_sparse_reset(entry);
1202 if (t->sparse_count+1 > t->sparse_list_size) {
1203 free(t->sparse_list);
1204 t->sparse_list_size = t->sparse_count + 1;
1205 t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1206 t->sparse_list_size);
1207 if (t->sparse_list == NULL) {
1208 t->sparse_list_size = 0;
1209 archive_set_error(&a->archive, ENOMEM,
1211 a->archive.state = ARCHIVE_STATE_FATAL;
1215 for (i = 0; i < t->sparse_count; i++) {
1217 t->sparse_list[i].offset = offset;
1218 t->sparse_list[i].length = length;
1221 t->sparse_list[i].offset = 0;
1222 t->sparse_list[i].length = archive_entry_size(entry);
1224 t->sparse_list[i].offset = archive_entry_size(entry);
1225 t->sparse_list[i].length = 0;
1227 t->current_sparse = t->sparse_list;
1240 a->matching = _ma;
1241 a->excluded_cb_func = _excluded_func;
1242 a->excluded_cb_data = _client_data;
1256 a->metadata_filter_func = _metadata_filter_func;
1257 a->metadata_filter_data = _client_data;
1265 struct tree *t = a->tree;
1271 return (t->visit_type == TREE_REGULAR && t->descend);
1282 struct tree *t = a->tree;
1297 * top level of a traverse.) But we can't necessarily assume t->st is
1298 * valid here (though t->lst is), which complicates the logic a
1302 tree_push(t, t->basename, t->current_filesystem_id,
1303 t->lst.st_dev, t->lst.st_ino, &t->restore_time);
1304 if (t->stack->parent->parent != NULL)
1305 t->stack->flags |= isDir;
1307 t->stack->flags |= isDirLink;
1309 tree_push(t, t->basename, t->current_filesystem_id,
1310 t->st.st_dev, t->st.st_ino, &t->restore_time);
1311 t->stack->flags |= isDirLink;
1313 t->descend = 0;
1325 archive_clear_error(&a->archive);
1340 archive_clear_error(&a->archive);
1347 archive_set_error(&a->archive, ENOMEM,
1350 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1352 a->archive.state = ARCHIVE_STATE_FATAL;
1366 if (a->tree != NULL)
1367 a->tree = tree_reopen(a->tree, pathname,
1368 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1370 a->tree = tree_open(pathname, a->symlink_mode,
1371 a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1372 if (a->tree == NULL) {
1373 archive_set_error(&a->archive, ENOMEM,
1375 a->archive.state = ARCHIVE_STATE_FATAL;
1378 a->archive.state = ARCHIVE_STATE_HEADER;
1395 return (a->tree->current_filesystem_id);
1401 struct tree *t = a->tree;
1404 if (t->current_filesystem != NULL &&
1405 t->current_filesystem->dev == dev)
1408 for (i = 0; i < t->max_filesystem_id; i++) {
1409 if (t->filesystem_table[i].dev == dev) {
1411 t->current_filesystem_id = i;
1412 t->current_filesystem = &(t->filesystem_table[i]);
1420 fid = t->max_filesystem_id++;
1421 if (t->max_filesystem_id > t->allocated_filesystem) {
1425 s = t->max_filesystem_id * 2;
1426 p = realloc(t->filesystem_table,
1427 s * sizeof(*t->filesystem_table));
1429 archive_set_error(&a->archive, ENOMEM,
1433 t->filesystem_table = (struct filesystem *)p;
1434 t->allocated_filesystem = s;
1436 t->current_filesystem_id = fid;
1437 t->current_filesystem = &(t->filesystem_table[fid]);
1438 t->current_filesystem->dev = dev;
1439 t->current_filesystem->allocation_ptr = NULL;
1440 t->current_filesystem->buff = NULL;
1449 * or -1 if it is unknown.
1459 return (a->tree->current_filesystem->synthetic);
1464 * or -1 if it is unknown.
1474 return (a->tree->current_filesystem->remote);
1482 t->current_filesystem->xfer_align = -1;
1485 t->current_filesystem->incr_xfer_size =
1487 t->current_filesystem->max_xfer_size =
1489 t->current_filesystem->min_xfer_size =
1491 t->current_filesystem->xfer_align =
1494 t->current_filesystem->incr_xfer_size =
1496 t->current_filesystem->max_xfer_size =
1498 t->current_filesystem->min_xfer_size =
1500 t->current_filesystem->xfer_align =
1504 if (t->current_filesystem->xfer_align == -1)
1505 return ((errno == EINVAL)?1:-1);
1524 fs->xfer_align = sfs->f_frsize > 0 ? (long)sfs->f_frsize : -1;
1525 fs->max_xfer_size = -1;
1527 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1528 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1530 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1531 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1540 fs->xfer_align = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1541 fs->max_xfer_size = -1;
1543 fs->min_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1544 fs->incr_xfer_size = sfs->f_iosize > 0 ? (long)sfs->f_iosize : -1;
1546 fs->min_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1547 fs->incr_xfer_size = sfs->f_bsize > 0 ? (long)sfs->f_bsize : -1;
1561 struct tree *t = a->tree;
1584 t->current_filesystem->synthetic = -1;
1585 t->current_filesystem->remote = -1;
1596 archive_set_error(&a->archive, errno,
1606 archive_set_error(&a->archive, errno, "fchdir failed");
1611 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1618 if (r == -1 || xr == -1) {
1619 archive_set_error(&a->archive, errno, "statfs failed");
1623 set_statfs_transfer_size(t->current_filesystem, &sfs);
1626 t->current_filesystem->remote = 0;
1628 t->current_filesystem->remote = 1;
1632 if (r == -1) {
1633 archive_set_error(&a->archive, errno, "getvfsbyname failed");
1637 t->current_filesystem->synthetic = 1;
1639 t->current_filesystem->synthetic = 0;
1644 t->current_filesystem->noatime = 1;
1647 t->current_filesystem->noatime = 0;
1652 t->current_filesystem->name_max = sfs.f_namemax;
1658 archive_set_error(&a->archive, errno, "fchdir failed");
1665 nm = -1;
1667 if (nm == -1)
1668 t->current_filesystem->name_max = NAME_MAX;
1670 t->current_filesystem->name_max = nm;
1672 if (t->current_filesystem->name_max == 0) {
1673 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1689 struct tree *t = a->tree;
1693 t->current_filesystem->synthetic = -1;
1695 archive_set_error(&a->archive, errno, "fchdir failed");
1701 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1710 xr = get_xfer_size(t, -1, ".");
1713 if (r == -1 || xr == -1) {
1714 t->current_filesystem->remote = -1;
1715 archive_set_error(&a->archive, errno, "statvfs failed");
1720 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1723 t->current_filesystem->remote = 0;
1725 t->current_filesystem->remote = 1;
1729 t->current_filesystem->noatime = 1;
1732 t->current_filesystem->noatime = 0;
1735 t->current_filesystem->name_max = svfs.f_namemax;
1758 struct tree *t = a->tree;
1775 archive_set_error(&a->archive, errno,
1788 archive_set_error(&a->archive, errno, "fchdir failed");
1796 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1808 archive_set_error(&a->archive, errno, "fchdir failed");
1816 xr = get_xfer_size(t, -1, ".");
1819 if (r == -1 || xr == -1 || vr == -1) {
1820 t->current_filesystem->synthetic = -1;
1821 t->current_filesystem->remote = -1;
1822 archive_set_error(&a->archive, errno, "statfs failed");
1827 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1829 set_statfs_transfer_size(t->current_filesystem, &sfs);
1839 t->current_filesystem->remote = 1;
1840 t->current_filesystem->synthetic = 0;
1845 t->current_filesystem->remote = 0;
1846 t->current_filesystem->synthetic = 1;
1849 t->current_filesystem->remote = 0;
1850 t->current_filesystem->synthetic = 0;
1860 t->current_filesystem->noatime = 1;
1863 t->current_filesystem->noatime = 0;
1868 t->current_filesystem->name_max = svfs.f_namemax;
1870 t->current_filesystem->name_max = sfs.f_namelen;
1872 if (t->current_filesystem->name_max == 0) {
1873 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1890 struct tree *t = a->tree;
1894 t->current_filesystem->synthetic = -1;/* Not supported */
1895 t->current_filesystem->remote = -1;/* Not supported */
1906 archive_set_error(&a->archive, errno,
1916 archive_set_error(&a->archive, errno, "fchdir failed");
1921 xr = get_xfer_size(t, -1, tree_current_access_path(t));
1930 archive_set_error(&a->archive, errno, "fchdir failed");
1935 xr = get_xfer_size(t, -1, ".");
1938 if (r == -1 || xr == -1) {
1939 t->current_filesystem->synthetic = -1;
1940 t->current_filesystem->remote = -1;
1941 archive_set_error(&a->archive, errno, "statvfs failed");
1945 set_statvfs_transfer_size(t->current_filesystem, &svfs);
1950 t->current_filesystem->noatime = 1;
1953 t->current_filesystem->noatime = 0;
1957 t->current_filesystem->name_max = svfs.f_namemax;
1958 if (t->current_filesystem->name_max == 0) {
1959 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1976 struct tree *t = a->tree;
1980 t->current_filesystem->synthetic = -1;/* Not supported */
1981 t->current_filesystem->remote = -1;/* Not supported */
1982 t->current_filesystem->noatime = 0;
1983 (void)get_xfer_size(t, -1, ".");/* Dummy call to avoid build error. */
1984 t->current_filesystem->xfer_align = -1;/* Unknown */
1985 t->current_filesystem->max_xfer_size = -1;
1986 t->current_filesystem->min_xfer_size = -1;
1987 t->current_filesystem->incr_xfer_size = -1;
1994 archive_set_error(&a->archive, errno, "fchdir failed");
2000 if (nm == -1)
2003 * Some systems (HP-UX or others?) incorrectly defined
2007 t->current_filesystem->name_max = NAME_MAX;
2011 t->current_filesystem->name_max = PATH_MAX;
2015 t->current_filesystem->name_max = nm;
2017 if (t->current_filesystem->name_max == 0) {
2018 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2041 if ((t->flags & needsRestoreTimes) == 0 || rt->noatime) {
2049 timespecs[1].tv_sec = rt->mtime;
2050 timespecs[1].tv_nsec = rt->mtime_nsec;
2052 timespecs[0].tv_sec = rt->atime;
2053 timespecs[0].tv_nsec = rt->atime_nsec;
2054 /* futimens() is defined in POSIX.1-2008. */
2059 times[1].tv_sec = rt->mtime;
2060 times[1].tv_usec = rt->mtime_nsec / 1000;
2062 times[0].tv_sec = rt->atime;
2063 times[0].tv_usec = rt->atime_nsec / 1000;
2071 if (futimesat(tree_current_dir_fd(t), rt->name, times) == 0)
2075 if (lutimes(rt->name, times) != 0)
2077 if (AE_IFLNK != rt->filetype && utimes(rt->name, times) != 0)
2079 return (-1);
2091 return (-1);
2105 if (new_fd != -1)
2107 /* Linux 2.6.18 - 2.6.23 declare F_DUPFD_CLOEXEC,
2125 struct tree_entry *te;
2127 te = calloc(1, sizeof(*te));
2128 if (te == NULL)
2130 te->next = t->stack;
2131 te->parent = t->current;
2132 if (te->parent)
2133 te->depth = te->parent->depth + 1;
2134 t->stack = te;
2135 archive_string_init(&te->name);
2136 te->symlink_parent_fd = -1;
2137 archive_strcpy(&te->name, path);
2138 te->flags = needsDescent | needsOpen | needsAscent;
2139 te->filesystem_id = filesystem_id;
2140 te->dev = dev;
2141 te->ino = ino;
2142 te->dirname_length = t->dirname_length;
2143 te->restore_time.name = te->name.s;
2145 te->restore_time.mtime = rt->mtime;
2146 te->restore_time.mtime_nsec = rt->mtime_nsec;
2147 te->restore_time.atime = rt->atime;
2148 te->restore_time.atime_nsec = rt->atime_nsec;
2149 te->restore_time.filetype = rt->filetype;
2150 te->restore_time.noatime = rt->noatime;
2162 t->path.s[t->dirname_length] = '\0';
2163 t->path.length = t->dirname_length;
2165 while (name_length > 1 && name[name_length - 1] == '/')
2166 name_length--;
2169 size_needed = name_length + t->dirname_length + 2;
2170 archive_string_ensure(&t->path, size_needed);
2172 if (t->dirname_length > 0 && t->path.s[archive_strlen(&t->path)-1] != '/')
2173 archive_strappend_char(&t->path, '/');
2174 t->basename = t->path.s + archive_strlen(&t->path);
2175 archive_strncat(&t->path, name, name_length);
2176 t->restore_time.name = t->basename;
2189 archive_string_init(&t->path);
2190 archive_string_ensure(&t->path, 31);
2191 t->initial_symlink_mode = symlink_mode;
2209 t->flags = (restore_time != 0)?needsRestoreTimes:0;
2210 t->flags |= onInitialDir;
2211 t->visit_type = 0;
2212 t->tree_errno = 0;
2213 t->dirname_length = 0;
2214 t->depth = 0;
2215 t->descend = 0;
2216 t->current = NULL;
2217 t->d = INVALID_DIR_HANDLE;
2218 t->symlink_mode = t->initial_symlink_mode;
2219 archive_string_empty(&t->path);
2220 t->entry_fd = -1;
2221 t->entry_eof = 0;
2222 t->entry_remaining_bytes = 0;
2223 t->initial_filesystem_id = -1;
2227 t->stack->flags = needsFirstVisit;
2228 t->maxOpenCount = t->openCount = 1;
2229 t->initial_dir_fd = open(".", O_RDONLY | O_CLOEXEC);
2237 if (t->initial_dir_fd < 0)
2238 t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
2240 __archive_ensure_cloexec_flag(t->initial_dir_fd);
2241 t->working_dir_fd = tree_dup(t->initial_dir_fd);
2250 t->dirname_length = archive_strlen(&t->path);
2255 new_fd = open_on_current_dir(t, t->stack->name.s, flag);
2258 t->tree_errno = errno;
2261 t->depth++;
2263 if (t->stack->flags & isDirLink) {
2264 t->stack->symlink_parent_fd = t->working_dir_fd;
2265 t->openCount++;
2266 if (t->openCount > t->maxOpenCount)
2267 t->maxOpenCount = t->openCount;
2269 close(t->working_dir_fd);
2271 t->working_dir_fd = new_fd;
2272 t->flags &= ~onWorkingDir;
2283 struct tree_entry *te;
2286 te = t->stack;
2287 prev_dir_fd = t->working_dir_fd;
2288 if (te->flags & isDirLink)
2289 new_fd = te->symlink_parent_fd;
2295 t->tree_errno = errno;
2299 t->working_dir_fd = new_fd;
2300 t->flags &= ~onWorkingDir;
2303 close_and_restore_time(prev_dir_fd, t, &te->restore_time);
2304 if (te->flags & isDirLink) {
2305 t->openCount--;
2306 te->symlink_parent_fd = -1;
2308 t->depth--;
2321 if ((t->flags & onInitialDir) == 0) {
2322 r = fchdir(t->initial_dir_fd);
2324 t->flags &= ~onWorkingDir;
2325 t->flags |= onInitialDir;
2344 if (t->depth > 0 && (t->flags & onWorkingDir) == 0) {
2345 r = fchdir(t->working_dir_fd);
2347 t->flags &= ~onInitialDir;
2348 t->flags |= onWorkingDir;
2357 return (t->working_dir_fd);
2366 struct tree_entry *te;
2368 t->path.s[t->dirname_length] = '\0';
2369 t->path.length = t->dirname_length;
2370 if (t->stack == t->current && t->current != NULL)
2371 t->current = t->current->parent;
2372 te = t->stack;
2373 t->stack = te->next;
2374 t->dirname_length = te->dirname_length;
2375 t->basename = t->path.s + t->dirname_length;
2376 while (t->basename[0] == '/')
2377 t->basename++;
2378 archive_string_free(&te->name);
2379 free(te);
2390 while (t->stack != NULL) {
2392 if (t->d != INVALID_DIR_HANDLE) {
2399 if (t->stack->flags & needsFirstVisit) {
2401 t->current = t->stack;
2402 tree_append(t, t->stack->name.s,
2403 archive_strlen(&(t->stack->name)));
2404 /* t->dirname_length = t->path_length; */
2406 t->stack->flags &= ~needsFirstVisit;
2407 return (t->visit_type = TREE_REGULAR);
2408 } else if (t->stack->flags & needsDescent) {
2410 t->current = t->stack;
2411 tree_append(t, t->stack->name.s,
2412 archive_strlen(&(t->stack->name)));
2413 t->stack->flags &= ~needsDescent;
2417 t->visit_type = r;
2419 t->visit_type = TREE_POSTDESCENT;
2420 return (t->visit_type);
2421 } else if (t->stack->flags & needsOpen) {
2422 t->stack->flags &= ~needsOpen;
2427 } else if (t->stack->flags & needsAscent) {
2431 t->visit_type = r != 0 ? r : TREE_POSTASCENT;
2432 return (t->visit_type);
2436 t->flags &= ~hasLstat;
2437 t->flags &= ~hasStat;
2440 return (t->visit_type = 0);
2450 if (t->d == NULL) {
2456 t->d = fdopendir(tree_dup(t->working_dir_fd));
2459 t->d = opendir(".");
2461 __archive_ensure_cloexec_flag(dirfd(t->d));
2465 if (t->d == NULL) {
2468 t->tree_errno = errno;
2469 t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2470 return (t->visit_type);
2474 t->filesystem_table[t->current->filesystem_id].name_max + 1;
2475 if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
2476 free(t->dirent);
2477 t->dirent = malloc(dirent_size);
2478 if (t->dirent == NULL) {
2479 closedir(t->d);
2480 t->d = INVALID_DIR_HANDLE;
2483 t->tree_errno = ENOMEM;
2484 t->visit_type = TREE_ERROR_DIR;
2485 return (t->visit_type);
2487 t->dirent_allocated = dirent_size;
2494 r = readdir_r(t->d, t->dirent, &t->de);
2505 if (r != 0 || t->de == NULL) {
2507 t->de = readdir(t->d);
2508 if (t->de == NULL) {
2511 closedir(t->d);
2512 t->d = INVALID_DIR_HANDLE;
2514 t->tree_errno = r;
2515 t->visit_type = TREE_ERROR_DIR;
2516 return (t->visit_type);
2520 name = t->de->d_name;
2521 namelen = D_NAMELEN(t->de);
2522 t->flags &= ~hasLstat;
2523 t->flags &= ~hasStat;
2529 return (t->visit_type = TREE_REGULAR);
2540 if (!(t->flags & hasStat)) {
2543 tree_current_access_path(t), &t->st, 0) != 0)
2547 if (la_stat(tree_current_access_path(t), &t->st) != 0)
2550 t->flags |= hasStat;
2552 return (&t->st);
2561 if (!(t->flags & hasLstat)) {
2564 tree_current_access_path(t), &t->lst,
2570 if (lstat(tree_current_access_path(t), &t->lst) != 0)
2572 if (la_stat(tree_current_access_path(t), &t->lst) != 0)
2576 t->flags |= hasLstat;
2578 return (&t->lst);
2592 if (t->flags & hasLstat) {
2597 if (S_ISDIR(st->st_mode))
2601 if (!S_ISLNK(st->st_mode))
2614 return (S_ISDIR(st->st_mode));
2631 if (t->flags & hasStat) {
2635 if (!S_ISDIR(st->st_mode))
2651 return (S_ISDIR(st->st_mode));
2660 struct tree_entry *te;
2662 for (te = t->current->parent; te != NULL; te = te->parent) {
2663 if (te->dev == (int64_t)st->st_dev &&
2664 te->ino == (int64_t)st->st_ino)
2682 (int64_t)st->st_dev == t->current_filesystem->dev &&
2683 st->st_dev != lst->st_dev);
2692 return (t->basename);
2701 return (t->path.s);
2713 if (t->entry_fd >= 0) {
2714 close_and_restore_time(t->entry_fd, t, &t->restore_time);
2715 t->entry_fd = -1;
2718 if (t->d != INVALID_DIR_HANDLE) {
2719 closedir(t->d);
2720 t->d = INVALID_DIR_HANDLE;
2723 while (t->stack != NULL) {
2724 if (t->stack->flags & isDirLink)
2725 close(t->stack->symlink_parent_fd);
2728 if (t->working_dir_fd >= 0) {
2729 close(t->working_dir_fd);
2730 t->working_dir_fd = -1;
2732 if (t->initial_dir_fd >= 0) {
2733 close(t->initial_dir_fd);
2734 t->initial_dir_fd = -1;
2748 archive_string_free(&t->path);
2750 free(t->dirent);
2752 free(t->sparse_list);
2753 for (i = 0; i < t->max_filesystem_id; i++)
2754 free(t->filesystem_table[i].allocation_ptr);
2755 free(t->filesystem_table);