]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Convert result type string to an enum (#478)
authorThomas Otto <thomas.otto@pdv-fs.de>
Sat, 19 Oct 2019 08:28:57 +0000 (10:28 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 19 Oct 2019 08:28:57 +0000 (10:28 +0200)
src/ccache.cpp
src/result.cpp
src/result.hpp

index b78d4a3d2f3bef914049b1e36ce4196795723a9f..fc60c5e8ec9eaf4a0dbe8027016d401c7f9fb3a9 100644 (file)
@@ -1440,25 +1440,25 @@ to_cache(struct args* args, struct hash* depend_mode_hash)
   }
   ResultFileMap result_file_map;
   if (st.st_size > 0) {
-    result_file_map.emplace(k_result_stderr_name, tmp_stderr);
+    result_file_map.emplace(FileType::stderr_output, tmp_stderr);
   }
-  result_file_map.emplace(".o", output_obj);
+  result_file_map.emplace(FileType::object, output_obj);
   if (generating_dependencies) {
-    result_file_map.emplace(".d", output_dep);
+    result_file_map.emplace(FileType::dependency, output_dep);
   }
   if (generating_coverage) {
-    result_file_map.emplace(".gcno", output_cov);
+    result_file_map.emplace(FileType::coverage, output_cov);
   }
   if (generating_stackusage) {
-    result_file_map.emplace(".su", output_su);
+    result_file_map.emplace(FileType::stackusage, output_su);
   }
   if (generating_diagnostics) {
-    result_file_map.emplace(".dia", output_dia);
+    result_file_map.emplace(FileType::diagnostic, output_dia);
   }
   if (seen_split_dwarf && stat(output_dwo, &st) == 0) {
     // Only copy .dwo file if it was created by the compiler (GCC and Clang
     // behave differently e.g. for "-gsplit-dwarf -g1").
-    result_file_map.emplace(".dwo", output_dwo);
+    result_file_map.emplace(FileType::dwarf_object, output_dwo);
   }
   struct stat orig_dest_st;
   bool orig_dest_existed = stat(cached_result_path, &orig_dest_st) == 0;
@@ -2179,23 +2179,23 @@ from_cache(enum fromcache_call_mode mode, bool put_result_in_manifest)
 
   ResultFileMap result_file_map;
   if (!str_eq(output_obj, "/dev/null")) {
-    result_file_map.emplace(".o", output_obj);
+    result_file_map.emplace(FileType::object, output_obj);
     if (seen_split_dwarf) {
-      result_file_map.emplace(".dwo", output_dwo);
+      result_file_map.emplace(FileType::dwarf_object, output_dwo);
     }
   }
-  result_file_map.emplace(k_result_stderr_name, tmp_stderr);
+  result_file_map.emplace(FileType::stderr_output, tmp_stderr);
   if (produce_dep_file) {
-    result_file_map.emplace(".d", output_dep);
+    result_file_map.emplace(FileType::dependency, output_dep);
   }
   if (generating_coverage) {
-    result_file_map.emplace(".gcno", output_cov);
+    result_file_map.emplace(FileType::coverage, output_cov);
   }
   if (generating_stackusage) {
-    result_file_map.emplace(".su", output_su);
+    result_file_map.emplace(FileType::stackusage, output_su);
   }
   if (generating_diagnostics) {
-    result_file_map.emplace(".dia", output_dia);
+    result_file_map.emplace(FileType::diagnostic, output_dia);
   }
   bool ok = result_get(cached_result_path, result_file_map);
   if (!ok) {
index e5b95727e5492b211738b928e271fed10eac8fc8..eec77c408ffa7a21b38ca10a4025dd259704f6e5 100644 (file)
@@ -48,8 +48,7 @@
 // <embedded_file_entry>  ::= <embedded_file_marker> <suffix_len> <suffix>
 //                            <data_len> <data>
 // <embedded_file_marker> ::= 0 (uint8_t)
-// <suffix_len>           ::= uint8_t
-// <suffix>               ::= suffix_len bytes
+// <embedded_file_type>   ::= uint8_t
 // <data_len>             ::= uint64_t
 // <data>                 ::= data_len bytes
 // <raw_file_entry>       ::= <raw_file_marker> <suffix_len> <suffix> <file_len>
@@ -68,8 +67,7 @@
 // --- [potentially compressed from here] -------------------------------------
 // <n_entries>            1 byte
 // <embedded_file_marker> 1 byte
-// <suffix_len>           1 byte
-// <suffix>               suffix_len bytes
+// <embedded_file_type>   1 byte
 // <data_len>             8 bytes
 // <data>                 data_len bytes
 // ...
@@ -89,7 +87,6 @@ extern char* stats_file;
 
 const uint8_t k_result_magic[4] = {'c', 'C', 'r', 'S'};
 const uint8_t k_result_version = 1;
-const std::string k_result_stderr_name = "<stderr>";
 
 // File data stored inside the result file.
 const uint8_t k_embedded_file_marker = 0;
@@ -116,30 +113,26 @@ read_embedded_file_entry(CacheEntryReader& reader,
                          const ResultFileMap* result_file_map,
                          FILE* dump_stream)
 {
-  uint8_t suffix_len;
-  reader.read(suffix_len);
-
-  char suffix[256];
-  reader.read(suffix, suffix_len);
+  FileType type;
+  reader.read(*reinterpret_cast<UnderlyingFileTypeInt*>(&type));
 
   uint64_t file_len;
   reader.read(file_len);
 
   bool content_read = false;
-  std::string suffix_str(suffix, suffix_len);
   if (dump_stream) {
     fmt::print(dump_stream,
-               "Embedded file #{}: {} ({} bytes)\n",
+               "Embedded file #{}: type {} ({} bytes)\n",
                entry_number,
-               suffix_str,
+               +static_cast<UnderlyingFileTypeInt>(type),
                file_len);
   } else {
-    cc_log("Retrieving embedded file #%u %s (%llu bytes)",
+    cc_log("Retrieving embedded file #%u type %u (%llu bytes)",
            entry_number,
-           suffix_str.c_str(),
+           +static_cast<UnderlyingFileTypeInt>(type),
            (unsigned long long)file_len);
 
-    const auto it = result_file_map->find(suffix_str);
+    const auto it = result_file_map->find(type);
     if (it != result_file_map->end()) {
       content_read = true;
 
@@ -217,26 +210,22 @@ read_raw_file_entry(CacheEntryReader& reader,
                     const ResultFileMap* result_file_map,
                     std::FILE* dump_stream)
 {
-  uint8_t suffix_len;
-  reader.read(suffix_len);
-
-  char suffix[256];
-  reader.read(suffix, suffix_len);
+  FileType type;
+  reader.read(*reinterpret_cast<UnderlyingFileTypeInt*>(&type));
 
   uint64_t file_len;
   reader.read(file_len);
 
-  std::string suffix_str(suffix, suffix_len);
   if (dump_stream) {
     fmt::print(dump_stream,
-               "Raw file #{}: {} ({} bytes)\n",
+               "Raw file #{}: type {} ({} bytes)\n",
                entry_number,
-               suffix_str,
+               +static_cast<UnderlyingFileTypeInt>(type),
                file_len);
   } else {
-    cc_log("Retrieving raw file #%u %s (%llu bytes)",
+    cc_log("Retrieving raw file #%u type %u (%llu bytes)",
            entry_number,
-           suffix_str.c_str(),
+           +static_cast<UnderlyingFileTypeInt>(type),
            (unsigned long long)file_len);
 
     auto raw_path = get_raw_file_path(result_path_in_cache, entry_number);
@@ -253,7 +242,7 @@ read_raw_file_entry(CacheEntryReader& reader,
                     file_len));
     }
 
-    const auto it = result_file_map->find(suffix_str);
+    const auto it = result_file_map->find(type);
     if (it != result_file_map->end()) {
       const auto& dest_path = it->second;
       if (!copy_raw_file(raw_path, dest_path, false)) {
@@ -326,7 +315,7 @@ write_embedded_file_entry(CacheEntryWriter& writer,
                           uint32_t entry_number,
                           const ResultFileMap::value_type& suffix_and_path)
 {
-  const auto& suffix = suffix_and_path.first;
+  FileType type = suffix_and_path.first;
   const auto& source_path = suffix_and_path.second;
 
   uint64_t source_file_size;
@@ -335,15 +324,14 @@ write_embedded_file_entry(CacheEntryWriter& writer,
       fmt::format("Failed to stat {}: {}", source_path, strerror(errno)));
   }
 
-  cc_log("Storing embedded file #%u %s (%llu bytes) from %s",
+  cc_log("Storing embedded file #%u type %u (%llu bytes) from %s",
          entry_number,
-         suffix.c_str(),
+         +static_cast<UnderlyingFileTypeInt>(type),
          (unsigned long long)source_file_size,
          source_path.c_str());
 
   writer.write<uint8_t>(k_embedded_file_marker);
-  writer.write<uint8_t>(suffix.length());
-  writer.write(suffix.data(), suffix.length());
+  writer.write(static_cast<UnderlyingFileTypeInt>(type));
   writer.write(source_file_size);
 
   File file(source_path, "rb");
@@ -369,7 +357,7 @@ write_raw_file_entry(CacheEntryWriter& writer,
                      uint32_t entry_number,
                      const ResultFileMap::value_type& suffix_and_path)
 {
-  const auto& suffix = suffix_and_path.first;
+  FileType type = suffix_and_path.first;
   const auto& source_path = suffix_and_path.second;
 
   uint64_t source_file_size;
@@ -381,15 +369,14 @@ write_raw_file_entry(CacheEntryWriter& writer,
   uint64_t old_size;
   uint64_t new_size;
 
-  cc_log("Storing raw file #%u %s (%llu bytes) from %s",
+  cc_log("Storing raw file #%u %u (%llu bytes) from %s",
          entry_number,
-         suffix.c_str(),
+         +static_cast<UnderlyingFileTypeInt>(type),
          (unsigned long long)source_file_size,
          source_path.c_str());
 
   writer.write<uint8_t>(k_raw_file_marker);
-  writer.write<uint8_t>(suffix.length());
-  writer.write(suffix.data(), suffix.length());
+  writer.write(static_cast<UnderlyingFileTypeInt>(type));
   writer.write(source_file_size);
 
   auto raw_file = get_raw_file_path(result_path_in_cache, entry_number);
@@ -410,7 +397,7 @@ write_raw_file_entry(CacheEntryWriter& writer,
 }
 
 static bool
-should_store_raw_file(const std::string& suffix)
+should_store_raw_file(FileType type)
 {
   if (!g_config.file_clone() && !g_config.hard_link()) {
     return false;
@@ -420,7 +407,7 @@ should_store_raw_file(const std::string& suffix)
   //   1. Never are large.
   //   2. Will end up in a temporary file anyway.
   //
-  // - Don't store .d files since they:
+  // - Don't store .d/dependency files since they:
   //   1. Never are large.
   //   2. Compress well.
   //   3. Cause trouble for automake if hard-linked (see ccache issue 378).
@@ -430,7 +417,7 @@ should_store_raw_file(const std::string& suffix)
   // could be fixed by letting read_raw_file_entry refuse to hard link .d
   // files, but it's easier to simply always store them embedded. This will
   // also save i-nodes in the cache.
-  return suffix != k_result_stderr_name && suffix != ".d";
+  return type != FileType::stderr_output && type != FileType::dependency;
 }
 
 static void
@@ -439,7 +426,6 @@ write_result(const std::string& path, const ResultFileMap& result_file_map)
   uint64_t payload_size = 0;
   payload_size += 1; // n_entries
   for (const auto& pair : result_file_map) {
-    const auto& suffix = pair.first;
     const auto& result_file = pair.second;
     uint64_t source_file_size;
     if (!Util::get_file_size(result_file, source_file_size)) {
@@ -447,8 +433,7 @@ write_result(const std::string& path, const ResultFileMap& result_file_map)
         fmt::format("Failed to stat {}: {}", result_file, strerror(errno)));
     }
     payload_size += 1;                // embedded_file_marker
-    payload_size += 1;                // suffix_len
-    payload_size += suffix.length();  // suffix
+    payload_size += 1;                // embedded_file_type
     payload_size += 8;                // data_len
     payload_size += source_file_size; // data
   }
index 79a27c9df953dd4e6b83af7848ed1f14a489550d..02665a4fda1f81dd352e49bacd74b0decc07fc55 100644 (file)
 
 extern const uint8_t k_result_magic[4];
 extern const uint8_t k_result_version;
-extern const std::string k_result_stderr_name;
 
-typedef std::map<std::string /*suffix*/, std::string /*path*/> ResultFileMap;
+using UnderlyingFileTypeInt = uint8_t;
+enum class FileType : UnderlyingFileTypeInt {
+  // These values are written into the cache result file.
+  // This means they must never be changed or removed unless the
+  // result file version is incremented. Adding new values is Ok.
+  object = 0,
+  dependency = 1,
+  stderr_output = 2,
+  coverage = 3,
+  stackusage = 4,
+  diagnostic = 5,
+  dwarf_object = 6,
+
+  Max = 255,
+};
+
+using ResultFileMap = std::map<FileType, std::string /*path*/>;
 
 bool result_get(const std::string& path, const ResultFileMap& result_file_map);
 bool result_put(const std::string& path, const ResultFileMap& result_file_map);