sourcelist.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* */
  211. bool pkgSourceList::ReadAppend(string File)
  212. {
  213. // try reading as deb822
  214. // FIXME: proper error handling so that we do not error for good old-style
  215. // sources
  216. FileFd Fd(File, FileFd::ReadOnly);
  217. pkgTagFile Sources(&Fd);
  218. if (_error->PendingError() == false)
  219. {
  220. pkgTagSection Tags;
  221. map<string, string> Options;
  222. int i=0;
  223. while (Sources.Step(Tags) == true)
  224. {
  225. if(!Tags.Exists("Type"))
  226. continue;
  227. string const type = Tags.FindS("Type");
  228. Type *Parse = Type::GetType(type.c_str());
  229. if (Parse == 0)
  230. return _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,File.c_str());
  231. string URI = Tags.FindS("URL");
  232. if (!Parse->FixupURI(URI))
  233. return _error->Error(_("Malformed stanza %lu in source list %s (URI parse)"),i,File.c_str());
  234. string const Dist = Tags.FindS("Dist");
  235. string const Section = Tags.FindS("Section");
  236. // check if there are any options we support
  237. const char* option_str[] = {
  238. "arch", "arch+", "arch-", "trusted" };
  239. for (unsigned int j=0; j < sizeof(option_str)/sizeof(char*); j++)
  240. if (Tags.Exists(option_str[j]))
  241. Options[option_str[j]] = Tags.FindS(option_str[j]);
  242. Parse->CreateItem(SrcList, URI, Dist, Section, Options);
  243. i++;
  244. }
  245. // we are done
  246. if(i>0)
  247. return true;
  248. }
  249. // Open the stream for reading
  250. ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
  251. if (!F != 0)
  252. return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
  253. // CNC:2003-12-10 - 300 is too short.
  254. char Buffer[1024];
  255. int CurLine = 0;
  256. while (F.eof() == false)
  257. {
  258. F.getline(Buffer,sizeof(Buffer));
  259. CurLine++;
  260. _strtabexpand(Buffer,sizeof(Buffer));
  261. if (F.fail() && !F.eof())
  262. return _error->Error(_("Line %u too long in source list %s."),
  263. CurLine,File.c_str());
  264. char *I;
  265. // CNC:2003-02-20 - Do not break if '#' is inside [].
  266. for (I = Buffer; *I != 0 && *I != '#'; I++)
  267. if (*I == '[')
  268. {
  269. char *b_end = strchr(I + 1, ']');
  270. if (b_end != NULL)
  271. I = b_end;
  272. }
  273. *I = 0;
  274. const char *C = _strstrip(Buffer);
  275. // Comment or blank
  276. if (C[0] == '#' || C[0] == 0)
  277. continue;
  278. // Grok it
  279. string LineType;
  280. if (ParseQuoteWord(C,LineType) == false)
  281. return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
  282. Type *Parse = Type::GetType(LineType.c_str());
  283. if (Parse == 0)
  284. return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
  285. if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
  286. return false;
  287. }
  288. return true;
  289. }
  290. /*}}}*/
  291. // SourceList::FindIndex - Get the index associated with a file /*{{{*/
  292. // ---------------------------------------------------------------------
  293. /* */
  294. bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
  295. pkgIndexFile *&Found) const
  296. {
  297. for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
  298. {
  299. vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  300. for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  301. J != Indexes->end(); ++J)
  302. {
  303. if ((*J)->FindInCache(*File.Cache()) == File)
  304. {
  305. Found = (*J);
  306. return true;
  307. }
  308. }
  309. }
  310. return false;
  311. }
  312. /*}}}*/
  313. // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
  314. // ---------------------------------------------------------------------
  315. /* */
  316. bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
  317. {
  318. for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
  319. if ((*I)->GetIndexes(Owner,GetAll) == false)
  320. return false;
  321. return true;
  322. }
  323. /*}}}*/
  324. // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
  325. // SourceList::ReadSourceDir - Read a directory with sources files
  326. // Based on ReadConfigDir() /*{{{*/
  327. // ---------------------------------------------------------------------
  328. /* */
  329. bool pkgSourceList::ReadSourceDir(string Dir)
  330. {
  331. vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
  332. // Read the files
  333. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  334. if (ReadAppend(*I) == false)
  335. return false;
  336. return true;
  337. }
  338. /*}}}*/
  339. // GetLastModified() /*{{{*/
  340. // ---------------------------------------------------------------------
  341. /* */
  342. time_t pkgSourceList::GetLastModifiedTime()
  343. {
  344. vector<string> List;
  345. string Main = _config->FindFile("Dir::Etc::sourcelist");
  346. string Parts = _config->FindDir("Dir::Etc::sourceparts");
  347. // go over the parts
  348. if (DirectoryExists(Parts) == true)
  349. List = GetListOfFilesInDir(Parts, "list", true);
  350. // calculate the time
  351. time_t mtime_sources = GetModificationTime(Main);
  352. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  353. mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
  354. return mtime_sources;
  355. }
  356. /*}}}*/