main.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "AvailablePort.h"
  7. #include "InstalledPort.h"
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/System.h>
  10. #include <LibMain/Main.h>
  11. #include <unistd.h>
  12. static void print_port_details(InstalledPort const& port)
  13. {
  14. outln("{}, installed as {}, version {}", port.name(), port.type_as_string_view(), port.version());
  15. if (!port.dependencies().is_empty()) {
  16. out(" Dependencies:");
  17. for (auto const& dependency : port.dependencies())
  18. out(" {}", dependency);
  19. outln();
  20. }
  21. }
  22. ErrorOr<int> serenity_main(Main::Arguments arguments)
  23. {
  24. TRY(Core::System::pledge("stdio recvfd thread unix rpath cpath wpath"));
  25. TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
  26. TRY(Core::System::unveil("/usr"sv, "c"sv));
  27. TRY(Core::System::unveil("/usr/Ports"sv, "rwc"sv));
  28. TRY(Core::System::unveil("/res"sv, "r"sv));
  29. TRY(Core::System::unveil("/usr/lib"sv, "r"sv));
  30. TRY(Core::System::unveil(nullptr, nullptr));
  31. bool verbose = false;
  32. bool show_all_installed_ports = false;
  33. bool update_packages_db = false;
  34. StringView query_package {};
  35. Core::ArgsParser args_parser;
  36. args_parser.add_option(show_all_installed_ports, "Show all manually-installed ports", "list-manual-ports", 'l');
  37. args_parser.add_option(update_packages_db, "Sync/Update ports database", "update-ports-database", 'u');
  38. args_parser.add_option(query_package, "Query ports database for package name", "query-package", 'q', "Package name to query");
  39. args_parser.add_option(verbose, "Verbose", "verbose", 'v');
  40. args_parser.parse(arguments);
  41. if (!update_packages_db && !show_all_installed_ports && query_package.is_null()) {
  42. outln("pkg: No action to be performed was specified.");
  43. return 0;
  44. }
  45. HashMap<String, InstalledPort> installed_ports;
  46. HashMap<String, AvailablePort> available_ports;
  47. if (show_all_installed_ports || !query_package.is_null()) {
  48. if (Core::System::access(ports_database, R_OK).is_error()) {
  49. warnln("pkg: {} isn't accessible, did you install a package in the past?", ports_database);
  50. return 1;
  51. }
  52. installed_ports = TRY(InstalledPort::read_ports_database());
  53. }
  54. int return_value = 0;
  55. if (update_packages_db) {
  56. if (getuid() != 0) {
  57. outln("pkg: Requires root to update packages database.");
  58. return 1;
  59. }
  60. return_value = TRY(AvailablePort::update_available_ports_list_file());
  61. }
  62. if (!query_package.is_null()) {
  63. if (Core::System::access("/usr/Ports/AvailablePorts.md"sv, R_OK).is_error()) {
  64. outln("pkg: Please run this program with -u first!");
  65. return 0;
  66. }
  67. available_ports = TRY(AvailablePort::read_available_ports_list());
  68. }
  69. if (show_all_installed_ports) {
  70. outln("Manually-installed ports:");
  71. TRY(InstalledPort::for_each_by_type(installed_ports, InstalledPort::Type::Manual, [](auto& port) -> ErrorOr<void> {
  72. print_port_details(port);
  73. return {};
  74. }));
  75. }
  76. if (!query_package.is_null()) {
  77. if (query_package.is_empty()) {
  78. outln("pkg: Queried package name is empty.");
  79. return 0;
  80. }
  81. return_value = TRY(AvailablePort::query_details_for_package(available_ports, installed_ports, query_package, verbose));
  82. }
  83. return return_value;
  84. }