main.cpp 4.0 KB

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