aboutsummaryrefslogtreecommitdiff
path: root/libarchive/archive_read_disk_posix.c
diff options
context:
space:
mode:
authorMartin Matuska <mm@FreeBSD.org>2025-10-16 17:36:33 +0000
committerMartin Matuska <mm@FreeBSD.org>2025-10-16 17:41:19 +0000
commit8f38cbcd9c4a4f27bdccf2e75a7e20026cff5181 (patch)
treed6d99051589c097001ba225066098ae579a57bcc /libarchive/archive_read_disk_posix.c
parent76141d3306dea42228646b9a4a5e97ccba59fe2a (diff)
Diffstat (limited to 'libarchive/archive_read_disk_posix.c')
-rw-r--r--libarchive/archive_read_disk_posix.c157
1 files changed, 15 insertions, 142 deletions
diff --git a/libarchive/archive_read_disk_posix.c b/libarchive/archive_read_disk_posix.c
index a7a98e9cb1cd..54a8e66188f8 100644
--- a/libarchive/archive_read_disk_posix.c
+++ b/libarchive/archive_read_disk_posix.c
@@ -168,9 +168,6 @@ struct filesystem {
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;
@@ -203,10 +200,6 @@ struct tree {
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. */
@@ -869,7 +862,7 @@ next_entry(struct archive_read_disk *a, struct tree *t,
tree_enter_initial_dir(t);
return (ARCHIVE_FATAL);
case TREE_ERROR_DIR:
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
+ archive_set_error(&a->archive, t->tree_errno,
"%s: Couldn't visit directory",
tree_current_path(t));
tree_enter_initial_dir(t);
@@ -1578,9 +1571,6 @@ setup_current_filesystem(struct archive_read_disk *a)
# endif
#endif
int r, xr = 0;
-#if !defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
- long nm;
-#endif
t->current_filesystem->synthetic = -1;
t->current_filesystem->remote = -1;
@@ -1647,35 +1637,6 @@ setup_current_filesystem(struct archive_read_disk *a)
#endif
t->current_filesystem->noatime = 0;
-#if defined(USE_READDIR_R)
- /* Set maximum filename length. */
-#if defined(HAVE_STRUCT_STATFS_F_NAMEMAX)
- t->current_filesystem->name_max = sfs.f_namemax;
-#else
-# if defined(_PC_NAME_MAX)
- /* Mac OS X does not have f_namemax in struct statfs. */
- if (tree_current_is_symblic_link_target(t)) {
- if (tree_enter_working_dir(t) != 0) {
- archive_set_error(&a->archive, errno, "fchdir failed");
- return (ARCHIVE_FAILED);
- }
- nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
- } else
- nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
-# else
- nm = -1;
-# endif
- if (nm == -1)
- t->current_filesystem->name_max = NAME_MAX;
- else
- t->current_filesystem->name_max = nm;
-#endif
- if (t->current_filesystem->name_max == 0) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Cannot determine name_max");
- return (ARCHIVE_FAILED);
- }
-#endif /* USE_READDIR_R */
return (ARCHIVE_OK);
}
@@ -1863,19 +1824,6 @@ setup_current_filesystem(struct archive_read_disk *a)
#endif
t->current_filesystem->noatime = 0;
-#if defined(USE_READDIR_R)
- /* Set maximum filename length. */
-#if defined(HAVE_STATVFS)
- t->current_filesystem->name_max = svfs.f_namemax;
-#else
- t->current_filesystem->name_max = sfs.f_namelen;
-#endif
- if (t->current_filesystem->name_max == 0) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Cannot determine name_max");
- return (ARCHIVE_FAILED);
- }
-#endif
return (ARCHIVE_OK);
}
@@ -1953,15 +1901,6 @@ setup_current_filesystem(struct archive_read_disk *a)
#endif
t->current_filesystem->noatime = 0;
-#if defined(USE_READDIR_R)
- /* Set maximum filename length. */
- t->current_filesystem->name_max = svfs.f_namemax;
- if (t->current_filesystem->name_max == 0) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Cannot determine name_max");
- return (ARCHIVE_FAILED);
- }
-#endif
return (ARCHIVE_OK);
}
@@ -1975,9 +1914,6 @@ static int
setup_current_filesystem(struct archive_read_disk *a)
{
struct tree *t = a->tree;
-#if defined(_PC_NAME_MAX) && defined(USE_READDIR_R)
- long nm;
-#endif
t->current_filesystem->synthetic = -1;/* Not supported */
t->current_filesystem->remote = -1;/* Not supported */
t->current_filesystem->noatime = 0;
@@ -1987,40 +1923,6 @@ setup_current_filesystem(struct archive_read_disk *a)
t->current_filesystem->min_xfer_size = -1;
t->current_filesystem->incr_xfer_size = -1;
-#if defined(USE_READDIR_R)
- /* Set maximum filename length. */
-# if defined(_PC_NAME_MAX)
- if (tree_current_is_symblic_link_target(t)) {
- if (tree_enter_working_dir(t) != 0) {
- archive_set_error(&a->archive, errno, "fchdir failed");
- return (ARCHIVE_FAILED);
- }
- nm = pathconf(tree_current_access_path(t), _PC_NAME_MAX);
- } else
- nm = fpathconf(tree_current_dir_fd(t), _PC_NAME_MAX);
- if (nm == -1)
-# endif /* _PC_NAME_MAX */
- /*
- * Some systems (HP-UX or others?) incorrectly defined
- * NAME_MAX macro to be a smaller value.
- */
-# if defined(NAME_MAX) && NAME_MAX >= 255
- t->current_filesystem->name_max = NAME_MAX;
-# else
- /* No way to get a trusted value of maximum filename
- * length. */
- t->current_filesystem->name_max = PATH_MAX;
-# endif /* NAME_MAX */
-# if defined(_PC_NAME_MAX)
- else
- t->current_filesystem->name_max = nm;
-# endif /* _PC_NAME_MAX */
- if (t->current_filesystem->name_max == 0) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Cannot determine name_max");
- return (ARCHIVE_FAILED);
- }
-#endif /* USE_READDIR_R */
return (ARCHIVE_OK);
}
@@ -2112,8 +2014,11 @@ tree_dup(int fd)
}
#endif /* F_DUPFD_CLOEXEC */
new_fd = dup(fd);
- __archive_ensure_cloexec_flag(new_fd);
- return (new_fd);
+ if (new_fd != -1) {
+ __archive_ensure_cloexec_flag(new_fd);
+ return (new_fd);
+ }
+ return (-1);
}
/*
@@ -2235,11 +2140,16 @@ tree_reopen(struct tree *t, const char *path, int restore_time)
* so try again for execute. The consequences of not opening this are
* unhelpful and unnecessary errors later.
*/
- if (t->initial_dir_fd < 0)
+ if (t->initial_dir_fd < 0) {
t->initial_dir_fd = open(".", o_flag | O_CLOEXEC);
+ if (t->initial_dir_fd < 0)
+ return NULL;
+ }
#endif
__archive_ensure_cloexec_flag(t->initial_dir_fd);
t->working_dir_fd = tree_dup(t->initial_dir_fd);
+ if (t->working_dir_fd < 0)
+ return NULL;
return (t);
}
@@ -2449,12 +2359,11 @@ tree_dir_next_posix(struct tree *t)
size_t namelen;
if (t->d == NULL) {
-#if defined(USE_READDIR_R)
- size_t dirent_size;
-#endif
#if defined(HAVE_FDOPENDIR)
- t->d = fdopendir(tree_dup(t->working_dir_fd));
+ int fd = tree_dup(t->working_dir_fd);
+ if (fd != -1)
+ t->d = fdopendir(fd);
#else /* HAVE_FDOPENDIR */
if (tree_enter_working_dir(t) == 0) {
t->d = opendir(".");
@@ -2470,45 +2379,12 @@ tree_dir_next_posix(struct tree *t)
t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
return (t->visit_type);
}
-#if defined(USE_READDIR_R)
- dirent_size = offsetof(struct dirent, d_name) +
- t->filesystem_table[t->current->filesystem_id].name_max + 1;
- if (t->dirent == NULL || t->dirent_allocated < dirent_size) {
- free(t->dirent);
- t->dirent = malloc(dirent_size);
- if (t->dirent == NULL) {
- closedir(t->d);
- t->d = INVALID_DIR_HANDLE;
- (void)tree_ascend(t);
- tree_pop(t);
- t->tree_errno = ENOMEM;
- t->visit_type = TREE_ERROR_DIR;
- return (t->visit_type);
- }
- t->dirent_allocated = dirent_size;
- }
-#endif /* USE_READDIR_R */
}
for (;;) {
errno = 0;
-#if defined(USE_READDIR_R)
- r = readdir_r(t->d, t->dirent, &t->de);
-#ifdef _AIX
- /* Note: According to the man page, return value 9 indicates
- * that the readdir_r was not successful and the error code
- * is set to the global errno variable. And then if the end
- * of directory entries was reached, the return value is 9
- * and the third parameter is set to NULL and errno is
- * unchanged. */
- if (r == 9)
- r = errno;
-#endif /* _AIX */
- if (r != 0 || t->de == NULL) {
-#else
t->de = readdir(t->d);
if (t->de == NULL) {
r = errno;
-#endif
closedir(t->d);
t->d = INVALID_DIR_HANDLE;
if (r != 0) {
@@ -2747,9 +2623,6 @@ tree_free(struct tree *t)
if (t == NULL)
return;
archive_string_free(&t->path);
-#if defined(USE_READDIR_R)
- free(t->dirent);
-#endif
free(t->sparse_list);
for (i = 0; i < t->max_filesystem_id; i++)
free(t->filesystem_table[i].allocation_ptr);