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