From: Dag-Erling Smørgrav Date: Tue, 14 Oct 2025 00:11:31 +0000 (+0200) Subject: Create temporary files in the target directory X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F2753%2Fhead;p=thirdparty%2Flibarchive.git Create temporary files in the target directory Whenever we need to create a temporary file while writing to disk on a POSIX system, try to create it in the same directory as the final file instead of the current working directory. The target directory can reasonably be expected to be writable (and if it isn't, creating the file will fail anyway), but the current working directory may not be. While here, consistently use __archive_mkstemp(), and increase the template from six to eight random characters. Fixes: 2e73ea3a7db1 ("Fix max path-length metadata writing (#2243)") Fixes: e12c955dca63 ("Unify temporary directory handling") --- diff --git a/libarchive/archive_read_disk_entry_from_file.c b/libarchive/archive_read_disk_entry_from_file.c index 42af4034b..121af1987 100644 --- a/libarchive/archive_read_disk_entry_from_file.c +++ b/libarchive/archive_read_disk_entry_from_file.c @@ -358,12 +358,10 @@ setup_mac_metadata(struct archive_read_disk *a, return (ARCHIVE_OK); archive_string_init(&tempfile); - if (__archive_get_tempdir(&tempfile) != ARCHIVE_OK) { - ret = ARCHIVE_WARN; - goto cleanup; - } - archive_strcat(&tempfile, "tar.md.XXXXXX"); - tempfd = mkstemp(tempfile.s); + archive_strcpy(&tempfile, name); + archive_string_dirname(&tempfile); + archive_strcat(&tempfile, "/tar.XXXXXXXX"); + tempfd = __archive_mkstemp(tempfile.s); if (tempfd < 0) { archive_set_error(&a->archive, errno, "Could not open extended attribute file"); diff --git a/libarchive/archive_write_disk_posix.c b/libarchive/archive_write_disk_posix.c index 6fcf3929b..cd2562030 100644 --- a/libarchive/archive_write_disk_posix.c +++ b/libarchive/archive_write_disk_posix.c @@ -412,12 +412,14 @@ static ssize_t _archive_write_disk_data_block(struct archive *, const void *, static int la_mktemp(struct archive_write_disk *a) { + struct archive_string *tmp = &a->_tmpname_data; int oerrno, fd; mode_t mode; - archive_string_empty(&a->_tmpname_data); - archive_string_sprintf(&a->_tmpname_data, "%s.XXXXXX", a->name); - a->tmpname = a->_tmpname_data.s; + archive_strcpy(tmp, a->name); + archive_string_dirname(tmp); + archive_strcat(tmp, "/tar.XXXXXXXX"); + a->tmpname = tmp->s; fd = __archive_mkstemp(a->tmpname); if (fd == -1) @@ -4287,8 +4289,10 @@ create_tempdatafork(struct archive_write_disk *a, const char *pathname) int tmpfd; archive_string_init(&tmpdatafork); - archive_strcpy(&tmpdatafork, "tar.md.XXXXXX"); - tmpfd = mkstemp(tmpdatafork.s); + archive_strcpy(&tmpdatafork, pathname); + archive_string_dirname(&tmpdatafork); + archive_strcat(&tmpdatafork, "/tar.XXXXXXXX"); + tmpfd = __archive_mkstemp(tmpdatafork.s); if (tmpfd < 0) { archive_set_error(&a->archive, errno, "Failed to mkstemp"); @@ -4367,8 +4371,10 @@ set_mac_metadata(struct archive_write_disk *a, const char *pathname, * silly dance of writing the data to disk just so that * copyfile() can read it back in again. */ archive_string_init(&tmp); - archive_strcpy(&tmp, "tar.mmd.XXXXXX"); - fd = mkstemp(tmp.s); + archive_strcpy(&tmp, pathname); + archive_string_dirname(&tmp); + archive_strcat(&tmp, "/tar.XXXXXXXX"); + fd = __archive_mkstemp(tmp.s); if (fd < 0) { archive_set_error(&a->archive, errno,