reorder includes: add <config.h> if needed and include it at first
[ntk/apt.git] / apt-pkg / sourcelist.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
34b53501 3// $Id: sourcelist.cc,v 1.3 2002/08/15 20:51:37 niemeyer Exp $
6c139d6e
AL
4/* ######################################################################
5
6 List of Sources
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
ea542140
DK
11#include<config.h>
12
094a497d
AL
13#include <apt-pkg/sourcelist.h>
14#include <apt-pkg/error.h>
15#include <apt-pkg/fileutl.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
7db98ffc 17#include <apt-pkg/configuration.h>
6c139d6e 18
90f057fd 19#include <fstream>
ea542140
DK
20
21#include <apti18n.h>
b2e465d6
AL
22 /*}}}*/
23
4d6f8bdf
AL
24using namespace std;
25
26// Global list of Items supported
b2e465d6
AL
27static pkgSourceList::Type *ItmList[10];
28pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
29unsigned long pkgSourceList::Type::GlobalListLen = 0;
30
31// Type::Type - Constructor /*{{{*/
32// ---------------------------------------------------------------------
33/* Link this to the global list of items*/
34pkgSourceList::Type::Type()
35{
36 ItmList[GlobalListLen] = this;
37 GlobalListLen++;
38}
39 /*}}}*/
40// Type::GetType - Get a specific meta for a given type /*{{{*/
41// ---------------------------------------------------------------------
42/* */
43pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
44{
45 for (unsigned I = 0; I != GlobalListLen; I++)
46 if (strcmp(GlobalList[I]->Name,Type) == 0)
47 return GlobalList[I];
48 return 0;
49}
50 /*}}}*/
51// Type::FixupURI - Normalize the URI and check it.. /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54bool pkgSourceList::Type::FixupURI(string &URI) const
55{
56 if (URI.empty() == true)
57 return false;
58
59 if (URI.find(':') == string::npos)
60 return false;
61
62 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
63
a7c835af 64 // Make sure that the URI is / postfixed
b2e465d6
AL
65 if (URI[URI.size() - 1] != '/')
66 URI += '/';
67
68 return true;
69}
70 /*}}}*/
71// Type::ParseLine - Parse a single line /*{{{*/
72// ---------------------------------------------------------------------
73/* This is a generic one that is the 'usual' format for sources.list
74 Weird types may override this. */
7db98ffc 75bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
b2e465d6 76 const char *Buffer,
5dd4c8b8
DK
77 unsigned long const &CurLine,
78 string const &File) const
b2e465d6 79{
5dd4c8b8
DK
80 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
81
82 // Parse option field if it exists
83 // e.g.: [ option1=value1 option2=value2 ]
84 map<string, string> Options;
85 if (Buffer != 0 && Buffer[0] == '[')
86 {
87 ++Buffer; // ignore the [
88 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
89 while (*Buffer != ']')
90 {
91 // get one option, e.g. option1=value1
92 string option;
93 if (ParseQuoteWord(Buffer,option) == false)
94 return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str());
95
96 if (option.length() < 3)
97 return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str());
98
6838dd87
DK
99 // accept options even if the last has no space before the ]-end marker
100 if (option.at(option.length()-1) == ']')
101 {
102 for (; *Buffer != ']'; --Buffer);
103 option.resize(option.length()-1);
104 }
105
5dd4c8b8
DK
106 size_t const needle = option.find('=');
107 if (needle == string::npos)
108 return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str());
109
110 string const key = string(option, 0, needle);
111 string const value = string(option, needle + 1, option.length());
112
113 if (key.empty() == true)
114 return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str());
115
116 if (value.empty() == true)
117 return _error->Error(_("Malformed line %lu in source list %s ([%s] key %s has no value)"),CurLine,File.c_str(),option.c_str(),key.c_str());
118
119 Options[key] = value;
120 }
121 ++Buffer; // ignore the ]
122 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
123 }
124
b2e465d6
AL
125 string URI;
126 string Dist;
5dd4c8b8
DK
127 string Section;
128
b2e465d6
AL
129 if (ParseQuoteWord(Buffer,URI) == false)
130 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
131 if (ParseQuoteWord(Buffer,Dist) == false)
132 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
133
134 if (FixupURI(URI) == false)
135 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
136
137 // Check for an absolute dists specification.
138 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
139 {
140 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 141 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 142 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
5dd4c8b8 143 return CreateItem(List, URI, Dist, Section, Options);
b2e465d6
AL
144 }
145
146 // Grab the rest of the dists
147 if (ParseQuoteWord(Buffer,Section) == false)
148 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
149
150 do
151 {
5dd4c8b8 152 if (CreateItem(List, URI, Dist, Section, Options) == false)
b2e465d6
AL
153 return false;
154 }
155 while (ParseQuoteWord(Buffer,Section) == true);
156
157 return true;
158}
6c139d6e
AL
159 /*}}}*/
160
161// SourceList::pkgSourceList - Constructors /*{{{*/
162// ---------------------------------------------------------------------
163/* */
164pkgSourceList::pkgSourceList()
165{
166}
167
168pkgSourceList::pkgSourceList(string File)
169{
170 Read(File);
171}
172 /*}}}*/
1c193e02
AL
173// SourceList::~pkgSourceList - Destructor /*{{{*/
174// ---------------------------------------------------------------------
175/* */
176pkgSourceList::~pkgSourceList()
177{
178 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
179 delete *I;
1c193e02
AL
180}
181 /*}}}*/
a7c835af 182 /*}}}*/
6c139d6e
AL
183// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
184// ---------------------------------------------------------------------
185/* */
186bool pkgSourceList::ReadMainList()
187{
34b53501
MV
188 // CNC:2003-03-03 - Multiple sources list support.
189 bool Res = true;
190#if 0
191 Res = ReadVendors();
192 if (Res == false)
193 return false;
194#endif
195
196 Reset();
197 // CNC:2003-11-28 - Entries in sources.list have priority over
198 // entries in sources.list.d.
199 string Main = _config->FindFile("Dir::Etc::sourcelist");
67793cf3
JAK
200 string Parts = _config->FindDir("Dir::Etc::sourceparts");
201
36f1098a 202 if (RealFileExists(Main) == true)
6009e60d 203 Res &= ReadAppend(Main);
448eaf8b 204 else if (DirectoryExists(Parts) == false)
67793cf3 205 // Only warn if there are no sources.list.d.
448eaf8b 206 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
34b53501 207
448eaf8b 208 if (DirectoryExists(Parts) == true)
34b53501 209 Res &= ReadSourceDir(Parts);
36f1098a 210 else if (RealFileExists(Main) == false)
67793cf3 211 // Only warn if there is no sources.list file.
36f1098a 212 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
6009e60d 213
34b53501 214 return Res;
6c139d6e
AL
215}
216 /*}}}*/
34b53501
MV
217// CNC:2003-03-03 - Needed to preserve backwards compatibility.
218// SourceList::Reset - Clear the sourcelist contents /*{{{*/
219// ---------------------------------------------------------------------
220/* */
221void pkgSourceList::Reset()
222{
223 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
224 delete *I;
225 SrcList.erase(SrcList.begin(),SrcList.end());
226}
227 /*}}}*/
228// CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
6c139d6e
AL
229// SourceList::Read - Parse the sourcelist file /*{{{*/
230// ---------------------------------------------------------------------
231/* */
232bool pkgSourceList::Read(string File)
34b53501
MV
233{
234 Reset();
235 return ReadAppend(File);
236}
237 /*}}}*/
238// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
239// ---------------------------------------------------------------------
240/* */
241bool pkgSourceList::ReadAppend(string File)
6c139d6e
AL
242{
243 // Open the stream for reading
4d6f8bdf 244 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 245 if (!F != 0)
b2e465d6 246 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
6c139d6e 247
34b53501 248#if 0 // Now Reset() does this.
1c193e02
AL
249 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
250 delete *I;
a7c835af 251 SrcList.erase(SrcList.begin(),SrcList.end());
34b53501
MV
252#endif
253 // CNC:2003-12-10 - 300 is too short.
254 char Buffer[1024];
6c139d6e
AL
255
256 int CurLine = 0;
257 while (F.eof() == false)
258 {
259 F.getline(Buffer,sizeof(Buffer));
260 CurLine++;
261 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
262 if (F.fail() && !F.eof())
263 return _error->Error(_("Line %u too long in source list %s."),
264 CurLine,File.c_str());
265
b2e465d6
AL
266
267 char *I;
34b53501
MV
268 // CNC:2003-02-20 - Do not break if '#' is inside [].
269 for (I = Buffer; *I != 0 && *I != '#'; I++)
270 if (*I == '[')
271 for (I++; *I != 0 && *I != ']'; I++);
b2e465d6
AL
272 *I = 0;
273
274 const char *C = _strstrip(Buffer);
6c139d6e
AL
275
276 // Comment or blank
b2e465d6 277 if (C[0] == '#' || C[0] == 0)
6c139d6e 278 continue;
b2e465d6 279
6c139d6e 280 // Grok it
b2e465d6
AL
281 string LineType;
282 if (ParseQuoteWord(C,LineType) == false)
283 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 284
b2e465d6
AL
285 Type *Parse = Type::GetType(LineType.c_str());
286 if (Parse == 0)
39442e44 287 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 288
5dd4c8b8 289 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
b2e465d6 290 return false;
6c139d6e
AL
291 }
292 return true;
293}
294 /*}}}*/
b2e465d6 295// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
296// ---------------------------------------------------------------------
297/* */
b2e465d6
AL
298bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
299 pkgIndexFile *&Found) const
6c139d6e 300{
a7c835af 301 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
6c139d6e 302 {
7db98ffc
MZ
303 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
304 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
305 J != Indexes->end(); J++)
b2e465d6 306 {
7db98ffc
MZ
307 if ((*J)->FindInCache(*File.Cache()) == File)
308 {
309 Found = (*J);
310 return true;
311 }
b2e465d6 312 }
be8922fd 313 }
7db98ffc 314
b2e465d6 315 return false;
36375005
AL
316}
317 /*}}}*/
b2e465d6 318// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
319// ---------------------------------------------------------------------
320/* */
7db98ffc 321bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 322{
a7c835af 323 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
7db98ffc 324 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
325 return false;
326 return true;
6c139d6e
AL
327}
328 /*}}}*/
34b53501
MV
329// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
330// SourceList::ReadSourceDir - Read a directory with sources files
331// Based on ReadConfigDir() /*{{{*/
332// ---------------------------------------------------------------------
333/* */
334bool pkgSourceList::ReadSourceDir(string Dir)
335{
52643bec 336 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
337
338 // Read the files
339 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
340 if (ReadAppend(*I) == false)
341 return false;
342 return true;
343
344}
345 /*}}}*/
346