]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
51b766095edee84fd307e232e8b0cf00df53da9b
[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
24 #include <apti18n.h>
25 /*}}}*/
26
27 using namespace std;
28
29 // Global list of Items supported
30 static pkgSourceList::Type *ItmList[10];
31 pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
32 unsigned long pkgSourceList::Type::GlobalListLen = 0;
33
34 // Type::Type - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* Link this to the global list of items*/
37 pkgSourceList::Type::Type() : Name(NULL), Label(NULL)
38 {
39 ItmList[GlobalListLen] = this;
40 GlobalListLen++;
41 }
42 /*}}}*/
43 // Type::GetType - Get a specific meta for a given type /*{{{*/
44 // ---------------------------------------------------------------------
45 /* */
46 pkgSourceList::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 /* */
57 bool 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
67 // Make sure that the URI is / postfixed
68 if (URI[URI.size() - 1] != '/')
69 URI += '/';
70
71 return true;
72 }
73 /*}}}*/
74 bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List,
75 pkgTagSection &Tags,
76 int i,
77 FileFd &Fd)
78 {
79 map<string, string> Options;
80
81 string URI = Tags.FindS("URI");
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
88 // Define external/internal options
89 const char* option_deb822[] = {
90 "Architectures", "Architectures-Add", "Architectures-Remove", "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
99 // now create one item per suite/section
100 string Suite = Tags.FindS("Suite");
101 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
102 string const Section = Tags.FindS("Section");
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 }
118 return true;
119 }
120
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. */
125 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
126 const char *Buffer,
127 unsigned long const &CurLine,
128 string const &File) const
129 {
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
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
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
175 string URI;
176 string Dist;
177 string Section;
178
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)
191 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
192 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
193 return CreateItem(List, URI, Dist, Section, Options);
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 {
202 if (CreateItem(List, URI, Dist, Section, Options) == false)
203 return false;
204 }
205 while (ParseQuoteWord(Buffer,Section) == true);
206
207 return true;
208 }
209 /*}}}*/
210 // SourceList::pkgSourceList - Constructors /*{{{*/
211 // ---------------------------------------------------------------------
212 /* */
213 pkgSourceList::pkgSourceList()
214 {
215 }
216
217 pkgSourceList::pkgSourceList(string File)
218 {
219 Read(File);
220 }
221 /*}}}*/
222 // SourceList::~pkgSourceList - Destructor /*{{{*/
223 // ---------------------------------------------------------------------
224 /* */
225 pkgSourceList::~pkgSourceList()
226 {
227 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
228 delete *I;
229 }
230 /*}}}*/
231 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
232 // ---------------------------------------------------------------------
233 /* */
234 bool pkgSourceList::ReadMainList()
235 {
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");
248 string Parts = _config->FindDir("Dir::Etc::sourceparts");
249
250 if (RealFileExists(Main) == true)
251 Res &= ReadAppend(Main);
252 else if (DirectoryExists(Parts) == false)
253 // Only warn if there are no sources.list.d.
254 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
255
256 if (DirectoryExists(Parts) == true)
257 Res &= ReadSourceDir(Parts);
258 else if (RealFileExists(Main) == false)
259 // Only warn if there is no sources.list file.
260 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
261
262 return Res;
263 }
264 /*}}}*/
265 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
266 // ---------------------------------------------------------------------
267 /* */
268 void pkgSourceList::Reset()
269 {
270 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
271 delete *I;
272 SrcList.erase(SrcList.begin(),SrcList.end());
273 }
274 /*}}}*/
275 // SourceList::Read - Parse the sourcelist file /*{{{*/
276 // ---------------------------------------------------------------------
277 /* */
278 bool pkgSourceList::Read(string File)
279 {
280 Reset();
281 return ReadAppend(File);
282 }
283 /*}}}*/
284 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
285 // ---------------------------------------------------------------------
286 /* */
287 bool pkgSourceList::ReadAppend(string File)
288 {
289 if (_config->FindB("APT::Sources::Use-Deb822", true) == true)
290 {
291 int lines_parsed =ParseFileDeb822(File);
292 if (lines_parsed < 0)
293 return false;
294 else if (lines_parsed > 0)
295 return true;
296 // no lines parsed ... fall through and use old style parser
297 }
298 return ParseFileOldStyle(File);
299 }
300
301 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
302 // ---------------------------------------------------------------------
303 /* */
304 bool pkgSourceList::ParseFileOldStyle(string File)
305 {
306 // Open the stream for reading
307 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
308 if (!F != 0)
309 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
310
311 // CNC:2003-12-10 - 300 is too short.
312 char Buffer[1024];
313
314 int CurLine = 0;
315 while (F.eof() == false)
316 {
317 F.getline(Buffer,sizeof(Buffer));
318 CurLine++;
319 _strtabexpand(Buffer,sizeof(Buffer));
320 if (F.fail() && !F.eof())
321 return _error->Error(_("Line %u too long in source list %s."),
322 CurLine,File.c_str());
323
324
325 char *I;
326 // CNC:2003-02-20 - Do not break if '#' is inside [].
327 for (I = Buffer; *I != 0 && *I != '#'; I++)
328 if (*I == '[')
329 {
330 char *b_end = strchr(I + 1, ']');
331 if (b_end != NULL)
332 I = b_end;
333 }
334 *I = 0;
335
336 const char *C = _strstrip(Buffer);
337
338 // Comment or blank
339 if (C[0] == '#' || C[0] == 0)
340 continue;
341
342 // Grok it
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());
346
347 Type *Parse = Type::GetType(LineType.c_str());
348 if (Parse == 0)
349 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
350
351 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
352 return false;
353 }
354 return true;
355 }
356 /*}}}*/
357 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
358 // ---------------------------------------------------------------------
359 /* Returns: the number of stanzas parsed*/
360 int pkgSourceList::ParseFileDeb822(string File)
361 {
362 pkgTagSection Tags;
363 unsigned int i=0;
364
365 // see if we can read the file
366 _error->PushToStack();
367 FileFd Fd(File, FileFd::ReadOnly);
368 pkgTagFile Sources(&Fd);
369 if (_error->PendingError() == true)
370 {
371 _error->RevertToStack();
372 return 0;
373 }
374 _error->MergeWithStack();
375
376 // read step by step
377 while (Sources.Step(Tags) == true)
378 {
379 if(!Tags.Exists("Type"))
380 continue;
381
382 string const type = Tags.FindS("Type");
383 Type *Parse = Type::GetType(type.c_str());
384 if (Parse == 0)
385 {
386 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,Fd.Name().c_str());
387 return -1;
388 }
389
390 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
391 return -1;
392
393 i++;
394 }
395
396 // we are done, return the number of stanzas read
397 return i;
398 }
399 /*}}}*/
400 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
401 // ---------------------------------------------------------------------
402 /* */
403 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
404 pkgIndexFile *&Found) const
405 {
406 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
407 {
408 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
409 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
410 J != Indexes->end(); ++J)
411 {
412 if ((*J)->FindInCache(*File.Cache()) == File)
413 {
414 Found = (*J);
415 return true;
416 }
417 }
418 }
419
420 return false;
421 }
422 /*}}}*/
423 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
424 // ---------------------------------------------------------------------
425 /* */
426 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
427 {
428 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
429 if ((*I)->GetIndexes(Owner,GetAll) == false)
430 return false;
431 return true;
432 }
433 /*}}}*/
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 /* */
439 bool pkgSourceList::ReadSourceDir(string Dir)
440 {
441 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
442
443 // Read the files
444 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
445 if (ReadAppend(*I) == false)
446 return false;
447 return true;
448
449 }
450 /*}}}*/
451 // GetLastModified() /*{{{*/
452 // ---------------------------------------------------------------------
453 /* */
454 time_t pkgSourceList::GetLastModifiedTime()
455 {
456 vector<string> List;
457
458 string Main = _config->FindFile("Dir::Etc::sourcelist");
459 string Parts = _config->FindDir("Dir::Etc::sourceparts");
460
461 // go over the parts
462 if (DirectoryExists(Parts) == true)
463 List = GetListOfFilesInDir(Parts, "list", true);
464
465 // calculate the time
466 time_t mtime_sources = GetModificationTime(Main);
467 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
468 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
469
470 return mtime_sources;
471 }
472 /*}}}*/
473