- add support for yum-style distroverpkg package whose version can be
[apt.git] / apt-pkg / init.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: init.cc,v 1.20 2003/02/09 20:31:05 doogie Exp $
4 /* ######################################################################
5
6    Init - Initialize the package library
7    
8    ##################################################################### */
9                                                                         /*}}}*/
10 // Include files                                                        /*{{{*/
11 #include <apt-pkg/init.h>
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/error.h>
14
15 #include <apti18n.h>
16 #include <config.h>
17 #include <sys/stat.h>
18
19 // CNC:2003-03-17
20 #include <apt-pkg/luaiface.h>
21
22                                                                         /*}}}*/
23
24 #define Stringfy_(x) # x
25 #define Stringfy(x)  Stringfy_(x)
26 const char *pkgVersion = VERSION;
27 const char *pkgLibVersion = Stringfy(APT_PKG_MAJOR) "."
28                             Stringfy(APT_PKG_MINOR) "." 
29                             Stringfy(APT_PKG_RELEASE);
30 const char *pkgCPU = COMMON_CPU;
31 const char *pkgOS = COMMON_OS;
32     
33 // pkgInitConfig - Initialize the configuration class                   /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Directories are specified in such a way that the FindDir function will
36    understand them. That is, if they don't start with a / then their parent
37    is prepended, this allows a fair degree of flexability. */
38 bool pkgInitConfig(Configuration &Cnf)
39 {
40    // General APT things
41    if (strcmp(COMMON_OS,"linux") == 0 ||
42        strcmp(COMMON_OS,"unknown") == 0)
43       Cnf.Set("APT::Architecture",COMMON_CPU);
44    else
45       Cnf.Set("APT::Architecture",COMMON_OS "-" COMMON_CPU);
46    // CNC:2002-09-10
47    //Cnf.Set("APT::Build-Essential::", "build-essential");
48    Cnf.Set("Dir","/");
49    
50    // State   
51    Cnf.Set("Dir::State","var/lib/apt/");
52    
53    /* Just in case something goes horribly wrong, we can fall back to the
54       old /var/state paths.. */
55    struct stat St;   
56    if (stat("/var/lib/apt/.",&St) != 0 &&
57        stat("/var/state/apt/.",&St) == 0)
58       Cnf.Set("Dir::State","var/state/apt/");
59        
60    Cnf.Set("Dir::State::lists","lists/");
61    Cnf.Set("Dir::State::cdroms","cdroms.list");
62    
63    // Cache
64    Cnf.Set("Dir::Cache","var/cache/apt/");
65    Cnf.Set("Dir::Cache::archives","archives/");
66    Cnf.Set("Dir::Cache::srcpkgcache","srcpkgcache.bin");
67    Cnf.Set("Dir::Cache::pkgcache","pkgcache.bin");
68    
69    // Configuration
70    Cnf.Set("Dir::Etc","etc/apt/");
71    Cnf.Set("Dir::Etc::sourcelist","sources.list");
72    // CNC:2003-03-03
73    Cnf.Set("Dir::Etc::sourceparts","sources.list.d");
74    Cnf.Set("Dir::Etc::vendorlist","vendors.list");
75    Cnf.Set("Dir::Etc::vendorparts","vendors.list.d");
76    Cnf.Set("Dir::Etc::main","apt.conf");
77    Cnf.Set("Dir::Etc::parts","apt.conf.d");
78    Cnf.Set("Dir::Etc::preferences","preferences");
79    Cnf.Set("Dir::Bin::methods",LIBDIR "/apt/methods");
80    Cnf.Set("Acquire::ComprExtension", ".bz2");
81               
82    bool Res = true;
83    
84    // Read an alternate config file
85    const char *Cfg = getenv("APT_CONFIG");
86    if (Cfg != 0 && FileExists(Cfg) == true)
87       Res &= ReadConfigFile(Cnf,Cfg);
88    
89    // Read the configuration parts dir
90    string Parts = Cnf.FindDir("Dir::Etc::parts");
91    if (FileExists(Parts) == true)
92       Res &= ReadConfigDir(Cnf,Parts);
93       
94    // Read the main config file
95    string FName = Cnf.FindFile("Dir::Etc::main");
96    if (FileExists(FName) == true)
97       Res &= ReadConfigFile(Cnf,FName);
98    
99    if (Res == false)
100       return false;
101    
102    if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
103       Cnf.Dump();
104    
105 #ifdef APT_DOMAIN
106    if (Cnf.Exists("Dir::Locale"))
107    {  
108       bindtextdomain(APT_DOMAIN,Cnf.FindDir("Dir::Locale").c_str());
109       bindtextdomain(textdomain(0),Cnf.FindDir("Dir::Locale").c_str());
110    }
111 #endif
112    
113    return true;
114 }
115                                                                         /*}}}*/
116
117 // CNC:2003-02-16 - We must do that to force a statically linked libapt-pkg
118 //                  library to include the package systems into the binary.
119 #include <apt-pkg/rpmsystem.h>
120 //#include <apt-pkg/debsystem.h>
121 void ForceLinkage()
122 {
123         rpmSystem *rpmsys = &rpmSys;
124         rpmsys->ArchiveSupported("");
125         //debSystem *debsys = &debSys;
126         //depsys->ArchiveSupported("");
127 }
128
129 // pkgInitSystem - Initialize the _system calss                         /*{{{*/
130 // ---------------------------------------------------------------------
131 /* */
132 bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
133 {
134    ForceLinkage(); // CNC:2003-02-16 - See above.
135
136    Sys = 0;
137    string Label = Cnf.Find("Apt::System","");
138    if (Label.empty() == false)
139    {
140       Sys = pkgSystem::GetSystem(Label.c_str());
141       if (Sys == 0)
142          return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
143    }
144    else
145    {
146       signed MaxScore = 0;
147       for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
148       {
149          signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
150          if (Score > MaxScore)
151          {
152             MaxScore = Score;
153             Sys = pkgSystem::GlobalList[I];
154          }
155       }
156       
157       if (Sys == 0)
158          return _error->Error(_("Unable to determine a suitable system type"));
159    }
160
161    // PM:2006-02-06
162    if (Cnf.Find("APT::DistroVersion", "").empty()) {
163         Cnf.Set("APT::DistroVersion", Sys->DistroVer(Cnf));
164    }
165
166    // CNC:2003-03-15
167    bool Ret = Sys->Initialize(Cnf);
168 #ifdef WITH_LUA
169    _lua->RunScripts("Scripts::Init");
170 #endif
171    return Ret;
172 }
173                                                                         /*}}}*/