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