#define O_NOFOLLOW 0
#endif
+#ifndef AT_FDCWD
+#define AT_FDCWD -100
+#endif
+
struct fixup_entry {
struct fixup_entry *next;
struct archive_acl acl;
#define HFS_BLOCKS(s) ((s) >> 12)
+
+static int la_opendirat(int, const char *);
static void fsobj_error(int *, struct archive_string *, int, const char *,
const char *);
static int check_symlinks_fsobj(char *, int *, struct archive_string *,
static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
size_t, int64_t);
+static int
+la_opendirat(int fd, const char *path) {
+ const int flags = O_CLOEXEC
+#if defined(O_BINARY)
+ | O_BINARY
+#endif
+#if defined(O_DIRECTORY)
+ | O_DIRECTORY
+#endif
+#if defined(O_PATH)
+ | O_PATH
+#elif defined(O_SEARCH)
+ | O_SEARCH
+#elif defined(O_EXEC)
+ | O_EXEC
+#else
+ | O_RDONLY
+#endif
+ ;
+
+#if !defined(HAVE_OPENAT)
+ if (fd != AT_FDCWD) {
+ errno = ENOTSUP;
+ return (-1);
+ } else
+ return (open(fd, path, flags));
+#else
+ return (openat(fd, path, flags));
+#endif
+}
+
static int
lazy_stat(struct archive_write_disk *a)
{
check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
int flags)
{
-#if !defined(HAVE_LSTAT)
+#if !defined(HAVE_LSTAT) && \
+ !(defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT))
/* Platform doesn't have lstat, so we can't look for symlinks. */
(void)path; /* UNUSED */
(void)error_number; /* UNUSED */
char c;
int r;
struct stat st;
- int restore_pwd;
+ int chdir_fd;
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ int fd;
+#endif
/* Nothing to do here if name is empty */
if(path[0] == '\0')
* c holds what used to be in *tail
* last is 1 if this is the last tail
*/
- restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC);
- __archive_ensure_cloexec_flag(restore_pwd);
- if (restore_pwd < 0) {
+ chdir_fd = la_opendirat(AT_FDCWD, ".");
+ __archive_ensure_cloexec_flag(chdir_fd);
+ if (chdir_fd < 0) {
fsobj_error(a_eno, a_estr, errno,
"Could not open ", path);
return (ARCHIVE_FATAL);
c = tail[0];
tail[0] = '\0';
/* Check that we haven't hit a symlink. */
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ r = fstatat(chdir_fd, head, &st, AT_SYMLINK_NOFOLLOW);
+#else
r = lstat(head, &st);
+#endif
if (r != 0) {
tail[0] = c;
/* We've hit a dir that doesn't exist; stop now. */
}
} else if (S_ISDIR(st.st_mode)) {
if (!last) {
- if (chdir(head) != 0) {
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ fd = la_opendirat(chdir_fd, head);
+ if (fd < 0)
+ r = -1;
+ else {
+ r = 0;
+ close(chdir_fd);
+ chdir_fd = fd;
+ }
+#else
+ r = chdir(head);
+#endif
+ if (r != 0) {
tail[0] = c;
fsobj_error(a_eno, a_estr, errno,
"Could not chdir ", path);
* so we can overwrite it with the
* item being extracted.
*/
- if (unlink(head)) {
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ r = unlinkat(chdir_fd, head, 0);
+#else
+ r = unlink(head);
+#endif
+ if (r != 0) {
tail[0] = c;
fsobj_error(a_eno, a_estr, errno,
"Could not remove symlink ",
break;
} else if (flags & ARCHIVE_EXTRACT_UNLINK) {
/* User asked us to remove problems. */
- if (unlink(head) != 0) {
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ r = unlinkat(chdir_fd, head, 0);
+#else
+ r = unlink(head);
+#endif
+ if (r != 0) {
tail[0] = c;
fsobj_error(a_eno, a_estr, 0,
"Cannot remove intervening "
* This is needed to extract hardlinks over
* symlinks.
*/
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ r = fstatat(chdir_fd, head, &st, 0);
+#else
r = la_stat(head, &st);
+#endif
if (r != 0) {
tail[0] = c;
if (errno == ENOENT) {
break;
}
} else if (S_ISDIR(st.st_mode)) {
- if (chdir(head) != 0) {
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ fd = la_opendirat(chdir_fd, head);
+ if (fd < 0)
+ r = -1;
+ else {
+ r = 0;
+ close(chdir_fd);
+ chdir_fd = fd;
+ }
+#else
+ r = chdir(head);
+#endif
+ if (r != 0) {
tail[0] = c;
fsobj_error(a_eno, a_estr,
errno,
}
/* Catches loop exits via break */
tail[0] = c;
-#ifdef HAVE_FCHDIR
+#if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
+ /* If we operate with openat(), fstatat() and unlinkat() there was
+ * no chdir(), so just close the fd */
+ if (chdir_fd >= 0)
+ close(chdir_fd);
+#elif HAVE_FCHDIR
/* If we changed directory above, restore it here. */
- if (restore_pwd >= 0) {
- r = fchdir(restore_pwd);
+ if (chdir_fd >= 0) {
+ r = fchdir(chdir_fd);
if (r != 0) {
fsobj_error(a_eno, a_estr, errno,
"chdir() failure", "");
}
- close(restore_pwd);
- restore_pwd = -1;
+ close(chdir_fd);
+ chdir_fd = -1;
if (r != 0) {
res = (ARCHIVE_FATAL);
}