debmetaindex.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // ijones, walters
  2. #include <apt-pkg/debmetaindex.h>
  3. #include <apt-pkg/debindexfile.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apt-pkg/acquire-item.h>
  6. #include <apt-pkg/configuration.h>
  7. #include <apt-pkg/aptconfiguration.h>
  8. #include <apt-pkg/error.h>
  9. #include <set>
  10. using namespace std;
  11. string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
  12. {
  13. string Info = ::URI::SiteOnly(URI) + ' ';
  14. if (Dist[Dist.size() - 1] == '/')
  15. {
  16. if (Dist != "/")
  17. Info += Dist;
  18. }
  19. else
  20. {
  21. Info += Dist + '/' + Section;
  22. if (Arch.empty() == true)
  23. Info += " " + Arch;
  24. }
  25. Info += " ";
  26. Info += Type;
  27. return Info;
  28. }
  29. string debReleaseIndex::MetaIndexInfo(const char *Type) const
  30. {
  31. string Info = ::URI::SiteOnly(URI) + ' ';
  32. if (Dist[Dist.size() - 1] == '/')
  33. {
  34. if (Dist != "/")
  35. Info += Dist;
  36. }
  37. else
  38. Info += Dist;
  39. Info += " ";
  40. Info += Type;
  41. return Info;
  42. }
  43. string debReleaseIndex::MetaIndexFile(const char *Type) const
  44. {
  45. return _config->FindDir("Dir::State::lists") +
  46. URItoFileName(MetaIndexURI(Type));
  47. }
  48. string debReleaseIndex::MetaIndexURI(const char *Type) const
  49. {
  50. string Res;
  51. if (Dist == "/")
  52. Res = URI;
  53. else if (Dist[Dist.size()-1] == '/')
  54. Res = URI + Dist;
  55. else
  56. Res = URI + "dists/" + Dist + "/";
  57. Res += Type;
  58. return Res;
  59. }
  60. string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
  61. {
  62. string Res ="";
  63. if (Dist[Dist.size() - 1] != '/')
  64. {
  65. if (Arch == "native")
  66. Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
  67. else
  68. Res += Section + "/binary-" + Arch + '/';
  69. }
  70. return Res + Type;
  71. }
  72. string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
  73. {
  74. if (Dist[Dist.size() - 1] == '/')
  75. {
  76. string Res;
  77. if (Dist != "/")
  78. Res = URI + Dist;
  79. else
  80. Res = URI;
  81. return Res + Type;
  82. }
  83. else
  84. return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
  85. }
  86. string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
  87. {
  88. string Res ="";
  89. if (Dist[Dist.size() - 1] != '/')
  90. Res += Section + "/source/";
  91. return Res + Type;
  92. }
  93. string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
  94. {
  95. string Res;
  96. if (Dist[Dist.size() - 1] == '/')
  97. {
  98. if (Dist != "/")
  99. Res = URI + Dist;
  100. else
  101. Res = URI;
  102. return Res + Type;
  103. }
  104. else
  105. return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
  106. }
  107. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) {
  108. this->URI = URI;
  109. this->Dist = Dist;
  110. this->Indexes = NULL;
  111. this->Type = "deb";
  112. }
  113. debReleaseIndex::~debReleaseIndex() {
  114. for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
  115. A != ArchEntries.end(); ++A)
  116. for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
  117. S != A->second.end(); ++S)
  118. delete *S;
  119. }
  120. vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
  121. vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
  122. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  123. if (src != ArchEntries.end()) {
  124. vector<debSectionEntry const*> const SectionEntries = src->second;
  125. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  126. I != SectionEntries.end(); ++I) {
  127. IndexTarget * Target = new IndexTarget();
  128. Target->ShortDesc = "Sources";
  129. Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
  130. Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
  131. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
  132. IndexTargets->push_back (Target);
  133. }
  134. }
  135. // Only source release
  136. if (IndexTargets->empty() == false && ArchEntries.size() == 1)
  137. return IndexTargets;
  138. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  139. a != ArchEntries.end(); ++a) {
  140. if (a->first == "source")
  141. continue;
  142. for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
  143. I != a->second.end(); ++I) {
  144. IndexTarget * Target = new IndexTarget();
  145. Target->ShortDesc = "Packages";
  146. Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  147. Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  148. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
  149. IndexTargets->push_back (Target);
  150. }
  151. }
  152. return IndexTargets;
  153. }
  154. /*}}}*/
  155. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
  156. {
  157. // special case for --print-uris
  158. if (GetAll) {
  159. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  160. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
  161. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  162. (*Target)->ShortDesc, HashString());
  163. }
  164. // this is normally created in pkgAcqMetaSig, but if we run
  165. // in --print-uris mode, we add it here
  166. new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
  167. MetaIndexInfo("Release"), "Release",
  168. MetaIndexURI("Release.gpg"),
  169. ComputeIndexTargets(),
  170. new indexRecords (Dist));
  171. }
  172. new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
  173. MetaIndexInfo("Release.gpg"), "Release.gpg",
  174. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  175. ComputeIndexTargets(),
  176. new indexRecords (Dist));
  177. // Queue the translations
  178. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  179. map<string, set<string> > sections;
  180. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  181. a != ArchEntries.end(); ++a) {
  182. if (a->first == "source")
  183. continue;
  184. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  185. I != a->second.end(); I++)
  186. sections[(*I)->Section].insert(lang.begin(), lang.end());
  187. }
  188. for (map<string, set<string> >::const_iterator s = sections.begin();
  189. s != sections.end(); ++s)
  190. for (set<string>::const_iterator l = s->second.begin();
  191. l != s->second.end(); l++) {
  192. debTranslationsIndex i = debTranslationsIndex(URI,Dist,s->first,(*l).c_str());
  193. i.GetIndexes(Owner);
  194. }
  195. return true;
  196. }
  197. bool debReleaseIndex::IsTrusted() const
  198. {
  199. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  200. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  201. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  202. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  203. return true;
  204. if (FileExists(VerifiedSigFile))
  205. return true;
  206. return false;
  207. }
  208. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  209. if (Indexes != NULL)
  210. return Indexes;
  211. Indexes = new vector <pkgIndexFile*>;
  212. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  213. if (src != ArchEntries.end()) {
  214. vector<debSectionEntry const*> const SectionEntries = src->second;
  215. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  216. I != SectionEntries.end(); I++)
  217. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  218. }
  219. // Only source release
  220. if (Indexes->empty() == false && ArchEntries.size() == 1)
  221. return Indexes;
  222. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  223. map<string, set<string> > sections;
  224. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  225. a != ArchEntries.end(); ++a) {
  226. if (a->first == "source")
  227. continue;
  228. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  229. I != a->second.end(); I++) {
  230. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  231. sections[(*I)->Section].insert(lang.begin(), lang.end());
  232. }
  233. }
  234. for (map<string, set<string> >::const_iterator s = sections.begin();
  235. s != sections.end(); ++s)
  236. for (set<string>::const_iterator l = s->second.begin();
  237. l != s->second.end(); l++)
  238. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  239. return Indexes;
  240. }
  241. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  242. for (vector<string>::const_iterator a = Archs.begin();
  243. a != Archs.end(); ++a)
  244. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  245. delete Entry;
  246. }
  247. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  248. ArchEntries[Arch].push_back(Entry);
  249. }
  250. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  251. if (Entry->IsSrc == true)
  252. PushSectionEntry("source", Entry);
  253. else {
  254. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  255. a != ArchEntries.end(); ++a) {
  256. a->second.push_back(Entry);
  257. }
  258. }
  259. }
  260. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  261. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  262. {}
  263. class debSLTypeDebian : public pkgSourceList::Type
  264. {
  265. protected:
  266. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  267. string const &Dist, string const &Section,
  268. bool const &IsSrc, map<string, string> const &Options) const
  269. {
  270. map<string, string>::const_iterator const arch = Options.find("arch");
  271. vector<string> const Archs =
  272. (arch != Options.end()) ? ExplodeString(arch->second) :
  273. APT::Configuration::getArchitectures();
  274. for (vector<metaIndex *>::const_iterator I = List.begin();
  275. I != List.end(); I++)
  276. {
  277. // We only worry about debian entries here
  278. if (strcmp((*I)->GetType(), "deb") != 0)
  279. continue;
  280. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  281. /* This check insures that there will be only one Release file
  282. queued for all the Packages files and Sources files it
  283. corresponds to. */
  284. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  285. {
  286. if (IsSrc == true)
  287. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  288. else
  289. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  290. return true;
  291. }
  292. }
  293. // No currently created Release file indexes this entry, so we create a new one.
  294. // XXX determine whether this release is trusted or not
  295. debReleaseIndex *Deb = new debReleaseIndex(URI, Dist);
  296. if (IsSrc == true)
  297. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  298. else
  299. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  300. List.push_back(Deb);
  301. return true;
  302. }
  303. };
  304. class debSLTypeDeb : public debSLTypeDebian
  305. {
  306. public:
  307. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  308. string const &Dist, string const &Section,
  309. std::map<string, string> const &Options) const
  310. {
  311. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  312. }
  313. debSLTypeDeb()
  314. {
  315. Name = "deb";
  316. Label = "Standard Debian binary tree";
  317. }
  318. };
  319. class debSLTypeDebSrc : public debSLTypeDebian
  320. {
  321. public:
  322. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  323. string const &Dist, string const &Section,
  324. std::map<string, string> const &Options) const
  325. {
  326. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  327. }
  328. debSLTypeDebSrc()
  329. {
  330. Name = "deb-src";
  331. Label = "Standard Debian source tree";
  332. }
  333. };
  334. debSLTypeDeb _apt_DebType;
  335. debSLTypeDebSrc _apt_DebSrcType;