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.
 
 
 
 
 
 

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