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