]> git.saurik.com Git - apt.git/blame - apt-pkg/sourcelist.cc
improve SOCKS error messages for http slightly
[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 13#include <apt-pkg/sourcelist.h>
14341a7e 14#include <apt-pkg/cmndline.h>
094a497d
AL
15#include <apt-pkg/error.h>
16#include <apt-pkg/fileutl.h>
cdcc6d34 17#include <apt-pkg/strutl.h>
7db98ffc 18#include <apt-pkg/configuration.h>
472ff00e
DK
19#include <apt-pkg/metaindex.h>
20#include <apt-pkg/indexfile.h>
a537ce19 21#include <apt-pkg/tagfile.h>
453b82a3
DK
22#include <apt-pkg/pkgcache.h>
23#include <apt-pkg/cacheiterators.h>
14341a7e 24#include <apt-pkg/debindexfile.h>
92296fe4 25#include <apt-pkg/debsrcrecords.h>
6c139d6e 26
453b82a3
DK
27#include <ctype.h>
28#include <stddef.h>
29#include <time.h>
30#include <cstring>
31#include <map>
32#include <string>
33#include <vector>
90f057fd 34#include <fstream>
41e6bd08 35#include <algorithm>
ea542140
DK
36
37#include <apti18n.h>
b2e465d6
AL
38 /*}}}*/
39
4d6f8bdf
AL
40using namespace std;
41
42// Global list of Items supported
463c8d80 43static pkgSourceList::Type *ItmList[10];
b2e465d6
AL
44pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
45unsigned long pkgSourceList::Type::GlobalListLen = 0;
46
47// Type::Type - Constructor /*{{{*/
48// ---------------------------------------------------------------------
49/* Link this to the global list of items*/
463c8d80 50pkgSourceList::Type::Type(char const * const pName, char const * const pLabel) : Name(pName), Label(pLabel)
b2e465d6
AL
51{
52 ItmList[GlobalListLen] = this;
463c8d80 53 ++GlobalListLen;
b2e465d6 54}
463c8d80 55pkgSourceList::Type::~Type() {}
b2e465d6
AL
56 /*}}}*/
57// Type::GetType - Get a specific meta for a given type /*{{{*/
58// ---------------------------------------------------------------------
59/* */
60pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
61{
463c8d80 62 for (unsigned I = 0; I != GlobalListLen; ++I)
b2e465d6
AL
63 if (strcmp(GlobalList[I]->Name,Type) == 0)
64 return GlobalList[I];
65 return 0;
66}
67 /*}}}*/
68// Type::FixupURI - Normalize the URI and check it.. /*{{{*/
69// ---------------------------------------------------------------------
70/* */
71bool pkgSourceList::Type::FixupURI(string &URI) const
72{
73 if (URI.empty() == true)
74 return false;
75
76 if (URI.find(':') == string::npos)
77 return false;
78
79 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
80
a7c835af 81 // Make sure that the URI is / postfixed
b2e465d6
AL
82 if (URI[URI.size() - 1] != '/')
83 URI += '/';
84
85 return true;
86}
87 /*}}}*/
81460e32 88bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List, /*{{{*/
7037aab5 89 pkgTagSection &Tags,
81460e32 90 unsigned int const i,
7037aab5
MV
91 FileFd &Fd)
92{
93 map<string, string> Options;
94
7dd62ea9 95 string Enabled = Tags.FindS("Enabled");
81460e32 96 if (Enabled.empty() == false && StringToBool(Enabled) == false)
7dd62ea9 97 return true;
463c8d80 98
b0d40854 99 std::map<char const * const, std::pair<char const * const, bool> > mapping;
463c8d80 100#define APT_PLUSMINUS(X, Y) \
b0d40854 101 mapping.insert(std::make_pair(X, std::make_pair(Y, true))); \
f5585106
JM
102 mapping.insert(std::make_pair(X "-Add", std::make_pair(Y "+", true))); \
103 mapping.insert(std::make_pair(X "-Remove", std::make_pair(Y "-", true)))
463c8d80
DK
104 APT_PLUSMINUS("Architectures", "arch");
105 APT_PLUSMINUS("Languages", "lang");
106 APT_PLUSMINUS("Targets", "target");
107#undef APT_PLUSMINUS
b0d40854
DK
108 mapping.insert(std::make_pair("Trusted", std::make_pair("trusted", false)));
109 mapping.insert(std::make_pair("Check-Valid-Until", std::make_pair("check-valid-until", false)));
110 mapping.insert(std::make_pair("Valid-Until-Min", std::make_pair("valid-until-min", false)));
111 mapping.insert(std::make_pair("Valid-Until-Max", std::make_pair("valid-until-max", false)));
112 mapping.insert(std::make_pair("Signed-By", std::make_pair("signed-by", false)));
1a3a14ac 113 mapping.insert(std::make_pair("PDiffs", std::make_pair("pdiffs", false)));
24e8f24e 114 mapping.insert(std::make_pair("By-Hash", std::make_pair("by-hash", false)));
5ad0096a 115
b0d40854 116 for (std::map<char const * const, std::pair<char const * const, bool> >::const_iterator m = mapping.begin(); m != mapping.end(); ++m)
463c8d80 117 if (Tags.Exists(m->first))
41e6bd08 118 {
b0d40854
DK
119 std::string option = Tags.FindS(m->first);
120 // for deb822 the " " is the delimiter, but the backend expects ","
121 if (m->second.second == true)
122 std::replace(option.begin(), option.end(), ' ', ',');
123 Options[m->second.first] = option;
41e6bd08 124 }
463c8d80 125
3090ae69
DK
126 {
127 std::string entry;
128 strprintf(entry, "%s:%i", Fd.Name().c_str(), i);
129 Options["sourceslist-entry"] = entry;
130 }
131
d73743dd 132 // now create one item per suite/section
6c069a22 133 string Suite = Tags.FindS("Suites");
d73743dd 134 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
81460e32
DK
135 string const Component = Tags.FindS("Components");
136 string const URIS = Tags.FindS("URIs");
137
138 std::vector<std::string> const list_uris = VectorizeString(URIS, ' ');
139 std::vector<std::string> const list_suite = VectorizeString(Suite, ' ');
140 std::vector<std::string> const list_comp = VectorizeString(Component, ' ');
141
142 if (list_uris.empty())
143 // 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
144 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI");
d73743dd 145
75c10df1 146 for (std::vector<std::string>::const_iterator U = list_uris.begin();
804de19f 147 U != list_uris.end(); ++U)
d73743dd 148 {
81460e32
DK
149 std::string URI = *U;
150 if (U->empty() || FixupURI(URI) == false)
151 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI parse");
152
153 if (list_suite.empty())
154 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Suite");
75c10df1 155
81460e32
DK
156 for (std::vector<std::string>::const_iterator S = list_suite.begin();
157 S != list_suite.end(); ++S)
75c10df1 158 {
81460e32
DK
159 if (S->empty() == false && (*S)[S->size() - 1] == '/')
160 {
161 if (list_comp.empty() == false)
162 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "absolute Suite Component");
163 if (CreateItem(List, URI, *S, "", Options) == false)
164 return false;
165 }
166 else
167 {
168 if (list_comp.empty())
169 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Component");
170
171 for (std::vector<std::string>::const_iterator C = list_comp.begin();
172 C != list_comp.end(); ++C)
173 {
174 if (CreateItem(List, URI, *S, *C, Options) == false)
175 {
176 return false;
177 }
178 }
179 }
75c10df1 180 }
d73743dd 181 }
7037aab5
MV
182 return true;
183}
81460e32 184 /*}}}*/
b2e465d6
AL
185// Type::ParseLine - Parse a single line /*{{{*/
186// ---------------------------------------------------------------------
187/* This is a generic one that is the 'usual' format for sources.list
188 Weird types may override this. */
7db98ffc 189bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
b2e465d6 190 const char *Buffer,
81460e32 191 unsigned int const CurLine,
5dd4c8b8 192 string const &File) const
b2e465d6 193{
5dd4c8b8
DK
194 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
195
196 // Parse option field if it exists
197 // e.g.: [ option1=value1 option2=value2 ]
198 map<string, string> Options;
3090ae69
DK
199 {
200 std::string entry;
201 strprintf(entry, "%s:%i", File.c_str(), CurLine);
202 Options["sourceslist-entry"] = entry;
203 }
5dd4c8b8
DK
204 if (Buffer != 0 && Buffer[0] == '[')
205 {
206 ++Buffer; // ignore the [
207 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
208 while (*Buffer != ']')
209 {
210 // get one option, e.g. option1=value1
211 string option;
212 if (ParseQuoteWord(Buffer,option) == false)
81460e32 213 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] unparseable");
5dd4c8b8
DK
214
215 if (option.length() < 3)
81460e32 216 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] too short");
5dd4c8b8 217
6838dd87
DK
218 // accept options even if the last has no space before the ]-end marker
219 if (option.at(option.length()-1) == ']')
220 {
221 for (; *Buffer != ']'; --Buffer);
222 option.resize(option.length()-1);
223 }
224
5dd4c8b8
DK
225 size_t const needle = option.find('=');
226 if (needle == string::npos)
81460e32 227 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] not assignment");
5dd4c8b8
DK
228
229 string const key = string(option, 0, needle);
230 string const value = string(option, needle + 1, option.length());
231
232 if (key.empty() == true)
81460e32 233 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no key");
5dd4c8b8
DK
234
235 if (value.empty() == true)
81460e32 236 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no value");
5dd4c8b8
DK
237
238 Options[key] = value;
239 }
240 ++Buffer; // ignore the ]
241 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
242 }
243
b2e465d6
AL
244 string URI;
245 string Dist;
5dd4c8b8
DK
246 string Section;
247
b2e465d6 248 if (ParseQuoteWord(Buffer,URI) == false)
81460e32 249 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI");
b2e465d6 250 if (ParseQuoteWord(Buffer,Dist) == false)
81460e32
DK
251 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Suite");
252
b2e465d6 253 if (FixupURI(URI) == false)
81460e32 254 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI parse");
463c8d80 255
b2e465d6
AL
256 // Check for an absolute dists specification.
257 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
258 {
259 if (ParseQuoteWord(Buffer,Section) == true)
81460e32 260 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "absolute Suite Component");
b2e465d6 261 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
5dd4c8b8 262 return CreateItem(List, URI, Dist, Section, Options);
b2e465d6 263 }
81460e32 264
b2e465d6
AL
265 // Grab the rest of the dists
266 if (ParseQuoteWord(Buffer,Section) == false)
81460e32
DK
267 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Component");
268
b2e465d6
AL
269 do
270 {
5dd4c8b8 271 if (CreateItem(List, URI, Dist, Section, Options) == false)
b2e465d6
AL
272 return false;
273 }
274 while (ParseQuoteWord(Buffer,Section) == true);
81460e32 275
b2e465d6
AL
276 return true;
277}
6c139d6e 278 /*}}}*/
6c139d6e
AL
279// SourceList::pkgSourceList - Constructors /*{{{*/
280// ---------------------------------------------------------------------
281/* */
6c55f07a 282pkgSourceList::pkgSourceList() : d(NULL)
6c139d6e 283{
6c139d6e
AL
284}
285 /*}}}*/
1c193e02
AL
286// SourceList::~pkgSourceList - Destructor /*{{{*/
287// ---------------------------------------------------------------------
288/* */
289pkgSourceList::~pkgSourceList()
290{
f7f0d6c7 291 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
1c193e02 292 delete *I;
5465192b 293 SrcList.clear();
c9d71534
JAK
294 for (auto F = VolatileFiles.begin(); F != VolatileFiles.end(); ++F)
295 delete (*F);
5465192b 296 VolatileFiles.clear();
1c193e02
AL
297}
298 /*}}}*/
6c139d6e
AL
299// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
300// ---------------------------------------------------------------------
301/* */
302bool pkgSourceList::ReadMainList()
303{
34b53501
MV
304 // CNC:2003-03-03 - Multiple sources list support.
305 bool Res = true;
306#if 0
307 Res = ReadVendors();
308 if (Res == false)
309 return false;
310#endif
311
312 Reset();
313 // CNC:2003-11-28 - Entries in sources.list have priority over
314 // entries in sources.list.d.
42610b9d
DK
315 string Main = _config->FindFile("Dir::Etc::sourcelist", "/dev/null");
316 string Parts = _config->FindDir("Dir::Etc::sourceparts", "/dev/null");
67793cf3 317
36f1098a 318 if (RealFileExists(Main) == true)
6009e60d 319 Res &= ReadAppend(Main);
42610b9d 320 else if (DirectoryExists(Parts) == false && APT::String::Endswith(Parts, "/dev/null") == false)
67793cf3 321 // Only warn if there are no sources.list.d.
448eaf8b 322 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
34b53501 323
448eaf8b 324 if (DirectoryExists(Parts) == true)
34b53501 325 Res &= ReadSourceDir(Parts);
42610b9d
DK
326 else if (Main.empty() == false && RealFileExists(Main) == false &&
327 APT::String::Endswith(Parts, "/dev/null") == false)
67793cf3 328 // Only warn if there is no sources.list file.
36f1098a 329 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
6009e60d 330
8bd823d0
DK
331 for (auto && file: _config->FindVector("APT::Sources::With"))
332 Res &= AddVolatileFile(file, nullptr);
333
34b53501 334 return Res;
6c139d6e
AL
335}
336 /*}}}*/
34b53501
MV
337// SourceList::Reset - Clear the sourcelist contents /*{{{*/
338// ---------------------------------------------------------------------
339/* */
340void pkgSourceList::Reset()
341{
f7f0d6c7 342 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
34b53501 343 delete *I;
8dd562a8 344 SrcList.clear();
34b53501
MV
345}
346 /*}}}*/
6c139d6e
AL
347// SourceList::Read - Parse the sourcelist file /*{{{*/
348// ---------------------------------------------------------------------
349/* */
81460e32 350bool pkgSourceList::Read(string const &File)
34b53501
MV
351{
352 Reset();
353 return ReadAppend(File);
354}
355 /*}}}*/
6f447813
MV
356// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
357// ---------------------------------------------------------------------
358/* */
81460e32 359bool pkgSourceList::ReadAppend(string const &File)
6f447813 360{
81460e32
DK
361 if (flExtension(File) == "sources")
362 return ParseFileDeb822(File);
363 else
364 return ParseFileOldStyle(File);
1fa78a8a 365}
5465192b 366 /*}}}*/
1fa78a8a
MV
367// SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
368// ---------------------------------------------------------------------
369/* */
81460e32 370bool pkgSourceList::ParseFileOldStyle(std::string const &File)
1fa78a8a 371{
6c139d6e 372 // Open the stream for reading
4d6f8bdf 373 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
2b4cead3 374 if (F.fail() == true)
b2e465d6 375 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
a537ce19 376
81460e32
DK
377 std::string Buffer;
378 for (unsigned int CurLine = 1; std::getline(F, Buffer); ++CurLine)
6c139d6e 379 {
81460e32
DK
380 // remove comments
381 size_t curpos = 0;
382 while ((curpos = Buffer.find('#', curpos)) != std::string::npos)
383 {
384 size_t const openbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, '[');
385 size_t const closedbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, ']');
386 if (openbrackets > closedbrackets)
387 {
388 // a # in an option, unlikely, but oh well, it was supported so stick to it
389 ++curpos;
390 continue;
391 }
392 Buffer.erase(curpos);
393 break;
394 }
395 // remove spaces before/after
396 curpos = Buffer.find_first_not_of(" \t\r");
397 if (curpos != 0)
398 Buffer.erase(0, curpos);
399 curpos = Buffer.find_last_not_of(" \t\r");
400 if (curpos != std::string::npos)
401 Buffer.erase(curpos + 1);
402
403 if (Buffer.empty())
6c139d6e 404 continue;
81460e32 405
6c139d6e 406 // Grok it
defe3231 407 std::string const LineType = Buffer.substr(0, Buffer.find_first_of(" \t\v"));
81460e32 408 if (LineType.empty() || LineType == Buffer)
b2e465d6 409 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 410
b2e465d6
AL
411 Type *Parse = Type::GetType(LineType.c_str());
412 if (Parse == 0)
39442e44 413 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
81460e32
DK
414
415 if (Parse->ParseLine(SrcList, Buffer.c_str() + LineType.length(), CurLine, File) == false)
b2e465d6 416 return false;
6c139d6e
AL
417 }
418 return true;
419}
420 /*}}}*/
1fa78a8a
MV
421// SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
422// ---------------------------------------------------------------------
4194c9ae 423/* Returns: the number of stanzas parsed*/
81460e32 424bool pkgSourceList::ParseFileDeb822(string const &File)
1fa78a8a 425{
fce9f472 426 // see if we can read the file
1fa78a8a 427 FileFd Fd(File, FileFd::ReadOnly);
f6459e64 428 pkgTagFile Sources(&Fd, pkgTagFile::SUPPORT_COMMENTS);
95278287 429 if (Fd.IsOpen() == false || Fd.Failed())
1485040e 430 return _error->Error(_("Malformed stanza %u in source list %s (type)"),0,File.c_str());
81460e32 431
fce9f472 432 // read step by step
f6459e64 433 pkgTagSection Tags;
1485040e 434 unsigned int i = 0;
1fa78a8a
MV
435 while (Sources.Step(Tags) == true)
436 {
1485040e 437 ++i;
81460e32
DK
438 if(Tags.Exists("Types") == false)
439 return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
42e19c82 440
7f316a3f 441 string const types = Tags.FindS("Types");
81460e32 442 std::vector<std::string> const list_types = VectorizeString(types, ' ');
7f316a3f 443 for (std::vector<std::string>::const_iterator I = list_types.begin();
804de19f 444 I != list_types.end(); ++I)
4194c9ae 445 {
7f316a3f
MV
446 Type *Parse = Type::GetType((*I).c_str());
447 if (Parse == 0)
448 {
449 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I).c_str(),i,Fd.Name().c_str());
81460e32 450 return false;
7f316a3f 451 }
81460e32 452
7f316a3f 453 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
81460e32 454 return false;
7f316a3f 455 }
1fa78a8a 456 }
81460e32 457 return true;
1fa78a8a
MV
458}
459 /*}}}*/
b2e465d6 460// SourceList::FindIndex - Get the index associated with a file /*{{{*/
8dd562a8
DK
461static bool FindInIndexFileContainer(std::vector<pkgIndexFile *> const &Cont, pkgCache::PkgFileIterator const &File, pkgIndexFile *&Found)
462{
463 auto const J = std::find_if(Cont.begin(), Cont.end(), [&File](pkgIndexFile const * const J) {
464 return J->FindInCache(*File.Cache()) == File;
465 });
466 if (J != Cont.end())
467 {
468 Found = (*J);
469 return true;
470 }
471 return false;
472}
b2e465d6
AL
473bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
474 pkgIndexFile *&Found) const
6c139d6e 475{
f7f0d6c7 476 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
8dd562a8 477 if (FindInIndexFileContainer(*(*I)->GetIndexFiles(), File, Found))
5465192b 478 return true;
8dd562a8
DK
479
480 return FindInIndexFileContainer(VolatileFiles, File, Found);
36375005
AL
481}
482 /*}}}*/
b2e465d6 483// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
484// ---------------------------------------------------------------------
485/* */
7db98ffc 486bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 487{
f7f0d6c7 488 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
7db98ffc 489 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
490 return false;
491 return true;
6c139d6e
AL
492}
493 /*}}}*/
34b53501
MV
494// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
495// SourceList::ReadSourceDir - Read a directory with sources files
496// Based on ReadConfigDir() /*{{{*/
497// ---------------------------------------------------------------------
498/* */
81460e32 499bool pkgSourceList::ReadSourceDir(string const &Dir)
34b53501 500{
81460e32
DK
501 std::vector<std::string> ext;
502 ext.push_back("list");
503 ext.push_back("sources");
504 std::vector<std::string> const List = GetListOfFilesInDir(Dir, ext, true);
34b53501
MV
505
506 // Read the files
f7f0d6c7 507 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
34b53501
MV
508 if (ReadAppend(*I) == false)
509 return false;
510 return true;
511
512}
513 /*}}}*/
2ec858bc
MV
514// GetLastModified() /*{{{*/
515// ---------------------------------------------------------------------
516/* */
517time_t pkgSourceList::GetLastModifiedTime()
518{
5e7b0aa9
MV
519 vector<string> List;
520
2ec858bc
MV
521 string Main = _config->FindFile("Dir::Etc::sourcelist");
522 string Parts = _config->FindDir("Dir::Etc::sourceparts");
5e7b0aa9
MV
523
524 // go over the parts
525 if (DirectoryExists(Parts) == true)
526 List = GetListOfFilesInDir(Parts, "list", true);
2ec858bc
MV
527
528 // calculate the time
8dd562a8
DK
529 std::vector<time_t> modtimes;
530 modtimes.reserve(1 + List.size());
531 modtimes.push_back(GetModificationTime(Main));
532 std::transform(List.begin(), List.end(), std::back_inserter(modtimes), GetModificationTime);
533 auto const maxmtime = std::max_element(modtimes.begin(), modtimes.end());
534 return *maxmtime;
2ec858bc
MV
535}
536 /*}}}*/
5465192b
DK
537std::vector<pkgIndexFile*> pkgSourceList::GetVolatileFiles() const /*{{{*/
538{
539 return VolatileFiles;
540}
541 /*}}}*/
542void pkgSourceList::AddVolatileFile(pkgIndexFile * const File) /*{{{*/
543{
14341a7e 544 if (File != nullptr)
5465192b
DK
545 VolatileFiles.push_back(File);
546}
547 /*}}}*/
8bd823d0
DK
548static bool fileNameMatches(std::string const &filename, std::string const &idxtype)/*{{{*/
549{
550 for (auto && type: APT::Configuration::getCompressionTypes())
551 {
552 if (type == "uncompressed")
553 {
554 if (filename == idxtype || APT::String::Endswith(filename, '_' + idxtype))
555 return true;
556 }
557 else if (filename == idxtype + '.' + type ||
558 APT::String::Endswith(filename, '_' + idxtype + '.' + type))
559 return true;
560 }
561 return false;
562}
563 /*}}}*/
92296fe4 564bool pkgSourceList::AddVolatileFile(std::string const &File, std::vector<std::string> * const VolatileCmdL)/*{{{*/
14341a7e 565{
f359b7e8 566 // Note: FileExists matches directories and links, too!
14341a7e
DK
567 if (File.empty() || FileExists(File) == false)
568 return false;
569
f359b7e8 570 std::string const ext = flExtension(File);
4f242a22
DK
571 // udeb is not included as installing it is usually a mistake rather than intended
572 if (ext == "deb" || ext == "ddeb")
14341a7e 573 AddVolatileFile(new debDebPkgFileIndex(File));
f359b7e8
DK
574 else if (ext == "dsc")
575 AddVolatileFile(new debDscFileIndex(File));
576 else if (FileExists(flCombine(File, "debian/control")))
577 AddVolatileFile(new debDscFileIndex(flCombine(File, "debian/control")));
92296fe4
DK
578 else if (ext == "changes")
579 {
580 debDscRecordParser changes(File, nullptr);
581 std::vector<pkgSrcRecords::File2> fileslst;
582 if (changes.Files2(fileslst) == false || fileslst.empty())
583 return false;
584 auto const basedir = flNotFile(File);
585 for (auto && file: fileslst)
586 {
587 auto const name = flCombine(basedir, file.Path);
588 AddVolatileFile(name, VolatileCmdL);
589 if (file.Hashes.VerifyFile(name) == false)
590 return _error->Error("The file %s does not match with the hashes in the %s file!", name.c_str(), File.c_str());
591 }
592 return true;
593 }
14341a7e 594 else
8bd823d0
DK
595 {
596 auto const filename = flNotDir(File);
597 auto const Target = IndexTarget(File, filename, File, "file:" + File, false, true, {
598 { "FILENAME", File },
599 { "REPO_URI", "file:" + flAbsPath(flNotFile(File)) + '/' },
600 { "COMPONENT", "volatile-packages-file" },
601 });
602 if (fileNameMatches(filename, "Packages"))
603 AddVolatileFile(new debPackagesIndex(Target, true));
604 else if (fileNameMatches(filename, "Sources"))
605 AddVolatileFile(new debSourcesIndex(Target, true));
606 else
607 return false;
608 }
14341a7e 609
92296fe4
DK
610 if (VolatileCmdL != nullptr)
611 VolatileCmdL->push_back(File);
14341a7e 612 return true;
92296fe4
DK
613}
614bool pkgSourceList::AddVolatileFile(std::string const &File)
615{
616 return AddVolatileFile(File, nullptr);
14341a7e
DK
617}
618 /*}}}*/
92296fe4
DK
619void pkgSourceList::AddVolatileFiles(CommandLine &CmdL, std::vector<std::string> * const VolatileCmdL)/*{{{*/
620{
621 std::remove_if(CmdL.FileList + 1, CmdL.FileList + 1 + CmdL.FileSize(), [&](char const * const I) {
622 if (I != nullptr && (I[0] == '/' || (I[0] == '.' && ((I[1] == '.' && I[2] == '/') || I[1] == '/'))))
623 {
624 if (AddVolatileFile(I, VolatileCmdL))
625 ;
626 else
627 _error->Error(_("Unsupported file %s given on commandline"), I);
628 return true;
629 }
630 return false;
631 });
632}
633void pkgSourceList::AddVolatileFiles(CommandLine &CmdL, std::vector<const char*> * const VolatileCmdL)
14341a7e
DK
634{
635 std::remove_if(CmdL.FileList + 1, CmdL.FileList + 1 + CmdL.FileSize(), [&](char const * const I) {
3dd64b9c 636 if (I != nullptr && (I[0] == '/' || (I[0] == '.' && ((I[1] == '.' && I[2] == '/') || I[1] == '/'))))
14341a7e
DK
637 {
638 if (AddVolatileFile(I))
639 {
640 if (VolatileCmdL != nullptr)
641 VolatileCmdL->push_back(I);
642 }
643 else
644 _error->Error(_("Unsupported file %s given on commandline"), I);
645 return true;
646 }
647 return false;
648 });
649}
650 /*}}}*/