cmSystemTools: Avoid allocation in archive entry pathname wrappers

On Windows we use UTF-8.  On other platforms we use the locale's narrow
encoding.  Reuse libarchive's allocation for each of these.
This commit is contained in:
Brad King
2026-02-01 15:24:35 -05:00
parent 1e54c23568
commit ae93a9dd2d
2 changed files with 12 additions and 12 deletions
+6 -4
View File
@@ -40,12 +40,14 @@ static std::string cm_archive_error_string(struct archive* a)
// Set path to be written to the archive.
static void cm_archive_entry_copy_pathname(struct archive_entry* e,
std::string const& dest)
char const* dest)
{
#ifdef _WIN32
archive_entry_copy_pathname_w(e, cmsys::Encoding::ToWide(dest).c_str());
// libarchive converts our UTF-8 encoding to the archive's encoding.
archive_entry_update_pathname_utf8(e, dest);
#else
archive_entry_copy_pathname(e, dest.c_str());
// libarchive converts our locale's encoding to the archive's encoding.
archive_entry_copy_pathname(e, dest);
#endif
}
@@ -457,7 +459,7 @@ bool cmArchiveWrite::AddFile(char const* file, size_t skip, char const* prefix)
}
Entry e;
cm_archive_entry_copy_sourcepath(e, file);
cm_archive_entry_copy_pathname(e, dest);
cm_archive_entry_copy_pathname(e, dest.c_str());
if (archive_read_disk_entry_from_file(this->Disk, e, -1, nullptr) !=
ARCHIVE_OK) {
this->Error =
+6 -8
View File
@@ -379,16 +379,14 @@ extern char** environ; // NOLINT(readability-redundant-declaration)
#if !defined(CMAKE_BOOTSTRAP)
// Get path that was read from the archive.
static std::string cm_archive_entry_pathname(struct archive_entry* entry)
static char const* cm_archive_entry_pathname(struct archive_entry* entry)
{
# ifdef _WIN32
return cmsys::Encoding::ToNarrow(archive_entry_pathname_w(entry));
// libarchive converts the archive's encoding to our UTF-8 encoding.
return archive_entry_pathname_utf8(entry);
# else
std::string pathname;
if (char const* p = archive_entry_pathname(entry)) {
pathname = p;
}
return pathname;
// libarchive converts the archive's encoding to our locale's encoding.
return archive_entry_pathname(entry);
# endif
}
@@ -2562,7 +2560,7 @@ void list_item_verbose(FILE* out, struct archive_entry* entry)
}
strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
fprintf(out, " %s ", tmp);
fprintf(out, "%s", cm_archive_entry_pathname(entry).c_str());
fprintf(out, "%s", cm_archive_entry_pathname(entry));
/* Extra information for links. */
if (archive_entry_hardlink(entry)) /* Hard link */