- add method to find a package by name from rpmdb
[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               
81    bool Res = true;
82    
83    // Read an alternate config file
84    const char *Cfg = getenv("APT_CONFIG");
85    if (Cfg != 0 && FileExists(Cfg) == true)
86       Res &= ReadConfigFile(Cnf,Cfg);
87    
88    // Read the configuration parts dir
89    string Parts = Cnf.FindDir("Dir::Etc::parts");
90    if (FileExists(Parts) == true)
91       Res &= ReadConfigDir(Cnf,Parts);
92       
93    // Read the main config file
94    string FName = Cnf.FindFile("Dir::Etc::main");
95    if (FileExists(FName) == true)
96       Res &= ReadConfigFile(Cnf,FName);
97    
98    if (Res == false)
99       return false;
100    
101    if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
102       Cnf.Dump();
103    
104 #ifdef APT_DOMAIN
105    if (Cnf.Exists("Dir::Locale"))
106    {  
107       bindtextdomain(APT_DOMAIN,Cnf.FindDir("Dir::Locale").c_str());
108       bindtextdomain(textdomain(0),Cnf.FindDir("Dir::Locale").c_str());
109    }
110 #endif
111    
112    return true;
113 }
114                                                                         /*}}}*/
115
116 // CNC:2003-02-16 - We must do that to force a statically linked libapt-pkg
117 //                  library to include the package systems into the binary.
118 #include <apt-pkg/rpmsystem.h>
119 //#include <apt-pkg/debsystem.h>
120 void ForceLinkage()
121 {
122         rpmSystem *rpmsys = &rpmSys;
123         rpmsys->ArchiveSupported("");
124         //debSystem *debsys = &debSys;
125         //depsys->ArchiveSupported("");
126 }
127
128 // pkgInitSystem - Initialize the _system calss                         /*{{{*/
129 // ---------------------------------------------------------------------
130 /* */
131 bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
132 {
133    ForceLinkage(); // CNC:2003-02-16 - See above.
134
135    Sys = 0;
136    string Label = Cnf.Find("Apt::System","");
137    if (Label.empty() == false)
138    {
139       Sys = pkgSystem::GetSystem(Label.c_str());
140       if (Sys == 0)
141          return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
142    }
143    else
144    {
145       signed MaxScore = 0;
146       for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
147       {
148          signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
149          if (Score > MaxScore)
150          {
151             MaxScore = Score;
152             Sys = pkgSystem::GlobalList[I];
153          }
154       }
155       
156       if (Sys == 0)
157          return _error->Error(_("Unable to determine a suitable system type"));
158    }
159
160    // CNC:2003-03-15
161    bool Ret = Sys->Initialize(Cnf);
162    // LORG:2006-02-06
163    Cnf.CndSet("APT::DistroVersion", Sys->DistroVer());
164
165 #ifdef WITH_LUA
166    _lua->RunScripts("Scripts::Init");
167 #endif
168    return Ret;
169 }
170                                                                         /*}}}*/