sourcelist.cc 16 KB

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