sourcelist.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <apt-pkg/sourcelist.h>
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/fileutl.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apti18n.h>
  15. #include <fstream>
  16. /*}}}*/
  17. using namespace std;
  18. // Global list of Items supported
  19. static pkgSourceList::Type *ItmList[10];
  20. pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
  21. unsigned long pkgSourceList::Type::GlobalListLen = 0;
  22. // Type::Type - Constructor /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* Link this to the global list of items*/
  25. pkgSourceList::Type::Type()
  26. {
  27. ItmList[GlobalListLen] = this;
  28. GlobalListLen++;
  29. }
  30. /*}}}*/
  31. // Type::GetType - Get a specific meta for a given type /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
  35. {
  36. for (unsigned I = 0; I != GlobalListLen; I++)
  37. if (strcmp(GlobalList[I]->Name,Type) == 0)
  38. return GlobalList[I];
  39. return 0;
  40. }
  41. /*}}}*/
  42. // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. bool pkgSourceList::Type::FixupURI(string &URI) const
  46. {
  47. if (URI.empty() == true)
  48. return false;
  49. if (URI.find(':') == string::npos)
  50. return false;
  51. URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
  52. // Make sure that the URI is / postfixed
  53. if (URI[URI.size() - 1] != '/')
  54. URI += '/';
  55. return true;
  56. }
  57. /*}}}*/
  58. // Type::ParseLine - Parse a single line /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* This is a generic one that is the 'usual' format for sources.list
  61. Weird types may override this. */
  62. bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
  63. const char *Buffer,
  64. unsigned long CurLine,
  65. string File) const
  66. {
  67. string URI;
  68. string Dist;
  69. string Section;
  70. if (ParseQuoteWord(Buffer,URI) == false)
  71. return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
  72. if (ParseQuoteWord(Buffer,Dist) == false)
  73. return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
  74. if (FixupURI(URI) == false)
  75. return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
  76. // Check for an absolute dists specification.
  77. if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
  78. {
  79. if (ParseQuoteWord(Buffer,Section) == true)
  80. return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
  81. Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
  82. return CreateItem(List,URI,Dist,Section);
  83. }
  84. // Grab the rest of the dists
  85. if (ParseQuoteWord(Buffer,Section) == false)
  86. return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
  87. do
  88. {
  89. if (CreateItem(List,URI,Dist,Section) == false)
  90. return false;
  91. }
  92. while (ParseQuoteWord(Buffer,Section) == true);
  93. return true;
  94. }
  95. /*}}}*/
  96. // SourceList::pkgSourceList - Constructors /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* */
  99. pkgSourceList::pkgSourceList()
  100. {
  101. }
  102. pkgSourceList::pkgSourceList(string File)
  103. {
  104. Read(File);
  105. }
  106. /*}}}*/
  107. // SourceList::~pkgSourceList - Destructor /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* */
  110. pkgSourceList::~pkgSourceList()
  111. {
  112. for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
  113. delete *I;
  114. }
  115. /*}}}*/
  116. /*}}}*/
  117. // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* */
  120. bool pkgSourceList::ReadMainList()
  121. {
  122. // CNC:2003-03-03 - Multiple sources list support.
  123. bool Res = true;
  124. #if 0
  125. Res = ReadVendors();
  126. if (Res == false)
  127. return false;
  128. #endif
  129. Reset();
  130. // CNC:2003-11-28 - Entries in sources.list have priority over
  131. // entries in sources.list.d.
  132. string Main = _config->FindFile("Dir::Etc::sourcelist");
  133. string Parts = _config->FindDir("Dir::Etc::sourceparts");
  134. if (FileExists(Main) == true)
  135. Res &= ReadAppend(Main);
  136. else if (FileExists(Parts) == false)
  137. // Only warn if there are no sources.list.d.
  138. _error->WarningE("FileExists",_("Unable to read %s"),Main.c_str());
  139. if (FileExists(Parts) == true)
  140. Res &= ReadSourceDir(Parts);
  141. else if (FileExists(Main) == false)
  142. // Only warn if there is no sources.list file.
  143. _error->WarningE("FileExists",_("Unable to read %s"),Parts.c_str());
  144. return Res;
  145. }
  146. /*}}}*/
  147. // CNC:2003-03-03 - Needed to preserve backwards compatibility.
  148. // SourceList::Reset - Clear the sourcelist contents /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* */
  151. void pkgSourceList::Reset()
  152. {
  153. for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
  154. delete *I;
  155. SrcList.erase(SrcList.begin(),SrcList.end());
  156. }
  157. /*}}}*/
  158. // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
  159. // SourceList::Read - Parse the sourcelist file /*{{{*/
  160. // ---------------------------------------------------------------------
  161. /* */
  162. bool pkgSourceList::Read(string File)
  163. {
  164. Reset();
  165. return ReadAppend(File);
  166. }
  167. /*}}}*/
  168. // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
  169. // ---------------------------------------------------------------------
  170. /* */
  171. bool pkgSourceList::ReadAppend(string File)
  172. {
  173. // Open the stream for reading
  174. ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
  175. if (!F != 0)
  176. return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
  177. #if 0 // Now Reset() does this.
  178. for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
  179. delete *I;
  180. SrcList.erase(SrcList.begin(),SrcList.end());
  181. #endif
  182. // CNC:2003-12-10 - 300 is too short.
  183. char Buffer[1024];
  184. int CurLine = 0;
  185. while (F.eof() == false)
  186. {
  187. F.getline(Buffer,sizeof(Buffer));
  188. CurLine++;
  189. _strtabexpand(Buffer,sizeof(Buffer));
  190. if (F.fail() && !F.eof())
  191. return _error->Error(_("Line %u too long in source list %s."),
  192. CurLine,File.c_str());
  193. char *I;
  194. // CNC:2003-02-20 - Do not break if '#' is inside [].
  195. for (I = Buffer; *I != 0 && *I != '#'; I++)
  196. if (*I == '[')
  197. for (I++; *I != 0 && *I != ']'; I++);
  198. *I = 0;
  199. const char *C = _strstrip(Buffer);
  200. // Comment or blank
  201. if (C[0] == '#' || C[0] == 0)
  202. continue;
  203. // Grok it
  204. string LineType;
  205. if (ParseQuoteWord(C,LineType) == false)
  206. return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
  207. Type *Parse = Type::GetType(LineType.c_str());
  208. if (Parse == 0)
  209. return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
  210. // Vendor name specified
  211. if (C[0] == '[')
  212. {
  213. string VendorID;
  214. if (ParseQuoteWord(C,VendorID) == false)
  215. return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
  216. if (VendorID.length() < 2 || VendorID.end()[-1] != ']')
  217. return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
  218. VendorID = string(VendorID,1,VendorID.size()-2);
  219. // for (vector<const Vendor *>::const_iterator iter = VendorList.begin();
  220. // iter != VendorList.end(); iter++)
  221. // {
  222. // if ((*iter)->GetVendorID() == VendorID)
  223. // {
  224. // if (_config->FindB("Debug::sourceList", false))
  225. // std::cerr << "Comparing VendorID \"" << VendorID << "\" with \"" << (*iter)->GetVendorID() << '"' << std::endl;
  226. // Verifier = *iter;
  227. // break;
  228. // }
  229. // }
  230. // if (Verifier == 0)
  231. // return _error->Error(_("Unknown vendor ID '%s' in line %u of source list %s"),
  232. // VendorID.c_str(),CurLine,File.c_str());
  233. }
  234. if (Parse->ParseLine(SrcList,C,CurLine,File) == false)
  235. return false;
  236. }
  237. return true;
  238. }
  239. /*}}}*/
  240. // SourceList::FindIndex - Get the index associated with a file /*{{{*/
  241. // ---------------------------------------------------------------------
  242. /* */
  243. bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
  244. pkgIndexFile *&Found) const
  245. {
  246. for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
  247. {
  248. vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  249. for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  250. J != Indexes->end(); J++)
  251. {
  252. if ((*J)->FindInCache(*File.Cache()) == File)
  253. {
  254. Found = (*J);
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. }
  261. /*}}}*/
  262. // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* */
  265. bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
  266. {
  267. for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
  268. if ((*I)->GetIndexes(Owner,GetAll) == false)
  269. return false;
  270. return true;
  271. }
  272. /*}}}*/
  273. // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
  274. // SourceList::ReadSourceDir - Read a directory with sources files
  275. // Based on ReadConfigDir() /*{{{*/
  276. // ---------------------------------------------------------------------
  277. /* */
  278. bool pkgSourceList::ReadSourceDir(string Dir)
  279. {
  280. vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
  281. // Read the files
  282. for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
  283. if (ReadAppend(*I) == false)
  284. return false;
  285. return true;
  286. }
  287. /*}}}*/