debmetaindex.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // ijones, walters
  2. #include <config.h>
  3. #include <apt-pkg/debmetaindex.h>
  4. #include <apt-pkg/debindexfile.h>
  5. #include <apt-pkg/strutl.h>
  6. #include <apt-pkg/acquire-item.h>
  7. #include <apt-pkg/configuration.h>
  8. #include <apt-pkg/aptconfiguration.h>
  9. #include <apt-pkg/error.h>
  10. #include <set>
  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. metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
  131. {}
  132. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
  133. metaIndex(URI, Dist, "deb") {
  134. SetTrusted(Trusted);
  135. }
  136. debReleaseIndex::~debReleaseIndex() {
  137. for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
  138. A != ArchEntries.end(); ++A)
  139. for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
  140. S != A->second.end(); ++S)
  141. delete *S;
  142. }
  143. vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
  144. vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
  145. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  146. if (src != ArchEntries.end()) {
  147. vector<debSectionEntry const*> const SectionEntries = src->second;
  148. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  149. I != SectionEntries.end(); ++I) {
  150. IndexTarget * Target = new IndexTarget();
  151. Target->ShortDesc = "Sources";
  152. Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
  153. Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
  154. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
  155. IndexTargets->push_back (Target);
  156. }
  157. }
  158. // Only source release
  159. if (IndexTargets->empty() == false && ArchEntries.size() == 1)
  160. return IndexTargets;
  161. std::set<std::string> sections;
  162. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  163. a != ArchEntries.end(); ++a) {
  164. if (a->first == "source")
  165. continue;
  166. for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
  167. I != a->second.end(); ++I) {
  168. IndexTarget * Target = new IndexTarget();
  169. Target->ShortDesc = "Packages";
  170. Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  171. Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  172. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
  173. IndexTargets->push_back (Target);
  174. sections.insert((*I)->Section);
  175. }
  176. }
  177. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  178. if (lang.empty() == true)
  179. return IndexTargets;
  180. // get the Translations:
  181. // - if its a dists-style repository get the i18n/Index first
  182. // - if its flat try to acquire files by guessing
  183. if (Dist[Dist.size() - 1] == '/') {
  184. for (std::set<std::string>::const_iterator s = sections.begin();
  185. s != sections.end(); ++s) {
  186. for (std::vector<std::string>::const_iterator l = lang.begin();
  187. l != lang.end(); ++l) {
  188. if (*l == "none") continue;
  189. IndexTarget * Target = new OptionalIndexTarget();
  190. Target->ShortDesc = "Translation-" + *l;
  191. Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
  192. Target->URI = TranslationIndexURI(l->c_str(), *s);
  193. Target->Description = Info (Target->ShortDesc.c_str(), *s);
  194. IndexTargets->push_back(Target);
  195. }
  196. }
  197. } else {
  198. for (std::set<std::string>::const_iterator s = sections.begin();
  199. s != sections.end(); ++s) {
  200. IndexTarget * Target = new OptionalIndexTarget();
  201. Target->ShortDesc = "TranslationIndex";
  202. Target->MetaKey = TranslationIndexURISuffix("Index", *s);
  203. Target->URI = TranslationIndexURI("Index", *s);
  204. Target->Description = Info (Target->ShortDesc.c_str(), *s);
  205. IndexTargets->push_back (Target);
  206. }
  207. }
  208. return IndexTargets;
  209. }
  210. /*}}}*/
  211. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
  212. {
  213. // special case for --print-uris
  214. if (GetAll) {
  215. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  216. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
  217. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  218. (*Target)->ShortDesc, HashString());
  219. }
  220. }
  221. new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
  222. MetaIndexInfo("InRelease"), "InRelease",
  223. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  224. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  225. ComputeIndexTargets(),
  226. new indexRecords (Dist));
  227. return true;
  228. }
  229. void debReleaseIndex::SetTrusted(bool const Trusted)
  230. {
  231. if (Trusted == true)
  232. this->Trusted = ALWAYS_TRUSTED;
  233. else
  234. this->Trusted = NEVER_TRUSTED;
  235. }
  236. bool debReleaseIndex::IsTrusted() const
  237. {
  238. if (Trusted == ALWAYS_TRUSTED)
  239. return true;
  240. else if (Trusted == NEVER_TRUSTED)
  241. return false;
  242. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  243. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  244. return true;
  245. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  246. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  247. if (FileExists(VerifiedSigFile))
  248. return true;
  249. VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  250. URItoFileName(MetaIndexURI("InRelease"));
  251. return FileExists(VerifiedSigFile);
  252. }
  253. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  254. if (Indexes != NULL)
  255. return Indexes;
  256. Indexes = new vector <pkgIndexFile*>;
  257. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  258. if (src != ArchEntries.end()) {
  259. vector<debSectionEntry const*> const SectionEntries = src->second;
  260. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  261. I != SectionEntries.end(); ++I)
  262. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  263. }
  264. // Only source release
  265. if (Indexes->empty() == false && ArchEntries.size() == 1)
  266. return Indexes;
  267. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  268. map<string, set<string> > sections;
  269. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  270. a != ArchEntries.end(); ++a) {
  271. if (a->first == "source")
  272. continue;
  273. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  274. I != a->second.end(); ++I) {
  275. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  276. sections[(*I)->Section].insert(lang.begin(), lang.end());
  277. }
  278. }
  279. for (map<string, set<string> >::const_iterator s = sections.begin();
  280. s != sections.end(); ++s)
  281. for (set<string>::const_iterator l = s->second.begin();
  282. l != s->second.end(); ++l) {
  283. if (*l == "none") continue;
  284. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  285. }
  286. return Indexes;
  287. }
  288. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  289. for (vector<string>::const_iterator a = Archs.begin();
  290. a != Archs.end(); ++a)
  291. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  292. delete Entry;
  293. }
  294. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  295. ArchEntries[Arch].push_back(Entry);
  296. }
  297. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  298. if (Entry->IsSrc == true)
  299. PushSectionEntry("source", Entry);
  300. else {
  301. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  302. a != ArchEntries.end(); ++a) {
  303. a->second.push_back(Entry);
  304. }
  305. }
  306. }
  307. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  308. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  309. {}
  310. class debSLTypeDebian : public pkgSourceList::Type
  311. {
  312. protected:
  313. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  314. string const &Dist, string const &Section,
  315. bool const &IsSrc, map<string, string> const &Options) const
  316. {
  317. map<string, string>::const_iterator const arch = Options.find("arch");
  318. vector<string> const Archs =
  319. (arch != Options.end()) ? VectorizeString(arch->second, ',') :
  320. APT::Configuration::getArchitectures();
  321. map<string, string>::const_iterator const trusted = Options.find("trusted");
  322. for (vector<metaIndex *>::const_iterator I = List.begin();
  323. I != List.end(); ++I)
  324. {
  325. // We only worry about debian entries here
  326. if (strcmp((*I)->GetType(), "deb") != 0)
  327. continue;
  328. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  329. if (trusted != Options.end())
  330. Deb->SetTrusted(StringToBool(trusted->second, false));
  331. /* This check insures that there will be only one Release file
  332. queued for all the Packages files and Sources files it
  333. corresponds to. */
  334. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  335. {
  336. if (IsSrc == true)
  337. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  338. else
  339. {
  340. if (Dist[Dist.size() - 1] == '/')
  341. Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  342. else
  343. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  344. }
  345. return true;
  346. }
  347. }
  348. // No currently created Release file indexes this entry, so we create a new one.
  349. debReleaseIndex *Deb;
  350. if (trusted != Options.end())
  351. Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
  352. else
  353. Deb = new debReleaseIndex(URI, Dist);
  354. if (IsSrc == true)
  355. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  356. else
  357. {
  358. if (Dist[Dist.size() - 1] == '/')
  359. Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  360. else
  361. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  362. }
  363. List.push_back(Deb);
  364. return true;
  365. }
  366. };
  367. class debSLTypeDeb : public debSLTypeDebian
  368. {
  369. public:
  370. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  371. string const &Dist, string const &Section,
  372. std::map<string, string> const &Options) const
  373. {
  374. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  375. }
  376. debSLTypeDeb()
  377. {
  378. Name = "deb";
  379. Label = "Standard Debian binary tree";
  380. }
  381. };
  382. class debSLTypeDebSrc : public debSLTypeDebian
  383. {
  384. public:
  385. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  386. string const &Dist, string const &Section,
  387. std::map<string, string> const &Options) const
  388. {
  389. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  390. }
  391. debSLTypeDebSrc()
  392. {
  393. Name = "deb-src";
  394. Label = "Standard Debian source tree";
  395. }
  396. };
  397. debSLTypeDeb _apt_DebType;
  398. debSLTypeDebSrc _apt_DebSrcType;