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