Serenity Operating System
at master 93 lines 3.6 kB view raw
1/* 2 * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/HashMap.h> 8#include <AK/StringView.h> 9#include <AK/Vector.h> 10#include <LibCore/DeprecatedFile.h> 11 12// Exit code is bitwise-or of these values: 13static constexpr auto EXIT_COLLISION = 0x1; 14static constexpr auto EXIT_ERROR = 0x2; 15 16int main(int argc, char** argv) 17{ 18 if (argc < 3) { 19 warnln("Usage: {} path/to/some.ipc path/to/other.ipc [more ipc files ...]", argv[0]); 20 return EXIT_ERROR; 21 } 22 23 // Read files, compute their hashes, ignore collisions for now. 24 HashMap<u32, Vector<DeprecatedString>> inverse_hashes; 25 bool had_errors = false; 26 for (int file_index = 1; file_index < argc; ++file_index) { 27 DeprecatedString filename(argv[file_index]); 28 auto file_or_error = Core::DeprecatedFile::open(filename, Core::OpenMode::ReadOnly); 29 if (file_or_error.is_error()) { 30 warnln("Error: Cannot open '{}': {}", filename, file_or_error.error()); 31 had_errors = true; 32 continue; // next file 33 } 34 auto file = file_or_error.value(); 35 DeprecatedString endpoint_name; 36 while (true) { 37 DeprecatedString line = file->read_line(); 38 if (file->error() != 0 || line.is_null()) 39 break; 40 if (!line.starts_with("endpoint "sv)) 41 continue; 42 auto line_endpoint_name = line.substring("endpoint "sv.length()); 43 if (!endpoint_name.is_null()) { 44 // Note: If there are three or more endpoints defined in a file, these errors will look a bit wonky. 45 // However, that's fine, because it shouldn't happen in the first place. 46 warnln("Error: Multiple endpoints in file '{}': Found {} and {}", filename, file->error()); 47 had_errors = true; 48 continue; // next line 49 } 50 endpoint_name = line_endpoint_name; 51 } 52 if (file->error() != 0) { 53 warnln("Error: Failed to read '{}': {}", filename, file->error()); 54 had_errors = true; 55 continue; // next file 56 } 57 if (endpoint_name.is_null()) { 58 // If this happens, this file probably needs to parse the endpoint name more carefully. 59 warnln("Error: Could not detect endpoint name in file '{}'", filename); 60 had_errors = true; 61 continue; // next file 62 } 63 u32 hash = endpoint_name.hash(); 64 auto& files_with_hash = inverse_hashes.ensure(hash); 65 files_with_hash.append(filename); 66 } 67 68 // Report any collisions 69 bool had_collisions = false; 70 for (auto const& specific_collisions : inverse_hashes) { 71 if (specific_collisions.value.size() <= 1) 72 continue; 73 outln("Collision: Multiple endpoints use the magic number {}:", specific_collisions.key); 74 for (auto const& colliding_file : specific_collisions.value) { 75 outln("- {}", colliding_file); 76 } 77 had_collisions = true; 78 } 79 80 outln("Checked {} files, saw {} distinct magic numbers.", argc - 1, inverse_hashes.size()); 81 if (had_collisions) 82 outln("Consider giving your new service a different name."); 83 84 if (had_errors) 85 warnln("Some errors were encountered. There may be endpoints with colliding magic numbers."); 86 87 int exit_code = 0; 88 if (had_collisions) 89 exit_code |= EXIT_COLLISION; 90 if (had_errors) 91 exit_code |= EXIT_ERROR; 92 return exit_code; 93}