Browse Source

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.
Brad King 1 month ago
parent
commit
ae93a9dd2d
2 changed files with 12 additions and 12 deletions
  1. 6 4
      Source/cmArchiveWrite.cxx
  2. 6 8
      Source/cmSystemTools.cxx

+ 6 - 4
Source/cmArchiveWrite.cxx

@@ -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
Source/cmSystemTools.cxx

@@ -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 */