sourcelist.cc 12 KB

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