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.
 
 
 
 
 
 

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