sourcelist.cc 11 KB

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