]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
implement reverse_iterators for cachesets
[apt.git] / apt-pkg / sourcelist.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sourcelist.cc,v 1.3 2002/08/15 20:51:37 niemeyer Exp $
4 /* ######################################################################
5
6 List of Sources
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/sourcelist.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/metaindex.h>
19 #include <apt-pkg/indexfile.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/pkgcache.h>
22 #include <apt-pkg/cacheiterators.h>
23
24 #include <ctype.h>
25 #include <stddef.h>
26 #include <time.h>
27 #include <cstring>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fstream>
32 #include <algorithm>
33
34 #include <apti18n.h>
35 /*}}}*/
36
37 using namespace std;
38
39 // Global list of Items supported
40 static pkgSourceList::Type *ItmList[10];
41 pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
42 unsigned long pkgSourceList::Type::GlobalListLen = 0;
43
44 // Type::Type - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* Link this to the global list of items*/
47 pkgSourceList::Type::Type(char const * const pName, char const * const pLabel) : Name(pName), Label(pLabel)
48 {
49 ItmList[GlobalListLen] = this;
50 ++GlobalListLen;
51 }
52 pkgSourceList::Type::~Type() {}
53 /*}}}*/
54 // Type::GetType - Get a specific meta for a given type /*{{{*/
55 // ---------------------------------------------------------------------
56 /* */
57 pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
58 {
59 for (unsigned I = 0; I != GlobalListLen; ++I)
60 if (strcmp(GlobalList[I]->Name,Type) == 0)
61 return GlobalList[I];
62 return 0;
63 }
64 /*}}}*/
65 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 bool pkgSourceList::Type::FixupURI(string &URI) const
69 {
70 if (URI.empty() == true)
71 return false;
72
73 if (URI.find(':') == string::npos)
74 return false;
75
76 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
77
78 // Make sure that the URI is / postfixed
79 if (URI[URI.size() - 1] != '/')
80 URI += '/';
81
82 return true;
83 }
84 /*}}}*/
85 bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List, /*{{{*/
86 pkgTagSection &Tags,
87 unsigned int const i,
88 FileFd &Fd)
89 {
90 map<string, string> Options;
91
92 string Enabled = Tags.FindS("Enabled");
93 if (Enabled.empty() == false && StringToBool(Enabled) == false)
94 return true;
95
96 std::map<char const * const, std::pair<char const * const, bool> > mapping;
97 #define APT_PLUSMINUS(X, Y) \
98 mapping.insert(std::make_pair(X, std::make_pair(Y, true))); \
99 mapping.insert(std::make_pair(X "Add", std::make_pair(Y "+", true))); \
100 mapping.insert(std::make_pair(X "Remove", std::make_pair(Y "-", true)))
101 APT_PLUSMINUS("Architectures", "arch");
102 APT_PLUSMINUS("Languages", "lang");
103 APT_PLUSMINUS("Targets", "target");
104 #undef APT_PLUSMINUS
105 mapping.insert(std::make_pair("Trusted", std::make_pair("trusted", false)));
106 mapping.insert(std::make_pair("Check-Valid-Until", std::make_pair("check-valid-until", false)));
107 mapping.insert(std::make_pair("Valid-Until-Min", std::make_pair("valid-until-min", false)));
108 mapping.insert(std::make_pair("Valid-Until-Max", std::make_pair("valid-until-max", false)));
109 mapping.insert(std::make_pair("Signed-By", std::make_pair("signed-by", false)));
110
111 for (std::map<char const * const, std::pair<char const * const, bool> >::const_iterator m = mapping.begin(); m != mapping.end(); ++m)
112 if (Tags.Exists(m->first))
113 {
114 std::string option = Tags.FindS(m->first);
115 // for deb822 the " " is the delimiter, but the backend expects ","
116 if (m->second.second == true)
117 std::replace(option.begin(), option.end(), ' ', ',');
118 Options[m->second.first] = option;
119 }
120
121 // now create one item per suite/section
122 string Suite = Tags.FindS("Suites");
123 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
124 string const Component = Tags.FindS("Components");
125 string const URIS = Tags.FindS("URIs");
126
127 std::vector<std::string> const list_uris = VectorizeString(URIS, ' ');
128 std::vector<std::string> const list_suite = VectorizeString(Suite, ' ');
129 std::vector<std::string> const list_comp = VectorizeString(Component, ' ');
130
131 if (list_uris.empty())
132 // TRANSLATOR: %u is a line number, the first %s is a filename of a file with the extension "second %s" and the third %s is a unique identifier for bugreports
133 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI");
134
135 for (std::vector<std::string>::const_iterator U = list_uris.begin();
136 U != list_uris.end(); ++U)
137 {
138 std::string URI = *U;
139 if (U->empty() || FixupURI(URI) == false)
140 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI parse");
141
142 if (list_suite.empty())
143 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Suite");
144
145 for (std::vector<std::string>::const_iterator S = list_suite.begin();
146 S != list_suite.end(); ++S)
147 {
148 if (S->empty() == false && (*S)[S->size() - 1] == '/')
149 {
150 if (list_comp.empty() == false)
151 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "absolute Suite Component");
152 if (CreateItem(List, URI, *S, "", Options) == false)
153 return false;
154 }
155 else
156 {
157 if (list_comp.empty())
158 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Component");
159
160 for (std::vector<std::string>::const_iterator C = list_comp.begin();
161 C != list_comp.end(); ++C)
162 {
163 if (CreateItem(List, URI, *S, *C, Options) == false)
164 {
165 return false;
166 }
167 }
168 }
169 }
170 }
171 return true;
172 }
173 /*}}}*/
174 // Type::ParseLine - Parse a single line /*{{{*/
175 // ---------------------------------------------------------------------
176 /* This is a generic one that is the 'usual' format for sources.list
177 Weird types may override this. */
178 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
179 const char *Buffer,
180 unsigned int const CurLine,
181 string const &File) const
182 {
183 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
184
185 // Parse option field if it exists
186 // e.g.: [ option1=value1 option2=value2 ]
187 map<string, string> Options;
188 if (Buffer != 0 && Buffer[0] == '[')
189 {
190 ++Buffer; // ignore the [
191 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
192 while (*Buffer != ']')
193 {
194 // get one option, e.g. option1=value1
195 string option;
196 if (ParseQuoteWord(Buffer,option) == false)
197 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] unparseable");
198
199 if (option.length() < 3)
200 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] too short");
201
202 // accept options even if the last has no space before the ]-end marker
203 if (option.at(option.length()-1) == ']')
204 {
205 for (; *Buffer != ']'; --Buffer);
206 option.resize(option.length()-1);
207 }
208
209 size_t const needle = option.find('=');
210 if (needle == string::npos)
211 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] not assignment");
212
213 string const key = string(option, 0, needle);
214 string const value = string(option, needle + 1, option.length());
215
216 if (key.empty() == true)
217 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no key");
218
219 if (value.empty() == true)
220 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no value");
221
222 Options[key] = value;
223 }
224 ++Buffer; // ignore the ]
225 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
226 }
227
228 string URI;
229 string Dist;
230 string Section;
231
232 if (ParseQuoteWord(Buffer,URI) == false)
233 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI");
234 if (ParseQuoteWord(Buffer,Dist) == false)
235 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Suite");
236
237 if (FixupURI(URI) == false)
238 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI parse");
239
240 // Check for an absolute dists specification.
241 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
242 {
243 if (ParseQuoteWord(Buffer,Section) == true)
244 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "absolute Suite Component");
245 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
246 return CreateItem(List, URI, Dist, Section, Options);
247 }
248
249 // Grab the rest of the dists
250 if (ParseQuoteWord(Buffer,Section) == false)
251 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Component");
252
253 do
254 {
255 if (CreateItem(List, URI, Dist, Section, Options) == false)
256 return false;
257 }
258 while (ParseQuoteWord(Buffer,Section) == true);
259
260 return true;
261 }
262 /*}}}*/
263 // SourceList::pkgSourceList - Constructors /*{{{*/
264 // ---------------------------------------------------------------------
265 /* */
266 pkgSourceList::pkgSourceList() : d(NULL)
267 {
268 }
269 /*}}}*/
270 // SourceList::~pkgSourceList - Destructor /*{{{*/
271 // ---------------------------------------------------------------------
272 /* */
273 pkgSourceList::~pkgSourceList()
274 {
275 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
276 delete *I;
277 }
278 /*}}}*/
279 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
280 // ---------------------------------------------------------------------
281 /* */
282 bool pkgSourceList::ReadMainList()
283 {
284 // CNC:2003-03-03 - Multiple sources list support.
285 bool Res = true;
286 #if 0
287 Res = ReadVendors();
288 if (Res == false)
289 return false;
290 #endif
291
292 Reset();
293 // CNC:2003-11-28 - Entries in sources.list have priority over
294 // entries in sources.list.d.
295 string Main = _config->FindFile("Dir::Etc::sourcelist");
296 string Parts = _config->FindDir("Dir::Etc::sourceparts");
297
298 if (RealFileExists(Main) == true)
299 Res &= ReadAppend(Main);
300 else if (DirectoryExists(Parts) == false)
301 // Only warn if there are no sources.list.d.
302 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
303
304 if (DirectoryExists(Parts) == true)
305 Res &= ReadSourceDir(Parts);
306 else if (RealFileExists(Main) == false)
307 // Only warn if there is no sources.list file.
308 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
309
310 return Res;
311 }
312 /*}}}*/
313 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
314 // ---------------------------------------------------------------------
315 /* */
316 void pkgSourceList::Reset()
317 {
318 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
319 delete *I;
320 SrcList.erase(SrcList.begin(),SrcList.end());
321 }
322 /*}}}*/
323 // SourceList::Read - Parse the sourcelist file /*{{{*/
324 // ---------------------------------------------------------------------
325 /* */
326 bool pkgSourceList::Read(string const &File)
327 {
328 Reset();
329 return ReadAppend(File);
330 }
331 /*}}}*/
332 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
333 // ---------------------------------------------------------------------
334 /* */
335 bool pkgSourceList::ReadAppend(string const &File)
336 {
337 if (flExtension(File) == "sources")
338 return ParseFileDeb822(File);
339 else
340 return ParseFileOldStyle(File);
341 }
342
343 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
344 // ---------------------------------------------------------------------
345 /* */
346 bool pkgSourceList::ParseFileOldStyle(std::string const &File)
347 {
348 // Open the stream for reading
349 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
350 if (F.fail() == true)
351 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
352
353 std::string Buffer;
354 for (unsigned int CurLine = 1; std::getline(F, Buffer); ++CurLine)
355 {
356 // remove comments
357 size_t curpos = 0;
358 while ((curpos = Buffer.find('#', curpos)) != std::string::npos)
359 {
360 size_t const openbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, '[');
361 size_t const closedbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, ']');
362 if (openbrackets > closedbrackets)
363 {
364 // a # in an option, unlikely, but oh well, it was supported so stick to it
365 ++curpos;
366 continue;
367 }
368 Buffer.erase(curpos);
369 break;
370 }
371 // remove spaces before/after
372 curpos = Buffer.find_first_not_of(" \t\r");
373 if (curpos != 0)
374 Buffer.erase(0, curpos);
375 curpos = Buffer.find_last_not_of(" \t\r");
376 if (curpos != std::string::npos)
377 Buffer.erase(curpos + 1);
378
379 if (Buffer.empty())
380 continue;
381
382 // Grok it
383 std::string const LineType = Buffer.substr(0, Buffer.find(' '));
384 if (LineType.empty() || LineType == Buffer)
385 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
386
387 Type *Parse = Type::GetType(LineType.c_str());
388 if (Parse == 0)
389 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
390
391 if (Parse->ParseLine(SrcList, Buffer.c_str() + LineType.length(), CurLine, File) == false)
392 return false;
393 }
394 return true;
395 }
396 /*}}}*/
397 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
398 // ---------------------------------------------------------------------
399 /* Returns: the number of stanzas parsed*/
400 bool pkgSourceList::ParseFileDeb822(string const &File)
401 {
402 pkgUserTagSection Tags;
403 unsigned int i = 1;
404
405 // see if we can read the file
406 FileFd Fd(File, FileFd::ReadOnly);
407 pkgTagFile Sources(&Fd);
408 if (_error->PendingError() == true)
409 return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
410
411 // read step by step
412 while (Sources.Step(Tags) == true)
413 {
414 if(Tags.Exists("Types") == false)
415 return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
416
417 string const types = Tags.FindS("Types");
418 std::vector<std::string> const list_types = VectorizeString(types, ' ');
419 for (std::vector<std::string>::const_iterator I = list_types.begin();
420 I != list_types.end(); ++I)
421 {
422 Type *Parse = Type::GetType((*I).c_str());
423 if (Parse == 0)
424 {
425 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I).c_str(),i,Fd.Name().c_str());
426 return false;
427 }
428
429 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
430 return false;
431
432 ++i;
433 }
434 }
435 return true;
436 }
437 /*}}}*/
438 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
439 // ---------------------------------------------------------------------
440 /* */
441 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
442 pkgIndexFile *&Found) const
443 {
444 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
445 {
446 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
447 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
448 J != Indexes->end(); ++J)
449 {
450 if ((*J)->FindInCache(*File.Cache()) == File)
451 {
452 Found = (*J);
453 return true;
454 }
455 }
456 }
457
458 return false;
459 }
460 /*}}}*/
461 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
462 // ---------------------------------------------------------------------
463 /* */
464 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
465 {
466 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
467 if ((*I)->GetIndexes(Owner,GetAll) == false)
468 return false;
469 return true;
470 }
471 /*}}}*/
472 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
473 // SourceList::ReadSourceDir - Read a directory with sources files
474 // Based on ReadConfigDir() /*{{{*/
475 // ---------------------------------------------------------------------
476 /* */
477 bool pkgSourceList::ReadSourceDir(string const &Dir)
478 {
479 std::vector<std::string> ext;
480 ext.push_back("list");
481 ext.push_back("sources");
482 std::vector<std::string> const List = GetListOfFilesInDir(Dir, ext, true);
483
484 // Read the files
485 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
486 if (ReadAppend(*I) == false)
487 return false;
488 return true;
489
490 }
491 /*}}}*/
492 // GetLastModified() /*{{{*/
493 // ---------------------------------------------------------------------
494 /* */
495 time_t pkgSourceList::GetLastModifiedTime()
496 {
497 vector<string> List;
498
499 string Main = _config->FindFile("Dir::Etc::sourcelist");
500 string Parts = _config->FindDir("Dir::Etc::sourceparts");
501
502 // go over the parts
503 if (DirectoryExists(Parts) == true)
504 List = GetListOfFilesInDir(Parts, "list", true);
505
506 // calculate the time
507 time_t mtime_sources = GetModificationTime(Main);
508 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
509 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
510
511 return mtime_sources;
512 }
513 /*}}}*/
514