* fix http data corruption (#280844)
[ntk/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
20 #define Stringfy_(x) # x
21 #define Stringfy(x) Stringfy_(x)
22 const char *pkgVersion = VERSION;
23 const char *pkgLibVersion = Stringfy(APT_PKG_MAJOR) "."
24 Stringfy(APT_PKG_MINOR) "."
25 Stringfy(APT_PKG_RELEASE);
26 const char *pkgCPU = COMMON_CPU;
27 const char *pkgOS = COMMON_OS;
28
29 // pkgInitConfig - Initialize the configuration class /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Directories are specified in such a way that the FindDir function will
32 understand them. That is, if they don't start with a / then their parent
33 is prepended, this allows a fair degree of flexability. */
34 bool pkgInitConfig(Configuration &Cnf)
35 {
36 // General APT things
37 if (strcmp(COMMON_OS,"linux") == 0 ||
38 strcmp(COMMON_OS,"unknown") == 0)
39 Cnf.Set("APT::Architecture",COMMON_CPU);
40 else
41 Cnf.Set("APT::Architecture",COMMON_OS "-" COMMON_CPU);
42 Cnf.Set("APT::Build-Essential::", "build-essential");
43 Cnf.Set("Dir","/");
44
45 // State
46 Cnf.Set("Dir::State","var/lib/apt/");
47
48 /* Just in case something goes horribly wrong, we can fall back to the
49 old /var/state paths.. */
50 struct stat St;
51 if (stat("/var/lib/apt/.",&St) != 0 &&
52 stat("/var/state/apt/.",&St) == 0)
53 Cnf.Set("Dir::State","var/state/apt/");
54
55 Cnf.Set("Dir::State::lists","lists/");
56 Cnf.Set("Dir::State::cdroms","cdroms.list");
57
58 // Cache
59 Cnf.Set("Dir::Cache","var/cache/apt/");
60 Cnf.Set("Dir::Cache::archives","archives/");
61 Cnf.Set("Dir::Cache::srcpkgcache","srcpkgcache.bin");
62 Cnf.Set("Dir::Cache::pkgcache","pkgcache.bin");
63
64 // Configuration
65 Cnf.Set("Dir::Etc","etc/apt/");
66 Cnf.Set("Dir::Etc::sourcelist","sources.list");
67 Cnf.Set("Dir::Etc::sourceparts","sources.list.d");
68 Cnf.Set("Dir::Etc::vendorlist","vendors.list");
69 Cnf.Set("Dir::Etc::vendorparts","vendors.list.d");
70 Cnf.Set("Dir::Etc::main","apt.conf");
71 Cnf.Set("Dir::Etc::parts","apt.conf.d");
72 Cnf.Set("Dir::Etc::preferences","preferences");
73 Cnf.Set("Dir::Bin::methods","/usr/lib/apt/methods");
74
75 bool Res = true;
76
77 // Read an alternate config file
78 const char *Cfg = getenv("APT_CONFIG");
79 if (Cfg != 0 && FileExists(Cfg) == true)
80 Res &= ReadConfigFile(Cnf,Cfg);
81
82 // Read the configuration parts dir
83 string Parts = Cnf.FindDir("Dir::Etc::parts");
84 if (FileExists(Parts) == true)
85 Res &= ReadConfigDir(Cnf,Parts);
86
87 // Read the main config file
88 string FName = Cnf.FindFile("Dir::Etc::main");
89 if (FileExists(FName) == true)
90 Res &= ReadConfigFile(Cnf,FName);
91
92 if (Res == false)
93 return false;
94
95 if (Cnf.FindB("Debug::pkgInitConfig",false) == true)
96 Cnf.Dump();
97
98 #ifdef APT_DOMAIN
99 if (Cnf.Exists("Dir::Locale"))
100 {
101 bindtextdomain(APT_DOMAIN,Cnf.FindDir("Dir::Locale").c_str());
102 bindtextdomain(textdomain(0),Cnf.FindDir("Dir::Locale").c_str());
103 }
104 #endif
105
106 return true;
107 }
108 /*}}}*/
109 // pkgInitSystem - Initialize the _system calss /*{{{*/
110 // ---------------------------------------------------------------------
111 /* */
112 bool pkgInitSystem(Configuration &Cnf,pkgSystem *&Sys)
113 {
114 Sys = 0;
115 string Label = Cnf.Find("Apt::System","");
116 if (Label.empty() == false)
117 {
118 Sys = pkgSystem::GetSystem(Label.c_str());
119 if (Sys == 0)
120 return _error->Error(_("Packaging system '%s' is not supported"),Label.c_str());
121 }
122 else
123 {
124 signed MaxScore = 0;
125 for (unsigned I = 0; I != pkgSystem::GlobalListLen; I++)
126 {
127 signed Score = pkgSystem::GlobalList[I]->Score(Cnf);
128 if (Score > MaxScore)
129 {
130 MaxScore = Score;
131 Sys = pkgSystem::GlobalList[I];
132 }
133 }
134
135 if (Sys == 0)
136 return _error->Error(_("Unable to determine a suitable packaging system type"));
137 }
138
139 return Sys->Initialize(Cnf);
140 }
141 /*}}}*/