sourcelist.cc 14 KB

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