init.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: init.cc,v 1.21 2004/02/27 00:46:44 mdz Exp $
  4. /* ######################################################################
  5. Init - Initialize the package library
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include files /*{{{*/
  9. #include <apt-pkg/init.h>
  10. #include <apt-pkg/fileutl.h>
  11. #include <apt-pkg/error.h>
  12. #include <apti18n.h>
  13. #include <config.h>
  14. #include <sys/stat.h>
  15. /*}}}*/
  16. #define Stringfy_(x) # x
  17. #define Stringfy(x) Stringfy_(x)
  18. const char *pkgVersion = VERSION;
  19. const char *pkgLibVersion = Stringfy(APT_PKG_MAJOR) "."
  20. Stringfy(APT_PKG_MINOR) "."
  21. Stringfy(APT_PKG_RELEASE);
  22. const char *pkgCPU = COMMON_CPU;
  23. const char *pkgOS = COMMON_OS;
  24. // pkgInitConfig - Initialize the configuration class /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* Directories are specified in such a way that the FindDir function will
  27. understand them. That is, if they don't start with a / then their parent
  28. is prepended, this allows a fair degree of flexability. */
  29. bool pkgInitConfig(Configuration &Cnf)
  30. {
  31. // General APT things
  32. if (strcmp(COMMON_OS,"linux") == 0 ||
  33. strcmp(COMMON_OS,"unknown") == 0)
  34. Cnf.Set("APT::Architecture",COMMON_CPU);
  35. else
  36. Cnf.Set("APT::Architecture",COMMON_OS "-" COMMON_CPU);
  37. Cnf.Set("APT::Build-Essential::", "build-essential");
  38. Cnf.Set("Dir","/");
  39. // State
  40. Cnf.Set("Dir::State","var/lib/apt/");
  41. /* Just in case something goes horribly wrong, we can fall back to the
  42. old /var/state paths.. */
  43. struct stat St;
  44. if (stat("/var/lib/apt/.",&St) != 0 &&
  45. stat("/var/state/apt/.",&St) == 0)
  46. Cnf.Set("Dir::State","var/state/apt/");
  47. Cnf.Set("Dir::State::lists","lists/");
  48. Cnf.Set("Dir::State::cdroms","cdroms.list");
  49. // Cache
  50. Cnf.Set("Dir::Cache","var/cache/apt/");
  51. Cnf.Set("Dir::Cache::archives","archives/");
  52. Cnf.Set("Dir::Cache::srcpkgcache","srcpkgcache.bin");
  53. Cnf.Set("Dir::Cache::pkgcache","pkgcache.bin");
  54. // Configuration
  55. Cnf.Set("Dir::Etc","etc/apt/");
  56. Cnf.Set("Dir::Etc::sourcelist","sources.list");
  57. Cnf.Set("Dir::Etc::vendorlist","vendors.list");
  58. Cnf.Set("Dir::Etc::vendorparts","vendors.list.d");
  59. Cnf.Set("Dir::Etc::main","apt.conf");
  60. Cnf.Set("Dir::Etc::parts","apt.conf.d");
  61. Cnf.Set("Dir::Etc::preferences","preferences");
  62. Cnf.Set("Dir::Bin::methods","/usr/lib/apt/methods");
  63. bool Res = true;
  64. // Read an alternate config file
  65. const char *Cfg = getenv("APT_CONFIG");
  66. if (Cfg != 0 && FileExists(Cfg) == true)
  67. Res &= ReadConfigFile(Cnf,Cfg);
  68. // Read the configuration parts dir
  69. string Parts = Cnf.FindDir("Dir::Etc::parts");
  70. if (FileExists(Parts) == true)
  71. Res &= ReadConfigDir(Cnf,Parts);
  72. // Read the main config file
  73. string FName = Cnf.FindFile("Dir::Etc::main");
  74. if (FileExists(FName) == true)
  75. Res &= ReadConfigFile(Cnf,FName);
  76. if (Res == false)
  77. return false;
  78. if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
  79. Cnf.Dump();
  80. #ifdef APT_DOMAIN
  81. if (Cnf.Exists("Dir::Locale"))
  82. {
  83. bindtextdomain(APT_DOMAIN,Cnf.FindDir("Dir::Locale").c_str());
  84. bindtextdomain(textdomain(0),Cnf.FindDir("Dir::Locale").c_str());
  85. }
  86. #endif
  87. return true;
  88. }
  89. /*}}}*/
  90. // pkgInitSystem - Initialize the _system calss /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* */
  93. bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
  94. {
  95. Sys = 0;
  96. string Label = Cnf.Find("Apt::System","");
  97. if (Label.empty() == false)
  98. {
  99. Sys = pkgSystem::GetSystem(Label.c_str());
  100. if (Sys == 0)
  101. return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
  102. }
  103. else
  104. {
  105. signed MaxScore = 0;
  106. for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
  107. {
  108. signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
  109. if (Score > MaxScore)
  110. {
  111. MaxScore = Score;
  112. Sys = pkgSystem::GlobalList[I];
  113. }
  114. }
  115. if (Sys == 0)
  116. return _error->Error(_("Unable to determine a suitable packaging system type"));
  117. }
  118. return Sys->Initialize(Cnf);
  119. }
  120. /*}}}*/