You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

542 lines
18 KiB

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