edspsystem.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. This system provides the abstraction to use the scenario file as the
  5. only source of package information to be able to feed the created file
  6. back to APT for its own consumption (eat your own dogfood).
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/debversion.h>
  13. #include <apt-pkg/edspindexfile.h>
  14. #include <apt-pkg/edspsystem.h>
  15. #include <apt-pkg/pkgcache.h>
  16. #include <apt-pkg/cacheiterators.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <string>
  22. #include <vector>
  23. /*}}}*/
  24. // System::System - Constructor /*{{{*/
  25. edspLikeSystem::edspLikeSystem(char const * const Label) : pkgSystem(Label, &debVS)
  26. {
  27. }
  28. edspSystem::edspSystem() : edspLikeSystem("Debian APT solver interface")
  29. {
  30. }
  31. /*}}}*/
  32. // System::Lock - Get the lock /*{{{*/
  33. bool edspLikeSystem::Lock()
  34. {
  35. return true;
  36. }
  37. /*}}}*/
  38. // System::UnLock - Drop a lock /*{{{*/
  39. bool edspLikeSystem::UnLock(bool /*NoErrors*/)
  40. {
  41. return true;
  42. }
  43. /*}}}*/
  44. // System::CreatePM - Create the underlying package manager /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* we can't use edsp input as input for real installations - just a
  47. simulation can work, but everything else will fail bigtime */
  48. pkgPackageManager *edspLikeSystem::CreatePM(pkgDepCache * /*Cache*/) const
  49. {
  50. return nullptr;
  51. }
  52. /*}}}*/
  53. // System::Initialize - Setup the configuration space.. /*{{{*/
  54. bool edspLikeSystem::Initialize(Configuration &Cnf)
  55. {
  56. Cnf.Set("Dir::Log", "/dev/null");
  57. // state is included completely in the input files
  58. Cnf.Set("Dir::Etc::preferences", "/dev/null");
  59. Cnf.Set("Dir::Etc::preferencesparts", "/dev/null");
  60. Cnf.Set("Dir::State::status","/dev/null");
  61. Cnf.Set("Dir::State::extended_states","/dev/null");
  62. Cnf.Set("Dir::State::lists","/dev/null");
  63. // do not store an mmap cache
  64. Cnf.Set("Dir::Cache::pkgcache", "");
  65. Cnf.Set("Dir::Cache::srcpkgcache", "");
  66. // the protocols only propose actions, not do them
  67. Cnf.Set("Debug::NoLocking", "true");
  68. Cnf.Set("APT::Get::Simulate", "true");
  69. StatusFile.reset(nullptr);
  70. return true;
  71. }
  72. bool edspSystem::Initialize(Configuration &Cnf)
  73. {
  74. if (edspLikeSystem::Initialize(Cnf) == false)
  75. return false;
  76. std::string const tmp = GetTempDir();
  77. char tmpname[300];
  78. snprintf(tmpname, sizeof(tmpname), "%s/apt-edsp-solver-XXXXXX", tmp.c_str());
  79. if (nullptr == mkdtemp(tmpname))
  80. return false;
  81. tempDir = tmpname;
  82. tempStatesFile = flCombine(tempDir, "extended_states");
  83. Cnf.Set("Dir::State::extended_states", tempStatesFile);
  84. tempPrefsFile = flCombine(tempDir, "apt_preferences");
  85. Cnf.Set("Dir::Etc::preferences", tempPrefsFile);
  86. return true;
  87. }
  88. /*}}}*/
  89. // System::ArchiveSupported - Is a file format supported /*{{{*/
  90. bool edspLikeSystem::ArchiveSupported(const char * /*Type*/)
  91. {
  92. return false;
  93. }
  94. /*}}}*/
  95. // System::Score - Never use the EDSP system automatically /*{{{*/
  96. signed edspLikeSystem::Score(Configuration const &)
  97. {
  98. return -1000;
  99. }
  100. /*}}}*/
  101. // System::FindIndex - Get an index file for status files /*{{{*/
  102. bool edspLikeSystem::FindIndex(pkgCache::PkgFileIterator File,
  103. pkgIndexFile *&Found) const
  104. {
  105. if (StatusFile == 0)
  106. return false;
  107. if (StatusFile->FindInCache(*File.Cache()) == File)
  108. {
  109. Found = StatusFile.get();
  110. return true;
  111. }
  112. return false;
  113. }
  114. /*}}}*/
  115. bool edspSystem::AddStatusFiles(std::vector<pkgIndexFile *> &List) /*{{{*/
  116. {
  117. if (StatusFile == nullptr)
  118. {
  119. if (_config->Find("edsp::scenario", "") == "/nonexistent/stdin")
  120. StatusFile.reset(new edspIndex("/nonexistent/stdin"));
  121. else
  122. StatusFile.reset(new edspIndex(_config->FindFile("edsp::scenario")));
  123. }
  124. List.push_back(StatusFile.get());
  125. return true;
  126. }
  127. /*}}}*/
  128. edspLikeSystem::~edspLikeSystem() {}
  129. edspSystem::~edspSystem()
  130. {
  131. if (tempDir.empty())
  132. return;
  133. RemoveFile("~edspSystem", tempStatesFile);
  134. RemoveFile("~edspSystem", tempPrefsFile);
  135. rmdir(tempDir.c_str());
  136. }
  137. APT_HIDDEN edspSystem edspSys;