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