sourcelist.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sourcelist.cc,v 1.5 1998/07/26 04:49:33 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 <strutl.h>
  17. #include <fstream.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <sys/stat.h>
  21. /*}}}*/
  22. // SourceList::pkgSourceList - Constructors /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* */
  25. pkgSourceList::pkgSourceList()
  26. {
  27. }
  28. pkgSourceList::pkgSourceList(string File)
  29. {
  30. Read(File);
  31. }
  32. /*}}}*/
  33. // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. bool pkgSourceList::ReadMainList()
  37. {
  38. return Read(_config->FindDir("Dir::Etc::sourcelist"));
  39. }
  40. /*}}}*/
  41. // SourceList::Read - Parse the sourcelist file /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* */
  44. bool pkgSourceList::Read(string File)
  45. {
  46. // Open the stream for reading
  47. ifstream F(File.c_str(),ios::in | ios::nocreate);
  48. if (!F != 0)
  49. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  50. List.erase(List.begin(),List.end());
  51. char Buffer[300];
  52. int CurLine = 0;
  53. while (F.eof() == false)
  54. {
  55. F.getline(Buffer,sizeof(Buffer));
  56. CurLine++;
  57. _strtabexpand(Buffer,sizeof(Buffer));
  58. _strstrip(Buffer);
  59. // Comment or blank
  60. if (Buffer[0] == '#' || Buffer[0] == 0)
  61. continue;
  62. // Grok it
  63. string Type;
  64. string URI;
  65. Item Itm;
  66. char *C = Buffer;
  67. if (ParseQuoteWord(C,Type) == false)
  68. return _error->Error("Malformed line %u in source list %s (type)",CurLine,File.c_str());
  69. if (ParseQuoteWord(C,URI) == false)
  70. return _error->Error("Malformed line %u in source list %s (URI)",CurLine,File.c_str());
  71. if (ParseQuoteWord(C,Itm.Dist) == false)
  72. return _error->Error("Malformed line %u in source list %s (dist)",CurLine,File.c_str());
  73. if (Itm.SetType(Type) == false)
  74. return _error->Error("Malformed line %u in source list %s (type parse)",CurLine,File.c_str());
  75. if (Itm.SetURI(URI) == false)
  76. return _error->Error("Malformed line %u in source list %s (URI parse)",CurLine,File.c_str());
  77. // Check for an absolute dists specification.
  78. if (Itm.Dist.empty() == false && Itm.Dist[Itm.Dist.size() - 1] == '/')
  79. {
  80. if (ParseQuoteWord(C,Itm.Section) == true)
  81. return _error->Error("Malformed line %u in source list %s (Absolute dist)",CurLine,File.c_str());
  82. Itm.Dist = SubstVar(Itm.Dist,"$(ARCH)",_config->Find("APT::Architecture"));
  83. List.push_back(Itm);
  84. continue;
  85. }
  86. // Grab the rest of the dists
  87. if (ParseQuoteWord(C,Itm.Section) == false)
  88. return _error->Error("Malformed line %u in source list %s (dist parse)",CurLine,File.c_str());
  89. do
  90. {
  91. List.push_back(Itm);
  92. }
  93. while (ParseQuoteWord(C,Itm.Section) == true);
  94. }
  95. return true;
  96. }
  97. /*}}}*/
  98. // SourceList::Item << - Writes the item to a stream /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* This is not suitable for rebuilding the sourcelist file but it good for
  101. debugging. */
  102. ostream &operator <<(ostream &O,pkgSourceList::Item &Itm)
  103. {
  104. O << Itm.Type << ' ' << Itm.URI << ' ' << Itm.Dist << ' ' << Itm.Section;
  105. return O;
  106. }
  107. /*}}}*/
  108. // SourceList::Item::SetType - Sets the distribution type /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* */
  111. bool pkgSourceList::Item::SetType(string S)
  112. {
  113. if (S == "deb")
  114. {
  115. Type = Deb;
  116. return true;
  117. }
  118. return true;
  119. }
  120. /*}}}*/
  121. // SourceList::Item::SetURI - Set the URI /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* For simplicity we strip the scheme off the uri */
  124. bool pkgSourceList::Item::SetURI(string S)
  125. {
  126. if (S.empty() == true)
  127. return false;
  128. if (S.find(':') == string::npos)
  129. return false;
  130. S = SubstVar(S,"$(ARCH)",_config->Find("APT::Architecture"));
  131. // Make sure that the URN is / postfixed
  132. URI = S;
  133. if (URI[URI.size() - 1] != '/')
  134. URI += '/';
  135. return true;
  136. }
  137. /*}}}*/
  138. // SourceList::Item::PackagesURI - Returns a URI to the packages file /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* */
  141. string pkgSourceList::Item::PackagesURI() const
  142. {
  143. string Res;
  144. switch (Type)
  145. {
  146. case Deb:
  147. if (Dist[Dist.size() - 1] == '/')
  148. Res = URI + Dist;
  149. else
  150. Res = URI + "dists/" + Dist + '/' + Section +
  151. "/binary-" + _config->Find("APT::Architecture") + '/';
  152. Res += "Packages";
  153. break;
  154. };
  155. return Res;
  156. }
  157. /*}}}*/
  158. // SourceList::Item::PackagesInfo - Shorter version of the URI /*{{{*/
  159. // ---------------------------------------------------------------------
  160. /* This is a shorter version that is designed to be < 60 chars or so */
  161. string pkgSourceList::Item::PackagesInfo() const
  162. {
  163. string Res;
  164. switch (Type)
  165. {
  166. case Deb:
  167. Res += SiteOnly(URI) + ' ';
  168. if (Dist[Dist.size() - 1] == '/')
  169. Res += Dist;
  170. else
  171. Res += Dist + '/' + Section;
  172. Res += " Packages";
  173. break;
  174. };
  175. return Res;
  176. }
  177. /*}}}*/
  178. // SourceList::Item::ArchiveInfo - Shorter version of the archive spec /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* This is a shorter version that is designed to be < 60 chars or so */
  181. string pkgSourceList::Item::ArchiveInfo(pkgCache::VerIterator Ver) const
  182. {
  183. string Res;
  184. switch (Type)
  185. {
  186. case Deb:
  187. Res += SiteOnly(URI) + ' ';
  188. if (Dist[Dist.size() - 1] == '/')
  189. Res += Dist;
  190. else
  191. Res += Dist + '/' + Section;
  192. Res += " ";
  193. Res += Ver.ParentPkg().Name();
  194. break;
  195. };
  196. return Res;
  197. }
  198. /*}}}*/
  199. // SourceList::Item::ArchiveURI - Returns a URI to the given archive /*{{{*/
  200. // ---------------------------------------------------------------------
  201. /* */
  202. string pkgSourceList::Item::ArchiveURI(string File) const
  203. {
  204. string Res;
  205. switch (Type)
  206. {
  207. case Deb:
  208. Res = URI + File;
  209. break;
  210. };
  211. return Res;
  212. }
  213. /*}}}*/
  214. // SourceList::Item::SiteOnly - Strip off the path part of a URI /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* */
  217. string pkgSourceList::Item::SiteOnly(string URI) const
  218. {
  219. unsigned int Pos = URI.find(':');
  220. if (Pos == string::npos || Pos + 3 > URI.length())
  221. return URI;
  222. if (URI[Pos + 1] != '/' || URI[Pos + 2] != '/')
  223. return URI;
  224. Pos = URI.find('/',Pos + 3);
  225. if (Pos == string::npos)
  226. return URI;
  227. return string(URI,0,Pos);
  228. }
  229. /*}}}*/