apt-dump-solver.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. dummy solver to get quickly a scenario file out of APT
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <apt-pkg/edsp.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <cstdio>
  12. #include <iostream>
  13. #include <sstream>
  14. #include <config.h>
  15. /*}}}*/
  16. // ShowHelp - Show a help screen /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. static bool ShowHelp() {
  20. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  21. std::cout <<
  22. "Usage: apt-dump-solver\n"
  23. "\n"
  24. "apt-dump-solver is a dummy solver who just dumps its input to the\n"
  25. "file specified in the environment variable APT_EDSP_DUMP_FILENAME and\n"
  26. "exists with a proper EDSP error.\n"
  27. "\n"
  28. " This dump has lost Super Cow Powers.\n";
  29. return true;
  30. }
  31. /*}}}*/
  32. int main(int argc,const char *argv[]) /*{{{*/
  33. {
  34. // we really don't need anything
  35. DropPrivileges();
  36. if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1],"-h") == 0 ||
  37. strcmp(argv[1],"-v") == 0 || strcmp(argv[1],"--version") == 0)) {
  38. ShowHelp();
  39. return 0;
  40. }
  41. FileFd stdoutfd;
  42. if (stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  43. return 1;
  44. char const * const filename = getenv("APT_EDSP_DUMP_FILENAME");
  45. if (filename == NULL || strlen(filename) == 0)
  46. {
  47. EDSP::WriteError("ERR_NO_FILENAME", "You have to set the environment variable APT_EDSP_DUMP_FILENAME\n"
  48. "to a valid filename to store the dump of EDSP solver input in.\n"
  49. "For example with: export APT_EDSP_DUMP_FILENAME=/tmp/dump.edsp", stdoutfd);
  50. return 0;
  51. }
  52. RemoveFile(argv[0], filename);
  53. FileFd input, output;
  54. if (input.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false ||
  55. output.Open(filename, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, FileFd::Extension, 0600) == false ||
  56. CopyFile(input, output) == false || input.Close() == false || output.Close() == false)
  57. {
  58. std::ostringstream out;
  59. out << "Writing EDSP solver input to file '" << filename << "' failed!\n";
  60. _error->DumpErrors(out);
  61. EDSP::WriteError("ERR_WRITE_ERROR", out.str(), stdoutfd);
  62. return 0;
  63. }
  64. EDSP::WriteError("ERR_JUST_DUMPING", "I am too dumb, i can just dump!\nPlease use one of my friends instead!", stdoutfd);
  65. return 0;
  66. }