sourcelist.cc 15 KB

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