sourcelist.cc 12 KB

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