sourcelist.cc 12 KB

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