sourcelist.cc 18 KB

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