debmetaindex.cc 13 KB

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