sourcelist.cc 14 KB

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