sourcelist.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sourcelist.cc,v 1.18 2001/02/20 07:03:17 jgg 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/configuration.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apti18n.h>
  18. #include <fstream.h>
  19. /*}}}*/
  20. // Global list of Item supported
  21. static pkgSourceList::Type *ItmList[10];
  22. pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
  23. unsigned long pkgSourceList::Type::GlobalListLen = 0;
  24. // Type::Type - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* Link this to the global list of items*/
  27. pkgSourceList::Type::Type()
  28. {
  29. ItmList[GlobalListLen] = this;
  30. GlobalListLen++;
  31. }
  32. /*}}}*/
  33. // Type::GetType - Get a specific meta for a given type /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
  37. {
  38. for (unsigned I = 0; I != GlobalListLen; I++)
  39. if (strcmp(GlobalList[I]->Name,Type) == 0)
  40. return GlobalList[I];
  41. return 0;
  42. }
  43. /*}}}*/
  44. // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. bool pkgSourceList::Type::FixupURI(string &URI) const
  48. {
  49. if (URI.empty() == true)
  50. return false;
  51. if (URI.find(':') == string::npos)
  52. return false;
  53. URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
  54. // Make sure that the URN is / postfixed
  55. if (URI[URI.size() - 1] != '/')
  56. URI += '/';
  57. return true;
  58. }
  59. /*}}}*/
  60. // Type::ParseLine - Parse a single line /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* This is a generic one that is the 'usual' format for sources.list
  63. Weird types may override this. */
  64. bool pkgSourceList::Type::ParseLine(vector<pkgIndexFile *> &List,
  65. const char *Buffer,
  66. unsigned long CurLine,
  67. string File) const
  68. {
  69. string URI;
  70. string Dist;
  71. string Section;
  72. if (ParseQuoteWord(Buffer,URI) == false)
  73. return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
  74. if (ParseQuoteWord(Buffer,Dist) == false)
  75. return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
  76. if (FixupURI(URI) == false)
  77. return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
  78. // Check for an absolute dists specification.
  79. if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
  80. {
  81. if (ParseQuoteWord(Buffer,Section) == true)
  82. return _error->Error(_("Malformed line %lu in source list %s (Absolute dist)"),CurLine,File.c_str());
  83. Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
  84. return CreateItem(List,URI,Dist,Section);
  85. }
  86. // Grab the rest of the dists
  87. if (ParseQuoteWord(Buffer,Section) == false)
  88. return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
  89. do
  90. {
  91. if (CreateItem(List,URI,Dist,Section) == false)
  92. return false;
  93. }
  94. while (ParseQuoteWord(Buffer,Section) == true);
  95. return true;
  96. }
  97. /*}}}*/
  98. // SourceList::pkgSourceList - Constructors /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. pkgSourceList::pkgSourceList()
  102. {
  103. }
  104. pkgSourceList::pkgSourceList(string File)
  105. {
  106. Read(File);
  107. }
  108. /*}}}*/
  109. // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* */
  112. bool pkgSourceList::ReadMainList()
  113. {
  114. return Read(_config->FindFile("Dir::Etc::sourcelist"));
  115. }
  116. /*}}}*/
  117. // SourceList::Read - Parse the sourcelist file /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* */
  120. bool pkgSourceList::Read(string File)
  121. {
  122. // Open the stream for reading
  123. ifstream F(File.c_str(),ios::in | ios::nocreate);
  124. if (!F != 0)
  125. return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
  126. List.erase(List.begin(),List.end());
  127. char Buffer[300];
  128. int CurLine = 0;
  129. while (F.eof() == false)
  130. {
  131. F.getline(Buffer,sizeof(Buffer));
  132. CurLine++;
  133. _strtabexpand(Buffer,sizeof(Buffer));
  134. char *I;
  135. for (I = Buffer; *I != 0 && *I != '#'; I++);
  136. *I = 0;
  137. const char *C = _strstrip(Buffer);
  138. // Comment or blank
  139. if (C[0] == '#' || C[0] == 0)
  140. continue;
  141. // Grok it
  142. string LineType;
  143. if (ParseQuoteWord(C,LineType) == false)
  144. return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
  145. Type *Parse = Type::GetType(LineType.c_str());
  146. if (Parse == 0)
  147. return _error->Error(_("Type '%s' is not known in on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
  148. if (Parse->ParseLine(List,C,CurLine,File) == false)
  149. return false;
  150. }
  151. return true;
  152. }
  153. /*}}}*/
  154. // SourceList::FindIndex - Get the index associated with a file /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* */
  157. bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
  158. pkgIndexFile *&Found) const
  159. {
  160. for (const_iterator I = List.begin(); I != List.end(); I++)
  161. {
  162. if ((*I)->FindInCache(*File.Cache()) == File)
  163. {
  164. Found = *I;
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. /*}}}*/
  171. // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* */
  174. bool pkgSourceList::GetIndexes(pkgAcquire *Owner) const
  175. {
  176. for (const_iterator I = List.begin(); I != List.end(); I++)
  177. if ((*I)->GetIndexes(Owner) == false)
  178. return false;
  179. return true;
  180. }
  181. /*}}}*/