sourcelist.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sourcelist.cc,v 1.17 1999/10/17 07:30:23 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 <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->FindFile("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. const 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 << (int)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. if (S == "deb-src")
  119. {
  120. Type = DebSrc;
  121. return true;
  122. }
  123. return false;
  124. }
  125. /*}}}*/
  126. // SourceList::Item::SetURI - Set the URI /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* For simplicity we strip the scheme off the uri */
  129. bool pkgSourceList::Item::SetURI(string S)
  130. {
  131. if (S.empty() == true)
  132. return false;
  133. if (S.find(':') == string::npos)
  134. return false;
  135. S = SubstVar(S,"$(ARCH)",_config->Find("APT::Architecture"));
  136. // Make sure that the URN is / postfixed
  137. URI = S;
  138. if (URI[URI.size() - 1] != '/')
  139. URI += '/';
  140. return true;
  141. }
  142. /*}}}*/
  143. // SourceList::Item::PackagesURI - Returns a URI to the packages file /*{{{*/
  144. // ---------------------------------------------------------------------
  145. /* */
  146. string pkgSourceList::Item::PackagesURI() const
  147. {
  148. string Res;
  149. switch (Type)
  150. {
  151. case Deb:
  152. if (Dist[Dist.size() - 1] == '/')
  153. {
  154. if (Dist != "/")
  155. Res = URI + Dist;
  156. else
  157. Res = URI;
  158. }
  159. else
  160. Res = URI + "dists/" + Dist + '/' + Section +
  161. "/binary-" + _config->Find("APT::Architecture") + '/';
  162. Res += "Packages";
  163. break;
  164. case DebSrc:
  165. if (Dist[Dist.size() - 1] == '/')
  166. Res = URI + Dist;
  167. else
  168. Res = URI + "dists/" + Dist + '/' + Section +
  169. "/source/";
  170. Res += "Sources";
  171. break;
  172. };
  173. return Res;
  174. }
  175. /*}}}*/
  176. // SourceList::Item::PackagesInfo - Shorter version of the URI /*{{{*/
  177. // ---------------------------------------------------------------------
  178. /* This is a shorter version that is designed to be < 60 chars or so */
  179. string pkgSourceList::Item::PackagesInfo() const
  180. {
  181. string Res;
  182. switch (Type)
  183. {
  184. case Deb:
  185. Res += SiteOnly(URI) + ' ';
  186. if (Dist[Dist.size() - 1] == '/')
  187. {
  188. if (Dist != "/")
  189. Res += Dist;
  190. }
  191. else
  192. Res += Dist + '/' + Section;
  193. Res += " Packages";
  194. break;
  195. case DebSrc:
  196. Res += SiteOnly(URI) + ' ';
  197. if (Dist[Dist.size() - 1] == '/')
  198. Res += Dist;
  199. else
  200. Res += Dist + '/' + Section;
  201. Res += " Sources";
  202. break;
  203. };
  204. return Res;
  205. }
  206. /*}}}*/
  207. // SourceList::Item::ReleaseURI - Returns a URI to the release file /*{{{*/
  208. // ---------------------------------------------------------------------
  209. /* */
  210. string pkgSourceList::Item::ReleaseURI() const
  211. {
  212. string Res;
  213. switch (Type)
  214. {
  215. case Deb:
  216. if (Dist[Dist.size() - 1] == '/')
  217. {
  218. if (Dist != "/")
  219. Res = URI + Dist;
  220. else
  221. Res = URI;
  222. }
  223. else
  224. Res = URI + "dists/" + Dist + '/' + Section +
  225. "/binary-" + _config->Find("APT::Architecture") + '/';
  226. Res += "Release";
  227. break;
  228. case DebSrc:
  229. if (Dist[Dist.size() - 1] == '/')
  230. Res = URI + Dist;
  231. else
  232. Res = URI + "dists/" + Dist + '/' + Section +
  233. "/source/";
  234. Res += "Release";
  235. break;
  236. };
  237. return Res;
  238. }
  239. /*}}}*/
  240. // SourceList::Item::ReleaseInfo - Shorter version of the URI /*{{{*/
  241. // ---------------------------------------------------------------------
  242. /* This is a shorter version that is designed to be < 60 chars or so */
  243. string pkgSourceList::Item::ReleaseInfo() const
  244. {
  245. string Res;
  246. switch (Type)
  247. {
  248. case Deb:
  249. case DebSrc:
  250. Res += SiteOnly(URI) + ' ';
  251. if (Dist[Dist.size() - 1] == '/')
  252. {
  253. if (Dist != "/")
  254. Res += Dist;
  255. }
  256. else
  257. Res += Dist + '/' + Section;
  258. Res += " Release";
  259. break;
  260. };
  261. return Res;
  262. }
  263. /*}}}*/
  264. // SourceList::Item::ArchiveInfo - Shorter version of the archive spec /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* This is a shorter version that is designed to be < 60 chars or so */
  267. string pkgSourceList::Item::ArchiveInfo(pkgCache::VerIterator Ver) const
  268. {
  269. string Res;
  270. switch (Type)
  271. {
  272. case DebSrc:
  273. case Deb:
  274. Res += SiteOnly(URI) + ' ';
  275. if (Dist[Dist.size() - 1] == '/')
  276. {
  277. if (Dist != "/")
  278. Res += Dist;
  279. }
  280. else
  281. Res += Dist + '/' + Section;
  282. Res += " ";
  283. Res += Ver.ParentPkg().Name();
  284. Res += " ";
  285. Res += Ver.VerStr();
  286. break;
  287. };
  288. return Res;
  289. }
  290. /*}}}*/
  291. // SourceList::Item::ArchiveURI - Returns a URI to the given archive /*{{{*/
  292. // ---------------------------------------------------------------------
  293. /* */
  294. string pkgSourceList::Item::ArchiveURI(string File) const
  295. {
  296. string Res;
  297. switch (Type)
  298. {
  299. case Deb:
  300. case DebSrc:
  301. Res = URI + File;
  302. break;
  303. };
  304. return Res;
  305. }
  306. /*}}}*/
  307. // SourceList::Item::SourceInfo - Returns an info line for a source /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* */
  310. string pkgSourceList::Item::SourceInfo(string Pkg,string Ver,string Comp) const
  311. {
  312. string Res;
  313. switch (Type)
  314. {
  315. case DebSrc:
  316. case Deb:
  317. Res += SiteOnly(URI) + ' ';
  318. if (Dist[Dist.size() - 1] == '/')
  319. {
  320. if (Dist != "/")
  321. Res += Dist;
  322. }
  323. else
  324. Res += Dist + '/' + Section;
  325. Res += " ";
  326. Res += Pkg;
  327. Res += " ";
  328. Res += Ver;
  329. if (Comp.empty() == false)
  330. Res += " (" + Comp + ")";
  331. break;
  332. };
  333. return Res;
  334. }
  335. /*}}}*/
  336. // SourceList::Item::SiteOnly - Strip off the path part of a URI /*{{{*/
  337. // ---------------------------------------------------------------------
  338. /* */
  339. string pkgSourceList::Item::SiteOnly(string URI) const
  340. {
  341. ::URI U(URI);
  342. U.User = string();
  343. U.Password = string();
  344. U.Path = string();
  345. U.Port = 0;
  346. return U;
  347. }
  348. /*}}}*/