sourcelist.cc 17 KB

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