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