sourcelist.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /*}}}*/
  157. // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
  158. // ---------------------------------------------------------------------
  159. /* */
  160. bool pkgSourceList::ReadMainList()
  161. {
  162. // CNC:2003-03-03 - Multiple sources list support.
  163. bool Res = true;
  164. #if 0
  165. Res = ReadVendors();
  166. if (Res == false)
  167. return false;
  168. #endif
  169. Reset();
  170. // CNC:2003-11-28 - Entries in sources.list have priority over
  171. // entries in sources.list.d.
  172. string Main = _config->FindFile("Dir::Etc::sourcelist");
  173. string Parts = _config->FindDir("Dir::Etc::sourceparts");
  174. if (RealFileExists(Main) == true)
  175. Res &= ReadAppend(Main);
  176. else if (DirectoryExists(Parts) == false)
  177. // Only warn if there are no sources.list.d.
  178. _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
  179. if (DirectoryExists(Parts) == true)
  180. Res &= ReadSourceDir(Parts);
  181. else if (RealFileExists(Main) == false)
  182. // Only warn if there is no sources.list file.
  183. _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
  184. return Res;
  185. }
  186. /*}}}*/
  187. // CNC:2003-03-03 - Needed to preserve backwards compatibility.
  188. // SourceList::Reset - Clear the sourcelist contents /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* */
  191. void pkgSourceList::Reset()
  192. {
  193. for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
  194. delete *I;
  195. SrcList.erase(SrcList.begin(),SrcList.end());
  196. }
  197. /*}}}*/
  198. // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
  199. // SourceList::Read - Parse the sourcelist file /*{{{*/
  200. // ---------------------------------------------------------------------
  201. /* */
  202. bool pkgSourceList::Read(string File)
  203. {
  204. Reset();
  205. return ReadAppend(File);
  206. }
  207. /*}}}*/
  208. // FIXME: move into pkgSourceList::Type::ParseFile()
  209. bool pkgSourceList::ParseFileDeb822(FileFd &Fd)
  210. {
  211. pkgTagSection Tags;
  212. map<string, string> Options;
  213. unsigned int i=0;
  214. pkgTagFile Sources(&Fd);
  215. while (Sources.Step(Tags) == true)
  216. {
  217. if(!Tags.Exists("Type"))
  218. continue;
  219. string const type = Tags.FindS("Type");
  220. Type *Parse = Type::GetType(type.c_str());
  221. if (Parse == 0)
  222. return _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,Fd.Name().c_str());
  223. string URI = Tags.FindS("URL");
  224. if (!Parse->FixupURI(URI))
  225. return _error->Error(_("Malformed stanza %u in source list %s (URI parse)"),i,Fd.Name().c_str());
  226. string const Dist = Tags.FindS("Dist");
  227. string const Section = Tags.FindS("Section");
  228. // check if there are any options we support
  229. const char* option_str[] = {
  230. "arch", "arch+", "arch-", "trusted" };
  231. for (unsigned int j=0; j < sizeof(option_str)/sizeof(char*); j++)
  232. if (Tags.Exists(option_str[j]))
  233. Options[option_str[j]] = Tags.FindS(option_str[j]);
  234. // now create one item per section
  235. std::vector<std::string> list;
  236. if (Section.find(","))
  237. list = StringSplit(Section, ",");
  238. else
  239. list = StringSplit(Section, " ");
  240. for (std::vector<std::string>::const_iterator I = list.begin();
  241. I != list.end(); I++)
  242. Parse->CreateItem(SrcList, URI, Dist, (*I), Options);
  243. i++;
  244. }
  245. // we are done
  246. if(i>0)
  247. return true;
  248. return false;
  249. }
  250. // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
  251. // ---------------------------------------------------------------------
  252. /* */
  253. bool pkgSourceList::ReadAppend(string File)
  254. {
  255. // *first* try reading as deb822
  256. // FIXME: proper error handling so that we do not error for good old-style
  257. // sources
  258. FileFd Fd(File, FileFd::ReadOnly);
  259. if (_error->PendingError() == false)
  260. {
  261. if (ParseFileDeb822(Fd))
  262. return true;
  263. }
  264. // *then* read as old-style sources.list
  265. // Open the stream for reading
  266. ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
  267. if (!F != 0)
  268. return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
  269. // CNC:2003-12-10 - 300 is too short.
  270. char Buffer[1024];
  271. int CurLine = 0;
  272. while (F.eof() == false)
  273. {
  274. F.getline(Buffer,sizeof(Buffer));
  275. CurLine++;
  276. _strtabexpand(Buffer,sizeof(Buffer));
  277. if (F.fail() && !F.eof())
  278. return _error->Error(_("Line %u too long in source list %s."),
  279. CurLine,File.c_str());
  280. char *I;
  281. // CNC:2003-02-20 - Do not break if '#' is inside [].
  282. for (I = Buffer; *I != 0 && *I != '#'; I++)
  283. if (*I == '[')
  284. {
  285. char *b_end = strchr(I + 1, ']');
  286. if (b_end != NULL)
  287. I = b_end;
  288. }
  289. *I = 0;
  290. const char *C = _strstrip(Buffer);
  291. // Comment or blank
  292. if (C[0] == '#' || C[0] == 0)
  293. continue;
  294. // Grok it
  295. string LineType;
  296. if (ParseQuoteWord(C,LineType) == false)
  297. return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
  298. Type *Parse = Type::GetType(LineType.c_str());
  299. if (Parse == 0)
  300. return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
  301. if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
  302. return false;
  303. }
  304. return true;
  305. }
  306. /*}}}*/
  307. // SourceList::FindIndex - Get the index associated with a file /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* */
  310. bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
  311. pkgIndexFile *&Found) const
  312. {
  313. for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
  314. {
  315. vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  316. for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  317. J != Indexes->end(); ++J)
  318. {
  319. if ((*J)->FindInCache(*File.Cache()) == File)
  320. {
  321. Found = (*J);
  322. return true;
  323. }
  324. }
  325. }
  326. return false;
  327. }
  328. /*}}}*/
  329. // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
  330. // ---------------------------------------------------------------------
  331. /* */
  332. bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
  333. {
  334. for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
  335. if ((*I)->GetIndexes(Owner,GetAll) == false)
  336. return false;
  337. return true;
  338. }
  339. /*}}}*/
  340. // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
  341. // SourceList::ReadSourceDir - Read a directory with sources files
  342. // Based on ReadConfigDir() /*{{{*/
  343. // ---------------------------------------------------------------------
  344. /* */
  345. bool pkgSourceList::ReadSourceDir(string Dir)
  346. {
  347. vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
  348. // Read the files
  349. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  350. if (ReadAppend(*I) == false)
  351. return false;
  352. return true;
  353. }
  354. /*}}}*/
  355. // GetLastModified() /*{{{*/
  356. // ---------------------------------------------------------------------
  357. /* */
  358. time_t pkgSourceList::GetLastModifiedTime()
  359. {
  360. vector<string> List;
  361. string Main = _config->FindFile("Dir::Etc::sourcelist");
  362. string Parts = _config->FindDir("Dir::Etc::sourceparts");
  363. // go over the parts
  364. if (DirectoryExists(Parts) == true)
  365. List = GetListOfFilesInDir(Parts, "list", true);
  366. // calculate the time
  367. time_t mtime_sources = GetModificationTime(Main);
  368. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  369. mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
  370. return mtime_sources;
  371. }
  372. /*}}}*/