From: Thomas Otto Date: Sat, 19 Oct 2019 08:28:57 +0000 (+0200) Subject: Convert result type string to an enum (#478) X-Git-Tag: v4.0~743 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a723b9472a8bf011491e5be8b9bbf3cffd7a64d3;p=thirdparty%2Fccache.git Convert result type string to an enum (#478) --- diff --git a/src/ccache.cpp b/src/ccache.cpp index b78d4a3d2..fc60c5e8e 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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) { diff --git a/src/result.cpp b/src/result.cpp index e5b95727e..eec77c408 100644 --- a/src/result.cpp +++ b/src/result.cpp @@ -48,8 +48,7 @@ // ::= // // ::= 0 (uint8_t) -// ::= uint8_t -// ::= suffix_len bytes +// ::= uint8_t // ::= uint64_t // ::= data_len bytes // ::= @@ -68,8 +67,7 @@ // --- [potentially compressed from here] ------------------------------------- // 1 byte // 1 byte -// 1 byte -// suffix_len bytes +// 1 byte // 8 bytes // 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 = ""; // 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(&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(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(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(&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(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(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(type), (unsigned long long)source_file_size, source_path.c_str()); writer.write(k_embedded_file_marker); - writer.write(suffix.length()); - writer.write(suffix.data(), suffix.length()); + writer.write(static_cast(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(type), (unsigned long long)source_file_size, source_path.c_str()); writer.write(k_raw_file_marker); - writer.write(suffix.length()); - writer.write(suffix.data(), suffix.length()); + writer.write(static_cast(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 } diff --git a/src/result.hpp b/src/result.hpp index 79a27c9df..02665a4fd 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -25,9 +25,24 @@ 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 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; bool result_get(const std::string& path, const ResultFileMap& result_file_map); bool result_put(const std::string& path, const ResultFileMap& result_file_map);