sourcelist.cc 18 KB

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