debmetaindex.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/Translation-";
  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 Translation-* files, later we will skip download of non-existent if we have an index
  188. for (std::set<std::string>::const_iterator s = sections.begin();
  189. s != sections.end(); ++s) {
  190. for (std::vector<std::string>::const_iterator l = lang.begin();
  191. l != lang.end(); ++l) {
  192. IndexTarget * Target = new OptionalIndexTarget();
  193. Target->ShortDesc = "Translation-" + *l;
  194. Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
  195. Target->URI = TranslationIndexURI(l->c_str(), *s);
  196. Target->Description = Info (Target->ShortDesc.c_str(), *s);
  197. IndexTargets->push_back(Target);
  198. }
  199. }
  200. return IndexTargets;
  201. }
  202. /*}}}*/
  203. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
  204. {
  205. bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
  206. // special case for --print-uris
  207. if (GetAll) {
  208. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  209. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
  210. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  211. (*Target)->ShortDesc, HashString());
  212. }
  213. // this is normally created in pkgAcqMetaSig, but if we run
  214. // in --print-uris mode, we add it here
  215. if (tryInRelease == false)
  216. new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
  217. MetaIndexInfo("Release"), "Release",
  218. MetaIndexURI("Release.gpg"),
  219. ComputeIndexTargets(),
  220. new indexRecords (Dist));
  221. }
  222. if (tryInRelease == true)
  223. new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
  224. MetaIndexInfo("InRelease"), "InRelease",
  225. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  226. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  227. ComputeIndexTargets(),
  228. new indexRecords (Dist));
  229. else
  230. new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
  231. MetaIndexInfo("Release.gpg"), "Release.gpg",
  232. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  233. ComputeIndexTargets(),
  234. new indexRecords (Dist));
  235. return true;
  236. }
  237. void debReleaseIndex::SetTrusted(bool const Trusted)
  238. {
  239. if (Trusted == true)
  240. this->Trusted = ALWAYS_TRUSTED;
  241. else
  242. this->Trusted = NEVER_TRUSTED;
  243. }
  244. bool debReleaseIndex::IsTrusted() const
  245. {
  246. if (Trusted == ALWAYS_TRUSTED)
  247. return true;
  248. else if (Trusted == NEVER_TRUSTED)
  249. return false;
  250. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  251. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  252. return true;
  253. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  254. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  255. if (FileExists(VerifiedSigFile))
  256. return true;
  257. VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  258. URItoFileName(MetaIndexURI("InRelease"));
  259. return FileExists(VerifiedSigFile);
  260. }
  261. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  262. if (Indexes != NULL)
  263. return Indexes;
  264. Indexes = new vector <pkgIndexFile*>;
  265. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  266. if (src != ArchEntries.end()) {
  267. vector<debSectionEntry const*> const SectionEntries = src->second;
  268. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  269. I != SectionEntries.end(); ++I)
  270. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  271. }
  272. // Only source release
  273. if (Indexes->empty() == false && ArchEntries.size() == 1)
  274. return Indexes;
  275. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  276. map<string, set<string> > sections;
  277. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  278. a != ArchEntries.end(); ++a) {
  279. if (a->first == "source")
  280. continue;
  281. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  282. I != a->second.end(); ++I) {
  283. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  284. sections[(*I)->Section].insert(lang.begin(), lang.end());
  285. }
  286. }
  287. for (map<string, set<string> >::const_iterator s = sections.begin();
  288. s != sections.end(); ++s)
  289. for (set<string>::const_iterator l = s->second.begin();
  290. l != s->second.end(); ++l) {
  291. if (*l == "none") continue;
  292. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  293. }
  294. return Indexes;
  295. }
  296. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  297. for (vector<string>::const_iterator a = Archs.begin();
  298. a != Archs.end(); ++a)
  299. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  300. delete Entry;
  301. }
  302. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  303. ArchEntries[Arch].push_back(Entry);
  304. }
  305. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  306. if (Entry->IsSrc == true)
  307. PushSectionEntry("source", Entry);
  308. else {
  309. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  310. a != ArchEntries.end(); ++a) {
  311. a->second.push_back(Entry);
  312. }
  313. }
  314. }
  315. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  316. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  317. {}
  318. class debSLTypeDebian : public pkgSourceList::Type
  319. {
  320. protected:
  321. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  322. string const &Dist, string const &Section,
  323. bool const &IsSrc, map<string, string> const &Options) const
  324. {
  325. map<string, string>::const_iterator const arch = Options.find("arch");
  326. vector<string> const Archs =
  327. (arch != Options.end()) ? VectorizeString(arch->second, ',') :
  328. APT::Configuration::getArchitectures();
  329. map<string, string>::const_iterator const trusted = Options.find("trusted");
  330. for (vector<metaIndex *>::const_iterator I = List.begin();
  331. I != List.end(); ++I)
  332. {
  333. // We only worry about debian entries here
  334. if (strcmp((*I)->GetType(), "deb") != 0)
  335. continue;
  336. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  337. if (trusted != Options.end())
  338. Deb->SetTrusted(StringToBool(trusted->second, false));
  339. /* This check insures that there will be only one Release file
  340. queued for all the Packages files and Sources files it
  341. corresponds to. */
  342. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  343. {
  344. if (IsSrc == true)
  345. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  346. else
  347. {
  348. if (Dist[Dist.size() - 1] == '/')
  349. Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  350. else
  351. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  352. }
  353. return true;
  354. }
  355. }
  356. // No currently created Release file indexes this entry, so we create a new one.
  357. debReleaseIndex *Deb;
  358. if (trusted != Options.end())
  359. Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
  360. else
  361. Deb = new debReleaseIndex(URI, Dist);
  362. if (IsSrc == true)
  363. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  364. else
  365. {
  366. if (Dist[Dist.size() - 1] == '/')
  367. Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  368. else
  369. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  370. }
  371. List.push_back(Deb);
  372. return true;
  373. }
  374. };
  375. class debSLTypeDeb : public debSLTypeDebian
  376. {
  377. public:
  378. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  379. string const &Dist, string const &Section,
  380. std::map<string, string> const &Options) const
  381. {
  382. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  383. }
  384. debSLTypeDeb()
  385. {
  386. Name = "deb";
  387. Label = "Standard Debian binary tree";
  388. }
  389. };
  390. class debSLTypeDebSrc : public debSLTypeDebian
  391. {
  392. public:
  393. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  394. string const &Dist, string const &Section,
  395. std::map<string, string> const &Options) const
  396. {
  397. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  398. }
  399. debSLTypeDebSrc()
  400. {
  401. Name = "deb-src";
  402. Label = "Standard Debian source tree";
  403. }
  404. };
  405. debSLTypeDeb _apt_DebType;
  406. debSLTypeDebSrc _apt_DebSrcType;