Merge remote-tracking branch 'mvo/debian/sid' into debian/sid
[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>
472ff00e
DK
18#include <apt-pkg/metaindex.h>
19#include <apt-pkg/indexfile.h>
a537ce19 20#include <apt-pkg/tagfile.h>
6c139d6e 21
90f057fd 22#include <fstream>
41e6bd08 23#include <algorithm>
ea542140
DK
24
25#include <apti18n.h>
b2e465d6
AL
26 /*}}}*/
27
4d6f8bdf
AL
28using namespace std;
29
30// Global list of Items supported
b2e465d6
AL
31static pkgSourceList::Type *ItmList[10];
32pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
33unsigned long pkgSourceList::Type::GlobalListLen = 0;
34
35// Type::Type - Constructor /*{{{*/
36// ---------------------------------------------------------------------
37/* Link this to the global list of items*/
dcaa1185 38pkgSourceList::Type::Type() : Name(NULL), Label(NULL)
b2e465d6
AL
39{
40 ItmList[GlobalListLen] = this;
41 GlobalListLen++;
42}
43 /*}}}*/
44// Type::GetType - Get a specific meta for a given type /*{{{*/
45// ---------------------------------------------------------------------
46/* */
47pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
48{
49 for (unsigned I = 0; I != GlobalListLen; I++)
50 if (strcmp(GlobalList[I]->Name,Type) == 0)
51 return GlobalList[I];
52 return 0;
53}
54 /*}}}*/
55// Type::FixupURI - Normalize the URI and check it.. /*{{{*/
56// ---------------------------------------------------------------------
57/* */
58bool pkgSourceList::Type::FixupURI(string &URI) const
59{
60 if (URI.empty() == true)
61 return false;
62
63 if (URI.find(':') == string::npos)
64 return false;
65
66 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
67
a7c835af 68 // Make sure that the URI is / postfixed
b2e465d6
AL
69 if (URI[URI.size() - 1] != '/')
70 URI += '/';
71
72 return true;
73}
74 /*}}}*/
7037aab5
MV
75bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List,
76 pkgTagSection &Tags,
77 int i,
78 FileFd &Fd)
79{
80 map<string, string> Options;
81
7dd62ea9
MV
82 string Enabled = Tags.FindS("Enabled");
83 if (Enabled.size() > 0 && StringToBool(Enabled) == false)
84 return true;
7037aab5 85
7037aab5
MV
86 // Define external/internal options
87 const char* option_deb822[] = {
866e9fad 88 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
7037aab5
MV
89 };
90 const char* option_internal[] = {
91 "arch", "arch+", "arch-", "trusted",
92 };
93 for (unsigned int j=0; j < sizeof(option_deb822)/sizeof(char*); j++)
94 if (Tags.Exists(option_deb822[j]))
41e6bd08
MV
95 {
96 // for deb822 the " " is the delimiter, but the backend expects ","
97 std::string option = Tags.FindS(option_deb822[j]);
98 std::replace(option.begin(), option.end(), ' ', ',');
99 Options[option_internal[j]] = option;
100 }
7037aab5 101
d73743dd 102 // now create one item per suite/section
6c069a22 103 string Suite = Tags.FindS("Suites");
d73743dd 104 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
6c069a22 105 string const Section = Tags.FindS("Sections");
75c10df1 106 string URIS = Tags.FindS("URIs");
d73743dd 107
75c10df1 108 std::vector<std::string> list_uris = StringSplit(URIS, " ");
d73743dd
MV
109 std::vector<std::string> list_dist = StringSplit(Suite, " ");
110 std::vector<std::string> list_section = StringSplit(Section, " ");
75c10df1
MV
111
112 for (std::vector<std::string>::const_iterator U = list_uris.begin();
113 U != list_uris.end(); U++)
d73743dd 114 {
75c10df1
MV
115 std::string URI = (*U);
116 if (!FixupURI(URI))
117 {
118 _error->Error(_("Malformed stanza %u in source list %s (URI parse)"),i,Fd.Name().c_str());
119 return false;
120 }
121
122 for (std::vector<std::string>::const_iterator I = list_dist.begin();
123 I != list_dist.end(); I++)
124 {
125 for (std::vector<std::string>::const_iterator J = list_section.begin();
126 J != list_section.end(); J++)
d73743dd
MV
127 {
128 if (CreateItem(List, URI, (*I), (*J), Options) == false)
129 {
130 return false;
131 }
132 }
75c10df1 133 }
d73743dd 134 }
7037aab5
MV
135 return true;
136}
137
b2e465d6
AL
138// Type::ParseLine - Parse a single line /*{{{*/
139// ---------------------------------------------------------------------
140/* This is a generic one that is the 'usual' format for sources.list
141 Weird types may override this. */
7db98ffc 142bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
b2e465d6 143 const char *Buffer,
5dd4c8b8
DK
144 unsigned long const &CurLine,
145 string const &File) const
b2e465d6 146{
5dd4c8b8
DK
147 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
148
149 // Parse option field if it exists
150 // e.g.: [ option1=value1 option2=value2 ]
151 map<string, string> Options;
152 if (Buffer != 0 && Buffer[0] == '[')
153 {
154 ++Buffer; // ignore the [
155 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
156 while (*Buffer != ']')
157 {
158 // get one option, e.g. option1=value1
159 string option;
160 if (ParseQuoteWord(Buffer,option) == false)
161 return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str());
162
163 if (option.length() < 3)
164 return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str());
165
6838dd87
DK
166 // accept options even if the last has no space before the ]-end marker
167 if (option.at(option.length()-1) == ']')
168 {
169 for (; *Buffer != ']'; --Buffer);
170 option.resize(option.length()-1);
171 }
172
5dd4c8b8
DK
173 size_t const needle = option.find('=');
174 if (needle == string::npos)
175 return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str());
176
177 string const key = string(option, 0, needle);
178 string const value = string(option, needle + 1, option.length());
179
180 if (key.empty() == true)
181 return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str());
182
183 if (value.empty() == true)
184 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());
185
186 Options[key] = value;
187 }
188 ++Buffer; // ignore the ]
189 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
190 }
191
b2e465d6
AL
192 string URI;
193 string Dist;
5dd4c8b8
DK
194 string Section;
195
b2e465d6
AL
196 if (ParseQuoteWord(Buffer,URI) == false)
197 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
198 if (ParseQuoteWord(Buffer,Dist) == false)
199 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
200
201 if (FixupURI(URI) == false)
202 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
203
204 // Check for an absolute dists specification.
205 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
206 {
207 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 208 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 209 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
5dd4c8b8 210 return CreateItem(List, URI, Dist, Section, Options);
b2e465d6
AL
211 }
212
213 // Grab the rest of the dists
214 if (ParseQuoteWord(Buffer,Section) == false)
215 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
216
217 do
218 {
5dd4c8b8 219 if (CreateItem(List, URI, Dist, Section, Options) == false)
b2e465d6
AL
220 return false;
221 }
222 while (ParseQuoteWord(Buffer,Section) == true);
223
224 return true;
225}
6c139d6e 226 /*}}}*/
6c139d6e
AL
227// SourceList::pkgSourceList - Constructors /*{{{*/
228// ---------------------------------------------------------------------
229/* */
230pkgSourceList::pkgSourceList()
231{
232}
233
234pkgSourceList::pkgSourceList(string File)
235{
236 Read(File);
237}
238 /*}}}*/
1c193e02
AL
239// SourceList::~pkgSourceList - Destructor /*{{{*/
240// ---------------------------------------------------------------------
241/* */
242pkgSourceList::~pkgSourceList()
243{
f7f0d6c7 244 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
1c193e02 245 delete *I;
1c193e02
AL
246}
247 /*}}}*/
6c139d6e
AL
248// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
249// ---------------------------------------------------------------------
250/* */
251bool pkgSourceList::ReadMainList()
252{
34b53501
MV
253 // CNC:2003-03-03 - Multiple sources list support.
254 bool Res = true;
255#if 0
256 Res = ReadVendors();
257 if (Res == false)
258 return false;
259#endif
260
261 Reset();
262 // CNC:2003-11-28 - Entries in sources.list have priority over
263 // entries in sources.list.d.
264 string Main = _config->FindFile("Dir::Etc::sourcelist");
67793cf3
JAK
265 string Parts = _config->FindDir("Dir::Etc::sourceparts");
266
36f1098a 267 if (RealFileExists(Main) == true)
6009e60d 268 Res &= ReadAppend(Main);
448eaf8b 269 else if (DirectoryExists(Parts) == false)
67793cf3 270 // Only warn if there are no sources.list.d.
448eaf8b 271 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
34b53501 272
448eaf8b 273 if (DirectoryExists(Parts) == true)
34b53501 274 Res &= ReadSourceDir(Parts);
36f1098a 275 else if (RealFileExists(Main) == false)
67793cf3 276 // Only warn if there is no sources.list file.
36f1098a 277 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
6009e60d 278
34b53501 279 return Res;
6c139d6e
AL
280}
281 /*}}}*/
34b53501
MV
282// SourceList::Reset - Clear the sourcelist contents /*{{{*/
283// ---------------------------------------------------------------------
284/* */
285void pkgSourceList::Reset()
286{
f7f0d6c7 287 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
34b53501
MV
288 delete *I;
289 SrcList.erase(SrcList.begin(),SrcList.end());
290}
291 /*}}}*/
6c139d6e
AL
292// SourceList::Read - Parse the sourcelist file /*{{{*/
293// ---------------------------------------------------------------------
294/* */
295bool pkgSourceList::Read(string File)
34b53501
MV
296{
297 Reset();
298 return ReadAppend(File);
299}
300 /*}}}*/
6f447813
MV
301// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
302// ---------------------------------------------------------------------
303/* */
304bool pkgSourceList::ReadAppend(string File)
305{
655af1e1 306 if (_config->FindB("APT::Sources::Use-Deb822", false) == true)
4194c9ae
MV
307 {
308 int lines_parsed =ParseFileDeb822(File);
309 if (lines_parsed < 0)
310 return false;
311 else if (lines_parsed > 0)
6f447813 312 return true;
4194c9ae
MV
313 // no lines parsed ... fall through and use old style parser
314 }
1fa78a8a
MV
315 return ParseFileOldStyle(File);
316}
6f447813 317
1fa78a8a
MV
318// SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
319// ---------------------------------------------------------------------
320/* */
321bool pkgSourceList::ParseFileOldStyle(string File)
322{
6c139d6e 323 // Open the stream for reading
4d6f8bdf 324 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 325 if (!F != 0)
b2e465d6 326 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
a537ce19 327
34b53501
MV
328 // CNC:2003-12-10 - 300 is too short.
329 char Buffer[1024];
6c139d6e
AL
330
331 int CurLine = 0;
332 while (F.eof() == false)
333 {
334 F.getline(Buffer,sizeof(Buffer));
335 CurLine++;
336 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
337 if (F.fail() && !F.eof())
338 return _error->Error(_("Line %u too long in source list %s."),
339 CurLine,File.c_str());
340
b2e465d6
AL
341
342 char *I;
34b53501
MV
343 // CNC:2003-02-20 - Do not break if '#' is inside [].
344 for (I = Buffer; *I != 0 && *I != '#'; I++)
345 if (*I == '[')
125bf782
MV
346 {
347 char *b_end = strchr(I + 1, ']');
348 if (b_end != NULL)
349 I = b_end;
350 }
b2e465d6
AL
351 *I = 0;
352
353 const char *C = _strstrip(Buffer);
6c139d6e
AL
354
355 // Comment or blank
b2e465d6 356 if (C[0] == '#' || C[0] == 0)
6c139d6e 357 continue;
b2e465d6 358
6c139d6e 359 // Grok it
b2e465d6
AL
360 string LineType;
361 if (ParseQuoteWord(C,LineType) == false)
362 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 363
b2e465d6
AL
364 Type *Parse = Type::GetType(LineType.c_str());
365 if (Parse == 0)
39442e44 366 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 367
5dd4c8b8 368 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
b2e465d6 369 return false;
6c139d6e
AL
370 }
371 return true;
372}
373 /*}}}*/
1fa78a8a
MV
374// SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
375// ---------------------------------------------------------------------
4194c9ae
MV
376/* Returns: the number of stanzas parsed*/
377int pkgSourceList::ParseFileDeb822(string File)
1fa78a8a 378{
fce9f472 379 pkgTagSection Tags;
fce9f472
MV
380 unsigned int i=0;
381
382 // see if we can read the file
383 _error->PushToStack();
1fa78a8a 384 FileFd Fd(File, FileFd::ReadOnly);
fce9f472 385 pkgTagFile Sources(&Fd);
1fa78a8a
MV
386 if (_error->PendingError() == true)
387 {
fce9f472 388 _error->RevertToStack();
4194c9ae 389 return 0;
1fa78a8a 390 }
fce9f472 391 _error->MergeWithStack();
1fa78a8a 392
fce9f472 393 // read step by step
1fa78a8a
MV
394 while (Sources.Step(Tags) == true)
395 {
7f316a3f 396 if(!Tags.Exists("Types"))
1fa78a8a 397 continue;
42e19c82 398
7f316a3f
MV
399 string const types = Tags.FindS("Types");
400 std::vector<std::string> list_types = StringSplit(types, " ");
401 for (std::vector<std::string>::const_iterator I = list_types.begin();
402 I != list_types.end(); I++)
4194c9ae 403 {
7f316a3f
MV
404 Type *Parse = Type::GetType((*I).c_str());
405 if (Parse == 0)
406 {
407 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I).c_str(),i,Fd.Name().c_str());
408 return -1;
409 }
1fa78a8a 410
7f316a3f
MV
411 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
412 return -1;
1fa78a8a 413
7f316a3f
MV
414 i++;
415 }
1fa78a8a
MV
416 }
417
4194c9ae
MV
418 // we are done, return the number of stanzas read
419 return i;
1fa78a8a
MV
420}
421 /*}}}*/
b2e465d6 422// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
423// ---------------------------------------------------------------------
424/* */
b2e465d6
AL
425bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
426 pkgIndexFile *&Found) const
6c139d6e 427{
f7f0d6c7 428 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
6c139d6e 429 {
7db98ffc
MZ
430 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
431 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
f7f0d6c7 432 J != Indexes->end(); ++J)
b2e465d6 433 {
7db98ffc
MZ
434 if ((*J)->FindInCache(*File.Cache()) == File)
435 {
436 Found = (*J);
437 return true;
438 }
b2e465d6 439 }
be8922fd 440 }
7db98ffc 441
b2e465d6 442 return false;
36375005
AL
443}
444 /*}}}*/
b2e465d6 445// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
446// ---------------------------------------------------------------------
447/* */
7db98ffc 448bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 449{
f7f0d6c7 450 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
7db98ffc 451 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
452 return false;
453 return true;
6c139d6e
AL
454}
455 /*}}}*/
34b53501
MV
456// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
457// SourceList::ReadSourceDir - Read a directory with sources files
458// Based on ReadConfigDir() /*{{{*/
459// ---------------------------------------------------------------------
460/* */
461bool pkgSourceList::ReadSourceDir(string Dir)
462{
52643bec 463 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
464
465 // Read the files
f7f0d6c7 466 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
34b53501
MV
467 if (ReadAppend(*I) == false)
468 return false;
469 return true;
470
471}
472 /*}}}*/
2ec858bc
MV
473// GetLastModified() /*{{{*/
474// ---------------------------------------------------------------------
475/* */
476time_t pkgSourceList::GetLastModifiedTime()
477{
5e7b0aa9
MV
478 vector<string> List;
479
2ec858bc
MV
480 string Main = _config->FindFile("Dir::Etc::sourcelist");
481 string Parts = _config->FindDir("Dir::Etc::sourceparts");
5e7b0aa9
MV
482
483 // go over the parts
484 if (DirectoryExists(Parts) == true)
485 List = GetListOfFilesInDir(Parts, "list", true);
2ec858bc
MV
486
487 // calculate the time
488 time_t mtime_sources = GetModificationTime(Main);
f7f0d6c7 489 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
2ec858bc
MV
490 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
491
492 return mtime_sources;
493}
494 /*}}}*/
34b53501 495