sourcelist.cc 15 KB

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