sourcelist.cc 16 KB

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