You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.cc 11 KiB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Init - Initialize the package library
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/configuration.h>
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/fileutl.h>
  12. #include <apt-pkg/init.h>
  13. #include <apt-pkg/macros.h>
  14. #include <apt-pkg/pkgsystem.h>
  15. #include <apt-pkg/strutl.h>
  16. #include <cstdlib>
  17. #include <fstream>
  18. #include <sstream>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <vector>
  22. #include <string.h>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. #define Stringfy_(x) # x
  26. #define Stringfy(x) Stringfy_(x)
  27. const char *pkgVersion = PACKAGE_VERSION;
  28. const char *pkgLibVersion = Stringfy(APT_PKG_MAJOR) "."
  29. Stringfy(APT_PKG_MINOR) "."
  30. Stringfy(APT_PKG_RELEASE);
  31. namespace APT {
  32. APT_HIDDEN extern std::unordered_map<std::string, std::vector<std::string>> ArchToTupleMap;
  33. }
  34. // Splits by whitespace. There may be continuous spans of whitespace - they
  35. // will be considered as one.
  36. static std::vector<std::string> split(std::string const & s)
  37. {
  38. std::vector<std::string> vec;
  39. std::istringstream iss(s);
  40. iss.imbue(std::locale::classic());
  41. for(std::string current_s; iss >> current_s; )
  42. vec.push_back(current_s);
  43. return vec;
  44. }
  45. // pkgInitArchTupleMap - Initialize the architecture tuple map /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* This initializes */
  48. static bool pkgInitArchTupleMap()
  49. {
  50. auto tuplepath = _config->FindFile("Dir::dpkg::tupletable", DPKG_DATADIR "/tupletable");
  51. auto tripletpath = _config->FindFile("Dir::dpkg::triplettable", DPKG_DATADIR "/triplettable");
  52. auto cpupath = _config->FindFile("Dir::dpkg::cputable", DPKG_DATADIR "/cputable");
  53. // Load a list of CPUs
  54. std::vector<std::string> cpus;
  55. std::ifstream cputable(cpupath);
  56. for (std::string cpuline; std::getline(cputable, cpuline); )
  57. {
  58. if (cpuline[0] == '#' || cpuline[0] == '\0')
  59. continue;
  60. auto cpurow = split(cpuline);
  61. auto cpu = APT::String::Strip(cpurow.at(0));
  62. cpus.push_back(cpu);
  63. }
  64. if (!cputable.eof())
  65. return _error->Error("Error reading the CPU table");
  66. // Load the architecture tuple
  67. std::ifstream tupletable;
  68. if (FileExists(tuplepath))
  69. tupletable.open(tuplepath);
  70. else if (FileExists(tripletpath))
  71. tupletable.open(tripletpath);
  72. else
  73. return _error->Error("Cannot find dpkg tuplet or triplet table");
  74. APT::ArchToTupleMap.clear();
  75. for (std::string tupleline; std::getline(tupletable, tupleline); )
  76. {
  77. if (tupleline[0] == '#' || tupleline[0] == '\0')
  78. continue;
  79. std::vector<std::string> tuplerow = split(tupleline);
  80. auto tuple = APT::String::Strip(tuplerow.at(0));
  81. auto arch = APT::String::Strip(tuplerow.at(1));
  82. if (tuple.find("<cpu>") == tuple.npos && arch.find("<cpu>") == arch.npos)
  83. {
  84. APT::ArchToTupleMap.insert({arch, VectorizeString(tuple, '-')});
  85. }
  86. else
  87. {
  88. for (auto && cpu : cpus)
  89. {
  90. auto mytuple = SubstVar(tuple, std::string("<cpu>"), cpu);
  91. auto myarch = SubstVar(arch, std::string("<cpu>"), cpu);
  92. APT::ArchToTupleMap.insert({myarch, VectorizeString(mytuple, '-')});
  93. }
  94. }
  95. }
  96. if (!tupletable.eof())
  97. return _error->Error("Error reading the Tuple table");
  98. return true;
  99. }
  100. /*}}}*/
  101. // pkgInitConfig - Initialize the configuration class /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* Directories are specified in such a way that the FindDir function will
  104. understand them. That is, if they don't start with a / then their parent
  105. is prepended, this allows a fair degree of flexibility. */
  106. bool pkgInitConfig(Configuration &Cnf)
  107. {
  108. // General APT things
  109. Cnf.CndSet("APT::Architecture", COMMON_ARCH);
  110. if (Cnf.Exists("APT::Build-Essential") == false)
  111. Cnf.Set("APT::Build-Essential::", "build-essential");
  112. Cnf.CndSet("APT::Install-Recommends", true);
  113. Cnf.CndSet("APT::Install-Suggests", false);
  114. Cnf.CndSet("Dir","/");
  115. // State
  116. Cnf.CndSet("Dir::State", STATE_DIR + 1);
  117. Cnf.CndSet("Dir::State::lists","lists/");
  118. Cnf.CndSet("Dir::State::cdroms","cdroms.list");
  119. // Cache
  120. Cnf.CndSet("Dir::Cache", CACHE_DIR + 1);
  121. Cnf.CndSet("Dir::Cache::archives","archives/");
  122. Cnf.CndSet("Dir::Cache::srcpkgcache","srcpkgcache.bin");
  123. Cnf.CndSet("Dir::Cache::pkgcache","pkgcache.bin");
  124. // Configuration
  125. Cnf.CndSet("Dir::Etc", CONF_DIR + 1);
  126. Cnf.CndSet("Dir::Etc::sourcelist","sources.list");
  127. Cnf.CndSet("Dir::Etc::sourceparts","sources.list.d");
  128. Cnf.CndSet("Dir::Etc::main","apt.conf");
  129. Cnf.CndSet("Dir::Etc::netrc", "auth.conf");
  130. Cnf.CndSet("Dir::Etc::parts","apt.conf.d");
  131. Cnf.CndSet("Dir::Etc::preferences","preferences");
  132. Cnf.CndSet("Dir::Etc::preferencesparts","preferences.d");
  133. Cnf.CndSet("Dir::Etc::trusted", "trusted.gpg");
  134. Cnf.CndSet("Dir::Etc::trustedparts","trusted.gpg.d");
  135. Cnf.CndSet("Dir::Bin::methods", LIBEXEC_DIR "/methods");
  136. Cnf.CndSet("Dir::Bin::solvers::",LIBEXEC_DIR "/solvers");
  137. Cnf.CndSet("Dir::Bin::planners::",LIBEXEC_DIR "/planners");
  138. Cnf.CndSet("Dir::Media::MountPath","/media/apt");
  139. // State
  140. Cnf.CndSet("Dir::Log", LOG_DIR + 1);
  141. Cnf.CndSet("Dir::Log::Terminal","term.log");
  142. Cnf.CndSet("Dir::Log::History","history.log");
  143. Cnf.CndSet("Dir::Log::Planner","eipp.log.xz");
  144. Cnf.Set("Dir::Ignore-Files-Silently::", "~$");
  145. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.disabled$");
  146. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.bak$");
  147. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.dpkg-[a-z]+$");
  148. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.ucf-[a-z]+$");
  149. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.save$");
  150. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.orig$");
  151. Cnf.Set("Dir::Ignore-Files-Silently::", "\\.distUpgrade$");
  152. // Repository security
  153. Cnf.CndSet("Acquire::AllowInsecureRepositories", false);
  154. Cnf.CndSet("Acquire::AllowWeakRepositories", false);
  155. Cnf.CndSet("Acquire::AllowDowngradeToInsecureRepositories", false);
  156. // Default cdrom mount point
  157. Cnf.CndSet("Acquire::cdrom::mount", "/media/cdrom/");
  158. // The default user we drop to in the methods
  159. Cnf.CndSet("APT::Sandbox::User", "_apt");
  160. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::MetaKey", "$(COMPONENT)/binary-$(ARCHITECTURE)/Packages");
  161. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::flatMetaKey", "Packages");
  162. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::ShortDescription", "Packages");
  163. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::Description", "$(RELEASE)/$(COMPONENT) $(ARCHITECTURE) Packages");
  164. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::flatDescription", "$(RELEASE) Packages");
  165. Cnf.CndSet("Acquire::IndexTargets::deb::Packages::Optional", false);
  166. Cnf.CndSet("Acquire::IndexTargets::deb::Translations::MetaKey", "$(COMPONENT)/i18n/Translation-$(LANGUAGE)");
  167. Cnf.CndSet("Acquire::IndexTargets::deb::Translations::flatMetaKey", "$(LANGUAGE)");
  168. Cnf.CndSet("Acquire::IndexTargets::deb::Translations::ShortDescription", "Translation-$(LANGUAGE)");
  169. Cnf.CndSet("Acquire::IndexTargets::deb::Translations::Description", "$(RELEASE)/$(COMPONENT) Translation-$(LANGUAGE)");
  170. Cnf.CndSet("Acquire::IndexTargets::deb::Translations::flatDescription", "$(RELEASE) Translation-$(LANGUAGE)");
  171. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::MetaKey", "$(COMPONENT)/source/Sources");
  172. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::flatMetaKey", "Sources");
  173. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::ShortDescription", "Sources");
  174. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::Description", "$(RELEASE)/$(COMPONENT) Sources");
  175. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::flatDescription", "$(RELEASE) Sources");
  176. Cnf.CndSet("Acquire::IndexTargets::deb-src::Sources::Optional", false);
  177. Cnf.CndSet("Acquire::Changelogs::URI::Origin::Debian", "http://metadata.ftp-master.debian.org/changelogs/@CHANGEPATH@_changelog");
  178. Cnf.CndSet("Acquire::Changelogs::URI::Origin::Tanglu", "http://metadata.tanglu.org/changelogs/@CHANGEPATH@_changelog");
  179. Cnf.CndSet("Acquire::Changelogs::URI::Origin::Ubuntu", "https://changelogs.ubuntu.com/changelogs/pool/@CHANGEPATH@/changelog");
  180. Cnf.CndSet("Acquire::Changelogs::URI::Origin::Ultimedia", "http://packages.ultimediaos.com/changelogs/pool/@CHANGEPATH@/changelog.txt");
  181. Cnf.CndSet("Acquire::Changelogs::AlwaysOnline::Origin::Ubuntu", true);
  182. // Read an alternate config file
  183. _error->PushToStack();
  184. const char *Cfg = getenv("APT_CONFIG");
  185. if (Cfg != 0 && strlen(Cfg) != 0)
  186. {
  187. if (RealFileExists(Cfg) == true)
  188. ReadConfigFile(Cnf, Cfg);
  189. else
  190. _error->WarningE("RealFileExists",_("Unable to read %s"),Cfg);
  191. }
  192. // Read the configuration parts dir
  193. std::string const Parts = Cnf.FindDir("Dir::Etc::parts", "/dev/null");
  194. if (DirectoryExists(Parts) == true)
  195. ReadConfigDir(Cnf, Parts);
  196. else if (APT::String::Endswith(Parts, "/dev/null") == false)
  197. _error->WarningE("DirectoryExists",_("Unable to read %s"),Parts.c_str());
  198. // Read the main config file
  199. std::string const FName = Cnf.FindFile("Dir::Etc::main", "/dev/null");
  200. if (RealFileExists(FName) == true)
  201. ReadConfigFile(Cnf, FName);
  202. if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
  203. Cnf.Dump();
  204. #ifdef APT_DOMAIN
  205. if (Cnf.Exists("Dir::Locale"))
  206. {
  207. bindtextdomain(APT_DOMAIN,Cnf.FindDir("Dir::Locale").c_str());
  208. bindtextdomain(textdomain(0),Cnf.FindDir("Dir::Locale").c_str());
  209. }
  210. #endif
  211. auto const good = _error->PendingError() == false;
  212. _error->MergeWithStack();
  213. return good;
  214. }
  215. /*}}}*/
  216. // pkgInitSystem - Initialize the _system class /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* */
  219. bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
  220. {
  221. Sys = 0;
  222. std::string Label = Cnf.Find("Apt::System","");
  223. if (Label.empty() == false)
  224. {
  225. Sys = pkgSystem::GetSystem(Label.c_str());
  226. if (Sys == 0)
  227. return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
  228. }
  229. else
  230. {
  231. signed MaxScore = 0;
  232. for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
  233. {
  234. signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
  235. if (Score > MaxScore)
  236. {
  237. MaxScore = Score;
  238. Sys = pkgSystem::GlobalList[I];
  239. }
  240. }
  241. if (Sys == 0)
  242. return _error->Error(_("Unable to determine a suitable packaging system type"));
  243. }
  244. if (pkgInitArchTupleMap() == false)
  245. return false;
  246. return Sys->Initialize(Cnf);
  247. }
  248. /*}}}*/