debmetaindex.cc 14 KB

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